CMS 3D CMS Logo

AllocMonitorRegistry.h
Go to the documentation of this file.
1 #ifndef PerfTools_AllocMonitor_AllocMonitorRegistry_h
2 #define PerfTools_AllocMonitor_AllocMonitorRegistry_h
3 // -*- C++ -*-
4 //
5 // Package: PerfTools/AllocMonitor
6 // Class : AllocMonitorRegistry
7 //
16 //
17 // Original Author: Christopher Jones
18 // Created: Mon, 21 Aug 2023 14:12:54 GMT
19 //
20 
21 // system include files
22 #include <memory>
23 #include <vector>
24 #include <malloc.h>
25 #include <stdlib.h>
26 
27 // user include files
29 
30 // forward declarations
31 
32 namespace cms::perftools {
33  class AllocTester;
34 
36  public:
38 
39  AllocMonitorRegistry(AllocMonitorRegistry&&) = delete; // stop default
40  AllocMonitorRegistry(const AllocMonitorRegistry&) = delete; // stop default
41  AllocMonitorRegistry& operator=(const AllocMonitorRegistry&) = delete; // stop default
42  AllocMonitorRegistry& operator=(AllocMonitorRegistry&&) = delete; // stop default
43 
44  // ---------- static member functions --------------------
46  static bool necessaryLibraryWasPreloaded();
47 
48  // ---------- member functions ---------------------------
49 
50  //The functions are not thread safe
51  template <typename T, typename... ARGS>
52  T* createAndRegisterMonitor(ARGS&&... iArgs);
54 
55  private:
56  friend void* ::malloc(size_t) noexcept;
57  friend void* ::calloc(size_t, size_t) noexcept;
58  friend void* ::realloc(void*, size_t) noexcept;
59  friend void* ::aligned_alloc(size_t, size_t) noexcept;
60  friend int ::posix_memalign(void**, size_t, size_t) noexcept;
61  friend void* ::memalign(size_t, size_t) noexcept;
62  friend void ::free(void*) noexcept;
63 
64  friend void* ::operator new(std::size_t size);
65  friend void* ::operator new[](std::size_t size);
66  friend void* ::operator new(std::size_t count, std::align_val_t al);
67  friend void* ::operator new[](std::size_t count, std::align_val_t al);
68  friend void* ::operator new(std::size_t count, const std::nothrow_t & tag) noexcept;
69  friend void* ::operator new[](std::size_t count, const std::nothrow_t & tag) noexcept;
70  friend void* ::operator new(std::size_t count, std::align_val_t al, const std::nothrow_t&) noexcept;
71  friend void* ::operator new[](std::size_t count, std::align_val_t al, const std::nothrow_t&) noexcept;
72 
73  friend void ::operator delete(void* ptr) noexcept;
74  friend void ::operator delete[](void* ptr) noexcept;
75  friend void ::operator delete(void* ptr, std::align_val_t al) noexcept;
76  friend void ::operator delete[](void* ptr, std::align_val_t al) noexcept;
77  friend void ::operator delete(void* ptr, std::size_t sz) noexcept;
78  friend void ::operator delete[](void* ptr, std::size_t sz) noexcept;
79  friend void ::operator delete(void* ptr, std::size_t sz, std::align_val_t al) noexcept;
80  friend void ::operator delete[](void* ptr, std::size_t sz, std::align_val_t al) noexcept;
81  friend void ::operator delete(void* ptr, const std::nothrow_t & tag) noexcept;
82  friend void ::operator delete[](void* ptr, const std::nothrow_t & tag) noexcept;
83  friend void ::operator delete(void* ptr, std::align_val_t al, const std::nothrow_t & tag) noexcept;
84  friend void ::operator delete[](void* ptr, std::align_val_t al, const std::nothrow_t & tag) noexcept;
85 
86  friend class AllocTester;
87 
88  // ---------- member data --------------------------------
89  void start();
90  bool stopReporting();
91  void startReporting();
92 
93  struct Guard {
94  explicit Guard(AllocMonitorRegistry& iReg) noexcept : reg_(iReg), original_(iReg.stopReporting()) {}
95  ~Guard() {
96  if (original_) {
98  }
99  }
100 
101  bool shouldReport() const noexcept { return original_; }
102 
103  Guard(Guard const&) = delete;
104  Guard(Guard&&) = delete;
105  Guard& operator=(Guard const&) = delete;
106  Guard& operator=(Guard&&) = delete;
107 
108  private:
110  bool original_;
111  };
112 
113  Guard makeGuard() { return Guard(*this); }
114 
115  void allocCalled_(size_t, size_t, void const*);
116  void deallocCalled_(size_t, void const*);
117 
118  template <typename ALLOC, typename ACT>
119  auto allocCalled(size_t iRequested, ALLOC iAlloc, ACT iGetActual) {
120  [[maybe_unused]] Guard g = makeGuard();
121  auto a = iAlloc();
122  if (g.shouldReport()) {
123  allocCalled_(iRequested, iGetActual(a), a);
124  }
125  return a;
126  }
127  template <typename DEALLOC, typename ACT>
128  void deallocCalled(void* iPtr, DEALLOC iDealloc, ACT iGetActual) {
129  [[maybe_unused]] Guard g = makeGuard();
130  if (g.shouldReport() and iPtr != nullptr) {
131  deallocCalled_(iGetActual(iPtr), iPtr);
132  }
133  iDealloc(iPtr);
134  }
135 
137  std::vector<std::unique_ptr<AllocMonitorBase>> monitors_;
138  };
139 
140  template <typename T, typename... ARGS>
142  [[maybe_unused]] Guard guard = makeGuard();
143  start();
144 
145  auto m = std::make_unique<T>(std::forward<ARGS>(iArgs)...);
146  auto p = m.get();
147  monitors_.push_back(std::move(m));
148  return p;
149  }
150 } // namespace cms::perftools
151 #endif
friend void * memalign(size_t, size_t) noexcept
void allocCalled_(size_t, size_t, void const *)
void deallocCalled_(size_t, void const *)
auto allocCalled(size_t iRequested, ALLOC iAlloc, ACT iGetActual)
int posix_memalign(void **memptr, size_t alignment, size_t size) noexcept
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
AllocMonitorRegistry & operator=(const AllocMonitorRegistry &)=delete
void free(void *ptr) noexcept
static AllocMonitorRegistry & instance()
friend void * aligned_alloc(size_t, size_t) noexcept
friend void * realloc(void *, size_t) noexcept
void deallocCalled(void *iPtr, DEALLOC iDealloc, ACT iGetActual)
friend void * malloc(size_t) noexcept
std::vector< std::unique_ptr< AllocMonitorBase > > monitors_
friend void * calloc(size_t, size_t) noexcept
Guard(AllocMonitorRegistry &iReg) noexcept
double a
Definition: hdecay.h:121
long double T
Guard & operator=(Guard const &)=delete
def move(src, dest)
Definition: eostools.py:511