Skip to content

Commit 51b3686

Browse files
committed
Add etdump to android
JNI not exposed yet. Need to think about API
1 parent e7d39c2 commit 51b3686

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

extension/android/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ list(
8080
fbjni
8181
)
8282

83+
if(EXECUTORCH_ANDROID_PROFILING)
84+
list(
85+
APPEND
86+
link_libraries
87+
etdump
88+
flatccrt
89+
)
90+
target_compile_definitions(executorch_jni PUBLIC EXECUTORCH_ANDROID_PROFILING=1)
91+
endif()
92+
8393
if(TARGET optimized_native_cpu_ops_lib)
8494
list(
8595
APPEND

extension/android/jni/jni_layer.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <string>
1515
#include <unordered_map>
1616
#include <vector>
17-
1817
#include "jni_layer_constants.h"
1918

2019
#include <executorch/extension/android/jni/log.h>
@@ -31,6 +30,12 @@
3130
#include <executorch/extension/threadpool/threadpool.h>
3231
#endif
3332

33+
#ifdef EXECUTORCH_ANDROID_PROFILING
34+
#include <fcntl.h>
35+
#include <unistd.h>
36+
#include <executorch/devtools/etdump/etdump_flatcc.h>
37+
#endif
38+
3439
#include <fbjni/ByteBuffer.h>
3540
#include <fbjni/fbjni.h>
3641

@@ -237,8 +242,12 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
237242
} else if (loadMode == 3) {
238243
load_mode = Module::LoadMode::MmapUseMlockIgnoreErrors;
239244
}
240-
241-
module_ = std::make_unique<Module>(modelPath->toStdString(), load_mode);
245+
#ifdef EXECUTORCH_ANDROID_PROFILING
246+
auto etdump_gen = std::make_unique<executorch::etdump::ETDumpGen>();
247+
#else
248+
auto etdump_gen = nullptr;
249+
#endif
250+
module_ = std::make_unique<Module>(modelPath->toStdString(), load_mode, std::move(etdump_gen));
242251

243252
#ifdef ET_USE_THREADPOOL
244253
// Default to using cores/2 threadpool threads. The long-term plan is to
@@ -361,6 +370,27 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
361370
auto jevalue = JEValue::newJEValueFromEValue(result.get()[i]);
362371
jresult->setElement(i, *jevalue);
363372
}
373+
#ifdef EXECUTORCH_ANDROID_PROFILING
374+
executorch::etdump::ETDumpGen* etdumpgen = (executorch::etdump::ETDumpGen*) module_->event_tracer();
375+
auto etdump_data = etdumpgen->get_etdump_data();
376+
377+
if (etdump_data.buf != nullptr && etdump_data.size > 0) {
378+
int etdump_file = open("/data/local/tmp/result.etdump", O_WRONLY | O_CREAT, 0644);
379+
if (etdump_file == -1) {
380+
ET_LOG(Error, "Cannot create result.etdump error: %d", errno);
381+
}
382+
ssize_t bytes_written = write(etdump_file, (uint8_t*)etdump_data.buf, etdump_data.size);
383+
if (bytes_written == -1) {
384+
ET_LOG(Error, "Cannot write result.etdump error: %d", errno);
385+
} else {
386+
ET_LOG(Info, "ETDump written %d bytes to file.", bytes_written);
387+
}
388+
close(etdump_file);
389+
free(etdump_data.buf);
390+
} else {
391+
ET_LOG(Error, "No ETDump data available!");
392+
}
393+
#endif
364394

365395
return jresult;
366396
}

scripts/build_android_library.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ build_android_native_library() {
4040
-DANDROID_PLATFORM=android-26 \
4141
-DBUILD_TESTING=OFF \
4242
-DEXECUTORCH_ENABLE_LOGGING=ON \
43+
-DEXECUTORCH_BUILD_DEVTOOLS=ON \
44+
-DEXECUTORCH_ENABLE_EVENT_TRACER=ON \
4345
-DEXECUTORCH_LOG_LEVEL=Info \
4446
-DEXECUTORCH_BUILD_XNNPACK=ON \
4547
-DEXECUTORCH_XNNPACK_SHARED_WORKSPACE=ON \
@@ -75,6 +77,7 @@ build_android_native_library() {
7577
-DEXECUTORCH_ENABLE_LOGGING=ON \
7678
-DEXECUTORCH_LOG_LEVEL=Info \
7779
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \
80+
-DEXECUTORCH_ANDROID_PROFILING="${EXECUTORCH_ANDROID_PROFILING:-OFF}" \
7881
-DNEURON_BUFFER_ALLOCATOR_LIB="$NEURON_BUFFER_ALLOCATOR_LIB" \
7982
-DEXECUTORCH_BUILD_KERNELS_CUSTOM="${EXECUTORCH_BUILD_EXTENSION_LLM:-ON}" \
8083
-DEXECUTORCH_BUILD_LLAMA_JNI="${EXECUTORCH_BUILD_EXTENSION_LLM:-ON}" \

0 commit comments

Comments
 (0)