CMS 3D CMS Logo

SimpleAllocMonitor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PerfTools/AllocMonitor
4 // Class : SimpleAllocMonitor
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Mon, 21 Aug 2023 20:31:57 GMT
11 //
12 
13 // system include files
14 #include <atomic>
15 
16 // user include files
22 
23 namespace {
24  class MonitorAdaptor : public cms::perftools::AllocMonitorBase {
25  public:
26  void allocCalled(size_t iRequested, size_t iActual, void const*) final {
27  nAllocations_.fetch_add(1, std::memory_order_acq_rel);
28  requested_.fetch_add(iRequested, std::memory_order_acq_rel);
29 
30  //returns previous value
31  auto a = presentActual_.fetch_add(iActual, std::memory_order_acq_rel);
32  a += iActual;
33  auto max = maxActual_.load(std::memory_order_relaxed);
34  while (a > max) {
35  if (maxActual_.compare_exchange_strong(max, a, std::memory_order_acq_rel)) {
36  break;
37  }
38  }
39  }
40  void deallocCalled(size_t iActual, void const*) final {
41  nDeallocations_.fetch_add(1, std::memory_order_acq_rel);
42  auto present = presentActual_.load(std::memory_order_acquire);
43  if (present >= iActual) {
44  presentActual_.fetch_sub(iActual, std::memory_order_acq_rel);
45  }
46  }
47 
48  void performanceReport() const {
49  auto finalRequested = requested_.load(std::memory_order_acquire);
50  auto maxActual = maxActual_.load(std::memory_order_acquire);
51  auto allocs = nAllocations_.load(std::memory_order_acquire);
52  auto deallocs = nDeallocations_.load(std::memory_order_acquire);
53 
54  edm::LogSystem("SimpleAllocMonitor")
55  << "Memory Report"
56  << "\n [monitoring spans the lifetime of Services (first plugins made and last to be deleted)]"
57  << "\n total additional memory requested: " << finalRequested
58  << "\n max additional memory used: " << maxActual << "\n # allocations calls: " << allocs
59  << "\n # deallocations calls: " << deallocs;
60  }
61 
62  private:
63  std::atomic<size_t> requested_ = 0;
64  std::atomic<size_t> presentActual_ = 0;
65  std::atomic<size_t> maxActual_ = 0;
66  std::atomic<size_t> nAllocations_ = 0;
67  std::atomic<size_t> nDeallocations_ = 0;
68  };
69 
70 } // namespace
71 
73 public:
75  : adaptor_(cms::perftools::AllocMonitorRegistry::instance().createAndRegisterMonitor<MonitorAdaptor>()) {}
76 
78  adaptor_->performanceReport();
80  }
81 
82  MonitorAdaptor* adaptor_;
83 };
84 
Log< level::System, false > LogSystem
#define DEFINE_FWK_SERVICE_MAKER(concrete, maker)
Definition: ServiceMaker.h:102
static PFTauRenderPlugin instance
virtual void allocCalled(size_t iRequestedSize, size_t iActualSize, void const *iAddress)=0
static AllocMonitorRegistry & instance()
Namespace of DDCMS conversion namespace.
MonitorAdaptor * adaptor_
virtual void deallocCalled(size_t iActualSize, void const *iAddress)=0
double a
Definition: hdecay.h:121