diff options
author | erdgeist <> | 2007-10-17 14:43:14 +0000 |
---|---|---|
committer | erdgeist <> | 2007-10-17 14:43:14 +0000 |
commit | 8e78ac70121b3d634cc224726b169e26938525d9 (patch) | |
tree | b319e83d0062e0c60e4edba50dcd771a14b50ded | |
parent | 1e6b48cbc0f426901a9fa5e7045a52f28d110361 (diff) |
HEADS UP: you need to check out the latest libowfat! Huge allocations are now taken from mmap()ed regions, not from heap anymore
-rw-r--r-- | opentracker.c | 13 | ||||
-rw-r--r-- | trackerlogic.c | 35 |
2 files changed, 31 insertions, 17 deletions
diff --git a/opentracker.c b/opentracker.c index ae755cc..2e46fa1 100644 --- a/opentracker.c +++ b/opentracker.c | |||
@@ -83,7 +83,7 @@ int main( int argc, char **argv ); | |||
83 | static void httperror( const int64 s, const char *title, const char *message ); | 83 | static void httperror( const int64 s, const char *title, const char *message ); |
84 | static void httpresponse( const int64 s, char *data ); | 84 | static void httpresponse( const int64 s, char *data ); |
85 | 85 | ||
86 | static void sendmallocdata( const int64 s, char *buffer, const size_t size ); | 86 | static void sendmmapdata( const int64 s, char *buffer, const size_t size ); |
87 | static void senddata( const int64 s, char *buffer, const size_t size ); | 87 | static void senddata( const int64 s, char *buffer, const size_t size ); |
88 | 88 | ||
89 | static void server_mainloop( ); | 89 | static void server_mainloop( ); |
@@ -132,7 +132,7 @@ static void httperror( const int64 s, const char *title, const char *message ) { | |||
132 | senddata(s,static_outbuf,reply_size); | 132 | senddata(s,static_outbuf,reply_size); |
133 | } | 133 | } |
134 | 134 | ||
135 | static void sendmallocdata( const int64 s, char *buffer, size_t size ) { | 135 | static void sendmmapdata( const int64 s, char *buffer, size_t size ) { |
136 | struct http_data *h = io_getcookie( s ); | 136 | struct http_data *h = io_getcookie( s ); |
137 | char *header; | 137 | char *header; |
138 | size_t header_size; | 138 | size_t header_size; |
@@ -152,7 +152,7 @@ static void sendmallocdata( const int64 s, char *buffer, size_t size ) { | |||
152 | 152 | ||
153 | iob_reset( &h->batch ); | 153 | iob_reset( &h->batch ); |
154 | iob_addbuf_free( &h->batch, header, header_size ); | 154 | iob_addbuf_free( &h->batch, header, header_size ); |
155 | iob_addbuf_free( &h->batch, buffer, size ); | 155 | iob_addbuf_munmap( &h->batch, buffer, size ); |
156 | 156 | ||
157 | /* writeable sockets timeout after twice the pool timeout | 157 | /* writeable sockets timeout after twice the pool timeout |
158 | which defaults to 5 minutes (e.g. after 10 minutes) */ | 158 | which defaults to 5 minutes (e.g. after 10 minutes) */ |
@@ -253,7 +253,7 @@ LOG_TO_STDERR( "sync: %d.%d.%d.%d\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); | |||
253 | 253 | ||
254 | if( mode == SYNC_OUT ) { | 254 | if( mode == SYNC_OUT ) { |
255 | if( !( reply_size = return_changeset_for_tracker( &reply ) ) ) HTTPERROR_500; | 255 | if( !( reply_size = return_changeset_for_tracker( &reply ) ) ) HTTPERROR_500; |
256 | return sendmallocdata( s, reply, reply_size ); | 256 | return sendmmapdata( s, reply, reply_size ); |
257 | } | 257 | } |
258 | 258 | ||
259 | /* Simple but proof for now */ | 259 | /* Simple but proof for now */ |
@@ -305,7 +305,7 @@ LOG_TO_STDERR( "sync: %d.%d.%d.%d\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); | |||
305 | LOG_TO_STDERR( "stats: %d.%d.%d.%d - mode: dmem\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); | 305 | LOG_TO_STDERR( "stats: %d.%d.%d.%d - mode: dmem\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); |
306 | 306 | ||
307 | if( !( reply_size = return_memstat_for_tracker( &reply ) ) ) HTTPERROR_500; | 307 | if( !( reply_size = return_memstat_for_tracker( &reply ) ) ) HTTPERROR_500; |
308 | return sendmallocdata( s, reply, reply_size ); | 308 | return sendmmapdata( s, reply, reply_size ); |
309 | 309 | ||
310 | case STATS_UDP: | 310 | case STATS_UDP: |
311 | t = time( NULL ) - ot_start_time; | 311 | t = time( NULL ) - ot_start_time; |
@@ -380,9 +380,8 @@ SCRAPE_WORKAROUND: | |||
380 | LOG_TO_STDERR( "scrp: %d.%d.%d.%d - FULL SCRAPE\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); | 380 | LOG_TO_STDERR( "scrp: %d.%d.%d.%d - FULL SCRAPE\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); |
381 | 381 | ||
382 | if( !( reply_size = return_fullscrape_for_tracker( &reply ) ) ) HTTPERROR_500; | 382 | if( !( reply_size = return_fullscrape_for_tracker( &reply ) ) ) HTTPERROR_500; |
383 | if( (c = realloc( reply, reply_size ) ) ) reply = c; | ||
384 | ot_overall_tcp_successfulannounces++; | 383 | ot_overall_tcp_successfulannounces++; |
385 | return sendmallocdata( s, reply, reply_size ); | 384 | return sendmmapdata( s, reply, reply_size ); |
386 | } | 385 | } |
387 | 386 | ||
388 | /* Enough for http header + whole scrape string */ | 387 | /* Enough for http header + whole scrape string */ |
diff --git a/trackerlogic.c b/trackerlogic.c index ee7b7fc..3f09d56 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
@@ -27,8 +27,8 @@ static ot_vector accesslist; | |||
27 | #define WANT_ACCESS_CONTROL | 27 | #define WANT_ACCESS_CONTROL |
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | size_t changeset_size = 0; | 30 | static size_t changeset_size = 0; |
31 | time_t last_clean_time = 0; | 31 | static time_t last_clean_time = 0; |
32 | 32 | ||
33 | /* Converter function from memory to human readable hex strings | 33 | /* Converter function from memory to human readable hex strings |
34 | - definitely not thread safe!!! | 34 | - definitely not thread safe!!! |
@@ -305,14 +305,16 @@ size_t return_peers_for_torrent( ot_torrent *torrent, size_t amount, char *reply | |||
305 | /* Fetch full scrape info for all torrents */ | 305 | /* Fetch full scrape info for all torrents */ |
306 | size_t return_fullscrape_for_tracker( char **reply ) { | 306 | size_t return_fullscrape_for_tracker( char **reply ) { |
307 | size_t torrent_count = 0, j; | 307 | size_t torrent_count = 0, j; |
308 | size_t allocated, replysize, usedpages; | ||
308 | int i, k; | 309 | int i, k; |
309 | char *r; | 310 | char *r; |
310 | 311 | ||
311 | for( i=0; i<256; ++i ) | 312 | for( i=0; i<256; ++i ) |
312 | torrent_count += all_torrents[i].size; | 313 | torrent_count += all_torrents[i].size; |
313 | 314 | ||
314 | // one extra for pro- and epilogue | 315 | /* one extra for pro- and epilogue */ |
315 | if( !( r = *reply = malloc( 100*(1+torrent_count) ) ) ) return 0; | 316 | allocated = 100*(1+torrent_count); |
317 | if( !( r = *reply = mmap( NULL, allocated, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) ) return 0; | ||
316 | 318 | ||
317 | memmove( r, "d5:filesd", 9 ); r += 9; | 319 | memmove( r, "d5:filesd", 9 ); r += 9; |
318 | for( i=0; i<256; ++i ) { | 320 | for( i=0; i<256; ++i ) { |
@@ -334,11 +336,19 @@ size_t return_fullscrape_for_tracker( char **reply ) { | |||
334 | } | 336 | } |
335 | 337 | ||
336 | *r++='e'; *r++='e'; | 338 | *r++='e'; *r++='e'; |
337 | return r - *reply; | 339 | |
340 | replysize = ( r - *reply ); | ||
341 | if( allocated > replysize ) { | ||
342 | usedpages = 1 + ( replysize / getpagesize() ); | ||
343 | munmap( *reply + usedpages * getpagesize(), allocated - replysize ); | ||
344 | } | ||
345 | |||
346 | return replysize; | ||
338 | } | 347 | } |
339 | 348 | ||
340 | size_t return_memstat_for_tracker( char **reply ) { | 349 | size_t return_memstat_for_tracker( char **reply ) { |
341 | size_t torrent_count = 0, j; | 350 | size_t torrent_count = 0, j; |
351 | size_t allocated, replysize, usedpages; | ||
342 | int i, k; | 352 | int i, k; |
343 | char *r; | 353 | char *r; |
344 | 354 | ||
@@ -347,7 +357,8 @@ size_t return_memstat_for_tracker( char **reply ) { | |||
347 | torrent_count += torrents_list->size; | 357 | torrent_count += torrents_list->size; |
348 | } | 358 | } |
349 | 359 | ||
350 | if( !( r = *reply = malloc( 256*32 + (43+OT_POOLS_COUNT*32)*torrent_count ) ) ) return 0; | 360 | allocated = 256*32 + (43+OT_POOLS_COUNT*32)*torrent_count; |
361 | if( !( r = *reply = mmap( NULL, allocated, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) ) return 0; | ||
351 | 362 | ||
352 | for( i=0; i<256; ++i ) | 363 | for( i=0; i<256; ++i ) |
353 | r += sprintf( r, "%02X: %08X %08X\n", i, (unsigned int)all_torrents[i].size, (unsigned int)all_torrents[i].space ); | 364 | r += sprintf( r, "%02X: %08X %08X\n", i, (unsigned int)all_torrents[i].size, (unsigned int)all_torrents[i].space ); |
@@ -363,7 +374,13 @@ size_t return_memstat_for_tracker( char **reply ) { | |||
363 | } | 374 | } |
364 | } | 375 | } |
365 | 376 | ||
366 | return r - *reply; | 377 | replysize = ( r - *reply ); |
378 | if( allocated > replysize ) { | ||
379 | usedpages = 1 + ( replysize / getpagesize() ); | ||
380 | munmap( *reply + usedpages * getpagesize(), allocated - replysize ); | ||
381 | } | ||
382 | |||
383 | return replysize; | ||
367 | } | 384 | } |
368 | 385 | ||
369 | /* Fetches scrape info for a specific torrent */ | 386 | /* Fetches scrape info for a specific torrent */ |
@@ -506,9 +523,7 @@ size_t return_changeset_for_tracker( char **reply ) { | |||
506 | 523 | ||
507 | clean_all_torrents(); | 524 | clean_all_torrents(); |
508 | 525 | ||
509 | *reply = malloc( 8 + changeset_size + 2 ); | 526 | if( !( *reply = mmap( NULL, 8 + changeset_size + 2, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) ) return 0; |
510 | if( !*reply ) | ||
511 | return 0; | ||
512 | 527 | ||
513 | memmove( *reply, "d4:syncd", 8 ); | 528 | memmove( *reply, "d4:syncd", 8 ); |
514 | for( i = 0; i < changeset.size; ++i ) { | 529 | for( i = 0; i < changeset.size; ++i ) { |