ATLAS Offline Software
Loading...
Searching...
No Matches
MemoryMonitorSvc.cxx
Go to the documentation of this file.
1//
2// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3//
4
5// Local include(s).
6#include "MemoryMonitorSvc.h"
7
8// System include(s).
9#include <algorithm>
10#include <format>
11#include <iomanip>
12
13namespace {
14
16std::string formatBytes(std::size_t bytes) {
17
18 if (bytes < 1024) {
19 return std::format("{} B", bytes);
20 } else if (bytes < 1024 * 1024) {
21 return std::format("{:.2f} kB", static_cast<double>(bytes) / 1024.0);
22 } else if (bytes < 1024 * 1024 * 1024) {
23 return std::format("{:.2f} MB",
24 static_cast<double>(bytes) / (1024.0 * 1024.0));
25 } else {
26 return std::format("{:.2f} GB",
27 static_cast<double>(bytes) / (1024.0 * 1024.0 * 1024.0));
28 }
29}
30
31} // namespace
32
33namespace AthDevice {
34
36
37 // Find the longest component name, to make the output more readable.
38 std::size_t longestComponentName = 0u;
39 auto helper = [&longestComponentName](const auto& map) {
40 for (const auto& [key, bytes] : map) {
41 longestComponentName =
42 std::max(longestComponentName, key.component.size());
43 }
44 };
45 helper(m_allocations);
46 helper(m_deallocations);
47
48 // Print the memory allocation monitoring data.
49 ATH_MSG_INFO("Memory allocations:");
50 std::string currentDevice = "";
51 for (const auto& [key, bytes] : m_allocations) {
52 if (key.device != currentDevice) {
53 ATH_MSG_INFO(" - " << key.device);
54 currentDevice = key.device;
55 }
56 ATH_MSG_INFO(" * " << std::setw(longestComponentName) << std::left
57 << key.component << " : " << std::setw(15)
58 << std::right << formatBytes(bytes));
59 }
60
61 // Print the memory deallocation monitoring data.
62 ATH_MSG_INFO("Memory deallocations:");
63 currentDevice = "";
64 for (const auto& [key, bytes] : m_deallocations) {
65 if (key.device != currentDevice) {
66 ATH_MSG_INFO(" - " << key.device);
67 currentDevice = key.device;
68 }
69 ATH_MSG_INFO(" * " << std::setw(longestComponentName) << std::left
70 << key.component << " : " << std::setw(15)
71 << std::right << formatBytes(bytes));
72 }
73
74 // TODO: Print the memory copy monitoring data.
75
76 // Return gracefully.
77 return StatusCode::SUCCESS;
78}
79
80void MemoryMonitorSvc::memoryAllocated(const std::string& component,
81 const std::string& device,
82 std::size_t bytes) {
83
84 std::lock_guard lock(m_allocationsMutex);
85 m_allocations[{component, device}] += bytes;
86}
87
88void MemoryMonitorSvc::memoryDeallocated(const std::string& component,
89 const std::string& device,
90 std::size_t bytes) {
91
92 std::lock_guard lock(m_deallocationsMutex);
93 m_deallocations[{component, device}] += bytes;
94}
95
96void MemoryMonitorSvc::memoryCopied(const std::string& component,
97 vecmem::copy::type::copy_type type,
98 std::size_t bytes) {
99
100 std::lock_guard lock(m_copiesMutex);
101 m_copies[{component, type}] += bytes;
102}
103
104} // namespace AthDevice
#define ATH_MSG_INFO(x,...)
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
virtual void memoryDeallocated(const std::string &component, const std::string &device, std::size_t bytes) override
Notify the service that a memory deallocation has been made.
std::map< MemoryCopyKey, std::size_t > m_copies
All the memory copies.
virtual void memoryCopied(const std::string &component, vecmem::copy::type::copy_type type, std::size_t bytes) override
Notify the service that a memory copy has been made.
std::map< MemoryAllocKey, std::size_t > m_deallocations
All the memory deallocations.
virtual StatusCode finalize() override
Finalize the service.
std::mutex m_deallocationsMutex
Mutex for m_deallocations.
std::mutex m_copiesMutex
Mutex for m_copies.
std::mutex m_allocationsMutex
Mutex for m_allocations.
std::map< MemoryAllocKey, std::size_t > m_allocations
All the memory allocations.
virtual void memoryAllocated(const std::string &component, const std::string &device, std::size_t bytes) override
Notify the service that a memory allocation has been made.
STL class.