xref: /aosp_15_r20/external/executorch/docs/source/etdump.md (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker# Prerequisite | ETDump - ExecuTorch Dump
2*523fa7a6SAndroid Build Coastguard Worker
3*523fa7a6SAndroid Build Coastguard WorkerETDump (ExecuTorch Dump) is one of the core components of the ExecuTorch Developer Tools. It is the mechanism through which all forms of profiling and debugging data is extracted from the runtime. Users can't parse ETDump directly; instead, they should pass it into the Inspector API, which deserializes the data, offering interfaces for flexible analysis and debugging.
4*523fa7a6SAndroid Build Coastguard Worker
5*523fa7a6SAndroid Build Coastguard Worker
6*523fa7a6SAndroid Build Coastguard Worker## Generating an ETDump
7*523fa7a6SAndroid Build Coastguard Worker
8*523fa7a6SAndroid Build Coastguard WorkerGenerating an ETDump is a relatively straightforward process. Users can follow the steps detailed below to integrate it into their application that uses ExecuTorch.
9*523fa7a6SAndroid Build Coastguard Worker
10*523fa7a6SAndroid Build Coastguard Worker1. ***Include*** the ETDump header in your code.
11*523fa7a6SAndroid Build Coastguard Worker```C++
12*523fa7a6SAndroid Build Coastguard Worker#include <executorch/devtools/etdump/etdump_flatcc.h>
13*523fa7a6SAndroid Build Coastguard Worker```
14*523fa7a6SAndroid Build Coastguard Worker
15*523fa7a6SAndroid Build Coastguard Worker2. ***Create*** an Instance of the ETDumpGen class and pass it into the `load_method` call that is invoked in the runtime.
16*523fa7a6SAndroid Build Coastguard Worker
17*523fa7a6SAndroid Build Coastguard Worker```C++
18*523fa7a6SAndroid Build Coastguard Workerexecutorch::etdump::ETDumpGen etdump_gen;
19*523fa7a6SAndroid Build Coastguard WorkerResult<Method> method =
20*523fa7a6SAndroid Build Coastguard Worker      program->load_method(method_name, &memory_manager, &etdump_gen);
21*523fa7a6SAndroid Build Coastguard Worker```
22*523fa7a6SAndroid Build Coastguard Worker
23*523fa7a6SAndroid Build Coastguard Worker3. ***Dump Out the ETDump Buffer*** - after the inference iterations have been completed, users can dump out the ETDump buffer. If users are on a device which has a filesystem, they could just write it out to the filesystem. For more constrained embedded devices, users will have to extract the ETDump buffer from the device through a mechanism that best suits them (e.g. UART, JTAG etc.)
24*523fa7a6SAndroid Build Coastguard Worker
25*523fa7a6SAndroid Build Coastguard Worker```C++
26*523fa7a6SAndroid Build Coastguard Workeretdump_result result = etdump_gen.get_etdump_data();
27*523fa7a6SAndroid Build Coastguard Workerif (result.buf != nullptr && result.size > 0) {
28*523fa7a6SAndroid Build Coastguard Worker    // On a device with a file system users can just write it out
29*523fa7a6SAndroid Build Coastguard Worker    // to the file-system.
30*523fa7a6SAndroid Build Coastguard Worker    FILE* f = fopen(FLAGS_etdump_path.c_str(), "w+");
31*523fa7a6SAndroid Build Coastguard Worker    fwrite((uint8_t*)result.buf, 1, result.size, f);
32*523fa7a6SAndroid Build Coastguard Worker    fclose(f);
33*523fa7a6SAndroid Build Coastguard Worker    free(result.buf);
34*523fa7a6SAndroid Build Coastguard Worker  }
35*523fa7a6SAndroid Build Coastguard Worker```
36*523fa7a6SAndroid Build Coastguard Worker
37*523fa7a6SAndroid Build Coastguard Worker4. ***Compile*** your binary using CMake with the `ET_EVENT_TRACER_ENABLED` pre-processor flag to enable events to be traced and logged into ETDump inside the ExecuTorch runtime. This flag needs to be added to the ExecuTorch library and any operator library that you are compiling into your binary. For reference, you can take a look at `examples/sdk/CMakeLists.txt`. The lines of interest are:
38*523fa7a6SAndroid Build Coastguard Worker```
39*523fa7a6SAndroid Build Coastguard Workertarget_compile_options(executorch INTERFACE -DET_EVENT_TRACER_ENABLED)
40*523fa7a6SAndroid Build Coastguard Workertarget_compile_options(portable_ops_lib INTERFACE -DET_EVENT_TRACER_ENABLED)
41*523fa7a6SAndroid Build Coastguard Worker```
42*523fa7a6SAndroid Build Coastguard Worker## Using an ETDump
43*523fa7a6SAndroid Build Coastguard Worker
44*523fa7a6SAndroid Build Coastguard WorkerPass this ETDump into the [Inspector API](./model-inspector.rst) to access this data and do post-run analysis.
45