From c350fa0b3c323979f73d079d389219e7f98edc1b Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Wed, 26 Sep 2007 16:49:13 +0000 Subject: New stats for s24s code, this is debug, do not use in real world --- opentracker.c | 17 +++++++++++- trackerlogic.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ trackerlogic.h | 3 +- 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/opentracker.c b/opentracker.c index 646184c..02aeb4a 100644 --- a/opentracker.c +++ b/opentracker.c @@ -281,6 +281,8 @@ static void httpresponse( const int64 s, char *data ) { mode = STATS_UDP; else if( !byte_diff(data,4,"s24s")) mode = STATS_SLASH24S; + else if( !byte_diff(data,4,"s24S")) + mode = STATS_SLASH24S_OLD; else HTTPERROR_400_PARAM; } @@ -312,8 +314,21 @@ static void httpresponse( const int64 s, char *data ) { if( !( reply_size = return_stats_for_tracker( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, mode ) ) ) HTTPERROR_500; break; case STATS_SLASH24S: - if( !( reply_size = return_stats_for_slash24s( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, 25, 64 ) ) ) HTTPERROR_500; +{ + ot_dword diff; struct timeval tv1, tv2; gettimeofday( &tv1, NULL ); + if( !( reply_size = return_stats_for_slash24s( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, 25, 16 ) ) ) HTTPERROR_500; + gettimeofday( &tv2, NULL ); diff = ( tv2.tv_sec - tv1.tv_sec ) * 1000000 + tv2.tv_usec - tv1.tv_usec; + reply_size += sprintf( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf + reply_size, "Time taken: %ld\n", diff ); break; +} + case STATS_SLASH24S_OLD: +{ + ot_dword diff; struct timeval tv1, tv2; gettimeofday( &tv1, NULL ); + if( !( reply_size = return_stats_for_slash24s_old( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, 25, 16 ) ) ) HTTPERROR_500; + gettimeofday( &tv2, NULL ); diff = ( tv2.tv_sec - tv1.tv_sec ) * 1000000 + tv2.tv_usec - tv1.tv_usec; + reply_size += sprintf( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf + reply_size, "Time taken: %ld\n", diff ); + break; +} } break; diff --git a/trackerlogic.c b/trackerlogic.c index 0777c54..7d50ed4 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -641,7 +641,93 @@ size_t return_stats_for_tracker( char *reply, int mode ) { return r - reply; } +/* This function collects 4096 /24s in 4096 possible + malloc blocks +*/ size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ) { + +#define NUM_TOPBITS 12 +#define NUM_LOWBITS (24-NUM_TOPBITS) +#define NUM_BUFS (1<size; ++j ) { + ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; + for( k=0; kpeers[k].data; + size_t numpeers = peer_list->peers[k].size; + for( l=0; l> 8; + ot_dword *count = counts[ s24 >> NUM_LOWBITS ]; + if( !count ) { + count = malloc( sizeof(ot_dword) * NUM_S24S ); + if( !count ) + goto bailout_cleanup; + byte_zero( count, sizeof( ot_dword ) * NUM_S24S ); + counts[ s24 >> NUM_LOWBITS ] = count; + } + count[ s24 & MSK_S24S ]++; + } + } + } + } + + k = l = 0; /* Debug: count allocated bufs */ + for( i=0; i < NUM_BUFS; ++i ) { + ot_dword *count = counts[i]; + if( !counts[i] ) + continue; + ++k; /* Debug: count allocated bufs */ + for( j=0; j < NUM_S24S; ++j ) { + if( count[j] > thresh ) { + /* This subnet seems to announce more torrents than the last in our list */ + int insert_pos = amount - 1; + while( ( insert_pos >= 0 ) && ( count[j] > slash24s[ 2 * insert_pos ] ) ) + --insert_pos; + ++insert_pos; + memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( ot_dword ) * ( amount - insert_pos - 1 ) ); + slash24s[ 2 * insert_pos ] = count[j]; + slash24s[ 2 * insert_pos + 1 ] = ( i << NUM_TOPBITS ) + j; + if( slash24s[ 2 * amount - 2 ] > thresh ) + thresh = slash24s[ 2 * amount - 2 ]; + } + if( count[j] ) ++l; + } + free( count ); + } + + r += sprintf( r, "Allocated bufs: %zd, used s24s: %zd\n", k, l ); + + for( i=0; i < amount; ++i ) + if( slash24s[ 2*i ] >= thresh ) { + ot_dword ip = slash24s[ 2*i +1 ]; + r += sprintf( r, "% 10ld %d.%d.%d.0/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) ); + } + + return r - reply; + +bailout_cleanup: + + for( i=0; i < NUM_BUFS; ++i ) + free( counts[i] ); + + return 0; +} + +size_t return_stats_for_slash24s_old( char *reply, size_t amount, ot_dword thresh ) { ot_word *count = malloc( 0x1000000 * sizeof(ot_word) ); ot_dword slash24s[amount*2]; /* first dword amount, second dword subnet */ size_t i, j, k, l; diff --git a/trackerlogic.h b/trackerlogic.h index cb67244..35dd9c8 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -84,7 +84,7 @@ typedef struct { int init_logic( const char * const serverdir ); void deinit_logic( void ); -enum { STATS_MRTG, STATS_TOP5, STATS_DMEM, STATS_TCP, STATS_UDP, STATS_SLASH24S, SYNC_IN, SYNC_OUT }; +enum { STATS_MRTG, STATS_TOP5, STATS_DMEM, STATS_TCP, STATS_UDP, STATS_SLASH24S, STATS_SLASH24S_OLD, SYNC_IN, SYNC_OUT }; ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer, int from_changeset ); size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, int is_tcp ); @@ -94,6 +94,7 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash, char *reply ); size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply ); size_t return_stats_for_tracker( char *reply, int mode ); size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ); +size_t return_stats_for_slash24s_old( char *reply, size_t amount, ot_dword thresh ); size_t return_memstat_for_tracker( char **reply ); size_t return_changeset_for_tracker( char **reply ); int add_changeset_to_tracker( ot_byte *data, size_t len ); -- cgit v1.2.3