summaryrefslogtreecommitdiffstats
path: root/logsmanagement/rrd_api/rrd_api_stats.c
blob: e845d04176ebebe897c19130465dfa0d1f93df04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rrd_api_stats.h"

static const char *const rrd_type = "netdata";

static char **dim_db_timings_write, **dim_db_timings_rotate;

extern bool logsmanagement_should_exit;

static void stats_charts_update(void){

    /* Circular buffer total memory stats - update */
    lgs_mng_update_chart_begin(rrd_type, "circular_buffers_mem_total_cached");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue; 

        lgs_mng_update_chart_set(p_file_info->chartname, 
            __atomic_load_n(&p_file_info->circ_buff->total_cached_mem, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

    /* Circular buffer number of items - update */
    lgs_mng_update_chart_begin(rrd_type, "circular_buffers_num_of_items");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(p_file_info->chartname, p_file_info->circ_buff->num_of_items);
    }
    lgs_mng_update_chart_end(0);

    /* Circular buffer uncompressed buffered items memory stats - update */
    lgs_mng_update_chart_begin(rrd_type, "circular_buffers_mem_uncompressed_used");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(p_file_info->chartname, 
            __atomic_load_n(&p_file_info->circ_buff->text_size_total, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

    /* Circular buffer compressed buffered items memory stats - update */
    lgs_mng_update_chart_begin(rrd_type, "circular_buffers_mem_compressed_used");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(p_file_info->chartname, 
            __atomic_load_n(&p_file_info->circ_buff->text_compressed_size_total, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

    /* Compression stats - update */
    lgs_mng_update_chart_begin(rrd_type, "average_compression_ratio");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(p_file_info->chartname, 
            __atomic_load_n(&p_file_info->circ_buff->compression_ratio, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

    /* DB disk usage stats - update */
    lgs_mng_update_chart_begin(rrd_type, "database_disk_usage");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(p_file_info->chartname, 
            __atomic_load_n(&p_file_info->blob_total_size, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

    /* DB timings - update */
    lgs_mng_update_chart_begin(rrd_type, "database_timings");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(dim_db_timings_write[i], 
            __atomic_exchange_n(&p_file_info->db_write_duration, 0, __ATOMIC_RELAXED));
        
        lgs_mng_update_chart_set(dim_db_timings_rotate[i], 
            __atomic_exchange_n(&p_file_info->db_rotate_duration, 0, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

    /* Query CPU time per byte (user) - update */
    lgs_mng_update_chart_begin(rrd_type, "query_cpu_time_per_MiB_user");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(p_file_info->chartname, 
            __atomic_load_n(&p_file_info->cpu_time_per_mib.user, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

    /* Query CPU time per byte (user) - update */
    lgs_mng_update_chart_begin(rrd_type, "query_cpu_time_per_MiB_sys");
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];
        if(!p_file_info->parser_config) 
            continue;
        
        lgs_mng_update_chart_set(p_file_info->chartname, 
            __atomic_load_n(&p_file_info->cpu_time_per_mib.sys, __ATOMIC_RELAXED));
    }
    lgs_mng_update_chart_end(0);

}

void stats_charts_init(void *arg){

    netdata_mutex_t *p_stdout_mut = (netdata_mutex_t *) arg;

    netdata_mutex_lock(p_stdout_mut);

    int chart_prio = NETDATA_CHART_PRIO_LOGS_STATS_BASE;

    /* Circular buffer total memory stats - initialise */
    lgs_mng_create_chart(
        rrd_type                                    // type
        , "circular_buffers_mem_total_cached"       // id
        , "Circular buffers total cached memory"    // title
        , "bytes"                                   // units
        , "logsmanagement"                          // family
        , NULL                                      // context
        , RRDSET_TYPE_STACKED_NAME                  // chart_type
        , ++chart_prio                              // priority
        , g_logs_manag_config.update_every          // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);

    /* Circular buffer number of items - initialise */
    lgs_mng_create_chart(
        rrd_type                                // type
        , "circular_buffers_num_of_items"       // id
        , "Circular buffers number of items"    // title
        , "items"                               // units
        , "logsmanagement"                      // family
        , NULL                                  // context
        , RRDSET_TYPE_LINE_NAME                 // chart_type
        , ++chart_prio                          // priority
        , g_logs_manag_config.update_every      // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);
        
    /* Circular buffer uncompressed buffered items memory stats - initialise */
    lgs_mng_create_chart(
        rrd_type                                                // type
        , "circular_buffers_mem_uncompressed_used"              // id
        , "Circular buffers used memory for uncompressed logs"  // title
        , "bytes"                                               // units
        , "logsmanagement"                                      // family
        , NULL                                                  // context
        , RRDSET_TYPE_STACKED_NAME                              // chart_type
        , ++chart_prio                                          // priority
        , g_logs_manag_config.update_every                      // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);

    /* Circular buffer compressed buffered items memory stats - initialise */
    lgs_mng_create_chart(
        rrd_type                                                // type
        , "circular_buffers_mem_compressed_used"                // id
        , "Circular buffers used memory for compressed logs"    // title
        , "bytes"                                               // units
        , "logsmanagement"                                      // family
        , NULL                                                  // context
        , RRDSET_TYPE_STACKED_NAME                              // chart_type
        , ++chart_prio                                          // priority
        , g_logs_manag_config.update_every                      // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);

    /* Compression stats - initialise */
    lgs_mng_create_chart(
        rrd_type                                // type
        , "average_compression_ratio"           // id
        , "Average compression ratio"           // title
        , "uncompressed / compressed ratio"     // units
        , "logsmanagement"                      // family
        , NULL                                  // context
        , RRDSET_TYPE_LINE_NAME                 // chart_type
        , ++chart_prio                          // priority
        , g_logs_manag_config.update_every      // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);

    /* DB disk usage stats - initialise */
    lgs_mng_create_chart(
        rrd_type                            // type
        , "database_disk_usage"             // id
        , "Database disk usage"             // title
        , "bytes"                           // units
        , "logsmanagement"                  // family
        , NULL                              // context
        , RRDSET_TYPE_STACKED_NAME          // chart_type
        , ++chart_prio                      // priority
        , g_logs_manag_config.update_every  // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);

    /* DB timings - initialise */
    lgs_mng_create_chart(
        rrd_type                            // type
        , "database_timings"                // id
        , "Database timings"                // title
        , "ns"                              // units
        , "logsmanagement"                  // family
        , NULL                              // context
        , RRDSET_TYPE_STACKED_NAME          // chart_type
        , ++chart_prio                      // priority
        , g_logs_manag_config.update_every  // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++){
        struct File_info *p_file_info = p_file_infos_arr->data[i];

        dim_db_timings_write = reallocz(dim_db_timings_write, (i + 1) * sizeof(char *));
        dim_db_timings_rotate = reallocz(dim_db_timings_rotate, (i + 1) * sizeof(char *));

        dim_db_timings_write[i] = mallocz(snprintf(NULL, 0, "%s_write", p_file_info->chartname) + 1);
        sprintf(dim_db_timings_write[i], "%s_write", p_file_info->chartname);
        lgs_mng_add_dim(dim_db_timings_write[i], RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);

        dim_db_timings_rotate[i] = mallocz(snprintf(NULL, 0, "%s_rotate", p_file_info->chartname) + 1);
        sprintf(dim_db_timings_rotate[i], "%s_rotate", p_file_info->chartname);
        lgs_mng_add_dim(dim_db_timings_rotate[i], RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1);
    }

    /* Query CPU time per byte (user) - initialise */
    lgs_mng_create_chart(
        rrd_type                                    // type
        , "query_cpu_time_per_MiB_user"             // id
        , "CPU user time per MiB of query results"  // title
        , "usec/MiB"                                // units
        , "logsmanagement"                          // family
        , NULL                                      // context
        , RRDSET_TYPE_STACKED_NAME                  // chart_type
        , ++chart_prio                              // priority
        , g_logs_manag_config.update_every          // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_INCREMENTAL_NAME, 1, 1);

    /* Query CPU time per byte (system) - initialise */
    lgs_mng_create_chart(
        rrd_type                                        // type
        , "query_cpu_time_per_MiB_sys"                  // id
        , "CPU system time per MiB of query results"    // title
        , "usec/MiB"                                    // units
        , "logsmanagement"                              // family
        , NULL                                          // context
        , RRDSET_TYPE_STACKED_NAME                      // chart_type
        , ++chart_prio                                  // priority
        , g_logs_manag_config.update_every              // update_every
    );
    for(int i = 0; i < p_file_infos_arr->count; i++)
        lgs_mng_add_dim(p_file_infos_arr->data[i]->chartname, RRD_ALGORITHM_INCREMENTAL_NAME, 1, 1);

    netdata_mutex_unlock(p_stdout_mut);


    heartbeat_t hb;
    heartbeat_init(&hb);
    usec_t step_ut = g_logs_manag_config.update_every * USEC_PER_SEC;

    while (0 == __atomic_load_n(&logsmanagement_should_exit, __ATOMIC_RELAXED)) {
        heartbeat_next(&hb, step_ut);

        netdata_mutex_lock(p_stdout_mut);
        stats_charts_update();
        fflush(stdout);
        netdata_mutex_unlock(p_stdout_mut);
    }

    collector_info("[stats charts]: thread exiting...");
}