forked from intel/pti-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_perfmon_collector.h
307 lines (247 loc) · 9.33 KB
/
gpu_perfmon_collector.h
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
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_SAMPLES_GPU_PERFMON_READ_GPU_INST_COUNT_COLLECTOR_H_
#define PTI_SAMPLES_GPU_PERFMON_READ_GPU_INST_COUNT_COLLECTOR_H_
#include <iostream>
#include <iomanip>
#include <map>
#include <mutex>
#include <sstream>
#include <vector>
#include "gen_binary_decoder.h"
#include "gtpin_utils.h"
struct PerfMonData {
uint32_t freq;
uint32_t cycles;
uint32_t pm;
uint32_t skipped;
};
struct PerfMonValue {
uint64_t cycles;
uint64_t pm;
};
struct KernelData {
std::string name;
uint32_t call_count;
std::vector<uint8_t> binary;
std::map<int32_t, PerfMonValue> block_map;
};
struct MemoryLocation {
int32_t offset;
GTPinMem location;
};
using KernelMemoryMap = std::map< GTPinKernel, std::vector<MemoryLocation> >;
using KernelDataMap = std::map<GTPinKernel, KernelData>;
class GpuPerfMonCollector {
public: // Interface
static GpuPerfMonCollector* Create() {
return new GpuPerfMonCollector();
}
~GpuPerfMonCollector() {}
const KernelDataMap& GetKernelDataMap() const {
return kernel_data_map_;
}
static void PrintResults(const KernelDataMap& kernel_data_map) {
if (kernel_data_map.size() == 0) {
return;
}
iga_gen_t arch = utils::gtpin::GetArch(GTPin_GetGenVersion());
if (arch == IGA_GEN_INVALID) {
std::cerr << "[WARNING] Unknown GPU architecture" << std::endl;
return;
}
for (auto data : kernel_data_map) {
if (data.second.call_count == 0) {
continue;
}
GenBinaryDecoder decoder(data.second.binary, arch);
std::vector<Instruction> instruction_list =
decoder.Disassemble();
PTI_ASSERT(instruction_list.size() > 0);
std::vector< std::pair<int32_t, PerfMonValue> > block_list;
for (auto block : data.second.block_map) {
block_list.push_back(std::make_pair(block.first, block.second));
}
PTI_ASSERT(block_list.size() > 0);
uint64_t total_cycles = 0;
uint64_t total_pm = 0;
for (auto value : block_list) {
total_cycles += value.second.cycles;
total_pm += value.second.pm;
}
if (total_cycles == 0) {
continue;
}
std::stringstream ss;
ss << "=== " << data.second.name << " (runs " <<
data.second.call_count << " times) ===";
std::string prologue = ss.str();
std::string epilogue(prologue.size(), '=');
std::cerr << prologue << std::endl;
size_t block_id = 1;
for (auto instruction : instruction_list) {
uint32_t block_offset = (block_id < block_list.size()) ?
block_list[block_id].first : UINT32_MAX;
if (instruction.offset >= block_offset) {
++block_id;
std::cerr << std::endl;
}
if (instruction.offset == instruction_list.front().offset ||
instruction.offset >= block_offset) {
uint64_t pm = block_list[block_id - 1].second.pm;
float percent = 100.0f * pm / total_cycles;
std::cerr << "[" << std::setw(7) << std::setprecision(2) <<
std::fixed << std::setfill(' ') << percent << "%]";
} else {
std::cerr << "[" << std::setw(8) << std::setfill(' ') << "-" << "]";
}
std::cerr << " 0x" << std::setw(4) << std::setfill('0') << std::hex <<
std::uppercase << instruction.offset << ": " << instruction.text <<
std::endl;
}
std::cerr << "Total PM percentage: " << std::setprecision(2) <<
std::fixed << 100.0f * total_pm / total_cycles << "%" << std::endl;
std::cerr << std::endl;
}
}
private: // Implementation Details
GpuPerfMonCollector() {
utils::gtpin::KnobAddBool("silent_warnings", false);
utils::gtpin::KnobAddInt("allow_sregs", 0);
utils::gtpin::KnobAddInt("use_global_ra", 1);
GTPin_OnKernelBuild(OnKernelBuild, this);
GTPin_OnKernelRun(OnKernelRun, this);
GTPin_OnKernelComplete(OnKernelComplete, this);
GTPIN_Start();
}
void AddKernelMemoryList(
GTPinKernel kernel,
const std::vector<MemoryLocation>& kernel_memory_list) {
PTI_ASSERT(kernel_memory_list.size() > 0);
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(kernel_memory_map_.count(kernel) == 0);
kernel_memory_map_[kernel] = kernel_memory_list;
}
std::vector<MemoryLocation> GetKernelMemoryList(GTPinKernel kernel) {
const std::lock_guard<std::mutex> lock(lock_);
auto it = kernel_memory_map_.find(kernel);
if (it == kernel_memory_map_.end()) {
return std::vector<MemoryLocation>();
}
return it->second;
}
void AddKernelData(GTPinKernel kernel, const KernelData& kernel_data) {
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(kernel_data_map_.count(kernel) == 0);
kernel_data_map_[kernel] = kernel_data;
}
void AppendKernelBlockValue(
GTPinKernel kernel, int32_t offset, PerfMonValue value) {
PTI_ASSERT(offset >= 0);
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(kernel_data_map_.count(kernel) == 1);
PTI_ASSERT(kernel_data_map_[kernel].block_map.count(offset) == 1);
kernel_data_map_[kernel].block_map[offset].cycles += value.cycles;
kernel_data_map_[kernel].block_map[offset].pm += value.pm;
}
void AppendKernelCallCount(GTPinKernel kernel, uint32_t call_count) {
const std::lock_guard<std::mutex> lock(lock_);
PTI_ASSERT(kernel_data_map_.count(kernel) == 1);
kernel_data_map_[kernel].call_count += call_count;
}
private: // Callbacks
static void OnKernelBuild(GTPinKernel kernel, void* data) {
GTPINTOOL_STATUS status = GTPINTOOL_STATUS_SUCCESS;
uint32_t num_regs = GTPin_PerfmonAvailableRegInstrument(kernel);
std::vector<MemoryLocation> kernel_memory_list;
KernelData kernel_data{};
for (GTPinBBL block = GTPin_BBLHead(kernel); GTPin_BBLValid(block);
block = GTPin_BBLNext(block)) {
GTPinINS head = GTPin_InsHead(block);
PTI_ASSERT(GTPin_InsValid(head));
int32_t offset = GTPin_InsOffset(head);
GTPinINS tail = GTPin_InsTail(block);
PTI_ASSERT(GTPin_InsValid(tail));
if (GTPin_InsIsEOT(head)) {
continue;
}
if (GTPin_InsIsChangingIP(tail)) {
if (head == tail) {
continue;
} else {
tail = GTPin_InsPrev(tail);
PTI_ASSERT(GTPin_InsValid(tail));
}
}
status = GTPin_PerfmonInstrumentPre(head);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
GTPinMem mem = nullptr;
status = GTPin_MemClaim(kernel, sizeof(PerfMonData), &mem);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
status = GTPin_PerfmonInstrumentPost_Mem(tail, mem, num_regs);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
kernel_memory_list.push_back({offset, mem});
PTI_ASSERT(kernel_data.block_map.count(offset) == 0);
kernel_data.block_map[offset] = {0, 0};
if (num_regs > 0) {
--num_regs;
}
}
uint32_t kernel_binary_size = 0;
status = GTPin_GetKernelBinary(kernel, 0, nullptr, &kernel_binary_size);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
kernel_data.binary.resize(kernel_binary_size);
status = GTPin_GetKernelBinary(
kernel, kernel_binary_size,
reinterpret_cast<char*>(kernel_data.binary.data()),
nullptr);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
char kernel_name[MAX_STR_SIZE];
status = GTPin_KernelGetName(kernel, MAX_STR_SIZE, kernel_name, nullptr);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
kernel_data.name = kernel_name;
kernel_data.call_count = 0;
GpuPerfMonCollector* collector =
reinterpret_cast<GpuPerfMonCollector*>(data);
PTI_ASSERT(collector != nullptr);
collector->AddKernelMemoryList(kernel, kernel_memory_list);
collector->AddKernelData(kernel, kernel_data);
}
static void OnKernelRun(GTPinKernelExec kernelExec, void* data) {
GTPINTOOL_STATUS status = GTPINTOOL_STATUS_SUCCESS;
GTPin_KernelProfilingActive(kernelExec, 1);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
}
static void OnKernelComplete(GTPinKernelExec kernelExec, void* data) {
GTPINTOOL_STATUS status = GTPINTOOL_STATUS_SUCCESS;
GpuPerfMonCollector* collector =
reinterpret_cast<GpuPerfMonCollector*>(data);
PTI_ASSERT(collector != nullptr);
GTPinKernel kernel = GTPin_KernelExec_GetKernel(kernelExec);
for (auto block : collector->GetKernelMemoryList(kernel)) {
uint32_t thread_count = GTPin_MemSampleLength(block.location);
PTI_ASSERT(thread_count > 0);
uint64_t total_cycles = 0, total_pm = 0;
PerfMonData value{};
for (uint32_t tid = 0; tid < thread_count; ++tid) {
status = GTPin_MemRead(
block.location, tid, sizeof(PerfMonData),
reinterpret_cast<char*>(&value), nullptr);
PTI_ASSERT(status == GTPINTOOL_STATUS_SUCCESS);
total_cycles += value.cycles;
total_pm += value.pm;
}
collector->AppendKernelBlockValue(
kernel, block.offset, {total_cycles, total_pm});
}
collector->AppendKernelCallCount(kernel, 1);
}
private: // Data
KernelMemoryMap kernel_memory_map_;
KernelDataMap kernel_data_map_;
std::mutex lock_;
};
#endif // PTI_SAMPLES_GPU_PERFMON_READ_GPU_INST_COUNT_COLLECTOR_H_