summaryrefslogtreecommitdiffstats
path: root/src/global_statistics.c
blob: bb2b1f08f7bf6dc6941e17766d34d39dff183627 (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
#include "common.h"

volatile struct global_statistics global_statistics = {
        .connected_clients = 0,
        .web_requests = 0,
        .web_usec = 0,
        .bytes_received = 0,
        .bytes_sent = 0,
        .content_size = 0,
        .compressed_content_size = 0
};

pthread_mutex_t global_statistics_mutex = PTHREAD_MUTEX_INITIALIZER;

inline void global_statistics_lock(void) {
    pthread_mutex_lock(&global_statistics_mutex);
}

inline void global_statistics_unlock(void) {
    pthread_mutex_unlock(&global_statistics_mutex);
}

void finished_web_request_statistics(uint64_t dt,
                                     uint64_t bytes_received,
                                     uint64_t bytes_sent,
                                     uint64_t content_size,
                                     uint64_t compressed_content_size) {
#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
    uint64_t old_web_usec_max = global_statistics.web_usec_max;
    while(dt > old_web_usec_max)
        __atomic_compare_exchange(&global_statistics.web_usec_max, &old_web_usec_max, &dt, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);

    __atomic_fetch_add(&global_statistics.web_requests, 1, __ATOMIC_SEQ_CST);
    __atomic_fetch_add(&global_statistics.web_usec, dt, __ATOMIC_SEQ_CST);
    __atomic_fetch_add(&global_statistics.bytes_received, bytes_received, __ATOMIC_SEQ_CST);
    __atomic_fetch_add(&global_statistics.bytes_sent, bytes_sent, __ATOMIC_SEQ_CST);
    __atomic_fetch_add(&global_statistics.content_size, content_size, __ATOMIC_SEQ_CST);
    __atomic_fetch_add(&global_statistics.compressed_content_size, compressed_content_size, __ATOMIC_SEQ_CST);
#else
#warning NOT using atomic operations - using locks for global statistics
    if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
        global_statistics_lock();

    if (dt > global_statistics.web_usec_max)
        global_statistics.web_usec_max = dt;

    global_statistics.web_requests++;
    global_statistics.web_usec += dt;
    global_statistics.bytes_received += bytes_received;
    global_statistics.bytes_sent += bytes_sent;
    global_statistics.content_size += content_size;
    global_statistics.compressed_content_size += compressed_content_size;

    if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
        global_statistics_unlock();
#endif
}

void web_client_connected(void) {
#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
    __atomic_fetch_add(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST);
#else
    if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
        global_statistics_lock();

    global_statistics.connected_clients++;

    if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
        global_statistics_unlock();
#endif
}

void web_client_disconnected(void) {
#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
    __atomic_fetch_sub(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST);
#else
    if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
        global_statistics_lock();

    global_statistics.connected_clients--;

    if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
        global_statistics_unlock();
#endif
}


inline void global_statistics_copy(struct global_statistics *gs, uint8_t options) {
#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
    gs->connected_clients       = __atomic_fetch_add(&global_statistics.connected_clients, 0, __ATOMIC_SEQ_CST);
    gs->web_requests            = __atomic_fetch_add(&global_statistics.web_requests, 0, __ATOMIC_SEQ_CST);
    gs->web_usec                = __atomic_fetch_add(&global_statistics.web_usec, 0, __ATOMIC_SEQ_CST);
    gs->web_usec_max            = __atomic_fetch_add(&global_statistics.web_usec_max, 0, __ATOMIC_SEQ_CST);
    gs->bytes_received          = __atomic_fetch_add(&global_statistics.bytes_received, 0, __ATOMIC_SEQ_CST);
    gs->bytes_sent              = __atomic_fetch_add(&global_statistics.bytes_sent, 0, __ATOMIC_SEQ_CST);
    gs->content_size            = __atomic_fetch_add(&global_statistics.content_size, 0, __ATOMIC_SEQ_CST);
    gs->compressed_content_size = __atomic_fetch_add(&global_statistics.compressed_content_size, 0, __ATOMIC_SEQ_CST);

    if(options & GLOBAL_STATS_RESET_WEB_USEC_MAX) {
        uint64_t n = 0;
        __atomic_compare_exchange(&global_statistics.web_usec_max, &gs->web_usec_max, &n, 1, __ATOMIC_SEQ_CST,
                                  __ATOMIC_SEQ_CST);
    }
#else
    global_statistics_lock();

    memcpy(gs, (const void *)&global_statistics, sizeof(struct global_statistics));

    if (options & GLOBAL_STATS_RESET_WEB_USEC_MAX)
        global_statistics.web_usec_max = 0;

    global_statistics_unlock();
#endif
}

void global_statistics_charts(void) {
    static unsigned long long old_web_requests = 0, old_web_usec = 0,
            old_content_size = 0, old_compressed_content_size = 0;

    static collected_number compression_ratio = -1, average_response_time = -1;

    static RRDSET *stcpu = NULL, *stcpu_thread = NULL, *stclients = NULL, *streqs = NULL, *stbytes = NULL, *stduration = NULL,
            *stcompression = NULL;

    struct global_statistics gs;
    struct rusage me, thread;

    global_statistics_copy(&gs, GLOBAL_STATS_RESET_WEB_USEC_MAX);
    getrusage(RUSAGE_THREAD, &thread);
    getrusage(RUSAGE_SELF, &me);

    if (!stcpu_thread) stcpu_thread = rrdset_find("netdata.plugin_proc_cpu");
    if (!stcpu_thread) {
        stcpu_thread = rrdset_create("netdata", "plugin_proc_cpu", NULL, "proc.internal", NULL,
                                     "NetData Proc Plugin CPU usage", "milliseconds/s", 132000, rrd_update_every,
                                     RRDSET_TYPE_STACKED);

        rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
        rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
    } else rrdset_next(stcpu_thread);

    rrddim_set(stcpu_thread, "user", thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
    rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
    rrdset_done(stcpu_thread);

    // ----------------------------------------------------------------

    if (!stcpu) stcpu = rrdset_find("netdata.server_cpu");
    if (!stcpu) {
        stcpu = rrdset_create("netdata", "server_cpu", NULL, "netdata", NULL, "NetData CPU usage", "milliseconds/s",
                              130000, rrd_update_every, RRDSET_TYPE_STACKED);

        rrddim_add(stcpu, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
        rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
    } else rrdset_next(stcpu);

    rrddim_set(stcpu, "user", me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec);
    rrddim_set(stcpu, "system", me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec);
    rrdset_done(stcpu);

    // ----------------------------------------------------------------

    if (!stclients) stclients = rrdset_find("netdata.clients");
    if (!stclients) {
        stclients = rrdset_create("netdata", "clients", NULL, "netdata", NULL, "NetData Web Clients",
                                  "connected clients", 130200, rrd_update_every, RRDSET_TYPE_LINE);

        rrddim_add(stclients, "clients", NULL, 1, 1, RRDDIM_ABSOLUTE);
    } else rrdset_next(stclients);

    rrddim_set(stclients, "clients", gs.connected_clients);
    rrdset_done(stclients);

    // ----------------------------------------------------------------

    if (!streqs) streqs = rrdset_find("netdata.requests");
    if (!streqs) {
        streqs = rrdset_create("netdata", "requests", NULL, "netdata", NULL, "NetData Web Requests", "requests/s",
                               130300, rrd_update_every, RRDSET_TYPE_LINE);

        rrddim_add(streqs, "requests", NULL, 1, 1, RRDDIM_INCREMENTAL);
    } else rrdset_next(streqs);

    rrddim_set(streqs, "requests", (collected_number) gs.web_requests);
    rrdset_done(streqs);

    // ----------------------------------------------------------------

    if (!stbytes) stbytes = rrdset_find("netdata.net");
    if (!stbytes