SLProject 4.0.000
|
Utility for profiling functions/scopes and writing the results to a file. More...
#include <Profiler.h>
Public Member Functions | |
void | beginSession (std::string filePath) |
std::string | filePath () |
void | endSession () |
void | recordResult (ProfilingResult result) |
void | profileThread (const std::string &name) |
Static Public Member Functions | |
static Profiler & | instance () |
Static Private Member Functions | |
static void | writeString (const char *s, std::ofstream &stream) |
Writes the length (32-bit) and the string (non-null-terminated) itself to the file stream. More... | |
Private Attributes | |
std::string | _filePath |
Future path of the trace file. More... | |
uint64_t | _sessionStart = 0 |
Start timestamp of the session in microseconds. More... | |
std::vector< ProfilingResult > | _results |
List of profiling results (of all threads) More... | |
std::vector< std::string > | _threadNames |
List of thread names (the thread ID is the index) More... | |
std::mutex | _mutex |
Mutex for accessing profiling results and thread names. More... | |
Utility for profiling functions/scopes and writing the results to a file.
To start the profiling, call BEGIN_PROFILING_SESSION(filePath) with the path to the trace file. After that you can place "PROFILE_FUNCTION();" or "PROFILE_SCOPE(name);" at the start of every function or scope you want to measure. The profiler supports multithreading. To add a new thread, call "PROFILE_THREAD(name)" at the start of the thread. Threads with the same name will appear merged in the trace file. To end the session and write the result to the trace file, call END_PROFILING_SESSION().
The resulting trace gets written into the data folder of SLProject and can be opened using the trace viewer located at /externals/trace-viewer/trace-viewer.jar. Note that a Java Runtime Environment is required to launch this JAR archive.
void Profiler::beginSession | ( | std::string | filePath | ) |
Starts a profiling session by saving the session start timestamp so it can later be subtracted from the individual result timestamps to get the time points relative to the start of the session.
filePath | The path where the trace file should be written to |
void Profiler::endSession | ( | ) |
Ends the profiling session and writes the result to a trace file. A trace file (.slt) has the following layout: Number of scopes: int32 Scope 1 name: (length: int32, name: non-null-terminated char array) Scope 2 name: (length: int32, name: non-null-terminated char array) ... Number of threads: int32 Thread 1 name: (length: int32, name: non-null-terminated char array) Number of scopes entered in thread 1: int32 Scope 1 in thread 1 (name index: int32, depth: int32, start time: int64, end time: int64) Scope 2 in thread 1 (name index: int32, depth: int32, start time: int64, end time: int64) ... Thread 2 name: (length: int32, name: non-null-terminated char array) Number of scopes entered in thread 2: int32 Scope 1 in thread 2 (name index: int32, depth: int32, start time: int64, end time: int64) Scope 2 in thread 2 (name index: int32, depth: int32, start time: int64, end time: int64) ... ...
All data is written in the big-endian format because that's the endianness that Java uses to read the data later on in the trace viewer. This means that the function has to check the endianness of the system and convert all integers to big endian if we're on a little-endian system.
|
inline |
|
inlinestatic |
void Profiler::profileThread | ( | const std::string & | name | ) |
Associates the thread in which the function was called with the name provided. This function must be called at the start of every profiled thread. It is sensibly also thread-safe.
name |
void Profiler::recordResult | ( | ProfilingResult | result | ) |
Stores a result thread-safely in a vector so it can be written to a trace file at the end of the session.
result |
|
staticprivate |
Writes the length (32-bit) and the string (non-null-terminated) itself to the file stream.
|
private |
Future path of the trace file.
|
private |
Mutex for accessing profiling results and thread names.
|
private |
List of profiling results (of all threads)
|
private |
Start timestamp of the session in microseconds.
|
private |
List of thread names (the thread ID is the index)