Skip to content

Commit

Permalink
Handle small memory allocations ( < 16 bytes) more space efficiently
Browse files Browse the repository at this point in the history
This patch adjusts std_malloc() to handle small
allocations (smaller than 16-bytes) more efficiently.
More specifically instead of allocating whole page
for each such request, adjusted std_malloc() uses memory
pool adequate to requested size and thus saves almost
entire page (4K) of memory.

Fixes #1011

Signed-off-by: Waldemar Kozaczuk <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
wkozaczuk authored and nyh committed Nov 18, 2018
1 parent b0df7a7 commit a58f446
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 3 additions & 2 deletions core/mempool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1546,8 +1546,9 @@ static inline void* std_malloc(size_t size, size_t alignment)
if ((ssize_t)size < 0)
return libc_error_ptr<void *>(ENOMEM);
void *ret;
if (size <= memory::pool::max_object_size && alignment <= size && smp_allocator) {
size = std::max(size, memory::pool::min_object_size);
size_t minimum_size = std::max(size, memory::pool::min_object_size);
if (size <= memory::pool::max_object_size && alignment <= minimum_size && smp_allocator) {
size = minimum_size;
unsigned n = ilog2_roundup(size);
ret = memory::malloc_pools[n].alloc();
ret = translate_mem_area(mmu::mem_area::main, mmu::mem_area::mempool,
Expand Down
2 changes: 1 addition & 1 deletion modules/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ tests := tst-pthread.so misc-ramdisk.so tst-vblk.so tst-bsd-evh.so \
tst-ifaddrs.so tst-pthread-affinity-inherit.so tst-sem-timed-wait.so \
tst-ttyname.so tst-pthread-barrier.so tst-feexcept.so tst-math.so \
tst-sigaltstack.so tst-fread.so tst-tcp-cork.so tst-tcp-v6.so \
tst-calloc.so tst-crypt.so tst-non-fpic.so
tst-calloc.so tst-crypt.so tst-non-fpic.so tst-small-malloc.so

# libstatic-thread-variable.so tst-static-thread-variable.so \
Expand Down
45 changes: 45 additions & 0 deletions tests/tst-small-malloc.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2018 Waldemar Kozaczuk
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <cassert>
#include <osv/trace-count.hh>

int main() {
tracepoint_counter *memory_malloc_mempool_counter = nullptr,
*memory_malloc_page_counter = nullptr;

for (auto & tp : tracepoint_base::tp_list) {
if ("memory_malloc_mempool" == std::string(tp.name)) {
memory_malloc_mempool_counter = new tracepoint_counter(tp);
}
if ("memory_malloc_page" == std::string(tp.name)) {
memory_malloc_page_counter = new tracepoint_counter(tp);
}
}
assert(memory_malloc_mempool_counter != nullptr);
assert(memory_malloc_page_counter != nullptr);

auto memory_malloc_mempool_counter_now = memory_malloc_mempool_counter->read();
auto memory_malloc_page_counter_now = memory_malloc_page_counter->read();

const int allocation_count = 1024;
for( int i = 0; i < allocation_count; i++) {
void *addr = malloc(7);
assert(addr);
free(addr);

addr = malloc(17);
assert(addr);
free(addr);
}

// Verify all allocations above were handled by malloc_pool
assert(memory_malloc_mempool_counter->read() - memory_malloc_mempool_counter_now >= 2 * allocation_count);
// Verify that NO allocations were handled by alloc_page
assert(memory_malloc_page_counter->read() - memory_malloc_page_counter_now == 0);
}

0 comments on commit a58f446

Please sign in to comment.