diff options
| author | erdgeist <> | 2007-11-06 11:58:32 +0000 |
|---|---|---|
| committer | erdgeist <> | 2007-11-06 11:58:32 +0000 |
| commit | 8900cc0dd980cb08a0af957a1d0dd849bf3c2ac6 (patch) | |
| tree | 70aeed1dbaceea343e6ebd000d46df025bae21fc /ot_stats.c | |
| parent | 5749f1d8fe80cbb84d66a265bcf9bafe159985ab (diff) | |
No one can get access to buckets now without locking them. Also split up the trackerlogic.c-monster in functional sub-units. HEADS UP: this code is untested and not considered stable.
Diffstat (limited to 'ot_stats.c')
| -rw-r--r-- | ot_stats.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/ot_stats.c b/ot_stats.c new file mode 100644 index 0000000..215d557 --- /dev/null +++ b/ot_stats.c | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
| 3 | |||
| 4 | /* System */ | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <sys/types.h> | ||
| 7 | #include <sys/mman.h> | ||
| 8 | #include <stdio.h> | ||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | /* Libowfat */ | ||
| 12 | #include "byte.h" | ||
| 13 | |||
| 14 | /* Opentracker */ | ||
| 15 | #include "trackerlogic.h" | ||
| 16 | #include "ot_mutex.h" | ||
| 17 | #include "ot_stats.h" | ||
| 18 | |||
| 19 | /* Converter function from memory to human readable hex strings */ | ||
| 20 | static char*to_hex(char*d,ot_byte*s){const char*m="0123456789ABCDEF";char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return d;} | ||
| 21 | |||
| 22 | typedef struct { size_t val; ot_torrent * torrent; } ot_record; | ||
| 23 | |||
| 24 | /* Fetches stats from tracker */ | ||
| 25 | size_t return_stats_for_tracker( char *reply, int mode ) { | ||
| 26 | size_t torrent_count = 0, peer_count = 0, seed_count = 0, j; | ||
| 27 | ot_record top5s[5], top5c[5]; | ||
| 28 | char *r = reply; | ||
| 29 | int bucket; | ||
| 30 | |||
| 31 | byte_zero( top5s, sizeof( top5s ) ); | ||
| 32 | byte_zero( top5c, sizeof( top5c ) ); | ||
| 33 | |||
| 34 | for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { | ||
| 35 | ot_vector *torrents_list = mutex_bucket_lock( bucket ); | ||
| 36 | torrent_count += torrents_list->size; | ||
| 37 | for( j=0; j<torrents_list->size; ++j ) { | ||
| 38 | ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; | ||
| 39 | if( mode == STATS_TOP5 ) { | ||
| 40 | int idx = 4; while( (idx >= 0) && ( peer_list->peer_count > top5c[idx].val ) ) --idx; | ||
| 41 | if ( idx++ != 4 ) { | ||
| 42 | memmove( top5c + idx + 1, top5c + idx, ( 4 - idx ) * sizeof( ot_record ) ); | ||
| 43 | top5c[idx].val = peer_list->peer_count; | ||
| 44 | top5c[idx].torrent = (ot_torrent*)(torrents_list->data) + j; | ||
| 45 | } | ||
| 46 | idx = 4; while( (idx >= 0) && ( peer_list->seed_count > top5s[idx].val ) ) --idx; | ||
| 47 | if ( idx++ != 4 ) { | ||
| 48 | memmove( top5s + idx + 1, top5s + idx, ( 4 - idx ) * sizeof( ot_record ) ); | ||
| 49 | top5s[idx].val = peer_list->seed_count; | ||
| 50 | top5s[idx].torrent = (ot_torrent*)(torrents_list->data) + j; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | peer_count += peer_list->peer_count; seed_count += peer_list->seed_count; | ||
| 54 | } | ||
| 55 | mutex_bucket_unlock( bucket ); | ||
| 56 | } | ||
| 57 | if( mode == STATS_TOP5 ) { | ||
| 58 | char hex_out[42]; | ||
| 59 | int idx; | ||
| 60 | r += sprintf( r, "Top5 torrents by peers:\n" ); | ||
| 61 | for( idx=0; idx<5; ++idx ) | ||
| 62 | if( top5c[idx].torrent ) | ||
| 63 | r += sprintf( r, "\t%zd\t%s\n", top5c[idx].val, to_hex( hex_out, top5c[idx].torrent->hash) ); | ||
| 64 | r += sprintf( r, "Top5 torrents by seeds:\n" ); | ||
| 65 | for( idx=0; idx<5; ++idx ) | ||
| 66 | if( top5s[idx].torrent ) | ||
| 67 | r += sprintf( r, "\t%zd\t%s\n", top5s[idx].val, to_hex( hex_out, top5s[idx].torrent->hash) ); | ||
| 68 | } else | ||
| 69 | r += sprintf( r, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker", peer_count, seed_count, torrent_count ); | ||
| 70 | |||
| 71 | return r - reply; | ||
| 72 | } | ||
| 73 | |||
| 74 | /* This function collects 4096 /24s in 4096 possible | ||
| 75 | malloc blocks | ||
| 76 | */ | ||
| 77 | size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ) { | ||
| 78 | |||
| 79 | #define NUM_TOPBITS 12 | ||
| 80 | #define NUM_LOWBITS (24-NUM_TOPBITS) | ||
| 81 | #define NUM_BUFS (1<<NUM_TOPBITS) | ||
| 82 | #define NUM_S24S (1<<NUM_LOWBITS) | ||
| 83 | #define MSK_S24S (NUM_S24S-1) | ||
| 84 | |||
| 85 | ot_dword *counts[ NUM_BUFS ]; | ||
| 86 | ot_dword slash24s[amount*2]; /* first dword amount, second dword subnet */ | ||
| 87 | int bucket; | ||
| 88 | size_t i, j, k, l; | ||
| 89 | char *r = reply; | ||
| 90 | |||
| 91 | byte_zero( counts, sizeof( counts ) ); | ||
| 92 | byte_zero( slash24s, amount * 2 * sizeof(ot_dword) ); | ||
| 93 | |||
| 94 | r += sprintf( r, "Stats for all /24s with more than %u announced torrents:\n\n", thresh ); | ||
| 95 | |||
| 96 | for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { | ||
| 97 | ot_vector *torrents_list = mutex_bucket_lock( bucket ); | ||
| 98 | for( j=0; j<torrents_list->size; ++j ) { | ||
| 99 | ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; | ||
| 100 | for( k=0; k<OT_POOLS_COUNT; ++k ) { | ||
| 101 | ot_peer *peers = peer_list->peers[k].data; | ||
| 102 | size_t numpeers = peer_list->peers[k].size; | ||
| 103 | for( l=0; l<numpeers; ++l ) { | ||
| 104 | ot_dword s24 = ntohl(*(ot_dword*)(peers+l)) >> 8; | ||
| 105 | ot_dword *count = counts[ s24 >> NUM_LOWBITS ]; | ||
| 106 | if( !count ) { | ||
| 107 | count = malloc( sizeof(ot_dword) * NUM_S24S ); | ||
| 108 | if( !count ) | ||
| 109 | goto bailout_cleanup; | ||
| 110 | byte_zero( count, sizeof( ot_dword ) * NUM_S24S ); | ||
| 111 | counts[ s24 >> NUM_LOWBITS ] = count; | ||
| 112 | } | ||
| 113 | count[ s24 & MSK_S24S ]++; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | mutex_bucket_unlock( bucket ); | ||
| 118 | } | ||
| 119 | |||
| 120 | k = l = 0; /* Debug: count allocated bufs */ | ||
| 121 | for( i=0; i < NUM_BUFS; ++i ) { | ||
| 122 | ot_dword *count = counts[i]; | ||
| 123 | if( !counts[i] ) | ||
| 124 | continue; | ||
| 125 | ++k; /* Debug: count allocated bufs */ | ||
| 126 | for( j=0; j < NUM_S24S; ++j ) { | ||
| 127 | if( count[j] > thresh ) { | ||
| 128 | /* This subnet seems to announce more torrents than the last in our list */ | ||
| 129 | int insert_pos = amount - 1; | ||
| 130 | while( ( insert_pos >= 0 ) && ( count[j] > slash24s[ 2 * insert_pos ] ) ) | ||
| 131 | --insert_pos; | ||
| 132 | ++insert_pos; | ||
| 133 | memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( ot_dword ) * ( amount - insert_pos - 1 ) ); | ||
| 134 | slash24s[ 2 * insert_pos ] = count[j]; | ||
| 135 | slash24s[ 2 * insert_pos + 1 ] = ( i << NUM_TOPBITS ) + j; | ||
| 136 | if( slash24s[ 2 * amount - 2 ] > thresh ) | ||
| 137 | thresh = slash24s[ 2 * amount - 2 ]; | ||
| 138 | } | ||
| 139 | if( count[j] ) ++l; | ||
| 140 | } | ||
| 141 | free( count ); | ||
| 142 | } | ||
| 143 | |||
| 144 | r += sprintf( r, "Allocated bufs: %zd, used s24s: %zd\n", k, l ); | ||
| 145 | |||
| 146 | for( i=0; i < amount; ++i ) | ||
| 147 | if( slash24s[ 2*i ] >= thresh ) { | ||
| 148 | ot_dword ip = slash24s[ 2*i +1 ]; | ||
| 149 | r += sprintf( r, "% 10ld %d.%d.%d.0/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) ); | ||
| 150 | } | ||
| 151 | |||
| 152 | return r - reply; | ||
| 153 | |||
| 154 | bailout_cleanup: | ||
| 155 | |||
| 156 | for( i=0; i < NUM_BUFS; ++i ) | ||
| 157 | free( counts[i] ); | ||
| 158 | |||
| 159 | return 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | size_t return_memstat_for_tracker( char **reply ) { | ||
| 163 | size_t torrent_count = 0, j; | ||
| 164 | size_t allocated, replysize; | ||
| 165 | ot_vector *torrents_list; | ||
| 166 | int bucket, k; | ||
| 167 | char *r; | ||
| 168 | |||
| 169 | for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { | ||
| 170 | torrents_list = mutex_bucket_lock(bucket); | ||
| 171 | torrent_count += torrents_list->size; | ||
| 172 | mutex_bucket_unlock(bucket); | ||
| 173 | } | ||
| 174 | |||
| 175 | allocated = OT_BUCKET_COUNT*32 + (43+OT_POOLS_COUNT*32)*torrent_count; | ||
| 176 | if( !( r = *reply = mmap( NULL, allocated, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) ) return 0; | ||
| 177 | |||
| 178 | for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { | ||
| 179 | torrents_list = mutex_bucket_lock(bucket); | ||
| 180 | r += sprintf( r, "%02X: %08X %08X\n", bucket, (unsigned int)torrents_list->size, (unsigned int)torrents_list->space ); | ||
| 181 | mutex_bucket_unlock(bucket); | ||
| 182 | } | ||
| 183 | |||
| 184 | for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { | ||
| 185 | ot_vector *torrents_list = mutex_bucket_lock(bucket); | ||
| 186 | char hex_out[42]; | ||
| 187 | for( j=0; j<torrents_list->size; ++j ) { | ||
| 188 | ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; | ||
| 189 | ot_hash *hash =&( ((ot_torrent*)(torrents_list->data))[j] ).hash; | ||
| 190 | r += sprintf( r, "\n%s:\n", to_hex( hex_out, (ot_byte*)hash) ); | ||
| 191 | for( k=0; k<OT_POOLS_COUNT; ++k ) | ||
| 192 | r += sprintf( r, "\t%05X %05X\n", ((unsigned int)peer_list->peers[k].size), (unsigned int)peer_list->peers[k].space ); | ||
| 193 | } | ||
| 194 | mutex_bucket_unlock(bucket); | ||
| 195 | } | ||
| 196 | |||
| 197 | replysize = ( r - *reply ); | ||
| 198 | fix_mmapallocation( *reply, allocated, replysize ); | ||
| 199 | |||
| 200 | return replysize; | ||
| 201 | } \ No newline at end of file | ||
