Fixed issue #3307: Abort Merge on a single file always results in a parameter error...
[TortoiseGit.git] / src / Utils / ProfilingInfo.h
blob8f7cf66b7af25548a04a6996f07d180482daa1f8
1 #pragma once
3 // TortoiseGit - a Windows shell extension for easy version control
5 // Copyright (C) 2003-2008, 2015 - 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, const char* file, int line);
75 /// record values
77 void Add (unsigned __int64 value);
79 /// modification
81 void Reset();
83 /// data access
85 const char* GetName() const {return name;}
86 const char* GetFile() const {return file;}
87 int GetLine() const {return line;}
89 size_t GetCount() const {return count;}
90 unsigned __int64 GetSum() const {return sum;}
91 unsigned __int64 GetMinValue() const {return minValue;}
92 unsigned __int64 GetMaxValue() const {return maxValue;}
95 /**
96 * RAII class that encapsulates a single execution of a profiled
97 * block / line. The result gets added to an existing profiling record.
100 class CRecordProfileEvent
102 private:
104 CProfilingRecord* record;
106 /// the initial CPU counter value
108 unsigned __int64 start;
110 public:
112 /// construction: start clock
114 CRecordProfileEvent (CProfilingRecord* aRecord);
116 /// destruction: time interval to profiling record,
117 /// if Stop() had not been called before
119 ~CRecordProfileEvent();
121 /// don't wait for destruction
123 void Stop();
126 #ifndef _DEBUG
128 /// construction / destruction
130 inline CRecordProfileEvent::CRecordProfileEvent (CProfilingRecord* aRecord)
131 : record (aRecord)
132 , start (__rdtsc())
136 inline CRecordProfileEvent::~CRecordProfileEvent()
138 if (record)
139 record->Add (__rdtsc() - start);
142 #endif
144 /// don't wait for destruction
146 inline void CRecordProfileEvent::Stop()
148 if (record)
150 record->Add (__rdtsc() - start);
151 record = nullptr;
156 * Singleton class that acts as container for all profiling records.
157 * You may reset its content as well as write it to disk.
160 class CProfilingInfo
162 private:
164 typedef std::vector<CProfilingRecord*> TRecords;
165 TRecords records;
167 /// construction / destruction
169 CProfilingInfo();
170 ~CProfilingInfo(void);
171 // prevent cloning
172 CProfilingInfo(const CProfilingInfo&) = delete;
173 CProfilingInfo& operator=(const CProfilingInfo&) = delete;
175 /// create report
177 std::string GetReport() const;
179 public:
181 /// access to default instance
183 static CProfilingInfo* GetInstance();
185 /// add a new record
187 CProfilingRecord* Create ( const char* name, const char* file, int line);
191 * Profiling macros
194 #define PROFILE_CONCAT3( a, b ) a##b
195 #define PROFILE_CONCAT2( a, b ) PROFILE_CONCAT3( a, b )
196 #define PROFILE_CONCAT( a, b ) PROFILE_CONCAT2( a, b )
198 /// measures the time from the point of usage to the end of the respective block
200 #define PROFILE_BLOCK\
201 static CProfilingRecord* PROFILE_CONCAT(record,__LINE__) \
202 = CProfilingInfo::GetInstance()->Create(__FUNCTION__,__FILE__,__LINE__);\
203 CRecordProfileEvent PROFILE_CONCAT(profileSection,__LINE__) (PROFILE_CONCAT(record,__LINE__));
205 /// measures the time taken to execute the respective code line
207 #define PROFILE_LINE(line)\
208 static CProfilingRecord* PROFILE_CONCAT(record,__LINE__) \
209 = CProfilingInfo::GetInstance()->Create(__FUNCTION__,__FILE__,__LINE__);\
210 CRecordProfileEvent PROFILE_CONCAT(profileSection,__LINE__) (PROFILE_CONCAT(record,__LINE__));\
211 line;\
212 PROFILE_CONCAT(profileSection,__LINE__).Stop();