-
Notifications
You must be signed in to change notification settings - Fork 957
Expand file tree
/
Copy pathevent_tracer_hooks.h
More file actions
340 lines (315 loc) · 9.68 KB
/
event_tracer_hooks.h
File metadata and controls
340 lines (315 loc) · 9.68 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <executorch/runtime/core/event_tracer.h>
/**
* @file
*
* This file contains the hooks that are inserted across various parts of the
* core runtime code to call into the EventTracer class for logging of profiling
* and debugging events. Any calls made to the EventTracer from the runtime must
* be made via these hooks.
* Users shouldn't directly add these hooks in their code and it's meant only
* for usage in ExecuTorch internal code.
*
* The benefit of defining these hooks is that we can easily control whether or
* not we want to compile in the EventTracer code based on the status of the
* ET_EVENT_TRACER_ENABLED flag.
*
* TODO(dbort): Make this a private header of runtime/executor. It only contains
* runtime-internal functions and should not be part of the public set of
* headers.
*/
namespace executorch {
namespace ET_RUNTIME_NAMESPACE {
namespace internal {
/**
* This class enables scope based profiling where needed using RAII for
* operators only. If operator profiling is disabled then this class is a no-op.
*/
class EventTracerProfileOpScope final {
public:
EventTracerProfileOpScope(EventTracer* event_tracer, const char* name) {
#ifdef ET_EVENT_TRACER_ENABLED
event_tracer_ = event_tracer;
if (event_tracer_ == nullptr) {
return;
}
if (event_tracer_->event_tracer_profiling_level() >
executorch::runtime::EventTracerProfilingLevel::kProfileMethodOnly) {
event_entry_ = event_tracer->start_profiling(name);
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)name;
#endif
}
~EventTracerProfileOpScope() {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer_ == nullptr) {
return;
}
if (event_tracer_->event_tracer_profiling_level() >
executorch::runtime::EventTracerProfilingLevel::kProfileMethodOnly) {
event_tracer_->end_profiling(event_entry_);
}
#endif
}
private:
#ifdef ET_EVENT_TRACER_ENABLED
EventTracer* event_tracer_;
EventTracerEntry event_entry_;
#endif
};
using EventTracerProfileScope = EventTracerProfileOpScope;
/**
* This class enables scope based profiling where needed using RAII.
* Profiling will be started when the object is created and will end
* when the object goes out of scope. This is specifically intended to
* be used for profiling methods in the runtime.
*/
class EventTracerProfileMethodScope final {
public:
EventTracerProfileMethodScope(EventTracer* event_tracer, const char* name) {
#ifdef ET_EVENT_TRACER_ENABLED
event_tracer_ = event_tracer;
if (event_tracer_ == nullptr) {
return;
}
event_entry_ = event_tracer->start_profiling(name);
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)name;
#endif
}
~EventTracerProfileMethodScope() {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer_ == nullptr) {
return;
}
event_tracer_->end_profiling(event_entry_);
#endif
}
private:
#ifdef ET_EVENT_TRACER_ENABLED
EventTracer* event_tracer_;
EventTracerEntry event_entry_;
#endif
};
/**
* This class helps us set and then clear out the chain id and debug handle
* values stored in the event tracer class using RAII. This is typically called
* in the executor loop before entering the codegen layer to configure the chain
* id and debug handle of the current instruction being executed.
* After we return from the kernel execution we can then reset the chain id and
* debug handle to defaults when this object goes out of scope.
*/
class EventTracerProfileInstructionScope final {
public:
EventTracerProfileInstructionScope(
EventTracer* event_tracer,
ChainID chain_idx,
DebugHandle debug_handle) {
#ifdef ET_EVENT_TRACER_ENABLED
event_tracer_ = event_tracer;
if (event_tracer_ == nullptr) {
return;
}
event_tracer_->set_chain_debug_handle(chain_idx, debug_handle);
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)chain_idx;
(void)debug_handle;
#endif
}
~EventTracerProfileInstructionScope() {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer_ == nullptr) {
return;
}
event_tracer_->set_chain_debug_handle(kUnsetChainId, kUnsetDebugHandle);
#endif
}
private:
#ifdef ET_EVENT_TRACER_ENABLED
EventTracer* event_tracer_;
#endif
};
inline bool event_tracer_enabled() {
#ifdef ET_EVENT_TRACER_ENABLED
return true;
#else //! ET_EVENT_TRACER_ENABLED
return false;
#endif
}
/**
* Create a new event block with the specified name. Any events logged
* after this will be associated with this new event block.
*/
inline void event_tracer_create_event_block(
EventTracer* event_tracer,
char const* name) {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer) {
event_tracer->create_event_block(name);
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)name;
#endif
}
/**
* Explicitly mark the beginning of a new profiling event. This returns
* an instance of an EventTracerEntry object that the user needs to keep
* around and pass into the corresponding event_tracer_end_profiling_event
* call.
*/
inline EventTracerEntry event_tracer_begin_profiling_event(
EventTracer* event_tracer,
char const* name) {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer) {
return event_tracer->start_profiling(name);
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)name;
#endif
// There is no active tracer; this value will be ignored.
return EventTracerEntry();
}
/**
* Mark the end of a profiling event passing in the entry token
* returned by a previous call to ET_EVENT_TRACER_BEGIN_PROFILING_EVENT.
*/
inline void event_tracer_end_profiling_event(
EventTracer* event_tracer,
EventTracerEntry event) {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer) {
event_tracer->end_profiling(event);
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)event;
#endif
}
/**
* Start the tracking of the allocator represented by this name and returns
* an AllocatorID that will be used to track all subsequent allocations done by
* this allocator.
*/
inline AllocatorID event_tracer_track_allocator(
EventTracer* event_tracer,
const char* name) {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer) {
return event_tracer->track_allocator(name);
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)name;
#endif
// There is no active tracer; this value will be ignored.
return 0;
}
/// Log the allocation event done via the allocator represented by id.
inline void event_tracer_track_allocation(
EventTracer* event_tracer,
AllocatorID id,
size_t size) {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer) {
event_tracer->track_allocation(id, size);
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)id;
(void)size;
#endif
}
/// Log an intermediate value.
inline void event_tracer_log_evalue(EventTracer* event_tracer, EValue& evalue) {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer) {
if (event_tracer->event_tracer_debug_level() >=
EventTracerDebugLogLevel::kIntermediateOutputs) {
event_tracer->log_evalue(evalue, LoggedEValueType::kIntermediateOutput);
}
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)evalue;
#endif
}
/// Log a program output.
inline void event_tracer_log_evalue_output(
EventTracer* event_tracer,
const EValue& evalue) {
#ifdef ET_EVENT_TRACER_ENABLED
/*
* If debugging via event tracer is enabled but intermediate output logging is
* disabled then we want to only log the outputs.
*/
if (event_tracer) {
if (event_tracer->event_tracer_debug_level() >=
EventTracerDebugLogLevel::kProgramOutputs) {
event_tracer->log_evalue(evalue, LoggedEValueType::kProgramOutput);
}
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)evalue;
#endif
}
// Set the bundled input index of the current bundled input being used by the
// method.
inline void event_tracer_set_bundled_input_index(
EventTracer* event_tracer,
int bundled_input_index) {
#ifdef ET_EVENT_TRACER_ENABLED
if (event_tracer) {
event_tracer->set_bundled_input_index(bundled_input_index);
}
#else //! ET_EVENT_TRACER_ENABLED
(void)event_tracer;
(void)bundled_input_index;
#endif
}
} // namespace internal
} // namespace ET_RUNTIME_NAMESPACE
} // namespace executorch
namespace torch {
namespace executor {
namespace internal {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
event_tracer_begin_profiling_event;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
event_tracer_create_event_block;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
event_tracer_end_profiling_event;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::event_tracer_log_evalue;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
event_tracer_log_evalue_output;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
event_tracer_set_bundled_input_index;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
event_tracer_track_allocation;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
event_tracer_track_allocator;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
EventTracerProfileInstructionScope;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::
EventTracerProfileMethodScope;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::EventTracerProfileOpScope;
using ::executorch::ET_RUNTIME_NAMESPACE::internal::EventTracerProfileScope;
} // namespace internal
} // namespace executor
} // namespace torch