ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthDevice
AthDeviceComps
src
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
13
namespace
{
14
16
std::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
33
namespace
AthDevice
{
34
35
StatusCode
MemoryMonitorSvc::finalize
() {
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
80
void
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
88
void
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
96
void
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
ATH_MSG_INFO
#define ATH_MSG_INFO(x,...)
Definition
AthMsgStreamMacros.h:45
lock
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
MemoryMonitorSvc.h
AthDevice::MemoryMonitorSvc::memoryDeallocated
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.
Definition
MemoryMonitorSvc.cxx:88
AthDevice::MemoryMonitorSvc::m_copies
std::map< MemoryCopyKey, std::size_t > m_copies
All the memory copies.
Definition
MemoryMonitorSvc.h:104
AthDevice::MemoryMonitorSvc::memoryCopied
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.
Definition
MemoryMonitorSvc.cxx:96
AthDevice::MemoryMonitorSvc::m_deallocations
std::map< MemoryAllocKey, std::size_t > m_deallocations
All the memory deallocations.
Definition
MemoryMonitorSvc.h:82
AthDevice::MemoryMonitorSvc::finalize
virtual StatusCode finalize() override
Finalize the service.
Definition
MemoryMonitorSvc.cxx:35
AthDevice::MemoryMonitorSvc::m_deallocationsMutex
std::mutex m_deallocationsMutex
Mutex for m_deallocations.
Definition
MemoryMonitorSvc.h:84
AthDevice::MemoryMonitorSvc::m_copiesMutex
std::mutex m_copiesMutex
Mutex for m_copies.
Definition
MemoryMonitorSvc.h:106
AthDevice::MemoryMonitorSvc::m_allocationsMutex
std::mutex m_allocationsMutex
Mutex for m_allocations.
Definition
MemoryMonitorSvc.h:80
AthDevice::MemoryMonitorSvc::m_allocations
std::map< MemoryAllocKey, std::size_t > m_allocations
All the memory allocations.
Definition
MemoryMonitorSvc.h:78
AthDevice::MemoryMonitorSvc::memoryAllocated
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.
Definition
MemoryMonitorSvc.cxx:80
map
STL class.
AthDevice
Definition
BinaryPageMemoryResourceSvc.cxx:15
type
Generated on
for ATLAS Offline Software by
1.17.0