Navigate selection history via log jump up/down button
[TortoiseGit.git] / src / Utils / ProfilingInfo.h
blobc84af2a4ffc8677af43abf672ca0db3d849d29a6
1 #pragma once
3 // TortoiseGit - a Windows shell extension for easy version control
5 // Copyright (C) 2003-2008 - TortoiseSVN
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //////////////////////////////////////////////////////////////////////
23 // required includes
24 //////////////////////////////////////////////////////////////////////
26 #ifndef _DEBUG
27 #ifndef __INTRIN_H_
28 #include <intrin.h>
29 #endif
30 #endif
32 #ifndef _PSAPI_H_
33 #include <psapi.h>
34 #endif
36 #ifndef _STRING_
37 #include <string>
38 #endif
40 #ifndef _VECTOR_
41 #include <vector>
42 #endif
44 #pragma comment (lib, "psapi.lib")
46 /**
47 * Collects the profiling info for a given profiled block / line.
48 * Records execution count, min, max and accumulated execution time
49 * in CPU clock ticks.
52 class CProfilingRecord
54 private:
56 /// identification
58 const char* name;
59 const char* file;
60 int line;
62 /// collected profiling info
64 size_t count;
65 unsigned __int64 sum;
66 unsigned __int64 minValue;
67 unsigned __int64 maxValue;
69 public:
71 /// construction
73 CProfilingRecord ( const char* name
74 , const char* file
75 , int line);
77 /// record values
79 void Add (unsigned __int64 value);
81 /// modification
83 void Reset();
85 /// data access
87 const char* GetName() const {return name;}
88 const char* GetFile() const {return file;}
89 int GetLine() const {return line;}
91 size_t GetCount() const {return count;}
92 unsigned __int64 GetSum() const {return sum;}
93 unsigned __int64 GetMinValue() const {return minValue;}
94 unsigned __int64 GetMaxValue() const {return maxValue;}
97 /**
98 * RAII class that encapsulates a single execution of a profiled
99 * block / line. The result gets added to an existing profiling record.
102 class CRecordProfileEvent
104 private:
106 CProfilingRecord* record;
108 /// the initial CPU counter value
110 unsigned __int64 start;
112 public:
114 /// construction: start clock
116 CRecordProfileEvent (CProfilingRecord* aRecord);
118 /// destruction: time interval to profiling record,
119 /// if Stop() had not been called before
121 ~CRecordProfileEvent();
123 /// don't wait for destruction
125 void Stop();
128 #ifndef _DEBUG
130 /// construction / destruction
132 inline CRecordProfileEvent::CRecordProfileEvent (CProfilingRecord* aRecord)
133 : record (aRecord)
134 , start (__rdtsc())
138 inline CRecordProfileEvent::~CRecordProfileEvent()
140 if (record)
141 record->Add (__rdtsc() - start);
144 #endif
146 /// don't wait for destruction
148 inline void CRecordProfileEvent::Stop()
150 if (record)
152 record->Add (__rdtsc() - start);
153 record = NULL;
158 * Singleton class that acts as container for all profiling records.
159 * You may reset its content as well as write it to disk.
162 class CProfilingInfo
164 private:
166 typedef std::vector<CProfilingRecord*> TRecords;
167 TRecords records;
169 /// construction / destruction
171 CProfilingInfo();
172 ~CProfilingInfo(void);
174 /// create report
176 std::string GetReport() const;
178 public:
180 /// access to default instance
182 static CProfilingInfo* GetInstance();
184 /// add a new record
186 CProfilingRecord* Create ( const char* name
187 , const char* file
188 , int line);
192 * Profiling macros
195 #define PROFILE_CONCAT3( a, b ) a##b
196 #define PROFILE_CONCAT2( a, b ) PROFILE_CONCAT3( a, b )
197 #define PROFILE_CONCAT( a, b ) PROFILE_CONCAT2( a, b )
199 /// measures the time from the point of usage to the end of the respective block
201 #define PROFILE_BLOCK\
202 static CProfilingRecord* PROFILE_CONCAT(record,__LINE__) \
203 = CProfilingInfo::GetInstance()->Create(__FUNCTION__,__FILE__,__LINE__);\
204 CRecordProfileEvent PROFILE_CONCAT(profileSection,__LINE__) (PROFILE_CONCAT(record,__LINE__));
206 /// measures the time taken to execute the respective code line
208 #define PROFILE_LINE(line)\
209 static CProfilingRecord* PROFILE_CONCAT(record,__LINE__) \
210 = CProfilingInfo::GetInstance()->Create(__FUNCTION__,__FILE__,__LINE__);\
211 CRecordProfileEvent PROFILE_CONCAT(profileSection,__LINE__) (PROFILE_CONCAT(record,__LINE__));\
212 line;\
213 PROFILE_CONCAT(profileSection,__LINE__).Stop();