source: src/Common/Stats/Heap.cc @ 8f74a6a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 8f74a6a was 8f74a6a, checked in by tdelisle <tdelisle@…>, 5 years ago

Added code to support generic statistic counters in the compiler

  • Property mode set to 100644
File size: 6.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Heap.cc --
8//
9// Author           : Thierry Delisle
10// Created On       : Thu May  3 16:16:10 2018
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri May  4 17:27:31 2018
13// Update Count     : 28
14//
15
16#include <cassert>
17#include <cmath>
18#include <cstddef>
19#include <cstring>
20#include <iomanip>
21#include <iostream>
22
23namespace Stats {
24        namespace Heap {
25#if defined( NO_HEAP_STATISTICS )
26                void newPass( const char * const ) {}
27
28                void print() {}
29#else
30                struct StatBlock {
31                        const char * name  = nullptr;   ///< Name of this pass
32                        size_t mallocs     = 0;                 ///< Allocations in this pass
33                        size_t frees       = 0;                 ///< Frees in this pass
34                        size_t n_allocs    = 0;                 ///< Current number of live allocations
35                        size_t peak_allocs = 0;                 ///< Peak number of live allocations this pass
36                };
37
38                StatBlock    passes[100] = {{ "Pre-Parse", 0, 0, 0, 0 }};
39                const size_t passes_size = sizeof(passes) / sizeof(passes[0]);
40                size_t       passes_cnt = 1;
41
42                void newPass( const char * const name ) {
43                        passes[passes_cnt].name    = name;
44                        passes[passes_cnt].mallocs = 0;
45                        passes[passes_cnt].frees   = 0;
46                        passes[passes_cnt].n_allocs
47                                = passes[passes_cnt].peak_allocs
48                                = passes[passes_cnt-1].n_allocs;
49                        passes_cnt++;
50
51                        assertf(passes_cnt < passes_size, "Too many passes for Stats::Heap, increase the size of the array in Heap.cc");
52                }
53
54                void print(size_t value, size_t total) {
55                        std::cerr << std::setw(12) << value;
56                        std::cerr << "(" << std::setw(3);
57                        std::cerr << (value == 0 ? 0 : value * 100 / total);
58                        std::cerr << "%) | ";
59                }
60
61                void print(const StatBlock& stat, size_t nc, size_t total_mallocs, size_t total_frees, size_t overall_peak) {
62                        std::cerr << std::setw(nc) << stat.name;
63                        std::cerr << " | ";
64
65                        print(stat.mallocs,     total_mallocs);
66                        print(stat.frees,       total_frees  );
67                        print(stat.peak_allocs, overall_peak );
68                        std::cerr << "\n";
69                }
70
71                void print(char c, size_t nc) {
72                        for(size_t i = 0; i < nc; i++) {
73                                std::cerr << c;
74                        }
75                        std::cerr << '\n';
76                }
77
78                void print() {
79                        size_t nc = 0;
80                        size_t total_mallocs = 0;
81                        size_t total_frees   = 0;
82                        size_t overall_peak  = 0;
83                        for(size_t i = 0; i < passes_cnt; i++) {
84                                nc = std::max(nc, std::strlen(passes[i].name));
85                                total_mallocs += passes[i].mallocs;
86                                total_frees   += passes[i].frees;
87                                overall_peak = std::max(overall_peak, passes[i].peak_allocs);
88                        }
89                        size_t nct = nc + 65;
90
91                        const char * const title = "Heap Usage Statistic";
92                        print('=', nct);
93                        for(size_t i = 0; i < (nct - std::strlen(title)) / 2; i++) std::cerr << ' ';
94                        std::cerr << title << std::endl;
95                        print('-', nct);
96                        std::cerr << std::setw(nc) << "Pass";
97                        std::cerr << " |       Malloc Count |         Free Count |        Peak Allocs |" << std::endl;
98
99                        print('-', nct);
100                        for(size_t i = 0; i < passes_cnt; i++) {
101                                print(passes[i], nc, total_mallocs, total_frees, overall_peak);
102                        }
103                        print('-', nct);
104                        print({"Sum", total_mallocs, total_frees, 0, overall_peak},
105                                nc, total_mallocs, total_frees, overall_peak);
106
107                }
108
109#include <stdarg.h>
110#include <stddef.h>
111#include <stdio.h>
112#include <string.h>
113#include <unistd.h>
114#include <signal.h>
115                extern "C" {
116#include <dlfcn.h>
117#include <execinfo.h>
118                }
119
120        //=============================================================================================
121        // Interposing helpers
122        //=============================================================================================
123
124                typedef void (* generic_fptr_t)(void);
125                generic_fptr_t interpose_symbol( const char * symbol, const char * version ) {
126                        const char * error;
127
128                        static void * library;
129                        if ( ! library ) {
130#                               if defined( RTLD_NEXT )
131                                        library = RTLD_NEXT;
132#                               else
133                                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
134                                        library = dlopen( "libc.so.6", RTLD_LAZY );
135                                        error = dlerror();
136                                        if ( error ) {
137                                                std::cerr << "interpose_symbol : failed to open libc, " << error << std::endl;
138                                                abort();
139                                        }
140#                               endif // RTLD_NEXT
141                        } // if
142
143                        generic_fptr_t fptr;
144
145#                       if defined( _GNU_SOURCE )
146                                if ( version ) {
147                                        fptr = (generic_fptr_t)dlvsym( library, symbol, version );
148                                } else {
149                                        fptr = (generic_fptr_t)dlsym( library, symbol );
150                                }
151#                       else
152                                fptr = (generic_fptr_t)dlsym( library, symbol );
153#                       endif // _GNU_SOURCE
154
155                        error = dlerror();
156                        if ( error ) {
157                                std::cerr << "interpose_symbol : internal error, " << error << std::endl;
158                                abort();
159                        }
160
161                        return fptr;
162                }
163
164                extern "C" {
165                        void * malloc( size_t size ) __attribute__((malloc));
166                        void * malloc( size_t size ) {
167                                static auto __malloc = reinterpret_cast<void * (*)(size_t)>(interpose_symbol( "malloc", nullptr ));
168                                if( passes_cnt > 0 ) {
169                                        passes[passes_cnt - 1].mallocs++;
170                                        passes[passes_cnt - 1].n_allocs++;
171                                        passes[passes_cnt - 1].peak_allocs
172                                                = std::max(passes[passes_cnt - 1].peak_allocs, passes[passes_cnt - 1].n_allocs);
173                                }
174                                return __malloc( size );
175                        }
176
177                        void free( void * ptr ) {
178                                static auto __free = reinterpret_cast<void   (*)(void *)>(interpose_symbol( "free", nullptr ));
179                                if( passes_cnt > 0 ) {
180                                        passes[passes_cnt - 1].frees++;
181                                        passes[passes_cnt - 1].n_allocs--;
182                                }
183                                return __free( ptr );
184                        }
185
186                        void * calloc( size_t nelem, size_t size ) {
187                                static auto __calloc = reinterpret_cast<void * (*)(size_t, size_t)>(interpose_symbol( "calloc", nullptr ));
188                                if( passes_cnt > 0 ) {
189                                        passes[passes_cnt - 1].mallocs++;
190                                        passes[passes_cnt - 1].n_allocs++;
191                                        passes[passes_cnt - 1].peak_allocs
192                                                = std::max(passes[passes_cnt - 1].peak_allocs, passes[passes_cnt - 1].n_allocs);
193                                }
194                                return __calloc( nelem, size );
195                        }
196
197                        void * realloc( void * ptr, size_t size ) {
198                                static auto __realloc = reinterpret_cast<void * (*)(void *, size_t)>(interpose_symbol( "realloc", nullptr ));
199                                void * s = __realloc( ptr, size );
200                                if ( s != ptr && passes_cnt > 0 ) {                     // did realloc get new storage ?
201                                        passes[passes_cnt - 1].mallocs++;
202                                        passes[passes_cnt - 1].frees++;
203                                } // if
204                                return s;
205                        }
206                }
207#endif
208        }
209}
Note: See TracBrowser for help on using the repository browser.