summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-04-03 02:56:06 +0200
committerDirk Engling <erdgeist@erdgeist.org>2026-04-03 02:56:06 +0200
commit5d2033ac8932d223a00d7d6e29cedd9b2e23bc7f (patch)
treebdbecd4b53ee8b1a620c5d1a41599a6be500bc03 /ot_stats.c
parente4eb716d2688f5c2d4615d79ad5f4f1eb50a9343 (diff)
Clean up top100 stats codeHEADmaster
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c56
1 files changed, 26 insertions, 30 deletions
diff --git a/ot_stats.c b/ot_stats.c
index 3d64bce..a9e745c 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -190,7 +190,7 @@ static size_t stats_get_highscore_networks(stats_network_node *node, int depth,
190 return score; 190 return score;
191} 191}
192 192
193static size_t stats_return_busy_networks(char *reply, stats_network_node *tree, int amount, int limit) { 193static size_t stats_return_busy_networks(char *reply, stats_network_node *tree, size_t amount, int limit) {
194 ot_ip6 networks[amount]; 194 ot_ip6 networks[amount];
195 ot_ip6 node_value; 195 ot_ip6 node_value;
196 size_t scores[amount]; 196 size_t scores[amount];
@@ -204,14 +204,14 @@ static size_t stats_return_busy_networks(char *reply, stats_network_node *tree,
204 stats_get_highscore_networks(tree, 0, node_value, scores, networks, amount, limit); 204 stats_get_highscore_networks(tree, 0, node_value, scores, networks, amount, limit);
205 205
206 r += sprintf(r, "Networks, limit /%d:\n", limit + STATS_NETWORK_NODE_BITWIDTH); 206 r += sprintf(r, "Networks, limit /%d:\n", limit + STATS_NETWORK_NODE_BITWIDTH);
207 for (i = amount - 1; i >= 0; --i) { 207 for (i = amount; i > 0; --i) {
208 if (scores[i]) { 208 if (scores[i - 1]) {
209 r += sprintf(r, "%08zd: ", scores[i]); 209 r += sprintf(r, "%08zd: ", scores[i - 1]);
210 // #ifdef WANT_V6 210 // #ifdef WANT_V6
211 r += fmt_ip6c(r, networks[i]); 211 r += fmt_ip6c(r, networks[i - 1]);
212#if 0 212#if 0
213 // XXX 213 // XXX
214 r += fmt_ip4( r, networks[i]); 214 r += fmt_ip4( r, networks[i - 1]);
215#endif 215#endif
216 *r++ = '\n'; 216 *r++ = '\n';
217 } 217 }
@@ -315,6 +315,21 @@ typedef struct {
315 ot_hash hash; 315 ot_hash hash;
316} ot_record; 316} ot_record;
317 317
318static inline void stats_insert_into_highscore(size_t val, ot_record* list, size_t amount, ot_hash* hash) {
319 size_t i;
320 for (i = 0; (i < amount) && (list[i].val > val); ++i)
321 {}
322 /* if we score better than a record in the list, we need to make room in the high
323 score array by moving lower scoring entries up */
324 if (i < amount) {
325 memmove(list + i + 1, list + i, (amount - i - 1) * sizeof(ot_record));
326
327 memcpy(&list[i].hash, hash, sizeof(ot_hash));
328 list[i].val = val;
329 }
330}
331
332
318/* Fetches stats from tracker */ 333/* Fetches stats from tracker */
319size_t stats_top_txt(char *reply, size_t amount) { 334size_t stats_top_txt(char *reply, size_t amount) {
320 size_t j, idx, bucket; 335 size_t j, idx, bucket;
@@ -328,6 +343,7 @@ size_t stats_top_txt(char *reply, size_t amount) {
328 byte_zero(top100c, sizeof(top100c)); 343 byte_zero(top100c, sizeof(top100c));
329 byte_zero(top100l, sizeof(top100l)); 344 byte_zero(top100l, sizeof(top100l));
330 345
346 /* Iterate over complete torrent list */
331 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { 347 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
332 ot_vector *torrents_list = mutex_bucket_lock(bucket); 348 ot_vector *torrents_list = mutex_bucket_lock(bucket);
333 for (j = 0; j < torrents_list->size; ++j) { 349 for (j = 0; j < torrents_list->size; ++j) {
@@ -335,30 +351,10 @@ size_t stats_top_txt(char *reply, size_t amount) {
335 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; 351 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
336 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; 352 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
337 size_t leech_count = peer_count - seed_count; 353 size_t leech_count = peer_count - seed_count;
338 idx = amount - 1; 354
339 while ((idx >= 0) && (peer_count > top100c[idx].val)) 355 stats_insert_into_highscore(peer_count, top100c, amount, &torrent->hash);
340 --idx; 356 stats_insert_into_highscore(seed_count, top100s, amount, &torrent->hash);
341 if (idx++ != amount - 1) { 357 stats_insert_into_highscore(leech_count, top100l, amount, &torrent->hash);
342 memmove(top100c + idx + 1, top100c + idx, (amount - 1 - idx) * sizeof(ot_record));
343 memcpy(&top100c[idx].hash, &torrent->hash, sizeof(ot_hash));
344 top100c[idx].val = peer_count;
345 }
346 idx = amount - 1;
347 while ((idx >= 0) && (seed_count > top100s[idx].val))
348 --idx;
349 if (idx++ != amount - 1) {
350 memmove(top100s + idx + 1, top100s + idx, (amount - 1 - idx) * sizeof(ot_record));
351 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash));
352 top100s[idx].val = seed_count;
353 }
354 idx = amount - 1;
355 while ((idx >= 0) && (leech_count > top100l[idx].val))
356 --idx;
357 if (idx++ != amount - 1) {
358 memmove(top100l + idx + 1, top100l + idx, (amount - 1 - idx) * sizeof(ot_record));
359 memcpy(&top100l[idx].hash, &torrent->hash, sizeof(ot_hash));
360 top100l[idx].val = leech_count;
361 }
362 } 358 }
363 mutex_bucket_unlock(bucket, 0); 359 mutex_bucket_unlock(bucket, 0);
364 if (!g_opentracker_running) 360 if (!g_opentracker_running)