Documentation of CSL
alloc_monitor.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef ALLOC_MONITOR_H_INCLUDED
24 #define ALLOC_MONITOR_H_INCLUDED
25 
26 #include <iostream>
27 #include <ctime>
28 #include <map>
29 
30 class __data_alloc {
31 
32  using Type = int;
33  using Time = std::clock_t;
34  struct data_alloc {
35 
36  int count;
37  Time time;
38 
39  };
40 
41  public:
42 
43  std::map<Type, data_alloc> DATA{};
44 
45  void record(Type type,
46  Time const& time)
47  {
48  if (auto pos = DATA.find(type); pos == DATA.end()) {
49  DATA[type] = {1, time};
50  }
51  else {
52  ++DATA[type].count;
53  DATA[type].time += time;
54  }
55  }
56 
57  void displayResults(std::ostream& out)
58  {
59  for (const auto& [key, data] : DATA) {
60  out << key << ": \n";
61  out << " --> " << data.count << " allocations.\n";
62  out << " --> " << data.time/CLOCKS_PER_SEC
63  << " s of total life time.\n";
64  }
65  }
66 };
67 
68 inline __data_alloc __DATA;
69 
71 
72  private:
73 
74  std::clock_t __allocation_time;
75 
76  public:
77 
78  __alloc_monitor() {
79  __allocation_time = std::clock();
80  }
81 
82  void __record_data_alloc(int type) {
83  __DATA.record(type, std::clock() - __allocation_time);
84  }
85 };
86 
87 #endif
Definition: alloc_monitor.h:70
Definition: alloc_monitor.h:30