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
|
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
#include <stdlib.h>
#include <unordered_map>
#include <gtest/gtest.h>
// TODO(T90238193)
// @lint-ignore-every CLANGTIDY facebook-hte-RelativeInclude
#include "CuptiRangeProfilerApi.h"
namespace KINETO_NAMESPACE {
#if HAS_CUPTI_RANGE_PROFILER
class MockCuptiRBProfilerSession : public CuptiRBProfilerSession {
public:
explicit MockCuptiRBProfilerSession(const CuptiRangeProfilerOptions& opts)
: CuptiRBProfilerSession(opts) {}
void beginPass() override {
LOG(INFO) << " Mock CUPTI begin pass";
passes_started++;
}
bool endPass() override {
passes_ended++;
return true;
}
void flushCounterData() override {}
void pushRange(const std::string& rangeName) override {
LOG(INFO) << " Mock CUPTI pushrange ( " << rangeName << " )";
ranges_started++;
}
void popRange() override {
LOG(INFO) << " Mock CUPTI poprange";
ranges_ended++;
}
void stop() override {
profilerStopTs_ = std::chrono::high_resolution_clock::now();
runChecks();
}
void enable() override {
enabled = true;
}
void disable() override {}
CuptiProfilerResult evaluateMetrics(bool /*verbose*/) override {
return getResults()[deviceId()];
}
protected:
void startInternal(
CUpti_ProfilerRange profilerRange,
CUpti_ProfilerReplayMode profilerReplayMode) override {
profilerStartTs_ = std::chrono::high_resolution_clock::now();
curRange_ = profilerRange;
curReplay_ = profilerReplayMode;
}
private:
void runChecks() {
EXPECT_EQ(passes_started, passes_ended);
EXPECT_EQ(ranges_started, ranges_ended);
}
public:
int passes_started = 0;
int passes_ended = 0;
int ranges_started = 0;
int ranges_ended = 0;
bool enabled = false;
static std::unordered_map<int, CuptiProfilerResult>& getResults();
};
struct MockCuptiRBProfilerSessionFactory : ICuptiRBProfilerSessionFactory {
std::unique_ptr<CuptiRBProfilerSession> make(
const CuptiRangeProfilerOptions& _opts) override {
auto opts = _opts;
opts.unitTest = true;
return std::make_unique<MockCuptiRBProfilerSession>(opts);
}
MockCuptiRBProfilerSession* asDerived(CuptiRBProfilerSession* base) {
return dynamic_cast<MockCuptiRBProfilerSession*>(base);
}
};
inline void simulateCudaContextCreate(CUcontext context, uint32_t dev) {
testing::trackCudaCtx(
context, dev, CUPTI_CBID_RESOURCE_CONTEXT_CREATED);
}
inline void simulateCudaContextDestroy(CUcontext context, uint32_t dev) {
testing::trackCudaCtx(
context, dev, CUPTI_CBID_RESOURCE_CONTEXT_DESTROY_STARTING);
}
inline void simulateKernelLaunch(
CUcontext context, const std::string& kernelName) {
testing::trackCudaKernelLaunch(context, kernelName.c_str());
}
#endif // HAS_CUPTI_RANGE_PROFILER
} // namespace KINETO_NAMESPACE
|