CMS 3D CMS Logo

EventProcessingAllocMonitor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PerfTools/AllocMonitor
4 // Class : EventProcessingAllocMonitor
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 performanceReport() {
27  started_.store(false, std::memory_order_release);
28 
29  auto finalRequested = requested_.load(std::memory_order_acquire);
30  auto maxActual = maxActual_.load(std::memory_order_acquire);
31  auto present = presentActual_.load(std::memory_order_acquire);
32  auto allocs = nAllocations_.load(std::memory_order_acquire);
33  auto deallocs = nDeallocations_.load(std::memory_order_acquire);
34 
35  edm::LogSystem("EventProcessingAllocMonitor")
36  << "Event Processing Memory Report"
37  << "\n total additional memory requested: " << finalRequested
38  << "\n max additional memory used: " << maxActual
39  << "\n total additional memory not deallocated: " << present << "\n # allocations calls: " << allocs
40  << "\n # deallocations calls: " << deallocs;
41  }
42 
43  void start() { started_.store(true, std::memory_order_release); }
44 
45  private:
46  void allocCalled(size_t iRequested, size_t iActual, void const*) final {
47  if (not started_.load(std::memory_order_acquire)) {
48  return;
49  }
50  nAllocations_.fetch_add(1, std::memory_order_acq_rel);
51  requested_.fetch_add(iRequested, std::memory_order_acq_rel);
52 
53  //returns previous value
54  auto a = presentActual_.fetch_add(iActual, std::memory_order_acq_rel);
55  a += iActual;
56 
57  auto max = maxActual_.load(std::memory_order_relaxed);
58  while (a > max) {
59  if (maxActual_.compare_exchange_strong(max, a, std::memory_order_acq_rel)) {
60  break;
61  }
62  }
63  }
64  void deallocCalled(size_t iActual, void const*) final {
65  if (not started_.load(std::memory_order_acquire)) {
66  return;
67  }
68  nDeallocations_.fetch_add(1, std::memory_order_acq_rel);
69  auto present = presentActual_.load(std::memory_order_acquire);
70  if (present >= iActual) {
71  presentActual_.fetch_sub(iActual, std::memory_order_acq_rel);
72  }
73  }
74 
75  std::atomic<size_t> requested_ = 0;
76  std::atomic<size_t> presentActual_ = 0;
77  std::atomic<size_t> maxActual_ = 0;
78  std::atomic<size_t> nAllocations_ = 0;
79  std::atomic<size_t> nDeallocations_ = 0;
80 
81  std::atomic<bool> started_ = false;
82  };
83 
84 } // namespace
85 
87 public:
90 
91  iAR.postBeginJobSignal_.connect([adaptor]() { adaptor->start(); });
92  iAR.preEndJobSignal_.connect([adaptor]() {
93  adaptor->performanceReport();
95  });
96  }
97 };
98 
Log< level::System, false > LogSystem
virtual void allocCalled(size_t iRequestedSize, size_t iActualSize, void const *iAddress)=0
static AllocMonitorRegistry & instance()
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
virtual void deallocCalled(size_t iActualSize, void const *iAddress)=0
double a
Definition: hdecay.h:121
EventProcessingAllocMonitor(edm::ParameterSet const &iPS, edm::ActivityRegistry &iAR)
PostBeginJob postBeginJobSignal_
signal is emitted after all modules have gotten their beginJob called
void connect(U iFunc)
Definition: Signal.h:74
PreEndJob preEndJobSignal_
signal is emitted before any modules have gotten their endJob called