7
7
*/
8
8
9
9
#include " executorch/sdk/etdump/etdump_flatcc.h"
10
+ #include < stdio.h>
10
11
#include < string.h>
11
12
#include " executorch/runtime/platform/assert.h"
12
13
@@ -18,7 +19,7 @@ ETDumpGen::ETDumpGen(void* buffer, size_t buf_size) {
18
19
// Initialize the flatcc builder using the buffer and buffer size
19
20
flatcc_builder_init (&builder);
20
21
flatbuffers_buffer_start (&builder, etdump_ETDump_file_identifier);
21
- etdump_ETDump_start (&builder);
22
+ etdump_ETDump_start_as_root_with_size (&builder);
22
23
etdump_ETDump_version_add (&builder, ETDUMP_VERSION);
23
24
etdump_ETDump_run_data_start (&builder);
24
25
etdump_ETDump_run_data_push_start (&builder);
@@ -49,6 +50,19 @@ int64_t ETDumpGen::create_string_entry(const char* name) {
49
50
return flatbuffers_string_create_str (&builder, name);
50
51
}
51
52
53
+ // ETDumpGen has the following possible states, ETDumpGen_Init,
54
+ // ETDumpGen_Block_Created, ETDumpGen_Adding_Allocators,
55
+ // ETDumpGen_Adding_Events. Right after boot-up the state of ETDump will be
56
+ // ETDumpGen_Init. At this point we have an option of adding allocators that
57
+ // we want to track. Once we've completed adding the allocators we want to track
58
+ // we will close the allocators table and move ETDumpGen to the
59
+ // ETDumpGen_Adding_Events state. After this point we can start adding events to
60
+ // ETDump as we wish.
61
+ // The reason we need to maintain this state machine inside of ETDumpGen is
62
+ // because, once a table of one type has been closed and another table of a
63
+ // different type is opened after it we cannot open another table of the first
64
+ // type again. In this case once we close the allocators table and start pushing
65
+ // to the events table we cannot push to the allocators table again.
52
66
void ETDumpGen::check_ready_to_add_events () {
53
67
if (etdump_gen_state != ETDumpGen_Adding_Events) {
54
68
ET_CHECK_MSG (
@@ -68,12 +82,12 @@ EventTracerEntry ETDumpGen::start_profiling(
68
82
ChainID chain_id,
69
83
DebugHandle debug_handle) {
70
84
EventTracerEntry prof_entry;
71
- prof_entry.event_id = create_string_entry (name);
85
+ prof_entry.event_id = name != nullptr ? create_string_entry (name) : -1 ;
86
+ prof_entry.delegate_event_id_type = DelegateDebugIdType::kNone ;
72
87
73
- if (chain_id == -1 && debug_handle == 0 ) {
88
+ if (chain_id == -1 ) {
74
89
prof_entry.chain_id = chain_id_;
75
90
prof_entry.debug_handle = debug_handle_;
76
-
77
91
} else {
78
92
prof_entry.chain_id = chain_id;
79
93
prof_entry.debug_handle = debug_handle;
@@ -84,22 +98,116 @@ EventTracerEntry ETDumpGen::start_profiling(
84
98
85
99
// TODO: Update all occurrences of the ProfileEvent calls once the
86
100
// EventTracerEntry struct is updated.
87
- void ETDumpGen::end_profiling (EventTracerEntry prof_entry) {
101
+ EventTracerEntry ETDumpGen::start_profiling_delegate (
102
+ const char * name,
103
+ DebugHandle delegate_debug_index) {
104
+ ET_CHECK_MSG (
105
+ (name == nullptr ) ^ (delegate_debug_index == -1 ),
106
+ " Only name or delegate_debug_index can be valid. Check DelegateMappingBuilder documentation for more details." );
107
+ check_ready_to_add_events ();
108
+ EventTracerEntry prof_entry;
109
+ DelegateDebugIdType delegate_event_id_type =
110
+ name == nullptr ? DelegateDebugIdType::kInt : DelegateDebugIdType::kStr ;
111
+ prof_entry.delegate_event_id_type = delegate_event_id_type;
112
+ prof_entry.chain_id = chain_id_;
113
+ prof_entry.debug_handle = debug_handle_;
114
+ prof_entry.event_id = delegate_debug_index == static_cast <unsigned int >(-1 )
115
+ ? create_string_entry (name)
116
+ : delegate_debug_index;
117
+ prof_entry.start_time = et_pal_current_ticks ();
118
+ return prof_entry;
119
+ }
120
+
121
+ void ETDumpGen::end_profiling_delegate (
122
+ EventTracerEntry event_tracer_entry,
123
+ const char * metadata) {
88
124
et_timestamp_t end_time = et_pal_current_ticks ();
89
125
check_ready_to_add_events ();
126
+
127
+ int64_t string_id_metadata =
128
+ metadata == nullptr ? -1 : create_string_entry (metadata);
129
+
130
+ // Start building the ProfileEvent entry.
131
+ etdump_ProfileEvent_start (&builder);
132
+ etdump_ProfileEvent_start_time_add (&builder, event_tracer_entry.start_time );
133
+ etdump_ProfileEvent_end_time_add (&builder, end_time);
134
+ etdump_ProfileEvent_chain_id_add (&builder, chain_id_);
135
+ etdump_ProfileEvent_instruction_id_add (&builder, debug_handle_);
136
+ // Delegate debug identifier can either be of a string type or an integer
137
+ // type. If it's a string type then it's a value of type
138
+ // flatbuffers_string_ref_t type, whereas if it's an integer type then we
139
+ // write the integer value directly.
140
+ if (event_tracer_entry.delegate_event_id_type == DelegateDebugIdType::kInt ) {
141
+ etdump_ProfileEvent_delegate_debug_id_int_add (
142
+ &builder, event_tracer_entry.event_id );
143
+ } else {
144
+ etdump_ProfileEvent_delegate_debug_id_str_add (
145
+ &builder, event_tracer_entry.event_id );
146
+ }
147
+ // String metadata is optional and if a nullptr is passed in then we don't
148
+ // add anything.
149
+ if (string_id_metadata != -1 ) {
150
+ etdump_ProfileEvent_delegate_debug_metadata_add (
151
+ &builder, string_id_metadata);
152
+ }
153
+ etdump_ProfileEvent_ref_t id = etdump_ProfileEvent_end (&builder);
90
154
etdump_RunData_events_push_start (&builder);
155
+ etdump_Event_profile_event_add (&builder, id);
156
+ etdump_RunData_events_push_end (&builder);
157
+ }
91
158
92
- etdump_Event_profile_event_create (
93
- &builder,
94
- prof_entry.event_id , // see todo - change to prof_entry.name
95
- prof_entry.chain_id ,
96
- -1 , // see todo - change to prof_entry.instruction_id
97
- prof_entry.debug_handle , // see todo - change to
98
- // prof_entry.delegate_debug_id_int
99
- flatbuffers_string_create_str (
100
- &builder, " " ), // see todo - change to prof_entry.dlegate_debug_id_str
101
- prof_entry.start_time ,
102
- end_time);
159
+ void ETDumpGen::log_profiling_delegate (
160
+ const char * name,
161
+ DebugHandle delegate_debug_index,
162
+ et_timestamp_t start_time,
163
+ et_timestamp_t end_time,
164
+ const char * metadata) {
165
+ ET_CHECK_MSG (
166
+ (name == nullptr ) ^ (delegate_debug_index == -1 ),
167
+ " Only name or delegate_debug_index can be valid. Check DelegateMappingBuilder documentation for more details." );
168
+ check_ready_to_add_events ();
169
+ int64_t string_id = name != nullptr ? create_string_entry (name) : -1 ;
170
+ int64_t string_id_metadata =
171
+ metadata == nullptr ? -1 : create_string_entry (metadata);
172
+ etdump_ProfileEvent_start (&builder);
173
+ etdump_ProfileEvent_start_time_add (&builder, start_time);
174
+ etdump_ProfileEvent_end_time_add (&builder, end_time);
175
+ etdump_ProfileEvent_chain_id_add (&builder, chain_id_);
176
+ etdump_ProfileEvent_instruction_id_add (&builder, debug_handle_);
177
+ if (string_id == -1 ) {
178
+ etdump_ProfileEvent_delegate_debug_id_int_add (
179
+ &builder, delegate_debug_index);
180
+ } else {
181
+ etdump_ProfileEvent_delegate_debug_id_str_add (&builder, string_id);
182
+ }
183
+ if (string_id_metadata != -1 ) {
184
+ etdump_ProfileEvent_delegate_debug_metadata_add (
185
+ &builder, string_id_metadata);
186
+ }
187
+ etdump_ProfileEvent_ref_t id = etdump_ProfileEvent_end (&builder);
188
+ etdump_RunData_events_push_start (&builder);
189
+ etdump_Event_profile_event_add (&builder, id);
190
+ etdump_RunData_events_push_end (&builder);
191
+ }
192
+
193
+ void ETDumpGen::end_profiling (EventTracerEntry prof_entry) {
194
+ et_timestamp_t end_time = et_pal_current_ticks ();
195
+ ET_CHECK_MSG (
196
+ prof_entry.delegate_event_id_type == DelegateDebugIdType::kNone ,
197
+ " Delegate events must use end_profiling_delegate to mark the end of a delegate profiling event." );
198
+ check_ready_to_add_events ();
199
+
200
+ etdump_ProfileEvent_start (&builder);
201
+ etdump_ProfileEvent_start_time_add (&builder, prof_entry.start_time );
202
+ etdump_ProfileEvent_end_time_add (&builder, end_time);
203
+ etdump_ProfileEvent_chain_id_add (&builder, prof_entry.chain_id );
204
+ etdump_ProfileEvent_instruction_id_add (&builder, prof_entry.debug_handle );
205
+ if (prof_entry.event_id != -1 ) {
206
+ etdump_ProfileEvent_name_add (&builder, prof_entry.event_id );
207
+ }
208
+ etdump_ProfileEvent_ref_t id = etdump_ProfileEvent_end (&builder);
209
+ etdump_RunData_events_push_start (&builder);
210
+ etdump_Event_profile_event_add (&builder, id);
103
211
etdump_RunData_events_push_end (&builder);
104
212
}
105
213
@@ -122,8 +230,6 @@ void ETDumpGen::track_allocation(
122
230
size_t allocation_size) {
123
231
check_ready_to_add_events ();
124
232
125
- // etdump_AllocationEvent_ref_t alloc_event_ref =
126
- // etdump_AllocationEvent_create(&builder, allocator_id, allocation_size);
127
233
etdump_RunData_events_push_start (&builder);
128
234
etdump_Event_allocation_event_create (&builder, allocator_id, allocation_size);
129
235
etdump_RunData_events_push_end (&builder);
@@ -151,5 +257,6 @@ etdump_result ETDumpGen::get_etdump_data() {
151
257
size_t ETDumpGen::get_num_blocks () {
152
258
return num_blocks;
153
259
}
260
+
154
261
} // namespace executor
155
262
} // namespace torch
0 commit comments