SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
ProfilerTimer Class Reference

A timer for profiling functions and scopes. More...

#include <Profiler.h>

Public Member Functions

 ProfilerTimer (const char *name)
 
 ~ProfilerTimer ()
 

Private Attributes

const char * _name
 
uint32_t _depth
 
std::chrono::time_point< std::chrono::high_resolution_clock > _startPoint
 
bool _running
 

Static Private Attributes

static constexpr uint32_t INVALID_THREAD_ID = -1
 
static thread_local uint32_t threadId = INVALID_THREAD_ID
 
static thread_local uint32_t threadDepth = 0
 

Friends

class Profiler
 

Detailed Description

A timer for profiling functions and scopes.

This class should be instantiated at the start of functions and scopes that should be profiled. The object will record the current time at it's construction and the current time at it's destruction (when the scope ends) and the depth in the call stack. The destructor automatically calls Profiler::instance().recordResult().

Definition at line 101 of file Profiler.h.

Constructor & Destructor Documentation

◆ ProfilerTimer()

ProfilerTimer::ProfilerTimer ( const char *  name)
explicit

Constructor for ProfilerTimer that saves the current time as the start time, the thread-local depth as the scope depth and increases the thread-local depth since we have just entered a scope. PROFILE_THREAD must be called in the current thread before this function or else the current thread can't be identified and the application exits.

Parameters
nameName of the scope

Definition at line 187 of file Profiler.cpp.

188 {
189  // If the thread ID is INVALID_THREAD_ID, PROFILE_THREAD hasn't been called
190  // We don't know the current thread in this case, so we simply skip
192  {
193  _running = false;
194  std::cout << ("Warning: Attempted to profile scope in non-profiled thread\nScope name: " + std::string(name) + "\n").c_str();
195  return;
196  }
197 
198  _name = name;
199  _startPoint = std::chrono::high_resolution_clock::now();
201  _running = true;
202 
203  threadDepth++;
204 }
const char * _name
Definition: Profiler.h:114
std::chrono::time_point< std::chrono::high_resolution_clock > _startPoint
Definition: Profiler.h:116
static thread_local uint32_t threadId
Definition: Profiler.h:111
static thread_local uint32_t threadDepth
Definition: Profiler.h:112
uint32_t _depth
Definition: Profiler.h:115
static constexpr uint32_t INVALID_THREAD_ID
Definition: Profiler.h:110
bool _running
Definition: Profiler.h:117

◆ ~ProfilerTimer()

ProfilerTimer::~ProfilerTimer ( )

Destructor for ProfilerTimer that creates a ProfilingResult with the scope name, the depth, the start time, the current time as the end time and the current thread ID. The ProfilingResult is then registered with the Profiler and the thread-local depth is decreased since we have just exited a scope.

Definition at line 213 of file Profiler.cpp.

214 {
215  if (!_running) return;
216  _running = false;
217 
218  auto endTimePoint = std::chrono::high_resolution_clock::now();
219  uint64_t start = std::chrono::time_point_cast<std::chrono::microseconds>(_startPoint).time_since_epoch().count();
220  uint64_t end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count();
221 
222  ProfilingResult result{_name, _depth, start, end, threadId};
224  threadDepth--;
225 }
static Profiler & instance()
Definition: Profiler.h:70
void recordResult(ProfilingResult result)
Definition: Profiler.cpp:135

Friends And Related Function Documentation

◆ Profiler

friend class Profiler
friend

Definition at line 103 of file Profiler.h.

Member Data Documentation

◆ _depth

uint32_t ProfilerTimer::_depth
private

Definition at line 115 of file Profiler.h.

◆ _name

const char* ProfilerTimer::_name
private

Definition at line 114 of file Profiler.h.

◆ _running

bool ProfilerTimer::_running
private

Definition at line 117 of file Profiler.h.

◆ _startPoint

std::chrono::time_point<std::chrono::high_resolution_clock> ProfilerTimer::_startPoint
private

Definition at line 116 of file Profiler.h.

◆ INVALID_THREAD_ID

constexpr uint32_t ProfilerTimer::INVALID_THREAD_ID = -1
staticconstexprprivate

Definition at line 110 of file Profiler.h.

◆ threadDepth

thread_local uint32_t ProfilerTimer::threadDepth = 0
staticprivate

Definition at line 112 of file Profiler.h.

◆ threadId

thread_local uint32_t ProfilerTimer::threadId = INVALID_THREAD_ID
staticprivate

Definition at line 111 of file Profiler.h.


The documentation for this class was generated from the following files: