SPH
Profiler.cpp
Go to the documentation of this file.
1 #include "system/Profiler.h"
2 #include "io/Logger.h"
3 #include <algorithm>
4 
6 
7 #ifdef SPH_PROFILE
8 AutoPtr<Profiler> Profiler::instance;
9 
10 Array<ScopeStatistics> Profiler::getStatistics() const {
12  uint64_t totalTime = 0;
13  for (auto& iter : records) {
14  stats.push(ScopeStatistics{ iter.first, iter.second.duration, 0, iter.second.cpuUsage });
15  totalTime += iter.second.duration;
16  }
17  for (auto& s : stats) {
18  s.relativeTime = float(s.totalTime) / float(totalTime);
19  }
20  std::sort(stats.begin(), stats.end(), [](ScopeStatistics& s1, ScopeStatistics& s2) {
21  return s1.totalTime > s2.totalTime;
22  });
23  return stats;
24 }
25 
26 void Profiler::printStatistics(ILogger& logger) const {
27  Array<ScopeStatistics> stats = this->getStatistics();
28  for (ScopeStatistics& s : stats) {
29  std::stringstream ss;
30  ss << std::setw(45) << std::left << s.name << " | " << std::setw(10) << std::right << s.totalTime
31  << "mus | rel: " << std::setw(8) << std::right << std::setprecision(3) << std::fixed
32  << 100._f * s.relativeTime << "% | cpu: " << std::setw(8) << std::right << std::setprecision(3)
33  << std::fixed << 100._f * s.cpuUsage << "%";
34  logger.write(ss.str());
35  }
36 }
37 #endif
38 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Logging routines of the run.
#define NAMESPACE_SPH_END
Definition: Object.h:12
Tool to measure time spent in functions and profile the code.
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
Interface providing generic (text, human readable) output of the program.
Definition: Logger.h:22
void write(TArgs &&... args)
Creates and logs a message by concatenating arguments.
Definition: Logger.h:37