|
| 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 | +#include <executorch/runtime/platform/platform.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <cstdint> |
| 12 | + |
| 13 | +#pragma once |
| 14 | + |
| 15 | +namespace torch { |
| 16 | +namespace executor { |
| 17 | + |
| 18 | +/// Represents an allocator id returned by track_allocator. |
| 19 | +typedef uint32_t AllocatorID; |
| 20 | +/// Represents the chain id that will be passed in by the user during |
| 21 | +/// event logging. |
| 22 | +typedef int32_t ChainID; |
| 23 | +/// Represents the debug handle that is generally associated with each |
| 24 | +/// op executed in the runtime. |
| 25 | +typedef uint32_t DebugHandle; |
| 26 | + |
| 27 | +/// Default id's for chain id and debug handle. |
| 28 | +constexpr ChainID kUnsetChainId = -1; |
| 29 | +constexpr DebugHandle kUnsetDebugHandle = 0; |
| 30 | + |
| 31 | +/** |
| 32 | + * This is the struct which should be returned when a profiling event is |
| 33 | + * started. This is used to uniquely identify that profiling event and will be |
| 34 | + * required to be passed into the end_profiling call to signal that the event |
| 35 | + * identified by this struct has completed. |
| 36 | + **/ |
| 37 | +struct EventTracerEntry { |
| 38 | + /// An event id to uniquely identify this event that was generated during a |
| 39 | + /// call to start the tracking of an event. |
| 40 | + int64_t event_id; |
| 41 | + /// The chain to which this event belongs to. |
| 42 | + ChainID chain_id; |
| 43 | + /// The debug handle corresponding to this event. |
| 44 | + DebugHandle debug_handle; |
| 45 | + /// The time at which this event was started to be tracked. |
| 46 | + et_timestamp_t start_time; |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * EventTracer is a class that users can inherit and implement to |
| 51 | + * log/serialize/stream etc. the profiling and debugging events that are |
| 52 | + * generated at runtime for a model. An example of this is the ETDump |
| 53 | + * implementation in the SDK codebase that serializes these events to a |
| 54 | + * flatbuffer. |
| 55 | + */ |
| 56 | +class EventTracer { |
| 57 | + public: |
| 58 | + /** |
| 59 | + * Start a new event block (can consist of profiling and/or debugging events.) |
| 60 | + * identified by this name. A block is conceptually a set of events that we |
| 61 | + * want to group together. e.g. all the events that occur during the call to |
| 62 | + * execute() (i.e. model inference) could be categorized as a block. |
| 63 | + * |
| 64 | + * @param[in] name A human readable identifier for the event block. Users |
| 65 | + * calling this interface do not need to keep the memory pointed to by this |
| 66 | + * pointer around. The string must be copied over into internal memory during |
| 67 | + * this call. |
| 68 | + */ |
| 69 | + virtual void create_event_block(const char* name) = 0; |
| 70 | + |
| 71 | + /** |
| 72 | + * Start the profiling of the event identified by name and debug_handle. |
| 73 | + * The user can pass in a chain_id and debug_handle to this call, or leave |
| 74 | + * them empty (default values) which would then result in the chain_id and |
| 75 | + * debug handle stored within (set by set_chain_debug_handle) this class to be |
| 76 | + * used. |
| 77 | + * @param[in] name Human readable name for the profiling event. Users calling |
| 78 | + * this interface do not need to keep the memory pointed to by this pointer |
| 79 | + * around. The string must be copied over into internal memory during this |
| 80 | + * call. |
| 81 | + * @param[in] chain_id The id of the chain to which this event belongs to. If |
| 82 | + * -1 is passed in the chain_id and debug_handle stored in the class |
| 83 | + * internally will be used. |
| 84 | + * @param[in] debug_handle Debug handle generated ahead-of-time during model |
| 85 | + * compilation. |
| 86 | + * |
| 87 | + * @return Returns an instance of EventTracerEntry which should be passed back |
| 88 | + * into the end_profiling() call. |
| 89 | + */ |
| 90 | + virtual EventTracerEntry start_profiling( |
| 91 | + const char* name, |
| 92 | + ChainID chain_id = kUnsetChainId, |
| 93 | + DebugHandle debug_handle = kUnsetDebugHandle) = 0; |
| 94 | + |
| 95 | + /** |
| 96 | + * End the profiling of the event identified by prof_entry |
| 97 | + * |
| 98 | + * @param[in] prof_entry Value returned by a call to start_profiling |
| 99 | + */ |
| 100 | + virtual void end_profiling(EventTracerEntry prof_entry) = 0; |
| 101 | + |
| 102 | + /** |
| 103 | + * Track this allocation done via a MemoryAllocator which had profiling |
| 104 | + * enabled on it. |
| 105 | + * |
| 106 | + * @param[in] id Allocator id generated by a call to track_allocator. |
| 107 | + * @param[in] size The size of the allocation done, in bytes. |
| 108 | + */ |
| 109 | + virtual void track_allocation(AllocatorID id, size_t size) = 0; |
| 110 | + |
| 111 | + /** |
| 112 | + * Generate an allocator id for this memory allocator that will be used in the |
| 113 | + * future to identify all the allocations done by this allocator. |
| 114 | + * |
| 115 | + * @param[in] name Human readable name for the allocator. Users calling |
| 116 | + * this interface do not need to keep the memory pointed to by this pointer |
| 117 | + * around. The string should be copied over into internal memory during this |
| 118 | + * call. |
| 119 | + * |
| 120 | + * @return Identifier to uniquely identify this allocator. |
| 121 | + */ |
| 122 | + virtual AllocatorID track_allocator(const char* name) = 0; |
| 123 | + |
| 124 | + /** |
| 125 | + * Helper function to set the chain id ands debug handle. Users have two |
| 126 | + * options, the first is that they can directly pass in the chain id and debug |
| 127 | + * handle to start_profiling or they can explicitly set them through this |
| 128 | + * helper before calling start_profiling. |
| 129 | + * |
| 130 | + * The reason this helper exists is to |
| 131 | + * solve a specific problem. We want to do profiling logging inside the |
| 132 | + * codegen layer which calls the kernels. The problem though is that the |
| 133 | + * codegen layer doesn't have access to these ids when calling |
| 134 | + * start_profiling. |
| 135 | + * |
| 136 | + * Users should ideally use these within a RAII scope interface to make sure |
| 137 | + * that these values are unset after the end_profiling call. If non-default |
| 138 | + * values are passed into the start_profiling call they will always be given |
| 139 | + * precedence over the values set by this interface. |
| 140 | + * |
| 141 | + * So what we do is call this helper in method.cpp before |
| 142 | + * we hit the codegen layer and in the codegen layer we do a start_profiling |
| 143 | + * call without passing in a chain_id or debug_handle. This ensures that the |
| 144 | + * values set via this helper are the ones associated with that call. |
| 145 | + * |
| 146 | + * @param[in] chain_id Chain id of the current instruction being exectuted. |
| 147 | + * @param[in] debug_handle Debug handle of the current instruction being |
| 148 | + * executed. In this context debug handle and instruction id are the same |
| 149 | + * thing. |
| 150 | + */ |
| 151 | + void set_chain_debug_handle(ChainID chain_id, DebugHandle debug_handle) { |
| 152 | + chain_id_ = chain_id; |
| 153 | + debug_handle_ = debug_handle; |
| 154 | + } |
| 155 | + |
| 156 | + ChainID get_current_chain_id() { |
| 157 | + return chain_id_; |
| 158 | + } |
| 159 | + |
| 160 | + DebugHandle get_current_debug_handle() { |
| 161 | + return debug_handle_; |
| 162 | + } |
| 163 | + |
| 164 | + virtual ~EventTracer() {} |
| 165 | + |
| 166 | + protected: |
| 167 | + ChainID chain_id_ = kUnsetChainId; |
| 168 | + DebugHandle debug_handle_ = kUnsetDebugHandle; |
| 169 | +}; |
| 170 | + |
| 171 | +} // namespace executor |
| 172 | +} // namespace torch |
0 commit comments