forked from GPUOpen-Tools/gpu_performance_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpa_profiler.cc
316 lines (251 loc) · 8.35 KB
/
gpa_profiler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//==============================================================================
// Copyright (c) 2016-2021 Advanced Micro Devices, Inc. All rights reserved.
/// @author AMD Developer Tools Team
/// @file
/// @brief Internal class to support profiling GPA calls themselves.
//==============================================================================
#ifdef ENABLE_PROFILING
#include "gpu_perf_api_common/gpa_profiler.h"
#include <assert.h>
#include <iostream>
#include <fstream>
using namespace std;
static DWORD GetCpuFreq()
{
DWORD buf_size = sizeof(DWORD);
DWORD freq_mhz;
HKEY key_handle;
// Open the key where the proc speed is hidden.
long l_error = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key_handle);
assert(l_error == ERROR_SUCCESS);
UNREFERENCED_PARAMETER(l_error);
// Query the key.
RegQueryValueExA(key_handle, "~MHz", nullptr, nullptr, (LPBYTE)&freq_mhz, &buf_size);
RegCloseKey(key_handle);
return freq_mhz;
}
static __int64 GetRdtscTicksPerSecond()
{
// Use this to control how much time is spent measuring the high freq clock.
// Measure for 1 / scaling seconds.
// Higher numbers mean less time spent measuring but larger potential error range.
const __int64 scaling = 4;
// Variables for the clock-cycles.
__int64 cycles_start = 0, cycles_stop = 0;
// Variables for the High-Res Performance Counter.
unsigned __int64 ctr_start = 0, freq = 0, ctr_stop = 0;
// Retrieve performance-counter frequency per second.
if (!QueryPerformanceFrequency((LARGE_INTEGER*)&freq))
{
return 0;
}
// Retrieve the current value of the performance counter.
QueryPerformanceCounter((LARGE_INTEGER*)&ctr_stop);
// Add the frequency to the counter-value.
ctr_stop += freq / scaling;
cycles_start = __rdtsc();
do
{
// Retrieve the value of the performance counter
// until 1 sec has gone by.
QueryPerformanceCounter((LARGE_INTEGER*)&ctr_start);
} while (ctr_start < ctr_stop);
cycles_stop = __rdtsc();
return (cycles_stop - cycles_start) * scaling;
}
Profiler::Profiler()
{
// Constructor uses init to determine tick rate on this computer.
rdtsc_ticks_per_second_ = 0;
Init();
}
bool Profiler::Init()
{
// Determine tick rate on this computer.
// Also SetThreadAffinityMask to ensure accurate timings since running on same core always.
Reset();
SetThreadAffinityMask(GetCurrentThread(), 1);
rdtsc_ticks_per_second_ = GetRdtscTicksPerSecond();
DWORD freq = GetCpuFreq();
DWORD freq_compare = DWORD(rdtsc_ticks_per_second_ / 1000000);
if (freq_compare != freq)
{
return false;
}
return true;
}
void Profiler::Reset()
{
started_timestamps_.clear();
total_time_below_parent_.clear();
function_map_.clear();
total_time_ = 0;
timing_errors_ = 0;
is_active_ = false;
}
void Profiler::Start()
{
Reset();
is_active_ = true;
start_time_ = __rdtsc();
}
void Profiler::Stop()
{
stop_time_ = __rdtsc();
is_active_ = false;
}
bool Profiler::EnterFunction(const char* function_name)
{
UNREFERENCED_PARAMETER(function_name);
__int64 start_timestamp = __rdtsc();
// Ignore any profiling calls when not active.
if (!Active())
{
return true;
}
// Add enter timestamp to stack.
started_timestamps_.push_back(start_timestamp);
// Start time below total at 0.
total_time_below_parent_.push_back(0);
return true;
}
bool Profiler::LeaveFunction(const char* function_name)
{
__int64 end_timestamp = __rdtsc();
// Ignore any profiling calls when not active.
if (!Active())
{
return true;
}
// Ensure that enter function was called prior to this leave.
assert(started_timestamps_.size() > 0);
if (started_timestamps_.size() == 0)
{
return false;
}
// Pop the start time for the function.
__int64 start_timestamp = started_timestamps_.back();
started_timestamps_.pop_back();
// Look up the function info for this function.
FunctionInfo& fi = function_map_[std::string(function_name)];
// Increment call count
fi.calls++;
// Compute delta between entering and leaving the function.
__int64 delta_for_this_invocation = end_timestamp - start_timestamp;
if (delta_for_this_invocation < 0)
{
// There is a problem with timing.
delta_for_this_invocation = 0;
timing_errors_++;
// Try and fix.
SetThreadAffinityMask(GetCurrentThread(), 1);
}
// Increment total time spent in profiled code.
fi.total_time_ += delta_for_this_invocation;
// Retrieve the time below total, which will have been updated by any functions below this one.
fi.time_below_ += total_time_below_parent_.back();
total_time_below_parent_.pop_back();
// Now contribute to parent of this function if there was one.
if (total_time_below_parent_.size() > 0)
{
// We are below a parent function, so add the time of this function to the time below total.
total_time_below_parent_[total_time_below_parent_.size() - 1] += delta_for_this_invocation;
}
else
{
// We are now evaluating a top level function call, add it's total time to the total time for profiling.
total_time_ += delta_for_this_invocation;
}
return true;
}
FunctionInfo& Profiler::GetFunctionInfo(const char* function_name)
{
FunctionInfo& fi = function_map_[std::string(function_name)];
return fi;
}
void Profiler::OutputTime(std::stringstream& ss, __int64 time) const
{
double t = (double)time / (double)rdtsc_ticks_per_second_;
ss << t;
ss << "(";
ss << time;
ss << ")";
}
// Create a string containing a report of all profiling.
std::string Profiler::GenerateReport()
{
// If profiling, stop.
if (Active())
{
Stop();
}
std::stringstream str;
str << "Time profiling = ";
OutputTime(str, stop_time_ - start_time_);
str << std::endl;
str << "Total time in functions = ";
OutputTime(str, total_time_);
str << std::endl;
str << "% time in functions = ";
double percent_of_total = (double)total_time_ * 100.0 / (double)(stop_time_ - start_time_);
str << percent_of_total;
str << std::endl;
str << "Timing errors = ";
str << timing_errors_;
str << std::endl;
str << std::endl;
str << "Function, # of calls, in % of total time, total % of total time, total time, time per call, total time in, time in per call";
str << std::endl;
for (std::map<std::string, FunctionInfo>::const_iterator it = function_map_.begin(); it != function_map_.end(); it++)
{
// str << "Function ";
#if 0
str << it->first;
str << ":[";
str << it->second.calls;
str << "] tot: ";
outputTime(str, it->second.total_time_);
str << ", in: ";
outputTime(str, it->second.total_time_ - it->second.time_below_);
str << ", %: ";
double pct = ((double)(it->second.total_time_ - it->second.time_below_) / (double)total_time_) * 100.0;
str << pct;
str << endl;
#endif
str << it->first;
str << ", ";
str << it->second.calls;
str << ", ";
double pct = ((double)(it->second.total_time_ - it->second.time_below_) / (double)total_time_) * 100.0;
str << pct;
str << ", ";
double percent_total = ((double)it->second.total_time_ / (double)total_time_) * 100.0;
str << percent_total;
str << ", ";
OutputTime(str, it->second.total_time_);
str << ", ";
OutputTime(str, it->second.total_time_ / it->second.calls);
str << ", ";
OutputTime(str, it->second.total_time_ - it->second.time_below_);
str << ", ";
OutputTime(str, (it->second.total_time_ - it->second.time_below_) / it->second.calls);
str << ", ";
str << endl;
}
return str.str();
}
// Generate a profiling report and write it to disk using the specified file_name.
void Profiler::WriteReport(std::string file_name)
{
string s = GenerateReport();
ofstream file(file_name.c_str(), ios::out);
if (file.is_open())
{
file << s;
file.close();
}
}
Profiler profiler_singleton;
#else
// !ENABLE_PROFILING
#endif // ENABLE_PROFILING