Merge remote-tracking branch 'public/bug10169_023' into bug10169_024

This commit is contained in:
Nick Mathewson 2014-02-12 12:39:15 -05:00
commit 05d8111eed
3 changed files with 27 additions and 6 deletions

View File

@ -244,12 +244,13 @@ static INLINE chunk_t *
chunk_grow(chunk_t *chunk, size_t sz) chunk_grow(chunk_t *chunk, size_t sz)
{ {
off_t offset; off_t offset;
size_t memlen_orig = chunk->memlen;
tor_assert(sz > chunk->memlen); tor_assert(sz > chunk->memlen);
offset = chunk->data - chunk->mem; offset = chunk->data - chunk->mem;
chunk = tor_realloc(chunk, CHUNK_ALLOC_SIZE(sz)); chunk = tor_realloc(chunk, CHUNK_ALLOC_SIZE(sz));
chunk->memlen = sz; chunk->memlen = sz;
chunk->data = chunk->mem + offset; chunk->data = chunk->mem + offset;
total_bytes_allocated_in_chunks += (sz - chunk->memlen); total_bytes_allocated_in_chunks += CHUNK_ALLOC_SIZE(sz) - CHUNK_ALLOC_SIZE(memlen_orig);
return chunk; return chunk;
} }
@ -274,12 +275,14 @@ preferred_chunk_size(size_t target)
} }
/** Remove from the freelists most chunks that have not been used since the /** Remove from the freelists most chunks that have not been used since the
* last call to buf_shrink_freelists(). */ * last call to buf_shrink_freelists(). Return the amount of memory
void * freed. */
size_t
buf_shrink_freelists(int free_all) buf_shrink_freelists(int free_all)
{ {
#ifdef ENABLE_BUF_FREELISTS #ifdef ENABLE_BUF_FREELISTS
int i; int i;
size_t total_freed = 0;
disable_control_logging(); disable_control_logging();
for (i = 0; freelists[i].alloc_size; ++i) { for (i = 0; freelists[i].alloc_size; ++i) {
int slack = freelists[i].slack; int slack = freelists[i].slack;
@ -313,6 +316,7 @@ buf_shrink_freelists(int free_all)
chunk_t *next = chunk->next; chunk_t *next = chunk->next;
tor_assert(total_bytes_allocated_in_chunks >= CHUNK_ALLOC_SIZE(chunk->memlen)); tor_assert(total_bytes_allocated_in_chunks >= CHUNK_ALLOC_SIZE(chunk->memlen));
total_bytes_allocated_in_chunks -= CHUNK_ALLOC_SIZE(chunk->memlen); total_bytes_allocated_in_chunks -= CHUNK_ALLOC_SIZE(chunk->memlen);
total_freed += CHUNK_ALLOC_SIZE(chunk->memlen);
tor_free(chunk); tor_free(chunk);
chunk = next; chunk = next;
--n_to_free; --n_to_free;
@ -330,18 +334,21 @@ buf_shrink_freelists(int free_all)
} }
// tor_assert(!n_to_free); // tor_assert(!n_to_free);
freelists[i].cur_length = new_length; freelists[i].cur_length = new_length;
tor_assert(orig_n_to_skip == new_length);
log_info(LD_MM, "Cleaned freelist for %d-byte chunks: original " log_info(LD_MM, "Cleaned freelist for %d-byte chunks: original "
"length %d, kept %d, dropped %d.", "length %d, kept %d, dropped %d. New length is %d",
(int)freelists[i].alloc_size, orig_length, (int)freelists[i].alloc_size, orig_length,
orig_n_to_skip, orig_n_to_free); orig_n_to_skip, orig_n_to_free, new_length);
} }
freelists[i].lowest_length = freelists[i].cur_length; freelists[i].lowest_length = freelists[i].cur_length;
assert_freelist_ok(&freelists[i]); assert_freelist_ok(&freelists[i]);
} }
done: done:
enable_control_logging(); enable_control_logging();
return total_freed;
#else #else
(void) free_all; (void) free_all;
return 0;
#endif #endif
} }
@ -579,6 +586,7 @@ static chunk_t *
chunk_copy(const chunk_t *in_chunk) chunk_copy(const chunk_t *in_chunk)
{ {
chunk_t *newch = tor_memdup(in_chunk, CHUNK_ALLOC_SIZE(in_chunk->memlen)); chunk_t *newch = tor_memdup(in_chunk, CHUNK_ALLOC_SIZE(in_chunk->memlen));
total_bytes_allocated_in_chunks += CHUNK_ALLOC_SIZE(in_chunk->memlen);
newch->next = NULL; newch->next = NULL;
if (in_chunk->data) { if (in_chunk->data) {
off_t offset = in_chunk->data - in_chunk->mem; off_t offset = in_chunk->data - in_chunk->mem;

View File

@ -18,7 +18,7 @@ void buf_free(buf_t *buf);
void buf_clear(buf_t *buf); void buf_clear(buf_t *buf);
buf_t *buf_copy(const buf_t *buf); buf_t *buf_copy(const buf_t *buf);
void buf_shrink(buf_t *buf); void buf_shrink(buf_t *buf);
void buf_shrink_freelists(int free_all); size_t buf_shrink_freelists(int free_all);
void buf_dump_freelist_sizes(int severity); void buf_dump_freelist_sizes(int severity);
size_t buf_datalen(const buf_t *buf); size_t buf_datalen(const buf_t *buf);

View File

@ -1674,6 +1674,17 @@ circuits_handle_oom(size_t current_allocation)
"over-long queues. (This behavior is controlled by " "over-long queues. (This behavior is controlled by "
"MaxMemInQueues.)"); "MaxMemInQueues.)");
{
const size_t recovered = buf_shrink_freelists(1);
if (recovered >= current_allocation) {
log_warn(LD_BUG, "We somehow recovered more memory from freelists "
"than we thought we had allocated");
current_allocation = 0;
} else {
current_allocation -= recovered;
}
}
{ {
size_t mem_target = (size_t)(get_options()->MaxMemInQueues * size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
FRACTION_OF_DATA_TO_RETAIN_ON_OOM); FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
@ -1717,6 +1728,8 @@ circuits_handle_oom(size_t current_allocation)
} SMARTLIST_FOREACH_END(circ); } SMARTLIST_FOREACH_END(circ);
clean_cell_pool(); /* In case this helps. */ clean_cell_pool(); /* In case this helps. */
buf_shrink_freelists(1); /* This is necessary to actually release buffer
chunks. */
log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits.", log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits.",
U64_PRINTF_ARG(mem_recovered), U64_PRINTF_ARG(mem_recovered),