Audacity 3.2.0
Profiler.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Profiler.cpp
6
7 Created by Michael Chinen (mchinen) on 8/12/08
8 Audacity(R) is copyright (c) 1999-2008 Audacity Team.
9 License: GPL v2 or later. See License.txt.
10
11******************************************************************//*******************************************************************/
22
23
24#include "Profiler.h"
25
26#include <string.h>
27#include <wx/crt.h>
28
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}
61
63void Profiler::Begin(const char* fileName, int lineNum, const char* taskDescription)
64{
65 std::lock_guard<std::mutex> guard{ mTasksMutex };
66 GetOrCreateTaskProfile(fileName,lineNum)->Begin(fileName,lineNum,taskDescription);
67}
68
70void Profiler::End(const char* fileName, int lineNum, const char* taskDescription)
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}
78
81{
82 static Profiler pro;
83 //this isn't 100% threadsafe but I think Okay for this purpose.
84
85 return &pro;
86}
87
89TaskProfile* Profiler::GetOrCreateTaskProfile(const char* fileName, int lineNum)
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}
101
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}
113
114
117{
118 mCumTime=0;
119 mNumHits=0;
120}
121
123{
124}
125
127void TaskProfile::Begin(const char* fileName, int lineNum, const char* taskDescription)
128{
129 if(!mFileName)
130 {
131 mFileName.reinit( strlen(fileName) + 1 );
132 strcpy(mFileName.get(), fileName);
133 mDescription.reinit( strlen(taskDescription) + 1 );
134 strcpy(mDescription.get(), taskDescription);
135 mLine = lineNum;
136 }
137
138 mLastTime = clock();
139
140}
141
143void TaskProfile::End(const char* WXUNUSED(fileName), int WXUNUSED(lineNum), const char* WXUNUSED(taskDescription))
144{
145 mCumTime += clock() - mLastTime;
146 mNumHits++;
147}
148
150{
151 if(mNumHits)
152 return (double) ((double)mCumTime/CLOCKS_PER_SEC)/mNumHits;
153 else
154 return 0.0;
155}
void reinit(Integral count, bool initialize=false)
Definition: MemoryX.h:59
A simple profiler to measure the average time lengths that a particular task/function takes....
Definition: Profiler.h:40
virtual ~Profiler()
write to a profile at the end of the test.
Definition: Profiler.cpp:30
TaskProfile * GetOrCreateTaskProfile(const char *fileName, int lineNum)
find a taskProfile for the given task, otherwise create
Definition: Profiler.cpp:89
void Begin(const char *fileName, int lineNum, const char *taskDescription)
start the task timer.
Definition: Profiler.cpp:63
void End(const char *fileName, int lineNum, const char *taskDescription)
end the task timer.
Definition: Profiler.cpp:70
std::mutex mTasksMutex
Definition: Profiler.h:65
std::vector< std::unique_ptr< TaskProfile > > mTasks
Definition: Profiler.h:63
TaskProfile * GetTaskProfileByDescription(const char *description)
Definition: Profiler.cpp:102
static Profiler * Instance()
Gets the singleton instance.
Definition: Profiler.cpp:80
a simple class to keep track of one task that may be called multiple times.
Definition: Profiler.h:70
clock_t mLastTime
Definition: Profiler.h:87
void End(const char *fileName, int lineNum, const char *taskDescription)
end the task timer.
Definition: Profiler.cpp:143
void Begin(const char *fileName, int lineNum, const char *taskDescription)
start the task timer.
Definition: Profiler.cpp:127
double ComputeAverageRunTime()
Definition: Profiler.cpp:149
int mNumHits
Definition: Profiler.h:85
ArrayOf< char > mDescription
Definition: Profiler.h:84
TaskProfile()
Task Profile.
Definition: Profiler.cpp:116
ArrayOf< char > mFileName
Definition: Profiler.h:82
int mLine
Definition: Profiler.h:83
virtual ~TaskProfile()
Definition: Profiler.cpp:122
clock_t mCumTime
Definition: Profiler.h:86