-
Notifications
You must be signed in to change notification settings - Fork 33
/
cproxy_stats.c
569 lines (478 loc) · 16.9 KB
/
cproxy_stats.c
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>
#include "memcached.h"
#include "cproxy.h"
#include "work.h"
#include "log.h"
// Protocol STATS command handling.
//
// Special STATS value merging rules, instead of the
// default to just sum the values. Note the trailing space.
//
char *protocol_stats_keys_first =
"pid version libevent "
"ep_version ep_dbname ep_storage_type ep_flusher_state ep_warmup_thread ";
char *protocol_stats_keys_smallest =
"uptime "
"time "
"pointer_size "
"limit_maxbytes "
"accepting_conns "
":chunk_size "
":chunk_per_page "
":age "; // TODO: Should age merge be largest, not smallest?
bool protocol_stats_merge_sum(char *v1, int v1len,
char *v2, int v2len,
char *out, int outlen);
bool protocol_stats_merge_smallest(char *v1, int v1len,
char *v2, int v2len,
char *out, int outlen);
int count_dot_pair(char *x, int xlen, char *y, int ylen);
int count_dot(char *x, int len);
// Per-key stats.
//
static char *key_stats_key(void *it);
static int key_stats_key_len(void *it);
static int key_stats_len(void *it);
static void *key_stats_get_next(void *it);
static void key_stats_set_next(void *it, void *next);
static void *key_stats_get_prev(void *it);
static void key_stats_set_prev(void *it, void *prev);
static uint32_t key_stats_get_exptime(void *it);
static void key_stats_set_exptime(void *it, uint32_t exptime);
mcache_funcs mcache_key_stats_funcs = {
.item_key = key_stats_key,
.item_key_len = key_stats_key_len,
.item_len = key_stats_len,
.item_add_ref = key_stats_add_ref,
.item_dec_ref = key_stats_dec_ref,
.item_get_next = key_stats_get_next,
.item_set_next = key_stats_set_next,
.item_get_prev = key_stats_get_prev,
.item_set_prev = key_stats_set_prev,
.item_get_exptime = key_stats_get_exptime,
.item_set_exptime = key_stats_set_exptime
};
#define MAX_TOKENS 5
#define PREFIX_TOKEN 0
#define NAME_TOKEN 1
#define VALUE_TOKEN 2
#define MERGE_BUF_SIZE 300
bool protocol_stats_merge_line(genhash_t *merger, char *line) {
assert(merger != NULL);
assert(line != NULL);
int nline = strlen(line); // Ex: "STATS uptime 123455"
if (nline <= 0 ||
nline >= MERGE_BUF_SIZE) {
return false;
}
token_t tokens[MAX_TOKENS];
size_t ntokens = scan_tokens(line, tokens, MAX_TOKENS, NULL);
if (ntokens != 4) { // 3 + 1 for the terminal token.
return false;
}
char *name = tokens[NAME_TOKEN].value;
int name_len = tokens[NAME_TOKEN].length;
if (name == NULL ||
name_len <= 0 ||
tokens[VALUE_TOKEN].value == NULL ||
tokens[VALUE_TOKEN].length <= 0 ||
tokens[VALUE_TOKEN].length >= MERGE_BUF_SIZE) {
return false;
}
return protocol_stats_merge_name_val(merger,
tokens[PREFIX_TOKEN].value,
tokens[PREFIX_TOKEN].length,
name, name_len,
tokens[VALUE_TOKEN].value,
tokens[VALUE_TOKEN].length);
}
// TODO: The stats merge assumes an ascii upstream.
//
bool protocol_stats_merge_name_val(genhash_t *merger,
char *prefix,
int prefix_len,
char *name,
int name_len,
char *val,
int val_len) {
assert(merger);
assert(name);
assert(val);
char *key = name + name_len - 1; // Key part for merge rule lookup.
while (key >= name && *key != ':') { // Scan for last colon.
key--;
}
if (key < name) {
key = name;
}
int key_len = name_len - (key - name);
if (key_len > 0 &&
key_len < MERGE_BUF_SIZE) {
char buf_name[MERGE_BUF_SIZE];
char buf_key[MERGE_BUF_SIZE];
char buf_val[MERGE_BUF_SIZE];
strncpy(buf_name, name, name_len);
buf_name[name_len] = '\0';
char *prev = (char *) genhash_find(merger, buf_name);
if (prev == NULL) {
char *hval = malloc(prefix_len + 1 +
name_len + 1 +
val_len + 1);
if (hval != NULL) {
memcpy(hval, prefix, prefix_len);
hval[prefix_len] = ' ';
memcpy(hval + prefix_len + 1, name, name_len);
hval[prefix_len + 1 + name_len] = ' ';
memcpy(hval + prefix_len + 1 + name_len + 1, val, val_len);
hval[prefix_len + 1 + name_len + 1 + val_len] = '\0';
genhash_update(merger, hval + prefix_len + 1, hval);
}
return true;
}
strncpy(buf_key, key, key_len);
buf_key[key_len] = '\0';
if (strstr(protocol_stats_keys_first, buf_key) != NULL) {
return true;
}
token_t prev_tokens[MAX_TOKENS];
size_t prev_ntokens = scan_tokens(prev, prev_tokens,
MAX_TOKENS, NULL);
if (prev_ntokens != 4) {
return true;
}
strncpy(buf_val, val, val_len);
buf_val[val_len] = '\0';
bool ok;
if (strstr(protocol_stats_keys_smallest, buf_key) != NULL) {
ok = protocol_stats_merge_smallest(prev_tokens[VALUE_TOKEN].value,
prev_tokens[VALUE_TOKEN].length,
buf_val, val_len,
buf_val, MERGE_BUF_SIZE);
} else {
ok = protocol_stats_merge_sum(prev_tokens[VALUE_TOKEN].value,
prev_tokens[VALUE_TOKEN].length,
buf_val, val_len,
buf_val, MERGE_BUF_SIZE);
}
if (ok) {
int vlen = strlen(buf_val);
char *hval = malloc(prefix_len + 1 +
name_len + 1 +
vlen + 1);
if (hval != NULL) {
memcpy(hval, prefix, prefix_len);
hval[prefix_len] = ' ';
memcpy(hval + prefix_len + 1, name, name_len);
hval[prefix_len + 1 + name_len] = ' ';
strcpy(hval + prefix_len + 1 + name_len + 1, buf_val);
hval[prefix_len + 1 + name_len + 1 + vlen] = '\0';
genhash_update(merger, hval + prefix_len + 1, hval);
free(prev);
}
}
// Note, if we couldn't merge, then just keep
// the previous value.
//
return true;
}
return false;
}
bool protocol_stats_merge_sum(char *v1, int v1len,
char *v2, int v2len,
char *out, int outlen) {
(void)outlen;
int dot = count_dot_pair(v1, v1len, v2, v2len);
if (dot > 0) {
float v1f = strtof(v1, NULL);
float v2f = strtof(v2, NULL);
sprintf(out, "%f", v1f + v2f);
return true;
} else {
uint64_t v1i = 0;
uint64_t v2i = 0;
if (safe_strtoull(v1, &v1i) &&
safe_strtoull(v2, &v2i)) {
sprintf(out, "%llu", (long long unsigned int) (v1i + v2i));
return true;
}
}
return false;
}
bool protocol_stats_merge_smallest(char *v1, int v1len,
char *v2, int v2len,
char *out, int outlen) {
(void)outlen;
int dot = count_dot_pair(v1, v1len, v2, v2len);
if (dot > 0) {
float v1f = strtof(v1, NULL);
float v2f = strtof(v2, NULL);
sprintf(out, "%f", (v1f > v2f ? v1f : v2f));
return true;
} else {
uint64_t v1i = 0;
uint64_t v2i = 0;
if (safe_strtoull(v1, &v1i) &&
safe_strtoull(v2, &v2i)) {
sprintf(out, "%llu", (long long unsigned int) (v1i > v2i ? v1i : v2i));
return true;
}
}
return false;
}
/* Callback to hash table iteration that frees the multiget_entry list.
*/
void protocol_stats_foreach_free(const void *key,
const void *value,
void *user_data) {
(void)key;
(void)user_data;
assert(value != NULL);
free((void*)value);
}
void protocol_stats_foreach_write(const void *key,
const void *value,
void *user_data) {
(void)key;
char *line = (char *) value;
assert(line != NULL);
conn *uc = (conn *) user_data;
assert(uc != NULL);
int nline = strlen(line);
if (nline > 0) {
if (settings.verbose > 2) {
moxi_log_write("%d: cproxy_stats writing: %s\n", uc->sfd, line);
}
if (IS_BINARY(uc->protocol)) {
token_t line_tokens[MAX_TOKENS];
size_t line_ntokens = scan_tokens(line, line_tokens, MAX_TOKENS, NULL);
if (line_ntokens == 4) {
uint16_t key_len = line_tokens[NAME_TOKEN].length;
uint32_t data_len = line_tokens[VALUE_TOKEN].length;
item *it = item_alloc("s", 1, 0, 0,
sizeof(protocol_binary_response_stats) + key_len + data_len);
if (it != NULL) {
protocol_binary_response_stats *header =
(protocol_binary_response_stats *) ITEM_data(it);
memset(ITEM_data(it), 0, it->nbytes);
header->message.header.response.magic = (uint8_t) PROTOCOL_BINARY_RES;
header->message.header.response.opcode = uc->binary_header.request.opcode;
header->message.header.response.keylen = (uint16_t) htons(key_len);
header->message.header.response.bodylen = htonl(key_len + data_len);
header->message.header.response.opaque = uc->opaque;
memcpy((ITEM_data(it)) + sizeof(protocol_binary_response_stats),
line_tokens[NAME_TOKEN].value, key_len);
memcpy((ITEM_data(it)) + sizeof(protocol_binary_response_stats) + key_len,
line_tokens[VALUE_TOKEN].value, data_len);
if (add_conn_item(uc, it)) {
add_iov(uc, ITEM_data(it), it->nbytes);
if (settings.verbose > 2) {
moxi_log_write("%d: cproxy_stats writing binary", uc->sfd);
cproxy_dump_header(uc->sfd, ITEM_data(it));
}
return;
}
item_remove(it);
}
}
return;
}
item *it = item_alloc("s", 1, 0, 0, nline + 2);
if (it != NULL) {
strncpy(ITEM_data(it), line, nline);
strncpy(ITEM_data(it) + nline, "\r\n", 2);
if (add_conn_item(uc, it)) {
add_iov(uc, ITEM_data(it), nline + 2);
return;
}
item_remove(it);
}
}
}
int count_dot_pair(char *x, int xlen, char *y, int ylen) {
int xdot = count_dot(x, xlen);
int ydot = count_dot(y, ylen);
return (xdot > ydot ? xdot : ydot);
}
int count_dot(char *x, int len) { // Number of '.' chars in a string.
int dot = 0;
for (char *end = x + len; x < end; x++) {
if (*x == '.')
dot++;
}
return dot;
}
// ----------------------------------------
void cproxy_reset_stats_td(proxy_stats_td *pstd) {
assert(pstd);
cproxy_reset_stats(&pstd->stats);
for (int j = 0; j < STATS_CMD_TYPE_last; j++) {
for (int k = 0; k < STATS_CMD_last; k++) {
cproxy_reset_stats_cmd(&pstd->stats_cmd[j][k]);
}
}
}
void cproxy_reset_stats(proxy_stats *ps) {
assert(ps);
// Only clear the tot_xxx stats, not the num_xxx ones.
//
ps->tot_upstream = 0;
ps->tot_downstream_conn = 0;
ps->tot_downstream_conn_acquired = 0;
ps->tot_downstream_conn_released = 0;
ps->tot_downstream_released = 0;
ps->tot_downstream_reserved = 0;
ps->tot_downstream_reserved_time = 0;
ps->max_downstream_reserved_time = 0;
ps->tot_downstream_freed = 0;
ps->tot_downstream_quit_server = 0;
ps->tot_downstream_max_reached = 0;
ps->tot_downstream_create_failed = 0;
ps->tot_downstream_connect = 0;
ps->tot_downstream_connect_failed = 0;
ps->tot_downstream_connect_timeout = 0;
ps->tot_downstream_connect_interval = 0;
ps->tot_downstream_connect_max_reached = 0;
ps->tot_downstream_waiting_errors = 0;
ps->tot_downstream_auth = 0;
ps->tot_downstream_auth_failed = 0;
ps->tot_downstream_bucket = 0;
ps->tot_downstream_bucket_failed = 0;
ps->tot_downstream_propagate_failed = 0;
ps->tot_downstream_close_on_upstream_close = 0;
ps->tot_downstream_timeout = 0;
ps->tot_wait_queue_timeout = 0;
ps->tot_assign_downstream = 0;
ps->tot_assign_upstream = 0;
ps->tot_assign_recursion = 0;
ps->tot_reset_upstream_avail = 0;
ps->tot_retry = 0;
ps->tot_retry_time = 0;
ps->max_retry_time = 0;
ps->tot_retry_vbucket = 0;
ps->tot_upstream_paused = 0;
ps->tot_upstream_unpaused = 0;
ps->tot_multiget_keys = 0;
ps->tot_multiget_keys_dedupe = 0;
ps->tot_multiget_bytes_dedupe = 0;
ps->tot_optimize_sets = 0;
ps->tot_optimize_self = 0;
ps->err_oom = 0;
ps->err_upstream_write_prep = 0;
ps->err_downstream_write_prep = 0;
}
void cproxy_reset_stats_cmd(proxy_stats_cmd *sc) {
assert(sc);
memset(sc, 0, sizeof(proxy_stats_cmd));
}
// -------------------------------------------------
key_stats *find_key_stats(proxy_td *ptd, char *key, int key_len,
uint32_t msec_time) {
assert(ptd);
assert(key);
assert(key_len > 0);
key_stats *ks = mcache_get(&ptd->key_stats, key, key_len,
msec_time);
if (ks == NULL) {
ks = calloc(1, sizeof(key_stats));
if (ks != NULL) {
memcpy(ks->key, key, key_len);
ks->key[key_len] = '\0';
ks->refcount = 1;
ks->added_at = msec_time;
mcache_set(&ptd->key_stats, ks,
msec_time +
ptd->behavior_pool.base.key_stats_lifespan,
true, false);
}
}
return ks;
}
void touch_key_stats(proxy_td *ptd, char *key, int key_len,
uint32_t msec_time,
enum_stats_cmd_type cmd_type,
enum_stats_cmd cmd,
int delta_seen,
int delta_hits,
int delta_misses,
int delta_read_bytes,
int delta_write_bytes) {
key_stats *ks = find_key_stats(ptd, key, key_len, msec_time);
if (ks != NULL) {
proxy_stats_cmd *psc = &ks->stats_cmd[cmd_type][cmd];
if (psc != NULL) {
psc->seen += delta_seen;
psc->hits += delta_hits;
psc->misses += delta_misses;
psc->read_bytes += delta_read_bytes;
psc->write_bytes += delta_write_bytes;
}
key_stats_dec_ref(ks);
}
}
// -------------------------------------------------
static char *key_stats_key(void *it) {
key_stats *i = it;
assert(i);
return i->key;
}
static int key_stats_key_len(void *it) {
key_stats *i = it;
assert(i);
return strlen(i->key);
}
static int key_stats_len(void *it) {
(void)it;
return sizeof(key_stats);
}
void key_stats_add_ref(void *it) {
key_stats *i = it;
if (i != NULL) {
i->refcount++;
}
}
void key_stats_dec_ref(void *it) {
key_stats *i = it;
if (i != NULL) {
i->refcount--;
if (i->refcount <= 0) {
free(it);
}
}
}
static void *key_stats_get_next(void *it) {
key_stats *i = it;
assert(i);
return i->next;
}
static void key_stats_set_next(void *it, void *next) {
key_stats *i = it;
assert(i);
i->next = (key_stats *) next;
}
static void *key_stats_get_prev(void *it) {
key_stats *i = it;
assert(i);
return i->prev;
}
static void key_stats_set_prev(void *it, void *prev) {
key_stats *i = it;
assert(i);
i->prev = (key_stats *) prev;
}
static uint32_t key_stats_get_exptime(void *it) {
key_stats *i = it;
assert(i);
return i->exptime;
}
static void key_stats_set_exptime(void *it, uint32_t exptime) {
key_stats *i = it;
assert(i);
i->exptime = exptime;
}