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

Basic instrumentation profiler for Google Chrome tracing format. More...

#include <Instrumentor.h>

Public Member Functions

 Instrumentor ()
 
void beginSession (const std::string &name, const bool storeInMemory=false, const std::string &filePath="Profiling-Results.json")
 
void endSession ()
 
void addProfile (const ProfileResult &result)
 
void writeProfile (const ProfileResult &result)
 
void writeHeader ()
 
void writeFooter ()
 
std::string filePath ()
 

Static Public Member Functions

static Instrumentorget ()
 

Private Attributes

InstrumentationSession_currentSession
 
std::string _filePath
 
std::ofstream _outputStream
 
int _profileCount
 
std::mutex _mutex
 
bool _storeInMemory = false
 
std::vector< ProfileResult_profileResults
 

Detailed Description

Basic instrumentation profiler for Google Chrome tracing format.

Usage: include this header file somewhere in your code. In your most outer function (e.g. main) you have to begin profiling session with: Instrumentor::get().beginSession("Session Name"); If you pass storeInMemory=true the profileResults will be stored in memory instead of being written into the file stream which is pretty slow. Of course the in memory storage can quickly use a lot of memory depending how fine grained your profiling is. In app-demo this is done in slCreateApp.

In between you can add either PROFILE_FUNCTION(); at the beginning of any routine or PROFILE_SCOPE(scopeName) at the beginning of any scope you want to measure.

At the end of your most outer function (e.g. main) you end the session with: Instrumentor::get().endSession();

After the endSession you can drag the Profiling-Results.json file into the chrome://tracing page of the Google Chrome browser. In app-demo this is done in SLInterface::slTerminate.

Definition at line 77 of file Instrumentor.h.

Constructor & Destructor Documentation

◆ Instrumentor()

Instrumentor::Instrumentor ( )
inline

Definition at line 80 of file Instrumentor.h.

80 : _currentSession(nullptr), _profileCount(0) {}
InstrumentationSession * _currentSession
Definition: Instrumentor.h:178

Member Function Documentation

◆ addProfile()

void Instrumentor::addProfile ( const ProfileResult result)
inline

addProfile should be as fast as possible for not influencing the profiling by the profiler itself. In addition it must be thread safe.

Definition at line 119 of file Instrumentor.h.

120  {
121  std::lock_guard<std::mutex> lock(_mutex);
122 
123  if (_storeInMemory)
124  {
125  _profileResults.emplace_back(result);
126  }
127  else
128  {
129  writeProfile(result);
130  }
131  }
std::vector< ProfileResult > _profileResults
Definition: Instrumentor.h:184
void writeProfile(const ProfileResult &result)
Definition: Instrumentor.h:133
bool _storeInMemory
Definition: Instrumentor.h:183
std::mutex _mutex
Definition: Instrumentor.h:182

◆ beginSession()

void Instrumentor::beginSession ( const std::string &  name,
const bool  storeInMemory = false,
const std::string &  filePath = "Profiling-Results.json" 
)
inline

Definition at line 82 of file Instrumentor.h.

85  {
86  _storeInMemory = storeInMemory;
89 
90  if (!_storeInMemory)
91  {
93  writeHeader();
94  }
95  }
std::ofstream _outputStream
Definition: Instrumentor.h:180
std::string filePath()
Definition: Instrumentor.h:174
std::string _filePath
Definition: Instrumentor.h:179
void writeHeader()
Definition: Instrumentor.h:156

◆ endSession()

void Instrumentor::endSession ( )
inline

Definition at line 97 of file Instrumentor.h.

98  {
99  if (_storeInMemory)
100  {
101  _outputStream.open(_filePath);
102  writeHeader();
103 
104  for (auto result : _profileResults)
105  writeProfile(result);
106  }
107 
108  // end the file
109  writeFooter();
110  _outputStream.close();
111 
112  delete _currentSession;
113  _currentSession = nullptr;
114  _profileCount = 0;
115  }
void writeFooter()
Definition: Instrumentor.h:162

◆ filePath()

std::string Instrumentor::filePath ( )
inline

Definition at line 174 of file Instrumentor.h.

174 { return _filePath; }

◆ get()

static Instrumentor& Instrumentor::get ( )
inlinestatic

Definition at line 168 of file Instrumentor.h.

169  {
170  static Instrumentor instance;
171  return instance;
172  }
Basic instrumentation profiler for Google Chrome tracing format.
Definition: Instrumentor.h:78

◆ writeFooter()

void Instrumentor::writeFooter ( )
inline

Definition at line 162 of file Instrumentor.h.

163  {
164  _outputStream << "]}";
165  _outputStream.flush();
166  }

◆ writeHeader()

void Instrumentor::writeHeader ( )
inline

Definition at line 156 of file Instrumentor.h.

157  {
158  _outputStream << "{\"otherData\": {},\"traceEvents\":[";
159  _outputStream.flush();
160  }

◆ writeProfile()

void Instrumentor::writeProfile ( const ProfileResult result)
inline

Definition at line 133 of file Instrumentor.h.

134  {
135  if (_profileCount++ > 0)
136  _outputStream << ",";
137 
138  std::string name = result.name;
139  std::replace(name.begin(), name.end(), '"', '\'');
140 
141  _outputStream << "{";
142  _outputStream << "\"cat\":\"function\",";
143  _outputStream << "\"dur\":" << (result.end - result.start) << ',';
144  _outputStream << "\"name\":\"" << name << "\",";
145  _outputStream << "\"ph\":\"X\",";
146  _outputStream << "\"pid\":0,";
147  _outputStream << "\"tid\":" << result.threadID << ",";
148  _outputStream << "\"ts\":" << result.start;
149  _outputStream << "}";
150 
151  // We constantly flush in case of file writing during profiling.
152  if (!_storeInMemory)
153  _outputStream.flush();
154  }
long long end
end time point
Definition: Instrumentor.h:48
long long start
start time point
Definition: Instrumentor.h:47
uint32_t threadID
thread ID
Definition: Instrumentor.h:49
const char * name
pointer to char has constant length
Definition: Instrumentor.h:46

Member Data Documentation

◆ _currentSession

InstrumentationSession* Instrumentor::_currentSession
private

Definition at line 178 of file Instrumentor.h.

◆ _filePath

std::string Instrumentor::_filePath
private

Definition at line 179 of file Instrumentor.h.

◆ _mutex

std::mutex Instrumentor::_mutex
private

Definition at line 182 of file Instrumentor.h.

◆ _outputStream

std::ofstream Instrumentor::_outputStream
private

Definition at line 180 of file Instrumentor.h.

◆ _profileCount

int Instrumentor::_profileCount
private

Definition at line 181 of file Instrumentor.h.

◆ _profileResults

std::vector<ProfileResult> Instrumentor::_profileResults
private

Definition at line 184 of file Instrumentor.h.

◆ _storeInMemory

bool Instrumentor::_storeInMemory = false
private

Definition at line 183 of file Instrumentor.h.


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