Mneme
Mneme a tool allowing recording the execution of a GPU (CUDA/HIP) kernel and replaying that kernel as an independent executable.
Loading...
Searching...
No Matches
MnemeKernelTimer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <atomic>
7#include <condition_variable>
8#include <cstdint>
9#include <memory>
10#include <mutex>
11#include <optional>
12#include <vector>
13
14#include <llvm/ADT/DenseMap.h>
15
16namespace mneme {
17
18// Times recorded kernels' launches with the vendor profiler. Durations arrive
19// on a profiler thread, which only queues them; the recording thread folds
20// them into the record database on later launches and in drain().
21class KernelTimer {
22public:
23 // Loads the vendor profiler and registers the timer with it. HIP must do
24 // this before the runtime initializes; get() does it otherwise.
25 static void configure();
26
27 // Aborts when the profiler is unavailable.
28 static KernelTimer &get();
29
30 // Returns the launch's token, which end() or abandon() must receive, or
31 // nothing once the profiler has stopped.
32 std::optional<uint64_t> begin(const LaunchHandle &Launch);
33 void end(uint64_t Token);
34 // Ends a launch that failed, so it never produces a dispatch record.
35 void abandon(uint64_t Token);
36 void collect(RecordDatabase &DB);
37 // Waits for the durations of all launches, which must have completed.
38 void drain(RecordDatabase &DB);
39
40private:
41 bool Configured = false;
42 std::unique_ptr<DispatchTracer> Tracer;
43 std::atomic<bool> Running{false};
44 std::mutex Mutex;
45 std::condition_variable Arrived;
46 std::vector<DispatchRecord> Measured;
47 llvm::DenseMap<uint64_t, LaunchHandle> Pending;
48 uint64_t LastToken = 0;
49
50 KernelTimer();
51 ~KernelTimer();
52 static KernelTimer &instance();
53 // The profiler tool's initialize and finalize callbacks.
54 static void initialize();
55 static void deinitialize();
56 static void onDispatch(void *Data, const DispatchRecord &Record);
57 // The caller must hold Mutex.
58 void addMeasured(RecordDatabase &DB);
59};
60
61} // namespace mneme
void collect(RecordDatabase &DB)
Definition MnemeKernelTimer.cpp:140
void end(uint64_t Token)
Definition MnemeKernelTimer.cpp:106
static KernelTimer & get()
Definition MnemeKernelTimer.cpp:68
void drain(RecordDatabase &DB)
Definition MnemeKernelTimer.cpp:145
static void configure()
Definition MnemeKernelTimer.cpp:38
void abandon(uint64_t Token)
Definition MnemeKernelTimer.cpp:117
std::optional< uint64_t > begin(const LaunchHandle &Launch)
Definition MnemeKernelTimer.cpp:97
Definition MnemeSnapshot.hpp:479
Definition MnemeAnnotation.cpp:3
auto & instance
Definition rocr_profile.cpp:205
Definition MnemeDispatchTracer.hpp:19
Definition MnemeSnapshot.hpp:50