Commit Graph

43 Commits

Author SHA1 Message Date
Avi Kivity
f256243ebd memory: fix debug build break (statistics) 2015-07-15 18:17:49 +03:00
Avi Kivity
a597d37bc1 memory: add statistics for reclaim operations and free memory 2015-07-15 18:08:54 +03:00
Avi Kivity
23d874f786 doc: low-level memory management 2015-06-01 11:35:37 +03:00
Asias He
88e7dcfa86 Remove redundant const in static constexpr const
From http://en.cppreference.com/w/cpp/language/constexpr:

   A constexpr specifier used in an object declaration implies const.

However, We can not change from
   static constexpr const char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";
to
   static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";

The compiler complains:

   In file included from json/formatter.cc:22:0:
   json/formatter.hh:132:42: error: deprecated conversion from string
   constant to ‘char*’ [-Werror=write-strings]
        static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";

Since, unlike const, constexpr does not modify a type. It just applies
to an object (or function), and incidentally implies const to the
top-level type.
2015-05-25 11:57:19 +03:00
Paweł Dziepak
5ffd057fd1 memory: use allocate_large_aligned() if alignment is more than a page
small_pools, which are responsible for allocations up to 16kB, aren't able
to provide a buffer with alignment stricter than a page. This results
in aligned allocations being broken for buffers in the range 4kB - 16kB.
This patch make sure that if the alignment requirement is too big for
small_pool to handle allocate_large_aligned() is used instead.

Fixes #36.

Signed-off-by: Paweł Dziepak <pdziepak@quarnos.org>
2015-04-20 10:32:43 +03:00
Paweł Dziepak
d3504f90a9 memory: fix trimming of allocated spans
Trimmers may request buffers smaller than n_pages, the original value
passed to allocate_large_and_trim(). That's why t.nr_page should be
used as a final size of the allocated span.

Another issue with the handling of span trimming is the order of operations
when pages from the beginning of the buffer are trimmed. In such case
span is updated to point to the actual beginning of the requested buffer,
but afterwards it is used to retrieve span->span_size value which is expected
to be the size of the originally allocated page span. Because the span
pointer was changed the value is invalid and the trimming doesn't free
all trimmed pages.

Signed-off-by: Paweł Dziepak <pdziepak@quarnos.org>
2015-03-30 14:21:36 +03:00
Avi Kivity
0d7b878539 memory: sprinkle magic attributes to allow building with -flto 2015-02-24 15:10:33 +02:00
Avi Kivity
7f8d88371a Add LICENSE, NOTICE, and copyright headers to all source files.
The two files imported from the OSv project retain their original licenses.
2015-02-19 16:52:34 +02:00
Avi Kivity
84234b5b9a memory: implement the C11 aligned_alloc() function 2015-02-18 19:48:18 +02:00
Vlad Zolotarov
18f35236db memory: Move page_size, page_bits and huge page size definitions to header
They are going to be used in more places (not just in memory.cc).

Signed-off-by: Vlad Zolotarov <vladz@cloudius-systems.com>
2015-02-11 19:27:12 +02:00
Vlad Zolotarov
85b62d8132 memory: hugetlbfs mapping may not be invalid
Turn a condition into an assert() since if a mapping is invalid this may
only mean that we have a bug.

Signed-off-by: Vlad Zolotarov <vladz@cloudius-systems.com>
2015-01-25 13:22:11 +02:00
Avi Kivity
a8054e5aae memory: init virt to phys mapping after binding memory to node
Binding may cause memory to move, so initialize the page map after it is done.
2015-01-22 12:38:22 +02:00
Avi Kivity
285d4af077 memory: adjust pagemap parsing
The pfn is in bits 0:54 inclusive, we missed the high bit.

Should have no effect in systems with less than a few exabytes of memory.
2015-01-21 19:06:30 +02:00
Avi Kivity
45f9c86943 memory: provide C++ facilities for aligned allocation
new (with_alignment(64)) foo(...)   // cache aligned foo
  std::vector<foo, aligned_allocator<foo, 64>> // cached aligned foo vector
2015-01-19 09:49:32 +02:00
Avi Kivity
b22186e623 memory: fix small aligned allocations
When size > align, we simply call the small allocator with the provided size,
but that does not guarantee any alignment above the default.

Round the allocation size to the nearest power of two in that case, to
guarantee alignment.
2015-01-19 09:49:32 +02:00
Avi Kivity
80bcf3f973 memory: provide virt-to-phys translation API
Convert virtual addresses to physical addresses.  Only works when
--hugepages is in effect.
2015-01-15 18:23:19 +02:00
Avi Kivity
e57be410b5 memory: support cross-cpu freeing
Currently we require that memory be freed on the same cpu it was allocated.
This does not impose difficulties on the user code, since our code is already
smp-unsafe, and so must use message-passing to run the destructor on the
origin cpu, so memory is naturally freed there as well.

However, library code does not run under our assumptions, specifically
std::exception_ptr, which we do transport across cores.

To support this use case, add low-performance support for cross-cpu frees,
using an atomic singly linked list per core.
2015-01-15 15:19:00 +02:00
Avi Kivity
87f63f7b90 shared_ptr: rename to lw_shared_ptr (for light-weight)
The current shared_ptr implementation is efficient, but does not support
polymorphic types.

Rename it in order to make room for a polymorphic shared_ptr.
2015-01-04 22:38:49 +02:00
Avi Kivity
4453fd1d6a memory: add support for allocating memory via hugetlbfs
This is a little tricky, since we only know we want hugetlbfs after memory
has been initialized, so we start up in anonymous memory, and later
switch to hugetlbfs by copying it to hugetlb-backed memory and mremap()ing
it back into place.
2014-12-11 12:25:31 +02:00
Avi Kivity
ca2c7d8767 memory: abstract mmap() call
To support hugepages, we will need a different mmap() call, so abstract
it out.
2014-12-11 12:25:31 +02:00
Avi Kivity
0043c1a994 memory: drop duplicate madvise() call 2014-12-11 12:25:31 +02:00
Avi Kivity
be0ae4f5dc memory: Un-hide standard allocator functions
With -fvisibility=hidden, all executable symbols are hidden from shared
objects, allowing more optimizations (especially with -flto).  However, hiding
the allocator symbols mean that memory allocated in the executable cannot
be freed in a library, since they will use different allocators.

Fix by exposing these symbols with default visibility.

Fixes crash loading some dpdk libraries.
2014-12-01 14:49:04 +02:00
Avi Kivity
9ab5dce5c4 memory: fix throw specifiers on sized delete
Noticed by clang.
2014-11-26 14:56:40 +02:00
Avi Kivity
f32a8be723 memory: make statistics thread local
Noticed by Gleb.
2014-11-24 11:50:07 +02:00
Avi Kivity
5cd200831b memory: add allocation statistics collection 2014-11-23 19:28:07 +02:00
Avi Kivity
1a7fd983ac memory: fix buffer overrun
We store spans in freelist i if the span's size >= 2^i.  However, when
picking a span to satisfy an allocation, we must use the next larger list
if the size is not a power of two, so that we can be sure that all spans on
that list can satisfy that request.

The current code doesn't do that, so it under-allocates, leading to memory
corruption.
2014-11-15 11:52:39 -08:00
Avi Kivity
1f80f7ee14 memory: fix realloc()
Because memcpy() is declared by gcc as receiving non-null attributes, gcc
assumes that ptr != null, as it is passed into memcpy() (though with a size
of zero).  As a result it ignores the null pointer check in ::free(), and
calls memory::free() directly, which does not expect a null pointer.

Fix by only calling memcpy() when the ptr is non-null.
2014-10-23 15:59:52 +03:00
Tomasz Grabiec
1a84e3560c core: implement "reclaimer" instance methods when running with default allocator 2014-10-21 16:43:09 +02:00
Gleb Natapov
a1533cc671 memory: call MADV_DONTDUMP on the whole mmaped memory
We mmap 2*alloc bytes not alloc.
2014-10-21 12:20:22 +03:00
Avi Kivity
b90670417b memory: fix off-by-one in to-resize
cpu_pages::initialize() established the one-past-the-end page as a sentinel
to avoid boundary conditions checks.  cpu_pages::do_resize() considers the
last page as the sentinel.  This discrepancy causes the last page to be
considered free by do_resize, which promptly ends up as a use-after-free
page.

Fix by aligning do_resize() with initialize().
2014-10-15 16:44:36 +03:00
Avi Kivity
2d13f95a9f memory: support for non-hwloc builds 2014-10-13 15:41:18 +03:00
Avi Kivity
60b4756a36 smp: allow tuning memory size 2014-10-13 13:03:48 +03:00
Avi Kivity
56598903f6 memory: add support for reconfiguring memory
- add memory to a cpu
- bind that memory to a NUMA node
2014-10-13 13:03:46 +03:00
Avi Kivity
d89e09c9ed memory: switch page_list to use page indices instead of pointers
Two motivations:
1. reduce the size of struct page
2. allow relocating the page array; needed for dynamically changing
   the amount of memory supported.
2014-10-12 14:47:11 +03:00
Avi Kivity
37ef3bc899 memory: request huge page backing for heap
Worth about 12% on httpd.
2014-10-07 15:23:30 +03:00
Avi Kivity
6746bcfd68 memory: reclaim support
Allow memory users to declare methods of reclaiming memory (reclaimers),
and allow the main loop to declare a safe point for calling these reclaimers.

The memory mananger will then schedule calls to reclaimers when memory runs
low.
2014-10-07 15:14:44 +03:00
Nadav Har'El
5db2c1b506 build: Compile-time option to use default memory allocator
Add a compile-time option, DEFAULT_ALLOCATOR, to use the existing
memory allocator (malloc() and friends) instead of redefining it.

This option is a workaround needed to run Seastar on OSv.

Without this workaround, what seems to happen is that some code compiled
into the kernel (notably, libboost_program_options.a) uses the standard
malloc(), while inline code compiled into Seastar uses the seastar free()
to try and free that memory, resulting in a spectacular crash.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2014-10-06 14:59:36 +03:00
Avi Kivity
2b12c13055 memory: support sized deallocation
With N3778, the compiler can provide us with the size of the object,
so we can avoid looking it up in the page array.  Unfortunately only
implemented in clang at the moment.
2014-10-05 20:24:00 +03:00
Avi Kivity
9cd0e4f405 memory: improve allocation density
Instead of rounding up to a power-of-two, have four equally spaced
regions between powers of two.  For example:

   1024
   1280 (+256)
   1536 (+256)
   1792 (+256)
   2048 (+256)
   2560 (+512)
   3072 (+512)
   3584 (+512)
   4096 (+512)
2014-10-05 19:58:12 +03:00
Avi Kivity
e42158aea6 memory: small allocation support
Allocate small objects within spans, minimizing waste.

Each object size class has its own pool, and its own freelist.  On overflow
free objects are pushed into the spans; if a span is completely free, it is
returned to the main free list.
2014-10-05 19:58:10 +03:00
Avi Kivity
c407f5e091 memory: hook standard library allocation functions 2014-10-05 19:57:46 +03:00
Avi Kivity
b849b6b4cd memory: add support for aligned large allocations 2014-10-05 19:57:46 +03:00
Avi Kivity
86c0b7b7c9 memory: add per-cpu memory allocator
Largely inspired by tcmalloc, but simpler.  No support for small allocations
at the moment.
2014-10-05 19:57:40 +03:00