Skip to content

Commit 8e0b82b

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
create MetaETDumpGen class to abstract copy_tensor_to_debug_buffer function
Summary: This diff creates a new class called `MetaETDumpGen` to abstract the `copy_tensor_to_debug_buffer` function from the `ETDumpGen` class, while keep the `ETDumpGen` class as it is, which makes user able to implement their own way dumping data into buffer, while make the API back-compatible. Differential Revision: D69079952
1 parent 6b58e2e commit 8e0b82b

File tree

2 files changed

+65
-50
lines changed

2 files changed

+65
-50
lines changed

devtools/etdump/etdump_flatcc.cpp

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static uint8_t* alignPointer(void* ptr, size_t alignment) {
107107
} // namespace
108108

109109
// Constructor implementation
110-
ETDumpGen::ETDumpGen(Span<uint8_t> buffer) {
110+
MetaETDumpGen::MetaETDumpGen(Span<uint8_t> buffer) {
111111
constexpr size_t max_alloc_buf_size = 128 * 1024;
112112

113113
// Initialize the flatcc builder_ using the buffer and buffer size.
@@ -121,7 +121,7 @@ ETDumpGen::ETDumpGen(Span<uint8_t> buffer) {
121121
size_t min_buf_size = max_alloc_buf_size + builder_size;
122122
ET_CHECK_MSG(
123123
buffer.size() > min_buf_size,
124-
"Static buffer size provided to ETDumpGen is %zu, which is less than or equal to the minimum size of %zu",
124+
"Static buffer size provided to MetaETDumpGen is %zu, which is less than or equal to the minimum size of %zu",
125125
buffer.size(),
126126
min_buf_size);
127127
size_t buffer_size = buffer.size() - builder_size;
@@ -140,14 +140,14 @@ ETDumpGen::ETDumpGen(Span<uint8_t> buffer) {
140140
reset();
141141
}
142142

143-
ETDumpGen::~ETDumpGen() {
143+
MetaETDumpGen::~MetaETDumpGen() {
144144
flatcc_builder_clear(builder_);
145145
if (!is_static_etdump()) {
146146
free(builder_);
147147
}
148148
}
149149

150-
void ETDumpGen::reset() {
150+
void MetaETDumpGen::reset() {
151151
state_ = State::Init;
152152
num_blocks_ = 0;
153153
flatcc_builder_reset(builder_);
@@ -158,7 +158,7 @@ void ETDumpGen::reset() {
158158
etdump_ETDump_run_data_push_start(builder_);
159159
}
160160

161-
void ETDumpGen::create_event_block(const char* name) {
161+
void MetaETDumpGen::create_event_block(const char* name) {
162162
if (state_ == State::AddingEvents) {
163163
etdump_RunData_events_end(builder_);
164164
} else if (state_ == State::Done) {
@@ -176,28 +176,28 @@ void ETDumpGen::create_event_block(const char* name) {
176176
state_ = State::BlockCreated;
177177
}
178178

179-
int64_t ETDumpGen::create_string_entry(const char* name) {
179+
int64_t MetaETDumpGen::create_string_entry(const char* name) {
180180
return flatbuffers_string_create_str(builder_, name);
181181
}
182182

183-
// ETDumpGen has the following possible states, ETDumpGen_Init,
183+
// MetaETDumpGen has the following possible states, ETDumpGen_Init,
184184
// ETDumpGen_Block_Created, ETDumpGen_Adding_Allocators,
185185
// ETDumpGen_Adding_Events. Right after boot-up the state of ETDump will be
186186
// ETDumpGen_Init. At this point we have an option of adding allocators that
187187
// we want to track. Once we've completed adding the allocators we want to track
188-
// we will close the allocators table and move ETDumpGen to the
188+
// we will close the allocators table and move MetaETDumpGen to the
189189
// ETDumpGen_Adding_Events state. After this point we can start adding events to
190190
// ETDump as we wish.
191-
// The reason we need to maintain this state machine inside of ETDumpGen is
191+
// The reason we need to maintain this state machine inside of MetaETDumpGen is
192192
// because, once a table of one type has been closed and another table of a
193193
// different type is opened after it we cannot open another table of the first
194194
// type again. In this case once we close the allocators table and start pushing
195195
// to the events table we cannot push to the allocators table again.
196-
void ETDumpGen::check_ready_to_add_events() {
196+
void MetaETDumpGen::check_ready_to_add_events() {
197197
if (state_ != State::AddingEvents) {
198198
ET_CHECK_MSG(
199199
(state_ == State::AddingAllocators || state_ == State::BlockCreated),
200-
"ETDumpGen in an invalid state. Cannot add new events now.");
200+
"MetaETDumpGen in an invalid state. Cannot add new events now.");
201201
if (state_ == State::AddingAllocators) {
202202
etdump_RunData_allocators_end(builder_);
203203
}
@@ -206,7 +206,7 @@ void ETDumpGen::check_ready_to_add_events() {
206206
}
207207
}
208208

209-
EventTracerEntry ETDumpGen::start_profiling(
209+
EventTracerEntry MetaETDumpGen::start_profiling(
210210
const char* name,
211211
ChainID chain_id,
212212
DebugHandle debug_handle) {
@@ -227,7 +227,7 @@ EventTracerEntry ETDumpGen::start_profiling(
227227

228228
// TODO: Update all occurrences of the ProfileEvent calls once the
229229
// EventTracerEntry struct is updated.
230-
EventTracerEntry ETDumpGen::start_profiling_delegate(
230+
EventTracerEntry MetaETDumpGen::start_profiling_delegate(
231231
const char* name,
232232
DebugHandle delegate_debug_index) {
233233
ET_CHECK_MSG(
@@ -247,7 +247,7 @@ EventTracerEntry ETDumpGen::start_profiling_delegate(
247247
return prof_entry;
248248
}
249249

250-
void ETDumpGen::end_profiling_delegate(
250+
void MetaETDumpGen::end_profiling_delegate(
251251
EventTracerEntry event_tracer_entry,
252252
const void* metadata,
253253
size_t metadata_len) {
@@ -280,7 +280,7 @@ void ETDumpGen::end_profiling_delegate(
280280
etdump_RunData_events_push_end(builder_);
281281
}
282282

283-
void ETDumpGen::log_profiling_delegate(
283+
void MetaETDumpGen::log_profiling_delegate(
284284
const char* name,
285285
DebugHandle delegate_debug_index,
286286
et_timestamp_t start_time,
@@ -312,43 +312,43 @@ void ETDumpGen::log_profiling_delegate(
312312
etdump_RunData_events_push_end(builder_);
313313
}
314314

315-
void ETDumpGen::log_intermediate_output_delegate(
315+
void MetaETDumpGen::log_intermediate_output_delegate(
316316
const char* name,
317317
DebugHandle delegate_debug_index,
318318
const Tensor& output) {
319319
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
320320
}
321321

322-
void ETDumpGen::log_intermediate_output_delegate(
322+
void MetaETDumpGen::log_intermediate_output_delegate(
323323
const char* name,
324324
DebugHandle delegate_debug_index,
325325
const ArrayRef<Tensor> output) {
326326
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
327327
}
328328

329-
void ETDumpGen::log_intermediate_output_delegate(
329+
void MetaETDumpGen::log_intermediate_output_delegate(
330330
const char* name,
331331
DebugHandle delegate_debug_index,
332332
const int& output) {
333333
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
334334
}
335335

336-
void ETDumpGen::log_intermediate_output_delegate(
336+
void MetaETDumpGen::log_intermediate_output_delegate(
337337
const char* name,
338338
DebugHandle delegate_debug_index,
339339
const bool& output) {
340340
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
341341
}
342342

343-
void ETDumpGen::log_intermediate_output_delegate(
343+
void MetaETDumpGen::log_intermediate_output_delegate(
344344
const char* name,
345345
DebugHandle delegate_debug_index,
346346
const double& output) {
347347
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
348348
}
349349

350350
template <typename T>
351-
void ETDumpGen::log_intermediate_output_delegate_helper(
351+
void MetaETDumpGen::log_intermediate_output_delegate_helper(
352352
const char* name,
353353
DebugHandle delegate_debug_index,
354354
const T& output) {
@@ -430,7 +430,7 @@ void ETDumpGen::log_intermediate_output_delegate_helper(
430430
etdump_RunData_events_push_end(builder_);
431431
}
432432

433-
void ETDumpGen::end_profiling(EventTracerEntry prof_entry) {
433+
void MetaETDumpGen::end_profiling(EventTracerEntry prof_entry) {
434434
et_timestamp_t end_time = et_pal_current_ticks();
435435
ET_CHECK_MSG(
436436
prof_entry.delegate_event_id_type == DelegateDebugIdType::kNone,
@@ -451,7 +451,7 @@ void ETDumpGen::end_profiling(EventTracerEntry prof_entry) {
451451
etdump_RunData_events_push_end(builder_);
452452
}
453453

454-
AllocatorID ETDumpGen::track_allocator(const char* name) {
454+
AllocatorID MetaETDumpGen::track_allocator(const char* name) {
455455
ET_CHECK_MSG(
456456
(state_ == State::BlockCreated || state_ == State::AddingAllocators),
457457
"Allocators can only be added immediately after a new block is created and before any events are added.");
@@ -464,7 +464,7 @@ AllocatorID ETDumpGen::track_allocator(const char* name) {
464464
return etdump_RunData_allocators_reserved_len(builder_);
465465
}
466466

467-
void ETDumpGen::track_allocation(
467+
void MetaETDumpGen::track_allocation(
468468
AllocatorID allocator_id,
469469
size_t allocation_size) {
470470
check_ready_to_add_events();
@@ -474,7 +474,7 @@ void ETDumpGen::track_allocation(
474474
etdump_RunData_events_push_end(builder_);
475475
}
476476

477-
ETDumpResult ETDumpGen::get_etdump_data() {
477+
ETDumpResult MetaETDumpGen::get_etdump_data() {
478478
ETDumpResult result;
479479
if (state_ == State::AddingEvents) {
480480
etdump_RunData_events_end(builder_);
@@ -504,25 +504,11 @@ ETDumpResult ETDumpGen::get_etdump_data() {
504504
return result;
505505
}
506506

507-
void ETDumpGen::set_debug_buffer(Span<uint8_t> buffer) {
507+
void MetaETDumpGen::set_debug_buffer(Span<uint8_t> buffer) {
508508
debug_buffer_ = buffer;
509509
}
510510

511-
size_t ETDumpGen::copy_tensor_to_debug_buffer(executorch::aten::Tensor tensor) {
512-
if (tensor.nbytes() == 0) {
513-
return static_cast<size_t>(-1);
514-
}
515-
uint8_t* offset_ptr =
516-
alignPointer(debug_buffer_.data() + debug_buffer_offset_, 64);
517-
debug_buffer_offset_ = (offset_ptr - debug_buffer_.data()) + tensor.nbytes();
518-
ET_CHECK_MSG(
519-
debug_buffer_offset_ <= debug_buffer_.size(),
520-
"Ran out of space to store intermediate outputs.");
521-
memcpy(offset_ptr, tensor.const_data_ptr(), tensor.nbytes());
522-
return (size_t)(offset_ptr - debug_buffer_.data());
523-
}
524-
525-
void ETDumpGen::log_evalue(const EValue& evalue, LoggedEValueType evalue_type) {
511+
void MetaETDumpGen::log_evalue(const EValue& evalue, LoggedEValueType evalue_type) {
526512
if (debug_buffer_.empty()) {
527513
return;
528514
}
@@ -635,17 +621,30 @@ void ETDumpGen::log_evalue(const EValue& evalue, LoggedEValueType evalue_type) {
635621
etdump_RunData_events_push_end(builder_);
636622
}
637623

638-
size_t ETDumpGen::get_num_blocks() {
624+
size_t MetaETDumpGen::get_num_blocks() {
639625
return num_blocks_;
640626
}
641627

642-
bool ETDumpGen::is_static_etdump() {
628+
bool MetaETDumpGen::is_static_etdump() {
643629
return alloc_.data != nullptr;
644630
}
645631

646-
size_t ETDumpGen::get_debug_buffer_size() const {
632+
size_t MetaETDumpGen::get_debug_buffer_size() const {
647633
return debug_buffer_.size();
648634
}
649635

636+
size_t ETDumpGen::copy_tensor_to_debug_buffer(const executorch::aten::Tensor& tensor) {
637+
if (tensor.nbytes() == 0) {
638+
return static_cast<size_t>(-1);
639+
}
640+
uint8_t* offset_ptr = alignPointer(debug_buffer_.data() + debug_buffer_offset_, 64);
641+
debug_buffer_offset_ = (offset_ptr - debug_buffer_.data()) + tensor.nbytes();
642+
ET_CHECK_MSG(
643+
debug_buffer_offset_ <= debug_buffer_.size(),
644+
"Ran out of space to store intermediate outputs.");
645+
memcpy(offset_ptr, tensor.const_data_ptr(), tensor.nbytes());
646+
return (size_t)(offset_ptr - debug_buffer_.data());
647+
}
648+
650649
} // namespace etdump
651650
} // namespace executorch

devtools/etdump/etdump_flatcc.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ struct ETDumpResult {
6363
size_t size;
6464
};
6565

66-
class ETDumpGen : public ::executorch::runtime::EventTracer {
66+
class MetaETDumpGen : public ::executorch::runtime::EventTracer {
6767
public:
68-
ETDumpGen(::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0});
69-
~ETDumpGen() override;
68+
MetaETDumpGen(::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0});
69+
70+
virtual ~MetaETDumpGen() override;
7071
void clear_builder();
7172

7273
void create_event_block(const char* name) override;
@@ -147,6 +148,14 @@ class ETDumpGen : public ::executorch::runtime::EventTracer {
147148
bool is_static_etdump();
148149
void reset();
149150

151+
protected:
152+
// Declare the function as pure virtual
153+
virtual size_t copy_tensor_to_debug_buffer(const executorch::aten::Tensor& tensor) = 0;
154+
155+
// Make them as protected to be accessible by derived classes
156+
::executorch::runtime::Span<uint8_t> debug_buffer_;
157+
size_t debug_buffer_offset_ = 0;
158+
150159
private:
151160
enum class State {
152161
Init,
@@ -158,7 +167,6 @@ class ETDumpGen : public ::executorch::runtime::EventTracer {
158167

159168
void check_ready_to_add_events();
160169
int64_t create_string_entry(const char* name);
161-
size_t copy_tensor_to_debug_buffer(executorch::aten::Tensor tensor);
162170

163171
/**
164172
* Templated helper function used to log various types of intermediate output.
@@ -172,13 +180,21 @@ class ETDumpGen : public ::executorch::runtime::EventTracer {
172180

173181
struct flatcc_builder* builder_;
174182
size_t num_blocks_ = 0;
175-
::executorch::runtime::Span<uint8_t> debug_buffer_;
176-
size_t debug_buffer_offset_ = 0;
183+
177184
int bundled_input_index_ = -1;
178185
State state_ = State::Init;
179186
struct internal::ETDumpStaticAllocator alloc_;
180187
};
181188

189+
class ETDumpGen : public MetaETDumpGen {
190+
public:
191+
ETDumpGen(::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0})
192+
: MetaETDumpGen(buffer) {}
193+
194+
protected:
195+
size_t copy_tensor_to_debug_buffer(const executorch::aten::Tensor& tensor) override;
196+
};
197+
182198
} // namespace etdump
183199
} // namespace executorch
184200

0 commit comments

Comments
 (0)