Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Profiler Class Reference

A simple profiler to measure the average time lengths that a particular task/function takes. Currently not thread-safe and not thread-smart, but it will probably work fine if you use it on a high level. More...

#include <Profiler.h>

Collaboration diagram for Profiler:
[legend]

Public Member Functions

virtual ~Profiler ()
 write to a profile at the end of the test. More...
 
void Begin (const char *fileName, int lineNum, const char *taskDescription)
 start the task timer. More...
 
void End (const char *fileName, int lineNum, const char *taskDescription)
 end the task timer. More...
 

Static Public Member Functions

static ProfilerInstance ()
 Gets the singleton instance. More...
 

Protected Member Functions

 Profiler ()
 private constructor - Singleton. More...
 
TaskProfileGetOrCreateTaskProfile (const char *fileName, int lineNum)
 find a taskProfile for the given task, otherwise create More...
 
TaskProfileGetTaskProfileByDescription (const char *description)
 

Protected Attributes

std::vector< std::unique_ptr< TaskProfile > > mTasks
 
std::mutex mTasksMutex
 

Detailed Description

A simple profiler to measure the average time lengths that a particular task/function takes. Currently not thread-safe and not thread-smart, but it will probably work fine if you use it on a high level.

Definition at line 39 of file Profiler.h.

Constructor & Destructor Documentation

◆ ~Profiler()

Profiler::~Profiler ( )
virtual

write to a profile at the end of the test.

Definition at line 30 of file Profiler.cpp.

31{
32 if(mTasks.size())
33 {
34 //print everything out. append to a log.
35 FILE* log = fopen("AudacityProfilerLog.txt", "a");
36 time_t now;
37
38 time(&now);
39 wxFprintf(log,"Audacity Profiler Run, Ended at ");
40 wxFprintf(log,"%s",ctime(&now));
41 wxFprintf(log,"****************************************\n");
42 //print out the tasks
43 for(int i=0;i<(int)mTasks.size();i++)
44 {
45 if(mTasks[i]->mNumHits>0)
46 {
47 wxFprintf(log,"Task: %s\n(begins at line %d in %s)\n\n",mTasks[i]->mDescription.get(), mTasks[i]->mLine, mTasks[i]->mFileName.get());
48 wxFprintf(log,"Number of times run: %d\n",mTasks[i]->mNumHits);
49 wxFprintf(log,"Total run time (seconds): %f\n", (double)mTasks[i]->mCumTime/CLOCKS_PER_SEC);
50 wxFprintf(log,"Average run time (seconds): %f\n",mTasks[i]->ComputeAverageRunTime());
51
52 if(i < ((int)mTasks.size()) -1)
53 wxFprintf(log,"----------------------------\n");
54 }
55 }
56 wxFprintf(log,"\n****************************************\n\n\n");
57
58 fclose(log);
59 }
60}
std::vector< std::unique_ptr< TaskProfile > > mTasks
Definition: Profiler.h:63

References mTasks.

◆ Profiler()

Profiler::Profiler ( )
inlineprotected

private constructor - Singleton.

Definition at line 56 of file Profiler.h.

56{};

Member Function Documentation

◆ Begin()

void Profiler::Begin ( const char *  fileName,
int  lineNum,
const char *  taskDescription 
)

start the task timer.

Definition at line 63 of file Profiler.cpp.

64{
65 std::lock_guard<std::mutex> guard{ mTasksMutex };
66 GetOrCreateTaskProfile(fileName,lineNum)->Begin(fileName,lineNum,taskDescription);
67}
TaskProfile * GetOrCreateTaskProfile(const char *fileName, int lineNum)
find a taskProfile for the given task, otherwise create
Definition: Profiler.cpp:89
std::mutex mTasksMutex
Definition: Profiler.h:65
void Begin(const char *fileName, int lineNum, const char *taskDescription)
start the task timer.
Definition: Profiler.cpp:127

References TaskProfile::Begin(), GetOrCreateTaskProfile(), and mTasksMutex.

Here is the call graph for this function:

◆ End()

void Profiler::End ( const char *  fileName,
int  lineNum,
const char *  taskDescription 
)

end the task timer.

Definition at line 70 of file Profiler.cpp.

71{
72 std::lock_guard<std::mutex> guard{ mTasksMutex };
73 TaskProfile* tp;
74 tp=GetTaskProfileByDescription(taskDescription);
75 if(tp)
76 tp->End(fileName,lineNum,taskDescription);
77}
TaskProfile * GetTaskProfileByDescription(const char *description)
Definition: Profiler.cpp:102
a simple class to keep track of one task that may be called multiple times.
Definition: Profiler.h:70
void End(const char *fileName, int lineNum, const char *taskDescription)
end the task timer.
Definition: Profiler.cpp:143

References TaskProfile::End(), GetTaskProfileByDescription(), and mTasksMutex.

Here is the call graph for this function:

◆ GetOrCreateTaskProfile()

TaskProfile * Profiler::GetOrCreateTaskProfile ( const char *  fileName,
int  lineNum 
)
protected

find a taskProfile for the given task, otherwise create

Definition at line 89 of file Profiler.cpp.

90{
91 for(int i=0;i<(int)mTasks.size();i++)
92 {
93 if(strcmp(fileName, mTasks[i]->mFileName.get())==0 && lineNum == mTasks[i]->mLine)
94 return mTasks[i].get();
95 }
96
97 auto tp = std::make_unique<TaskProfile>();
98 mTasks.push_back(std::move(tp));
99 return mTasks.back().get();
100}

References mTasks.

Referenced by Begin().

Here is the caller graph for this function:

◆ GetTaskProfileByDescription()

TaskProfile * Profiler::GetTaskProfileByDescription ( const char *  description)
protected

Definition at line 102 of file Profiler.cpp.

103{
104 for(int i=0;i<(int)mTasks.size();i++)
105 {
106 if(strcmp(description, mTasks[i]->mDescription.get())==0)
107 return mTasks[i].get();
108 }
109
110 return NULL;
111
112}

References mTasks.

Referenced by End().

Here is the caller graph for this function:

◆ Instance()

Profiler * Profiler::Instance ( )
static

Gets the singleton instance.

Definition at line 80 of file Profiler.cpp.

81{
82 static Profiler pro;
83 //this isn't 100% threadsafe but I think Okay for this purpose.
84
85 return &pro;
86}
A simple profiler to measure the average time lengths that a particular task/function takes....
Definition: Profiler.h:40

Member Data Documentation

◆ mTasks

std::vector<std::unique_ptr<TaskProfile> > Profiler::mTasks
protected

Definition at line 63 of file Profiler.h.

Referenced by GetOrCreateTaskProfile(), GetTaskProfileByDescription(), and ~Profiler().

◆ mTasksMutex

std::mutex Profiler::mTasksMutex
protected

Definition at line 65 of file Profiler.h.

Referenced by Begin(), and End().


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