Skip to content

Commit 39426f6

Browse files
committed
Android: build/runtime fixes for modern Clang + ETDump path override
Three small fixes needed to build and run ExecuTorch Android on hosts with Apple Clang 21 and on devices where /data/local/tmp isn't app-writable: 1. Workaround flatcc's vendored CMakeLists.txt hitting -Werror on new Clang warnings (-Wimplicit-int-conversion-on-negation, -Wunterminated-string-initialization). Passes FLATCC_ALLOW_WERROR=OFF into both the top-level flatcc CMake build and the flatcc_ep ExternalProject. 2. scripts/build_android_library.sh was setting EXECUTORCH_ENABLE_EVENT_TRACER from $EXECUTORCH_ANDROID_PROFILING but never forwarding the separate -DEXECUTORCH_ANDROID_PROFILING define that gates ETDumpGen allocation in extension/android/jni/jni_layer.cpp. As a result, the event tracer was compiled in but the JNI never attached it, and Module.etdump() always returned false. 3. Add Module.etdump(String outputPath) / etdumpTo() JNI method so apps can write the ETDump into their own filesDir. The existing etdump() hardcodes /data/local/tmp/result.etdump, which is not app-writable on modern Android (Pixel 9 / API 36 returns EACCES). The new overload factors the write path through a shared etdump_to_path() helper; etdump() now delegates to it with the legacy path.
1 parent 369f5ca commit 39426f6

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

extension/android/executorch_android/src/main/java/org/pytorch/executorch/Module.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,28 @@ public boolean etdump() {
265265
@DoNotStrip
266266
private native boolean etdumpNative();
267267

268+
/**
269+
* Dump the ExecuTorch ETDump file to {@code outputPath}.
270+
*
271+
* @param outputPath absolute path to write the etdump file to.
272+
* @return true if the etdump was successfully written, false otherwise.
273+
*/
274+
@Experimental
275+
public boolean etdump(String outputPath) {
276+
mLock.lock();
277+
try {
278+
if (!mHybridData.isValid()) {
279+
throw new IllegalStateException("Module has been destroyed");
280+
}
281+
return etdumpToNative(outputPath);
282+
} finally {
283+
mLock.unlock();
284+
}
285+
}
286+
287+
@DoNotStrip
288+
private native boolean etdumpToNative(String outputPath);
289+
268290
/**
269291
* Explicitly destroys the native Module object. Calling this method is not required, as the
270292
* native object will be destroyed when this object is garbage-collected. However, the timing of

extension/android/jni/jni_layer.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,36 +472,51 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
472472
}
473473

474474
jboolean etdump() {
475+
return etdump_to_path("/data/local/tmp/result.etdump");
476+
}
477+
478+
jboolean etdumpTo(facebook::jni::alias_ref<jstring> outputPath) {
479+
return etdump_to_path(outputPath->toStdString().c_str());
480+
}
481+
482+
private:
483+
jboolean etdump_to_path(const char* path) {
475484
#ifdef EXECUTORCH_ANDROID_PROFILING
476485
executorch::etdump::ETDumpGen* etdumpgen =
477486
(executorch::etdump::ETDumpGen*)module_->event_tracer();
478487
auto etdump_data = etdumpgen->get_etdump_data();
479488

480489
if (etdump_data.buf != nullptr && etdump_data.size > 0) {
481-
int etdump_file =
482-
open("/data/local/tmp/result.etdump", O_WRONLY | O_CREAT, 0644);
490+
int etdump_file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
483491
if (etdump_file == -1) {
484-
ET_LOG(Error, "Cannot create result.etdump error: %d", errno);
492+
ET_LOG(Error, "Cannot create %s error: %d", path, errno);
493+
free(etdump_data.buf);
485494
return false;
486495
}
487496
ssize_t bytes_written =
488497
write(etdump_file, (uint8_t*)etdump_data.buf, etdump_data.size);
489498
if (bytes_written == -1) {
490-
ET_LOG(Error, "Cannot write result.etdump error: %d", errno);
499+
ET_LOG(Error, "Cannot write %s error: %d", path, errno);
500+
close(etdump_file);
501+
free(etdump_data.buf);
491502
return false;
492503
} else {
493-
ET_LOG(Info, "ETDump written %d bytes to file.", bytes_written);
504+
ET_LOG(Info, "ETDump written %zd bytes to %s.", bytes_written, path);
494505
}
495506
close(etdump_file);
496507
free(etdump_data.buf);
497508
return true;
498509
} else {
499510
ET_LOG(Error, "No ETDump data available!");
500511
}
512+
#else
513+
(void)path;
501514
#endif
502515
return false;
503516
}
504517

518+
public:
519+
505520
facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>> getMethods() {
506521
const auto& names_result = module_->method_names();
507522
if (!names_result.ok()) {
@@ -571,6 +586,7 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
571586
makeNativeMethod(
572587
"readLogBufferStaticNative", ExecuTorchJni::readLogBufferStatic),
573588
makeNativeMethod("etdumpNative", ExecuTorchJni::etdump),
589+
makeNativeMethod("etdumpToNative", ExecuTorchJni::etdumpTo),
574590
makeNativeMethod("getMethodsNative", ExecuTorchJni::getMethods),
575591
makeNativeMethod("getUsedBackends", ExecuTorchJni::getUsedBackends),
576592
});

scripts/build_android_library.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ build_android_native_library() {
4040
--preset "android-${ANDROID_ABI}" \
4141
-DANDROID_PLATFORM=android-26 \
4242
-DEXECUTORCH_ENABLE_EVENT_TRACER="${EXECUTORCH_ANDROID_PROFILING:-OFF}" \
43+
-DEXECUTORCH_ANDROID_PROFILING="${EXECUTORCH_ANDROID_PROFILING:-OFF}" \
4344
-DEXECUTORCH_BUILD_EXTENSION_LLM="${EXECUTORCH_BUILD_EXTENSION_LLM:-ON}" \
4445
-DEXECUTORCH_BUILD_EXTENSION_LLM_RUNNER="${EXECUTORCH_BUILD_EXTENSION_LLM:-ON}" \
4546
-DEXECUTORCH_BUILD_EXTENSION_ASR_RUNNER="${EXECUTORCH_BUILD_EXTENSION_LLM:-ON}" \
@@ -51,6 +52,7 @@ build_android_native_library() {
5152
-DQNN_SDK_ROOT="${QNN_SDK_ROOT}" \
5253
-DEXECUTORCH_BUILD_VULKAN="${EXECUTORCH_BUILD_VULKAN}" \
5354
-DXNNPACK_ENABLE_ARM_SME2="${XNNPACK_ENABLE_ARM_SME2}" \
55+
-DFLATCC_ALLOW_WERROR=OFF \
5456
-DSUPPORT_REGEX_LOOKAHEAD=ON \
5557
-DCMAKE_BUILD_TYPE="${EXECUTORCH_CMAKE_BUILD_TYPE}" \
5658
-B"${CMAKE_OUT}"

third-party/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ ExternalProject_Add(
9191
-DFLATCC_TEST=OFF
9292
-DFLATCC_REFLECTION=OFF
9393
-DFLATCC_DEBUG_CLANG_SANITIZE=OFF
94+
-DFLATCC_ALLOW_WERROR=OFF
9495
-DFLATCC_INSTALL=ON
9596
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
9697
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>

0 commit comments

Comments
 (0)