CMS 3D CMS Logo

preload.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PerfTools/MaxMemoryPreload
4 // Class : preload
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Wed, 23 Aug 2023 17:56:44 GMT
11 //
12 
13 // system include files
14 #include <atomic>
15 #include <iostream>
16 
17 // user include files
20 
21 //
22 // constants, enums and typedefs
23 //
24 
25 //
26 // static data member definitions
27 //
28 
29 namespace {
30  class MonitorAdaptor : public cms::perftools::AllocMonitorBase {
31  public:
32  MonitorAdaptor() noexcept = default;
33  ~MonitorAdaptor() noexcept override { performanceReport(); }
34 
35  private:
36  void allocCalled(size_t iRequested, size_t iActual) final {
37  nAllocations_.fetch_add(1, std::memory_order_acq_rel);
38  requested_.fetch_add(iRequested, std::memory_order_acq_rel);
39 
40  auto a = presentActual_.fetch_add(iActual, std::memory_order_acq_rel);
41  a += iActual;
42  auto max = maxActual_.load(std::memory_order_relaxed);
43 
44  while (a > max) {
45  if (maxActual_.compare_exchange_strong(max, a, std::memory_order_acq_rel)) {
46  break;
47  }
48  }
49  }
50  void deallocCalled(size_t iActual) final {
51  nDeallocations_.fetch_add(1, std::memory_order_acq_rel);
52  auto present = presentActual_.load(std::memory_order_acquire);
53  if (present >= iActual) {
54  presentActual_.fetch_sub(iActual, std::memory_order_acq_rel);
55  }
56  }
57 
58  void performanceReport() const {
59  auto finalRequested = requested_.load(std::memory_order_acquire);
60  auto maxActual = maxActual_.load(std::memory_order_acquire);
61  auto present = presentActual_.load(std::memory_order_acquire);
62  auto allocs = nAllocations_.load(std::memory_order_acquire);
63  auto deallocs = nDeallocations_.load(std::memory_order_acquire);
64 
65  std::cerr << "Memory Report"
66  << "\n total memory requested: " << finalRequested << "\n max memory used: " << maxActual
67  << "\n presently used: " << present << "\n # allocations calls: " << allocs
68  << "\n # deallocations calls: " << deallocs << "\n";
69  }
70 
71  private:
72  std::atomic<size_t> requested_ = 0;
73  std::atomic<size_t> presentActual_ = 0;
74  std::atomic<size_t> maxActual_ = 0;
75  std::atomic<size_t> nAllocations_ = 0;
76  std::atomic<size_t> nDeallocations_ = 0;
77  };
78 
79  [[maybe_unused]] auto const* const s_instance =
81 } // namespace
virtual void deallocCalled(size_t iActualSize)=0
static AllocMonitorRegistry & instance()
virtual void allocCalled(size_t iRequestedSize, size_t iActualSize)=0
double a
Definition: hdecay.h:121