TEST(UnitTestMallocUsed, Malloc_100_1M) { #if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR static const size_t bytes_to_allocate = 1024*1024; size_t start = malloc_used(); char *x[100]; size_t used = 0; for (size_t i = 0; i < 100; ++i) x[i] = static_cast<char *>(malloc(bytes_to_allocate)); used += malloc_used(); for (size_t i = 0; i < 100; ++i) free(x[i]); size_t end = malloc_used(); EXPECT_LE(bytes_to_allocate*100, used - start); EXPECT_LE(start, end); std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; #endif }
TEST(UnitTestMallocUsed, Malloc_1_1024) { #if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR static const size_t bytes_to_allocate = 1024; size_t start = malloc_used(); size_t used = 0; char *x = static_cast<char *>(malloc(bytes_to_allocate)); used += malloc_used(); free(x); size_t end = malloc_used(); EXPECT_LE(bytes_to_allocate, used - start); EXPECT_LE(start, end); std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; #endif }
void get_memory_usage() { #if DO_MEMORY_ACCOUNTING #if !defined(SIERRA_PTMALLOC3_ALLOCATOR) && !defined(SIERRA_PTMALLOC2_ALLOCATOR) std::cout << "WARNING: ptmalloc2|3 not compiled in so malloc_used info unavailable. Recompile with e.g. 'bake allocator=ptmalloc2 (or 3)'. Printing zeros..." << std::endl; #else #endif m_malloc_used = malloc_used(); m_malloc_footprint = malloc_footprint(); m_malloc_max_footprint = malloc_max_footprint(); #else m_malloc_used = 0; m_malloc_footprint = 0; m_malloc_max_footprint = 0; #endif }
// Similar to Platform.cpp's get_heap_info, but no 'largest_free' and no log output. void get_heap_used(size_t &heap_size) { heap_size = 0; #if defined(SIERRA_HEAP_INFO) # if defined(SIERRA_PTMALLOC3_ALLOCATOR) || defined(SIERRA_PTMALLOC2_ALLOCATOR) heap_size = malloc_used(); # elif defined(__linux__) && ! defined(__IBMCPP__) static struct mallinfo minfo; minfo = mallinfo(); heap_size = static_cast<unsigned int>(minfo.uordblks) + static_cast<unsigned int>(minfo.hblkhd); # endif #endif // defined(SIERRA_HEAP_INFO) }
void malloc_statistics() { is_debug = false; printf("Caller Count Size\n"); MapIterator iter; map_iterator_init(&iter, statistics); while(map_iterator_has_next(&iter)) { MapEntry* entry = map_iterator_next(&iter); Stat* s = entry->data; if(s->count >= 2 || s->size > 2048) printf("%p %16ld %16ld\n", entry->key, s->count, s->size); } printf("usage: %ld/%ld\n", malloc_used(), malloc_total()); printf("tracing: class: %ld total: %ld\n", map_size(statistics), map_size(tracing)); printf("malloc/free count: %d - %d = %d\n", debug_malloc_count, debug_free_count, debug_malloc_count - debug_free_count); debug_malloc_count = debug_free_count = 0; is_debug = true; }
void get_heap_info( size_t & heap_size, size_t & largest_free) { heap_size = 0; largest_free = 0; #if defined(SIERRA_HEAP_INFO) # if defined(SIERRA_PTMALLOC3_ALLOCATOR) || defined(SIERRA_PTMALLOC2_ALLOCATOR) heap_size = malloc_used(); # elif 0 // if defined(REDS) // Redstorm now links in gnu's malloc static size_t reds_fragments; static unsigned long reds_total_free; static unsigned long reds_heap_size; static unsigned long reds_largest_free; ::heap_info(&reds_fragments, &reds_total_free, &reds_largest_free, &reds_heap_size); heap_size = reds_heap_size; largest_free = reds_largest_free; slibout.m(Slib::LOG_MEMORY) <<"reds_fragments " << reds_fragments << ", reds_total_free " << reds_total_free << ", reds_largest_free " << reds_largest_free << ", reds_heap_size " << reds_heap_size << Diag::dendl; # elif ( defined(__linux__) || defined(REDS) ) && ! defined(__IBMCPP__) static struct mallinfo minfo; minfo = mallinfo(); heap_size = static_cast<unsigned int>(minfo.uordblks) + static_cast<unsigned int>(minfo.hblkhd); largest_free = static_cast<unsigned int>(minfo.fordblks); slibout.m(Slib::LOG_MEMORY) << "size_t size " << sizeof(size_t)*8 << " bits" << ", heap size " << heap_size << ", arena " << static_cast<unsigned int>(minfo.arena) << ", ordblks " << minfo.ordblks << ", smblks " << minfo.smblks << ", hblks " << minfo.hblks << ", hblkhd " << static_cast<unsigned int>(minfo.hblkhd) << ", usmblks " << minfo.usmblks << ", fsmblks " << minfo.fsmblks << ", uordblks " << static_cast<unsigned int>(minfo.uordblks) << ", fordblks " << static_cast<unsigned int>(minfo.fordblks) << ", keepcost " << minfo.keepcost << Diag::dendl; # elif defined(__sun) pstatus_t proc_status; std::ifstream proc("/proc/self/status", std::ios_base::in|std::ios_base::binary); if (proc) { proc.read(reinterpret_cast<char *>(&proc_status), sizeof(proc_status)); heap_size = proc_status.pr_brksize; slibout.m(Slib::LOG_MEMORY) <<"pr_brksize " << proc_status.pr_brksize << ", pr_stksize " << proc_status.pr_stksize << Diag::dendl; } # endif #endif // defined(SIERRA_HEAP_INFO) }
TEST(UnitTestMalloc, DISABLED_Performance) { void *pointers[MAXP]; int size[MAXP]; int i = 0, r = 0, loop = 0, pass = 0, subpass = 0; size_t absmax=0, curmax=0; int realloc_mask = -1; size_t allocations = 0; size_t allocations_size = 0; size_t frees = 0; memset(pointers, 0, MAXP*sizeof(pointers[0])); double start_time = stk::cpu_time(); #if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR free(malloc(1)); ptrdiff_t start_mem = malloc_used(); ptrdiff_t start_footprint = malloc_footprint(); #else ptrdiff_t start_mem = reinterpret_cast<ptrdiff_t>(sbrk(0)); #endif #if defined SIERRA_PTMALLOC3_ALLOCATOR std::cout << "Modified ptmalloc3 allocator: "; #elif defined SIERRA_PTMALLOC2_ALLOCATOR std::cout << "Modified ptmalloc2 allocator: "; #else std::cout << "Default allocator: "; #endif #ifndef NDEBUG std::cout << "(debug)" << std::endl; #endif std::cout << std::endl; #if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR std::cout << "Start used " << start_mem << std::endl; std::cout << "Start footprint " << start_footprint << std::endl; #endif std::cout << std::endl << std::setw(14) << "elapsed" << " " #if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR << std::setw(14) << "footprint" << " " << std::setw(14) << "max_footprint" << " " #endif << std::setw(14) << "used " << " " << std::setw(14) << "curmax" << " " << std::endl; for (loop=0; loop<NLOOP; loop++) { for (pass=0; pass<NPASS; pass++) { for (subpass=0; subpass<SUBP; subpass++) { for (i=0; i<MAXP; i++) { int rno = rand(); if (rno & 8) { if (pointers[i]) { if (!(rno & realloc_mask)) { r = rsize(); curmax -= size[i]; curmax += r; pointers[i] = realloc(pointers[i], rsize()); size[i] = r; if (absmax < curmax) absmax = curmax; } else { curmax -= size[i]; free(pointers[i]); pointers[i] = 0; ++frees; } } else { r = rsize(); curmax += r; pointers[i] = malloc(r); size[i] = r; ++allocations; allocations_size += r; if (absmax < curmax) absmax = curmax; } } } } } double end_time = stk::cpu_time(); #if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR ptrdiff_t end_mem = malloc_used(); ptrdiff_t end_footprint = malloc_footprint(); #else ptrdiff_t end_mem = reinterpret_cast<ptrdiff_t>(sbrk(0)); #endif double elapsed = end_time - start_time; std::cout << std::setw(14) << elapsed << " ticks " #if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR << std::setw(14) << (end_footprint - start_footprint)/1024 << " K " << std::setw(14) << malloc_max_footprint()/1024 << " K " #endif << std::setw(14) << (end_mem - start_mem)/1024 << " K " << std::setw(14) << curmax/1024 << " K" << std::endl; } std::cout << allocations << " allocations of " << allocations_size/1024 << " K " << std::endl << frees << " frees" << std::endl; for (i=0; i<MAXP; i++) if (pointers[i]) free(pointers[i]); }