summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2007-11-19 21:10:53 +0000
committererdgeist <>2007-11-19 21:10:53 +0000
commit49ba269b9d09746e90e153f3e245eaf3f97e35cc (patch)
treef4847038b777d85dccc3f0ac594ef28d88f4ec40
parent3528752c7b82d23b4cf9331a6df56e2cc8db9239 (diff)
Cleanup now moved to its own thread, too
-rw-r--r--opentracker.c12
-rw-r--r--ot_clean.c59
-rw-r--r--ot_mutex.c24
-rw-r--r--ot_mutex.h7
-rw-r--r--trackerlogic.c10
5 files changed, 78 insertions, 34 deletions
diff --git a/opentracker.c b/opentracker.c
index 59b561b..642815a 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -40,12 +40,14 @@
40#include "ot_iovec.h" 40#include "ot_iovec.h"
41#include "ot_accesslist.h" 41#include "ot_accesslist.h"
42#include "ot_mutex.h" 42#include "ot_mutex.h"
43#include "ot_clean.h"
43 44
44/* Globals */ 45/* Globals */
45static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; 46static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80;
46static const size_t SUCCESS_HTTP_SIZE_OFF = 17; 47static const size_t SUCCESS_HTTP_SIZE_OFF = 17;
47static uint32_t g_adminip_addresses[OT_ADMINIP_MAX]; 48static uint32_t g_adminip_addresses[OT_ADMINIP_MAX];
48static unsigned int g_adminip_count = 0; 49static unsigned int g_adminip_count = 0;
50static time_t ot_last_clean_time;
49time_t ot_start_time; 51time_t ot_start_time;
50time_t g_now; 52time_t g_now;
51 53
@@ -623,6 +625,7 @@ ANNOUNCE_WORKAROUND:
623static void signal_handler( int s ) { 625static void signal_handler( int s ) {
624 if( s == SIGINT ) { 626 if( s == SIGINT ) {
625 signal( SIGINT, SIG_IGN); 627 signal( SIGINT, SIG_IGN);
628
626 trackerlogic_deinit(); 629 trackerlogic_deinit();
627 exit( 0 ); 630 exit( 0 );
628 } else if( s == SIGALRM ) { 631 } else if( s == SIGALRM ) {
@@ -783,7 +786,10 @@ static void server_mainloop( ) {
783 } 786 }
784 787
785 /* See if we need to move our pools */ 788 /* See if we need to move our pools */
786 clean_all_torrents(); 789 if( g_now != ot_last_clean_time ) {
790 ot_last_clean_time = g_now;
791 clean_all_torrents();
792 }
787 } 793 }
788} 794}
789 795
@@ -872,9 +878,7 @@ int main( int argc, char **argv ) {
872 if( trackerlogic_init( serverdir ) == -1 ) 878 if( trackerlogic_init( serverdir ) == -1 )
873 panic( "Logic not started" ); 879 panic( "Logic not started" );
874 880
875 fullscrape_init( ); 881 g_now = ot_start_time = ot_last_clean_time = time( NULL );
876
877 g_now = ot_start_time = time( NULL );
878 alarm(5); 882 alarm(5);
879 883
880 server_mainloop( ); 884 server_mainloop( );
diff --git a/ot_clean.c b/ot_clean.c
index 509dd2e..625e94a 100644
--- a/ot_clean.c
+++ b/ot_clean.c
@@ -4,6 +4,7 @@
4/* System */ 4/* System */
5#include <stdlib.h> 5#include <stdlib.h>
6#include <string.h> 6#include <string.h>
7#include <pthread.h>
7 8
8/* Libowfat */ 9/* Libowfat */
9#include "byte.h" 10#include "byte.h"
@@ -12,9 +13,6 @@
12#include "trackerlogic.h" 13#include "trackerlogic.h"
13#include "ot_mutex.h" 14#include "ot_mutex.h"
14 15
15/* To remember, when we last cleaned up */
16static ot_time all_torrents_clean[OT_BUCKET_COUNT];
17
18/* Clean a single torrent 16/* Clean a single torrent
19 return 1 if torrent timed out 17 return 1 if torrent timed out
20*/ 18*/
@@ -83,37 +81,46 @@ int clean_single_torrent( ot_torrent *torrent ) {
83 return 0; 81 return 0;
84} 82}
85 83
86/* Clean up all peers in current bucket, remove timedout pools and 84static void clean_make() {
87 torrents */ 85 int bucket;
88void clean_all_torrents( void ) {
89 ot_vector *torrents_list;
90 size_t i;
91 static int bucket;
92 ot_time time_now = NOW;
93
94 /* Search for an uncleaned bucked */
95 while( ( all_torrents_clean[bucket] == time_now ) && ( ++bucket < OT_BUCKET_COUNT ) );
96 if( bucket >= OT_BUCKET_COUNT ) {
97 bucket = 0; return;
98 }
99 86
100 all_torrents_clean[bucket] = time_now; 87 for( bucket = OT_BUCKET_COUNT - 1; bucket >= 0; --bucket ) {
88 ot_vector *torrents_list = mutex_bucket_lock( bucket );
89 size_t toffs;
101 90
102 torrents_list = mutex_bucket_lock( bucket ); 91 for( toffs=0; toffs<torrents_list->size; ++toffs ) {
103 for( i=0; i<torrents_list->size; ++i ) { 92 ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + toffs;
104 ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + i; 93 if( clean_single_torrent( torrent ) ) {
105 if( clean_single_torrent( torrent ) ) { 94 vector_remove_torrent( torrents_list, torrent );
106 vector_remove_torrent( torrents_list, torrent ); 95 --toffs; continue;
107 --i; continue; 96 }
108 } 97 }
98 mutex_bucket_unlock( bucket );
109 } 99 }
110 mutex_bucket_unlock( bucket );
111} 100}
112 101
102/* Clean up all peers in current bucket, remove timedout pools and
103 torrents */
104static void * clean_worker( void * args ) {
105 args = args;
106 while( 1 ) {
107 ot_tasktype tasktype = TASK_CLEAN;
108 ot_taskid taskid = mutex_workqueue_poptask( &tasktype );
109 clean_make( );
110 mutex_workqueue_pushsuccess( taskid );
111 }
112 return NULL;
113}
114
115void clean_all_torrents( ) {
116 mutex_workqueue_pushtask( 0, TASK_CLEAN );
117}
118
119static pthread_t thread_id;
113void clean_init( void ) { 120void clean_init( void ) {
114 byte_zero( all_torrents_clean, sizeof( all_torrents_clean ) ); 121 pthread_create( &thread_id, NULL, clean_worker, NULL );
115} 122}
116 123
117void clean_deinit( void ) { 124void clean_deinit( void ) {
118 byte_zero( all_torrents_clean, sizeof( all_torrents_clean ) ); 125 pthread_cancel( thread_id );
119} 126}
diff --git a/ot_mutex.c b/ot_mutex.c
index a08b5ef..b01bb4a 100644
--- a/ot_mutex.c
+++ b/ot_mutex.c
@@ -218,6 +218,30 @@ ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ) {
218 return taskid; 218 return taskid;
219} 219}
220 220
221void mutex_workqueue_pushsuccess( ot_taskid taskid ) {
222 struct ot_task ** task;
223
224 /* Want exclusive access to tasklist */
225 MTX_DBG( "pushsuccess locks.\n" );
226 pthread_mutex_lock( &tasklist_mutex );
227 MTX_DBG( "pushsuccess locked.\n" );
228
229 task = &tasklist;
230 while( *task && ( (*task)->taskid != taskid ) )
231 *task = (*task)->next;
232
233 if( *task && ( (*task)->taskid == taskid ) ) {
234 struct ot_task *ptask = *task;
235 *task = (*task)->next;
236 free( ptask );
237 }
238
239 /* Release lock */
240 MTX_DBG( "pushsuccess unlocks.\n" );
241 pthread_mutex_unlock( &tasklist_mutex );
242 MTX_DBG( "pushsuccess unlocked.\n" );
243}
244
221int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) { 245int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovec ) {
222 struct ot_task * task; 246 struct ot_task * task;
223 /* Want exclusive access to tasklist */ 247 /* Want exclusive access to tasklist */
diff --git a/ot_mutex.h b/ot_mutex.h
index 8c2e356..b2668ce 100644
--- a/ot_mutex.h
+++ b/ot_mutex.h
@@ -32,9 +32,11 @@ typedef enum {
32 TASK_FULLSCRAPE_TPB_ASCII = 0x0202, 32 TASK_FULLSCRAPE_TPB_ASCII = 0x0202,
33 TASK_FULLSCRAPE_TPB_URLENCODED = 0x0203, 33 TASK_FULLSCRAPE_TPB_URLENCODED = 0x0203,
34 34
35 TASK_SYNC = 0x0300, 35 TASK_CLEAN = 0x0300,
36 36
37 TASK_DMEM = 0x0400, 37 TASK_SYNC = 0x0400,
38
39 TASK_DMEM = 0x0500,
38 40
39 TASK_DONE = 0x0f00, 41 TASK_DONE = 0x0f00,
40 TASK_MASK = 0xff00 42 TASK_MASK = 0xff00
@@ -44,6 +46,7 @@ typedef unsigned long ot_taskid;
44 46
45int mutex_workqueue_pushtask( int64 socket, ot_tasktype tasktype ); 47int mutex_workqueue_pushtask( int64 socket, ot_tasktype tasktype );
46void mutex_workqueue_canceltask( int64 socket ); 48void mutex_workqueue_canceltask( int64 socket );
49void mutex_workqueue_pushsuccess( ot_taskid taskid );
47ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype ); 50ot_taskid mutex_workqueue_poptask( ot_tasktype *tasktype );
48int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovector ); 51int mutex_workqueue_pushresult( ot_taskid taskid, int iovec_entries, struct iovec *iovector );
49int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovector ); 52int64 mutex_workqueue_popresult( int *iovec_entries, struct iovec ** iovector );
diff --git a/trackerlogic.c b/trackerlogic.c
index d0d5235..e4dc3a6 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -22,6 +22,7 @@
22#include "ot_stats.h" 22#include "ot_stats.h"
23#include "ot_clean.h" 23#include "ot_clean.h"
24#include "ot_accesslist.h" 24#include "ot_accesslist.h"
25#include "ot_fullscrape.h"
25 26
26void free_peerlist( ot_peerlist *peer_list ) { 27void free_peerlist( ot_peerlist *peer_list ) {
27 size_t i; 28 size_t i;
@@ -321,8 +322,10 @@ int trackerlogic_init( const char * const serverdir ) {
321 322
322 srandom( time(NULL) ); 323 srandom( time(NULL) );
323 324
324 clean_init( ); 325 /* Initialise background worker threads */
325 mutex_init( ); 326 mutex_init( );
327 clean_init( );
328 fullscrape_init( );
326 329
327 return 0; 330 return 0;
328} 331}
@@ -343,6 +346,9 @@ void trackerlogic_deinit( void ) {
343 } 346 }
344 mutex_bucket_unlock( bucket ); 347 mutex_bucket_unlock( bucket );
345 } 348 }
346 mutex_deinit( ); 349
350 /* Deinitialise background worker threads */
351 fullscrape_init( );
347 clean_deinit( ); 352 clean_deinit( );
353 mutex_deinit( );
348} 354}