Arcane  4.2.1.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#include <iostream>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::Accelerator::Impl
31{
32
34{
35 struct Info
36 {
37 Int64 length;
38 String name;
39 String stack;
40 };
41
42 public:
43
44 void add(Span<const std::byte> bytes, const String& name, const String& stack_trace, [[maybe_unused]] Int64 timestamp)
45 {
46 const void* ptr = bytes.data();
47 m_infos_map.insert(std::make_pair(ptr, Info{ bytes.size(), name, stack_trace }));
48 }
49
50 void remove(void* ptr, [[maybe_unused]] const String& name, [[maybe_unused]] const String& stack_trace, [[maybe_unused]] Int64 timestamp)
51 {
52 auto x = m_infos_map.find(ptr);
53 if (x != m_infos_map.end())
54 m_infos_map.erase(x);
55 }
56
57 std::pair<String, String> find(const void* ptr)
58 {
59 auto x = m_infos_map.lower_bound(ptr);
60 if (x != m_infos_map.end())
61 return std::make_pair(x->second.name, x->second.stack);
62 return {};
63 }
64
65 private:
66
67 std::map<const void*, Info> m_infos_map;
68};
69
70namespace
71{
72 MemoryTracerMng m_memory_tracer_mng;
73}
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78void MemoryTracer::
79notifyMemoryAllocation(Span<const std::byte> bytes, const String& name,
80 const String& stack_trace, Int64 timestamp)
81{
82 // TODO: make thread-safe
83 m_memory_tracer_mng.add(bytes, name, stack_trace, timestamp);
84}
85
86void MemoryTracer::
87notifyMemoryFree(void* ptr, const String& name, const String& stack_trace, Int64 timestamp)
88{
89 // TODO: make thread-safe
90 m_memory_tracer_mng.remove(ptr, name, stack_trace, timestamp);
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96std::pair<String, String> MemoryTracer::
97findMemory(const void* ptr)
98{
99 return m_memory_tracer_mng.find(ptr);
100}
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
105MemoryTracerWrapper::
106MemoryTracerWrapper()
107{
108}
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113void MemoryTracerWrapper::
114traceDeallocate(const AllocatedMemoryInfo& mem_info, const MemoryAllocationArgs& args)
115{
116 if (!isActive())
117 return;
118
119 void* ptr = mem_info.baseAddress();
120 // Uses a specific stream to ensure that outputs are not mixed
121 // in case of multi-threading
122 std::ostringstream ostr;
123 if (m_trace_level >= 2)
124 ostr << "FREE_MANAGED=" << ptr << " size=" << mem_info.capacity() << " name=" << args.arrayName();
125 String s;
126 if (m_trace_level >= 3) {
128 if (m_trace_level >= 4) {
129 ostr << " stack=" << s;
130 }
131 }
132 MemoryTracer::notifyMemoryFree(ptr, args.arrayName(), s, 0);
133 if (m_trace_level >= 2) {
134 ostr << "\n";
135 std::cout << ostr.str();
136 }
137}
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
142void MemoryTracerWrapper::
143traceAllocate(void* p, size_t new_size, MemoryAllocationArgs args)
144{
145 if (!isActive())
146 return;
147
148 // Uses a specific stream to ensure that outputs are not mixed
149 // in case of multi-threading
150 std::ostringstream ostr;
151 if (m_trace_level >= 2)
152 ostr << "MALLOC_MANAGED=" << p << " size=" << new_size << " name=" << args.arrayName();
153 String s;
154 if (m_trace_level >= 3) {
156 if (m_trace_level >= 4) {
157 ostr << " stack=" << s;
158 }
159 }
160 Span<const std::byte> bytes(reinterpret_cast<std::byte*>(p), new_size);
161 MemoryTracer::notifyMemoryAllocation(bytes, args.arrayName(), s, 0);
162 if (m_trace_level >= 2) {
163 ostr << "\n";
164 std::cout << ostr.str();
165 }
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171} // namespace Arcane::Accelerator::Impl
172
173/*---------------------------------------------------------------------------*/
174/*---------------------------------------------------------------------------*/
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:537
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:325
View of an array of elements of type T.
Definition Span.h:633
String getStackTrace()
Returns a string containing the call stack.
std::int64_t Int64
Signed integer type of 64 bits.