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& isRunning();
91 
92  struct Guard {
93  explicit Guard(bool& iOriginal) noexcept : address_(&iOriginal), original_(iOriginal) { *address_ = false; }
95 
96  bool running() const noexcept { return original_; }
97 
98  Guard(Guard const&) = delete;
99  Guard(Guard&&) = delete;
100  Guard& operator=(Guard const&) = delete;
101  Guard& operator=(Guard&&) = delete;
102 
103  private:
104  bool* address_;
105  bool original_;
106  };
107 
108  Guard makeGuard() { return Guard(isRunning()); }
109 
110  void allocCalled_(size_t, size_t);
111  void deallocCalled_(size_t);
112 
113  template <typename ALLOC, typename ACT>
114  auto allocCalled(size_t iRequested, ALLOC iAlloc, ACT iGetActual) {
115  [[maybe_unused]] Guard g = makeGuard();
116  auto a = iAlloc();
117  if (g.running()) {
118  allocCalled_(iRequested, iGetActual(a));
119  }
120  return a;
121  }
122  template <typename DEALLOC, typename ACT>
123  void deallocCalled(void* iPtr, DEALLOC iDealloc, ACT iGetActual) {
124  [[maybe_unused]] Guard g = makeGuard();
125  if (g.running() and iPtr != nullptr) {
126  deallocCalled_(iGetActual(iPtr));
127  }
128  iDealloc(iPtr);
129  }
130 
132  std::vector<std::unique_ptr<AllocMonitorBase>> monitors_;
133  };
134 
135  template <typename T, typename... ARGS>
137  [[maybe_unused]] Guard guard = makeGuard();
138  start();
139 
140  auto m = std::make_unique<T>(std::forward<ARGS>(iArgs)...);
141  auto p = m.get();
142  monitors_.push_back(std::move(m));
143  return p;
144  }
145 } // namespace cms::perftools
146 #endif
friend void * memalign(size_t, size_t) noexcept
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
double a
Definition: hdecay.h:121
long double T
void deregisterMonitor(AllocMonitorBase *)
Guard & operator=(Guard const &)=delete
def move(src, dest)
Definition: eostools.py:511