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
MnemeDispatchTracer.hpp
Go to the documentation of this file.
1#pragma once
2
3// Kernel dispatch tracing with the vendor profiler, shared by recording and
4// replay so that both measure kernel durations the same way. A launch is one
5// kernel launch call on the host; a dispatch is one kernel the GPU runs. Each
6// dispatch is tagged with the external correlation token pushed on the
7// launching thread.
8
9#include <chrono>
10#include <condition_variable>
11#include <cstdint>
12#include <mutex>
13#include <optional>
14#include <string_view>
15
16namespace mneme {
17
18// Timestamps are in nanoseconds, and KernelId is only known on HIP.
20 uint64_t Token;
21 uint64_t KernelId;
22 uint64_t Start;
23 uint64_t End;
24
25 // Returns nothing when the profiler reported the dispatch ending before it
26 // started.
27 std::optional<uint64_t> duration() const {
28 if (End < Start)
29 return std::nullopt;
30 return End - Start;
31 }
32};
33
34// Called on a profiler thread, or on the thread that flushes the profiler,
35// only for dispatches launched while a token was pushed.
36using DispatchSink = void (*)(void *Data, const DispatchRecord &Record);
37
38inline constexpr auto RecordWaitTimeout = std::chrono::seconds(10);
39
40// Only start() reports failure, by returning false; any other profiler error
41// aborts.
43public:
44 virtual ~DispatchTracer() = default;
45
46 // Returns false when the profiler is unavailable, e.g. because another tool
47 // uses it. On HIP this must run in a rocprofiler tool's initialize callback;
48 // on CUDA only one tracer per process may start.
49 virtual bool start(DispatchSink Sink, void *Data) = 0;
50 // Runs in the HIP tool's finalize callback, so errors are ignored.
51 virtual void stop() = 0;
52
53 virtual void push(uint64_t Token) = 0;
54 virtual uint64_t pop() = 0;
55 virtual void flush() = 0;
56
57 // Returns whether Record is a dispatch of the kernel whose source symbol is
58 // KernelName. A launch can produce more than one dispatch with its token,
59 // because the runtime may first dispatch its own kernels, such as heap
60 // initialization.
61 virtual bool isDispatchOf(const DispatchRecord &Record,
62 std::string_view KernelName) = 0;
63
64 // Why start() can fail or never run, for the error that stops recording.
65 virtual const char *unavailableReason() const = 0;
66};
67
68// Flushes Tracer until Done() holds or Timeout passes. Lock guards the state
69// Done() reads and is the lock the sink takes.
70template <typename DoneFn>
71void waitForRecords(std::unique_lock<std::mutex> &Lock,
72 std::condition_variable &Arrived, DispatchTracer &Tracer,
73 DoneFn Done, std::chrono::steady_clock::duration Timeout) {
74 auto Deadline = std::chrono::steady_clock::now() + Timeout;
75 while (!Done() && std::chrono::steady_clock::now() < Deadline) {
76 // Flushing may run the sink on this thread, which takes Lock.
77 Lock.unlock();
78 Tracer.flush();
79 Lock.lock();
80 Arrived.wait_for(Lock, std::chrono::milliseconds(10), Done);
81 }
82}
83
84using ToolCallback = void (*)();
85
86// Defined by the vendor tracer source, which links the vendor profiler.
87// Recording loads them from the dispatch tracer library only when kernel
88// timing is requested, so that it does not otherwise depend on the profiler.
89extern "C" {
91// Registers a profiler tool whose Initialize starts a tracer. HIP requires
92// this before the runtime initializes; CUDA calls Initialize immediately.
94}
95
96inline constexpr const char *DispatchTracerLibrary =
97 "libmneme_dispatch_tracer.so";
98
99} // namespace mneme
Definition MnemeDispatchTracer.hpp:42
virtual void flush()=0
virtual const char * unavailableReason() const =0
virtual ~DispatchTracer()=default
virtual bool isDispatchOf(const DispatchRecord &Record, std::string_view KernelName)=0
virtual uint64_t pop()=0
virtual void push(uint64_t Token)=0
virtual void stop()=0
virtual bool start(DispatchSink Sink, void *Data)=0
Definition MnemeAnnotation.cpp:3
constexpr const char * DispatchTracerLibrary
Definition MnemeDispatchTracer.hpp:96
void waitForRecords(std::unique_lock< std::mutex > &Lock, std::condition_variable &Arrived, DispatchTracer &Tracer, DoneFn Done, std::chrono::steady_clock::duration Timeout)
Definition MnemeDispatchTracer.hpp:71
DispatchTracer * mnemeCreateDispatchTracer()
Definition MnemeDispatchTracerCUDA.cpp:160
void(*)() ToolCallback
Definition MnemeDispatchTracer.hpp:84
constexpr auto RecordWaitTimeout
Definition MnemeDispatchTracer.hpp:38
void mnemeConfigureDispatchTool(ToolCallback Initialize, ToolCallback)
Definition MnemeDispatchTracerCUDA.cpp:165
void(*)(void *Data, const DispatchRecord &Record) DispatchSink
Definition MnemeDispatchTracer.hpp:36
Definition MnemeDispatchTracer.hpp:19
std::optional< uint64_t > duration() const
Definition MnemeDispatchTracer.hpp:27
uint64_t KernelId
Definition MnemeDispatchTracer.hpp:21
uint64_t Start
Definition MnemeDispatchTracer.hpp:22
uint64_t End
Definition MnemeDispatchTracer.hpp:23
uint64_t Token
Definition MnemeDispatchTracer.hpp:20