Skip to content

Commit a53b77a

Browse files
tarun292facebook-github-bot
authored andcommitted
ETDump changes for delegate logging.
Differential Revision: D48993977 fbshipit-source-id: ff7323bc51d347b46c022b47a710603243a12da1
1 parent d515135 commit a53b77a

File tree

5 files changed

+258
-37
lines changed

5 files changed

+258
-37
lines changed

sdk/etdump/etdump_flatcc.cpp

Lines changed: 125 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include "executorch/sdk/etdump/etdump_flatcc.h"
10+
#include <stdio.h>
1011
#include <string.h>
1112
#include "executorch/runtime/platform/assert.h"
1213

@@ -18,7 +19,7 @@ ETDumpGen::ETDumpGen(void* buffer, size_t buf_size) {
1819
// Initialize the flatcc builder using the buffer and buffer size
1920
flatcc_builder_init(&builder);
2021
flatbuffers_buffer_start(&builder, etdump_ETDump_file_identifier);
21-
etdump_ETDump_start(&builder);
22+
etdump_ETDump_start_as_root_with_size(&builder);
2223
etdump_ETDump_version_add(&builder, ETDUMP_VERSION);
2324
etdump_ETDump_run_data_start(&builder);
2425
etdump_ETDump_run_data_push_start(&builder);
@@ -49,6 +50,19 @@ int64_t ETDumpGen::create_string_entry(const char* name) {
4950
return flatbuffers_string_create_str(&builder, name);
5051
}
5152

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.
5266
void ETDumpGen::check_ready_to_add_events() {
5367
if (etdump_gen_state != ETDumpGen_Adding_Events) {
5468
ET_CHECK_MSG(
@@ -68,12 +82,12 @@ EventTracerEntry ETDumpGen::start_profiling(
6882
ChainID chain_id,
6983
DebugHandle debug_handle) {
7084
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;
7287

73-
if (chain_id == -1 && debug_handle == 0) {
88+
if (chain_id == -1) {
7489
prof_entry.chain_id = chain_id_;
7590
prof_entry.debug_handle = debug_handle_;
76-
7791
} else {
7892
prof_entry.chain_id = chain_id;
7993
prof_entry.debug_handle = debug_handle;
@@ -84,22 +98,116 @@ EventTracerEntry ETDumpGen::start_profiling(
8498

8599
// TODO: Update all occurrences of the ProfileEvent calls once the
86100
// 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) {
88124
et_timestamp_t end_time = et_pal_current_ticks();
89125
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);
90154
etdump_RunData_events_push_start(&builder);
155+
etdump_Event_profile_event_add(&builder, id);
156+
etdump_RunData_events_push_end(&builder);
157+
}
91158

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);
103211
etdump_RunData_events_push_end(&builder);
104212
}
105213

@@ -122,8 +230,6 @@ void ETDumpGen::track_allocation(
122230
size_t allocation_size) {
123231
check_ready_to_add_events();
124232

125-
// etdump_AllocationEvent_ref_t alloc_event_ref =
126-
// etdump_AllocationEvent_create(&builder, allocator_id, allocation_size);
127233
etdump_RunData_events_push_start(&builder);
128234
etdump_Event_allocation_event_create(&builder, allocator_id, allocation_size);
129235
etdump_RunData_events_push_end(&builder);
@@ -151,5 +257,6 @@ etdump_result ETDumpGen::get_etdump_data() {
151257
size_t ETDumpGen::get_num_blocks() {
152258
return num_blocks;
153259
}
260+
154261
} // namespace executor
155262
} // namespace torch

sdk/etdump/etdump_flatcc.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,20 @@ class ETDumpGen : public EventTracer {
4343
ChainID chain_id = -1,
4444
DebugHandle debug_handle = 0) override;
4545
virtual void end_profiling(EventTracerEntry prof_entry) override;
46-
virtual void track_allocation(AllocatorID id, size_t size) override;
47-
virtual AllocatorID track_allocator(const char* name) override;
4846
virtual EventTracerEntry start_profiling_delegate(
4947
const char* name,
50-
DebugHandle delegate_debug_index) override {
51-
return EventTracerEntry();
52-
};
48+
DebugHandle delegate_debug_index) override;
5349
virtual void end_profiling_delegate(
54-
EventTracerEntry event_tracer_entry,
55-
const char* metadata = nullptr) override{};
50+
EventTracerEntry prof_entry,
51+
const char* metadata) override;
5652
virtual void log_profiling_delegate(
5753
const char* name,
5854
DebugHandle delegate_debug_index,
5955
et_timestamp_t start_time,
6056
et_timestamp_t end_time,
61-
const char* metadata = nullptr) override{};
57+
const char* metadata) override;
58+
virtual void track_allocation(AllocatorID id, size_t size) override;
59+
virtual AllocatorID track_allocator(const char* name) override;
6260
etdump_result get_etdump_data();
6361
size_t get_num_blocks();
6462

sdk/etdump/etdump_schema_flatcc.fbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ table ProfileEvent {
9797
// String based delegate debug identifier.
9898
delegate_debug_id_str:string;
9999

100+
// Delegate debug metadata free form string.
101+
delegate_debug_metadata:string;
102+
100103
// Time at which this event started. Could be in units of time or CPU cycles.
101104
start_time:ulong;
102105

sdk/etdump/schema_flatcc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ class PROFILE_EVENT_ENUM(Enum):
9090

9191
@dataclass
9292
class ProfileEvent:
93-
name: str
93+
name: Optional[str]
9494
chain_id: int
9595
instruction_id: int
96-
delegate_debug_id_int: int
97-
delegate_debug_id_str: str
96+
delegate_debug_id_int: Optional[int]
97+
delegate_debug_id_str: Optional[str]
98+
delegate_debug_metadata: Optional[str]
9899
start_time: int
99100
end_time: int
100101

0 commit comments

Comments
 (0)