|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <executorch/runtime/core/event_tracer.h> |
| 12 | + |
| 13 | +/** |
| 14 | + * @file |
| 15 | + * |
| 16 | + * This file contains the hooks that are inserted across various parts of the |
| 17 | + * core runtime code to call into the EventTracer class for logging of profiling |
| 18 | + * and debugging events. Any calls made to the EventTracer from the runtime must |
| 19 | + * be made via these hooks. |
| 20 | + * Users shouldn't directly add these hooks in their code and it's meant only |
| 21 | + * for usage in ExecuTorch internal code. |
| 22 | + * |
| 23 | + * The benefit of defining these hooks is that we can easily control whether or |
| 24 | + * not we want to compile in the EventTracer code based on the status of the |
| 25 | + * ET_EVENT_TRACER_ENABLED flag. |
| 26 | + */ |
| 27 | + |
| 28 | +namespace torch { |
| 29 | +namespace executor { |
| 30 | +namespace internal { |
| 31 | + |
| 32 | +/** |
| 33 | + * This class enables scope based profiling where needed using RAII. |
| 34 | + * Profiling will be started when the object is created and will end |
| 35 | + * when the object goes out of scope. |
| 36 | + */ |
| 37 | +class EventTracerProfileScope final { |
| 38 | + public: |
| 39 | + EventTracerProfileScope(EventTracer* event_tracer, const char* name) { |
| 40 | + event_tracer_ = event_tracer; |
| 41 | + if (event_tracer_ == nullptr) { |
| 42 | + return; |
| 43 | + } |
| 44 | + event_entry_ = event_tracer->start_profiling(name); |
| 45 | + } |
| 46 | + |
| 47 | + ~EventTracerProfileScope() { |
| 48 | + if (event_tracer_ == nullptr) { |
| 49 | + return; |
| 50 | + } |
| 51 | + event_tracer_->end_profiling(event_entry_); |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + EventTracer* event_tracer_; |
| 56 | + EventTracerEntry event_entry_; |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * This class helps us set and then clear out the chain id and debug handle |
| 61 | + * values stored in the event tracer class using RAII. This is typically called |
| 62 | + * in the executor loop before entering the codegen layer to configure the chain |
| 63 | + * id and debug handle of the current instruction being executed. |
| 64 | + * After we return from the kernel execution we can then reset the chain id and |
| 65 | + * debug handle to defaults when this object goes out of scope. |
| 66 | + */ |
| 67 | +class EventTracerProfileInstructionScope final { |
| 68 | + public: |
| 69 | + EventTracerProfileInstructionScope( |
| 70 | + EventTracer* event_tracer, |
| 71 | + ChainID chain_idx, |
| 72 | + DebugHandle debug_handle) { |
| 73 | + event_tracer_ = event_tracer; |
| 74 | + if (event_tracer_ == nullptr) { |
| 75 | + return; |
| 76 | + } |
| 77 | + event_tracer_->set_chain_debug_handle(chain_idx, debug_handle); |
| 78 | + } |
| 79 | + |
| 80 | + ~EventTracerProfileInstructionScope() { |
| 81 | + if (event_tracer_ == nullptr) { |
| 82 | + return; |
| 83 | + } |
| 84 | + event_tracer_->set_chain_debug_handle(kUnsetChainId, kUnsetDebugHandle); |
| 85 | + } |
| 86 | + |
| 87 | + private: |
| 88 | + EventTracer* event_tracer_; |
| 89 | +}; |
| 90 | + |
| 91 | +/** |
| 92 | + * Create a new event block with the specified name. Any events logged |
| 93 | + * after this will be associated with this new event block. |
| 94 | + */ |
| 95 | +inline void event_tracer_create_event_block( |
| 96 | + EventTracer* event_tracer, |
| 97 | + char const* name) { |
| 98 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 99 | + if (event_tracer) { |
| 100 | + event_tracer->create_event_block(name); |
| 101 | + } |
| 102 | +#else //! ET_EVENT_TRACER_ENABLED |
| 103 | + (void)event_tracer; |
| 104 | + (void)name; |
| 105 | +#endif |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Explicitly mark the beginning of a new profiling event. This returns |
| 110 | + * an instance of an EventTracerEntry object that the user needs to keep |
| 111 | + * around and pass into the corresponding event_tracer_end_profiling_event |
| 112 | + * call. |
| 113 | + */ |
| 114 | +inline EventTracerEntry event_tracer_begin_profiling_event( |
| 115 | + EventTracer* event_tracer, |
| 116 | + char const* name) { |
| 117 | + EventTracerEntry event_tracer_entry; |
| 118 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 119 | + if (event_tracer) { |
| 120 | + event_tracer_entry = event_tracer->start_profiling(name); |
| 121 | + } |
| 122 | +#else //! ET_EVENT_TRACER_ENABLED |
| 123 | + (void)event_tracer; |
| 124 | + (void)name; |
| 125 | +#endif |
| 126 | + // There is no active tracer; this value will be ignored. |
| 127 | + return event_tracer_entry; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Mark the end of a profiling event passing in the entry token |
| 132 | + * returned by a previous call to ET_EVENT_TRACER_BEGIN_PROFILING_EVENT. |
| 133 | + */ |
| 134 | +inline void event_tracer_end_profiling_event( |
| 135 | + EventTracer* event_tracer, |
| 136 | + EventTracerEntry event) { |
| 137 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 138 | + if (event_tracer) { |
| 139 | + event_tracer->end_profiling(event); |
| 140 | + } |
| 141 | +#else //! ET_EVENT_TRACER_ENABLED |
| 142 | + (void)event_tracer; |
| 143 | + (void)event; |
| 144 | +#endif |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Start the tracking of the allocator represented by this name and returns |
| 149 | + * an AllocatorID that will be used to track all subsequent allocations done by |
| 150 | + * this allocator. |
| 151 | + */ |
| 152 | +inline AllocatorID event_tracer_track_allocator( |
| 153 | + EventTracer* event_tracer, |
| 154 | + const char* name) { |
| 155 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 156 | + if (event_tracer) { |
| 157 | + return event_tracer->track_allocator(name); |
| 158 | + } |
| 159 | +#else //! ET_EVENT_TRACER_ENABLED |
| 160 | + (void)event_tracer; |
| 161 | + (void)name; |
| 162 | +#endif |
| 163 | + // There is no active tracer; this value will be ignored. |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +/// Log the allocation event done via the allocator represented by id. |
| 168 | +inline void event_tracer_track_allocation( |
| 169 | + EventTracer* event_tracer, |
| 170 | + AllocatorID id, |
| 171 | + size_t size) { |
| 172 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 173 | + if (event_tracer) { |
| 174 | + event_tracer->track_allocation(id, size); |
| 175 | + } |
| 176 | +#else //! ET_EVENT_TRACER_ENABLED |
| 177 | + (void)event_tracer; |
| 178 | + (void)id; |
| 179 | + (void)size; |
| 180 | +#endif |
| 181 | +} |
| 182 | + |
| 183 | +} // namespace internal |
| 184 | +} // namespace executor |
| 185 | +} // namespace torch |
0 commit comments