27 using OnScopeEnds = std::function<void(
const std::string&,
const uint64_t)>;
35 ScopedTimer(
const std::string& name,
const OnScopeEnds& callback)
37 , callback(callback) {}
51 void next(
const std::string& newName) {
59 #define MEASURE_SCOPE(name) \
60 ScopedTimer __timer("", [](const std::string&, const uint64_t time) { \
61 StdOutLogger logger; \
62 logger.write(name, " took ", time / 1000, " ms"); \
64 #define MEASURE(name, what) \
66 MEASURE_SCOPE(name); \
70 #define MEASURE_SCOPE(name)
71 #define MEASURE(name, what) what
76 struct ScopeStatistics {
98 std::atomic<uint64_t> duration{ 0 };
101 Float cpuUsage = 0._f;
108 std::map<std::string, ScopeRecord> records;
111 std::string currentScope;
115 std::atomic_bool quitting{
false };
119 cpuUsage.thread = std::thread([
this] {
122 if (usage && !cpuUsage.currentScope.empty()) {
123 ScopeRecord& scope = records[cpuUsage.currentScope];
124 scope.cpuUsage = (scope.cpuUsage * scope.weight + usage.
value()) / (scope.weight + 1);
127 std::this_thread::sleep_for(std::chrono::milliseconds(20));
134 cpuUsage.thread.join();
137 static Profiler& getInstance() {
139 instance = makeAuto<Profiler>();
147 ScopedTimer makeScopedTimer(
const std::string& name) {
148 cpuUsage.currentScope = name;
149 return ScopedTimer(name, [
this](
const std::string& n,
const uint64_t elapsed) {
150 records[n].duration += elapsed;
158 void printStatistics(
ILogger& logger)
const;
166 #define PROFILE_SCOPE(name) \
167 Profiler& __instance = Profiler::getInstance(); \
168 ScopedTimer __scopedTimer = __instance.makeScopedTimer(name);
169 #define PROFILE(name, what) \
171 PROFILE_SCOPE(name); \
175 #define PROFILE_SCOPE(name)
176 #define PROFILE(name, what) what
uint32_t Size
Integral type used to index arrays (by default).
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Logging routines of the run.
INLINE Float weight(const Vector &r1, const Vector &r2)
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
Measuring time intervals and executing periodic events.
Generic dynamically allocated resizable storage.
Wrapper of pointer that deletes the resource from destructor.
Interface providing generic (text, human readable) output of the program.
INLINE Type & value()
Returns the reference to the stored value.
Simple extension of Timer allowing to pause and continue timer.
int64_t elapsed(const TimerUnit unit) const
Returns elapsed time in timer units. Does not reset the timer.
void stop()
Stops the timer. Function getElapsed() will report the same value from now on.
void resume()
Resumes stopped timer.
void restart()
Reset elapsed duration to zero.
Object with deleted copy constructor and copy operator.
Timer that reports the measured duration when being destroyed.
void next(const std::string &newName)
ScopedTimer(const std::string &name, const OnScopeEnds &callback)