Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 6 additions & 40 deletions runtime/platform/default/android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,13 @@
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <mutex>

#include <android/log.h>

/**
* On debug builds, ensure that `et_pal_init` has been called before
* other PAL functions which depend on initialization.
*/
#ifdef NDEBUG

/**
* Assert that the PAL has been initialized.
*/
#define _ASSERT_PAL_INITIALIZED() ((void)0)

#else // NDEBUG

/**
* Assert that the PAL has been initialized.
*/
#define _ASSERT_PAL_INITIALIZED() \
do { \
if (!initialized) { \
__android_log_print( \
ANDROID_LOG_FATAL, \
"ExecuTorch", \
"ExecuTorch PAL must be initialized before call to %s()", \
ET_FUNCTION); \
} \
} while (0)

#endif // NDEBUG

/// Start time of the system (used to zero the system timestamp).
static std::chrono::time_point<std::chrono::steady_clock> systemStartTime;

/// Flag set to true if the PAL has been successfully initialized.
static bool initialized = false;

/**
* Initialize the platform abstraction layer.
*
Expand All @@ -69,12 +38,9 @@ static bool initialized = false;
#pragma weak et_pal_init
#endif // _MSC_VER
void et_pal_init(void) {
if (initialized) {
return;
}

systemStartTime = std::chrono::steady_clock::now();
initialized = true;
static std::once_flag init_flag;
std::call_once(
init_flag, []() { systemStartTime = std::chrono::steady_clock::now(); });
}

/**
Expand All @@ -97,7 +63,7 @@ ET_NORETURN void et_pal_abort(void) {
#pragma weak et_pal_current_ticks
#endif // _MSC_VER
et_timestamp_t et_pal_current_ticks(void) {
_ASSERT_PAL_INITIALIZED();
et_pal_init();
auto systemCurrentTime = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(
systemCurrentTime - systemStartTime)
Expand Down Expand Up @@ -143,7 +109,7 @@ void et_pal_emit_log_message(
ET_UNUSED size_t line,
const char* message,
ET_UNUSED size_t length) {
_ASSERT_PAL_INITIALIZED();
et_pal_init();

int android_log_level = ANDROID_LOG_UNKNOWN;
if (level == 'D') {
Expand Down
47 changes: 6 additions & 41 deletions runtime/platform/default/posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,16 @@
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <mutex>

#include <executorch/runtime/platform/compiler.h>

// The FILE* to write logs to.
#define ET_LOG_OUTPUT_FILE stderr

/**
* On debug builds, ensure that `et_pal_init` has been called before
* other PAL functions which depend on initialization.
*/
#ifdef NDEBUG

/**
* Assert that the PAL has been initialized.
*/
#define _ASSERT_PAL_INITIALIZED() ((void)0)

#else // NDEBUG

/**
* Assert that the PAL has been initialized.
*/
#define _ASSERT_PAL_INITIALIZED() \
do { \
if (!initialized) { \
fprintf( \
ET_LOG_OUTPUT_FILE, \
"ExecuTorch PAL must be initialized before call to %s()", \
ET_FUNCTION); \
fflush(ET_LOG_OUTPUT_FILE); \
et_pal_abort(); \
} \
} while (0)

#endif // NDEBUG

/// Start time of the system (used to zero the system timestamp).
static std::chrono::time_point<std::chrono::steady_clock> systemStartTime;

/// Flag set to true if the PAL has been successfully initialized.
static bool initialized = false;

/**
* Initialize the platform abstraction layer.
*
Expand All @@ -79,12 +47,9 @@ static bool initialized = false;
#pragma weak et_pal_init
#endif // _MSC_VER
void et_pal_init(void) {
if (initialized) {
return;
}

systemStartTime = std::chrono::steady_clock::now();
initialized = true;
static std::once_flag init_flag;
std::call_once(
init_flag, []() { systemStartTime = std::chrono::steady_clock::now(); });
}

/**
Expand All @@ -107,7 +72,7 @@ ET_NORETURN void et_pal_abort(void) {
#pragma weak et_pal_current_ticks
#endif // _MSC_VER
et_timestamp_t et_pal_current_ticks(void) {
_ASSERT_PAL_INITIALIZED();
et_pal_init();
auto systemCurrentTime = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(
systemCurrentTime - systemStartTime)
Expand Down Expand Up @@ -153,7 +118,7 @@ void et_pal_emit_log_message(
size_t line,
const char* message,
ET_UNUSED size_t length) {
_ASSERT_PAL_INITIALIZED();
et_pal_init();

// Not all platforms have ticks == nanoseconds, but this one does.
timestamp /= 1000; // To microseconds
Expand Down
2 changes: 1 addition & 1 deletion runtime/platform/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ et_cxx_test(
executor_pal_static_runtime_override_test.cpp
)

et_cxx_test(platform_death_test SOURCES executor_pal_death_test.cpp)
et_cxx_test(platform_autoinit_test SOURCES executor_pal_autoinit_test.cpp)

# No weak function symbols on Windows/MSVC, thus PAL intercept doesn't work.
# Skip logging tests in Release mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,15 @@
#include <gtest/gtest.h>

#include <executorch/runtime/platform/platform.h>
#include <executorch/test/utils/DeathTest.h>

TEST(ExecutorPalTest, UninitializedPalDeath) {
// Check for assertion failure on debug builds.
TEST(ExecutorPalTest, AutoInitialization) {
// Functions should auto-initialize and work without explicit et_pal_init()
et_timestamp_t time = et_pal_current_ticks();
EXPECT_GE(time, 0);

#ifndef NDEBUG

ET_EXPECT_DEATH_NO_PAL_INIT(
{ et_pal_current_ticks(); }, "PAL must be initialized");

ET_EXPECT_DEATH_NO_PAL_INIT(
{
et_pal_emit_log_message(
0, et_pal_log_level_t::kFatal, "", "", 0, "", 0);
},
"PAL must be initialized");

#endif // !defined(NDEBUG)
// Logging should also work
et_pal_emit_log_message(
0, et_pal_log_level_t::kInfo, "test", "test", 0, "auto-init test", 14);
}

/// Override the default weak main declaration.
Expand Down
4 changes: 2 additions & 2 deletions runtime/platform/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def define_common_targets():
)

runtime.cxx_test(
name = "platform_death_test",
name = "platform_autoinit_test",
srcs = [
"executor_pal_death_test.cpp",
"executor_pal_autoinit_test.cpp",
],
deps = [
"//executorch/runtime/core:core",
Expand Down
Loading