summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
authorerdgeist <>2007-12-03 01:05:32 +0000
committererdgeist <>2007-12-03 01:05:32 +0000
commit9bc0d99c6273e845c98dad9f7fc202b695055c9c (patch)
treedd6eba238d38d2216634e9dcc922c326ae8fd8ee /ot_stats.c
parent598b68561b4a290fc0eed486aa2f74042b5e7af7 (diff)
Drop ot_{byte,word,dword} and use uint{8,16,32}_t, also move run time keeping and FULLSCRAPE stderr output to ot_stats
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c65
1 files changed, 53 insertions, 12 deletions
diff --git a/ot_stats.c b/ot_stats.c
index 44e24fd..59268b1 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -11,12 +11,19 @@
11 11
12/* Libowfat */ 12/* Libowfat */
13#include "byte.h" 13#include "byte.h"
14#include "io.h"
14 15
15/* Opentracker */ 16/* Opentracker */
16#include "trackerlogic.h" 17#include "trackerlogic.h"
17#include "ot_mutex.h" 18#include "ot_mutex.h"
18#include "ot_stats.h" 19#include "ot_stats.h"
19 20
21#ifndef NO_FULLSCRAPE_LOGGING
22#define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ )
23#else
24#define LOG_TO_STDERR( ... )
25#endif
26
20/* Clumsy counters... to be rethought */ 27/* Clumsy counters... to be rethought */
21static unsigned long long ot_overall_tcp_connections = 0; 28static unsigned long long ot_overall_tcp_connections = 0;
22static unsigned long long ot_overall_udp_connections = 0; 29static unsigned long long ot_overall_udp_connections = 0;
@@ -27,10 +34,13 @@ static unsigned long long ot_overall_udp_successfulscrapes = 0;
27static unsigned long long ot_overall_tcp_connects = 0; 34static unsigned long long ot_overall_tcp_connects = 0;
28static unsigned long long ot_overall_udp_connects = 0; 35static unsigned long long ot_overall_udp_connects = 0;
29static unsigned long long ot_full_scrape_count = 0; 36static unsigned long long ot_full_scrape_count = 0;
37static unsigned long long ot_full_scrape_request_count = 0;
30static unsigned long long ot_full_scrape_size = 0; 38static unsigned long long ot_full_scrape_size = 0;
31 39
40static time_t ot_start_time;
41
32/* Converter function from memory to human readable hex strings */ 42/* Converter function from memory to human readable hex strings */
33static char*to_hex(char*d,ot_byte*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;} 43static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;}
34 44
35typedef struct { size_t val; ot_torrent * torrent; } ot_record; 45typedef struct { size_t val; ot_torrent * torrent; } ot_record;
36 46
@@ -79,7 +89,7 @@ size_t stats_top5_txt( char * reply ) {
79/* This function collects 4096 /24s in 4096 possible 89/* This function collects 4096 /24s in 4096 possible
80 malloc blocks 90 malloc blocks
81*/ 91*/
82static size_t stats_slash24s_txt( char * reply, size_t amount, ot_dword thresh ) { 92static size_t stats_slash24s_txt( char * reply, size_t amount, uint32_t thresh ) {
83 93
84#define NUM_TOPBITS 12 94#define NUM_TOPBITS 12
85#define NUM_LOWBITS (24-NUM_TOPBITS) 95#define NUM_LOWBITS (24-NUM_TOPBITS)
@@ -87,14 +97,14 @@ static size_t stats_slash24s_txt( char * reply, size_t amount, ot_dword thresh )
87#define NUM_S24S (1<<NUM_LOWBITS) 97#define NUM_S24S (1<<NUM_LOWBITS)
88#define MSK_S24S (NUM_S24S-1) 98#define MSK_S24S (NUM_S24S-1)
89 99
90 ot_dword *counts[ NUM_BUFS ]; 100 uint32_t *counts[ NUM_BUFS ];
91 ot_dword slash24s[amount*2]; /* first dword amount, second dword subnet */ 101 uint32_t slash24s[amount*2]; /* first dword amount, second dword subnet */
92 int bucket; 102 int bucket;
93 size_t i, j, k, l; 103 size_t i, j, k, l;
94 char *r = reply; 104 char *r = reply;
95 105
96 byte_zero( counts, sizeof( counts ) ); 106 byte_zero( counts, sizeof( counts ) );
97 byte_zero( slash24s, amount * 2 * sizeof(ot_dword) ); 107 byte_zero( slash24s, amount * 2 * sizeof(uint32_t) );
98 108
99 r += sprintf( r, "Stats for all /24s with more than %u announced torrents:\n\n", thresh ); 109 r += sprintf( r, "Stats for all /24s with more than %u announced torrents:\n\n", thresh );
100 110
@@ -106,13 +116,13 @@ static size_t stats_slash24s_txt( char * reply, size_t amount, ot_dword thresh )
106 ot_peer *peers = peer_list->peers[k].data; 116 ot_peer *peers = peer_list->peers[k].data;
107 size_t numpeers = peer_list->peers[k].size; 117 size_t numpeers = peer_list->peers[k].size;
108 for( l=0; l<numpeers; ++l ) { 118 for( l=0; l<numpeers; ++l ) {
109 ot_dword s24 = ntohl(*(ot_dword*)(peers+l)) >> 8; 119 uint32_t s24 = ntohl(*(uint32_t*)(peers+l)) >> 8;
110 ot_dword *count = counts[ s24 >> NUM_LOWBITS ]; 120 uint32_t *count = counts[ s24 >> NUM_LOWBITS ];
111 if( !count ) { 121 if( !count ) {
112 count = malloc( sizeof(ot_dword) * NUM_S24S ); 122 count = malloc( sizeof(uint32_t) * NUM_S24S );
113 if( !count ) 123 if( !count )
114 goto bailout_cleanup; 124 goto bailout_cleanup;
115 byte_zero( count, sizeof( ot_dword ) * NUM_S24S ); 125 byte_zero( count, sizeof( uint32_t ) * NUM_S24S );
116 counts[ s24 >> NUM_LOWBITS ] = count; 126 counts[ s24 >> NUM_LOWBITS ] = count;
117 } 127 }
118 count[ s24 & MSK_S24S ]++; 128 count[ s24 & MSK_S24S ]++;
@@ -124,7 +134,7 @@ static size_t stats_slash24s_txt( char * reply, size_t amount, ot_dword thresh )
124 134
125 k = l = 0; /* Debug: count allocated bufs */ 135 k = l = 0; /* Debug: count allocated bufs */
126 for( i=0; i < NUM_BUFS; ++i ) { 136 for( i=0; i < NUM_BUFS; ++i ) {
127 ot_dword *count = counts[i]; 137 uint32_t *count = counts[i];
128 if( !counts[i] ) 138 if( !counts[i] )
129 continue; 139 continue;
130 ++k; /* Debug: count allocated bufs */ 140 ++k; /* Debug: count allocated bufs */
@@ -135,7 +145,7 @@ static size_t stats_slash24s_txt( char * reply, size_t amount, ot_dword thresh )
135 while( ( insert_pos >= 0 ) && ( count[j] > slash24s[ 2 * insert_pos ] ) ) 145 while( ( insert_pos >= 0 ) && ( count[j] > slash24s[ 2 * insert_pos ] ) )
136 --insert_pos; 146 --insert_pos;
137 ++insert_pos; 147 ++insert_pos;
138 memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( ot_dword ) * ( amount - insert_pos - 1 ) ); 148 memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( uint32_t ) * ( amount - insert_pos - 1 ) );
139 slash24s[ 2 * insert_pos ] = count[j]; 149 slash24s[ 2 * insert_pos ] = count[j];
140 slash24s[ 2 * insert_pos + 1 ] = ( i << NUM_TOPBITS ) + j; 150 slash24s[ 2 * insert_pos + 1 ] = ( i << NUM_TOPBITS ) + j;
141 if( slash24s[ 2 * amount - 2 ] > thresh ) 151 if( slash24s[ 2 * amount - 2 ] > thresh )
@@ -150,7 +160,7 @@ static size_t stats_slash24s_txt( char * reply, size_t amount, ot_dword thresh )
150 160
151 for( i=0; i < amount; ++i ) 161 for( i=0; i < amount; ++i )
152 if( slash24s[ 2*i ] >= thresh ) { 162 if( slash24s[ 2*i ] >= thresh ) {
153 ot_dword ip = slash24s[ 2*i +1 ]; 163 uint32_t ip = slash24s[ 2*i +1 ];
154 r += sprintf( r, "% 10ld %d.%d.%d.0/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) ); 164 r += sprintf( r, "% 10ld %d.%d.%d.0/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) );
155 } 165 }
156 166
@@ -284,6 +294,9 @@ void stats_issue_event( ot_status_event event, int is_tcp, size_t event_data ) {
284 case EVENT_ANNOUNCE: 294 case EVENT_ANNOUNCE:
285 if( is_tcp ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++; 295 if( is_tcp ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++;
286 break; 296 break;
297 case EVENT_CONNECT:
298 if( is_tcp ) ot_overall_tcp_connects++; else ot_overall_udp_connects++;
299 break;
287 case EVENT_SCRAPE: 300 case EVENT_SCRAPE:
288 if( is_tcp ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++; 301 if( is_tcp ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++;
289 case EVENT_CONNECT: 302 case EVENT_CONNECT:
@@ -292,7 +305,35 @@ void stats_issue_event( ot_status_event event, int is_tcp, size_t event_data ) {
292 ot_full_scrape_count++; 305 ot_full_scrape_count++;
293 ot_full_scrape_size += event_data; 306 ot_full_scrape_size += event_data;
294 break; 307 break;
308 case EVENT_FULLSCRAPE_REQUEST:
309 {
310 char ip[4]; *(int*)ip = is_tcp; /* ugly hack to transfer ip to stats */
311 LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE\n", (unsigned int)(g_now - ot_start_time), ip[0], ip[1], ip[2], ip[3] );
312 ot_full_scrape_request_count++;
313 }
314 break;
315 case EVENT_FULLSCRAPE_REQUEST_GZIP:
316 {
317 char ip[4]; *(int*)ip = is_tcp; /* ugly hack to transfer ip to stats */
318 LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE GZIP\n", (unsigned int)(g_now - ot_start_time), ip[0], ip[1], ip[2], ip[3] );
319 ot_full_scrape_request_count++;
320 }
321 break;
322 case EVENT_SYNC_IN_REQUEST:
323 case EVENT_SYNC_IN:
324 case EVENT_SYNC_OUT_REQUEST:
325 case EVENT_SYNC_OUT:
326 break;
295 default: 327 default:
296 break; 328 break;
297 } 329 }
298} 330}
331
332void stats_init( ) {
333 ot_start_time = g_now;
334}
335
336void stats_deinit( ) {
337
338}
339