xref: /aosp_15_r20/external/executorch/devtools/etdump/etdump_flatcc.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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 <cstdint>
12 
13 #include <executorch/runtime/core/event_tracer.h>
14 #include <executorch/runtime/core/span.h>
15 #include <executorch/runtime/platform/platform.h>
16 
17 #define ETDUMP_VERSION 0
18 
19 struct flatcc_builder;
20 
21 namespace executorch {
22 namespace etdump {
23 
24 namespace internal {
25 struct ETDumpStaticAllocator {
26   ETDumpStaticAllocator() = default;
27 
28   void
set_bufferETDumpStaticAllocator29   set_buffer(uint8_t* buffer, size_t total_buf_size, size_t alloc_buf_size) {
30     data = buffer;
31     data_size = alloc_buf_size;
32     allocated = 0;
33     out_size = total_buf_size - alloc_buf_size;
34     front_cursor = &buffer[alloc_buf_size];
35     front_left = out_size / 2;
36   }
37 
38   // Pointer to backing buffer to allocate from.
39   uint8_t* data{nullptr};
40 
41   // Size of backing buffer.
42   size_t data_size{0};
43 
44   // Current allocation offset.
45   size_t allocated{0};
46 
47   // Size of build buffer.
48   size_t out_size{0};
49 
50   // Pointer to front of build buffer.
51   uint8_t* front_cursor{nullptr};
52 
53   // Bytes left in front of front_cursor.
54   size_t front_left{0};
55 };
56 } // namespace internal
57 
58 struct ETDumpResult {
59   void* buf;
60   size_t size;
61 };
62 
63 class ETDumpGen : public ::executorch::runtime::EventTracer {
64  public:
65   ETDumpGen(::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0});
66   ~ETDumpGen() override;
67   void clear_builder();
68 
69   void create_event_block(const char* name) override;
70   virtual ::executorch::runtime::EventTracerEntry start_profiling(
71       const char* name,
72       ::executorch::runtime::ChainID chain_id = -1,
73       ::executorch::runtime::DebugHandle debug_handle = 0) override;
74   virtual void end_profiling(
75       ::executorch::runtime::EventTracerEntry prof_entry) override;
76   virtual ::executorch::runtime::EventTracerEntry start_profiling_delegate(
77       const char* name,
78       ::executorch::runtime::DebugHandle delegate_debug_index) override;
79   virtual void end_profiling_delegate(
80       ::executorch::runtime::EventTracerEntry prof_entry,
81       const void* metadata,
82       size_t metadata_len) override;
83   virtual void log_profiling_delegate(
84       const char* name,
85       ::executorch::runtime::DebugHandle delegate_debug_index,
86       et_timestamp_t start_time,
87       et_timestamp_t end_time,
88       const void* metadata,
89       size_t metadata_len) override;
90   virtual void track_allocation(
91       ::executorch::runtime::AllocatorID id,
92       size_t size) override;
93   virtual ::executorch::runtime::AllocatorID track_allocator(
94       const char* name) override;
95   virtual void log_evalue(
96       const ::executorch::runtime::EValue& evalue,
97       ::executorch::runtime::LoggedEValueType evalue_type =
98           ::executorch::runtime::LoggedEValueType::kIntermediateOutput)
99       override;
100   /**
101    * Log an intermediate tensor output from a delegate.
102    */
103   virtual void log_intermediate_output_delegate(
104       const char* name,
105       ::executorch::runtime::DebugHandle delegate_debug_index,
106       const exec_aten::Tensor& output) override;
107 
108   /**
109    * Log an intermediate tensor array output from a delegate.
110    */
111   virtual void log_intermediate_output_delegate(
112       const char* name,
113       ::executorch::runtime::DebugHandle delegate_debug_index,
114       const ::executorch::runtime::ArrayRef<exec_aten::Tensor> output) override;
115 
116   /**
117    * Log an intermediate int output from a delegate.
118    */
119   virtual void log_intermediate_output_delegate(
120       const char* name,
121       ::executorch::runtime::DebugHandle delegate_debug_index,
122       const int& output) override;
123 
124   /**
125    * Log an intermediate bool output from a delegate.
126    */
127   virtual void log_intermediate_output_delegate(
128       const char* name,
129       ::executorch::runtime::DebugHandle delegate_debug_index,
130       const bool& output) override;
131 
132   /**
133    * Log an intermediate double output from a delegate.
134    */
135   virtual void log_intermediate_output_delegate(
136       const char* name,
137       ::executorch::runtime::DebugHandle delegate_debug_index,
138       const double& output) override;
139   void set_debug_buffer(::executorch::runtime::Span<uint8_t> buffer);
140   ETDumpResult get_etdump_data();
141   size_t get_num_blocks();
142   bool is_static_etdump();
143   void reset();
144 
145  private:
146   enum class State {
147     Init,
148     BlockCreated,
149     AddingAllocators,
150     AddingEvents,
151     Done,
152   };
153 
154   void check_ready_to_add_events();
155   int64_t create_string_entry(const char* name);
156   size_t copy_tensor_to_debug_buffer(exec_aten::Tensor tensor);
157 
158   /**
159    * Templated helper function used to log various types of intermediate output.
160    * Supported types include tensor, tensor array, int, bool and double.
161    */
162   template <typename T>
163   void log_intermediate_output_delegate_helper(
164       const char* name,
165       ::executorch::runtime::DebugHandle delegate_debug_index,
166       const T& output);
167 
168   struct flatcc_builder* builder_;
169   size_t num_blocks_ = 0;
170   ::executorch::runtime::Span<uint8_t> debug_buffer_;
171   size_t debug_buffer_offset_ = 0;
172   int bundled_input_index_ = -1;
173   State state_ = State::Init;
174   struct internal::ETDumpStaticAllocator alloc_;
175 };
176 
177 } // namespace etdump
178 } // namespace executorch
179 
180 namespace torch {
181 namespace executor {
182 // TODO(T197294990): Remove these deprecated aliases once all users have moved
183 // to the new `::executorch` namespaces.
184 using etdump_result = ::executorch::etdump::ETDumpResult;
185 using ::executorch::etdump::ETDumpGen;
186 } // namespace executor
187 } // namespace torch
188