Changes in / [43c461d:4ea632e]
- Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/Heap.cc
r43c461d r4ea632e 7 7 // Heap.cc -- 8 8 // 9 // Author : Thierry Delisle10 // Created On : Thu May 3 16:16:10 20189 // Author : Peter A. Buhr 10 // Created On : 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri May 4 17:27:31201813 // Update Count : 2 812 // Last Modified On : Thu May 3 16:16:10 2018 13 // Update Count : 2 14 14 // 15 15 … … 21 21 #include <iostream> 22 22 23 //#define WITH_HEAP_STATISTICS 23 namespace HeapStats { 24 #if !defined( WITH_HEAP_STATISTICS ) 25 void newPass( const char * const ) {} 24 26 25 namespace HeapStats { 26 #if !defined( WITH_HEAP_STATISTICS ) 27 void newPass( const char * const ) {} 27 void printStats() {} 28 #else 29 struct StatBlock { 30 const char * name = nullptr; 31 size_t mallocs = 0; 32 size_t frees = 0; 33 }; 28 34 29 void printStats() {} 30 #else 31 struct StatBlock { 32 const char * name = nullptr; 33 size_t mallocs = 0; 34 size_t frees = 0; 35 }; 35 StatBlock passes[100]; 36 const size_t passes_size = sizeof(passes) / sizeof(passes[0]); 37 size_t passes_cnt = 0; 36 38 37 StatBlock passes[100] = {{ "Pre-Parse", 0, 0 }}; 38 const size_t passes_size = sizeof(passes) / sizeof(passes[0]); 39 size_t passes_cnt = 1; 39 void newPass( const char * const name ) { 40 passes[passes_cnt].name = name; 41 passes[passes_cnt].mallocs = 0; 42 passes[passes_cnt].frees = 0; 43 passes_cnt++; 40 44 41 void newPass( const char * const name ) { 42 passes[passes_cnt].name = name; 43 passes[passes_cnt].mallocs = 0; 44 passes[passes_cnt].frees = 0; 45 passes_cnt++; 46 47 assertf(passes_cnt < passes_size, "Too many passes for HeapStats, increase the size of the array in Heap.h"); 48 } 49 50 void print(size_t value, size_t total) { 51 std::cerr << std::setw(12) << value; 52 std::cerr << "(" << std::setw(3); 53 std::cerr << (value == 0 ? 0 : value * 100 / total); 54 std::cerr << "%) | "; 55 } 56 57 void print(const StatBlock& stat, size_t nc, size_t total_mallocs, size_t total_frees) { 58 std::cerr << std::setw(nc) << stat.name; 59 std::cerr << " | "; 60 61 print(stat.mallocs, total_mallocs); 62 print(stat.frees , total_frees ); 63 std::cerr << "\n"; 64 } 65 66 void print(char c, size_t nc) { 67 for(size_t i = 0; i < nc; i++) { 68 std::cerr << c; 69 } 70 std::cerr << '\n'; 71 } 72 73 void printStats() { 74 size_t nc = 0; 75 size_t total_mallocs = 0; 76 size_t total_frees = 0; 77 for(size_t i = 0; i < passes_cnt; i++) { 78 nc = std::max(nc, std::strlen(passes[i].name)); 79 total_mallocs += passes[i].mallocs; 80 total_frees += passes[i].frees; 81 } 82 size_t nct = nc + 44; 83 84 const char * const title = "Heap Usage Statistic"; 85 print('=', nct); 86 for(size_t i = 0; i < (nct - std::strlen(title)) / 2; i++) std::cerr << ' '; 87 std::cerr << title << std::endl; 88 print('-', nct); 89 std::cerr << std::setw(nc) << "Pass"; 90 std::cerr << " | Malloc Count | Free Count |" << std::endl; 91 92 print('-', nct); 93 for(size_t i = 0; i < passes_cnt; i++) { 94 print(passes[i], nc, total_mallocs, total_frees); 95 } 96 print('-', nct); 97 print({"Sum", total_mallocs, total_frees}, nc, total_mallocs, total_frees); 98 99 } 100 101 #include <stdarg.h> 102 #include <stddef.h> 103 #include <stdio.h> 104 #include <string.h> 105 #include <unistd.h> 106 #include <signal.h> 107 extern "C" { 108 #include <dlfcn.h> 109 #include <execinfo.h> 110 } 111 112 //============================================================================================= 113 // Interposing helpers 114 //============================================================================================= 115 116 typedef void (* generic_fptr_t)(void); 117 generic_fptr_t interpose_symbol( const char * symbol, const char * version ) { 118 const char * error; 119 120 static void * library; 121 if ( ! library ) { 122 #if defined( RTLD_NEXT ) 123 library = RTLD_NEXT; 124 #else 125 // missing RTLD_NEXT => must hard-code library name, assuming libstdc++ 126 library = dlopen( "libc.so.6", RTLD_LAZY ); 127 error = dlerror(); 128 if ( error ) { 129 std::cerr << "interpose_symbol : failed to open libc, " << error << std::endl; 130 abort(); 131 } 132 #endif 133 } // if 134 135 generic_fptr_t fptr; 136 137 #if defined( _GNU_SOURCE ) 138 if ( version ) { 139 fptr = (generic_fptr_t)dlvsym( library, symbol, version ); 140 } else { 141 fptr = (generic_fptr_t)dlsym( library, symbol ); 142 } 143 #else 144 fptr = (generic_fptr_t)dlsym( library, symbol ); 145 #endif // _GNU_SOURCE 146 147 extern "C" { 148 void * malloc( size_t size ) __attribute__((malloc)); 149 void * malloc( size_t size ) { 150 static auto __malloc = reinterpret_cast<void * (*)(size_t)>(interpose_symbol( "malloc", nullptr )); 151 if( passes_cnt > 0 ) passes[passes_cnt - 1].mallocs++; 152 return __malloc( size ); 45 assertf(passes_cnt < passes_size, "Too many passes for HeapStats, increase the size of the array in Heap.h"); 153 46 } 154 47 155 void free( void * ptr ) { 156 static auto __free = reinterpret_cast<void (*)(void *)>(interpose_symbol( "free", nullptr )); 157 if( passes_cnt > 0 ) passes[passes_cnt - 1].frees++; 158 return __free( ptr ); 48 void print(size_t value, size_t total) { 49 std::cerr << std::setw(12) << value; 50 std::cerr << "(" << std::setw(3); 51 std::cerr << (value == 0 ? 0 : value * 100 / total); 52 std::cerr << "%) | "; 159 53 } 160 54 161 void * calloc( size_t nelem, size_t size ) { 162 static auto __calloc = reinterpret_cast<void * (*)(size_t, size_t)>(interpose_symbol( "calloc", nullptr )); 163 if( passes_cnt > 0 ) passes[passes_cnt - 1].mallocs++; 164 return __calloc( nelem, size ); 55 void print(const StatBlock& stat, size_t nc, size_t total_mallocs, size_t total_frees) { 56 std::cerr << std::setw(nc) << stat.name; 57 std::cerr << " | "; 58 59 print(stat.mallocs, total_mallocs); 60 print(stat.frees , total_frees ); 61 std::cerr << "\n"; 165 62 } 166 63 167 void * realloc( void * ptr, size_t size ) { 168 static auto __realloc = reinterpret_cast<void * (*)(void *, size_t)>(interpose_symbol( "realloc", nullptr )); 169 void * s = __realloc( ptr, size ); 170 if ( s != ptr && passes_cnt > 0 ) { // did realloc get new storage ? 171 passes[passes_cnt - 1].mallocs++; 172 passes[passes_cnt - 1].frees++; 173 } // if 174 return s; 64 void print(char c, size_t nc) { 65 for(size_t i = 0; i < nc; i++) { 66 std::cerr << c; 67 } 68 std::cerr << '\n'; 175 69 } 176 } 177 #endif 70 71 void printStats() { 72 size_t nc = 0; 73 size_t total_mallocs = 0; 74 size_t total_frees = 0; 75 for(size_t i = 0; i < passes_cnt; i++) { 76 nc = std::max(nc, std::strlen(passes[i].name)); 77 total_mallocs += passes[i].mallocs; 78 total_frees += passes[i].frees; 79 } 80 size_t nct = nc + 44; 81 82 const char * const title = "Heap Usage Statistic"; 83 print('=', nct); 84 for(size_t i = 0; i < (nct - std::strlen(title)) / 2; i++) std::cerr << ' '; 85 std::cerr << title << std::endl; 86 87 print('-', nct); 88 std::cerr << std::setw(nc) << "Pass"; 89 std::cerr << " | Malloc Count | Free Count |" << std::endl; 90 91 print('-', nct); 92 for(size_t i = 0; i < passes_cnt; i++) { 93 print(passes[i], nc, total_mallocs, total_frees); 94 } 95 96 print('-', nct); 97 print({"Sum", total_mallocs, total_frees}, nc, total_mallocs, total_frees); 98 99 } 100 #endif 178 101 } 102 -
src/Common/Heap.h
r43c461d r4ea632e 7 7 // Heap.h -- 8 8 // 9 // Author : Thierry Delisle10 // Created On : Thu May 3 16:16:10 20189 // Author : Peter A. Buhr 10 // Created On : 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri May 4 14:34:08201813 // Update Count : 312 // Last Modified On : Thu May 3 16:16:10 2018 13 // Update Count : 2 14 14 // 15 15 -
src/libcfa/interpose.c
r43c461d r4ea632e 10 10 // Created On : Wed Mar 29 16:10:31 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri May 4 14:35:13201813 // Update Count : 9712 // Last Modified On : Tue May 1 15:05:35 2018 13 // Update Count : 83 14 14 // 15 15 16 16 #include <stdarg.h> 17 17 #include <stddef.h> 18 19 extern "C" { 18 20 #include <stdio.h> 19 21 #include <string.h> 22 #include <dlfcn.h> 20 23 #include <unistd.h> 21 24 #define __USE_GNU 22 25 #include <signal.h> 23 26 #undef __USE_GNU 24 extern "C" {25 #include <dlfcn.h>26 27 #include <execinfo.h> 27 28 } … … 36 37 //============================================================================================= 37 38 38 typedef void (* 39 generic_fptr_t interpose_symbol( const char * symbol, const char *version ) {39 typedef void (*generic_fptr_t)(void); 40 generic_fptr_t interpose_symbol( const char* symbol, const char *version ) { 40 41 const char * error; 41 42 … … 54 55 } // if 55 56 56 union { generic_fptr_t fptr; void 57 union { generic_fptr_t fptr; void* ptr; } originalFunc; 57 58 58 59 #if defined( _GNU_SOURCE ) … … 72 73 } 73 74 74 #define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver ) 75 76 //============================================================================================= 77 // Interposition Startup logic 75 forall(dtype T) 76 static inline void ptr_from_symbol( T** symbol_ptr, const char * symbol_name, const char * version) { 77 union { 78 generic_fptr_t gp; 79 T* tp; 80 } u; 81 82 u.gp = interpose_symbol( symbol_name, version ); 83 84 *symbol_ptr = u.tp; 85 } 86 87 #define INTERPOSE_LIBC( x, ver ) ptr_from_symbol( (void**)&__cabi_libc.x, #x, ver) 88 89 //============================================================================================= 90 // Terminating Signals logic 78 91 //============================================================================================= 79 92
Note: See TracChangeset
for help on using the changeset viewer.