Skip to content

Commit 85eff04

Browse files
committed
Add a android log implementation
1 parent d069d65 commit 85eff04

File tree

2 files changed

+199
-4
lines changed

2 files changed

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

runtime/platform/targets.bzl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ 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
],

0 commit comments

Comments
 (0)