From 408c5f98ed073e43560be5319080a34104441e6a Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Thu, 17 Jan 2008 10:46:25 +0000 Subject: Introducing live busy network detection. --- ot_stats.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) (limited to 'ot_stats.c') diff --git a/ot_stats.c b/ot_stats.c index 6113275..81ff487 100644 --- a/ot_stats.c +++ b/ot_stats.c @@ -43,6 +43,110 @@ static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT]; static time_t ot_start_time; +#ifdef WANT_LOG_NETWORKS +#define STATS_NETWORK_NODE_BITWIDTH 8 +#define STATS_NETWORK_NODE_MAXDEPTH 3 + +#define STATS_NETWORK_NODE_BITMASK ((1<> ( 32 - STATS_NETWORK_NODE_BITWIDTH * ( ++depth ) ) ) & STATS_NETWORK_NODE_BITMASK; + + if( !*node ) { + *node = malloc( sizeof( stats_network_node ) ); + if( !*node ) + return -1; + memset( *node, 0, sizeof( stats_network_node ) ); + } + + if( depth < STATS_NETWORK_NODE_MAXDEPTH ) + return stat_increase_network_count( &(*node)->children[ foo ], depth, ip ); + + (*node)->counters[ foo ]++; + return 0; +} + +static int stats_shift_down_network_count( stats_network_node **node, int depth, int shift ) { + int i, rest = 0; + if( !*node ) return 0; + + if( ++depth == STATS_NETWORK_NODE_MAXDEPTH ) + for( i=0; icounters[i]>>=shift); + return rest; + } + + for( i=0; ichildren[i]; + int rest_val; + + if( !*childnode ) continue; + + rest += rest_val = stats_shift_down_network_count( childnode, depth, shift ); + + if( rest_val ) continue; + + free( (*node)->children[i] ); + (*node)->children[i] = NULL; + } + + return rest; +} + +static void stats_get_highscore_networks( stats_network_node *node, int depth, uint32_t node_value, int *scores, uint32_t *networks, int network_count ) { + int i; + + if( !node ) return; + + if( !depth++ ) { + memset( scores, 0, sizeof( *scores ) * network_count ); + memset( networks, 0, sizeof( *networks ) * network_count ); + } + + if( depth < STATS_NETWORK_NODE_MAXDEPTH ) { + for( i=0; ichildren[i] ) + stats_get_highscore_networks( node->children[i], depth, node_value | ( i << ( 32 - depth * STATS_NETWORK_NODE_BITWIDTH ) ), scores, networks, network_count ); + } else + for( i=0; icounters[i] <= scores[0] ) continue; + + while( (jcounters[i]>scores[j] ) ) ++j; + --j; + + memmove( scores, scores + 1, j * sizeof( *scores ) ); + memmove( networks, networks + 1, j * sizeof( *networks ) ); + scores[ j ] = node->counters[ i ]; + networks[ j ] = node_value | ( i << ( 32 - depth * STATS_NETWORK_NODE_BITWIDTH ) ); + } +} + +static size_t stats_return_busy_networks( char * reply ) { + uint32_t networks[16]; + int scores[16]; + int i; + char * r = reply; + + stats_get_highscore_networks( stats_network_counters_root, 0, 0, scores, networks, 16 ); + + for( i=15; i>=0; ++i) + r += sprintf( r, "%08i: %d.%d.%d.0/24\n", scores[i], (networks[i]>>24)&0xff, (networks[i]>>16)&0xff, (networks[i]>>8)&0xff ); + + return r - reply; +} + +#endif + /* Converter function from memory to human readable hex strings */ static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d>4];*d++=m[*s++&15];}*d=0;return t;} @@ -377,15 +481,20 @@ size_t return_stats_for_tracker( char *reply, int mode, int format ) { return stats_httperrors_txt( reply ); case TASK_STATS_VERSION: return stats_return_tracker_version( reply ); + case TASK_STATS_BUSY_NETWORKS: + return stats_return_busy_networks( reply ); default: return 0; } } -void stats_issue_event( ot_status_event event, int is_tcp, size_t event_data ) { +void stats_issue_event( ot_status_event event, int is_tcp, uint32_t event_data ) { switch( event ) { case EVENT_ACCEPT: if( is_tcp ) ot_overall_tcp_connections++; else ot_overall_udp_connections++; +#ifdef WANT_LOG_NETWORKS + stat_increase_network_count( &stats_network_counters_root, 0, event_data ); +#endif break; case EVENT_ANNOUNCE: if( is_tcp ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++; -- cgit v1.2.3