|
| 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 | +} |
0 commit comments