4 #include "sys/malloc.h" 6 #include "sys/sysctl.h" 9 #include "sys/resource.h" 18 #include <spi/kernel_interface.h> 19 #include <common/bgp_personality.h> 20 #include <common/bgp_personality_inlines.h> 23 #include <spi/include/kernel/memory.h> 27 #include "../interface/common.h" 73 #define MAX_THREADS 256 104 #define CPY_BUFFER_SIZE 1000 126 std::list<mem_transfer> transfers;
128 if (mst_buffer_ptr > .80*mst_buffer_size &&
129 mst_buffer_used < .40*mst_buffer_size){
131 std::list<mem_loc> old_mst =
mst;
137 std::list<mem_loc>::iterator
it;
138 for (it=old_mst.begin(); it!=old_mst.end(); it++){
151 transfers.push_back(t);
153 printf(
"Contracted MST\n");
172 void * new_mst_buffer;
173 if (size > mst_buffer_size){
174 pm = posix_memalign((
void**)&new_mst_buffer,
ALIGN_BYTES, size);
176 if (mst_buffer != NULL){
177 memcpy(new_mst_buffer, mst_buffer, mst_buffer_ptr);
179 mst_buffer = new_mst_buffer;
180 mst_buffer_size = size;
189 if (instance_counter == 1){
191 max_threads = omp_get_max_threads();
211 if (instance_counter == 0){
213 if (mem_used[i] > 0){
215 printf(
"Warning: memory leak in CTF on thread %d, %ld bytes of memory in use at termination",
217 printf(
" in %zu unfreed items\n",
218 mem_stacks[i].size());
222 printf(
"Warning: %zu items not deallocated from custom stack, consuming %ld bytes of memory\n",
235 ASSERT((int64_t)((
char*)ptr-(
char*)mst_buffer)<mst_buffer_size);
237 std::list<mem_loc>::iterator
it;
238 for (it=--mst.end(); it!=mst.begin(); it--){
240 mst_buffer_used = mst_buffer_used - it->len;
245 if (it == mst.begin()){
247 mst_buffer_used = mst_buffer_used - it->len;
250 printf(
"CTF CTF_int::ERROR: Invalid mst free of pointer %p\n", ptr);
257 mst_buffer_ptr = (int64_t)((
char*)mst.back().ptr - (
char*)mst_buffer)+mst.back().len;
273 if (mst_buffer_size == 0)
285 if (mst_buffer_ptr + plen < mst_buffer_size){
290 mst_buffer_ptr = mst_buffer_ptr+plen;
291 mst_buffer_used += plen;
293 DPRINTF(2,
"Exceeded mst buffer size (" PRId64
"), current ptr is " PRId64
", composed of %d items of size " PRId64
"\n",
321 int64_t len =
MAX(4,len_);
330 int pm = posix_memalign(ptr, (int64_t)
ALIGN_BYTES, len);
336 if (max_threads == 1) tid = 0;
337 else tid = omp_get_thread_num();
342 mem_used[tid] += len;
343 std::list<mem_loc> * mem_stack;
344 mem_stack = &mem_stacks[tid];
347 mem_stack->push_back(m);
352 printf(
"CTF CTF_int::ERROR: posix memalign returned an error, " PRId64
" memory alloced on this process, wanted to alloc " PRId64
" more\n",
377 #if 0 //ndef PRODUCTION 380 std::list<mem_loc> * mem_stack;
382 mem_stack = &mem_stacks[0];
384 std::list<mem_loc>::reverse_iterator
it;
386 for (it=mem_stack->rbegin(); it!=mem_stack->rend(); it++){
387 if ((*it).ptr == ptr){
389 mem_stack->erase((++it).base());
395 printf(
"CTF CTF_int::ERROR: failed memory untag\n");
413 if ((int64_t)((
char*)ptr-(
char*)mst_buffer) < mst_buffer_size &&
414 (int64_t)((
char*)ptr-(
char*)mst_buffer) >= 0){
419 std::list<mem_loc> * mem_stack;
421 mem_stack = &mem_stacks[tid];
423 std::list<mem_loc>::reverse_iterator
it;
425 for (it=mem_stack->rbegin(); it!=mem_stack->rend(); it++){
426 if ((*it).ptr == ptr){
428 mem_stack->erase((++it).base());
436 mem_used[tid] -= len;
454 if (max_threads == 1) tid = 0;
455 else tid = omp_get_thread_num();
486 if ((int64_t)((
char*)ptr-(
char*)mst_buffer) < mst_buffer_size &&
487 (int64_t)((
char*)ptr-(
char*)mst_buffer) >= 0){
496 if (max_threads == 1) tid = 0;
497 else tid = omp_get_thread_num();
505 if (tid != 0 || max_threads == 1){
506 printf(
"CTF CTF_int::ERROR: Invalid free of pointer %p by thread %d\n", ptr, tid);
518 printf(
"CTF CTF_int::ERROR: Invalid free of pointer %p by zeroth thread\n", ptr);
609 Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &total);
611 return MIN(total,uint64_t(mem_size));
619 _BGP_Personality_t personality;
621 Kernel_GetPersonality(&personality,
sizeof(personality));
622 total = (int64_t)BGP_Personality_DDRSizeMB(&personality);
624 node_config = BGP_Personality_processConfig(&personality);
625 if (node_config == _BGP_PERS_PROCESSCONFIG_VNM) total /= 4;
626 else if (node_config == _BGP_PERS_PROCESSCONFIG_2x2) total /= 2;
631 if (tot_mem_available == -1){
633 int mib[] = {CTL_HW,HW_MEMSIZE};
636 sysctl(mib, 2, &mem, &len, NULL, 0);
637 tot_mem_available = mem;
639 int64_t pages = (int64_t)sysconf(_SC_PHYS_PAGES);
640 int64_t page_size = (int64_t)sysconf(_SC_PAGE_SIZE);
642 tot_mem_available =
MIN((int64_t)(pages * page_size), (int64_t)mem_size);
644 tot_mem_available = pages * page_size;
658 Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAPAVAIL, &mem_avail);
669 if (pused > memcap*ptotal){ printf(
"CTF ERROR: less than %lf percent of local memory remaining, ensuing segfault likely.\n", (100.*(1.-memcap))); }
670 return memcap*ptotal-pused;
void inc_tot_mem_used(int64_t a)
int64_t tot_mem_available
std::list< mem_transfer > contract_mst()
gets rid of empty space on the stack
void mst_create(int64_t size)
initializes stack buffer
int mst_free(void *ptr)
frees buffer allocated on stack
void * mst_alloc(int64_t len)
mst_alloc allocates buffer on the specialized memory stack
void * alloc(int64_t len)
alloc abstraction
char * cpy_buffer[CPY_BUFFER_SIZE]
void mem_exit(int rank)
exit instance of memory manager
int untag_mem(void *ptr)
stops tracking memory allocated by CTF, so user doesn't have to call free
int cdealloc(void *ptr, int const tid)
free abstraction
int mst_alloc_ptr(int64_t len, void **const ptr)
mst_alloc abstraction
int alloc_ptr(int64_t len, void **const ptr)
alloc abstraction
int64_t proc_bytes_used()
gives total memory used on this MPI process
void set_memcap(double cap)
sets what fraction of the memory capacity CTF can use
void mem_create()
create instance of memory manager
int64_t proc_bytes_available()
gives total memory available on this MPI process
int64_t proc_bytes_total()
gives total memory size per MPI process
int64_t mem_used[MAX_THREADS]
std::list< mem_loc > * get_mst()
void set_mem_size(int64_t size)
sets what fraction of the memory capacity CTF can use
std::list< mem_loc > mem_stacks[MAX_THREADS]
int cdealloc_cond(void *ptr)
free abstraction (conditional (no error if not found))