summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c992
1 files changed, 495 insertions, 497 deletions
diff --git a/ot_stats.c b/ot_stats.c
index 83cd058..158884f 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -4,16 +4,16 @@
4 $id$ */ 4 $id$ */
5 5
6/* System */ 6/* System */
7#include <stdlib.h>
8#include <arpa/inet.h> 7#include <arpa/inet.h>
9#include <sys/types.h> 8#include <inttypes.h>
10#include <sys/uio.h> 9#include <pthread.h>
11#include <sys/mman.h>
12#include <stdio.h> 10#include <stdio.h>
11#include <stdlib.h>
13#include <string.h> 12#include <string.h>
14#include <pthread.h> 13#include <sys/mman.h>
14#include <sys/types.h>
15#include <sys/uio.h>
15#include <unistd.h> 16#include <unistd.h>
16#include <inttypes.h>
17#ifdef WANT_SYSLOGS 17#ifdef WANT_SYSLOGS
18#include <syslog.h> 18#include <syslog.h>
19#endif 19#endif
@@ -25,61 +25,63 @@
25#include "ip6.h" 25#include "ip6.h"
26 26
27/* Opentracker */ 27/* Opentracker */
28#include "trackerlogic.h" 28#include "ot_accesslist.h"
29#include "ot_mutex.h"
30#include "ot_iovec.h" 29#include "ot_iovec.h"
30#include "ot_mutex.h"
31#include "ot_stats.h" 31#include "ot_stats.h"
32#include "ot_accesslist.h" 32#include "trackerlogic.h"
33 33
34#ifndef NO_FULLSCRAPE_LOGGING 34#ifndef NO_FULLSCRAPE_LOGGING
35#define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ ) 35#define LOG_TO_STDERR(...) fprintf(stderr, __VA_ARGS__)
36#else 36#else
37#define LOG_TO_STDERR( ... ) 37#define LOG_TO_STDERR(...)
38#endif 38#endif
39 39
40/* Forward declaration */ 40/* Forward declaration */
41static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ); 41static void stats_make(int *iovec_entries, struct iovec **iovector, ot_tasktype mode);
42#define OT_STATS_TMPSIZE 8192 42#define OT_STATS_TMPSIZE 8192
43 43
44/* Clumsy counters... to be rethought */ 44/* Clumsy counters... to be rethought */
45static unsigned long long ot_overall_tcp_connections = 0; 45static unsigned long long ot_overall_tcp_connections;
46static unsigned long long ot_overall_udp_connections = 0; 46static unsigned long long ot_overall_udp_connections;
47static unsigned long long ot_overall_tcp_successfulannounces = 0; 47static unsigned long long ot_overall_tcp_successfulannounces;
48static unsigned long long ot_overall_udp_successfulannounces = 0; 48static unsigned long long ot_overall_udp_successfulannounces;
49static unsigned long long ot_overall_tcp_successfulscrapes = 0; 49static unsigned long long ot_overall_tcp_successfulscrapes;
50static unsigned long long ot_overall_udp_successfulscrapes = 0; 50static unsigned long long ot_overall_udp_successfulscrapes;
51static unsigned long long ot_overall_udp_connectionidmissmatches = 0; 51static unsigned long long ot_overall_udp_connectionidmissmatches;
52static unsigned long long ot_overall_tcp_connects = 0; 52static unsigned long long ot_overall_tcp_connects;
53static unsigned long long ot_overall_udp_connects = 0; 53static unsigned long long ot_overall_udp_connects;
54static unsigned long long ot_overall_completed = 0; 54static unsigned long long ot_overall_completed;
55static unsigned long long ot_full_scrape_count = 0; 55static unsigned long long ot_full_scrape_count;
56static unsigned long long ot_full_scrape_request_count = 0; 56static unsigned long long ot_full_scrape_request_count;
57static unsigned long long ot_full_scrape_size = 0; 57static unsigned long long ot_full_scrape_size;
58static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT]; 58static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT];
59static char * ot_failed_request_names[] = { "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest", "402 Payment Required", "403 Access Denied", "404 Not found", "500 Internal Server Error" }; 59static char *ot_failed_request_names[] = {
60 "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest",
61 "402 Payment Required", "403 Access Denied", "404 Not found", "500 Internal Server Error"};
60static unsigned long long ot_renewed[OT_PEER_TIMEOUT]; 62static unsigned long long ot_renewed[OT_PEER_TIMEOUT];
61static unsigned long long ot_overall_sync_count; 63static unsigned long long ot_overall_sync_count;
62static unsigned long long ot_overall_stall_count; 64static unsigned long long ot_overall_stall_count;
63 65
64static time_t ot_start_time; 66static time_t ot_start_time;
65 67
66#define STATS_NETWORK_NODE_BITWIDTH 4 68#define STATS_NETWORK_NODE_BITWIDTH 4
67#define STATS_NETWORK_NODE_COUNT (1<<STATS_NETWORK_NODE_BITWIDTH) 69#define STATS_NETWORK_NODE_COUNT (1 << STATS_NETWORK_NODE_BITWIDTH)
68 70
69#define __BYTE(P,D) (((uint8_t*)P)[D/8]) 71#define __BYTE(P, D) (((uint8_t *)P)[D / 8])
70#define __MSK (STATS_NETWORK_NODE_COUNT-1) 72#define __MSK (STATS_NETWORK_NODE_COUNT - 1)
71#define __SHFT(D) ((D^STATS_NETWORK_NODE_BITWIDTH)&STATS_NETWORK_NODE_BITWIDTH) 73#define __SHFT(D) ((D ^ STATS_NETWORK_NODE_BITWIDTH) & STATS_NETWORK_NODE_BITWIDTH)
72 74
73#define __LDR(P,D) ((__BYTE((P),(D))>>__SHFT((D)))&__MSK) 75#define __LDR(P, D) ((__BYTE((P), (D)) >> __SHFT((D))) & __MSK)
74#define __STR(P,D,V) __BYTE((P),(D))=(__BYTE((P),(D))&~(__MSK<<__SHFT((D))))|((V)<<__SHFT((D))) 76#define __STR(P, D, V) __BYTE((P), (D)) = (__BYTE((P), (D)) & ~(__MSK << __SHFT((D)))) | ((V) << __SHFT((D)))
75 77
76#ifdef WANT_V6 78#if 0
77#define STATS_NETWORK_NODE_MAXDEPTH (68-STATS_NETWORK_NODE_BITWIDTH) 79// XXX
78#define STATS_NETWORK_NODE_LIMIT (48-STATS_NETWORK_NODE_BITWIDTH) 80#define STATS_NETWORK_NODE_MAXDEPTH (68 - STATS_NETWORK_NODE_BITWIDTH)
79#else 81#define STATS_NETWORK_NODE_LIMIT (48 - STATS_NETWORK_NODE_BITWIDTH)
80#define STATS_NETWORK_NODE_MAXDEPTH (28-STATS_NETWORK_NODE_BITWIDTH)
81#define STATS_NETWORK_NODE_LIMIT (24-STATS_NETWORK_NODE_BITWIDTH)
82#endif 82#endif
83#define STATS_NETWORK_NODE_MAXDEPTH (28 - STATS_NETWORK_NODE_BITWIDTH)
84#define STATS_NETWORK_NODE_LIMIT (24 - STATS_NETWORK_NODE_BITWIDTH)
83 85
84typedef union stats_network_node stats_network_node; 86typedef union stats_network_node stats_network_node;
85union stats_network_node { 87union stats_network_node {
@@ -91,120 +93,125 @@ union stats_network_node {
91static stats_network_node *stats_network_counters_root; 93static stats_network_node *stats_network_counters_root;
92#endif 94#endif
93 95
94static int stat_increase_network_count( stats_network_node **pnode, int depth, uintptr_t ip ) { 96static int stat_increase_network_count(stats_network_node **pnode, int depth, uintptr_t ip) {
95 int foo = __LDR(ip,depth); 97 int foo = __LDR(ip, depth);
96 stats_network_node *node; 98 stats_network_node *node;
97 99
98 if( !*pnode ) { 100 if (!*pnode) {
99 *pnode = malloc( sizeof( stats_network_node ) ); 101 *pnode = malloc(sizeof(stats_network_node));
100 if( !*pnode ) 102 if (!*pnode)
101 return -1; 103 return -1;
102 memset( *pnode, 0, sizeof( stats_network_node ) ); 104 memset(*pnode, 0, sizeof(stats_network_node));
103 } 105 }
104 node = *pnode; 106 node = *pnode;
105 107
106 if( depth < STATS_NETWORK_NODE_MAXDEPTH ) 108 if (depth < STATS_NETWORK_NODE_MAXDEPTH)
107 return stat_increase_network_count( node->children + foo, depth+STATS_NETWORK_NODE_BITWIDTH, ip ); 109 return stat_increase_network_count(node->children + foo, depth + STATS_NETWORK_NODE_BITWIDTH, ip);
108 110
109 node->counters[ foo ]++; 111 node->counters[foo]++;
110 return 0; 112 return 0;
111} 113}
112 114
113static int stats_shift_down_network_count( stats_network_node **node, int depth, int shift ) { 115static int stats_shift_down_network_count(stats_network_node **node, int depth, int shift) {
114 int i, rest = 0; 116 int i, rest = 0;
115 117
116 if( !*node ) 118 if (!*node)
117 return 0; 119 return 0;
118 120
119 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) 121 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
120 if( depth < STATS_NETWORK_NODE_MAXDEPTH ) 122 if (depth < STATS_NETWORK_NODE_MAXDEPTH)
121 rest += stats_shift_down_network_count( (*node)->children + i, depth+STATS_NETWORK_NODE_BITWIDTH, shift ); 123 rest += stats_shift_down_network_count((*node)->children + i, depth + STATS_NETWORK_NODE_BITWIDTH, shift);
122 else 124 else
123 rest += (*node)->counters[i] >>= shift; 125 rest += (*node)->counters[i] >>= shift;
124 126
125 if( !rest ) { 127 if (!rest) {
126 free( *node ); 128 free(*node);
127 *node = NULL; 129 *node = NULL;
128 } 130 }
129 131
130 return rest; 132 return rest;
131} 133}
132 134
133static size_t stats_get_highscore_networks( stats_network_node *node, int depth, ot_ip6 node_value, size_t *scores, ot_ip6 *networks, int network_count, int limit ) { 135static size_t stats_get_highscore_networks(stats_network_node *node, int depth, ot_ip6 node_value, size_t *scores, ot_ip6 *networks, int network_count,
136 int limit) {
134 size_t score = 0; 137 size_t score = 0;
135 int i; 138 int i;
136 139
137 if( !node ) return 0; 140 if (!node)
141 return 0;
138 142
139 if( depth < limit ) { 143 if (depth < limit) {
140 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) 144 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
141 if( node->children[i] ) { 145 if (node->children[i]) {
142 __STR(node_value,depth,i); 146 __STR(node_value, depth, i);
143 score += stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit ); 147 score += stats_get_highscore_networks(node->children[i], depth + STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit);
144 } 148 }
145 return score; 149 return score;
146 } 150 }
147 151
148 if( depth > limit && depth < STATS_NETWORK_NODE_MAXDEPTH ) { 152 if (depth > limit && depth < STATS_NETWORK_NODE_MAXDEPTH) {
149 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) 153 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
150 if( node->children[i] ) 154 if (node->children[i])
151 score += stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit ); 155 score += stats_get_highscore_networks(node->children[i], depth + STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit);
152 return score; 156 return score;
153 } 157 }
154 158
155 if( depth > limit && depth == STATS_NETWORK_NODE_MAXDEPTH ) { 159 if (depth > limit && depth == STATS_NETWORK_NODE_MAXDEPTH) {
156 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) 160 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
157 score += node->counters[i]; 161 score += node->counters[i];
158 return score; 162 return score;
159 } 163 }
160 164
161 /* if( depth == limit ) */ 165 /* if( depth == limit ) */
162 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) { 166 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i) {
163 int j=1; 167 int j = 1;
164 size_t node_score; 168 size_t node_score;
165 169
166 if( depth == STATS_NETWORK_NODE_MAXDEPTH ) 170 if (depth == STATS_NETWORK_NODE_MAXDEPTH)
167 node_score = node->counters[i]; 171 node_score = node->counters[i];
168 else 172 else
169 node_score = stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit ); 173 node_score = stats_get_highscore_networks(node->children[i], depth + STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit);
170 174
171 score += node_score; 175 score += node_score;
172 176
173 if( node_score <= scores[0] ) continue; 177 if (node_score <= scores[0])
178 continue;
174 179
175 __STR(node_value,depth,i); 180 __STR(node_value, depth, i);
176 while( j < network_count && node_score > scores[j] ) ++j; 181 while (j < network_count && node_score > scores[j])
182 ++j;
177 --j; 183 --j;
178 184
179 memcpy( scores, scores + 1, j * sizeof( *scores ) ); 185 memcpy(scores, scores + 1, j * sizeof(*scores));
180 memcpy( networks, networks + 1, j * sizeof( *networks ) ); 186 memcpy(networks, networks + 1, j * sizeof(*networks));
181 scores[ j ] = node_score; 187 scores[j] = node_score;
182 memcpy( networks + j, node_value, sizeof( *networks ) ); 188 memcpy(networks + j, node_value, sizeof(*networks));
183 } 189 }
184 190
185 return score; 191 return score;
186} 192}
187 193
188static size_t stats_return_busy_networks( char * reply, stats_network_node *tree, int amount, int limit ) { 194static size_t stats_return_busy_networks(char *reply, stats_network_node *tree, int amount, int limit) {
189 ot_ip6 networks[amount]; 195 ot_ip6 networks[amount];
190 ot_ip6 node_value; 196 ot_ip6 node_value;
191 size_t scores[amount]; 197 size_t scores[amount];
192 int i; 198 int i;
193 char * r = reply; 199 char *r = reply;
194 200
195 memset( scores, 0, sizeof( scores ) ); 201 memset(scores, 0, sizeof(scores));
196 memset( networks, 0, sizeof( networks ) ); 202 memset(networks, 0, sizeof(networks));
197 memset( node_value, 0, sizeof( node_value ) ); 203 memset(node_value, 0, sizeof(node_value));
198 204
199 stats_get_highscore_networks( tree, 0, node_value, scores, networks, amount, limit ); 205 stats_get_highscore_networks(tree, 0, node_value, scores, networks, amount, limit);
200 206
201 r += sprintf( r, "Networks, limit /%d:\n", limit+STATS_NETWORK_NODE_BITWIDTH ); 207 r += sprintf(r, "Networks, limit /%d:\n", limit + STATS_NETWORK_NODE_BITWIDTH);
202 for( i=amount-1; i>=0; --i) { 208 for (i = amount - 1; i >= 0; --i) {
203 if( scores[i] ) { 209 if (scores[i]) {
204 r += sprintf( r, "%08zd: ", scores[i] ); 210 r += sprintf(r, "%08zd: ", scores[i]);
205#ifdef WANT_V6 211 // #ifdef WANT_V6
206 r += fmt_ip6c( r, networks[i] ); 212 r += fmt_ip6c(r, networks[i]);
207#else 213#if 0
214 // XXX
208 r += fmt_ip4( r, networks[i]); 215 r += fmt_ip4( r, networks[i]);
209#endif 216#endif
210 *r++ = '\n'; 217 *r++ = '\n';
@@ -215,64 +222,66 @@ static size_t stats_return_busy_networks( char * reply, stats_network_node *tree
215 return r - reply; 222 return r - reply;
216} 223}
217 224
218static size_t stats_slash24s_txt( char *reply, size_t amount ) { 225static size_t stats_slash24s_txt(char *reply, size_t amount) {
219 stats_network_node *slash24s_network_counters_root = NULL; 226 stats_network_node *slash24s_network_counters_root = NULL;
220 char *r=reply; 227 char *r = reply;
221 int bucket; 228 int bucket;
222 size_t i; 229 size_t i, peer_size = OT_PEER_SIZE4;
223 230
224 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { 231 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
225 ot_vector *torrents_list = mutex_bucket_lock( bucket ); 232 ot_vector *torrents_list = mutex_bucket_lock(bucket);
226 for( i=0; i<torrents_list->size; ++i ) { 233 for (i = 0; i < torrents_list->size; ++i) {
227 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[i] ).peer_list; 234 ot_peerlist *peer_list = (((ot_torrent *)(torrents_list->data))[i]).peer_list4;
228 ot_vector *bucket_list = &peer_list->peers; 235 ot_vector *bucket_list = &peer_list->peers;
229 int num_buckets = 1; 236 int num_buckets = 1;
230 237
231 if( OT_PEERLIST_HASBUCKETS( peer_list ) ) { 238 if (OT_PEERLIST_HASBUCKETS(peer_list)) {
232 num_buckets = bucket_list->size; 239 num_buckets = bucket_list->size;
233 bucket_list = (ot_vector *)bucket_list->data; 240 bucket_list = (ot_vector *)bucket_list->data;
234 } 241 }
235 242
236 while( num_buckets-- ) { 243 while (num_buckets--) {
237 ot_peer *peers = (ot_peer*)bucket_list->data; 244 ot_peer *peers = (ot_peer *)bucket_list->data;
238 size_t numpeers = bucket_list->size; 245 size_t numpeers = bucket_list->size;
239 while( numpeers-- ) 246 while (numpeers--) {
240 if( stat_increase_network_count( &slash24s_network_counters_root, 0, (uintptr_t)(peers++) ) ) 247 if (stat_increase_network_count(&slash24s_network_counters_root, 0, (uintptr_t)(peers)))
241 goto bailout_unlock; 248 goto bailout_unlock;
249 peers += peer_size;
250 }
242 ++bucket_list; 251 ++bucket_list;
243 } 252 }
244 } 253 }
245 mutex_bucket_unlock( bucket, 0 ); 254 mutex_bucket_unlock(bucket, 0);
246 if( !g_opentracker_running ) 255 if (!g_opentracker_running)
247 goto bailout_error; 256 goto bailout_error;
248 } 257 }
249 258
250 /* The tree is built. Now analyze */ 259 /* The tree is built. Now analyze */
251 r += stats_return_busy_networks( r, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_MAXDEPTH ); 260 r += stats_return_busy_networks(r, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_MAXDEPTH);
252 r += stats_return_busy_networks( r, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_LIMIT ); 261 r += stats_return_busy_networks(r, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_LIMIT);
253 goto success; 262 goto success;
254 263
255bailout_unlock: 264bailout_unlock:
256 mutex_bucket_unlock( bucket, 0 ); 265 mutex_bucket_unlock(bucket, 0);
257bailout_error: 266bailout_error:
258 r = reply; 267 r = reply;
259success: 268success:
260 stats_shift_down_network_count( &slash24s_network_counters_root, 0, sizeof(int)*8-1 ); 269 stats_shift_down_network_count(&slash24s_network_counters_root, 0, sizeof(int) * 8 - 1);
261 270
262 return r-reply; 271 return r - reply;
263} 272}
264 273
265#ifdef WANT_SPOT_WOODPECKER 274#ifdef WANT_SPOT_WOODPECKER
266static stats_network_node *stats_woodpeckers_tree; 275static stats_network_node *stats_woodpeckers_tree;
267static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER; 276static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER;
268 277
269static size_t stats_return_woodpeckers( char * reply, int amount ) { 278static size_t stats_return_woodpeckers(char *reply, int amount) {
270 char * r = reply; 279 char *r = reply;
271 280
272 pthread_mutex_lock( &g_woodpeckers_mutex ); 281 pthread_mutex_lock(&g_woodpeckers_mutex);
273 r += stats_return_busy_networks( r, stats_woodpeckers_tree, amount, STATS_NETWORK_NODE_MAXDEPTH ); 282 r += stats_return_busy_networks(r, stats_woodpeckers_tree, amount, STATS_NETWORK_NODE_MAXDEPTH);
274 pthread_mutex_unlock( &g_woodpeckers_mutex ); 283 pthread_mutex_unlock(&g_woodpeckers_mutex);
275 return r-reply; 284 return r - reply;
276} 285}
277#endif 286#endif
278 287
@@ -282,492 +291,481 @@ typedef struct {
282 unsigned long long seed_count; 291 unsigned long long seed_count;
283} torrent_stats; 292} torrent_stats;
284 293
285static int torrent_statter( ot_torrent *torrent, uintptr_t data ) { 294static int torrent_statter(ot_torrent *torrent, uintptr_t data) {
286 torrent_stats *stats = (torrent_stats*)data; 295 torrent_stats *stats = (torrent_stats *)data;
287 stats->torrent_count++; 296 stats->torrent_count++;
288 stats->peer_count += torrent->peer_list->peer_count; 297 stats->peer_count += torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
289 stats->seed_count += torrent->peer_list->seed_count; 298 stats->seed_count += torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
290 return 0; 299 return 0;
291} 300}
292 301
293/* Converter function from memory to human readable hex strings */ 302/* Converter function from memory to human readable hex strings */
294static 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;} 303static char *to_hex(char *d, uint8_t *s) {
304 char *m = "0123456789ABCDEF";
305 char *t = d;
306 char *e = d + 40;
307 while (d < e) {
308 *d++ = m[*s >> 4];
309 *d++ = m[*s++ & 15];
310 }
311 *d = 0;
312 return t;
313}
295 314
296typedef struct { size_t val; ot_torrent * torrent; } ot_record; 315typedef struct {
316 size_t val;
317 ot_hash hash;
318} ot_record;
297 319
298/* Fetches stats from tracker */ 320/* Fetches stats from tracker */
299size_t stats_top_txt( char * reply, int amount ) { 321size_t stats_top_txt(char *reply, int amount) {
300 size_t j; 322 size_t j;
301 ot_record top100s[100], top100c[100]; 323 ot_record top100s[100], top100c[100];
302 char *r = reply, hex_out[42]; 324 char *r = reply, hex_out[42];
303 int idx, bucket; 325 int idx, bucket;
304 326
305 if( amount > 100 ) 327 if (amount > 100)
306 amount = 100; 328 amount = 100;
307 329
308 byte_zero( top100s, sizeof( top100s ) ); 330 byte_zero(top100s, sizeof(top100s));
309 byte_zero( top100c, sizeof( top100c ) ); 331 byte_zero(top100c, sizeof(top100c));
310 332
311 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { 333 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
312 ot_vector *torrents_list = mutex_bucket_lock( bucket ); 334 ot_vector *torrents_list = mutex_bucket_lock(bucket);
313 for( j=0; j<torrents_list->size; ++j ) { 335 for (j = 0; j < torrents_list->size; ++j) {
314 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; 336 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j;
315 int idx = amount - 1; while( (idx >= 0) && ( peer_list->peer_count > top100c[idx].val ) ) --idx; 337 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
316 if ( idx++ != amount - 1 ) { 338 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
317 memmove( top100c + idx + 1, top100c + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); 339 idx = amount - 1;
318 top100c[idx].val = peer_list->peer_count; 340 while ((idx >= 0) && (peer_count > top100c[idx].val))
319 top100c[idx].torrent = (ot_torrent*)(torrents_list->data) + j; 341 --idx;
342 if (idx++ != amount - 1) {
343 memmove(top100c + idx + 1, top100c + idx, (amount - 1 - idx) * sizeof(ot_record));
344 memcpy(&top100c[idx].hash, &torrent->hash, sizeof(ot_hash));
345 top100c[idx].val = peer_count;
320 } 346 }
321 idx = amount - 1; while( (idx >= 0) && ( peer_list->seed_count > top100s[idx].val ) ) --idx; 347 idx = amount - 1;
322 if ( idx++ != amount - 1 ) { 348 while ((idx >= 0) && (seed_count > top100s[idx].val))
323 memmove( top100s + idx + 1, top100s + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); 349 --idx;
324 top100s[idx].val = peer_list->seed_count; 350 if (idx++ != amount - 1) {
325 top100s[idx].torrent = (ot_torrent*)(torrents_list->data) + j; 351 memmove(top100s + idx + 1, top100s + idx, (amount - 1 - idx) * sizeof(ot_record));
352 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash));
353 top100s[idx].val = seed_count;
326 } 354 }
327 } 355 }
328 mutex_bucket_unlock( bucket, 0 ); 356 mutex_bucket_unlock(bucket, 0);
329 if( !g_opentracker_running ) 357 if (!g_opentracker_running)
330 return 0; 358 return 0;
331 } 359 }
332 360
333 r += sprintf( r, "Top %d torrents by peers:\n", amount ); 361 r += sprintf(r, "Top %d torrents by peers:\n", amount);
334 for( idx=0; idx<amount; ++idx ) 362 for (idx = 0; idx < amount; ++idx)
335 if( top100c[idx].torrent ) 363 if (top100c[idx].val)
336 r += sprintf( r, "\t%zd\t%s\n", top100c[idx].val, to_hex( hex_out, top100c[idx].torrent->hash) ); 364 r += sprintf(r, "\t%zd\t%s\n", top100c[idx].val, to_hex(hex_out, top100c[idx].hash));
337 r += sprintf( r, "Top %d torrents by seeds:\n", amount ); 365 r += sprintf(r, "Top %d torrents by seeds:\n", amount);
338 for( idx=0; idx<amount; ++idx ) 366 for (idx = 0; idx < amount; ++idx)
339 if( top100s[idx].torrent ) 367 if (top100s[idx].val)
340 r += sprintf( r, "\t%zd\t%s\n", top100s[idx].val, to_hex( hex_out, top100s[idx].torrent->hash) ); 368 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash));
341 369
342 return r - reply; 370 return r - reply;
343} 371}
344 372
345static unsigned long events_per_time( unsigned long long events, time_t t ) { 373static unsigned long events_per_time(unsigned long long events, time_t t) { return events / ((unsigned int)t ? (unsigned int)t : 1); }
346 return events / ( (unsigned int)t ? (unsigned int)t : 1 );
347}
348 374
349static size_t stats_connections_mrtg( char * reply ) { 375static size_t stats_connections_mrtg(char *reply) {
350 ot_time t = time( NULL ) - ot_start_time; 376 ot_time t = time(NULL) - ot_start_time;
351 return sprintf( reply, 377 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
352 "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.", 378 ot_overall_tcp_connections + ot_overall_udp_connections,
353 ot_overall_tcp_connections+ot_overall_udp_connections, 379 ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, (int)t, (int)(t / 3600),
354 ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, 380 events_per_time(ot_overall_tcp_connections + ot_overall_udp_connections, t),
355 (int)t, 381 events_per_time(ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, t));
356 (int)(t / 3600),
357 events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
358 events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
359 );
360} 382}
361 383
362static size_t stats_udpconnections_mrtg( char * reply ) { 384static size_t stats_udpconnections_mrtg(char *reply) {
363 ot_time t = time( NULL ) - ot_start_time; 385 ot_time t = time(NULL) - ot_start_time;
364 return sprintf( reply, 386 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker udp4 stats, %lu conns/s :: %lu success/s.", ot_overall_udp_connections,
365 "%llu\n%llu\n%i seconds (%i hours)\nopentracker udp4 stats, %lu conns/s :: %lu success/s.", 387 ot_overall_udp_successfulannounces + ot_overall_udp_connects, (int)t, (int)(t / 3600), events_per_time(ot_overall_udp_connections, t),
366 ot_overall_udp_connections, 388 events_per_time(ot_overall_udp_successfulannounces + ot_overall_udp_connects, t));
367 ot_overall_udp_successfulannounces+ot_overall_udp_connects,
368 (int)t,
369 (int)(t / 3600),
370 events_per_time( ot_overall_udp_connections, t ),
371 events_per_time( ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
372 );
373} 389}
374 390
375static size_t stats_tcpconnections_mrtg( char * reply ) { 391static size_t stats_tcpconnections_mrtg(char *reply) {
376 time_t t = time( NULL ) - ot_start_time; 392 time_t t = time(NULL) - ot_start_time;
377 return sprintf( reply, 393 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker tcp4 stats, %lu conns/s :: %lu success/s.", ot_overall_tcp_connections,
378 "%llu\n%llu\n%i seconds (%i hours)\nopentracker tcp4 stats, %lu conns/s :: %lu success/s.", 394 ot_overall_tcp_successfulannounces, (int)t, (int)(t / 3600), events_per_time(ot_overall_tcp_connections, t),
379 ot_overall_tcp_connections, 395 events_per_time(ot_overall_tcp_successfulannounces, t));
380 ot_overall_tcp_successfulannounces,
381 (int)t,
382 (int)(t / 3600),
383 events_per_time( ot_overall_tcp_connections, t ),
384 events_per_time( ot_overall_tcp_successfulannounces, t )
385 );
386} 396}
387 397
388static size_t stats_scrape_mrtg( char * reply ) { 398static size_t stats_scrape_mrtg(char *reply) {
389 time_t t = time( NULL ) - ot_start_time; 399 time_t t = time(NULL) - ot_start_time;
390 return sprintf( reply, 400 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker scrape stats, %lu scrape/s (tcp and udp)", ot_overall_tcp_successfulscrapes,
391 "%llu\n%llu\n%i seconds (%i hours)\nopentracker scrape stats, %lu scrape/s (tcp and udp)", 401 ot_overall_udp_successfulscrapes, (int)t, (int)(t / 3600),
392 ot_overall_tcp_successfulscrapes, 402 events_per_time((ot_overall_tcp_successfulscrapes + ot_overall_udp_successfulscrapes), t));
393 ot_overall_udp_successfulscrapes,
394 (int)t,
395 (int)(t / 3600),
396 events_per_time( (ot_overall_tcp_successfulscrapes+ot_overall_udp_successfulscrapes), t )
397 );
398} 403}
399 404
400static size_t stats_fullscrapes_mrtg( char * reply ) { 405static size_t stats_fullscrapes_mrtg(char *reply) {
401 ot_time t = time( NULL ) - ot_start_time; 406 ot_time t = time(NULL) - ot_start_time;
402 return sprintf( reply, 407 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker full scrape stats, %lu conns/s :: %lu bytes/s.", ot_full_scrape_count * 1000,
403 "%llu\n%llu\n%i seconds (%i hours)\nopentracker full scrape stats, %lu conns/s :: %lu bytes/s.", 408 ot_full_scrape_size, (int)t, (int)(t / 3600), events_per_time(ot_full_scrape_count, t), events_per_time(ot_full_scrape_size, t));
404 ot_full_scrape_count * 1000,
405 ot_full_scrape_size,
406 (int)t,
407 (int)(t / 3600),
408 events_per_time( ot_full_scrape_count, t ),
409 events_per_time( ot_full_scrape_size, t )
410 );
411} 409}
412 410
413static size_t stats_peers_mrtg( char * reply ) { 411static size_t stats_peers_mrtg(char *reply) {
414 torrent_stats stats = {0,0,0}; 412 torrent_stats stats = {0, 0, 0};
415 413
416 iterate_all_torrents( torrent_statter, (uintptr_t)&stats ); 414 iterate_all_torrents(torrent_statter, (uintptr_t)&stats);
417 415
418 return sprintf( reply, "%llu\n%llu\nopentracker serving %llu torrents\nopentracker", 416 return sprintf(reply, "%llu\n%llu\nopentracker serving %llu torrents\nopentracker", stats.peer_count, stats.seed_count, stats.torrent_count);
419 stats.peer_count,
420 stats.seed_count,
421 stats.torrent_count
422 );
423} 417}
424 418
425static size_t stats_torrents_mrtg( char * reply ) 419static size_t stats_torrents_mrtg(char *reply) {
426{
427 size_t torrent_count = mutex_get_torrent_count(); 420 size_t torrent_count = mutex_get_torrent_count();
428 421
429 return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker", 422 return sprintf(reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker", torrent_count, (size_t)0, torrent_count);
430 torrent_count,
431 (size_t)0,
432 torrent_count
433 );
434} 423}
435 424
436static size_t stats_httperrors_txt ( char * reply ) { 425static size_t stats_httperrors_txt(char *reply) {
437 return sprintf( reply, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n", 426 return sprintf(reply, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n", ot_failed_request_counts[0],
438 ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2], 427 ot_failed_request_counts[1], ot_failed_request_counts[2], ot_failed_request_counts[3], ot_failed_request_counts[4],
439 ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5], 428 ot_failed_request_counts[5], ot_failed_request_counts[6]);
440 ot_failed_request_counts[6] );
441} 429}
442 430
443static size_t stats_return_renew_bucket( char * reply ) { 431static size_t stats_return_renew_bucket(char *reply) {
444 char *r = reply; 432 char *r = reply;
445 int i; 433 int i;
446 434
447 for( i=0; i<OT_PEER_TIMEOUT; ++i ) 435 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
448 r+=sprintf(r,"%02i %llu\n", i, ot_renewed[i] ); 436 r += sprintf(r, "%02i %llu\n", i, ot_renewed[i]);
449 return r - reply; 437 return r - reply;
450} 438}
451 439
452static size_t stats_return_sync_mrtg( char * reply ) { 440static size_t stats_return_sync_mrtg(char *reply) {
453 ot_time t = time( NULL ) - ot_start_time; 441 ot_time t = time(NULL) - ot_start_time;
454 return sprintf( reply, 442 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.", ot_overall_sync_count, 0LL, (int)t,
455 "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.", 443 (int)(t / 3600), events_per_time(ot_overall_tcp_connections + ot_overall_udp_connections, t),
456 ot_overall_sync_count, 444 events_per_time(ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, t));
457 0LL,
458 (int)t,
459 (int)(t / 3600),
460 events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
461 events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
462 );
463} 445}
464 446
465static size_t stats_return_completed_mrtg( char * reply ) { 447static size_t stats_return_completed_mrtg(char *reply) {
466 ot_time t = time( NULL ) - ot_start_time; 448 ot_time t = time(NULL) - ot_start_time;
467 449
468 return sprintf( reply, 450 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker, %lu completed/h.", ot_overall_completed, 0LL, (int)t, (int)(t / 3600),
469 "%llu\n%llu\n%i seconds (%i hours)\nopentracker, %lu completed/h.", 451 events_per_time(ot_overall_completed, t / 3600));
470 ot_overall_completed,
471 0LL,
472 (int)t,
473 (int)(t / 3600),
474 events_per_time( ot_overall_completed, t / 3600 )
475 );
476} 452}
477 453
478#ifdef WANT_LOG_NUMWANT 454#ifdef WANT_LOG_NUMWANT
479extern unsigned long long numwants[201]; 455extern unsigned long long numwants[201];
480static size_t stats_return_numwants( char * reply ) { 456static size_t stats_return_numwants(char *reply) {
481 char * r = reply; 457 char *r = reply;
482 int i; 458 int i;
483 for( i=0; i<=200; ++i ) 459 for (i = 0; i <= 200; ++i)
484 r += sprintf( r, "%03d => %lld\n", i, numwants[i] ); 460 r += sprintf(r, "%03d => %lld\n", i, numwants[i]);
485 return r-reply; 461 return r - reply;
486} 462}
487#endif 463#endif
488 464
489#ifdef WANT_FULLLOG_NETWORKS 465#ifdef WANT_FULLLOG_NETWORKS
490static void stats_return_fulllog( int *iovec_entries, struct iovec **iovector, char *r ) { 466static void stats_return_fulllog(int *iovec_entries, struct iovec **iovector, char *r) {
491 ot_log *loglist = g_logchain_first, *llnext; 467 ot_log *loglist = g_logchain_first, *llnext;
492 char * re = r + OT_STATS_TMPSIZE; 468 char *re = r + OT_STATS_TMPSIZE;
493 469
494 g_logchain_first = g_logchain_last = 0; 470 g_logchain_first = g_logchain_last = 0;
495 471
496 while( loglist ) { 472 while (loglist) {
497 if( r + ( loglist->size + 64 ) >= re ) { 473 if (r + (loglist->size + 64) >= re) {
498 r = iovec_fix_increase_or_free( iovec_entries, iovector, r, 32 * OT_STATS_TMPSIZE ); 474 r = iovec_fix_increase_or_free(iovec_entries, iovector, r, 32 * OT_STATS_TMPSIZE);
499 if( !r ) return; 475 if (!r)
476 return;
500 re = r + 32 * OT_STATS_TMPSIZE; 477 re = r + 32 * OT_STATS_TMPSIZE;
501 } 478 }
502 r += sprintf( r, "%08ld: ", loglist->time ); 479 r += sprintf(r, "%08ld: ", loglist->time);
503 r += fmt_ip6c( r, loglist->ip ); 480 r += fmt_ip6c(r, loglist->ip);
504 *r++ = '\n'; 481 *r++ = '\n';
505 memcpy( r, loglist->data, loglist->size ); 482 memcpy(r, loglist->data, loglist->size);
506 r += loglist->size; 483 r += loglist->size;
507 *r++ = '\n'; 484 *r++ = '\n';
508 *r++ = '*'; 485 *r++ = '*';
509 *r++ = '\n'; 486 *r++ = '\n';
510 *r++ = '\n'; 487 *r++ = '\n';
511 488
512 llnext = loglist->next; 489 llnext = loglist->next;
513 free( loglist->data ); 490 free(loglist->data);
514 free( loglist ); 491 free(loglist);
515 loglist = llnext; 492 loglist = llnext;
516 } 493 }
517 iovec_fixlast( iovec_entries, iovector, r ); 494 iovec_fixlast(iovec_entries, iovector, r);
518} 495}
519#endif 496#endif
520 497
521static size_t stats_return_everything( char * reply ) { 498static size_t stats_return_everything(char *reply) {
522 torrent_stats stats = {0,0,0}; 499 torrent_stats stats = {0, 0, 0};
523 int i; 500 int i;
524 char * r = reply; 501 char *r = reply;
525 502
526 iterate_all_torrents( torrent_statter, (uintptr_t)&stats ); 503 iterate_all_torrents(torrent_statter, (uintptr_t)&stats);
527 504
528 r += sprintf( r, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); 505 r += sprintf(r, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
529 r += sprintf( r, "<stats>\n" ); 506 r += sprintf(r, "<stats>\n");
530 r += sprintf( r, " <tracker_id>%" PRIu32 "</tracker_id>\n", g_tracker_id ); 507 r += sprintf(r, " <tracker_id>%" PRIu32 "</tracker_id>\n", g_tracker_id);
531 r += sprintf( r, " <version>\n" ); r += stats_return_tracker_version( r ); r += sprintf( r, " </version>\n" ); 508 r += sprintf(r, " <version>\n");
532 r += sprintf( r, " <uptime>%llu</uptime>\n", (unsigned long long)(time( NULL ) - ot_start_time) ); 509 r += stats_return_tracker_version(r);
533 r += sprintf( r, " <torrents>\n" ); 510 r += sprintf(r, " </version>\n");
534 r += sprintf( r, " <count_mutex>%zd</count_mutex>\n", mutex_get_torrent_count() ); 511 r += sprintf(r, " <uptime>%llu</uptime>\n", (unsigned long long)(time(NULL) - ot_start_time));
535 r += sprintf( r, " <count_iterator>%llu</count_iterator>\n", stats.torrent_count ); 512 r += sprintf(r, " <torrents>\n");
536 r += sprintf( r, " </torrents>\n" ); 513 r += sprintf(r, " <count_mutex>%zd</count_mutex>\n", mutex_get_torrent_count());
537 r += sprintf( r, " <peers>\n <count>%llu</count>\n </peers>\n", stats.peer_count ); 514 r += sprintf(r, " <count_iterator>%llu</count_iterator>\n", stats.torrent_count);
538 r += sprintf( r, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count ); 515 r += sprintf(r, " </torrents>\n");
539 r += sprintf( r, " <completed>\n <count>%llu</count>\n </completed>\n", ot_overall_completed ); 516 r += sprintf(r, " <peers>\n <count>%llu</count>\n </peers>\n", stats.peer_count);
540 r += sprintf( r, " <connections>\n" ); 517 r += sprintf(r, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count);
541 r += sprintf( r, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n", ot_overall_tcp_connections, ot_overall_tcp_successfulannounces, ot_overall_tcp_successfulscrapes ); 518 r += sprintf(r, " <completed>\n <count>%llu</count>\n </completed>\n", ot_overall_completed);
542 r += sprintf( r, " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n", ot_overall_udp_connections, ot_overall_udp_connects, ot_overall_udp_successfulannounces, ot_overall_udp_successfulscrapes, ot_overall_udp_connectionidmissmatches ); 519 r += sprintf(r, " <connections>\n");
543 r += sprintf( r, " <livesync>\n <count>%llu</count>\n </livesync>\n", ot_overall_sync_count ); 520 r += sprintf(r, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n",
544 r += sprintf( r, " </connections>\n" ); 521 ot_overall_tcp_connections, ot_overall_tcp_successfulannounces, ot_overall_tcp_successfulscrapes);
545 r += sprintf( r, " <debug>\n" ); 522 r += sprintf(r, " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n",
546 r += sprintf( r, " <renew>\n" ); 523 ot_overall_udp_connections, ot_overall_udp_connects, ot_overall_udp_successfulannounces, ot_overall_udp_successfulscrapes,
547 for( i=0; i<OT_PEER_TIMEOUT; ++i ) 524 ot_overall_udp_connectionidmissmatches);
548 r += sprintf( r, " <count interval=\"%02i\">%llu</count>\n", i, ot_renewed[i] ); 525 r += sprintf(r, " <livesync>\n <count>%llu</count>\n </livesync>\n", ot_overall_sync_count);
549 r += sprintf( r, " </renew>\n" ); 526 r += sprintf(r, " </connections>\n");
550 r += sprintf( r, " <http_error>\n" ); 527 r += sprintf(r, " <debug>\n");
551 for( i=0; i<CODE_HTTPERROR_COUNT; ++i ) 528 r += sprintf(r, " <renew>\n");
552 r += sprintf( r, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], ot_failed_request_counts[i] ); 529 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
553 r += sprintf( r, " </http_error>\n" ); 530 r += sprintf(r, " <count interval=\"%02i\">%llu</count>\n", i, ot_renewed[i]);
554 r += sprintf( r, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", ot_overall_stall_count ); 531 r += sprintf(r, " </renew>\n");
555 r += sprintf( r, " </debug>\n" ); 532 r += sprintf(r, " <http_error>\n");
556 r += sprintf( r, "</stats>" ); 533 for (i = 0; i < CODE_HTTPERROR_COUNT; ++i)
534 r += sprintf(r, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], ot_failed_request_counts[i]);
535 r += sprintf(r, " </http_error>\n");
536 r += sprintf(r, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", ot_overall_stall_count);
537 r += sprintf(r, " </debug>\n");
538 r += sprintf(r, "</stats>");
557 return r - reply; 539 return r - reply;
558} 540}
559 541
560extern const char 542size_t stats_return_tracker_version(char *reply) {
561*g_version_opentracker_c, *g_version_accesslist_c, *g_version_clean_c, *g_version_fullscrape_c, *g_version_http_c, 543#define QUOTE(name) #name
562*g_version_iovec_c, *g_version_mutex_c, *g_version_stats_c, *g_version_udp_c, *g_version_vector_c, 544#define SQUOTE(name) QUOTE(name)
563*g_version_scan_urlencoded_query_c, *g_version_trackerlogic_c, *g_version_livesync_c, *g_version_rijndael_c; 545 return sprintf(reply, "https://erdgeist.org/gitweb/opentracker/commit/?id=" SQUOTE(GIT_VERSION) "\n");
564 546}
565size_t stats_return_tracker_version( char *reply ) { 547
566 return sprintf( reply, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 548size_t return_stats_for_tracker(char *reply, int mode, int format) {
567 g_version_opentracker_c, g_version_accesslist_c, g_version_clean_c, g_version_fullscrape_c, g_version_http_c, 549 (void)format;
568 g_version_iovec_c, g_version_mutex_c, g_version_stats_c, g_version_udp_c, g_version_vector_c, 550 switch (mode & TASK_TASK_MASK) {
569 g_version_scan_urlencoded_query_c, g_version_trackerlogic_c, g_version_livesync_c, g_version_rijndael_c ); 551 case TASK_STATS_CONNS:
570} 552 return stats_connections_mrtg(reply);
571 553 case TASK_STATS_SCRAPE:
572size_t return_stats_for_tracker( char *reply, int mode, int format ) { 554 return stats_scrape_mrtg(reply);
573 (void) format; 555 case TASK_STATS_UDP:
574 switch( mode & TASK_TASK_MASK ) { 556 return stats_udpconnections_mrtg(reply);
575 case TASK_STATS_CONNS: 557 case TASK_STATS_TCP:
576 return stats_connections_mrtg( reply ); 558 return stats_tcpconnections_mrtg(reply);
577 case TASK_STATS_SCRAPE: 559 case TASK_STATS_FULLSCRAPE:
578 return stats_scrape_mrtg( reply ); 560 return stats_fullscrapes_mrtg(reply);
579 case TASK_STATS_UDP: 561 case TASK_STATS_COMPLETED:
580 return stats_udpconnections_mrtg( reply ); 562 return stats_return_completed_mrtg(reply);
581 case TASK_STATS_TCP: 563 case TASK_STATS_HTTPERRORS:
582 return stats_tcpconnections_mrtg( reply ); 564 return stats_httperrors_txt(reply);
583 case TASK_STATS_FULLSCRAPE: 565 case TASK_STATS_VERSION:
584 return stats_fullscrapes_mrtg( reply ); 566 return stats_return_tracker_version(reply);
585 case TASK_STATS_COMPLETED: 567 case TASK_STATS_RENEW:
586 return stats_return_completed_mrtg( reply ); 568 return stats_return_renew_bucket(reply);
587 case TASK_STATS_HTTPERRORS: 569 case TASK_STATS_SYNCS:
588 return stats_httperrors_txt( reply ); 570 return stats_return_sync_mrtg(reply);
589 case TASK_STATS_VERSION:
590 return stats_return_tracker_version( reply );
591 case TASK_STATS_RENEW:
592 return stats_return_renew_bucket( reply );
593 case TASK_STATS_SYNCS:
594 return stats_return_sync_mrtg( reply );
595#ifdef WANT_LOG_NUMWANT 571#ifdef WANT_LOG_NUMWANT
596 case TASK_STATS_NUMWANTS: 572 case TASK_STATS_NUMWANTS:
597 return stats_return_numwants( reply ); 573 return stats_return_numwants(reply);
598#endif 574#endif
599 default: 575 default:
600 return 0; 576 return 0;
601 } 577 }
602} 578}
603 579
604static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) { 580static void stats_make(int *iovec_entries, struct iovec **iovector, ot_tasktype mode) {
605 char *r; 581 char *r;
606 582
607 *iovec_entries = 0; 583 *iovec_entries = 0;
608 *iovector = NULL; 584 *iovector = NULL;
609 if( !( r = iovec_increase( iovec_entries, iovector, OT_STATS_TMPSIZE ) ) ) 585 if (!(r = iovec_increase(iovec_entries, iovector, OT_STATS_TMPSIZE)))
610 return; 586 return;
611 587
612 switch( mode & TASK_TASK_MASK ) { 588 switch (mode & TASK_TASK_MASK) {
613 case TASK_STATS_TORRENTS: r += stats_torrents_mrtg( r ); break; 589 case TASK_STATS_TORRENTS:
614 case TASK_STATS_PEERS: r += stats_peers_mrtg( r ); break; 590 r += stats_torrents_mrtg(r);
615 case TASK_STATS_SLASH24S: r += stats_slash24s_txt( r, 128 ); break; 591 break;
616 case TASK_STATS_TOP10: r += stats_top_txt( r, 10 ); break; 592 case TASK_STATS_PEERS:
617 case TASK_STATS_TOP100: 593 r += stats_peers_mrtg(r);
618 r = iovec_fix_increase_or_free( iovec_entries, iovector, r, 4 * OT_STATS_TMPSIZE ); 594 break;
619 if( !r ) return; 595 case TASK_STATS_SLASH24S:
620 r += stats_top_txt( r, 100 ); break; 596 r += stats_slash24s_txt(r, 128);
621 case TASK_STATS_EVERYTHING: r += stats_return_everything( r ); break; 597 break;
598 case TASK_STATS_TOP10:
599 r += stats_top_txt(r, 10);
600 break;
601 case TASK_STATS_TOP100:
602 r = iovec_fix_increase_or_free(iovec_entries, iovector, r, 4 * OT_STATS_TMPSIZE);
603 if (!r)
604 return;
605 r += stats_top_txt(r, 100);
606 break;
607 case TASK_STATS_EVERYTHING:
608 r = iovec_fix_increase_or_free(iovec_entries, iovector, r, OT_STATS_TMPSIZE + 64 * OT_PEER_TIMEOUT);
609 if (!r)
610 return;
611 r += stats_return_everything(r);
612 break;
622#ifdef WANT_SPOT_WOODPECKER 613#ifdef WANT_SPOT_WOODPECKER
623 case TASK_STATS_WOODPECKERS: r += stats_return_woodpeckers( r, 128 ); break; 614 case TASK_STATS_WOODPECKERS:
615 r += stats_return_woodpeckers(r, 128);
616 break;
624#endif 617#endif
625#ifdef WANT_FULLLOG_NETWORKS 618#ifdef WANT_FULLLOG_NETWORKS
626 case TASK_STATS_FULLLOG: stats_return_fulllog( iovec_entries, iovector, r ); 619 case TASK_STATS_FULLLOG:
627 return; 620 stats_return_fulllog(iovec_entries, iovector, r);
621 return;
628#endif 622#endif
629 default: 623 default:
630 iovec_free(iovec_entries, iovector); 624 iovec_free(iovec_entries, iovector);
631 return; 625 return;
632 } 626 }
633 iovec_fixlast( iovec_entries, iovector, r ); 627 iovec_fixlast(iovec_entries, iovector, r);
634} 628}
635 629
636void stats_issue_event( ot_status_event event, PROTO_FLAG proto, uintptr_t event_data ) { 630void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_data) {
637 switch( event ) { 631 switch (event) {
638 case EVENT_ACCEPT: 632 case EVENT_ACCEPT:
639 if( proto == FLAG_TCP ) ot_overall_tcp_connections++; else ot_overall_udp_connections++; 633 if (proto == FLAG_TCP)
634 ot_overall_tcp_connections++;
635 else
636 ot_overall_udp_connections++;
640#ifdef WANT_LOG_NETWORKS 637#ifdef WANT_LOG_NETWORKS
641 stat_increase_network_count( &stats_network_counters_root, 0, event_data ); 638 stat_increase_network_count(&stats_network_counters_root, 0, event_data);
642#endif 639#endif
643 break; 640 break;
644 case EVENT_ANNOUNCE: 641 case EVENT_ANNOUNCE:
645 if( proto == FLAG_TCP ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++; 642 if (proto == FLAG_TCP)
646 break; 643 ot_overall_tcp_successfulannounces++;
647 case EVENT_CONNECT: 644 else
648 if( proto == FLAG_TCP ) ot_overall_tcp_connects++; else ot_overall_udp_connects++; 645 ot_overall_udp_successfulannounces++;
649 break; 646 break;
650 case EVENT_COMPLETED: 647 case EVENT_CONNECT:
648 if (proto == FLAG_TCP)
649 ot_overall_tcp_connects++;
650 else
651 ot_overall_udp_connects++;
652 break;
653 case EVENT_COMPLETED:
651#ifdef WANT_SYSLOGS 654#ifdef WANT_SYSLOGS
652 if( event_data) { 655 if (event_data) {
653 struct ot_workstruct *ws = (struct ot_workstruct *)event_data; 656 struct ot_workstruct *ws = (struct ot_workstruct *)event_data;
654 char timestring[64]; 657 char timestring[64];
655 char hash_hex[42], peerid_hex[42], ip_readable[64]; 658 char hash_hex[42], peerid_hex[42], ip_readable[64];
656 struct tm time_now; 659 struct tm time_now;
657 time_t ttt; 660 time_t ttt;
658 661
659 time( &ttt ); 662 time(&ttt);
660 localtime_r( &ttt, &time_now ); 663 localtime_r(&ttt, &time_now);
661 strftime( timestring, sizeof( timestring ), "%FT%T%z", &time_now ); 664 strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now);
662 665
663 to_hex( hash_hex, *ws->hash ); 666 to_hex(hash_hex, *ws->hash);
664 if( ws->peer_id ) 667 if (ws->peer_id)
665 to_hex( peerid_hex, (uint8_t*)ws->peer_id ); 668 to_hex(peerid_hex, (uint8_t *)ws->peer_id);
666 else { 669 else {
667 *peerid_hex=0; 670 *peerid_hex = 0;
668 } 671 }
669 672
670#ifdef WANT_V6 673 ip_readable[fmt_ip6c(ip_readable, (char *)&ws->peer)] = 0;
671 ip_readable[ fmt_ip6c( ip_readable, (char*)&ws->peer ) ] = 0; 674#if 0
672#else 675 /* XXX */
673 ip_readable[ fmt_ip4( ip_readable, (char*)&ws->peer ) ] = 0; 676 ip_readable[ fmt_ip4( ip_readable, (char*)&ws->peer ) ] = 0;
674#endif 677#endif
675 syslog( LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable ); 678 syslog(LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable);
676 }
677#endif
678 ot_overall_completed++;
679 break;
680 case EVENT_SCRAPE:
681 if( proto == FLAG_TCP ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++;
682 break;
683 case EVENT_FULLSCRAPE:
684 ot_full_scrape_count++;
685 ot_full_scrape_size += event_data;
686 break;
687 case EVENT_FULLSCRAPE_REQUEST:
688 {
689 ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */
690 char _debug[512];
691 int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 );
692 off += fmt_ip6c( _debug+off, *ip );
693 off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" );
694 write( 2, _debug, off );
695 ot_full_scrape_request_count++;
696 } 679 }
697 break; 680#endif
698 case EVENT_FULLSCRAPE_REQUEST_GZIP: 681 ot_overall_completed++;
699 { 682 break;
700 ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */ 683 case EVENT_SCRAPE:
701 char _debug[512]; 684 if (proto == FLAG_TCP)
702 int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 ); 685 ot_overall_tcp_successfulscrapes++;
703 off += fmt_ip6c(_debug+off, *ip ); 686 else
704 off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" ); 687 ot_overall_udp_successfulscrapes++;
705 write( 2, _debug, off ); 688 break;
706 ot_full_scrape_request_count++; 689 case EVENT_FULLSCRAPE:
707 } 690 ot_full_scrape_count++;
708 break; 691 ot_full_scrape_size += event_data;
709 case EVENT_FAILED: 692 break;
710 ot_failed_request_counts[event_data]++; 693 case EVENT_FULLSCRAPE_REQUEST: {
711 break; 694 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
712 case EVENT_RENEW: 695 char _debug[512];
713 ot_renewed[event_data]++; 696 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60);
714 break; 697 off += fmt_ip6c(_debug + off, *ip);
715 case EVENT_SYNC: 698 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
716 ot_overall_sync_count+=event_data; 699 (void)write(2, _debug, off);
717 break; 700 ot_full_scrape_request_count++;
718 case EVENT_BUCKET_LOCKED: 701 } break;
719 ot_overall_stall_count++; 702 case EVENT_FULLSCRAPE_REQUEST_GZIP: {
720 break; 703 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
704 char _debug[512];
705 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60);
706 off += fmt_ip6c(_debug + off, *ip);
707 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
708 (void)write(2, _debug, off);
709 ot_full_scrape_request_count++;
710 } break;
711 case EVENT_FAILED:
712 ot_failed_request_counts[event_data]++;
713 break;
714 case EVENT_RENEW:
715 ot_renewed[event_data]++;
716 break;
717 case EVENT_SYNC:
718 ot_overall_sync_count += event_data;
719 break;
720 case EVENT_BUCKET_LOCKED:
721 ot_overall_stall_count++;
722 break;
721#ifdef WANT_SPOT_WOODPECKER 723#ifdef WANT_SPOT_WOODPECKER
722 case EVENT_WOODPECKER: 724 case EVENT_WOODPECKER:
723 pthread_mutex_lock( &g_woodpeckers_mutex ); 725 pthread_mutex_lock(&g_woodpeckers_mutex);
724 stat_increase_network_count( &stats_woodpeckers_tree, 0, event_data ); 726 stat_increase_network_count(&stats_woodpeckers_tree, 0, event_data);
725 pthread_mutex_unlock( &g_woodpeckers_mutex ); 727 pthread_mutex_unlock(&g_woodpeckers_mutex);
726 break; 728 break;
727#endif 729#endif
728 case EVENT_CONNID_MISSMATCH: 730 case EVENT_CONNID_MISSMATCH:
729 ++ot_overall_udp_connectionidmissmatches; 731 ++ot_overall_udp_connectionidmissmatches;
730 default: 732 default:
731 break; 733 break;
732 } 734 }
733} 735}
734 736
735void stats_cleanup() { 737void stats_cleanup() {
736#ifdef WANT_SPOT_WOODPECKER 738#ifdef WANT_SPOT_WOODPECKER
737 pthread_mutex_lock( &g_woodpeckers_mutex ); 739 pthread_mutex_lock(&g_woodpeckers_mutex);
738 stats_shift_down_network_count( &stats_woodpeckers_tree, 0, 1 ); 740 stats_shift_down_network_count(&stats_woodpeckers_tree, 0, 1);
739 pthread_mutex_unlock( &g_woodpeckers_mutex ); 741 pthread_mutex_unlock(&g_woodpeckers_mutex);
740#endif 742#endif
741} 743}
742 744
743static void * stats_worker( void * args ) { 745static void *stats_worker(void *args) {
744 int iovec_entries; 746 int iovec_entries;
745 struct iovec *iovector; 747 struct iovec *iovector;
746 748
747 (void) args; 749 (void)args;
748 750
749 while( 1 ) { 751 while (1) {
750 ot_tasktype tasktype = TASK_STATS; 752 ot_tasktype tasktype = TASK_STATS;
751 ot_taskid taskid = mutex_workqueue_poptask( &tasktype ); 753 ot_taskid taskid = mutex_workqueue_poptask(&tasktype);
752 stats_make( &iovec_entries, &iovector, tasktype ); 754 stats_make(&iovec_entries, &iovector, tasktype);
753 if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) ) 755 if (mutex_workqueue_pushresult(taskid, iovec_entries, iovector))
754 iovec_free( &iovec_entries, &iovector ); 756 iovec_free(&iovec_entries, &iovector);
755 } 757 }
756 return NULL; 758 return NULL;
757} 759}
758 760
759void stats_deliver( int64 sock, int tasktype ) { 761void stats_deliver(int64 sock, int tasktype) { mutex_workqueue_pushtask(sock, tasktype); }
760 mutex_workqueue_pushtask( sock, tasktype );
761}
762 762
763static pthread_t thread_id; 763static pthread_t thread_id;
764void stats_init( ) { 764void stats_init() {
765 ot_start_time = g_now_seconds; 765 ot_start_time = g_now_seconds;
766 pthread_create( &thread_id, NULL, stats_worker, NULL ); 766 pthread_create(&thread_id, NULL, stats_worker, NULL);
767} 767}
768 768
769void stats_deinit( ) { 769void stats_deinit() {
770 pthread_cancel( thread_id ); 770 pthread_cancel(thread_id);
771} 771}
772
773const char *g_version_stats_c = "$Source$: $Revision$\n";