Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
stacktrace.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
22#include "shambase/string.hpp"
24#include <stack>
25
26namespace shambase::details {
27
33 f64 get_wtime();
34
35#ifdef SHAMROCK_USE_PROFILING
36
45
56
63 void dump_profilings(const std::string &process_prefix, u32 world_rank);
64
71 void dump_profilings_chrome(const std::string &process_prefix, u32 world_rank);
72
77
78#endif
79
84 inline std::stack<SourceLocation> call_stack;
85
88 inline CallStackEntry(SourceLocation &loc) {
89 // Push the source location to the call stack
90 call_stack.emplace(loc);
91 }
92
93 // This class is not safe if copied or moved
94
95 CallStackEntry(const CallStackEntry &) = delete;
96 CallStackEntry &operator=(const CallStackEntry &) = delete;
97 CallStackEntry(CallStackEntry &&) = delete;
98 CallStackEntry &operator=(CallStackEntry &&) = delete;
99
100 inline ~CallStackEntry() {
101 // Pop the source location from the call stack
102 call_stack.pop();
103 }
104 };
105
108 bool do_timer;
109
110#ifdef SHAMROCK_USE_PROFILING
112#endif
113
115
124#ifdef SHAMROCK_USE_PROFILING
125 if (do_timer) {
127 shambase::profiling::stack_entry_start(loc, wtime_start);
128 } else {
129 shambase::profiling::stack_entry_start_no_time(loc);
130 }
131#endif
132 }
133
143#ifdef SHAMROCK_USE_PROFILING
144 if (do_timer) {
146 shambase::profiling::stack_entry_start(loc, wtime_start);
147 } else {
148 shambase::profiling::stack_entry_start_no_time(loc);
149 }
150#endif
151 }
152
159#ifdef SHAMROCK_USE_PROFILING
160 if (do_timer) {
162 shambase::profiling::stack_entry_end(loc, wtime_start, wtime_end);
163 } else {
164 shambase::profiling::stack_entry_end_no_time(loc);
165 }
166#endif
167 }
168 };
169
172 bool do_timer;
173 std::string name;
174
175#ifdef SHAMROCK_USE_PROFILING
177#endif
178
180
189 std::string name, bool do_timer = true, SourceLocation &&loc = SourceLocation{})
191#ifdef SHAMROCK_USE_PROFILING
192 if (do_timer) {
194 shambase::profiling::stack_entry_start(loc, wtime_start, name);
195 } else {
196 shambase::profiling::stack_entry_start_no_time(loc, name);
197 }
198#endif
199 }
200
207#ifdef SHAMROCK_USE_PROFILING
208 if (do_timer) {
210 shambase::profiling::stack_entry_end(loc, wtime_start, wtime_end, name);
211 } else {
212 shambase::profiling::stack_entry_end_no_time(loc);
213 }
214#endif
215 }
216 };
217
218} // namespace shambase::details
219
220namespace shambase {
221
229 std::string fmt_callstack();
230
231 void set_callstack_process_identifier(std::string identifier);
232
233 void add_callstack_gen_info_generator(std::string (*generator)());
234
235} // namespace shambase
236
243
250
257#define __shamrock_stack_entry() \
258 [[maybe_unused]] StackEntry __shamrock_unique_name(stack_loc_) {}
259
266#define __shamrock_log_callsite(callsite) \
267 [[maybe_unused]] shambase::details::CallStackEntry __shamrock_unique_name(call_site_loc) { \
268 callsite \
269 }
270
277#define __shamrock_stack_entry_with_callsite(callsite) \
278 [[maybe_unused]] StackEntry __shamrock_unique_name(stack_loc_) { callsite }
Source location utility.
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
namespace for basic c++ utilities
std::string fmt_callstack()
Get the formatted callstack.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
std::stack< SourceLocation > call_stack
The call stack used to keep track of the stack trace. It is used to print the stack trace when an exc...
void register_profile_entry(std::source_location loc, f64 start_time, f64 end_time)
Register a profile entry. This register the end of a profile entry for chrome tracing and a complete ...
void dump_profilings(const std::string &process_prefix, u32 world_rank)
Dump the profiling data in a JSON format to a file.
void dump_profilings_chrome(const std::string &process_prefix, u32 world_rank)
Dump the profiling data in a Chrome Tracing format.
void register_profile_entry_start(std::source_location loc, f64 start_time)
Register the start of a profile entry. This is required for chrome profiling as there is a separate e...
f64 get_wtime()
Returns the current wall clock time in seconds.
void clear_profiling_data()
Clear the profiling data. (should be done in large run to avoid out-of-memory)
provide information about the source location
SourceLocation loc
Source location attached to the entry.
~BasicStackEntry()
Destroy the Basic Stack Entry object.
CallStackEntry scoped_callstack_entry
scoped call stack entry
BasicStackEntry(bool do_timer=true, SourceLocation &&loc=SourceLocation{})
Construct a new Basic Stack Entry object.
bool do_timer
is the timer enabled for this entry
BasicStackEntry(SourceLocation &callsite, bool do_timer=true, SourceLocation &&loc=SourceLocation{})
Construct a new Basic Stack Entry object.
f64 wtime_start
start time of the entry
Helper class to manage the call stack entry.
bool do_timer
is the timer enabled for this entry
SourceLocation loc
Source location attached to the entry.
CallStackEntry scoped_callstack_entry
scoped call stack entry
~NamedBasicStackEntry()
Destroy the Named Basic Stack Entry object.
NamedBasicStackEntry(std::string name, bool do_timer=true, SourceLocation &&loc=SourceLocation{})
Construct a new Named Basic Stack Entry object.
f64 wtime_start
start time of the entry
std::string name
Name of the entry.
Utility class to emulates std::source_location class introduced in c++20 This class provides informat...
Provides macros for generating unique identifiers at compile time.