diff options
Diffstat (limited to 'src/ot_stats.c')
| -rw-r--r-- | src/ot_stats.c | 100 |
1 files changed, 30 insertions, 70 deletions
diff --git a/src/ot_stats.c b/src/ot_stats.c index 2d6335b..95f3cff 100644 --- a/src/ot_stats.c +++ b/src/ot_stats.c | |||
| @@ -83,7 +83,7 @@ static void counters_total(unsigned long long *total) { | |||
| 83 | pthread_mutex_unlock(&g_counter_mutex); | 83 | pthread_mutex_unlock(&g_counter_mutex); |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | #define COUNT(index, by) atomic_fetch_add_explicit(ws->counters + (index), (unsigned long long)(by), memory_order_relaxed) | 86 | #define COUNT(index, by) atomic_fetch_add_explicit(ws->counters + (index), (unsigned long)(by), memory_order_relaxed) |
| 87 | 87 | ||
| 88 | static const char *ot_failed_request_names[] = { | 88 | static const char *ot_failed_request_names[] = { |
| 89 | "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest", | 89 | "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest", |
| @@ -91,12 +91,13 @@ static const char *ot_failed_request_names[] = { | |||
| 91 | 91 | ||
| 92 | static time_t ot_start_time; | 92 | static time_t ot_start_time; |
| 93 | 93 | ||
| 94 | #ifdef WANT_SPOT_WOODPECKER | ||
| 94 | #define STATS_NETWORK_NODE_BITWIDTH 4 | 95 | #define STATS_NETWORK_NODE_BITWIDTH 4 |
| 95 | #define STATS_NETWORK_NODE_COUNT (1 << STATS_NETWORK_NODE_BITWIDTH) | 96 | #define STATS_NETWORK_NODE_COUNT (1 << STATS_NETWORK_NODE_BITWIDTH) |
| 96 | 97 | ||
| 97 | #define __BYTE(P, D) (((uint8_t *)P)[D / 8]) | 98 | #define __BYTE(P, D) (((uint8_t *)(P))[(D) / 8]) |
| 98 | #define __MSK (STATS_NETWORK_NODE_COUNT - 1) | 99 | #define __MSK (STATS_NETWORK_NODE_COUNT - 1) |
| 99 | #define __SHFT(D) ((D ^ STATS_NETWORK_NODE_BITWIDTH) & STATS_NETWORK_NODE_BITWIDTH) | 100 | #define __SHFT(D) (((D) ^ STATS_NETWORK_NODE_BITWIDTH) & STATS_NETWORK_NODE_BITWIDTH) |
| 100 | 101 | ||
| 101 | #define __LDR(P, D) ((__BYTE((P), (D)) >> __SHFT((D))) & __MSK) | 102 | #define __LDR(P, D) ((__BYTE((P), (D)) >> __SHFT((D))) & __MSK) |
| 102 | #define __STR(P, D, V) __BYTE((P), (D)) = (__BYTE((P), (D)) & ~(__MSK << __SHFT((D)))) | ((V) << __SHFT((D))) | 103 | #define __STR(P, D, V) __BYTE((P), (D)) = (__BYTE((P), (D)) & ~(__MSK << __SHFT((D)))) | ((V) << __SHFT((D))) |
| @@ -236,50 +237,6 @@ static void stats_return_busy_networks(char **reply, const char *reply_end, stat | |||
| 236 | fmt_append(reply, reply_end, "\n"); | 237 | fmt_append(reply, reply_end, "\n"); |
| 237 | } | 238 | } |
| 238 | 239 | ||
| 239 | static void stats_slash24s_txt(char **reply, const char *reply_end, size_t amount) { | ||
| 240 | stats_network_node *slash24s_network_counters_root = NULL; | ||
| 241 | size_t i, bucket, peer_size = OT_PEER_SIZE4; | ||
| 242 | |||
| 243 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { | ||
| 244 | ot_vector *torrents_list = mutex_bucket_lock(bucket); | ||
| 245 | for (i = 0; i < torrents_list->size; ++i) { | ||
| 246 | ot_peerlist *peer_list = (((ot_torrent *)(torrents_list->data))[i]).peer_list4; | ||
| 247 | ot_vector *bucket_list = &peer_list->peers; | ||
| 248 | int num_buckets = 1; | ||
| 249 | |||
| 250 | if (OT_PEERLIST_HASBUCKETS(peer_list)) { | ||
| 251 | num_buckets = bucket_list->size; | ||
| 252 | bucket_list = (ot_vector *)bucket_list->data; | ||
| 253 | } | ||
| 254 | |||
| 255 | while (num_buckets--) { | ||
| 256 | ot_peer *peers = (ot_peer *)bucket_list->data; | ||
| 257 | size_t numpeers = bucket_list->size; | ||
| 258 | while (numpeers--) { | ||
| 259 | if (stat_increase_network_count(&slash24s_network_counters_root, 0, (uintptr_t)(peers))) | ||
| 260 | goto bailout_unlock; | ||
| 261 | peers += peer_size; | ||
| 262 | } | ||
| 263 | ++bucket_list; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | mutex_bucket_unlock(bucket, 0); | ||
| 267 | if (!g_opentracker_running) | ||
| 268 | goto done; | ||
| 269 | } | ||
| 270 | |||
| 271 | /* The tree is built. Now analyze */ | ||
| 272 | stats_return_busy_networks(reply, reply_end, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_MAXDEPTH); | ||
| 273 | stats_return_busy_networks(reply, reply_end, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_LIMIT); | ||
| 274 | goto done; | ||
| 275 | |||
| 276 | bailout_unlock: | ||
| 277 | mutex_bucket_unlock(bucket, 0); | ||
| 278 | done: | ||
| 279 | stats_shift_down_network_count(&slash24s_network_counters_root, 0, sizeof(int) * 8 - 1); | ||
| 280 | } | ||
| 281 | |||
| 282 | #ifdef WANT_SPOT_WOODPECKER | ||
| 283 | static stats_network_node *stats_woodpeckers_tree; | 240 | static stats_network_node *stats_woodpeckers_tree; |
| 284 | static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER; | 241 | static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 285 | 242 | ||
| @@ -299,8 +256,8 @@ typedef struct { | |||
| 299 | static int torrent_statter(ot_torrent *torrent, uintptr_t data) { | 256 | static int torrent_statter(ot_torrent *torrent, uintptr_t data) { |
| 300 | torrent_stats *stats = (torrent_stats *)data; | 257 | torrent_stats *stats = (torrent_stats *)data; |
| 301 | stats->torrent_count++; | 258 | stats->torrent_count++; |
| 302 | stats->peer_count += torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; | 259 | stats->peer_count += torrent_peer_count(torrent); |
| 303 | stats->seed_count += torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; | 260 | stats->seed_count += torrent_seed_count(torrent); |
| 304 | return 0; | 261 | return 0; |
| 305 | } | 262 | } |
| 306 | 263 | ||
| @@ -343,8 +300,8 @@ void stats_top_txt(char **reply, const char *reply_end, size_t amount) { | |||
| 343 | ot_vector *torrents_list = mutex_bucket_lock(bucket); | 300 | ot_vector *torrents_list = mutex_bucket_lock(bucket); |
| 344 | for (j = 0; j < torrents_list->size; ++j) { | 301 | for (j = 0; j < torrents_list->size; ++j) { |
| 345 | ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j; | 302 | ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j; |
| 346 | size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; | 303 | size_t peer_count = torrent_peer_count(torrent); |
| 347 | size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; | 304 | size_t seed_count = torrent_seed_count(torrent); |
| 348 | size_t leech_count = peer_count - seed_count; | 305 | size_t leech_count = peer_count - seed_count; |
| 349 | 306 | ||
| 350 | stats_insert_into_highscore(peer_count, top100c, amount, &torrent->hash); | 307 | stats_insert_into_highscore(peer_count, top100c, amount, &torrent->hash); |
| @@ -373,7 +330,7 @@ void stats_top_txt(char **reply, const char *reply_end, size_t amount) { | |||
| 373 | static unsigned long events_per_time(unsigned long long events, time_t t) { return events / ((unsigned int)t ? (unsigned int)t : 1); } | 330 | static unsigned long events_per_time(unsigned long long events, time_t t) { return events / ((unsigned int)t ? (unsigned int)t : 1); } |
| 374 | 331 | ||
| 375 | static void stats_connections_mrtg(char **reply, const char *reply_end) { | 332 | static void stats_connections_mrtg(char **reply, const char *reply_end) { |
| 376 | ot_time t = time(NULL) - ot_start_time; | 333 | ot_time t = time_since_seconds(ot_start_time); |
| 377 | unsigned long long total[OT_COUNTER_COUNT]; | 334 | unsigned long long total[OT_COUNTER_COUNT]; |
| 378 | 335 | ||
| 379 | counters_total(total); | 336 | counters_total(total); |
| @@ -386,7 +343,7 @@ static void stats_connections_mrtg(char **reply, const char *reply_end) { | |||
| 386 | } | 343 | } |
| 387 | 344 | ||
| 388 | static void stats_udpconnections_mrtg(char **reply, const char *reply_end) { | 345 | static void stats_udpconnections_mrtg(char **reply, const char *reply_end) { |
| 389 | ot_time t = time(NULL) - ot_start_time; | 346 | ot_time t = time_since_seconds(ot_start_time); |
| 390 | unsigned long long total[OT_COUNTER_COUNT]; | 347 | unsigned long long total[OT_COUNTER_COUNT]; |
| 391 | 348 | ||
| 392 | counters_total(total); | 349 | counters_total(total); |
| @@ -398,7 +355,7 @@ static void stats_udpconnections_mrtg(char **reply, const char *reply_end) { | |||
| 398 | } | 355 | } |
| 399 | 356 | ||
| 400 | static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) { | 357 | static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) { |
| 401 | time_t t = time(NULL) - ot_start_time; | 358 | time_t t = time_since_seconds(ot_start_time); |
| 402 | unsigned long long total[OT_COUNTER_COUNT]; | 359 | unsigned long long total[OT_COUNTER_COUNT]; |
| 403 | 360 | ||
| 404 | counters_total(total); | 361 | counters_total(total); |
| @@ -409,7 +366,7 @@ static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) { | |||
| 409 | } | 366 | } |
| 410 | 367 | ||
| 411 | static void stats_scrape_mrtg(char **reply, const char *reply_end) { | 368 | static void stats_scrape_mrtg(char **reply, const char *reply_end) { |
| 412 | time_t t = time(NULL) - ot_start_time; | 369 | time_t t = time_since_seconds(ot_start_time); |
| 413 | unsigned long long total[OT_COUNTER_COUNT]; | 370 | unsigned long long total[OT_COUNTER_COUNT]; |
| 414 | 371 | ||
| 415 | counters_total(total); | 372 | counters_total(total); |
| @@ -420,7 +377,7 @@ static void stats_scrape_mrtg(char **reply, const char *reply_end) { | |||
| 420 | } | 377 | } |
| 421 | 378 | ||
| 422 | static void stats_fullscrapes_mrtg(char **reply, const char *reply_end) { | 379 | static void stats_fullscrapes_mrtg(char **reply, const char *reply_end) { |
| 423 | ot_time t = time(NULL) - ot_start_time; | 380 | ot_time t = time_since_seconds(ot_start_time); |
| 424 | unsigned long long total[OT_COUNTER_COUNT]; | 381 | unsigned long long total[OT_COUNTER_COUNT]; |
| 425 | 382 | ||
| 426 | counters_total(total); | 383 | counters_total(total); |
| @@ -468,7 +425,7 @@ static void stats_return_renew_bucket(char **reply, const char *reply_end) { | |||
| 468 | } | 425 | } |
| 469 | 426 | ||
| 470 | static void stats_return_sync_mrtg(char **reply, const char *reply_end) { | 427 | static void stats_return_sync_mrtg(char **reply, const char *reply_end) { |
| 471 | ot_time t = time(NULL) - ot_start_time; | 428 | ot_time t = time_since_seconds(ot_start_time); |
| 472 | unsigned long long total[OT_COUNTER_COUNT]; | 429 | unsigned long long total[OT_COUNTER_COUNT]; |
| 473 | 430 | ||
| 474 | counters_total(total); | 431 | counters_total(total); |
| @@ -479,7 +436,7 @@ static void stats_return_sync_mrtg(char **reply, const char *reply_end) { | |||
| 479 | } | 436 | } |
| 480 | 437 | ||
| 481 | static void stats_return_completed_mrtg(char **reply, const char *reply_end) { | 438 | static void stats_return_completed_mrtg(char **reply, const char *reply_end) { |
| 482 | ot_time t = time(NULL) - ot_start_time; | 439 | ot_time t = time_since_seconds(ot_start_time); |
| 483 | unsigned long long total[OT_COUNTER_COUNT]; | 440 | unsigned long long total[OT_COUNTER_COUNT]; |
| 484 | 441 | ||
| 485 | counters_total(total); | 442 | counters_total(total); |
| @@ -494,7 +451,7 @@ extern unsigned long long numwants[201]; | |||
| 494 | static void stats_return_numwants(char **reply, const char *reply_end) { | 451 | static void stats_return_numwants(char **reply, const char *reply_end) { |
| 495 | int i; | 452 | int i; |
| 496 | for (i = 0; i <= 200; ++i) | 453 | for (i = 0; i <= 200; ++i) |
| 497 | fmt_append(reply, reply_end, "%03d => %lld\n", i, numwants[i]); | 454 | fmt_append(reply, reply_end, "%03d => %llu\n", i, numwants[i]); |
| 498 | } | 455 | } |
| 499 | #endif | 456 | #endif |
| 500 | 457 | ||
| @@ -510,7 +467,7 @@ static void stats_return_everything(char **reply, const char *reply_end) { | |||
| 510 | 467 | ||
| 511 | fmt_append(reply, reply_end, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); | 468 | fmt_append(reply, reply_end, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
| 512 | fmt_append(reply, reply_end, "<stats>\n"); | 469 | fmt_append(reply, reply_end, "<stats>\n"); |
| 513 | fmt_append(reply, reply_end, " <uptime>%llu</uptime>\n", (unsigned long long)(time(NULL) - ot_start_time)); | 470 | fmt_append(reply, reply_end, " <uptime>%llu</uptime>\n", (unsigned long long)time_since_seconds(ot_start_time)); |
| 514 | fmt_append(reply, reply_end, " <torrents>\n"); | 471 | fmt_append(reply, reply_end, " <torrents>\n"); |
| 515 | fmt_append(reply, reply_end, " <count_mutex>%zd</count_mutex>\n", mutex_get_torrent_count()); | 472 | fmt_append(reply, reply_end, " <count_mutex>%zd</count_mutex>\n", mutex_get_torrent_count()); |
| 516 | fmt_append(reply, reply_end, " <count_iterator>%llu</count_iterator>\n", stats.torrent_count); | 473 | fmt_append(reply, reply_end, " <count_iterator>%llu</count_iterator>\n", stats.torrent_count); |
| @@ -588,6 +545,9 @@ static const char *const build_features[] = { | |||
| 588 | #ifdef WANT_RESTRICT_STATS | 545 | #ifdef WANT_RESTRICT_STATS |
| 589 | "WANT_RESTRICT_STATS", | 546 | "WANT_RESTRICT_STATS", |
| 590 | #endif | 547 | #endif |
| 548 | #ifdef WANT_RESTRICT_FULLSCRAPE | ||
| 549 | "WANT_RESTRICT_FULLSCRAPE", | ||
| 550 | #endif | ||
| 591 | #ifdef WANT_LOG_NUMWANT | 551 | #ifdef WANT_LOG_NUMWANT |
| 592 | "WANT_LOG_NUMWANT", | 552 | "WANT_LOG_NUMWANT", |
| 593 | #endif | 553 | #endif |
| @@ -692,9 +652,6 @@ static void stats_make(size_t *iovec_entries, struct iovec **iovector, ot_taskty | |||
| 692 | case TASK_STATS_PEERS: | 652 | case TASK_STATS_PEERS: |
| 693 | stats_peers_mrtg(&r, r_end); | 653 | stats_peers_mrtg(&r, r_end); |
| 694 | break; | 654 | break; |
| 695 | case TASK_STATS_SLASH24S: | ||
| 696 | stats_slash24s_txt(&r, r_end, 128); | ||
| 697 | break; | ||
| 698 | case TASK_STATS_TOP10: | 655 | case TASK_STATS_TOP10: |
| 699 | stats_top_txt(&r, r_end, 10); | 656 | stats_top_txt(&r, r_end, 10); |
| 700 | break; | 657 | break; |
| @@ -757,7 +714,8 @@ void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FL | |||
| 757 | 714 | ||
| 758 | time(&ttt); | 715 | time(&ttt); |
| 759 | localtime_r(&ttt, &time_now); | 716 | localtime_r(&ttt, &time_now); |
| 760 | strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now); | 717 | if (!strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now)) |
| 718 | timestring[0] = 0; | ||
| 761 | 719 | ||
| 762 | hex_from_hash(hash_hex, *ws->hash); | 720 | hex_from_hash(hash_hex, *ws->hash); |
| 763 | if (ws->peer_id) | 721 | if (ws->peer_id) |
| @@ -785,26 +743,28 @@ void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FL | |||
| 785 | case EVENT_FULLSCRAPE_REQUEST: { | 743 | case EVENT_FULLSCRAPE_REQUEST: { |
| 786 | ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ | 744 | ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ |
| 787 | char _debug[512]; | 745 | char _debug[512]; |
| 788 | int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(time_now_seconds() - ot_start_time) / 60); | 746 | int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)time_since_seconds(ot_start_time) / 60); |
| 789 | off += ip_format(_debug + off, *ip); | 747 | off += ip_format(_debug + off, *ip); |
| 790 | off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); | 748 | off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); |
| 791 | (void)write(2, _debug, off); | 749 | (void)!write(2, _debug, off); |
| 792 | COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1); | 750 | COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1); |
| 793 | } break; | 751 | } break; |
| 794 | case EVENT_FULLSCRAPE_REQUEST_GZIP: { | 752 | case EVENT_FULLSCRAPE_REQUEST_GZIP: { |
| 795 | ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ | 753 | ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ |
| 796 | char _debug[512]; | 754 | char _debug[512]; |
| 797 | int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(time_now_seconds() - ot_start_time) / 60); | 755 | int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)time_since_seconds(ot_start_time) / 60); |
| 798 | off += ip_format(_debug + off, *ip); | 756 | off += ip_format(_debug + off, *ip); |
| 799 | off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); | 757 | off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); |
| 800 | (void)write(2, _debug, off); | 758 | (void)!write(2, _debug, off); |
| 801 | COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1); | 759 | COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1); |
| 802 | } break; | 760 | } break; |
| 803 | case EVENT_FAILED: | 761 | case EVENT_FAILED: |
| 804 | COUNT(OT_COUNTER_FAILED + event_data, 1); | 762 | if (event_data < CODE_HTTPERROR_COUNT) |
| 763 | COUNT(OT_COUNTER_FAILED + event_data, 1); | ||
| 805 | break; | 764 | break; |
| 806 | case EVENT_RENEW: | 765 | case EVENT_RENEW: |
| 807 | COUNT(OT_COUNTER_RENEWED + event_data, 1); | 766 | if (event_data < OT_PEER_TIMEOUT) |
| 767 | COUNT(OT_COUNTER_RENEWED + event_data, 1); | ||
| 808 | break; | 768 | break; |
| 809 | case EVENT_SYNC: | 769 | case EVENT_SYNC: |
| 810 | COUNT(OT_COUNTER_SYNCS, event_data); | 770 | COUNT(OT_COUNTER_SYNCS, event_data); |
