summaryrefslogtreecommitdiff
path: root/src/ot_clean.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ot_clean.c')
-rw-r--r--src/ot_clean.c208
1 files changed, 208 insertions, 0 deletions
diff --git a/src/ot_clean.c b/src/ot_clean.c
new file mode 100644
index 0000000..8eb1da2
--- /dev/null
+++ b/src/ot_clean.c
@@ -0,0 +1,208 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <poll.h>
6#include <pthread.h>
7#include <stdio.h>
8#include <string.h>
9#include <unistd.h>
10
11/* Opentracker */
12#include "ot_accesslist.h"
13#include "ot_clean.h"
14#include "ot_mutex.h"
15#include "ot_stats.h"
16#include "ot_vector.h"
17#include "trackerlogic.h"
18
19ot_time g_empty_torrent_timeout = OT_TORRENT_TIMEOUT;
20
21/* Returns amount of removed peers */
22static ssize_t clean_single_bucket(ot_peer *peers, size_t peer_count, size_t peer_size, time_t timedout, size_t *removed_seeders) {
23 ot_peer *last_peer, *insert_point;
24
25 if (!peer_count)
26 return 0;
27
28 last_peer = peers + peer_count * peer_size;
29
30 /* Two scan modes: unless there is one peer removed, just increase ot_peertime */
31 while (peers < last_peer) {
32 time_t timediff = timedout + OT_PEERTIME(peers, peer_size);
33 if (timediff >= OT_PEER_TIMEOUT)
34 break;
35 OT_PEERTIME(peers, peer_size) = timediff;
36 peers += peer_size;
37 }
38
39 /* If we at least remove one peer, we have to copy */
40 for (insert_point = peers; peers < last_peer; peers += peer_size) {
41 time_t timediff = timedout + OT_PEERTIME(peers, peer_size);
42
43 if (timediff < OT_PEER_TIMEOUT) {
44 OT_PEERTIME(peers, peer_size) = timediff;
45 memcpy(insert_point, peers, peer_size);
46 insert_point += peer_size;
47 } else if (OT_PEERFLAG_D(peers, peer_size) & PEER_FLAG_SEEDING)
48 (*removed_seeders)++;
49 }
50
51 return (peers - insert_point) / peer_size;
52}
53
54/* Age one family's peers by timedout minutes. Releases and zeroes the vector
55 once its last peer is gone, so an empty family costs nothing. */
56static void clean_family_vector(ot_vector *peer_vector, size_t peer_size, ot_time timedout, size_t *removed_seeders) {
57 ot_vector *bucket = peer_vector;
58 size_t num_buckets = 1, peer_count;
59
60 if (OT_VECTOR_HASBUCKETS(peer_vector)) {
61 num_buckets = peer_vector->size;
62 bucket = (ot_vector *)peer_vector->data;
63 }
64
65 while (num_buckets--) {
66 size_t removed_peers = clean_single_bucket(bucket->data, bucket->size, peer_size, timedout, removed_seeders);
67 bucket->size -= removed_peers;
68 if (removed_peers)
69 vector_fixup_peers(bucket, peer_size);
70
71 /* Skip to next bucket, a vector containing peers */
72 ++bucket;
73 }
74
75 peer_count = vector_peer_count(peer_vector);
76
77 /* Run dry: release the family (a collapsed bucket list or a plain vector) */
78 if (!peer_count) {
79 free_peer_vector(peer_vector);
80 memset(peer_vector, 0, sizeof(*peer_vector));
81 return;
82 }
83
84 /* See if we need to convert the vector from simple to bucket list or back */
85 if ((peer_count > OT_PEER_BUCKET_MINCOUNT) || OT_VECTOR_HASBUCKETS(peer_vector))
86 vector_redistribute_buckets(peer_vector, peer_size, peer_count);
87}
88
89/* Clean a single torrent
90 return 1 if torrent timed out
91*/
92int clean_single_torrent(ot_torrent *torrent) {
93 ot_time timedout = time_since_minutes(torrent->base);
94 ot_peerlist *peer_list = torrent->peer_list;
95 size_t removed_seeders = 0;
96
97 /* Touched since the last sweep: nothing to do */
98 if (!timedout)
99 return 0;
100
101 /* Torrent has idled out */
102 if (timedout > OT_TORRENT_TIMEOUT)
103 return 1;
104
105 /* Already empty and nothing arrived before the peer timeout: keep it only
106 while a down count is worth remembering */
107 if (timedout > OT_PEER_TIMEOUT && !peer_list)
108 return (torrent->down_count && timedout <= g_empty_torrent_timeout) ? 0 : 1;
109
110 /* Peers past the peer timeout are gone regardless, which caps how far the
111 lists are aged in this pass */
112 if (timedout > OT_PEER_TIMEOUT)
113 timedout = OT_PEER_TIMEOUT;
114
115 if (peer_list) {
116 clean_family_vector(&peer_list->peers6, OT_PEER_SIZE6, timedout, &removed_seeders);
117 clean_family_vector(&peer_list->peers4, OT_PEER_SIZE4, timedout, &removed_seeders);
118 peer_list->seed_count -= removed_seeders;
119
120 /* Both families gone: drop the peerlist, the torrent keeps only its count */
121 if (!vector_peer_count(&peer_list->peers6) && !vector_peer_count(&peer_list->peers4)) {
122 free_peerlist(peer_list);
123 torrent->peer_list = peer_list = NULL;
124 }
125 }
126
127 if (peer_list)
128 torrent->base = time_now_minutes(); /* still carrying peers */
129 else {
130 /* Fell empty: hold base a peer timeout back so a later idle sweep makes
131 the keep-or-drop decision above */
132 ot_time now = time_now_minutes();
133 torrent->base = now > OT_PEER_TIMEOUT ? (uint32_t)(now - OT_PEER_TIMEOUT) : 0;
134 }
135 return 0;
136}
137
138/* Written to once, by clean_deinit, and never read: the byte stays in and
139 every sleep after it returns at once */
140static int g_clean_pipe[2] = {-1, -1};
141
142static void clean_sleep(void) {
143 struct pollfd pfd;
144
145 pfd.fd = g_clean_pipe[0];
146 pfd.events = POLLIN;
147 pfd.revents = 0;
148
149 poll(&pfd, 1, OT_CLEAN_SLEEP / 1000);
150}
151
152/* Clean up all peers in one bucket, remove timedout pools and torrents */
153static void clean_bucket(size_t bucket) {
154 ot_vector *torrents_list = mutex_bucket_lock((int)bucket);
155 size_t toffs;
156 int delta_torrentcount = 0;
157
158 for (toffs = 0; toffs < torrents_list->size; ++toffs) {
159 ot_torrent *torrent = ((ot_torrent *)(torrents_list->data)) + toffs;
160 if (clean_single_torrent(torrent)) {
161 vector_remove_torrent(torrents_list, torrent);
162 --delta_torrentcount;
163 --toffs;
164 }
165 }
166 mutex_bucket_unlock((int)bucket, delta_torrentcount);
167}
168
169static void *clean_worker(void *args) {
170 (void)args;
171 while (1) {
172 size_t bucket = OT_BUCKET_COUNT;
173 while (bucket) {
174 size_t todo = bucket < OT_CLEAN_BUCKETS_PER_WAKE ? bucket : OT_CLEAN_BUCKETS_PER_WAKE;
175
176 /* One bucket is locked at a time, however many a wake gets through */
177 while (todo--)
178 clean_bucket(--bucket);
179
180 if (!g_opentracker_running)
181 return NULL;
182 clean_sleep();
183 }
184 stats_cleanup();
185#ifdef WANT_ACCESSLIST
186 accesslist_cleanup();
187#endif
188 }
189 return NULL;
190}
191
192static pthread_t thread_id;
193void clean_init(void) {
194 if (pipe(g_clean_pipe) == -1)
195 fprintf(stderr, "Warning: the cleaner has no pipe to be woken through and will take up to one sleep interval to stop.\n");
196 if (pthread_create(&thread_id, NULL, clean_worker, NULL))
197 exerr("Cannot create cleanup worker thread.");
198}
199
200void clean_deinit(void) {
201 const char byte = 0;
202
203 (void)!write(g_clean_pipe[1], &byte, 1);
204 pthread_join(thread_id, NULL);
205
206 close(g_clean_pipe[0]);
207 close(g_clean_pipe[1]);
208}