|
| 1 | +#ifndef PROTON_PROFILER_CUPTI_PC_SAMPLING_H_ |
| 2 | +#define PROTON_PROFILER_CUPTI_PC_SAMPLING_H_ |
| 3 | + |
| 4 | +#include "CuptiProfiler.h" |
| 5 | +#include "Driver/GPU/CudaApi.h" |
| 6 | +#include "Driver/GPU/CuptiApi.h" |
| 7 | +#include "Utility/Map.h" |
| 8 | +#include "Utility/Singleton.h" |
| 9 | +#include <atomic> |
| 10 | +#include <mutex> |
| 11 | + |
| 12 | +namespace proton { |
| 13 | + |
| 14 | +struct CubinData { |
| 15 | + size_t cubinCrc; |
| 16 | + const char *cubin; |
| 17 | + size_t cubinSize; |
| 18 | + |
| 19 | + struct LineInfoKey { |
| 20 | + uint32_t functionIndex; |
| 21 | + uint64_t pcOffset; |
| 22 | + |
| 23 | + bool operator<(const LineInfoKey &other) const { |
| 24 | + return functionIndex < other.functionIndex || |
| 25 | + (functionIndex == other.functionIndex && |
| 26 | + pcOffset < other.pcOffset); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + struct LineInfoValue { |
| 31 | + uint32_t lineNumber{}; |
| 32 | + const std::string functionName{}; |
| 33 | + const std::string dirName{}; |
| 34 | + const std::string fileName{}; |
| 35 | + |
| 36 | + LineInfoValue() = default; |
| 37 | + |
| 38 | + LineInfoValue(uint32_t lineNumber, const std::string &functionName, |
| 39 | + const std::string &dirName, const std::string &fileName) |
| 40 | + : lineNumber(lineNumber), functionName(functionName), dirName(dirName), |
| 41 | + fileName(fileName) {} |
| 42 | + }; |
| 43 | + |
| 44 | + std::map<LineInfoKey, LineInfoValue> lineInfo; |
| 45 | +}; |
| 46 | + |
| 47 | +struct ConfigureData { |
| 48 | + ConfigureData() = default; |
| 49 | + |
| 50 | + ~ConfigureData() { |
| 51 | + if (stallReasonNames) { |
| 52 | + for (size_t i = 0; i < numStallReasons; i++) { |
| 53 | + if (stallReasonNames[i]) |
| 54 | + std::free(stallReasonNames[i]); |
| 55 | + } |
| 56 | + std::free(stallReasonNames); |
| 57 | + } |
| 58 | + if (stallReasonIndices) |
| 59 | + std::free(stallReasonIndices); |
| 60 | + if (pcSamplingData.pPcData) { |
| 61 | + for (size_t i = 0; i < numValidStallReasons; ++i) { |
| 62 | + std::free(pcSamplingData.pPcData[i].stallReason); |
| 63 | + } |
| 64 | + std::free(pcSamplingData.pPcData); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + void initialize(CUcontext context); |
| 69 | + |
| 70 | + CUpti_PCSamplingConfigurationInfo configureStallReasons(); |
| 71 | + CUpti_PCSamplingConfigurationInfo configureSamplingPeriod(); |
| 72 | + CUpti_PCSamplingConfigurationInfo configureSamplingBuffer(); |
| 73 | + CUpti_PCSamplingConfigurationInfo configureScratchBuffer(); |
| 74 | + CUpti_PCSamplingConfigurationInfo configureHardwareBufferSize(); |
| 75 | + CUpti_PCSamplingConfigurationInfo configureStartStopControl(); |
| 76 | + CUpti_PCSamplingConfigurationInfo configureCollectionMode(); |
| 77 | + |
| 78 | + // The amount of data reserved on the GPU |
| 79 | + static constexpr size_t HardwareBufferSize = 128 * 1024 * 1024; |
| 80 | + // The amount of data copied from the hardware buffer each time |
| 81 | + static constexpr size_t ScratchBufferSize = 16 * 1024 * 1024; |
| 82 | + // The number of PCs copied from the scratch buffer each time |
| 83 | + static constexpr size_t DataBufferPCCount = 1024; |
| 84 | + // The sampling period in cycles = 2^frequency |
| 85 | + static constexpr uint32_t DefaultFrequency = 10; |
| 86 | + |
| 87 | + CUcontext context{}; |
| 88 | + uint32_t contextId; |
| 89 | + uint32_t numStallReasons{}; |
| 90 | + uint32_t numValidStallReasons{}; |
| 91 | + char **stallReasonNames{}; |
| 92 | + uint32_t *stallReasonIndices{}; |
| 93 | + std::map<size_t, size_t> stallReasonIndexToMetricIndex{}; |
| 94 | + std::set<size_t> notIssuedStallReasonIndices{}; |
| 95 | + CUpti_PCSamplingData pcSamplingData{}; |
| 96 | + // The memory storing configuration information has to be kept alive during |
| 97 | + // the profiling session |
| 98 | + std::vector<CUpti_PCSamplingConfigurationInfo> configurationInfos; |
| 99 | +}; |
| 100 | + |
| 101 | +class CuptiPCSampling : public Singleton<CuptiPCSampling> { |
| 102 | + |
| 103 | +public: |
| 104 | + CuptiPCSampling() = default; |
| 105 | + virtual ~CuptiPCSampling() = default; |
| 106 | + |
| 107 | + void initialize(CUcontext context); |
| 108 | + |
| 109 | + void start(CUcontext context); |
| 110 | + |
| 111 | + void stop(CUcontext context, uint64_t externId, bool isAPI); |
| 112 | + |
| 113 | + void finalize(CUcontext context); |
| 114 | + |
| 115 | + void loadModule(const char *cubin, size_t cubinSize); |
| 116 | + |
| 117 | + void unloadModule(const char *cubin, size_t cubinSize); |
| 118 | + |
| 119 | +private: |
| 120 | + ConfigureData *getConfigureData(uint32_t contextId); |
| 121 | + |
| 122 | + CubinData *getCubinData(uint64_t cubinCrc); |
| 123 | + |
| 124 | + void processPCSamplingData(ConfigureData *configureData, uint64_t externId, |
| 125 | + bool isAPI); |
| 126 | + |
| 127 | + ThreadSafeMap<uint32_t, ConfigureData> contextIdToConfigureData; |
| 128 | + // In case the same cubin is loaded multiple times, we need to keep track of |
| 129 | + // all of them |
| 130 | + ThreadSafeMap<size_t, std::pair<CubinData, /*count=*/size_t>> |
| 131 | + cubinCrcToCubinData; |
| 132 | + ThreadSafeSet<uint32_t> contextInitialized; |
| 133 | + |
| 134 | + std::atomic<bool> pcSamplingStarted{false}; |
| 135 | + std::mutex pcSamplingMutex{}; |
| 136 | + std::mutex contextMutex{}; |
| 137 | +}; |
| 138 | + |
| 139 | +} // namespace proton |
| 140 | + |
| 141 | +#endif // PROTON_PROFILER_CUPTI_PC_SAMPLING_H_ |
0 commit comments