|
| 1 | +#include "FWCore/AbstractServices/interface/IntrusiveMonitorBase.h" |
| 2 | +#include "FWCore/MessageLogger/interface/MessageLogger.h" |
| 3 | +#include "FWCore/ServiceRegistry/interface/ServiceMaker.h" |
| 4 | + |
| 5 | +#include "PerfTools/AllocMonitor/interface/AllocMonitorBase.h" |
| 6 | +#include "PerfTools/AllocMonitor/interface/AllocMonitorRegistry.h" |
| 7 | + |
| 8 | +#include "ThreadAllocInfo.h" |
| 9 | +#include "ThreadTracker.h" |
| 10 | + |
| 11 | +namespace { |
| 12 | + using namespace edm::service::moduleAlloc; |
| 13 | + class MonitorAdaptor : public cms::perftools::AllocMonitorBase { |
| 14 | + public: |
| 15 | + static void startOnThread() { threadAllocInfo().reset(); } |
| 16 | + static ThreadAllocInfo const& stopOnThread() { |
| 17 | + auto& t = threadAllocInfo(); |
| 18 | + if (not t.active_) { |
| 19 | + t.reset(); |
| 20 | + } else { |
| 21 | + t.deactivate(); |
| 22 | + } |
| 23 | + return t; |
| 24 | + } |
| 25 | + |
| 26 | + static ThreadAllocInfo& threadAllocInfo() { |
| 27 | + using namespace cms::perftools::allocMon; |
| 28 | + static ThreadAllocInfo s_info[ThreadTracker::kTotalEntries]; |
| 29 | + return s_info[ThreadTracker::instance().thread_index()]; |
| 30 | + } |
| 31 | + |
| 32 | + private: |
| 33 | + void allocCalled(size_t iRequested, size_t iActual, void const*) final { |
| 34 | + auto& allocInfo = threadAllocInfo(); |
| 35 | + if (not allocInfo.active_) { |
| 36 | + return; |
| 37 | + } |
| 38 | + allocInfo.nAllocations_ += 1; |
| 39 | + allocInfo.requested_ += iRequested; |
| 40 | + |
| 41 | + if (allocInfo.maxSingleAlloc_ < iRequested) { |
| 42 | + allocInfo.maxSingleAlloc_ = iRequested; |
| 43 | + } |
| 44 | + |
| 45 | + allocInfo.presentActual_ += iActual; |
| 46 | + if (allocInfo.presentActual_ > static_cast<long long>(allocInfo.maxActual_)) { |
| 47 | + allocInfo.maxActual_ = allocInfo.presentActual_; |
| 48 | + } |
| 49 | + } |
| 50 | + void deallocCalled(size_t iActual, void const*) final { |
| 51 | + auto& allocInfo = threadAllocInfo(); |
| 52 | + if (not allocInfo.active_) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + allocInfo.nDeallocations_ += 1; |
| 57 | + allocInfo.presentActual_ -= iActual; |
| 58 | + if (allocInfo.presentActual_ < 0) { |
| 59 | + if (allocInfo.minActual_ == 0 or allocInfo.minActual_ > allocInfo.presentActual_) { |
| 60 | + allocInfo.minActual_ = allocInfo.presentActual_; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + }; |
| 65 | +} // namespace |
| 66 | + |
| 67 | +class IntrusiveAllocMonitor : public edm::IntrusiveMonitorBase { |
| 68 | +public: |
| 69 | + IntrusiveAllocMonitor() { |
| 70 | + (void)cms::perftools::AllocMonitorRegistry::instance().createAndRegisterMonitor<MonitorAdaptor>(); |
| 71 | + }; |
| 72 | + ~IntrusiveAllocMonitor() override = default; |
| 73 | + |
| 74 | + void start() final { MonitorAdaptor::startOnThread(); } |
| 75 | + void stop(std::string_view name) final { |
| 76 | + MonitorAdaptor::stopOnThread(); |
| 77 | + auto& info = MonitorAdaptor::threadAllocInfo(); |
| 78 | + edm::LogSystem("IntrusiveAllocMonitor") |
| 79 | + .format("{}: requested {} added {} max alloc {} peak {} nAlloc {} nDealloc {}", |
| 80 | + name, |
| 81 | + info.requested_, |
| 82 | + info.presentActual_, |
| 83 | + info.maxSingleAlloc_, |
| 84 | + info.maxActual_, |
| 85 | + info.nAllocations_, |
| 86 | + info.nDeallocations_); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +typedef edm::serviceregistry::NoArgsMaker<edm::IntrusiveMonitorBase, IntrusiveAllocMonitor> IntrusiveAllocMonitorMaker; |
| 91 | +DEFINE_FWK_SERVICE_MAKER(IntrusiveAllocMonitor, IntrusiveAllocMonitorMaker); |
0 commit comments