|
| 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 |
0 commit comments