File indexing completed on 2024-04-06 12:23:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <atomic>
0015 #include <iostream>
0016
0017
0018 #include "PerfTools/AllocMonitor/interface/AllocMonitorBase.h"
0019 #include "PerfTools/AllocMonitor/interface/AllocMonitorRegistry.h"
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 namespace {
0030 class MonitorAdaptor : public cms::perftools::AllocMonitorBase {
0031 public:
0032 MonitorAdaptor() noexcept = default;
0033 ~MonitorAdaptor() noexcept override { performanceReport(); }
0034
0035 private:
0036 void allocCalled(size_t iRequested, size_t iActual) final {
0037 nAllocations_.fetch_add(1, std::memory_order_acq_rel);
0038 requested_.fetch_add(iRequested, std::memory_order_acq_rel);
0039
0040 auto a = presentActual_.fetch_add(iActual, std::memory_order_acq_rel);
0041 a += iActual;
0042 auto max = maxActual_.load(std::memory_order_relaxed);
0043
0044 while (a > max) {
0045 if (maxActual_.compare_exchange_strong(max, a, std::memory_order_acq_rel)) {
0046 break;
0047 }
0048 }
0049 }
0050 void deallocCalled(size_t iActual) final {
0051 nDeallocations_.fetch_add(1, std::memory_order_acq_rel);
0052 auto present = presentActual_.load(std::memory_order_acquire);
0053 if (present >= iActual) {
0054 presentActual_.fetch_sub(iActual, std::memory_order_acq_rel);
0055 }
0056 }
0057
0058 void performanceReport() const {
0059 auto finalRequested = requested_.load(std::memory_order_acquire);
0060 auto maxActual = maxActual_.load(std::memory_order_acquire);
0061 auto present = presentActual_.load(std::memory_order_acquire);
0062 auto allocs = nAllocations_.load(std::memory_order_acquire);
0063 auto deallocs = nDeallocations_.load(std::memory_order_acquire);
0064
0065 std::cerr << "Memory Report: total memory requested: " << finalRequested
0066 << "\nMemory Report: max memory used: " << maxActual << "\nMemory Report: presently used: " << present
0067 << "\nMemory Report: # allocations calls: " << allocs
0068 << "\nMemory Report: # deallocations calls: " << deallocs << "\n";
0069 }
0070
0071 private:
0072 std::atomic<size_t> requested_ = 0;
0073 std::atomic<size_t> presentActual_ = 0;
0074 std::atomic<size_t> maxActual_ = 0;
0075 std::atomic<size_t> nAllocations_ = 0;
0076 std::atomic<size_t> nDeallocations_ = 0;
0077 };
0078
0079 [[maybe_unused]] auto const* const s_instance =
0080 cms::perftools::AllocMonitorRegistry::instance().createAndRegisterMonitor<MonitorAdaptor>();
0081 }