Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ libflexmalloc_la_SOURCES = \
allocator.cxx allocator.hxx \
allocator-posix.cxx allocator-posix.hxx \
allocator-statistics.cxx allocator-statistics.hxx \
statistics-recorder-allocator.hxx \
cache-callstack.cxx cache-callstack.hxx \
flex-malloc.cxx flex-malloc.hxx \
malloc-interposer.cxx
Expand Down
23 changes: 7 additions & 16 deletions src/allocator-memkind-hbwmalloc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define ALLOCATOR_NAME "memkind/hbwmalloc"

AllocatorMemkindHBWMalloc::AllocatorMemkindHBWMalloc (allocation_functions_t &af)
: Allocator (af)
: StatisticsRecorderAllocator (af)
{
hbw_set_policy (HBW_POLICY_PREFERRED);
}
Expand All @@ -38,7 +38,7 @@ void * AllocatorMemkindHBWMalloc::malloc (size_t size)

// Verbosity and emit statistics
VERBOSE_MSG(3, ALLOCATOR_NAME": Allocated %lu bytes in %p (hdr & base at %p) w/ allocator %s (%p)\n", size, res, Allocator::getAllocatorHeader (res), name(), this);
_stats.record_malloc (size);
record_malloc (size);
}

return res;
Expand All @@ -59,7 +59,7 @@ void * AllocatorMemkindHBWMalloc::calloc (size_t nmemb, size_t size)

// Verbosity and emit statistics
VERBOSE_MSG(3, ALLOCATOR_NAME": Allocated %lu bytes in %p (hdr & base %p) w/ allocator %s (%p)\n", size, res, Allocator::getAllocatorHeader (res), name(), this);
_stats.record_calloc (nmemb * size);
record_calloc (nmemb * size);
}

return res;
Expand All @@ -82,7 +82,7 @@ int AllocatorMemkindHBWMalloc::posix_memalign (void **ptr, size_t align, size_t

// Verbosity and emit statistics
VERBOSE_MSG(3, ALLOCATOR_NAME": Allocated %lu bytes in %p (hdr %p, base %p) w/ allocator %s (%p)\n", size, res, Allocator::getAllocatorHeader (res), baseptr, name(), this);
_stats.record_aligned_malloc (size + align);
record_aligned_malloc (size + align);

*ptr = res;
return 0;
Expand All @@ -98,7 +98,7 @@ void AllocatorMemkindHBWMalloc::free (void *ptr)
// When freeing the memory, need to free the base pointe
VERBOSE_MSG(3, ALLOCATOR_NAME": Freeing up pointer %p (hdr %p) w/ size - %lu (base pointer located in %p)\n", ptr, hdr, hdr->size, hdr->base_ptr);

_stats.record_free (hdr->size);
record_free (hdr->size);
hbw_free (hdr->base_ptr);
}

Expand Down Expand Up @@ -129,7 +129,7 @@ void * AllocatorMemkindHBWMalloc::realloc (void *ptr, size_t size)
DBG("Reallocated (%ld->%ld [extra bytes = %lu]) from %p (base at %p, header at %p) into %p (base at %p, header at %p) w/ allocator %s (%p)\n", prev_size, size, extra_size, ptr, prev_baseptr, prev_hdr, res, new_baseptr, Allocator::getAllocatorHeader (res), name(), this);
}

_stats.record_realloc (size, prev_size);
record_realloc (size, prev_size);

return res;
}
Expand All @@ -142,7 +142,7 @@ void * AllocatorMemkindHBWMalloc::realloc (void *ptr, size_t size)
else
{
VERBOSE_MSG(3, ALLOCATOR_NAME": realloc (NULL, ...) forwarded to malloc\n");
_stats.record_realloc_forward_malloc();
record_realloc_forward_malloc();

return this->malloc (size);
}
Expand Down Expand Up @@ -218,12 +218,3 @@ const char * AllocatorMemkindHBWMalloc::description (void) const
return "Allocator based on hbwmalloc on top of memkind";
}

void AllocatorMemkindHBWMalloc::show_statistics (void) const
{
_stats.show_statistics (ALLOCATOR_NAME, true);
}

bool AllocatorMemkindHBWMalloc::fits (size_t s) const
{
return _stats.water_mark() + s <= this->size();
}
33 changes: 2 additions & 31 deletions src/allocator-memkind-hbwmalloc.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

#pragma once

#include "allocator.hxx"
#include "statistics-recorder-allocator.hxx"

class AllocatorMemkindHBWMalloc final : public Allocator
class AllocatorMemkindHBWMalloc final : public StatisticsRecorderAllocator
{
private:
AllocatorStatistics _stats;

public:
AllocatorMemkindHBWMalloc (allocation_functions_t &);
~AllocatorMemkindHBWMalloc();
Expand All @@ -25,30 +22,4 @@ class AllocatorMemkindHBWMalloc final : public Allocator
void configure (const char *);
const char * name (void) const;
const char * description (void) const;
void show_statistics (void) const;

void *memcpy (void *dest, const void *src, size_t n)
{ return ::memcpy (dest, src, n); }

bool fits (size_t s) const;
size_t hwm (void) const
{ return _stats.water_mark(); }
void record_unfitted_malloc (size_t s)
{ _stats.record_unfitted_malloc (s); } ;
void record_unfitted_calloc (size_t s)
{ _stats.record_unfitted_calloc (s); } ;
void record_unfitted_aligned_malloc (size_t s)
{ _stats.record_unfitted_aligned_malloc (s); } ;
void record_unfitted_realloc (size_t s)
{ _stats.record_unfitted_realloc (s); } ;

void record_source_realloc (size_t s)
{ _stats.record_source_realloc (s); };
void record_target_realloc (size_t s)
{ _stats.record_target_realloc (s); };
void record_self_realloc (size_t s)
{ _stats.record_self_realloc (s); };

void record_realloc_forward_malloc (void)
{ _stats.record_realloc_forward_malloc (); }
};
112 changes: 64 additions & 48 deletions src/allocator-memkind-pmem.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@ AllocatorMemkindPMEM::AllocatorMemkindPMEM (allocation_functions_t &af)

AllocatorMemkindPMEM::~AllocatorMemkindPMEM ()
{
// Explicitely call the deallocator as the new was done on pre-allocated memory
for (int i = 0; _num_NUMA_nodes > i; ++i)
_stats[i].~AllocatorStatistics();
_af.free (_stats);
_af.free (_kind);
_af.free (_cpu_2_NUMA);
}

void * AllocatorMemkindPMEM::malloc (size_t size)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
assert (0 <= n && n < _num_NUMA_nodes);
const short n = get_local_numa ();

DBG("Running on CPU %d - NUMA node %ld\n", cpu, n);
DBG("Running on CPU %d - NUMA node %hd\n", get_current_cpu (), n);

// Forward memory request to real malloc and reserve some space for the header
void * baseptr = memkind_malloc (_kind[n], Allocator::getTotalSize (size));
Expand All @@ -73,19 +74,17 @@ void * AllocatorMemkindPMEM::malloc (size_t size)

// Verbosity and emit statistics
VERBOSE_MSG(3, ALLOCATOR_NAME": Allocated %lu bytes in %p (hdr & base at %p) w/ allocator %s (%p)\n", size, res, Allocator::getAllocatorHeader (res), name(), this);
_stats[n].record_malloc (size);
record_malloc (size, n);
}

return res;
}

void * AllocatorMemkindPMEM::calloc (size_t nmemb, size_t size)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
assert (0 <= n && n < _num_NUMA_nodes);
const short n = get_local_numa ();

DBG("Running on CPU %d - NUMA node %ld\n", cpu, n);
DBG("Running on CPU %d - NUMA node %hd\n", get_current_cpu (), n);

// Forward memory request to real malloc and request additional space to store
// the allocator and the basepointer
Expand All @@ -101,7 +100,7 @@ void * AllocatorMemkindPMEM::calloc (size_t nmemb, size_t size)

// Verbosity and emit statistics
VERBOSE_MSG(3, ALLOCATOR_NAME": Allocated %lu bytes in %p (hdr & base %p) w/ allocator %s (%p)\n", size, res, Allocator::getAllocatorHeader (res), name(), this);
_stats[n].record_calloc (nmemb * size);
record_calloc (nmemb * size, n);
}

return res;
Expand All @@ -111,11 +110,9 @@ int AllocatorMemkindPMEM::posix_memalign (void **ptr, size_t align, size_t size)
{
assert (ptr != nullptr);

int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
assert (0 <= n && n < _num_NUMA_nodes);
const short n = get_local_numa ();

DBG("Running on CPU %d - NUMA node %ld\n", cpu, n);
DBG("Running on CPU %d - NUMA node %hd\n", get_current_cpu (), n);

// Forward memory request to real malloc and request additional space to
// store the allocator and the basepointer
Expand All @@ -131,7 +128,7 @@ int AllocatorMemkindPMEM::posix_memalign (void **ptr, size_t align, size_t size)

// Verbosity and emit statistics
VERBOSE_MSG(3, ALLOCATOR_NAME": Allocated %lu bytes in %p (hdr %p, base %p) w/ allocator %s (%p)\n", size, res, Allocator::getAllocatorHeader (res), baseptr, name(), this);
_stats[n].record_aligned_malloc (size + align);
record_aligned_malloc (size + align, n);

*ptr = res;
return 0;
Expand All @@ -153,7 +150,7 @@ void AllocatorMemkindPMEM::free (void *ptr)
assert (gotnode);
assert (0 <= n && n < _num_NUMA_nodes);

_stats[n].record_free (hdr->size);
record_free (hdr->size, n);
memkind_free (_kind[n], hdr->base_ptr);
}

Expand Down Expand Up @@ -190,7 +187,7 @@ void * AllocatorMemkindPMEM::realloc (void *ptr, size_t size)
DBG("Reallocated (%ld->%ld [extra bytes = %lu]) from %p (base at %p, header at %p) into %p (base at %p, header at %p) w/ allocator %s (%p) on node %d\n", prev_size, size, extra_size, ptr, prev_baseptr, prev_hdr, res, new_baseptr, Allocator::getAllocatorHeader (res), name(), this, n);
}

_stats[n].record_realloc (size, prev_size);
record_realloc (size, prev_size, n);

return res;
}
Expand All @@ -203,9 +200,7 @@ void * AllocatorMemkindPMEM::realloc (void *ptr, size_t size)
else
{
VERBOSE_MSG(3, ALLOCATOR_NAME": realloc (NULL, ...) forwarded to malloc\n");
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_realloc_forward_malloc();
record_realloc_forward_malloc();

return this->malloc (size);
}
Expand Down Expand Up @@ -341,71 +336,92 @@ void AllocatorMemkindPMEM::show_statistics (void) const
}
}

bool AllocatorMemkindPMEM::fits (size_t) const
int AllocatorMemkindPMEM::get_current_cpu (void) const
{
return sched_getcpu ();
}

short AllocatorMemkindPMEM::get_local_numa (void) const
{
int cpu = get_current_cpu ();
short n = _cpu_2_NUMA[cpu];
assert (0 <= n && n < _num_NUMA_nodes);
return n;
}

bool AllocatorMemkindPMEM::fits (size_t,short) const
{
return true;
}

size_t AllocatorMemkindPMEM::hwm (void) const
size_t AllocatorMemkindPMEM::hwm (short n) const
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
return _stats[n].water_mark ();
}

void AllocatorMemkindPMEM::record_unfitted_malloc (size_t s)
void AllocatorMemkindPMEM::record_malloc (size_t s, short n)
{
_stats[n].record_malloc (s);
}

void AllocatorMemkindPMEM::record_calloc (size_t s, short n)
{
_stats[n].record_calloc (s);
}

void AllocatorMemkindPMEM::record_aligned_malloc (size_t s, short n)
{
_stats[n].record_aligned_malloc (s);
}

void AllocatorMemkindPMEM::record_realloc (size_t size, size_t prev_size, short n)
{
_stats[n].record_realloc (size, prev_size);
}

void AllocatorMemkindPMEM::record_free (size_t s, short n)
{
_stats[n].record_free (s);
}


void AllocatorMemkindPMEM::record_unfitted_malloc (size_t s, short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_unfitted_malloc (s);
}

void AllocatorMemkindPMEM::record_unfitted_calloc (size_t s)
void AllocatorMemkindPMEM::record_unfitted_calloc (size_t s, short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_unfitted_calloc (s);
}

void AllocatorMemkindPMEM::record_unfitted_aligned_malloc (size_t s)
void AllocatorMemkindPMEM::record_unfitted_aligned_malloc (size_t s, short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_unfitted_aligned_malloc (s);
}

void AllocatorMemkindPMEM::record_unfitted_realloc (size_t s)
void AllocatorMemkindPMEM::record_unfitted_realloc (size_t s, short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_unfitted_realloc (s);
}

void AllocatorMemkindPMEM::record_source_realloc (size_t s)
void AllocatorMemkindPMEM::record_source_realloc (size_t s, short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_source_realloc (s);
}

void AllocatorMemkindPMEM::record_target_realloc (size_t s)
void AllocatorMemkindPMEM::record_target_realloc (size_t s, short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_target_realloc (s);
}

void AllocatorMemkindPMEM::record_self_realloc (size_t s)
void AllocatorMemkindPMEM::record_self_realloc (size_t s, short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_self_realloc (s);
}

void AllocatorMemkindPMEM::record_realloc_forward_malloc (void)
void AllocatorMemkindPMEM::record_realloc_forward_malloc (short n)
{
int cpu = sched_getcpu();
long n = _cpu_2_NUMA[cpu];
_stats[n].record_realloc_forward_malloc ();
}

Loading