From 8f520e52d7d4081a0835374c7a448116baac64ab Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 2 Nov 2026 20:39:26 +0100 Subject: Turn the bucket count into a knob, but keep cleaner wake-ups at a constant rate --- ot_clean.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'ot_clean.c') diff --git a/ot_clean.c b/ot_clean.c index e1d1971..f181e83 100644 --- a/ot_clean.c +++ b/ot_clean.c @@ -125,26 +125,34 @@ static void clean_sleep(void) { pthread_mutex_unlock(&g_clean_mutex); } -/* Clean up all peers in current bucket, remove timedout pools and - torrents */ +/* Clean up all peers in one bucket, remove timedout pools and torrents */ +static void clean_bucket(size_t bucket) { + ot_vector *torrents_list = mutex_bucket_lock((int)bucket); + size_t toffs; + int delta_torrentcount = 0; + + for (toffs = 0; toffs < torrents_list->size; ++toffs) { + ot_torrent *torrent = ((ot_torrent *)(torrents_list->data)) + toffs; + if (clean_single_torrent(torrent)) { + vector_remove_torrent(torrents_list, torrent); + --delta_torrentcount; + --toffs; + } + } + mutex_bucket_unlock((int)bucket, delta_torrentcount); +} + static void *clean_worker(void *args) { (void)args; while (1) { size_t bucket = OT_BUCKET_COUNT; - while (bucket--) { - ot_vector *torrents_list = mutex_bucket_lock(bucket); - size_t toffs; - int delta_torrentcount = 0; - - for (toffs = 0; toffs < torrents_list->size; ++toffs) { - ot_torrent *torrent = ((ot_torrent *)(torrents_list->data)) + toffs; - if (clean_single_torrent(torrent)) { - vector_remove_torrent(torrents_list, torrent); - --delta_torrentcount; - --toffs; - } - } - mutex_bucket_unlock(bucket, delta_torrentcount); + while (bucket) { + size_t todo = bucket < OT_CLEAN_BUCKETS_PER_WAKE ? bucket : OT_CLEAN_BUCKETS_PER_WAKE; + + /* One bucket is locked at a time, however many a wake gets through */ + while (todo--) + clean_bucket(--bucket); + if (!g_opentracker_running) return NULL; clean_sleep(); -- cgit v1.3