Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
profiling.cpp
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
19#include "shambase/string.hpp"
20#include "fmt/base.h"
21#include <fstream>
22#include <iostream>
23#include <utility>
24
25#ifdef SHAMROCK_USE_NVTX
26 #include <nvtx3/nvtx3.hpp>
27#endif
28
29std::string src_loc_to_name(const SourceLocation &loc) {
30 return fmt::format(
31 "{} ({}:{}:{})",
32 loc.loc.function_name(),
33 loc.loc.file_name(),
34 loc.loc.line(),
35 loc.loc.column());
36}
37
38auto get_profiling = []() {
39 const char *val = std::getenv("SHAM_PROFILING");
40 if (val != nullptr) {
41 if (std::string(val) == "1") {
42 return true;
43 } else if (std::string(val) == "0") {
44 return false;
45 }
46 }
47 return false;
48};
49
50auto get_nvtx = []() {
51 const char *val = std::getenv("SHAM_PROF_USE_NVTX");
52 if (val != nullptr) {
53 if (std::string(val) == "1") {
54
55 if (!get_profiling()) {
56 fmt::println(
57 "-- SHAM_PROF_USE_NVTX is set to 1 but SHAM_PROFILING is not set to 1.\n"
58 " please set SHAM_PROFILING=1 before SHAM_PROF_USE_NVTX=1");
59 }
60
61 return true;
62 } else if (std::string(val) == "0") {
63 return false;
64 }
65 }
66 return false;
67};
68
69auto get_complete_event = []() {
70 const char *val = std::getenv("SHAM_PROF_USE_COMPLETE_EVENT");
71 if (val != nullptr) {
72 if (std::string(val) == "1") {
73 return true;
74 } else if (std::string(val) == "0") {
75 return false;
76 }
77 }
78 return true;
79};
80
81auto get_threshold = []() {
82 const char *val = std::getenv("SHAM_PROF_EVENT_RECORD_THRES");
83 if (val != nullptr) {
84 return std::stod(val);
85 }
86 return 1e-5;
87};
88
89bool enable_profiling = get_profiling();
90bool use_complete_event = get_complete_event();
91f64 threshold = get_threshold();
92bool enable_nvtx = get_nvtx();
93
94bool shambase::profiling::is_profiling_enabled() { return enable_profiling; }
95
96void shambase::profiling::set_enable_nvtx(bool enable) { enable_nvtx = enable; }
97void shambase::profiling::set_enable_profiling(bool enable) { enable_profiling = enable; }
98void shambase::profiling::set_use_complete_event(bool enable) { use_complete_event = enable; }
99void shambase::profiling::set_event_record_threshold(f64 threshold_) { threshold = threshold_; }
100
102 const SourceLocation &fileloc,
103 f64 t_start,
104 const std::optional<std::string> &name,
105 const std::optional<std::string> &category_name) {
106
107 if (enable_profiling) {
108
109 if (!use_complete_event) {
110 chrome::register_event_start(
111 src_loc_to_name(fileloc), fileloc.loc.function_name(), t_start, 0, 0);
112 }
113 }
114 stack_entry_start_no_time(fileloc, name, category_name);
115}
116
118 const SourceLocation &fileloc,
119 f64 t_start,
120 f64 tend,
121 const std::optional<std::string> &name,
122 const std::optional<std::string> &category_name) {
123
124 if (enable_profiling) {
125 if (use_complete_event) {
126 if (tend - t_start > threshold) {
127 chrome::register_event_complete(
128 src_loc_to_name(fileloc), fileloc.loc.function_name(), t_start, tend, 0, 0);
129 }
130 } else {
131 chrome::register_event_end(
132 src_loc_to_name(fileloc), fileloc.loc.function_name(), tend, 0, 0);
133 }
134 }
135 stack_entry_end_no_time(fileloc, name, category_name);
136}
137
139 const SourceLocation &fileloc,
140 const std::optional<std::string> &name,
141 const std::optional<std::string> &category_name) {
142
143 if (enable_profiling && enable_nvtx) {
144#ifdef SHAMROCK_USE_NVTX
145 // Push a NVTX range
146 nvtxRangePush(fileloc.loc.function_name());
147#endif
148 }
149}
150
152 const SourceLocation &fileloc,
153 const std::optional<std::string> &name,
154 const std::optional<std::string> &category_name) {
155
156 if (enable_profiling && enable_nvtx) {
157#ifdef SHAMROCK_USE_NVTX
158 // Pop the NVTX range
159 nvtxRangePop();
160#endif
161 }
162}
163
164void shambase::profiling::register_counter_val(const std::string &name, f64 time, f64 val) {
165
166 if (enable_profiling) {
167 chrome::register_counter_val(0, time, name, val);
168 }
169}
double f64
Alias for double.
void set_use_complete_event(bool use_complete_event)
Use complete event, or start and begin event in chrome tracing.
Definition profiling.cpp:98
void set_event_record_threshold(f64 threshold)
Set the event record threshold.
Definition profiling.cpp:99
bool is_profiling_enabled()
Check if profiling is enabled.
Definition profiling.cpp:94
void stack_entry_start_no_time(const SourceLocation &fileloc, const std::optional< std::string > &name=std::nullopt, const std::optional< std::string > &category_name=std::nullopt)
Start a profiling event without a time info.
void set_enable_profiling(bool enable_profiling)
Set wether to enable profiling.
Definition profiling.cpp:97
void register_counter_val(const std::string &name, f64 time, f64 val)
Register a counter value.
void set_enable_nvtx(bool enable_nvtx)
Set wether to enable NVTX profiling.
Definition profiling.cpp:96
void stack_entry_end_no_time(const SourceLocation &fileloc, const std::optional< std::string > &name=std::nullopt, const std::optional< std::string > &category_name=std::nullopt)
End a profiling event without a time info.
void stack_entry_start(const SourceLocation &fileloc, f64 t_start, const std::optional< std::string > &name=std::nullopt, const std::optional< std::string > &category_name=std::nullopt)
Register the start of a profiling event.
void stack_entry_end(const SourceLocation &fileloc, f64 t_start, f64 tend, const std::optional< std::string > &name=std::nullopt, const std::optional< std::string > &category_name=std::nullopt)
Register the end of a profiling event.
provide information about the source location
constexpr const char * file_name() const noexcept
Returns the file name of the source location.
constexpr unsigned column() const noexcept
Returns the column offset of the source location.
constexpr unsigned line() const noexcept
Returns the line number of the source location.
constexpr const char * function_name() const noexcept
Returns the function name of the source location.