timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
trace.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25/** \file "timemory/trace.hpp"
26 * Header file for library tracing operations
27 *
28 */
29
30#pragma once
31
32#include <atomic>
33#include <cstdint>
34#include <limits>
35#include <type_traits>
36#include <utility>
37
38//--------------------------------------------------------------------------------------//
39
40namespace tim
41{
42namespace trace
43{
44//
45//--------------------------------------------------------------------------------------//
46//
47/// \struct tim::trace::trace
48/// \brief Prevents recursion with a thread for tracing functions. See also \see
49/// tim::trace::lock
50///
51/// \code{.cpp}
52/// tim::trace::lock<tim::trace::trace> lk{};
53/// if(!lk)
54/// return;
55/// \endcode
56struct trace
57{
58 using type = std::true_type;
59 static constexpr bool value = true;
60};
61//
62/// \struct tim::trace::region
63/// \brief Prevents recursion within a thread for region functions. See also \see
64/// tim::trace::lock
65///
66/// \code{.cpp}
67/// tim::trace::lock<tim::trace::region> lk{};
68/// if(!lk)
69/// return;
70/// \endcode
71struct region
72{
73 using type = std::true_type;
74 static constexpr bool value = true;
75};
76//
77/// \struct tim::trace::region
78/// \brief Prevents recursion within a thread for library functions. See also \see
79/// tim::trace::lock
80///
81/// \code{.cpp}
82/// tim::trace::lock<tim::trace::library> lk{};
83/// if(!lk)
84/// return;
85/// \endcode
86struct library
87{
88 using type = std::true_type;
89 static constexpr bool value = true;
90};
91//
92/// \struct tim::trace::compiler
93/// \brief Prevents recursion within a thread for compiler instrumentation functions. See
94/// also \see tim::trace::lock
95///
96/// \code{.cpp}
97/// tim::trace::lock<tim::trace::compiler> lk{};
98/// if(!lk)
99/// return;
100/// \endcode
102{
103 using type = std::true_type;
104 static constexpr bool value = true;
105};
106//
107/// \struct tim::trace::region
108/// \brief Prevents recursion within a process for threading wrappers. See also \see
109/// tim::trace::lock
110///
111/// \code{.cpp}
112/// tim::trace::lock<tim::trace::threading> lk{};
113/// if(!lk)
114/// return;
115/// \endcode
117{
118 using type = std::false_type;
119 static constexpr bool value = false;
120};
121//
122//--------------------------------------------------------------------------------------//
123//
124/// \struct tim::trace::lock
125/// \brief A lightweight synchronization object for preventing recursion. The first
126/// template parameter should have a constexpr boolean indicating whether the lock
127/// is thread-local (Tp::value == true) or whether the lock is global (Tp::value == false)
128///
129/// \code{.cpp}
130/// tim::trace::lock<tim::trace::library> lk{};
131/// if(!lk)
132/// return;
133/// \endcode
134template <typename Tp, bool ThrLoc = Tp::value>
135struct lock;
136
137template <typename Tp>
138struct lock<Tp, true>
139{
141 : m_value(!get_global())
142 {
143 if(m_value)
144 get_global() = true;
145 }
146
148 {
149 if(m_value)
150 get_global() = false;
151 }
152
153 lock(lock&&) noexcept = default;
154 lock& operator=(lock&&) noexcept = default;
155
156 lock(const lock&) = delete;
157 lock& operator=(const lock&) = delete;
158
159 operator bool() const { return m_value; }
160
161 bool& get_local() { return m_value; }
162
163 bool release()
164 {
165 if(!m_value)
166 return false;
167
168 get_global() = false;
169 m_value = false;
170 return true;
171 }
172
173 bool acquire()
174 {
175 int64_t itr = 0;
176 while(!m_value)
177 {
178 if((m_value = !get_global()))
179 get_global() = true;
181 break;
182 }
183 return m_value;
184 }
185
186public:
187 static bool& get_global()
188 {
189 static thread_local bool _instance = false;
190 return _instance;
191 }
192
193private:
194 bool m_value;
195};
196//
197//--------------------------------------------------------------------------------------//
198//
199template <typename Tp>
200struct lock<Tp, false>
201{
203 : m_value(exchange(true))
204 {}
205
207 {
208 if(m_value)
209 exchange(false);
210 }
211
212 lock(lock&&) noexcept = default;
213 lock& operator=(lock&&) noexcept = default;
214
215 lock(const lock&) = delete;
216 lock& operator=(const lock&) = delete;
217
218 operator bool() const { return m_value; }
219
220 bool& get_local() { return m_value; }
221
222 bool release()
223 {
224 if(!m_value)
225 return false;
226
227 m_value = exchange(false);
228 return true;
229 }
230
231 bool acquire()
232 {
233 int64_t itr = 0;
234 if(!m_value)
235 {
236 while(!(m_value = exchange(true)))
237 {
239 break;
240 }
241 }
242 return m_value;
243 }
244
245public:
246 static auto load() { return get_global().load(std::memory_order_relaxed); }
247 static auto exchange(bool _value)
248 {
249 auto _load = load();
250 if(_load == _value)
251 return false;
252 return get_global().compare_exchange_strong(_load, _value,
253 std::memory_order_relaxed);
254 }
255
256 static std::atomic<bool>& get_global()
257 {
258 static std::atomic<bool> _instance{ false };
259 return _instance;
260 }
261
262private:
263 bool m_value;
264};
265//
266//--------------------------------------------------------------------------------------//
267//
268} // namespace trace
269} // namespace tim
270
271//--------------------------------------------------------------------------------------//
::tim::statistics< Tp > max(::tim::statistics< Tp > lhs, const Tp &rhs)
Definition: statistics.hpp:320
void load(Archive &ar, tim::node::graph< Tp > &d)
Definition: node.hpp:520
return false
Definition: definition.hpp:326
A lightweight synchronization object for preventing recursion. The first template parameter should ha...
Definition: trace.hpp:135
Definition: kokkosp.cpp:39
Prevents recursion within a thread for compiler instrumentation functions. See also.
Definition: trace.hpp:102
std::true_type type
Definition: trace.hpp:103
static constexpr bool value
Definition: trace.hpp:104
std::true_type type
Definition: trace.hpp:88
static constexpr bool value
Definition: trace.hpp:89
static auto exchange(bool _value)
Definition: trace.hpp:247
lock(lock &&) noexcept=default
static std::atomic< bool > & get_global()
Definition: trace.hpp:256
lock(lock &&) noexcept=default
static bool & get_global()
Definition: trace.hpp:187
Prevents recursion within a thread for region functions. See also.
Definition: trace.hpp:72
std::true_type type
Definition: trace.hpp:73
static constexpr bool value
Definition: trace.hpp:74
std::false_type type
Definition: trace.hpp:118
static constexpr bool value
Definition: trace.hpp:119
Prevents recursion with a thread for tracing functions. See also.
Definition: trace.hpp:57
static constexpr bool value
Definition: trace.hpp:59
std::true_type type
Definition: trace.hpp:58