Skip to content

Commit d1a009e

Browse files
Varun Purifacebook-github-bot
authored andcommitted
Add etdump_gen to generate flatbuffer containing profiling/debug events (#338)
Summary: Pull Request resolved: #338 1. generate etdump 2. deserialize & verify the values 3. check for edge cases There is a TODO in here once the `EventTracerEntry` struct has been modified to include the updates to the etdump flatbuffer schema or devise a way to evaluate the necessary information from the existing struct. Reviewed By: tarun292 Differential Revision: D48743211 fbshipit-source-id: 35327250efe9150d0ddcebd56b6b5b5fd3d53e8d
1 parent 3482830 commit d1a009e

File tree

5 files changed

+504
-0
lines changed

5 files changed

+504
-0
lines changed

sdk/etdump/etdump_flatcc.cpp

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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/sdk/etdump/etdump_flatcc.h"
10+
#include <string.h>
11+
#include "executorch/runtime/platform/assert.h"
12+
13+
namespace torch {
14+
namespace executor {
15+
16+
// Constructor implementation
17+
ETDumpGen::ETDumpGen(void* buffer, size_t buf_size) {
18+
// Initialize the flatcc builder using the buffer and buffer size
19+
flatcc_builder_init(&builder);
20+
flatbuffers_buffer_start(&builder, etdump_ETDump_file_identifier);
21+
etdump_ETDump_start(&builder);
22+
etdump_ETDump_version_add(&builder, ETDUMP_VERSION);
23+
etdump_ETDump_run_data_start(&builder);
24+
etdump_ETDump_run_data_push_start(&builder);
25+
}
26+
27+
ETDumpGen::~ETDumpGen() {
28+
flatcc_builder_clear(&builder);
29+
}
30+
31+
void ETDumpGen::clear_builder() {
32+
flatcc_builder_clear(&builder);
33+
}
34+
35+
void ETDumpGen::create_event_block(const char* name) {
36+
if (etdump_gen_state == ETDumpGen_Adding_Events) {
37+
etdump_RunData_events_end(&builder);
38+
}
39+
if (num_blocks > 0) {
40+
etdump_ETDump_run_data_push_end(&builder);
41+
etdump_ETDump_run_data_push_start(&builder);
42+
}
43+
++num_blocks;
44+
etdump_RunData_name_create_strn(&builder, name, strlen(name));
45+
etdump_gen_state = ETDumpGen_Block_Created;
46+
}
47+
48+
int64_t ETDumpGen::create_string_entry(const char* name) {
49+
return flatbuffers_string_create_str(&builder, name);
50+
}
51+
52+
void ETDumpGen::check_ready_to_add_events() {
53+
if (etdump_gen_state != ETDumpGen_Adding_Events) {
54+
ET_CHECK_MSG(
55+
(etdump_gen_state == ETDumpGen_Adding_Allocators ||
56+
etdump_gen_state == ETDumpGen_Block_Created),
57+
"ETDumpGen in an invalid state. Cannot add new events now.");
58+
if (etdump_gen_state == ETDumpGen_Adding_Allocators) {
59+
etdump_RunData_allocators_end(&builder);
60+
}
61+
etdump_RunData_events_start(&builder);
62+
etdump_gen_state = ETDumpGen_Adding_Events;
63+
}
64+
}
65+
66+
EventTracerEntry ETDumpGen::start_profiling(
67+
const char* name,
68+
ChainID chain_id,
69+
DebugHandle debug_handle) {
70+
EventTracerEntry prof_entry;
71+
prof_entry.event_id = create_string_entry(name);
72+
73+
if (chain_id == -1 && debug_handle == 0) {
74+
prof_entry.chain_id = chain_id_;
75+
prof_entry.debug_handle = debug_handle_;
76+
77+
} else {
78+
prof_entry.chain_id = chain_id;
79+
prof_entry.debug_handle = debug_handle;
80+
}
81+
prof_entry.start_time = et_pal_current_ticks();
82+
return prof_entry;
83+
}
84+
85+
// TODO: Update all occurrences of the ProfileEvent calls once the
86+
// EventTracerEntry struct is updated.
87+
void ETDumpGen::end_profiling(EventTracerEntry prof_entry) {
88+
et_timestamp_t end_time = et_pal_current_ticks();
89+
check_ready_to_add_events();
90+
etdump_RunData_events_push_start(&builder);
91+
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);
103+
etdump_RunData_events_push_end(&builder);
104+
}
105+
106+
AllocatorID ETDumpGen::track_allocator(const char* name) {
107+
ET_CHECK_MSG(
108+
(etdump_gen_state == ETDumpGen_Block_Created ||
109+
etdump_gen_state == ETDumpGen_Adding_Allocators),
110+
"Allocators can only be added immediately after a new block is created and before any events are added.");
111+
if (etdump_gen_state != ETDumpGen_Adding_Allocators) {
112+
etdump_RunData_allocators_start(&builder);
113+
etdump_gen_state = ETDumpGen_Adding_Allocators;
114+
}
115+
flatbuffers_string_ref_t ref = create_string_entry(name);
116+
etdump_RunData_allocators_push_create(&builder, ref);
117+
return etdump_RunData_allocators_reserved_len(&builder);
118+
}
119+
120+
void ETDumpGen::track_allocation(
121+
AllocatorID allocator_id,
122+
size_t allocation_size) {
123+
check_ready_to_add_events();
124+
125+
// etdump_AllocationEvent_ref_t alloc_event_ref =
126+
// etdump_AllocationEvent_create(&builder, allocator_id, allocation_size);
127+
etdump_RunData_events_push_start(&builder);
128+
etdump_Event_allocation_event_create(&builder, allocator_id, allocation_size);
129+
etdump_RunData_events_push_end(&builder);
130+
}
131+
132+
etdump_result ETDumpGen::get_etdump_data() {
133+
etdump_result result;
134+
if (etdump_gen_state == ETDumpGen_Adding_Events) {
135+
etdump_RunData_events_end(&builder);
136+
} else if (etdump_gen_state == ETDumpGen_Adding_Allocators) {
137+
etdump_RunData_allocators_end(&builder);
138+
}
139+
etdump_ETDump_run_data_push_end(&builder);
140+
etdump_ETDump_run_data_end(&builder);
141+
etdump_ETDump_ref_t root = etdump_ETDump_end(&builder);
142+
flatbuffers_buffer_end(&builder, root);
143+
if (num_blocks == 0) {
144+
result = {nullptr, 0};
145+
} else {
146+
result.buf = flatcc_builder_finalize_aligned_buffer(&builder, &result.size);
147+
}
148+
return result;
149+
}
150+
151+
size_t ETDumpGen::get_num_blocks() {
152+
return num_blocks;
153+
}
154+
} // namespace executor
155+
} // namespace torch

sdk/etdump/etdump_flatcc.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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/sdk/etdump/etdump_schema_flatcc_builder.h>
12+
#include <executorch/sdk/etdump/etdump_schema_flatcc_reader.h>
13+
#include "executorch/runtime/core/event_tracer.h"
14+
#include "executorch/runtime/platform/platform.h"
15+
16+
#define ETDUMP_VERSION 0
17+
18+
namespace torch {
19+
namespace executor {
20+
21+
enum ETDumpGen_State {
22+
ETDumpGen_Init,
23+
ETDumpGen_Block_Created,
24+
ETDumpGen_Adding_Allocators,
25+
ETDumpGen_Adding_Events,
26+
};
27+
28+
struct etdump_result {
29+
void* buf;
30+
size_t size;
31+
};
32+
33+
class ETDumpGen : public EventTracer {
34+
public:
35+
ETDumpGen(void* buffer, size_t buf_size);
36+
37+
~ETDumpGen() override;
38+
void clear_builder();
39+
40+
void create_event_block(const char* name) override;
41+
virtual EventTracerEntry start_profiling(
42+
const char* name,
43+
ChainID chain_id = -1,
44+
DebugHandle debug_handle = 0) override;
45+
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;
48+
etdump_result get_etdump_data();
49+
size_t get_num_blocks();
50+
51+
private:
52+
flatcc_builder_t builder;
53+
size_t num_blocks = 0;
54+
ETDumpGen_State etdump_gen_state = ETDumpGen_Init;
55+
56+
void check_ready_to_add_events();
57+
int64_t create_string_entry(const char* name);
58+
};
59+
60+
} // namespace executor
61+
} // namespace torch

sdk/etdump/targets.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,21 @@ def define_common_targets():
174174
"fbsource//arvr/third-party/flatcc:flatcc",
175175
],
176176
)
177+
178+
runtime.cxx_library(
179+
name = "etdump_flatcc",
180+
srcs = [
181+
"etdump_flatcc.cpp",
182+
],
183+
headers = [
184+
"etdump_flatcc.h",
185+
],
186+
deps = [
187+
"//executorch/runtime/platform:platform",
188+
],
189+
exported_deps = [
190+
":etdump_schema_flatcc",
191+
"//executorch/runtime/core:core",
192+
],
193+
visibility = ["//executorch/..."],
194+
)

0 commit comments

Comments
 (0)