summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2024-04-15 00:39:02 +0200
committerDirk Engling <erdgeist@erdgeist.org>2024-04-15 00:39:02 +0200
commit7c633c259ebc4a863c5076462c5792ecb8b9f617 (patch)
tree550a272a82325c554923c7498811cb8971aa49c2 /ot_stats.c
parent4c5935c0574481dc4b0e0bf57528dc3069e34742 (diff)
clang-format
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c971
1 files changed, 483 insertions, 488 deletions
diff --git a/ot_stats.c b/ot_stats.c
index 66bed49..fa456c3 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#if 0 78#if 0
77// XXX 79// XXX
78#define STATS_NETWORK_NODE_MAXDEPTH (68-STATS_NETWORK_NODE_BITWIDTH) 80#define STATS_NETWORK_NODE_MAXDEPTH (68 - STATS_NETWORK_NODE_BITWIDTH)
79#define STATS_NETWORK_NODE_LIMIT (48-STATS_NETWORK_NODE_BITWIDTH) 81#define STATS_NETWORK_NODE_LIMIT (48 - STATS_NETWORK_NODE_BITWIDTH)
80#endif 82#endif
81#define STATS_NETWORK_NODE_MAXDEPTH (28-STATS_NETWORK_NODE_BITWIDTH) 83#define STATS_NETWORK_NODE_MAXDEPTH (28 - STATS_NETWORK_NODE_BITWIDTH)
82#define STATS_NETWORK_NODE_LIMIT (24-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,119 +93,123 @@ 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#if 0 213#if 0
208 // XXX 214 // XXX
209 r += fmt_ip4( r, networks[i]); 215 r += fmt_ip4( r, networks[i]);
@@ -216,66 +222,66 @@ static size_t stats_return_busy_networks( char * reply, stats_network_node *tree
216 return r - reply; 222 return r - reply;
217} 223}
218 224
219static size_t stats_slash24s_txt( char *reply, size_t amount ) { 225static size_t stats_slash24s_txt(char *reply, size_t amount) {
220 stats_network_node *slash24s_network_counters_root = NULL; 226 stats_network_node *slash24s_network_counters_root = NULL;
221 char *r=reply; 227 char *r = reply;
222 int bucket; 228 int bucket;
223 size_t i, peer_size = OT_PEER_SIZE4; 229 size_t i, peer_size = OT_PEER_SIZE4;
224 230
225 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { 231 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
226 ot_vector *torrents_list = mutex_bucket_lock( bucket ); 232 ot_vector *torrents_list = mutex_bucket_lock(bucket);
227 for( i=0; i<torrents_list->size; ++i ) { 233 for (i = 0; i < torrents_list->size; ++i) {
228 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[i] ).peer_list4; 234 ot_peerlist *peer_list = (((ot_torrent *)(torrents_list->data))[i]).peer_list4;
229 ot_vector *bucket_list = &peer_list->peers; 235 ot_vector *bucket_list = &peer_list->peers;
230 int num_buckets = 1; 236 int num_buckets = 1;
231 237
232 if( OT_PEERLIST_HASBUCKETS( peer_list ) ) { 238 if (OT_PEERLIST_HASBUCKETS(peer_list)) {
233 num_buckets = bucket_list->size; 239 num_buckets = bucket_list->size;
234 bucket_list = (ot_vector *)bucket_list->data; 240 bucket_list = (ot_vector *)bucket_list->data;
235 } 241 }
236 242
237 while( num_buckets-- ) { 243 while (num_buckets--) {
238 ot_peer *peers = (ot_peer*)bucket_list->data; 244 ot_peer *peers = (ot_peer *)bucket_list->data;
239 size_t numpeers = bucket_list->size; 245 size_t numpeers = bucket_list->size;
240 while( numpeers-- ) { 246 while (numpeers--) {
241 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)))
242 goto bailout_unlock; 248 goto bailout_unlock;
243 peers += peer_size; 249 peers += peer_size;
244 } 250 }
245 ++bucket_list; 251 ++bucket_list;
246 } 252 }
247 } 253 }
248 mutex_bucket_unlock( bucket, 0 ); 254 mutex_bucket_unlock(bucket, 0);
249 if( !g_opentracker_running ) 255 if (!g_opentracker_running)
250 goto bailout_error; 256 goto bailout_error;
251 } 257 }
252 258
253 /* The tree is built. Now analyze */ 259 /* The tree is built. Now analyze */
254 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);
255 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);
256 goto success; 262 goto success;
257 263
258bailout_unlock: 264bailout_unlock:
259 mutex_bucket_unlock( bucket, 0 ); 265 mutex_bucket_unlock(bucket, 0);
260bailout_error: 266bailout_error:
261 r = reply; 267 r = reply;
262success: 268success:
263 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);
264 270
265 return r-reply; 271 return r - reply;
266} 272}
267 273
268#ifdef WANT_SPOT_WOODPECKER 274#ifdef WANT_SPOT_WOODPECKER
269static stats_network_node *stats_woodpeckers_tree; 275static stats_network_node *stats_woodpeckers_tree;
270static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER; 276static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER;
271 277
272static size_t stats_return_woodpeckers( char * reply, int amount ) { 278static size_t stats_return_woodpeckers(char *reply, int amount) {
273 char * r = reply; 279 char *r = reply;
274 280
275 pthread_mutex_lock( &g_woodpeckers_mutex ); 281 pthread_mutex_lock(&g_woodpeckers_mutex);
276 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);
277 pthread_mutex_unlock( &g_woodpeckers_mutex ); 283 pthread_mutex_unlock(&g_woodpeckers_mutex);
278 return r-reply; 284 return r - reply;
279} 285}
280#endif 286#endif
281 287
@@ -285,8 +291,8 @@ typedef struct {
285 unsigned long long seed_count; 291 unsigned long long seed_count;
286} torrent_stats; 292} torrent_stats;
287 293
288static int torrent_statter( ot_torrent *torrent, uintptr_t data ) { 294static int torrent_statter(ot_torrent *torrent, uintptr_t data) {
289 torrent_stats *stats = (torrent_stats*)data; 295 torrent_stats *stats = (torrent_stats *)data;
290 stats->torrent_count++; 296 stats->torrent_count++;
291 stats->peer_count += torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; 297 stats->peer_count += torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
292 stats->seed_count += torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; 298 stats->seed_count += torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
@@ -294,491 +300,480 @@ static int torrent_statter( ot_torrent *torrent, uintptr_t data ) {
294} 300}
295 301
296/* Converter function from memory to human readable hex strings */ 302/* Converter function from memory to human readable hex strings */
297static 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}
298 314
299typedef struct { size_t val; ot_hash hash; } ot_record; 315typedef struct {
316 size_t val;
317 ot_hash hash;
318} ot_record;
300 319
301/* Fetches stats from tracker */ 320/* Fetches stats from tracker */
302size_t stats_top_txt( char * reply, int amount ) { 321size_t stats_top_txt(char *reply, int amount) {
303 size_t j; 322 size_t j;
304 ot_record top100s[100], top100c[100]; 323 ot_record top100s[100], top100c[100];
305 char *r = reply, hex_out[42]; 324 char *r = reply, hex_out[42];
306 int idx, bucket; 325 int idx, bucket;
307 326
308 if( amount > 100 ) 327 if (amount > 100)
309 amount = 100; 328 amount = 100;
310 329
311 byte_zero( top100s, sizeof( top100s ) ); 330 byte_zero(top100s, sizeof(top100s));
312 byte_zero( top100c, sizeof( top100c ) ); 331 byte_zero(top100c, sizeof(top100c));
313 332
314 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { 333 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
315 ot_vector *torrents_list = mutex_bucket_lock( bucket ); 334 ot_vector *torrents_list = mutex_bucket_lock(bucket);
316 for( j=0; j<torrents_list->size; ++j ) { 335 for (j = 0; j < torrents_list->size; ++j) {
317 ot_torrent *torrent = (ot_torrent*)(torrents_list->data) + j; 336 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j;
318 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; 337 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
319 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; 338 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
320 idx = amount - 1; 339 idx = amount - 1;
321 while( (idx >= 0) && ( peer_count > top100c[idx].val ) ) 340 while ((idx >= 0) && (peer_count > top100c[idx].val))
322 --idx; 341 --idx;
323 if ( idx++ != amount - 1 ) { 342 if (idx++ != amount - 1) {
324 memmove( top100c + idx + 1, top100c + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); 343 memmove(top100c + idx + 1, top100c + idx, (amount - 1 - idx) * sizeof(ot_record));
325 memcpy( &top100c[idx].hash, &torrent->hash, sizeof(ot_hash)); 344 memcpy(&top100c[idx].hash, &torrent->hash, sizeof(ot_hash));
326 top100c[idx].val = peer_count; 345 top100c[idx].val = peer_count;
327 } 346 }
328 idx = amount - 1; 347 idx = amount - 1;
329 while( (idx >= 0) && ( seed_count > top100s[idx].val ) ) 348 while ((idx >= 0) && (seed_count > top100s[idx].val))
330 --idx; 349 --idx;
331 if ( idx++ != amount - 1 ) { 350 if (idx++ != amount - 1) {
332 memmove( top100s + idx + 1, top100s + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); 351 memmove(top100s + idx + 1, top100s + idx, (amount - 1 - idx) * sizeof(ot_record));
333 memcpy( &top100s[idx].hash, &torrent->hash, sizeof(ot_hash)); 352 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash));
334 top100s[idx].val = seed_count; 353 top100s[idx].val = seed_count;
335 } 354 }
336 } 355 }
337 mutex_bucket_unlock( bucket, 0 ); 356 mutex_bucket_unlock(bucket, 0);
338 if( !g_opentracker_running ) 357 if (!g_opentracker_running)
339 return 0; 358 return 0;
340 } 359 }
341 360
342 r += sprintf( r, "Top %d torrents by peers:\n", amount ); 361 r += sprintf(r, "Top %d torrents by peers:\n", amount);
343 for( idx=0; idx<amount; ++idx ) 362 for (idx = 0; idx < amount; ++idx)
344 if( top100c[idx].val ) 363 if (top100c[idx].val)
345 r += sprintf( r, "\t%zd\t%s\n", top100c[idx].val, to_hex( hex_out, top100c[idx].hash) ); 364 r += sprintf(r, "\t%zd\t%s\n", top100c[idx].val, to_hex(hex_out, top100c[idx].hash));
346 r += sprintf( r, "Top %d torrents by seeds:\n", amount ); 365 r += sprintf(r, "Top %d torrents by seeds:\n", amount);
347 for( idx=0; idx<amount; ++idx ) 366 for (idx = 0; idx < amount; ++idx)
348 if( top100s[idx].val ) 367 if (top100s[idx].val)
349 r += sprintf( r, "\t%zd\t%s\n", top100s[idx].val, to_hex( hex_out, top100s[idx].hash) ); 368 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash));
350 369
351 return r - reply; 370 return r - reply;
352} 371}
353 372
354static 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); }
355 return events / ( (unsigned int)t ? (unsigned int)t : 1 );
356}
357 374
358static size_t stats_connections_mrtg( char * reply ) { 375static size_t stats_connections_mrtg(char *reply) {
359 ot_time t = time( NULL ) - ot_start_time; 376 ot_time t = time(NULL) - ot_start_time;
360 return sprintf( reply, 377 return sprintf(reply, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
361 "%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,
362 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),
363 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),
364 (int)t, 381 events_per_time(ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, t));
365 (int)(t / 3600),
366 events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
367 events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
368 );
369} 382}
370 383
371static size_t stats_udpconnections_mrtg( char * reply ) { 384static size_t stats_udpconnections_mrtg(char *reply) {
372 ot_time t = time( NULL ) - ot_start_time; 385 ot_time t = time(NULL) - ot_start_time;
373 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,
374 "%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),
375 ot_overall_udp_connections, 388 events_per_time(ot_overall_udp_successfulannounces + ot_overall_udp_connects, t));
376 ot_overall_udp_successfulannounces+ot_overall_udp_connects,
377 (int)t,
378 (int)(t / 3600),
379 events_per_time( ot_overall_udp_connections, t ),
380 events_per_time( ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
381 );
382} 389}
383 390
384static size_t stats_tcpconnections_mrtg( char * reply ) { 391static size_t stats_tcpconnections_mrtg(char *reply) {
385 time_t t = time( NULL ) - ot_start_time; 392 time_t t = time(NULL) - ot_start_time;
386 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,
387 "%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),
388 ot_overall_tcp_connections, 395 events_per_time(ot_overall_tcp_successfulannounces, t));
389 ot_overall_tcp_successfulannounces,
390 (int)t,
391 (int)(t / 3600),
392 events_per_time( ot_overall_tcp_connections, t ),
393 events_per_time( ot_overall_tcp_successfulannounces, t )
394 );
395} 396}
396 397
397static size_t stats_scrape_mrtg( char * reply ) { 398static size_t stats_scrape_mrtg(char *reply) {
398 time_t t = time( NULL ) - ot_start_time; 399 time_t t = time(NULL) - ot_start_time;
399 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,
400 "%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),
401 ot_overall_tcp_successfulscrapes, 402 events_per_time((ot_overall_tcp_successfulscrapes + ot_overall_udp_successfulscrapes), t));
402 ot_overall_udp_successfulscrapes,
403 (int)t,
404 (int)(t / 3600),
405 events_per_time( (ot_overall_tcp_successfulscrapes+ot_overall_udp_successfulscrapes), t )
406 );
407} 403}
408 404
409static size_t stats_fullscrapes_mrtg( char * reply ) { 405static size_t stats_fullscrapes_mrtg(char *reply) {
410 ot_time t = time( NULL ) - ot_start_time; 406 ot_time t = time(NULL) - ot_start_time;
411 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,
412 "%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));
413 ot_full_scrape_count * 1000,
414 ot_full_scrape_size,
415 (int)t,
416 (int)(t / 3600),
417 events_per_time( ot_full_scrape_count, t ),
418 events_per_time( ot_full_scrape_size, t )
419 );
420} 409}
421 410
422static size_t stats_peers_mrtg( char * reply ) { 411static size_t stats_peers_mrtg(char *reply) {
423 torrent_stats stats = {0,0,0}; 412 torrent_stats stats = {0, 0, 0};
424 413
425 iterate_all_torrents( torrent_statter, (uintptr_t)&stats ); 414 iterate_all_torrents(torrent_statter, (uintptr_t)&stats);
426 415
427 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);
428 stats.peer_count,
429 stats.seed_count,
430 stats.torrent_count
431 );
432} 417}
433 418
434static size_t stats_torrents_mrtg( char * reply ) 419static size_t stats_torrents_mrtg(char *reply) {
435{
436 size_t torrent_count = mutex_get_torrent_count(); 420 size_t torrent_count = mutex_get_torrent_count();
437 421
438 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);
439 torrent_count,
440 (size_t)0,
441 torrent_count
442 );
443} 423}
444 424
445static size_t stats_httperrors_txt ( char * reply ) { 425static size_t stats_httperrors_txt(char *reply) {
446 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],
447 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],
448 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]);
449 ot_failed_request_counts[6] );
450} 429}
451 430
452static size_t stats_return_renew_bucket( char * reply ) { 431static size_t stats_return_renew_bucket(char *reply) {
453 char *r = reply; 432 char *r = reply;
454 int i; 433 int i;
455 434
456 for( i=0; i<OT_PEER_TIMEOUT; ++i ) 435 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
457 r+=sprintf(r,"%02i %llu\n", i, ot_renewed[i] ); 436 r += sprintf(r, "%02i %llu\n", i, ot_renewed[i]);
458 return r - reply; 437 return r - reply;
459} 438}
460 439
461static size_t stats_return_sync_mrtg( char * reply ) { 440static size_t stats_return_sync_mrtg(char *reply) {
462 ot_time t = time( NULL ) - ot_start_time; 441 ot_time t = time(NULL) - ot_start_time;
463 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,
464 "%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),
465 ot_overall_sync_count, 444 events_per_time(ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces + ot_overall_udp_connects, t));
466 0LL,
467 (int)t,
468 (int)(t / 3600),
469 events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
470 events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
471 );
472} 445}
473 446
474static size_t stats_return_completed_mrtg( char * reply ) { 447static size_t stats_return_completed_mrtg(char *reply) {
475 ot_time t = time( NULL ) - ot_start_time; 448 ot_time t = time(NULL) - ot_start_time;
476 449
477 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),
478 "%llu\n%llu\n%i seconds (%i hours)\nopentracker, %lu completed/h.", 451 events_per_time(ot_overall_completed, t / 3600));
479 ot_overall_completed,
480 0LL,
481 (int)t,
482 (int)(t / 3600),
483 events_per_time( ot_overall_completed, t / 3600 )
484 );
485} 452}
486 453
487#ifdef WANT_LOG_NUMWANT 454#ifdef WANT_LOG_NUMWANT
488extern unsigned long long numwants[201]; 455extern unsigned long long numwants[201];
489static size_t stats_return_numwants( char * reply ) { 456static size_t stats_return_numwants(char *reply) {
490 char * r = reply; 457 char *r = reply;
491 int i; 458 int i;
492 for( i=0; i<=200; ++i ) 459 for (i = 0; i <= 200; ++i)
493 r += sprintf( r, "%03d => %lld\n", i, numwants[i] ); 460 r += sprintf(r, "%03d => %lld\n", i, numwants[i]);
494 return r-reply; 461 return r - reply;
495} 462}
496#endif 463#endif
497 464
498#ifdef WANT_FULLLOG_NETWORKS 465#ifdef WANT_FULLLOG_NETWORKS
499static 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) {
500 ot_log *loglist = g_logchain_first, *llnext; 467 ot_log *loglist = g_logchain_first, *llnext;
501 char * re = r + OT_STATS_TMPSIZE; 468 char *re = r + OT_STATS_TMPSIZE;
502 469
503 g_logchain_first = g_logchain_last = 0; 470 g_logchain_first = g_logchain_last = 0;
504 471
505 while( loglist ) { 472 while (loglist) {
506 if( r + ( loglist->size + 64 ) >= re ) { 473 if (r + (loglist->size + 64) >= re) {
507 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);
508 if( !r ) return; 475 if (!r)
476 return;
509 re = r + 32 * OT_STATS_TMPSIZE; 477 re = r + 32 * OT_STATS_TMPSIZE;
510 } 478 }
511 r += sprintf( r, "%08ld: ", loglist->time ); 479 r += sprintf(r, "%08ld: ", loglist->time);
512 r += fmt_ip6c( r, loglist->ip ); 480 r += fmt_ip6c(r, loglist->ip);
513 *r++ = '\n'; 481 *r++ = '\n';
514 memcpy( r, loglist->data, loglist->size ); 482 memcpy(r, loglist->data, loglist->size);
515 r += loglist->size; 483 r += loglist->size;
516 *r++ = '\n'; 484 *r++ = '\n';
517 *r++ = '*'; 485 *r++ = '*';
518 *r++ = '\n'; 486 *r++ = '\n';
519 *r++ = '\n'; 487 *r++ = '\n';
520 488
521 llnext = loglist->next; 489 llnext = loglist->next;
522 free( loglist->data ); 490 free(loglist->data);
523 free( loglist ); 491 free(loglist);
524 loglist = llnext; 492 loglist = llnext;
525 } 493 }
526 iovec_fixlast( iovec_entries, iovector, r ); 494 iovec_fixlast(iovec_entries, iovector, r);
527} 495}
528#endif 496#endif
529 497
530static size_t stats_return_everything( char * reply ) { 498static size_t stats_return_everything(char *reply) {
531 torrent_stats stats = {0,0,0}; 499 torrent_stats stats = {0, 0, 0};
532 int i; 500 int i;
533 char * r = reply; 501 char *r = reply;
534 502
535 iterate_all_torrents( torrent_statter, (uintptr_t)&stats ); 503 iterate_all_torrents(torrent_statter, (uintptr_t)&stats);
536 504
537 r += sprintf( r, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); 505 r += sprintf(r, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
538 r += sprintf( r, "<stats>\n" ); 506 r += sprintf(r, "<stats>\n");
539 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);
540 r += sprintf( r, " <version>\n" ); r += stats_return_tracker_version( r ); r += sprintf( r, " </version>\n" ); 508 r += sprintf(r, " <version>\n");
541 r += sprintf( r, " <uptime>%llu</uptime>\n", (unsigned long long)(time( NULL ) - ot_start_time) ); 509 r += stats_return_tracker_version(r);
542 r += sprintf( r, " <torrents>\n" ); 510 r += sprintf(r, " </version>\n");
543 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));
544 r += sprintf( r, " <count_iterator>%llu</count_iterator>\n", stats.torrent_count ); 512 r += sprintf(r, " <torrents>\n");
545 r += sprintf( r, " </torrents>\n" ); 513 r += sprintf(r, " <count_mutex>%zd</count_mutex>\n", mutex_get_torrent_count());
546 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);
547 r += sprintf( r, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count ); 515 r += sprintf(r, " </torrents>\n");
548 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);
549 r += sprintf( r, " <connections>\n" ); 517 r += sprintf(r, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count);
550 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);
551 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");
552 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",
553 r += sprintf( r, " </connections>\n" ); 521 ot_overall_tcp_connections, ot_overall_tcp_successfulannounces, ot_overall_tcp_successfulscrapes);
554 r += sprintf( r, " <debug>\n" ); 522 r += sprintf(
555 r += sprintf( r, " <renew>\n" ); 523 r,
556 for( i=0; i<OT_PEER_TIMEOUT; ++i ) 524 " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n",
557 r += sprintf( r, " <count interval=\"%02i\">%llu</count>\n", i, ot_renewed[i] ); 525 ot_overall_udp_connections, ot_overall_udp_connects, ot_overall_udp_successfulannounces, ot_overall_udp_successfulscrapes,
558 r += sprintf( r, " </renew>\n" ); 526 ot_overall_udp_connectionidmissmatches);
559 r += sprintf( r, " <http_error>\n" ); 527 r += sprintf(r, " <livesync>\n <count>%llu</count>\n </livesync>\n", ot_overall_sync_count);
560 for( i=0; i<CODE_HTTPERROR_COUNT; ++i ) 528 r += sprintf(r, " </connections>\n");
561 r += sprintf( r, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], ot_failed_request_counts[i] ); 529 r += sprintf(r, " <debug>\n");
562 r += sprintf( r, " </http_error>\n" ); 530 r += sprintf(r, " <renew>\n");
563 r += sprintf( r, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", ot_overall_stall_count ); 531 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
564 r += sprintf( r, " </debug>\n" ); 532 r += sprintf(r, " <count interval=\"%02i\">%llu</count>\n", i, ot_renewed[i]);
565 r += sprintf( r, "</stats>" ); 533 r += sprintf(r, " </renew>\n");
534 r += sprintf(r, " <http_error>\n");
535 for (i = 0; i < CODE_HTTPERROR_COUNT; ++i)
536 r += sprintf(r, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], ot_failed_request_counts[i]);
537 r += sprintf(r, " </http_error>\n");
538 r += sprintf(r, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", ot_overall_stall_count);
539 r += sprintf(r, " </debug>\n");
540 r += sprintf(r, "</stats>");
566 return r - reply; 541 return r - reply;
567} 542}
568 543
569extern const char 544extern const char *g_version_opentracker_c, *g_version_accesslist_c, *g_version_clean_c, *g_version_fullscrape_c, *g_version_http_c, *g_version_iovec_c,
570*g_version_opentracker_c, *g_version_accesslist_c, *g_version_clean_c, *g_version_fullscrape_c, *g_version_http_c, 545 *g_version_mutex_c, *g_version_stats_c, *g_version_udp_c, *g_version_vector_c, *g_version_scan_urlencoded_query_c, *g_version_trackerlogic_c,
571*g_version_iovec_c, *g_version_mutex_c, *g_version_stats_c, *g_version_udp_c, *g_version_vector_c, 546 *g_version_livesync_c, *g_version_rijndael_c;
572*g_version_scan_urlencoded_query_c, *g_version_trackerlogic_c, *g_version_livesync_c, *g_version_rijndael_c; 547
573 548size_t stats_return_tracker_version(char *reply) {
574size_t stats_return_tracker_version( char *reply ) { 549 return sprintf(reply, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s", g_version_opentracker_c, g_version_accesslist_c, g_version_clean_c, g_version_fullscrape_c,
575 return sprintf( reply, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 550 g_version_http_c, g_version_iovec_c, g_version_mutex_c, g_version_stats_c, g_version_udp_c, g_version_vector_c,
576 g_version_opentracker_c, g_version_accesslist_c, g_version_clean_c, g_version_fullscrape_c, g_version_http_c, 551 g_version_scan_urlencoded_query_c, g_version_trackerlogic_c, g_version_livesync_c, g_version_rijndael_c);
577 g_version_iovec_c, g_version_mutex_c, g_version_stats_c, g_version_udp_c, g_version_vector_c, 552}
578 g_version_scan_urlencoded_query_c, g_version_trackerlogic_c, g_version_livesync_c, g_version_rijndael_c ); 553
579} 554size_t return_stats_for_tracker(char *reply, int mode, int format) {
580 555 (void)format;
581size_t return_stats_for_tracker( char *reply, int mode, int format ) { 556 switch (mode & TASK_TASK_MASK) {
582 (void) format; 557 case TASK_STATS_CONNS:
583 switch( mode & TASK_TASK_MASK ) { 558 return stats_connections_mrtg(reply);
584 case TASK_STATS_CONNS: 559 case TASK_STATS_SCRAPE:
585 return stats_connections_mrtg( reply ); 560 return stats_scrape_mrtg(reply);
586 case TASK_STATS_SCRAPE: 561 case TASK_STATS_UDP:
587 return stats_scrape_mrtg( reply ); 562 return stats_udpconnections_mrtg(reply);
588 case TASK_STATS_UDP: 563 case TASK_STATS_TCP:
589 return stats_udpconnections_mrtg( reply ); 564 return stats_tcpconnections_mrtg(reply);
590 case TASK_STATS_TCP: 565 case TASK_STATS_FULLSCRAPE:
591 return stats_tcpconnections_mrtg( reply ); 566 return stats_fullscrapes_mrtg(reply);
592 case TASK_STATS_FULLSCRAPE: 567 case TASK_STATS_COMPLETED:
593 return stats_fullscrapes_mrtg( reply ); 568 return stats_return_completed_mrtg(reply);
594 case TASK_STATS_COMPLETED: 569 case TASK_STATS_HTTPERRORS:
595 return stats_return_completed_mrtg( reply ); 570 return stats_httperrors_txt(reply);
596 case TASK_STATS_HTTPERRORS: 571 case TASK_STATS_VERSION:
597 return stats_httperrors_txt( reply ); 572 return stats_return_tracker_version(reply);
598 case TASK_STATS_VERSION: 573 case TASK_STATS_RENEW:
599 return stats_return_tracker_version( reply ); 574 return stats_return_renew_bucket(reply);
600 case TASK_STATS_RENEW: 575 case TASK_STATS_SYNCS:
601 return stats_return_renew_bucket( reply ); 576 return stats_return_sync_mrtg(reply);
602 case TASK_STATS_SYNCS:
603 return stats_return_sync_mrtg( reply );
604#ifdef WANT_LOG_NUMWANT 577#ifdef WANT_LOG_NUMWANT
605 case TASK_STATS_NUMWANTS: 578 case TASK_STATS_NUMWANTS:
606 return stats_return_numwants( reply ); 579 return stats_return_numwants(reply);
607#endif 580#endif
608 default: 581 default:
609 return 0; 582 return 0;
610 } 583 }
611} 584}
612 585
613static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) { 586static void stats_make(int *iovec_entries, struct iovec **iovector, ot_tasktype mode) {
614 char *r; 587 char *r;
615 588
616 *iovec_entries = 0; 589 *iovec_entries = 0;
617 *iovector = NULL; 590 *iovector = NULL;
618 if( !( r = iovec_increase( iovec_entries, iovector, OT_STATS_TMPSIZE ) ) ) 591 if (!(r = iovec_increase(iovec_entries, iovector, OT_STATS_TMPSIZE)))
619 return; 592 return;
620 593
621 switch( mode & TASK_TASK_MASK ) { 594 switch (mode & TASK_TASK_MASK) {
622 case TASK_STATS_TORRENTS: r += stats_torrents_mrtg( r ); break; 595 case TASK_STATS_TORRENTS:
623 case TASK_STATS_PEERS: r += stats_peers_mrtg( r ); break; 596 r += stats_torrents_mrtg(r);
624 case TASK_STATS_SLASH24S: r += stats_slash24s_txt( r, 128 ); break; 597 break;
625 case TASK_STATS_TOP10: r += stats_top_txt( r, 10 ); break; 598 case TASK_STATS_PEERS:
626 case TASK_STATS_TOP100: 599 r += stats_peers_mrtg(r);
627 r = iovec_fix_increase_or_free( iovec_entries, iovector, r, 4 * OT_STATS_TMPSIZE ); 600 break;
628 if( !r ) return; 601 case TASK_STATS_SLASH24S:
629 r += stats_top_txt( r, 100 ); break; 602 r += stats_slash24s_txt(r, 128);
630 case TASK_STATS_EVERYTHING: r = iovec_fix_increase_or_free( iovec_entries, iovector, r, OT_STATS_TMPSIZE + 64 * OT_PEER_TIMEOUT ); 603 break;
631 if( !r ) return; 604 case TASK_STATS_TOP10:
632 r += stats_return_everything( r ); break; 605 r += stats_top_txt(r, 10);
606 break;
607 case TASK_STATS_TOP100:
608 r = iovec_fix_increase_or_free(iovec_entries, iovector, r, 4 * OT_STATS_TMPSIZE);
609 if (!r)
610 return;
611 r += stats_top_txt(r, 100);
612 break;
613 case TASK_STATS_EVERYTHING:
614 r = iovec_fix_increase_or_free(iovec_entries, iovector, r, OT_STATS_TMPSIZE + 64 * OT_PEER_TIMEOUT);
615 if (!r)
616 return;
617 r += stats_return_everything(r);
618 break;
633#ifdef WANT_SPOT_WOODPECKER 619#ifdef WANT_SPOT_WOODPECKER
634 case TASK_STATS_WOODPECKERS: r += stats_return_woodpeckers( r, 128 ); break; 620 case TASK_STATS_WOODPECKERS:
621 r += stats_return_woodpeckers(r, 128);
622 break;
635#endif 623#endif
636#ifdef WANT_FULLLOG_NETWORKS 624#ifdef WANT_FULLLOG_NETWORKS
637 case TASK_STATS_FULLLOG: stats_return_fulllog( iovec_entries, iovector, r ); 625 case TASK_STATS_FULLLOG:
638 return; 626 stats_return_fulllog(iovec_entries, iovector, r);
627 return;
639#endif 628#endif
640 default: 629 default:
641 iovec_free(iovec_entries, iovector); 630 iovec_free(iovec_entries, iovector);
642 return; 631 return;
643 } 632 }
644 iovec_fixlast( iovec_entries, iovector, r ); 633 iovec_fixlast(iovec_entries, iovector, r);
645} 634}
646 635
647void stats_issue_event( ot_status_event event, PROTO_FLAG proto, uintptr_t event_data ) { 636void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_data) {
648 switch( event ) { 637 switch (event) {
649 case EVENT_ACCEPT: 638 case EVENT_ACCEPT:
650 if( proto == FLAG_TCP ) ot_overall_tcp_connections++; else ot_overall_udp_connections++; 639 if (proto == FLAG_TCP)
640 ot_overall_tcp_connections++;
641 else
642 ot_overall_udp_connections++;
651#ifdef WANT_LOG_NETWORKS 643#ifdef WANT_LOG_NETWORKS
652 stat_increase_network_count( &stats_network_counters_root, 0, event_data ); 644 stat_increase_network_count(&stats_network_counters_root, 0, event_data);
653#endif 645#endif
654 break; 646 break;
655 case EVENT_ANNOUNCE: 647 case EVENT_ANNOUNCE:
656 if( proto == FLAG_TCP ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++; 648 if (proto == FLAG_TCP)
657 break; 649 ot_overall_tcp_successfulannounces++;
658 case EVENT_CONNECT: 650 else
659 if( proto == FLAG_TCP ) ot_overall_tcp_connects++; else ot_overall_udp_connects++; 651 ot_overall_udp_successfulannounces++;
660 break; 652 break;
661 case EVENT_COMPLETED: 653 case EVENT_CONNECT:
654 if (proto == FLAG_TCP)
655 ot_overall_tcp_connects++;
656 else
657 ot_overall_udp_connects++;
658 break;
659 case EVENT_COMPLETED:
662#ifdef WANT_SYSLOGS 660#ifdef WANT_SYSLOGS
663 if( event_data) { 661 if (event_data) {
664 struct ot_workstruct *ws = (struct ot_workstruct *)event_data; 662 struct ot_workstruct *ws = (struct ot_workstruct *)event_data;
665 char timestring[64]; 663 char timestring[64];
666 char hash_hex[42], peerid_hex[42], ip_readable[64]; 664 char hash_hex[42], peerid_hex[42], ip_readable[64];
667 struct tm time_now; 665 struct tm time_now;
668 time_t ttt; 666 time_t ttt;
669 667
670 time( &ttt ); 668 time(&ttt);
671 localtime_r( &ttt, &time_now ); 669 localtime_r(&ttt, &time_now);
672 strftime( timestring, sizeof( timestring ), "%FT%T%z", &time_now ); 670 strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now);
673 671
674 to_hex( hash_hex, *ws->hash ); 672 to_hex(hash_hex, *ws->hash);
675 if( ws->peer_id ) 673 if (ws->peer_id)
676 to_hex( peerid_hex, (uint8_t*)ws->peer_id ); 674 to_hex(peerid_hex, (uint8_t *)ws->peer_id);
677 else { 675 else {
678 *peerid_hex=0; 676 *peerid_hex = 0;
679 } 677 }
680 678
681 ip_readable[ fmt_ip6c( ip_readable, (char*)&ws->peer ) ] = 0; 679 ip_readable[fmt_ip6c(ip_readable, (char *)&ws->peer)] = 0;
682#if 0 680#if 0
683 /* XXX */ 681 /* XXX */
684 ip_readable[ fmt_ip4( ip_readable, (char*)&ws->peer ) ] = 0; 682 ip_readable[ fmt_ip4( ip_readable, (char*)&ws->peer ) ] = 0;
685#endif 683#endif
686 syslog( LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable ); 684 syslog(LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable);
687 }
688#endif
689 ot_overall_completed++;
690 break;
691 case EVENT_SCRAPE:
692 if( proto == FLAG_TCP ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++;
693 break;
694 case EVENT_FULLSCRAPE:
695 ot_full_scrape_count++;
696 ot_full_scrape_size += event_data;
697 break;
698 case EVENT_FULLSCRAPE_REQUEST:
699 {
700 ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */
701 char _debug[512];
702 int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 );
703 off += fmt_ip6c( _debug+off, *ip );
704 off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" );
705 (void)write( 2, _debug, off );
706 ot_full_scrape_request_count++;
707 }
708 break;
709 case EVENT_FULLSCRAPE_REQUEST_GZIP:
710 {
711 ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */
712 char _debug[512];
713 int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 );
714 off += fmt_ip6c(_debug+off, *ip );
715 off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" );
716 (void)write( 2, _debug, off );
717 ot_full_scrape_request_count++;
718 } 685 }
719 break; 686#endif
720 case EVENT_FAILED: 687 ot_overall_completed++;
721 ot_failed_request_counts[event_data]++; 688 break;
722 break; 689 case EVENT_SCRAPE:
723 case EVENT_RENEW: 690 if (proto == FLAG_TCP)
724 ot_renewed[event_data]++; 691 ot_overall_tcp_successfulscrapes++;
725 break; 692 else
726 case EVENT_SYNC: 693 ot_overall_udp_successfulscrapes++;
727 ot_overall_sync_count+=event_data; 694 break;
728 break; 695 case EVENT_FULLSCRAPE:
729 case EVENT_BUCKET_LOCKED: 696 ot_full_scrape_count++;
730 ot_overall_stall_count++; 697 ot_full_scrape_size += event_data;
731 break; 698 break;
699 case EVENT_FULLSCRAPE_REQUEST: {
700 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
701 char _debug[512];
702 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60);
703 off += fmt_ip6c(_debug + off, *ip);
704 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
705 (void)write(2, _debug, off);
706 ot_full_scrape_request_count++;
707 } break;
708 case EVENT_FULLSCRAPE_REQUEST_GZIP: {
709 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
710 char _debug[512];
711 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60);
712 off += fmt_ip6c(_debug + off, *ip);
713 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
714 (void)write(2, _debug, off);
715 ot_full_scrape_request_count++;
716 } break;
717 case EVENT_FAILED:
718 ot_failed_request_counts[event_data]++;
719 break;
720 case EVENT_RENEW:
721 ot_renewed[event_data]++;
722 break;
723 case EVENT_SYNC:
724 ot_overall_sync_count += event_data;
725 break;
726 case EVENT_BUCKET_LOCKED:
727 ot_overall_stall_count++;
728 break;
732#ifdef WANT_SPOT_WOODPECKER 729#ifdef WANT_SPOT_WOODPECKER
733 case EVENT_WOODPECKER: 730 case EVENT_WOODPECKER:
734 pthread_mutex_lock( &g_woodpeckers_mutex ); 731 pthread_mutex_lock(&g_woodpeckers_mutex);
735 stat_increase_network_count( &stats_woodpeckers_tree, 0, event_data ); 732 stat_increase_network_count(&stats_woodpeckers_tree, 0, event_data);
736 pthread_mutex_unlock( &g_woodpeckers_mutex ); 733 pthread_mutex_unlock(&g_woodpeckers_mutex);
737 break; 734 break;
738#endif 735#endif
739 case EVENT_CONNID_MISSMATCH: 736 case EVENT_CONNID_MISSMATCH:
740 ++ot_overall_udp_connectionidmissmatches; 737 ++ot_overall_udp_connectionidmissmatches;
741 default: 738 default:
742 break; 739 break;
743 } 740 }
744} 741}
745 742
746void stats_cleanup() { 743void stats_cleanup() {
747#ifdef WANT_SPOT_WOODPECKER 744#ifdef WANT_SPOT_WOODPECKER
748 pthread_mutex_lock( &g_woodpeckers_mutex ); 745 pthread_mutex_lock(&g_woodpeckers_mutex);
749 stats_shift_down_network_count( &stats_woodpeckers_tree, 0, 1 ); 746 stats_shift_down_network_count(&stats_woodpeckers_tree, 0, 1);
750 pthread_mutex_unlock( &g_woodpeckers_mutex ); 747 pthread_mutex_unlock(&g_woodpeckers_mutex);
751#endif 748#endif
752} 749}
753 750
754static void * stats_worker( void * args ) { 751static void *stats_worker(void *args) {
755 int iovec_entries; 752 int iovec_entries;
756 struct iovec *iovector; 753 struct iovec *iovector;
757 754
758 (void) args; 755 (void)args;
759 756
760 while( 1 ) { 757 while (1) {
761 ot_tasktype tasktype = TASK_STATS; 758 ot_tasktype tasktype = TASK_STATS;
762 ot_taskid taskid = mutex_workqueue_poptask( &tasktype ); 759 ot_taskid taskid = mutex_workqueue_poptask(&tasktype);
763 stats_make( &iovec_entries, &iovector, tasktype ); 760 stats_make(&iovec_entries, &iovector, tasktype);
764 if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) ) 761 if (mutex_workqueue_pushresult(taskid, iovec_entries, iovector))
765 iovec_free( &iovec_entries, &iovector ); 762 iovec_free(&iovec_entries, &iovector);
766 } 763 }
767 return NULL; 764 return NULL;
768} 765}
769 766
770void stats_deliver( int64 sock, int tasktype ) { 767void stats_deliver(int64 sock, int tasktype) { mutex_workqueue_pushtask(sock, tasktype); }
771 mutex_workqueue_pushtask( sock, tasktype );
772}
773 768
774static pthread_t thread_id; 769static pthread_t thread_id;
775void stats_init( ) { 770void stats_init() {
776 ot_start_time = g_now_seconds; 771 ot_start_time = g_now_seconds;
777 pthread_create( &thread_id, NULL, stats_worker, NULL ); 772 pthread_create(&thread_id, NULL, stats_worker, NULL);
778} 773}
779 774
780void stats_deinit( ) { 775void stats_deinit() {
781 pthread_cancel( thread_id ); 776 pthread_cancel(thread_id);
782} 777}
783 778
784const char *g_version_stats_c = "$Source$: $Revision$\n"; 779const char *g_version_stats_c = "$Source$: $Revision$\n";