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