-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextensions.cpp
More file actions
331 lines (266 loc) · 20.3 KB
/
extensions.cpp
File metadata and controls
331 lines (266 loc) · 20.3 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <nvperf_host.h>
#include <nvperf_cuda_host.h>
#include <nvperf_target.h>
#include <iostream>
#include "extensions.h"
namespace NV {
namespace Metric {
namespace Config {
bool GetRawMetricRequests(NVPA_MetricsContext* pMetricsContext,
std::vector<std::string> metricNames,
std::vector<NVPA_RawMetricRequest>& rawMetricRequests,
std::vector<std::string>& temp) {
std::string reqName;
bool isolated = true;
bool keepInstances = true;
for (auto& metricName : metricNames)
{
NV::Metric::Parser::ParseMetricNameString(metricName, &reqName, &isolated, &keepInstances);
/* Bug in collection with collection of metrics without instances, keep it to true*/
keepInstances = true;
NVPW_MetricsContext_GetMetricProperties_Begin_Params getMetricPropertiesBeginParams = { NVPW_MetricsContext_GetMetricProperties_Begin_Params_STRUCT_SIZE };
getMetricPropertiesBeginParams.pMetricsContext = pMetricsContext;
getMetricPropertiesBeginParams.pMetricName = reqName.c_str();
RETURN_IF_NVPW_ERROR(false, NVPW_MetricsContext_GetMetricProperties_Begin(&getMetricPropertiesBeginParams));
for (const char** ppMetricDependencies = getMetricPropertiesBeginParams.ppRawMetricDependencies; *ppMetricDependencies; ++ppMetricDependencies)
{
temp.push_back(*ppMetricDependencies);
}
NVPW_MetricsContext_GetMetricProperties_End_Params getMetricPropertiesEndParams = { NVPW_MetricsContext_GetMetricProperties_End_Params_STRUCT_SIZE };
getMetricPropertiesEndParams.pMetricsContext = pMetricsContext;
RETURN_IF_NVPW_ERROR(false, NVPW_MetricsContext_GetMetricProperties_End(&getMetricPropertiesEndParams));
}
for (auto& rawMetricName : temp)
{
NVPA_RawMetricRequest metricRequest = { NVPA_RAW_METRIC_REQUEST_STRUCT_SIZE };
metricRequest.pMetricName = rawMetricName.c_str();
metricRequest.isolated = isolated;
metricRequest.keepInstances = keepInstances;
rawMetricRequests.push_back(metricRequest);
}
return true;
}
bool GetConfigImage(std::string chipName, std::vector<std::string> metricNames, std::vector<uint8_t>& configImage, const uint8_t* pCounterAvailabilityImage)
{
NVPW_CUDA_MetricsContext_Create_Params metricsContextCreateParams = { NVPW_CUDA_MetricsContext_Create_Params_STRUCT_SIZE };
metricsContextCreateParams.pChipName = chipName.c_str();
RETURN_IF_NVPW_ERROR(false, NVPW_CUDA_MetricsContext_Create(&metricsContextCreateParams));
NVPW_MetricsContext_Destroy_Params metricsContextDestroyParams = { NVPW_MetricsContext_Destroy_Params_STRUCT_SIZE };
metricsContextDestroyParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
SCOPE_EXIT([&]() { NVPW_MetricsContext_Destroy((NVPW_MetricsContext_Destroy_Params *)&metricsContextDestroyParams); });
std::vector<NVPA_RawMetricRequest> rawMetricRequests;
std::vector<std::string> temp;
GetRawMetricRequests(metricsContextCreateParams.pMetricsContext, metricNames, rawMetricRequests, temp);
NVPA_RawMetricsConfigOptions metricsConfigOptions = { NVPA_RAW_METRICS_CONFIG_OPTIONS_STRUCT_SIZE };
metricsConfigOptions.activityKind = NVPA_ACTIVITY_KIND_PROFILER;
metricsConfigOptions.pChipName = chipName.c_str();
NVPA_RawMetricsConfig* pRawMetricsConfig;
RETURN_IF_NVPW_ERROR(false, NVPA_RawMetricsConfig_Create(&metricsConfigOptions, &pRawMetricsConfig));
if(pCounterAvailabilityImage)
{
NVPW_RawMetricsConfig_SetCounterAvailability_Params setCounterAvailabilityParams = {NVPW_RawMetricsConfig_SetCounterAvailability_Params_STRUCT_SIZE};
setCounterAvailabilityParams.pRawMetricsConfig = pRawMetricsConfig;
setCounterAvailabilityParams.pCounterAvailabilityImage = pCounterAvailabilityImage;
RETURN_IF_NVPW_ERROR(false, NVPW_RawMetricsConfig_SetCounterAvailability(&setCounterAvailabilityParams));
}
NVPW_RawMetricsConfig_Destroy_Params rawMetricsConfigDestroyParams = { NVPW_RawMetricsConfig_Destroy_Params_STRUCT_SIZE };
rawMetricsConfigDestroyParams.pRawMetricsConfig = pRawMetricsConfig;
SCOPE_EXIT([&]() { NVPW_RawMetricsConfig_Destroy((NVPW_RawMetricsConfig_Destroy_Params *)&rawMetricsConfigDestroyParams); });
NVPW_RawMetricsConfig_BeginPassGroup_Params beginPassGroupParams = { NVPW_RawMetricsConfig_BeginPassGroup_Params_STRUCT_SIZE };
beginPassGroupParams.pRawMetricsConfig = pRawMetricsConfig;
RETURN_IF_NVPW_ERROR(false, NVPW_RawMetricsConfig_BeginPassGroup(&beginPassGroupParams));
NVPW_RawMetricsConfig_AddMetrics_Params addMetricsParams = { NVPW_RawMetricsConfig_AddMetrics_Params_STRUCT_SIZE };
addMetricsParams.pRawMetricsConfig = pRawMetricsConfig;
addMetricsParams.pRawMetricRequests = &rawMetricRequests[0];
addMetricsParams.numMetricRequests = rawMetricRequests.size();
RETURN_IF_NVPW_ERROR(false, NVPW_RawMetricsConfig_AddMetrics(&addMetricsParams));
NVPW_RawMetricsConfig_EndPassGroup_Params endPassGroupParams = { NVPW_RawMetricsConfig_EndPassGroup_Params_STRUCT_SIZE };
endPassGroupParams.pRawMetricsConfig = pRawMetricsConfig;
RETURN_IF_NVPW_ERROR(false, NVPW_RawMetricsConfig_EndPassGroup(&endPassGroupParams));
NVPW_RawMetricsConfig_GenerateConfigImage_Params generateConfigImageParams = { NVPW_RawMetricsConfig_GenerateConfigImage_Params_STRUCT_SIZE };
generateConfigImageParams.pRawMetricsConfig = pRawMetricsConfig;
RETURN_IF_NVPW_ERROR(false, NVPW_RawMetricsConfig_GenerateConfigImage(&generateConfigImageParams));
NVPW_RawMetricsConfig_GetConfigImage_Params getConfigImageParams = { NVPW_RawMetricsConfig_GetConfigImage_Params_STRUCT_SIZE };
getConfigImageParams.pRawMetricsConfig = pRawMetricsConfig;
getConfigImageParams.bytesAllocated = 0;
getConfigImageParams.pBuffer = NULL;
RETURN_IF_NVPW_ERROR(false, NVPW_RawMetricsConfig_GetConfigImage(&getConfigImageParams));
configImage.resize(getConfigImageParams.bytesCopied);
getConfigImageParams.bytesAllocated = configImage.size();
getConfigImageParams.pBuffer = &configImage[0];
RETURN_IF_NVPW_ERROR(false, NVPW_RawMetricsConfig_GetConfigImage(&getConfigImageParams));
return true;
}
bool GetCounterDataPrefixImage(std::string chipName, std::vector<std::string> metricNames, std::vector<uint8_t>& counterDataImagePrefix)
{
NVPW_CUDA_MetricsContext_Create_Params metricsContextCreateParams = { NVPW_CUDA_MetricsContext_Create_Params_STRUCT_SIZE };
metricsContextCreateParams.pChipName = chipName.c_str();
RETURN_IF_NVPW_ERROR(false, NVPW_CUDA_MetricsContext_Create(&metricsContextCreateParams));
NVPW_MetricsContext_Destroy_Params metricsContextDestroyParams = { NVPW_MetricsContext_Destroy_Params_STRUCT_SIZE };
metricsContextDestroyParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
SCOPE_EXIT([&]() { NVPW_MetricsContext_Destroy((NVPW_MetricsContext_Destroy_Params *)&metricsContextDestroyParams); });
std::vector<NVPA_RawMetricRequest> rawMetricRequests;
std::vector<std::string> temp;
GetRawMetricRequests(metricsContextCreateParams.pMetricsContext, metricNames, rawMetricRequests, temp);
NVPW_CounterDataBuilder_Create_Params counterDataBuilderCreateParams = { NVPW_CounterDataBuilder_Create_Params_STRUCT_SIZE };
counterDataBuilderCreateParams.pChipName = chipName.c_str();
RETURN_IF_NVPW_ERROR(false, NVPW_CounterDataBuilder_Create(&counterDataBuilderCreateParams));
NVPW_CounterDataBuilder_Destroy_Params counterDataBuilderDestroyParams = { NVPW_CounterDataBuilder_Destroy_Params_STRUCT_SIZE };
counterDataBuilderDestroyParams.pCounterDataBuilder = counterDataBuilderCreateParams.pCounterDataBuilder;
SCOPE_EXIT([&]() { NVPW_CounterDataBuilder_Destroy((NVPW_CounterDataBuilder_Destroy_Params *)&counterDataBuilderDestroyParams); });
NVPW_CounterDataBuilder_AddMetrics_Params addMetricsParams = { NVPW_CounterDataBuilder_AddMetrics_Params_STRUCT_SIZE };
addMetricsParams.pCounterDataBuilder = counterDataBuilderCreateParams.pCounterDataBuilder;
addMetricsParams.pRawMetricRequests = &rawMetricRequests[0];
addMetricsParams.numMetricRequests = rawMetricRequests.size();
RETURN_IF_NVPW_ERROR(false, NVPW_CounterDataBuilder_AddMetrics(&addMetricsParams));
size_t counterDataPrefixSize = 0;
NVPW_CounterDataBuilder_GetCounterDataPrefix_Params getCounterDataPrefixParams = { NVPW_CounterDataBuilder_GetCounterDataPrefix_Params_STRUCT_SIZE };
getCounterDataPrefixParams.pCounterDataBuilder = counterDataBuilderCreateParams.pCounterDataBuilder;
getCounterDataPrefixParams.bytesAllocated = 0;
getCounterDataPrefixParams.pBuffer = NULL;
RETURN_IF_NVPW_ERROR(false, NVPW_CounterDataBuilder_GetCounterDataPrefix(&getCounterDataPrefixParams));
counterDataImagePrefix.resize(getCounterDataPrefixParams.bytesCopied);
getCounterDataPrefixParams.bytesAllocated = counterDataImagePrefix.size();
getCounterDataPrefixParams.pBuffer = &counterDataImagePrefix[0];
RETURN_IF_NVPW_ERROR(false, NVPW_CounterDataBuilder_GetCounterDataPrefix(&getCounterDataPrefixParams));
return true;
}
}
}
}
namespace NV {
namespace Metric {
namespace Eval {
std::string GetHwUnit(const std::string& metricName)
{
return metricName.substr(0, metricName.find("__", 0));
}
bool GetMetricGpuValue(std::string chipName, std::vector<uint8_t> counterDataImage, std::vector<std::string> metricNames, std::vector<MetricNameValue>& metricNameValueMap) {
if (!counterDataImage.size()) {
std::cout << "Counter Data Image is empty!\n";
return false;
}
NVPW_CUDA_MetricsContext_Create_Params metricsContextCreateParams = { NVPW_CUDA_MetricsContext_Create_Params_STRUCT_SIZE };
metricsContextCreateParams.pChipName = chipName.c_str();
RETURN_IF_NVPW_ERROR(false, NVPW_CUDA_MetricsContext_Create(&metricsContextCreateParams));
NVPW_MetricsContext_Destroy_Params metricsContextDestroyParams = { NVPW_MetricsContext_Destroy_Params_STRUCT_SIZE };
metricsContextDestroyParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
SCOPE_EXIT([&]() { NVPW_MetricsContext_Destroy((NVPW_MetricsContext_Destroy_Params *)&metricsContextDestroyParams); });
NVPW_CounterData_GetNumRanges_Params getNumRangesParams = { NVPW_CounterData_GetNumRanges_Params_STRUCT_SIZE };
getNumRangesParams.pCounterDataImage = &counterDataImage[0];
RETURN_IF_NVPW_ERROR(false, NVPW_CounterData_GetNumRanges(&getNumRangesParams));
std::vector<std::string> reqName;
reqName.resize(metricNames.size());
bool isolated = true;
bool keepInstances = true;
std::vector<const char*> metricNamePtrs;
metricNameValueMap.resize(metricNames.size());
for (size_t metricIndex = 0; metricIndex < metricNames.size(); ++metricIndex) {
NV::Metric::Parser::ParseMetricNameString(metricNames[metricIndex], &reqName[metricIndex], &isolated, &keepInstances);
metricNamePtrs.push_back(reqName[metricIndex].c_str());
metricNameValueMap[metricIndex].metricName = metricNames[metricIndex];
metricNameValueMap[metricIndex].numRanges = getNumRangesParams.numRanges;
}
for (size_t rangeIndex = 0; rangeIndex < getNumRangesParams.numRanges; ++rangeIndex) {
std::vector<const char*> descriptionPtrs;
NVPW_Profiler_CounterData_GetRangeDescriptions_Params getRangeDescParams = { NVPW_Profiler_CounterData_GetRangeDescriptions_Params_STRUCT_SIZE };
getRangeDescParams.pCounterDataImage = &counterDataImage[0];
getRangeDescParams.rangeIndex = rangeIndex;
RETURN_IF_NVPW_ERROR(false, NVPW_Profiler_CounterData_GetRangeDescriptions(&getRangeDescParams));
descriptionPtrs.resize(getRangeDescParams.numDescriptions);
getRangeDescParams.ppDescriptions = &descriptionPtrs[0];
RETURN_IF_NVPW_ERROR(false, NVPW_Profiler_CounterData_GetRangeDescriptions(&getRangeDescParams));
std::string rangeName;
for (size_t descriptionIndex = 0; descriptionIndex < getRangeDescParams.numDescriptions; ++descriptionIndex)
{
if (descriptionIndex)
{
rangeName += "/";
}
rangeName += descriptionPtrs[descriptionIndex];
}
std::vector<double> gpuValues;
gpuValues.resize(metricNames.size());
NVPW_MetricsContext_SetCounterData_Params setCounterDataParams = { NVPW_MetricsContext_SetCounterData_Params_STRUCT_SIZE };
setCounterDataParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
setCounterDataParams.pCounterDataImage = &counterDataImage[0];
setCounterDataParams.isolated = true;
setCounterDataParams.rangeIndex = rangeIndex;
NVPW_MetricsContext_SetCounterData(&setCounterDataParams);
NVPW_MetricsContext_EvaluateToGpuValues_Params evalToGpuParams = { NVPW_MetricsContext_EvaluateToGpuValues_Params_STRUCT_SIZE };
evalToGpuParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
evalToGpuParams.numMetrics = metricNamePtrs.size();
evalToGpuParams.ppMetricNames = &metricNamePtrs[0];
evalToGpuParams.pMetricValues = &gpuValues[0];
NVPW_MetricsContext_EvaluateToGpuValues(&evalToGpuParams);
for (size_t metricIndex = 0; metricIndex < metricNames.size(); ++metricIndex) {
metricNameValueMap[metricIndex].rangeNameMetricValueMap.push_back(std::make_pair(rangeName, gpuValues[metricIndex]));
}
}
return true;
}
bool PrintMetricValues(std::string chipName, std::vector<uint8_t> counterDataImage, std::vector<std::string> metricNames) {
if (!counterDataImage.size()) {
std::cout << "Counter Data Image is empty!\n";
return false;
}
NVPW_CUDA_MetricsContext_Create_Params metricsContextCreateParams = { NVPW_CUDA_MetricsContext_Create_Params_STRUCT_SIZE };
metricsContextCreateParams.pChipName = chipName.c_str();
RETURN_IF_NVPW_ERROR(false, NVPW_CUDA_MetricsContext_Create(&metricsContextCreateParams));
NVPW_MetricsContext_Destroy_Params metricsContextDestroyParams = { NVPW_MetricsContext_Destroy_Params_STRUCT_SIZE };
metricsContextDestroyParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
SCOPE_EXIT([&]() { NVPW_MetricsContext_Destroy((NVPW_MetricsContext_Destroy_Params *)&metricsContextDestroyParams); });
NVPW_CounterData_GetNumRanges_Params getNumRangesParams = { NVPW_CounterData_GetNumRanges_Params_STRUCT_SIZE };
getNumRangesParams.pCounterDataImage = &counterDataImage[0];
RETURN_IF_NVPW_ERROR(false, NVPW_CounterData_GetNumRanges(&getNumRangesParams));
std::vector<std::string> reqName;
reqName.resize(metricNames.size());
bool isolated = true;
bool keepInstances = true;
std::vector<const char*> metricNamePtrs;
for (size_t metricIndex = 0; metricIndex < metricNames.size(); ++metricIndex) {
NV::Metric::Parser::ParseMetricNameString(metricNames[metricIndex], &reqName[metricIndex], &isolated, &keepInstances);
metricNamePtrs.push_back(reqName[metricIndex].c_str());
}
for (size_t rangeIndex = 0; rangeIndex < getNumRangesParams.numRanges; ++rangeIndex) {
std::vector<const char*> descriptionPtrs;
NVPW_Profiler_CounterData_GetRangeDescriptions_Params getRangeDescParams = { NVPW_Profiler_CounterData_GetRangeDescriptions_Params_STRUCT_SIZE };
getRangeDescParams.pCounterDataImage = &counterDataImage[0];
getRangeDescParams.rangeIndex = rangeIndex;
RETURN_IF_NVPW_ERROR(false, NVPW_Profiler_CounterData_GetRangeDescriptions(&getRangeDescParams));
descriptionPtrs.resize(getRangeDescParams.numDescriptions);
getRangeDescParams.ppDescriptions = &descriptionPtrs[0];
RETURN_IF_NVPW_ERROR(false, NVPW_Profiler_CounterData_GetRangeDescriptions(&getRangeDescParams));
std::string rangeName;
for (size_t descriptionIndex = 0; descriptionIndex < getRangeDescParams.numDescriptions; ++descriptionIndex)
{
if (descriptionIndex)
{
rangeName += "/";
}
rangeName += descriptionPtrs[descriptionIndex];
}
const bool isolated = true;
std::vector<double> gpuValues;
gpuValues.resize(metricNames.size());
NVPW_MetricsContext_SetCounterData_Params setCounterDataParams = { NVPW_MetricsContext_SetCounterData_Params_STRUCT_SIZE };
setCounterDataParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
setCounterDataParams.pCounterDataImage = &counterDataImage[0];
setCounterDataParams.isolated = true;
setCounterDataParams.rangeIndex = rangeIndex;
NVPW_MetricsContext_SetCounterData(&setCounterDataParams);
NVPW_MetricsContext_EvaluateToGpuValues_Params evalToGpuParams = { NVPW_MetricsContext_EvaluateToGpuValues_Params_STRUCT_SIZE };
evalToGpuParams.pMetricsContext = metricsContextCreateParams.pMetricsContext;
evalToGpuParams.numMetrics = metricNamePtrs.size();
evalToGpuParams.ppMetricNames = &metricNamePtrs[0];
evalToGpuParams.pMetricValues = &gpuValues[0];
NVPW_MetricsContext_EvaluateToGpuValues(&evalToGpuParams);
for (size_t metricIndex = 0; metricIndex < metricNames.size(); ++metricIndex) {
std::cout << "rangeName: " << rangeName << "\tmetricName: " << metricNames[metricIndex] << "\tgpuValue: " << gpuValues[metricIndex] << std::endl;
}
}
return true;
}
}
}
}