Arcane  v4.1.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
MemoryTracer.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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-2025 */
9/* */
10/* Utilitaires pour tracer les accès mémoire entre l'accélérateur et l'hôte. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/accelerator/core/internal/MemoryTracer.h"
15
16#include "arccore/base/PlatformUtils.h"
17#include "arccore/base/ArrayView.h"
18#include "arccore/base/String.h"
19
20#include "arccore/common/AllocatedMemoryInfo.h"
21#include "arccore/common/IMemoryAllocator.h"
22
23#include <map>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane::Accelerator::impl
29{
30
32{
33 struct Info
34 {
35 Int64 length;
36 String name;
37 String stack;
38 };
39
40 public:
41
42 void add(Span<const std::byte> bytes, const String& name, const String& stack_trace, [[maybe_unused]] Int64 timestamp)
43 {
44 const void* ptr = bytes.data();
45 m_infos_map.insert(std::make_pair(ptr, Info{ bytes.size(), name, stack_trace }));
46 }
47
48 void remove(void* ptr, [[maybe_unused]] const String& name, [[maybe_unused]] const String& stack_trace, [[maybe_unused]] Int64 timestamp)
49 {
50 auto x = m_infos_map.find(ptr);
51 if (x != m_infos_map.end())
52 m_infos_map.erase(x);
53 }
54
55 std::pair<String, String> find(const void* ptr)
56 {
57 auto x = m_infos_map.lower_bound(ptr);
58 if (x != m_infos_map.end())
59 return std::make_pair(x->second.name, x->second.stack);
60 return {};
61 }
62
63 private:
64
65 std::map<const void*, Info> m_infos_map;
66};
67
68namespace
69{
70 MemoryTracerMng m_memory_tracer_mng;
71}
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
76void MemoryTracer::
77notifyMemoryAllocation(Span<const std::byte> bytes, const String& name,
78 const String& stack_trace, Int64 timestamp)
79{
80 // TODO: rendre thread-safe
81 m_memory_tracer_mng.add(bytes, name, stack_trace, timestamp);
82}
83
84void MemoryTracer::
85notifyMemoryFree(void* ptr, const String& name, const String& stack_trace, Int64 timestamp)
86{
87 // TODO: rendre thread-safe
88 m_memory_tracer_mng.remove(ptr, name, stack_trace, timestamp);
89}
90
91/*---------------------------------------------------------------------------*/
92/*---------------------------------------------------------------------------*/
93
94std::pair<String, String> MemoryTracer::
95findMemory(const void* ptr)
96{
97 return m_memory_tracer_mng.find(ptr);
98}
99
100/*---------------------------------------------------------------------------*/
101/*---------------------------------------------------------------------------*/
102
103MemoryTracerWrapper::
104MemoryTracerWrapper()
105{
106}
107
108/*---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
110
111void MemoryTracerWrapper::
112traceDeallocate(const AllocatedMemoryInfo& mem_info, const MemoryAllocationArgs& args)
113{
114 if (!isActive())
115 return;
116
117 void* ptr = mem_info.baseAddress();
118 // Utilise un flux spécifique pour être sur que les affichages ne seront pas mélangés
119 // en cas de multi-threading
120 std::ostringstream ostr;
121 if (m_trace_level >= 2)
122 ostr << "FREE_MANAGED=" << ptr << " size=" << mem_info.capacity() << " name=" << args.arrayName();
123 String s;
124 if (m_trace_level >= 3) {
126 if (m_trace_level >= 4) {
127 ostr << " stack=" << s;
128 }
129 }
130 impl::MemoryTracer::notifyMemoryFree(ptr, args.arrayName(), s, 0);
131 if (m_trace_level >= 2) {
132 ostr << "\n";
133 std::cout << ostr.str();
134 }
135}
136
137/*---------------------------------------------------------------------------*/
138/*---------------------------------------------------------------------------*/
139
140void MemoryTracerWrapper::
141traceAllocate(void* p, size_t new_size, MemoryAllocationArgs args)
142{
143 if (!isActive())
144 return;
145
146 // Utilise un flux spécifique pour être sur que les affichages ne seront pas mélangés
147 // en cas de multi-threading
148 std::ostringstream ostr;
149 if (m_trace_level >= 2)
150 ostr << "MALLOC_MANAGED=" << p << " size=" << new_size << " name=" << args.arrayName();
151 String s;
152 if (m_trace_level >= 3) {
154 if (m_trace_level >= 4) {
155 ostr << " stack=" << s;
156 }
157 }
158 Span<const std::byte> bytes(reinterpret_cast<std::byte*>(p), new_size);
159 impl::MemoryTracer::notifyMemoryAllocation(bytes, args.arrayName(), s, 0);
160 if (m_trace_level >= 2) {
161 ostr << "\n";
162 std::cout << ostr.str();
163 }
164}
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169} // namespace Arcane::Accelerator::impl
170
171/*---------------------------------------------------------------------------*/
172/*---------------------------------------------------------------------------*/
constexpr __host__ __device__ pointer data() const noexcept
Pointeur sur le début de la vue.
Definition Span.h:516
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:304
Vue d'un tableau d'éléments de type T.
Definition Span.h:612
Chaîne de caractères unicode.
ARCCORE_BASE_EXPORT String getStackTrace()
Retourne une chaîne de caractere contenant la pile d'appel.
std::int64_t Int64
Type entier signé sur 64 bits.