summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
authorerdgeist <>2012-05-18 05:16:57 +0000
committererdgeist <>2012-05-18 05:16:57 +0000
commit05e0de1a5fa0efd99ada80ce8431dc69a39df0da (patch)
tree496f35f718f65a8fcfb7ff9225d682014109229c /ot_stats.c
parent914e0ac3020b7c842f2a1fd975217f70fa705449 (diff)
Add a top100 for most popular torrents
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/ot_stats.c b/ot_stats.c
index ba08bbc..81343b8 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -93,7 +93,7 @@ static stats_network_node *stats_network_counters_root;
93static int stat_increase_network_count( stats_network_node **pnode, int depth, uintptr_t ip ) { 93static int stat_increase_network_count( stats_network_node **pnode, int depth, uintptr_t ip ) {
94 int foo = __LDR(ip,depth); 94 int foo = __LDR(ip,depth);
95 stats_network_node *node; 95 stats_network_node *node;
96 96
97 if( !*pnode ) { 97 if( !*pnode ) {
98 *pnode = malloc( sizeof( stats_network_node ) ); 98 *pnode = malloc( sizeof( stats_network_node ) );
99 if( !*pnode ) 99 if( !*pnode )
@@ -125,7 +125,7 @@ static int stats_shift_down_network_count( stats_network_node **node, int depth,
125 free( *node ); 125 free( *node );
126 *node = NULL; 126 *node = NULL;
127 } 127 }
128 128
129 return rest; 129 return rest;
130} 130}
131 131
@@ -295,30 +295,33 @@ static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=
295typedef struct { size_t val; ot_torrent * torrent; } ot_record; 295typedef struct { size_t val; ot_torrent * torrent; } ot_record;
296 296
297/* Fetches stats from tracker */ 297/* Fetches stats from tracker */
298size_t stats_top10_txt( char * reply ) { 298size_t stats_top_txt( char * reply, int amount ) {
299 size_t j; 299 size_t j;
300 ot_record top10s[10], top10c[10]; 300 ot_record top100s[100], top100c[100];
301 char *r = reply, hex_out[42]; 301 char *r = reply, hex_out[42];
302 int idx, bucket; 302 int idx, bucket;
303 303
304 byte_zero( top10s, sizeof( top10s ) ); 304 if( amount > 100 )
305 byte_zero( top10c, sizeof( top10c ) ); 305 amount = 100;
306
307 byte_zero( top100s, sizeof( top100s ) );
308 byte_zero( top100c, sizeof( top100c ) );
306 309
307 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { 310 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
308 ot_vector *torrents_list = mutex_bucket_lock( bucket ); 311 ot_vector *torrents_list = mutex_bucket_lock( bucket );
309 for( j=0; j<torrents_list->size; ++j ) { 312 for( j=0; j<torrents_list->size; ++j ) {
310 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; 313 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
311 int idx = 9; while( (idx >= 0) && ( peer_list->peer_count > top10c[idx].val ) ) --idx; 314 int idx = amount - 1; while( (idx >= 0) && ( peer_list->peer_count > top100c[idx].val ) ) --idx;
312 if ( idx++ != 9 ) { 315 if ( idx++ != amount - 1 ) {
313 memmove( top10c + idx + 1, top10c + idx, ( 9 - idx ) * sizeof( ot_record ) ); 316 memmove( top100c + idx + 1, top100c + idx, ( amount - 1 - idx ) * sizeof( ot_record ) );
314 top10c[idx].val = peer_list->peer_count; 317 top100c[idx].val = peer_list->peer_count;
315 top10c[idx].torrent = (ot_torrent*)(torrents_list->data) + j; 318 top100c[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
316 } 319 }
317 idx = 9; while( (idx >= 0) && ( peer_list->seed_count > top10s[idx].val ) ) --idx; 320 idx = amount - 1; while( (idx >= 0) && ( peer_list->seed_count > top100s[idx].val ) ) --idx;
318 if ( idx++ != 9 ) { 321 if ( idx++ != amount - 1 ) {
319 memmove( top10s + idx + 1, top10s + idx, ( 9 - idx ) * sizeof( ot_record ) ); 322 memmove( top100s + idx + 1, top100s + idx, ( amount - 1 - idx ) * sizeof( ot_record ) );
320 top10s[idx].val = peer_list->seed_count; 323 top100s[idx].val = peer_list->seed_count;
321 top10s[idx].torrent = (ot_torrent*)(torrents_list->data) + j; 324 top100s[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
322 } 325 }
323 } 326 }
324 mutex_bucket_unlock( bucket, 0 ); 327 mutex_bucket_unlock( bucket, 0 );
@@ -326,14 +329,14 @@ size_t stats_top10_txt( char * reply ) {
326 return 0; 329 return 0;
327 } 330 }
328 331
329 r += sprintf( r, "Top 10 torrents by peers:\n" ); 332 r += sprintf( r, "Top %d torrents by peers:\n", amount );
330 for( idx=0; idx<10; ++idx ) 333 for( idx=0; idx<amount; ++idx )
331 if( top10c[idx].torrent ) 334 if( top100c[idx].torrent )
332 r += sprintf( r, "\t%zd\t%s\n", top10c[idx].val, to_hex( hex_out, top10c[idx].torrent->hash) ); 335 r += sprintf( r, "\t%zd\t%s\n", top100c[idx].val, to_hex( hex_out, top100c[idx].torrent->hash) );
333 r += sprintf( r, "Top 10 torrents by seeds:\n" ); 336 r += sprintf( r, "Top %d torrents by seeds:\n", amount );
334 for( idx=0; idx<10; ++idx ) 337 for( idx=0; idx<amount; ++idx )
335 if( top10s[idx].torrent ) 338 if( top100s[idx].torrent )
336 r += sprintf( r, "\t%zd\t%s\n", top10s[idx].val, to_hex( hex_out, top10s[idx].torrent->hash) ); 339 r += sprintf( r, "\t%zd\t%s\n", top100s[idx].val, to_hex( hex_out, top100s[idx].torrent->hash) );
337 340
338 return r - reply; 341 return r - reply;
339} 342}
@@ -609,7 +612,8 @@ static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype
609 case TASK_STATS_TORRENTS: r += stats_torrents_mrtg( r ); break; 612 case TASK_STATS_TORRENTS: r += stats_torrents_mrtg( r ); break;
610 case TASK_STATS_PEERS: r += stats_peers_mrtg( r ); break; 613 case TASK_STATS_PEERS: r += stats_peers_mrtg( r ); break;
611 case TASK_STATS_SLASH24S: r += stats_slash24s_txt( r, 128 ); break; 614 case TASK_STATS_SLASH24S: r += stats_slash24s_txt( r, 128 ); break;
612 case TASK_STATS_TOP10: r += stats_top10_txt( r ); break; 615 case TASK_STATS_TOP10: r += stats_top_txt( r, 10 ); break;
616 case TASK_STATS_TOP100: r += stats_top_txt( r, 100 ); break;
613 case TASK_STATS_EVERYTHING: r += stats_return_everything( r ); break; 617 case TASK_STATS_EVERYTHING: r += stats_return_everything( r ); break;
614#ifdef WANT_SPOT_WOODPECKER 618#ifdef WANT_SPOT_WOODPECKER
615 case TASK_STATS_WOODPECKERS: r += stats_return_woodpeckers( r, 128 ); break; 619 case TASK_STATS_WOODPECKERS: r += stats_return_woodpeckers( r, 128 ); break;