Skip to content

Commit 7d194cf

Browse files
authored
Add a android log implementation
Differential Revision: D74865527 Pull Request resolved: #10938
1 parent 40736e2 commit 7d194cf

File tree

5 files changed

+201
-6
lines changed

5 files changed

+201
-6
lines changed

runtime/platform/default/android.cpp

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
/**
10+
* @file
11+
* Default PAL implementations for Android system.
12+
*/
13+
14+
// This cpp file will provide weak implementations of the symbols declared in
15+
// Platform.h. Client users can strongly define any or all of the functions to
16+
// override them.
17+
#define ET_INTERNAL_PLATFORM_WEAKNESS ET_WEAK
18+
#include <executorch/runtime/platform/compiler.h>
19+
#include <executorch/runtime/platform/platform.h>
20+
21+
#include <chrono>
22+
#include <cstdio>
23+
#include <cstdlib>
24+
25+
#include <android/log.h>
26+
27+
/**
28+
* On debug builds, ensure that `et_pal_init` has been called before
29+
* other PAL functions which depend on initialization.
30+
*/
31+
#ifdef NDEBUG
32+
33+
/**
34+
* Assert that the PAL has been initialized.
35+
*/
36+
#define _ASSERT_PAL_INITIALIZED() ((void)0)
37+
38+
#else // NDEBUG
39+
40+
/**
41+
* Assert that the PAL has been initialized.
42+
*/
43+
#define _ASSERT_PAL_INITIALIZED() \
44+
do { \
45+
if (!initialized) { \
46+
__android_log_print( \
47+
ANDROID_LOG_FATAL, \
48+
"ExecuTorch", \
49+
"%s", \
50+
"ExecuTorch PAL must be initialized before call to %s()", \
51+
ET_FUNCTION); \
52+
} \
53+
} while (0)
54+
55+
#endif // NDEBUG
56+
57+
/// Start time of the system (used to zero the system timestamp).
58+
static std::chrono::time_point<std::chrono::steady_clock> systemStartTime;
59+
60+
/// Flag set to true if the PAL has been successfully initialized.
61+
static bool initialized = false;
62+
63+
/**
64+
* Initialize the platform abstraction layer.
65+
*
66+
* This function should be called before any other function provided by the PAL
67+
* to initialize any global state. Typically overridden by PAL implementer.
68+
*/
69+
#ifdef _MSC_VER
70+
#pragma weak et_pal_init
71+
#endif // _MSC_VER
72+
void et_pal_init(void) {
73+
if (initialized) {
74+
return;
75+
}
76+
77+
systemStartTime = std::chrono::steady_clock::now();
78+
initialized = true;
79+
}
80+
81+
/**
82+
* Immediately abort execution, setting the device into an error state, if
83+
* available.
84+
*/
85+
#ifdef _MSC_VER
86+
#pragma weak et_pal_abort
87+
#endif // _MSC_VER
88+
ET_NORETURN void et_pal_abort(void) {
89+
std::abort();
90+
}
91+
92+
/**
93+
* Return a monotonically non-decreasing timestamp in system ticks.
94+
*
95+
* @retval Timestamp value in system ticks.
96+
*/
97+
#ifdef _MSC_VER
98+
#pragma weak et_pal_current_ticks
99+
#endif // _MSC_VER
100+
et_timestamp_t et_pal_current_ticks(void) {
101+
_ASSERT_PAL_INITIALIZED();
102+
auto systemCurrentTime = std::chrono::steady_clock::now();
103+
return std::chrono::duration_cast<std::chrono::nanoseconds>(
104+
systemCurrentTime - systemStartTime)
105+
.count();
106+
}
107+
108+
/**
109+
* Return the conversion rate from system ticks to nanoseconds, as a fraction.
110+
* To convert an interval from system ticks to nanoseconds, multiply the tick
111+
* count by the numerator and then divide by the denominator:
112+
* nanoseconds = ticks * numerator / denominator
113+
*
114+
* @retval The ratio of nanoseconds to system ticks.
115+
*/
116+
#ifdef _MSC_VER
117+
#pragma weak et_pal_ticks_to_ns_multiplier
118+
#endif // _MSC_VER
119+
et_tick_ratio_t et_pal_ticks_to_ns_multiplier(void) {
120+
// The system tick interval is 1 nanosecond, so the conversion factor is 1.
121+
return {1, 1};
122+
}
123+
124+
/**
125+
* Emit a log message to adb logcat.
126+
*
127+
* @param[in] timestamp Timestamp of the log event in system ticks since boot.
128+
* @param[in] level Severity level of the message. Must be a printable 7-bit
129+
* ASCII uppercase letter.
130+
* @param[in] filename Name of the file that created the log event.
131+
* @param[in] function Name of the function that created the log event.
132+
* @param[in] line Line in the source file where the log event was created.
133+
* @param[in] message Message string to log.
134+
* @param[in] length Message string length.
135+
*/
136+
#ifdef _MSC_VER
137+
#pragma weak et_pal_emit_log_message
138+
#endif // _MSC_VER
139+
void et_pal_emit_log_message(
140+
ET_UNUSED et_timestamp_t timestamp,
141+
et_pal_log_level_t level,
142+
ET_UNUSED const char* filename,
143+
ET_UNUSED const char* function,
144+
ET_UNUSED size_t line,
145+
const char* message,
146+
ET_UNUSED size_t length) {
147+
_ASSERT_PAL_INITIALIZED();
148+
149+
int android_log_level = ANDROID_LOG_UNKNOWN;
150+
if (level == 'D') {
151+
android_log_level = ANDROID_LOG_DEBUG;
152+
} else if (level == 'I') {
153+
android_log_level = ANDROID_LOG_INFO;
154+
} else if (level == 'E') {
155+
android_log_level = ANDROID_LOG_ERROR;
156+
} else if (level == 'F') {
157+
android_log_level = ANDROID_LOG_FATAL;
158+
}
159+
160+
__android_log_print(android_log_level, "ExecuTorch", "%s", message);
161+
}
162+
163+
/**
164+
* NOTE: Core runtime code must not call this directly. It may only be called by
165+
* a MemoryAllocator wrapper.
166+
*
167+
* Allocates size bytes of memory via malloc.
168+
*
169+
* @param[in] size Number of bytes to allocate.
170+
* @returns the allocated memory, or nullptr on failure. Must be freed using
171+
* et_pal_free().
172+
*/
173+
#ifdef _MSC_VER
174+
#pragma weak et_pal_allocate
175+
#endif // _MSC_VER
176+
void* et_pal_allocate(size_t size) {
177+
return malloc(size);
178+
}
179+
180+
/**
181+
* Frees memory allocated by et_pal_allocate().
182+
*
183+
* @param[in] ptr Pointer to memory to free. May be nullptr.
184+
*/
185+
#ifdef _MSC_VER
186+
#pragma weak et_pal_free
187+
#endif // _MSC_VER
188+
void et_pal_free(void* ptr) {
189+
free(ptr);
190+
}

runtime/platform/targets.bzl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ def define_common_targets():
4141
# client defined implementations will overide them.
4242
runtime.cxx_library(
4343
name = "platform_private",
44-
srcs = _select_pal({
45-
"minimal": ["default/minimal.cpp"],
46-
"posix": ["default/posix.cpp"],
47-
}),
44+
srcs = select({
45+
"ovr_config//os:android": ["default/android.cpp"],
46+
"DEFAULT": _select_pal({
47+
"minimal": ["default/minimal.cpp"],
48+
"posix": ["default/posix.cpp"],
49+
})}),
4850
deps = [
4951
":pal_interface",
5052
],
53+
external_deps = ["log"],
5154
visibility = [
5255
"//executorch/core/...",
5356
],

scripts/build_android_library.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ build_android_native_library() {
3939
-DANDROID_ABI="${ANDROID_ABI}" \
4040
-DANDROID_PLATFORM=android-26 \
4141
-DBUILD_TESTING=OFF \
42+
-DEXECUTORCH_PAL_DEFAULT=android \
4243
-DEXECUTORCH_ENABLE_LOGGING=ON \
4344
-DEXECUTORCH_BUILD_DEVTOOLS=ON \
4445
-DEXECUTORCH_ENABLE_EVENT_TRACER="${EXECUTORCH_ANDROID_PROFILING:-OFF}" \

shim_et/xplat/executorch/build/env_interface.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ _EXTERNAL_DEPS = {
4343
"gtest_aten": "//third-party:gtest_aten",
4444
"libtorch": "//third-party:libtorch",
4545
"libtorch_python": "//third-party:libtorch_python",
46+
"log": [], # Intentionally not supporting OSS buck build log
4647
# Huggingface Tokenizer
4748
"nlohmann_json": [], # Intentionally not supporting OSS buck build HF tokenizer.
4849
"prettytable": "//third-party:prettytable",

tools/cmake/preset/default.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ define_overridable_option(
3131
)
3232
define_overridable_option(
3333
EXECUTORCH_PAL_DEFAULT
34-
"Which PAL default implementation to use. Choices: posix, minimal"
34+
"Which PAL default implementation to use. Choices: posix, minimal, android"
3535
STRING "posix"
3636
)
3737
define_overridable_option(
@@ -276,7 +276,7 @@ define_overridable_option(
276276
# At this point all the options should be configured with their final value.
277277

278278
if(NOT EXISTS ${EXECUTORCH_PAL_DEFAULT_FILE_PATH})
279-
message(FATAL_ERROR "PAL default implementation (EXECUTORCH_PAL_DEFAULT=${EXECUTORCH_PAL_DEFAULT}) file not found: ${EXECUTORCH_PAL_DEFAULT_FILE_PATH}. Choices: posix, minimal")
279+
message(FATAL_ERROR "PAL default implementation (EXECUTORCH_PAL_DEFAULT=${EXECUTORCH_PAL_DEFAULT}) file not found: ${EXECUTORCH_PAL_DEFAULT_FILE_PATH}. Choices: posix, minimal, android")
280280
endif()
281281

282282

0 commit comments

Comments
 (0)