Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:21

0001 // -*- C++ -*-
0002 //
0003 // Package:     PerfTools/MaxMemoryPreload
0004 // Class  :     preload
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Wed, 23 Aug 2023 17:56:44 GMT
0011 //
0012 
0013 // system include files
0014 #include <atomic>
0015 #include <iostream>
0016 
0017 // user include files
0018 #include "PerfTools/AllocMonitor/interface/AllocMonitorBase.h"
0019 #include "PerfTools/AllocMonitor/interface/AllocMonitorRegistry.h"
0020 
0021 //
0022 // constants, enums and typedefs
0023 //
0024 
0025 //
0026 // static data member definitions
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 }  // namespace