|
| 1 | +#ifndef PerfTools_AllocMonitor_AllocMonitorRegistry_h |
| 2 | +#define PerfTools_AllocMonitor_AllocMonitorRegistry_h |
| 3 | +// -*- C++ -*- |
| 4 | +// |
| 5 | +// Package: PerfTools/AllocMonitor |
| 6 | +// Class : AllocMonitorRegistry |
| 7 | +// |
| 8 | +/**\class AllocMonitorRegistry AllocMonitorRegistry.h "AllocMonitorRegistry.h" |
| 9 | +
|
| 10 | + Description: [one line class summary] |
| 11 | +
|
| 12 | + Usage: |
| 13 | + <usage> |
| 14 | +
|
| 15 | +*/ |
| 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 |
| 28 | +#include "PerfTools/AllocMonitor/interface/AllocMonitorBase.h" |
| 29 | + |
| 30 | +// forward declarations |
| 31 | + |
| 32 | +namespace cms::perftools { |
| 33 | + class AllocTester; |
| 34 | + |
| 35 | + class AllocMonitorRegistry { |
| 36 | + public: |
| 37 | + ~AllocMonitorRegistry(); |
| 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 -------------------- |
| 45 | + static AllocMonitorRegistry& instance(); |
| 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); |
| 53 | + void deregisterMonitor(AllocMonitorBase*); |
| 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 void ::free(void*) noexcept; |
| 61 | + |
| 62 | + friend void* ::operator new(std::size_t size); |
| 63 | + friend void* ::operator new[](std::size_t size); |
| 64 | + friend void* ::operator new(std::size_t count, std::align_val_t al); |
| 65 | + friend void* ::operator new[](std::size_t count, std::align_val_t al); |
| 66 | + friend void* ::operator new(std::size_t count, const std::nothrow_t& tag) noexcept; |
| 67 | + friend void* ::operator new[](std::size_t count, const std::nothrow_t& tag) noexcept; |
| 68 | + friend void* ::operator new(std::size_t count, std::align_val_t al, const std::nothrow_t&) noexcept; |
| 69 | + friend void* ::operator new[](std::size_t count, std::align_val_t al, const std::nothrow_t&) noexcept; |
| 70 | + |
| 71 | + friend void ::operator delete(void* ptr) noexcept; |
| 72 | + friend void ::operator delete[](void* ptr) noexcept; |
| 73 | + friend void ::operator delete(void* ptr, std::align_val_t al) noexcept; |
| 74 | + friend void ::operator delete[](void* ptr, std::align_val_t al) noexcept; |
| 75 | + friend void ::operator delete(void* ptr, std::size_t sz) noexcept; |
| 76 | + friend void ::operator delete[](void* ptr, std::size_t sz) noexcept; |
| 77 | + friend void ::operator delete(void* ptr, std::size_t sz, std::align_val_t al) noexcept; |
| 78 | + friend void ::operator delete[](void* ptr, std::size_t sz, std::align_val_t al) noexcept; |
| 79 | + friend void ::operator delete(void* ptr, const std::nothrow_t& tag) noexcept; |
| 80 | + friend void ::operator delete[](void* ptr, const std::nothrow_t& tag) noexcept; |
| 81 | + friend void ::operator delete(void* ptr, std::align_val_t al, const std::nothrow_t& tag) noexcept; |
| 82 | + friend void ::operator delete[](void* ptr, std::align_val_t al, const std::nothrow_t& tag) noexcept; |
| 83 | + |
| 84 | + friend class AllocTester; |
| 85 | + |
| 86 | + // ---------- member data -------------------------------- |
| 87 | + void start(); |
| 88 | + bool& isRunning(); |
| 89 | + |
| 90 | + struct Guard { |
| 91 | + explicit Guard(bool& iOriginal) noexcept : address_(&iOriginal), original_(iOriginal) { *address_ = false; } |
| 92 | + ~Guard() { *address_ = original_; } |
| 93 | + |
| 94 | + bool running() const noexcept { return original_; } |
| 95 | + |
| 96 | + Guard(Guard const&) = delete; |
| 97 | + Guard(Guard&&) = delete; |
| 98 | + Guard& operator=(Guard const&) = delete; |
| 99 | + Guard& operator=(Guard&&) = delete; |
| 100 | + |
| 101 | + private: |
| 102 | + bool* address_; |
| 103 | + bool original_; |
| 104 | + }; |
| 105 | + |
| 106 | + Guard makeGuard() { return Guard(isRunning()); } |
| 107 | + |
| 108 | + void allocCalled_(size_t, size_t); |
| 109 | + void deallocCalled_(size_t); |
| 110 | + |
| 111 | + template <typename ALLOC, typename ACT> |
| 112 | + auto allocCalled(size_t iRequested, ALLOC iAlloc, ACT iGetActual) { |
| 113 | + [[maybe_unused]] Guard g = makeGuard(); |
| 114 | + auto a = iAlloc(); |
| 115 | + if (g.running()) { |
| 116 | + allocCalled_(iRequested, iGetActual(a)); |
| 117 | + } |
| 118 | + return a; |
| 119 | + } |
| 120 | + template <typename DEALLOC, typename ACT> |
| 121 | + void deallocCalled(DEALLOC iDealloc, ACT iGetActual) { |
| 122 | + [[maybe_unused]] Guard g = makeGuard(); |
| 123 | + if (g.running()) { |
| 124 | + deallocCalled_(iGetActual()); |
| 125 | + } |
| 126 | + iDealloc(); |
| 127 | + } |
| 128 | + |
| 129 | + AllocMonitorRegistry(); |
| 130 | + std::vector<std::unique_ptr<AllocMonitorBase>> monitors_; |
| 131 | + }; |
| 132 | + |
| 133 | + template <typename T, typename... ARGS> |
| 134 | + T* AllocMonitorRegistry::createAndRegisterMonitor(ARGS&&... iArgs) { |
| 135 | + [[maybe_unused]] Guard guard = makeGuard(); |
| 136 | + start(); |
| 137 | + |
| 138 | + auto m = std::make_unique<T>(std::forward<ARGS>(iArgs)...); |
| 139 | + auto p = m.get(); |
| 140 | + monitors_.push_back(std::move(m)); |
| 141 | + return p; |
| 142 | + } |
| 143 | +} // namespace cms::perftools |
| 144 | +#endif |
0 commit comments