source: src/Common/Heap.cc @ a64a413

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since a64a413 was a96691d, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Better formatting for heap stats

  • Property mode set to 100644
File size: 2.6 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           : Peter A. Buhr
10// Created On       :
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu May  3 16:16:10 2018
13// Update Count     : 2
14//
15
16#include <cassert>
17#include <cmath>
18#include <cstddef>
19#include <cstring>
20#include <iomanip>
21#include <iostream>
22
23namespace HeapStats {
24        #if !defined( WITH_HEAP_STATISTICS )
25                void newPass( const char * const ) {}
26
27                void printStats() {}
28        #else
29                struct StatBlock {
30                        const char * name = nullptr;
31                        size_t mallocs    = 0;
32                        size_t frees      = 0;
33                };
34
35                StatBlock    passes[100];
36                const size_t passes_size = sizeof(passes) / sizeof(passes[0]);
37                size_t       passes_cnt = 0;
38
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++;
44
45                        assertf(passes_cnt < passes_size, "Too many passes for HeapStats, increase the size of the array in Heap.h");
46                }
47
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 << "%) | ";
53                }
54
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";
62                }
63
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';
69                }
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                        print('-', nct);
87                        std::cerr << std::setw(nc) << "Pass";
88                        std::cerr << " |       Malloc Count |         Free Count |" << std::endl;
89
90                        print('-', nct);
91                        for(size_t i = 0; i < passes_cnt; i++) {
92                                print(passes[i], nc, total_mallocs, total_frees);
93                        }
94                        print('-', nct);
95                        print({"Sum", total_mallocs, total_frees}, nc, total_mallocs, total_frees);
96
97                }
98        #endif
99}
100
Note: See TracBrowser for help on using the repository browser.