summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c227
1 files changed, 153 insertions, 74 deletions
diff --git a/ot_stats.c b/ot_stats.c
index 45dc8db..5f97a2f 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -6,6 +6,7 @@
6/* System */ 6/* System */
7#include <inttypes.h> 7#include <inttypes.h>
8#include <pthread.h> 8#include <pthread.h>
9#include <stdatomic.h>
9#include <stdio.h> 10#include <stdio.h>
10#include <stdlib.h> 11#include <stdlib.h>
11#include <string.h> 12#include <string.h>
@@ -36,27 +37,58 @@
36static void stats_make(size_t *iovec_entries, struct iovec **iovector, ot_tasktype mode); 37static void stats_make(size_t *iovec_entries, struct iovec **iovector, ot_tasktype mode);
37#define OT_STATS_TMPSIZE 8192 38#define OT_STATS_TMPSIZE 8192
38 39
39/* Clumsy counters... to be rethought */ 40/* Each serving thread keeps its own statistics tally. The list is linked
40static unsigned long long ot_overall_tcp_connections; 41 through intrusive next pointers within the workstruct.
41static unsigned long long ot_overall_udp_connections; 42
42static unsigned long long ot_overall_tcp_successfulannounces; 43 Registration happens once per thread and the adding up once per statistics
43static unsigned long long ot_overall_udp_successfulannounces; 44 request, so this lock is never on anybody's hot path. */
44static unsigned long long ot_overall_tcp_successfulscrapes; 45static struct ot_workstruct *g_counter_contexts;
45static unsigned long long ot_overall_udp_successfulscrapes; 46static ot_counter g_retired[OT_COUNTER_COUNT];
46static unsigned long long ot_overall_udp_connectionidmissmatches; 47static pthread_mutex_t g_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
47static unsigned long long ot_overall_tcp_connects; 48
48static unsigned long long ot_overall_udp_connects; 49void stats_counters_register(struct ot_workstruct *ws) {
49static unsigned long long ot_overall_completed; 50 pthread_mutex_lock(&g_counter_mutex);
50static unsigned long long ot_full_scrape_count; 51 ws->counters_next = g_counter_contexts;
51static unsigned long long ot_full_scrape_request_count; 52 g_counter_contexts = ws;
52static unsigned long long ot_full_scrape_size; 53 pthread_mutex_unlock(&g_counter_mutex);
53static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT]; 54}
55
56void stats_counters_retire(struct ot_workstruct *ws) {
57 struct ot_workstruct **link;
58 size_t i;
59
60 pthread_mutex_lock(&g_counter_mutex);
61 for (link = &g_counter_contexts; *link; link = &(*link)->counters_next)
62 if (*link == ws) {
63 *link = ws->counters_next;
64 break;
65 }
66
67 /* When retiring a worker thread, collect the counter tally in the global */
68 for (i = 0; i < OT_COUNTER_COUNT; ++i)
69 atomic_fetch_add_explicit(g_retired + i, atomic_load_explicit(ws->counters + i, memory_order_relaxed), memory_order_relaxed);
70 pthread_mutex_unlock(&g_counter_mutex);
71}
72
73/* What every thread has counted so far, added together */
74static void counters_total(unsigned long long *total) {
75 struct ot_workstruct *ws;
76 size_t i;
77
78 pthread_mutex_lock(&g_counter_mutex);
79 for (i = 0; i < OT_COUNTER_COUNT; ++i)
80 total[i] = atomic_load_explicit(g_retired + i, memory_order_relaxed);
81 for (ws = g_counter_contexts; ws; ws = ws->counters_next)
82 for (i = 0; i < OT_COUNTER_COUNT; ++i)
83 total[i] += atomic_load_explicit(ws->counters + i, memory_order_relaxed);
84 pthread_mutex_unlock(&g_counter_mutex);
85}
86
87#define COUNT(index, by) atomic_fetch_add_explicit(ws->counters + (index), (unsigned long long)(by), memory_order_relaxed)
88
54static const char *ot_failed_request_names[] = { 89static const char *ot_failed_request_names[] = {
55 "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest", 90 "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest",
56 "402 Payment Required", "403 Access Denied", "404 Not found", "500 Internal Server Error"}; 91 "402 Payment Required", "403 Access Denied", "404 Not found", "500 Internal Server Error"};
57static unsigned long long ot_renewed[OT_PEER_TIMEOUT];
58static unsigned long long ot_overall_sync_count;
59static unsigned long long ot_overall_stall_count;
60 92
61static time_t ot_start_time; 93static time_t ot_start_time;
62 94
@@ -347,39 +379,61 @@ static unsigned long events_per_time(unsigned long long events, time_t t) { retu
347 379
348static void stats_connections_mrtg(char **reply, const char *reply_end) { 380static void stats_connections_mrtg(char **reply, const char *reply_end) {
349 ot_time t = time(NULL) - ot_start_time; 381 ot_time t = time(NULL) - ot_start_time;
382 unsigned long long total[OT_COUNTER_COUNT];
383
384 counters_total(total);
385
350 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.", 386 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
351 ot_overall_tcp_connections + ot_overall_udp_connections, 387 total[OT_COUNTER_TCP_CONNECTIONS] + total[OT_COUNTER_UDP_CONNECTIONS],
352 ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, (int)t, (int)(t / 3600), 388 total[OT_COUNTER_TCP_ANNOUNCES] + total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], (int)t, (int)(t / 3600),
353 events_per_time(ot_overall_tcp_connections + ot_overall_udp_connections, t), 389 events_per_time(total[OT_COUNTER_TCP_CONNECTIONS] + total[OT_COUNTER_UDP_CONNECTIONS], t),
354 events_per_time(ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, t)); 390 events_per_time(total[OT_COUNTER_TCP_ANNOUNCES] + total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], t));
355} 391}
356 392
357static void stats_udpconnections_mrtg(char **reply, const char *reply_end) { 393static void stats_udpconnections_mrtg(char **reply, const char *reply_end) {
358 ot_time t = time(NULL) - ot_start_time; 394 ot_time t = time(NULL) - ot_start_time;
359 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker udp4 stats, %lu conns/s :: %lu success/s.", ot_overall_udp_connections, 395 unsigned long long total[OT_COUNTER_COUNT];
360 ot_overall_udp_successfulannounces + ot_overall_udp_connects, (int)t, (int)(t / 3600), events_per_time(ot_overall_udp_connections, t), 396
361 events_per_time(ot_overall_udp_successfulannounces + ot_overall_udp_connects, t)); 397 counters_total(total);
398
399 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker udp4 stats, %lu conns/s :: %lu success/s.", total[OT_COUNTER_UDP_CONNECTIONS],
400 total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], (int)t, (int)(t / 3600),
401 events_per_time(total[OT_COUNTER_UDP_CONNECTIONS], t),
402 events_per_time(total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], t));
362} 403}
363 404
364static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) { 405static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) {
365 time_t t = time(NULL) - ot_start_time; 406 time_t t = time(NULL) - ot_start_time;
366 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker tcp4 stats, %lu conns/s :: %lu success/s.", ot_overall_tcp_connections, 407 unsigned long long total[OT_COUNTER_COUNT];
367 ot_overall_tcp_successfulannounces, (int)t, (int)(t / 3600), events_per_time(ot_overall_tcp_connections, t), 408
368 events_per_time(ot_overall_tcp_successfulannounces, t)); 409 counters_total(total);
410
411 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker tcp4 stats, %lu conns/s :: %lu success/s.", total[OT_COUNTER_TCP_CONNECTIONS],
412 total[OT_COUNTER_TCP_ANNOUNCES], (int)t, (int)(t / 3600), events_per_time(total[OT_COUNTER_TCP_CONNECTIONS], t),
413 events_per_time(total[OT_COUNTER_TCP_ANNOUNCES], t));
369} 414}
370 415
371static void stats_scrape_mrtg(char **reply, const char *reply_end) { 416static void stats_scrape_mrtg(char **reply, const char *reply_end) {
372 time_t t = time(NULL) - ot_start_time; 417 time_t t = time(NULL) - ot_start_time;
418 unsigned long long total[OT_COUNTER_COUNT];
419
420 counters_total(total);
421
373 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker scrape stats, %lu scrape/s (tcp and udp)", 422 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker scrape stats, %lu scrape/s (tcp and udp)",
374 ot_overall_tcp_successfulscrapes, ot_overall_udp_successfulscrapes, (int)t, (int)(t / 3600), 423 total[OT_COUNTER_TCP_SCRAPES], total[OT_COUNTER_UDP_SCRAPES], (int)t, (int)(t / 3600),
375 events_per_time((ot_overall_tcp_successfulscrapes + ot_overall_udp_successfulscrapes), t)); 424 events_per_time((total[OT_COUNTER_TCP_SCRAPES] + total[OT_COUNTER_UDP_SCRAPES]), t));
376} 425}
377 426
378static void stats_fullscrapes_mrtg(char **reply, const char *reply_end) { 427static void stats_fullscrapes_mrtg(char **reply, const char *reply_end) {
379 ot_time t = time(NULL) - ot_start_time; 428 ot_time t = time(NULL) - ot_start_time;
429 unsigned long long total[OT_COUNTER_COUNT];
430
431 counters_total(total);
432
380 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker full scrape stats, %lu conns/s :: %lu bytes/s.", 433 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker full scrape stats, %lu conns/s :: %lu bytes/s.",
381 ot_full_scrape_count * 1000, ot_full_scrape_size, (int)t, (int)(t / 3600), events_per_time(ot_full_scrape_count, t), 434 total[OT_COUNTER_FULLSCRAPES] * 1000, total[OT_COUNTER_FULLSCRAPE_BYTES], (int)t, (int)(t / 3600),
382 events_per_time(ot_full_scrape_size, t)); 435 events_per_time(total[OT_COUNTER_FULLSCRAPES], t),
436 events_per_time(total[OT_COUNTER_FULLSCRAPE_BYTES], t));
383} 437}
384 438
385static void stats_peers_mrtg(char **reply, const char *reply_end) { 439static void stats_peers_mrtg(char **reply, const char *reply_end) {
@@ -398,30 +452,46 @@ static void stats_torrents_mrtg(char **reply, const char *reply_end) {
398} 452}
399 453
400static void stats_httperrors_txt(char **reply, const char *reply_end) { 454static void stats_httperrors_txt(char **reply, const char *reply_end) {
455 unsigned long long total[OT_COUNTER_COUNT];
456
457 counters_total(total);
458
401 fmt_append(reply, reply_end, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n", 459 fmt_append(reply, reply_end, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n",
402 ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2], ot_failed_request_counts[3], 460 total[OT_COUNTER_FAILED + 0], total[OT_COUNTER_FAILED + 1], total[OT_COUNTER_FAILED + 2], total[OT_COUNTER_FAILED + 3],
403 ot_failed_request_counts[4], ot_failed_request_counts[5], ot_failed_request_counts[6]); 461 total[OT_COUNTER_FAILED + 4], total[OT_COUNTER_FAILED + 5], total[OT_COUNTER_FAILED + 6]);
404} 462}
405 463
406static void stats_return_renew_bucket(char **reply, const char *reply_end) { 464static void stats_return_renew_bucket(char **reply, const char *reply_end) {
465 unsigned long long total[OT_COUNTER_COUNT];
466
467 counters_total(total);
468
407 int i; 469 int i;
408 470
409 for (i = 0; i < OT_PEER_TIMEOUT; ++i) 471 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
410 fmt_append(reply, reply_end, "%02i %llu\n", i, ot_renewed[i]); 472 fmt_append(reply, reply_end, "%02i %llu\n", i, total[OT_COUNTER_RENEWED + i]);
411} 473}
412 474
413static void stats_return_sync_mrtg(char **reply, const char *reply_end) { 475static void stats_return_sync_mrtg(char **reply, const char *reply_end) {
414 ot_time t = time(NULL) - ot_start_time; 476 ot_time t = time(NULL) - ot_start_time;
415 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.", ot_overall_sync_count, 0LL, 477 unsigned long long total[OT_COUNTER_COUNT];
416 (int)t, (int)(t / 3600), events_per_time(ot_overall_tcp_connections + ot_overall_udp_connections, t), 478
417 events_per_time(ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, t)); 479 counters_total(total);
480
481 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.", total[OT_COUNTER_SYNCS], 0LL,
482 (int)t, (int)(t / 3600), events_per_time(total[OT_COUNTER_TCP_CONNECTIONS] + total[OT_COUNTER_UDP_CONNECTIONS], t),
483 events_per_time(total[OT_COUNTER_TCP_ANNOUNCES] + total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], t));
418} 484}
419 485
420static void stats_return_completed_mrtg(char **reply, const char *reply_end) { 486static void stats_return_completed_mrtg(char **reply, const char *reply_end) {
421 ot_time t = time(NULL) - ot_start_time; 487 ot_time t = time(NULL) - ot_start_time;
488 unsigned long long total[OT_COUNTER_COUNT];
489
490 counters_total(total);
491
422 492
423 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker, %lu completed/h.", ot_overall_completed, 0LL, (int)t, (int)(t / 3600), 493 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker, %lu completed/h.", total[OT_COUNTER_COMPLETED], 0LL, (int)t, (int)(t / 3600),
424 events_per_time(ot_overall_completed, t / 3600)); 494 events_per_time(total[OT_COUNTER_COMPLETED], t / 3600));
425} 495}
426 496
427#ifdef WANT_LOG_NUMWANT 497#ifdef WANT_LOG_NUMWANT
@@ -464,6 +534,10 @@ static void stats_return_fulllog(size_t *iovec_entries, struct iovec **iovector,
464#endif 534#endif
465 535
466static void stats_return_everything(char **reply, const char *reply_end) { 536static void stats_return_everything(char **reply, const char *reply_end) {
537 unsigned long long total[OT_COUNTER_COUNT];
538
539 counters_total(total);
540
467 torrent_stats stats = {0, 0, 0}; 541 torrent_stats stats = {0, 0, 0};
468 int i; 542 int i;
469 543
@@ -482,25 +556,25 @@ static void stats_return_everything(char **reply, const char *reply_end) {
482 fmt_append(reply, reply_end, " </torrents>\n"); 556 fmt_append(reply, reply_end, " </torrents>\n");
483 fmt_append(reply, reply_end, " <peers>\n <count>%llu</count>\n </peers>\n", stats.peer_count); 557 fmt_append(reply, reply_end, " <peers>\n <count>%llu</count>\n </peers>\n", stats.peer_count);
484 fmt_append(reply, reply_end, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count); 558 fmt_append(reply, reply_end, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count);
485 fmt_append(reply, reply_end, " <completed>\n <count>%llu</count>\n </completed>\n", ot_overall_completed); 559 fmt_append(reply, reply_end, " <completed>\n <count>%llu</count>\n </completed>\n", total[OT_COUNTER_COMPLETED]);
486 fmt_append(reply, reply_end, " <connections>\n"); 560 fmt_append(reply, reply_end, " <connections>\n");
487 fmt_append(reply, reply_end, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n", 561 fmt_append(reply, reply_end, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n",
488 ot_overall_tcp_connections, ot_overall_tcp_successfulannounces, ot_overall_tcp_successfulscrapes); 562 total[OT_COUNTER_TCP_CONNECTIONS], total[OT_COUNTER_TCP_ANNOUNCES], total[OT_COUNTER_TCP_SCRAPES]);
489 fmt_append(reply, reply_end, " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n", 563 fmt_append(reply, reply_end, " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n",
490 ot_overall_udp_connections, ot_overall_udp_connects, ot_overall_udp_successfulannounces, ot_overall_udp_successfulscrapes, 564 total[OT_COUNTER_UDP_CONNECTIONS], total[OT_COUNTER_UDP_CONNECTS], total[OT_COUNTER_UDP_ANNOUNCES], total[OT_COUNTER_UDP_SCRAPES],
491 ot_overall_udp_connectionidmissmatches); 565 total[OT_COUNTER_UDP_CONNID_MISSMATCH]);
492 fmt_append(reply, reply_end, " <livesync>\n <count>%llu</count>\n </livesync>\n", ot_overall_sync_count); 566 fmt_append(reply, reply_end, " <livesync>\n <count>%llu</count>\n </livesync>\n", total[OT_COUNTER_SYNCS]);
493 fmt_append(reply, reply_end, " </connections>\n"); 567 fmt_append(reply, reply_end, " </connections>\n");
494 fmt_append(reply, reply_end, " <debug>\n"); 568 fmt_append(reply, reply_end, " <debug>\n");
495 fmt_append(reply, reply_end, " <renew>\n"); 569 fmt_append(reply, reply_end, " <renew>\n");
496 for (i = 0; i < OT_PEER_TIMEOUT; ++i) 570 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
497 fmt_append(reply, reply_end, " <count interval=\"%02i\">%llu</count>\n", i, ot_renewed[i]); 571 fmt_append(reply, reply_end, " <count interval=\"%02i\">%llu</count>\n", i, total[OT_COUNTER_RENEWED + i]);
498 fmt_append(reply, reply_end, " </renew>\n"); 572 fmt_append(reply, reply_end, " </renew>\n");
499 fmt_append(reply, reply_end, " <http_error>\n"); 573 fmt_append(reply, reply_end, " <http_error>\n");
500 for (i = 0; i < CODE_HTTPERROR_COUNT; ++i) 574 for (i = 0; i < CODE_HTTPERROR_COUNT; ++i)
501 fmt_append(reply, reply_end, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], ot_failed_request_counts[i]); 575 fmt_append(reply, reply_end, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], total[OT_COUNTER_FAILED + i]);
502 fmt_append(reply, reply_end, " </http_error>\n"); 576 fmt_append(reply, reply_end, " </http_error>\n");
503 fmt_append(reply, reply_end, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", ot_overall_stall_count); 577 fmt_append(reply, reply_end, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", total[OT_COUNTER_STALLS]);
504 fmt_append(reply, reply_end, " </debug>\n"); 578 fmt_append(reply, reply_end, " </debug>\n");
505 fmt_append(reply, reply_end, "</stats>"); 579 fmt_append(reply, reply_end, "</stats>");
506} 580}
@@ -608,33 +682,35 @@ static void stats_make(size_t *iovec_entries, struct iovec **iovector, ot_taskty
608 iovec_trim_last(iovec_entries, iovector, r); 682 iovec_trim_last(iovec_entries, iovector, r);
609} 683}
610 684
611void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_data) { 685void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FLAG proto, uintptr_t event_data) {
686 if (!ws)
687 return;
688
612 switch (event) { 689 switch (event) {
613 case EVENT_ACCEPT: 690 case EVENT_ACCEPT:
614 if (proto == FLAG_TCP) 691 if (proto == FLAG_TCP)
615 ot_overall_tcp_connections++; 692 COUNT(OT_COUNTER_TCP_CONNECTIONS, 1);
616 else 693 else
617 ot_overall_udp_connections++; 694 COUNT(OT_COUNTER_UDP_CONNECTIONS, 1);
618#ifdef WANT_LOG_NETWORKS 695#ifdef WANT_LOG_NETWORKS
619 stat_increase_network_count(&stats_network_counters_root, 0, event_data); 696 stat_increase_network_count(&stats_network_counters_root, 0, event_data);
620#endif 697#endif
621 break; 698 break;
622 case EVENT_ANNOUNCE: 699 case EVENT_ANNOUNCE:
623 if (proto == FLAG_TCP) 700 if (proto == FLAG_TCP)
624 ot_overall_tcp_successfulannounces++; 701 COUNT(OT_COUNTER_TCP_ANNOUNCES, 1);
625 else 702 else
626 ot_overall_udp_successfulannounces++; 703 COUNT(OT_COUNTER_UDP_ANNOUNCES, 1);
627 break; 704 break;
628 case EVENT_CONNECT: 705 case EVENT_CONNECT:
629 if (proto == FLAG_TCP) 706 if (proto == FLAG_TCP)
630 ot_overall_tcp_connects++; 707 COUNT(OT_COUNTER_TCP_CONNECTS, 1);
631 else 708 else
632 ot_overall_udp_connects++; 709 COUNT(OT_COUNTER_UDP_CONNECTS, 1);
633 break; 710 break;
634 case EVENT_COMPLETED: 711 case EVENT_COMPLETED:
635#ifdef WANT_SYSLOGS 712#ifdef WANT_SYSLOGS
636 if (event_data) { 713 {
637 struct ot_workstruct *ws = (struct ot_workstruct *)event_data;
638 char timestring[64]; 714 char timestring[64];
639 char hash_hex[42], peerid_hex[42], ip_readable[64]; 715 char hash_hex[42], peerid_hex[42], ip_readable[64];
640 struct tm time_now; 716 struct tm time_now;
@@ -655,47 +731,47 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_
655 syslog(LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable); 731 syslog(LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable);
656 } 732 }
657#endif 733#endif
658 ot_overall_completed++; 734 COUNT(OT_COUNTER_COMPLETED, 1);
659 break; 735 break;
660 case EVENT_SCRAPE: 736 case EVENT_SCRAPE:
661 if (proto == FLAG_TCP) 737 if (proto == FLAG_TCP)
662 ot_overall_tcp_successfulscrapes++; 738 COUNT(OT_COUNTER_TCP_SCRAPES, 1);
663 else 739 else
664 ot_overall_udp_successfulscrapes++; 740 COUNT(OT_COUNTER_UDP_SCRAPES, 1);
665 break; 741 break;
666 case EVENT_FULLSCRAPE: 742 case EVENT_FULLSCRAPE:
667 ot_full_scrape_count++; 743 COUNT(OT_COUNTER_FULLSCRAPES, 1);
668 ot_full_scrape_size += event_data; 744 COUNT(OT_COUNTER_FULLSCRAPE_BYTES, event_data);
669 break; 745 break;
670 case EVENT_FULLSCRAPE_REQUEST: { 746 case EVENT_FULLSCRAPE_REQUEST: {
671 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ 747 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
672 char _debug[512]; 748 char _debug[512];
673 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60); 749 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(time_now_seconds() - ot_start_time) / 60);
674 off += ip_format(_debug + off, *ip); 750 off += ip_format(_debug + off, *ip);
675 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); 751 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
676 (void)write(2, _debug, off); 752 (void)write(2, _debug, off);
677 ot_full_scrape_request_count++; 753 COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1);
678 } break; 754 } break;
679 case EVENT_FULLSCRAPE_REQUEST_GZIP: { 755 case EVENT_FULLSCRAPE_REQUEST_GZIP: {
680 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ 756 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
681 char _debug[512]; 757 char _debug[512];
682 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60); 758 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(time_now_seconds() - ot_start_time) / 60);
683 off += ip_format(_debug + off, *ip); 759 off += ip_format(_debug + off, *ip);
684 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); 760 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
685 (void)write(2, _debug, off); 761 (void)write(2, _debug, off);
686 ot_full_scrape_request_count++; 762 COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1);
687 } break; 763 } break;
688 case EVENT_FAILED: 764 case EVENT_FAILED:
689 ot_failed_request_counts[event_data]++; 765 COUNT(OT_COUNTER_FAILED + event_data, 1);
690 break; 766 break;
691 case EVENT_RENEW: 767 case EVENT_RENEW:
692 ot_renewed[event_data]++; 768 COUNT(OT_COUNTER_RENEWED + event_data, 1);
693 break; 769 break;
694 case EVENT_SYNC: 770 case EVENT_SYNC:
695 ot_overall_sync_count += event_data; 771 COUNT(OT_COUNTER_SYNCS, event_data);
696 break; 772 break;
697 case EVENT_BUCKET_LOCKED: 773 case EVENT_BUCKET_LOCKED:
698 ot_overall_stall_count++; 774 COUNT(OT_COUNTER_STALLS, 1);
699 break; 775 break;
700#ifdef WANT_SPOT_WOODPECKER 776#ifdef WANT_SPOT_WOODPECKER
701 case EVENT_WOODPECKER: 777 case EVENT_WOODPECKER:
@@ -705,7 +781,7 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_
705 break; 781 break;
706#endif 782#endif
707 case EVENT_CONNID_MISSMATCH: 783 case EVENT_CONNID_MISSMATCH:
708 ++ot_overall_udp_connectionidmissmatches; 784 COUNT(OT_COUNTER_UDP_CONNID_MISSMATCH, 1);
709 default: 785 default:
710 break; 786 break;
711 } 787 }
@@ -725,9 +801,11 @@ static void *stats_worker(void *args) {
725 801
726 (void)args; 802 (void)args;
727 803
728 while (1) { 804 while (g_opentracker_running) {
729 ot_tasktype tasktype = TASK_STATS; 805 ot_tasktype tasktype = TASK_STATS;
730 ot_taskid taskid = mutex_workqueue_poptask(&tasktype); 806 ot_taskid taskid = mutex_workqueue_poptask(&tasktype);
807 if (!taskid)
808 break;
731 stats_make(&iovec_entries, &iovector, tasktype); 809 stats_make(&iovec_entries, &iovector, tasktype);
732 if (mutex_workqueue_pushresult(taskid, iovec_entries, iovector)) 810 if (mutex_workqueue_pushresult(taskid, iovec_entries, iovector))
733 iovec_free(&iovec_entries, &iovector); 811 iovec_free(&iovec_entries, &iovector);
@@ -739,10 +817,11 @@ void stats_deliver(int64_t sock, int tasktype) { mutex_workqueue_pushtask(sock,
739 817
740static pthread_t thread_id; 818static pthread_t thread_id;
741void stats_init(void) { 819void stats_init(void) {
742 ot_start_time = g_now_seconds; 820 ot_start_time = time_now_seconds();
743 pthread_create(&thread_id, NULL, stats_worker, NULL); 821 pthread_create(&thread_id, NULL, stats_worker, NULL);
744} 822}
745 823
746void stats_deinit(void) { 824void stats_deinit(void) {
747 pthread_cancel(thread_id); 825 mutex_workqueue_wakeall();
826 pthread_join(thread_id, NULL);
748} 827}