File size: 9,669 Bytes
563c80f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <List.h>
#include <Utils.h>
#include <iostream>
#include <vector>
#include <string>
#include <nvperf_host.h>
#include <nvperf_cuda_host.h>
#include <ScopeExit.h>

namespace NV {
    namespace Metric {
        namespace Enum {

            std::string GetMetricRollupOpString(NVPW_RollupOp rollupOp)
            {
                switch (rollupOp)
                {
                    case NVPW_ROLLUP_OP_AVG:
                        return ".avg";
                    case NVPW_ROLLUP_OP_MAX:
                        return ".max";
                    case NVPW_ROLLUP_OP_MIN:
                        return ".min";
                    case NVPW_ROLLUP_OP_SUM:
                        return ".sum";
                    default:
                        return "";
                }
                return "";
            }

            std::string GetSubMetricString(NVPW_Submetric subMetric)
            {
                // TODO Check for peak brust missing submetrics
                switch (subMetric)
                {
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED:
                        return ".peak_sustained";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_ACTIVE:
                        return ".peak_sustained_active";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_ACTIVE_PER_SECOND:
                        return ".peak_sustained_active.per_second";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_ELAPSED:
                        return ".peak_sustained_elapsed";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_ELAPSED_PER_SECOND:
                        return ".peak_sustained_elapsed.per_second";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_FRAME:
                        return ".peak_sustained_frame";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_FRAME_PER_SECOND:
                        return ".peak_sustained_frame.per_second";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_REGION:
                        return ".peak_sustained_region";
                    case NVPW_SUBMETRIC_PEAK_SUSTAINED_REGION_PER_SECOND:
                        return ".peak_sustained_region.per_second";
                    case NVPW_SUBMETRIC_PER_CYCLE_ACTIVE:
                        return ".per_cycle_active";
                    case NVPW_SUBMETRIC_PER_CYCLE_ELAPSED:
                        return ".per_cycle_elapsed";
                    case NVPW_SUBMETRIC_PER_CYCLE_IN_FRAME:
                        return ".per_cycle_in_frame";
                    case NVPW_SUBMETRIC_PER_CYCLE_IN_REGION:
                        return ".per_cycle_in_region";
                    case NVPW_SUBMETRIC_PER_SECOND:
                        return ".per_second";
                    case NVPW_SUBMETRIC_PCT_OF_PEAK_SUSTAINED_ACTIVE:
                        return ".pct_of_peak_sustained_active";
                    case NVPW_SUBMETRIC_PCT_OF_PEAK_SUSTAINED_ELAPSED:
                        return ".pct_of_peak_sustained_elapsed";
                    case NVPW_SUBMETRIC_PCT_OF_PEAK_SUSTAINED_FRAME:
                        return ".pct_of_peak_sustained_frame";
                    case NVPW_SUBMETRIC_PCT_OF_PEAK_SUSTAINED_REGION:
                        return ".pct_of_peak_sustained_region";
                    case NVPW_SUBMETRIC_MAX_RATE:
                        return ".max_rate";
                    case NVPW_SUBMETRIC_PCT:
                        return ".pct";
                    case NVPW_SUBMETRIC_RATIO:
                        return ".ratio";
                    case NVPW_SUBMETRIC_NONE:
                    default:
                        return "";
                }
                return "";
            }

            bool ExportSupportedMetrics(const char* chipName,
                                        bool listSubMetrics,
                                        const uint8_t* pCounterAvailabilityImage,
                                        std::vector<std::string>& pMetricsList)
            {
                NVPW_CUDA_MetricsEvaluator_CalculateScratchBufferSize_Params calculateScratchBufferSizeParam = {NVPW_CUDA_MetricsEvaluator_CalculateScratchBufferSize_Params_STRUCT_SIZE};
                calculateScratchBufferSizeParam.pChipName = chipName;
                calculateScratchBufferSizeParam.pCounterAvailabilityImage = pCounterAvailabilityImage;
                RETURN_IF_NVPW_ERROR(false, NVPW_CUDA_MetricsEvaluator_CalculateScratchBufferSize(&calculateScratchBufferSizeParam));

                std::vector<uint8_t> scratchBuffer(calculateScratchBufferSizeParam.scratchBufferSize);
                NVPW_CUDA_MetricsEvaluator_Initialize_Params metricEvaluatorInitializeParams = {NVPW_CUDA_MetricsEvaluator_Initialize_Params_STRUCT_SIZE};
                metricEvaluatorInitializeParams.scratchBufferSize = scratchBuffer.size();
                metricEvaluatorInitializeParams.pScratchBuffer = scratchBuffer.data();
                metricEvaluatorInitializeParams.pChipName = chipName;
                metricEvaluatorInitializeParams.pCounterAvailabilityImage = pCounterAvailabilityImage;
                RETURN_IF_NVPW_ERROR(false, NVPW_CUDA_MetricsEvaluator_Initialize(&metricEvaluatorInitializeParams));
                NVPW_MetricsEvaluator* metricEvaluator = metricEvaluatorInitializeParams.pMetricsEvaluator;

                for (auto i = 0; i < NVPW_MetricType::NVPW_METRIC_TYPE__COUNT; ++i)
                {
                    NVPW_MetricType metricType = static_cast<NVPW_MetricType>(i);
                    NVPW_MetricsEvaluator_GetMetricNames_Params getMetricNamesParams = { NVPW_MetricsEvaluator_GetMetricNames_Params_STRUCT_SIZE };
                    getMetricNamesParams.metricType = metricType;
                    getMetricNamesParams.pMetricsEvaluator = metricEvaluator;
                    RETURN_IF_NVPW_ERROR(false, NVPW_MetricsEvaluator_GetMetricNames(&getMetricNamesParams));
                    
                    for (size_t metricIndex = 0; metricIndex < getMetricNamesParams.numMetrics; ++metricIndex)
                    {
                        size_t metricNameBeginIndex = getMetricNamesParams.pMetricNameBeginIndices[metricIndex];
                        for (size_t rollupOpIndex = 0; rollupOpIndex < NVPW_RollupOp::NVPW_ROLLUP_OP__COUNT; ++rollupOpIndex)
                        {
                            std::string metricName = &getMetricNamesParams.pMetricNames[metricNameBeginIndex];
                            if (metricType != NVPW_MetricType::NVPW_METRIC_TYPE_RATIO)
                            {
                                metricName += GetMetricRollupOpString((NVPW_RollupOp)rollupOpIndex);
                            }
                        
                            if (listSubMetrics)
                            {
                                NVPW_MetricsEvaluator_GetSupportedSubmetrics_Params getSupportedSubmetricsParmas = { NVPW_MetricsEvaluator_GetSupportedSubmetrics_Params_STRUCT_SIZE };
                                getSupportedSubmetricsParmas.metricType = metricType;
                                getSupportedSubmetricsParmas.pMetricsEvaluator = metricEvaluator;
                                RETURN_IF_NVPW_ERROR(false, NVPW_MetricsEvaluator_GetSupportedSubmetrics(&getSupportedSubmetricsParmas));
                                for (size_t submetricIndex = 0; submetricIndex < getSupportedSubmetricsParmas.numSupportedSubmetrics; ++submetricIndex)
                                {
                                    std::string submetricName = std::string(metricName) + GetSubMetricString((NVPW_Submetric)getSupportedSubmetricsParmas.pSupportedSubmetrics[submetricIndex]);
                                    pMetricsList.push_back(submetricName);
                                }
                            }
                        }
                    }
                }

                NVPW_MetricsEvaluator_Destroy_Params metricEvaluatorDestroyParams = { NVPW_MetricsEvaluator_Destroy_Params_STRUCT_SIZE };
                metricEvaluatorDestroyParams.pMetricsEvaluator = metricEvaluator;
                RETURN_IF_NVPW_ERROR(false, NVPW_MetricsEvaluator_Destroy(&metricEvaluatorDestroyParams));
                return true;
            }

            bool ListSupportedChips()
            {
                NVPW_GetSupportedChipNames_Params getSupportedChipNames = { NVPW_GetSupportedChipNames_Params_STRUCT_SIZE };
                RETURN_IF_NVPW_ERROR(false, NVPW_GetSupportedChipNames(&getSupportedChipNames));
                std::cout << "\nNumber of supported chips : " << getSupportedChipNames.numChipNames;
                std::cout << "\nList of supported chips : \n";

                for (size_t i = 0; i < getSupportedChipNames.numChipNames; i++)
                {
                    std::cout << " " << getSupportedChipNames.ppChipNames[i] << "\t";
                }
                std::cout << "\n";
                return true;
            }

            bool ListMetrics(const char* chip, bool listSubMetrics, const uint8_t* pCounterAvailabilityImage) 
            {
                std::vector<std::string> metricsList;
                if (ExportSupportedMetrics(chip, listSubMetrics, pCounterAvailabilityImage, metricsList))
                {
                    std::cout << metricsList.size() << " metrics in total on the chip\nMetrics List : \n";
                    for (auto metric : metricsList)
                    {
                        std::cout << metric << "\n";
                    }
                    return true;
                }
                return false;
            }
        
        }
    }
}