1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "ProfilingInfo.h"
30 //////////////////////////////////////////////////////////////////////
31 /// construction / destruction
32 //////////////////////////////////////////////////////////////////////
34 CRecordProfileEvent::CRecordProfileEvent (CProfilingRecord
* aRecord
)
40 CRecordProfileEvent::~CRecordProfileEvent()
43 record
->Add (__rdtsc() - start
);
48 //////////////////////////////////////////////////////////////////////
49 // construction / destruction
50 //////////////////////////////////////////////////////////////////////
52 CProfilingRecord::CProfilingRecord ( const char* name
60 , minValue (ULLONG_MAX
)
65 //////////////////////////////////////////////////////////////////////
67 //////////////////////////////////////////////////////////////////////
69 void CProfilingRecord::Add (unsigned __int64 value
)
80 //////////////////////////////////////////////////////////////////////
82 //////////////////////////////////////////////////////////////////////
84 void CProfilingRecord::Reset()
93 //////////////////////////////////////////////////////////////////////
94 // construction / destruction
95 //////////////////////////////////////////////////////////////////////
97 CProfilingInfo::CProfilingInfo()
101 CProfilingInfo::~CProfilingInfo(void)
103 if (!records
.empty())
105 // write profile to file
107 TCHAR buffer
[MAX_PATH
];
108 if (GetModuleFileNameEx (GetCurrentProcess(), NULL
, buffer
, MAX_PATH
) > 0)
111 std::wstring
fileName (buffer
);
112 fileName
+= L
".profile";
114 std::string report
= GetInstance()->GetReport();
116 CFile
file (fileName
.c_str(), CFile::modeCreate
| CFile::modeWrite
);
117 file
.Write (report
.c_str(), (UINT
)report
.size());
121 // ignore all file errors etc.
127 for (size_t i
= 0; i
< records
.size(); ++i
)
132 //////////////////////////////////////////////////////////////////////
133 // access to default instance
134 //////////////////////////////////////////////////////////////////////
136 CProfilingInfo
* CProfilingInfo::GetInstance()
138 static CProfilingInfo instance
;
142 //////////////////////////////////////////////////////////////////////
144 //////////////////////////////////////////////////////////////////////
146 static std::string
IntToStr (unsigned __int64 value
)
149 _ui64toa_s (value
, buffer
, 100, 10);
151 std::string result
= buffer
;
152 for (size_t i
= 3; i
< result
.length(); i
+= 4)
153 result
.insert (result
.length() - i
, 1, ',');
158 std::string
CProfilingInfo::GetReport() const
160 enum { LINE_LENGTH
= 500 };
162 char lineBuffer
[LINE_LENGTH
];
163 const char * const format
="%10s%17s%17s%17s%6s %s\t%s\n";
166 result
.reserve (LINE_LENGTH
* records
.size());
167 sprintf_s ( lineBuffer
, format
168 , "count", "sum", "min", "max"
169 , "line", "name", "file");
170 result
+= lineBuffer
;
172 for ( TRecords::const_iterator iter
= records
.begin(), end
= records
.end()
176 unsigned __int64 minValue
= (*iter
)->GetMinValue();
177 if (minValue
== ULLONG_MAX
)
180 sprintf_s ( lineBuffer
, format
182 , IntToStr ((*iter
)->GetCount()).c_str()
183 , IntToStr ((*iter
)->GetSum()).c_str()
184 , IntToStr (minValue
).c_str()
185 , IntToStr ((*iter
)->GetMaxValue()).c_str()
187 , IntToStr ((*iter
)->GetLine()).c_str()
189 , (*iter
)->GetFile());
191 result
+= lineBuffer
;
197 //////////////////////////////////////////////////////////////////////
199 //////////////////////////////////////////////////////////////////////
201 CProfilingRecord
* CProfilingInfo::Create ( const char* name
205 CProfilingRecord
* record
= new CProfilingRecord (name
, file
, line
);
206 records
.push_back (record
);