Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
MemoryTracer.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MemoryTracer.cc (C) 2000-2026 */
9/* */
10/* Utilities for tracing memory accesses between the accelerator and the */
11/* host. */
12/*---------------------------------------------------------------------------*/
13/*---------------------------------------------------------------------------*/
14
15#include "arccore/common/accelerator/internal/MemoryTracer.h"
16
17#include "arccore/base/PlatformUtils.h"
19#include "arccore/base/String.h"
20
21#include "arccore/common/AllocatedMemoryInfo.h"
22#include "arccore/common/IMemoryAllocator.h"
23
24#include <map>
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane::Accelerator::Impl
30{
31
33{
34 struct Info
35 {
36 Int64 length;
37 String name;
38 String stack;
39 };
40
41 public:
42
43 void add(Span<const std::byte> bytes, const String& name, const String& stack_trace, [[maybe_unused]] Int64 timestamp)
44 {
45 const void* ptr = bytes.data();
46 m_infos_map.insert(std::make_pair(ptr, Info{ bytes.size(), name, stack_trace }));
47 }
48
49 void remove(void* ptr, [[maybe_unused]] const String& name, [[maybe_unused]] const String& stack_trace, [[maybe_unused]] Int64 timestamp)
50 {
51 auto x = m_infos_map.find(ptr);
52 if (x != m_infos_map.end())
53 m_infos_map.erase(x);
54 }
55
56 std::pair<String, String> find(const void* ptr)
57 {
58 auto x = m_infos_map.lower_bound(ptr);
59 if (x != m_infos_map.end())
60 return std::make_pair(x->second.name, x->second.stack);
61 return {};
62 }
63
64 private:
65
66 std::map<const void*, Info> m_infos_map;
67};
68
69namespace
70{
71 MemoryTracerMng m_memory_tracer_mng;
72}
73
74/*---------------------------------------------------------------------------*/
75/*---------------------------------------------------------------------------*/
76
77void MemoryTracer::
78notifyMemoryAllocation(Span<const std::byte> bytes, const String& name,
79 const String& stack_trace, Int64 timestamp)
80{
81 // TODO: make thread-safe
82 m_memory_tracer_mng.add(bytes, name, stack_trace, timestamp);
83}
84
85void MemoryTracer::
86notifyMemoryFree(void* ptr, const String& name, const String& stack_trace, Int64 timestamp)
87{
88 // TODO: make thread-safe
89 m_memory_tracer_mng.remove(ptr, name, stack_trace, timestamp);
90}
91
92/*---------------------------------------------------------------------------*/
93/*---------------------------------------------------------------------------*/
94
95std::pair<String, String> MemoryTracer::
96findMemory(const void* ptr)
97{
98 return m_memory_tracer_mng.find(ptr);
99}
100
101/*---------------------------------------------------------------------------*/
102/*---------------------------------------------------------------------------*/
103
104MemoryTracerWrapper::
105MemoryTracerWrapper()
106{
107}
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112void MemoryTracerWrapper::
113traceDeallocate(const AllocatedMemoryInfo& mem_info, const MemoryAllocationArgs& args)
114{
115 if (!isActive())
116 return;
117
118 void* ptr = mem_info.baseAddress();
119 // Uses a specific stream to ensure that outputs are not mixed
120 // in case of multi-threading
121 std::ostringstream ostr;
122 if (m_trace_level >= 2)
123 ostr << "FREE_MANAGED=" << ptr << " size=" << mem_info.capacity() << " name=" << args.arrayName();
124 String s;
125 if (m_trace_level >= 3) {
127 if (m_trace_level >= 4) {
128 ostr << " stack=" << s;
129 }
130 }
131 MemoryTracer::notifyMemoryFree(ptr, args.arrayName(), s, 0);
132 if (m_trace_level >= 2) {
133 ostr << "\n";
134 std::cout << ostr.str();
135 }
136}
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
140
141void MemoryTracerWrapper::
142traceAllocate(void* p, size_t new_size, MemoryAllocationArgs args)
143{
144 if (!isActive())
145 return;
146
147 // Uses a specific stream to ensure that outputs are not mixed
148 // in case of multi-threading
149 std::ostringstream ostr;
150 if (m_trace_level >= 2)
151 ostr << "MALLOC_MANAGED=" << p << " size=" << new_size << " name=" << args.arrayName();
152 String s;
153 if (m_trace_level >= 3) {
155 if (m_trace_level >= 4) {
156 ostr << " stack=" << s;
157 }
158 }
159 Span<const std::byte> bytes(reinterpret_cast<std::byte*>(p), new_size);
160 MemoryTracer::notifyMemoryAllocation(bytes, args.arrayName(), s, 0);
161 if (m_trace_level >= 2) {
162 ostr << "\n";
163 std::cout << ostr.str();
164 }
165}
166
167/*---------------------------------------------------------------------------*/
168/*---------------------------------------------------------------------------*/
169
170} // namespace Arcane::Accelerator::Impl
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
Types and functions associated with the classes ArrayView and ConstArrayView.
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:539
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
View of an array of elements of type T.
Definition Span.h:635
String getStackTrace()
Returns a string containing the call stack.
std::int64_t Int64
Signed integer type of 64 bits.