File indexing completed on 2024-10-23 22:48:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <atomic>
0015
0016
0017 #include "PerfTools/AllocMonitor/interface/AllocMonitorBase.h"
0018 #include "PerfTools/AllocMonitor/interface/AllocMonitorRegistry.h"
0019 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0021 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
0022
0023 namespace {
0024 class MonitorAdaptor : public cms::perftools::AllocMonitorBase {
0025 public:
0026 void performanceReport() {
0027 started_.store(false, std::memory_order_release);
0028
0029 auto finalRequested = requested_.load(std::memory_order_acquire);
0030 auto maxActual = maxActual_.load(std::memory_order_acquire);
0031 auto present = presentActual_.load(std::memory_order_acquire);
0032 auto allocs = nAllocations_.load(std::memory_order_acquire);
0033 auto deallocs = nDeallocations_.load(std::memory_order_acquire);
0034
0035 edm::LogSystem("EventProcessingAllocMonitor")
0036 << "Event Processing Memory Report"
0037 << "\n total additional memory requested: " << finalRequested
0038 << "\n max additional memory used: " << maxActual
0039 << "\n total additional memory not deallocated: " << present << "\n # allocations calls: " << allocs
0040 << "\n # deallocations calls: " << deallocs;
0041 }
0042
0043 void start() { started_.store(true, std::memory_order_release); }
0044
0045 private:
0046 void allocCalled(size_t iRequested, size_t iActual, void const*) final {
0047 if (not started_.load(std::memory_order_acquire)) {
0048 return;
0049 }
0050 nAllocations_.fetch_add(1, std::memory_order_acq_rel);
0051 requested_.fetch_add(iRequested, std::memory_order_acq_rel);
0052
0053
0054 auto a = presentActual_.fetch_add(iActual, std::memory_order_acq_rel);
0055 a += iActual;
0056
0057 auto max = maxActual_.load(std::memory_order_relaxed);
0058 while (a > max) {
0059 if (maxActual_.compare_exchange_strong(max, a, std::memory_order_acq_rel)) {
0060 break;
0061 }
0062 }
0063 }
0064 void deallocCalled(size_t iActual, void const*) final {
0065 if (not started_.load(std::memory_order_acquire)) {
0066 return;
0067 }
0068 nDeallocations_.fetch_add(1, std::memory_order_acq_rel);
0069 auto present = presentActual_.load(std::memory_order_acquire);
0070 if (present >= iActual) {
0071 presentActual_.fetch_sub(iActual, std::memory_order_acq_rel);
0072 }
0073 }
0074
0075 std::atomic<size_t> requested_ = 0;
0076 std::atomic<size_t> presentActual_ = 0;
0077 std::atomic<size_t> maxActual_ = 0;
0078 std::atomic<size_t> nAllocations_ = 0;
0079 std::atomic<size_t> nDeallocations_ = 0;
0080
0081 std::atomic<bool> started_ = false;
0082 };
0083
0084 }
0085
0086 class EventProcessingAllocMonitor {
0087 public:
0088 EventProcessingAllocMonitor(edm::ParameterSet const& iPS, edm::ActivityRegistry& iAR) {
0089 auto adaptor = cms::perftools::AllocMonitorRegistry::instance().createAndRegisterMonitor<MonitorAdaptor>();
0090
0091 iAR.postBeginJobSignal_.connect([adaptor]() { adaptor->start(); });
0092 iAR.preEndJobSignal_.connect([adaptor]() {
0093 adaptor->performanceReport();
0094 cms::perftools::AllocMonitorRegistry::instance().deregisterMonitor(adaptor);
0095 });
0096 }
0097 };
0098
0099 DEFINE_FWK_SERVICE(EventProcessingAllocMonitor);