Changeset 45040f06 for src/Common
- Timestamp:
- May 25, 2018, 2:10:02 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
- Children:
- 58e822a
- Parents:
- 34ca532
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/Heap.cc
r34ca532 r45040f06 21 21 #include <iostream> 22 22 23 //#define WITH_HEAP_STATISTICS23 #define WITH_HEAP_STATISTICS 24 24 25 25 namespace HeapStats { … … 30 30 #else 31 31 struct StatBlock { 32 const char * name = nullptr; 33 size_t mallocs = 0; 34 size_t frees = 0; 32 const char * name = nullptr; ///< Name of this pass 33 size_t mallocs = 0; ///< Allocations in this pass 34 size_t frees = 0; ///< Frees in this pass 35 size_t n_allocs = 0; ///< Current number of live allocations 36 size_t peak_allocs = 0; ///< Peak number of live allocations this pass 35 37 }; 36 38 37 StatBlock passes[100] = {{ "Pre-Parse", 0, 0 }};39 StatBlock passes[100] = {{ "Pre-Parse", 0, 0, 0, 0 }}; 38 40 const size_t passes_size = sizeof(passes) / sizeof(passes[0]); 39 41 size_t passes_cnt = 1; … … 43 45 passes[passes_cnt].mallocs = 0; 44 46 passes[passes_cnt].frees = 0; 47 passes[passes_cnt].n_allocs 48 = passes[passes_cnt].peak_allocs 49 = passes[passes_cnt-1].n_allocs; 45 50 passes_cnt++; 46 51 … … 55 60 } 56 61 57 void print(const StatBlock& stat, size_t nc, size_t total_mallocs, size_t total_frees ) {62 void print(const StatBlock& stat, size_t nc, size_t total_mallocs, size_t total_frees, size_t overall_peak) { 58 63 std::cerr << std::setw(nc) << stat.name; 59 64 std::cerr << " | "; 60 65 61 print(stat.mallocs, total_mallocs); 62 print(stat.frees , total_frees ); 66 print(stat.mallocs, total_mallocs); 67 print(stat.frees, total_frees ); 68 print(stat.peak_allocs, overall_peak ); 63 69 std::cerr << "\n"; 64 70 } … … 75 81 size_t total_mallocs = 0; 76 82 size_t total_frees = 0; 83 size_t overall_peak = 0; 77 84 for(size_t i = 0; i < passes_cnt; i++) { 78 85 nc = std::max(nc, std::strlen(passes[i].name)); 79 86 total_mallocs += passes[i].mallocs; 80 87 total_frees += passes[i].frees; 81 } 82 size_t nct = nc + 44; 88 overall_peak = std::max(overall_peak, passes[i].peak_allocs); 89 } 90 size_t nct = nc + 65; 83 91 84 92 const char * const title = "Heap Usage Statistic"; … … 88 96 print('-', nct); 89 97 std::cerr << std::setw(nc) << "Pass"; 90 std::cerr << " | Malloc Count | Free Count | " << std::endl;98 std::cerr << " | Malloc Count | Free Count | Peak Allocs |" << std::endl; 91 99 92 100 print('-', nct); 93 101 for(size_t i = 0; i < passes_cnt; i++) { 94 print(passes[i], nc, total_mallocs, total_frees );102 print(passes[i], nc, total_mallocs, total_frees, overall_peak); 95 103 } 96 104 print('-', nct); 97 print({"Sum", total_mallocs, total_frees}, nc, total_mallocs, total_frees); 105 print({"Sum", total_mallocs, total_frees, 0, overall_peak}, 106 nc, total_mallocs, total_frees, overall_peak); 98 107 99 108 } … … 158 167 void * malloc( size_t size ) { 159 168 static auto __malloc = reinterpret_cast<void * (*)(size_t)>(interpose_symbol( "malloc", nullptr )); 160 if( passes_cnt > 0 ) passes[passes_cnt - 1].mallocs++; 169 if( passes_cnt > 0 ) { 170 passes[passes_cnt - 1].mallocs++; 171 passes[passes_cnt - 1].n_allocs++; 172 passes[passes_cnt - 1].peak_allocs 173 = std::max(passes[passes_cnt - 1].peak_allocs, passes[passes_cnt - 1].n_allocs); 174 } 161 175 return __malloc( size ); 162 176 } … … 164 178 void free( void * ptr ) { 165 179 static auto __free = reinterpret_cast<void (*)(void *)>(interpose_symbol( "free", nullptr )); 166 if( passes_cnt > 0 ) passes[passes_cnt - 1].frees++; 180 if( passes_cnt > 0 ) { 181 passes[passes_cnt - 1].frees++; 182 passes[passes_cnt - 1].n_allocs--; 183 } 167 184 return __free( ptr ); 168 185 } … … 170 187 void * calloc( size_t nelem, size_t size ) { 171 188 static auto __calloc = reinterpret_cast<void * (*)(size_t, size_t)>(interpose_symbol( "calloc", nullptr )); 172 if( passes_cnt > 0 ) passes[passes_cnt - 1].mallocs++; 189 if( passes_cnt > 0 ) { 190 passes[passes_cnt - 1].mallocs++; 191 passes[passes_cnt - 1].n_allocs++; 192 passes[passes_cnt - 1].peak_allocs 193 = std::max(passes[passes_cnt - 1].peak_allocs, passes[passes_cnt - 1].n_allocs); 194 } 173 195 return __calloc( nelem, size ); 174 196 }
Note: See TracChangeset
for help on using the changeset viewer.