PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Profiling.h
Go to the documentation of this file.
1
9
10#ifndef PBAT_PROFILING_PROFILING_H
11#define PBAT_PROFILING_PROFILING_H
12
13#include "PhysicsBasedAnimationToolkitExport.h"
14
15#if defined(PBAT_HAS_TRACY_PROFILER)
16 #if not defined(__CUDACC__)
17 #define PBAT_CAN_USE_TRACY_CPP
18 #include <tracy/Tracy.hpp>
19 #define PBAT_PROFILE_SCOPE ZoneScoped
20 #define PBAT_PROFILE_NAMED_SCOPE(name) ZoneScopedN(name)
21 #else
22 #define PBAT_CAN_USE_TRACY_C
23 #include <tracy/TracyC.h>
24 #define PBAT_PROFILE_SCOPE
25 #define PBAT_PROFILE_NAMED_SCOPE(name)
26 #define PBAT_PROFILE_CUDA_HOST_SCOPE_START(var) TracyCZone(var, true)
27 #define PBAT_PROFILE_NAMED_CUDA_HOST_SCOPE_START(var, name) TracyCZoneN(var, name, true)
28 #define PBAT_PROFILE_CUDA_HOST_SCOPE_END(var) TracyCZoneEnd(var)
29 #endif // __CUDACC__
30#else
31 #define PBAT_PROFILE_SCOPE
32 #define PBAT_PROFILE_NAMED_SCOPE(name)
33 #define PBAT_PROFILE_CUDA_HOST_SCOPE_START(var)
34 #define PBAT_PROFILE_CUDA_HOST_SCOPE_START(var, name)
35 #define PBAT_PROFILE_CUDA_HOST_SCOPE_END(var)
36#endif // PBAT_CAN_USE_TRACY
37
42
49
54
59
66
72
78
84
85#include <map>
86#include <string>
87#include <string_view>
88#include <type_traits>
89
90namespace pbat {
91namespace profiling {
92
99PBAT_API void BeginFrame(std::string_view name);
100
107PBAT_API void EndFrame(std::string_view name);
108
114PBAT_API bool IsConnectedToServer();
115
127template <class Func, class... Args>
128std::invoke_result_t<Func, Args...> Profile(std::string const& zoneName, Func&& f, Args&&... args)
129{
130#ifdef PBAT_CAN_USE_TRACY_CPP
131 static auto constexpr line = (uint32_t)TracyLine;
132 struct SourceLocationData
133 {
134 SourceLocationData(std::string_view zoneNameView)
135 : name(zoneNameView), function(TracyFunction), file(TracyFile), data()
136 {
137 data.name = name.data();
138 data.function = function.data();
139 data.file = file.data();
140 data.line = line;
141 data.color = 0;
142 }
143 std::string name;
144 std::string function;
145 std::string file;
146 tracy::SourceLocationData data;
147 };
148 static std::map<std::string, SourceLocationData> zones{};
149 auto it = zones.find(zoneName);
150 if (it == zones.end())
151 {
152 bool inserted{false};
153 std::tie(it, inserted) = zones.insert({zoneName, SourceLocationData(zoneName)});
154 assert(inserted);
155 }
156 SourceLocationData const& data = it->second;
157 tracy::ScopedZone zone(&(data.data));
158#endif // PBAT_CAN_USE_TRACY
159 return f(std::forward<Args>(args)...);
160}
161
162} // namespace profiling
163} // namespace pbat
164
165#endif // PBAT_PROFILING_PROFILING_H
std::invoke_result_t< Func, Args... > Profile(std::string const &zoneName, Func &&f, Args &&... args)
Profile a function as a Tracy named zone.
Definition Profiling.h:128
The main namespace of the library.
Definition Aliases.h:15