forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.h
More file actions
276 lines (245 loc) · 8.5 KB
/
platform.h
File metadata and controls
276 lines (245 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* @file
* Platform abstraction layer to allow individual platform libraries to override
* symbols in ExecuTorch. PAL functions are defined as C functions so a platform
* library implementer can use C in lieu of C++.
*
* The et_pal_ methods should not be called directly. Use the corresponding
* methods in the executorch::runtime namespace instead to appropriately
* dispatch through the PAL function table.
*/
#pragma once
// Use C-style includes so that C code can include this header.
#include <stddef.h>
#include <stdint.h>
#include <executorch/runtime/platform/compiler.h>
#include <executorch/runtime/platform/types.h>
/**
* Clients should neither define nor use this macro. Used to optionally declare
* the et_pal_*() functions as weak symbols.
*
* This provides a way to both:
* - Include the header and define weak symbols (used by the internal default
* implementations)
* - Include the header and define strong symbols (used by client overrides)
*/
#ifndef ET_INTERNAL_PLATFORM_WEAKNESS
#define ET_INTERNAL_PLATFORM_WEAKNESS
#endif
extern "C" {
/**
* Represents the conversion ratio from system ticks to nanoseconds.
* To convert, use nanoseconds = ticks * numerator / denominator.
*/
typedef struct {
uint64_t numerator;
uint64_t denominator;
} et_tick_ratio_t;
/**
* Initialize the platform abstraction layer.
*
* This function should be called before any other function provided by the PAL
* to initialize any global state. Typically overridden by PAL implementer.
*/
void et_pal_init(void) ET_INTERNAL_PLATFORM_WEAKNESS;
using pal_init_method = void (*)();
/**
* Immediately abort execution, setting the device into an error state, if
* available.
*/
ET_NORETURN void et_pal_abort(void) ET_INTERNAL_PLATFORM_WEAKNESS;
using pal_abort_method = void (*)();
/**
* Return a monotonically non-decreasing timestamp in system ticks.
*
* @retval Timestamp value in system ticks.
*/
et_timestamp_t et_pal_current_ticks(void) ET_INTERNAL_PLATFORM_WEAKNESS;
typedef et_timestamp_t (*et_pal_current_ticks_t)(void);
using pal_current_ticks_method = et_timestamp_t (*)();
/**
* Return the conversion rate from system ticks to nanoseconds as a fraction.
* To convert a system ticks to nanoseconds, multiply the tick count by the
* numerator and then divide by the denominator:
* nanoseconds = ticks * numerator / denominator
*
* The utility method executorch::runtime::ticks_to_ns(et_timestamp_t) can also
* be used to perform the conversion for a given tick count. It is defined in
* torch/executor/runtime/platform/clock.h.
*
* @retval The ratio of nanoseconds to system ticks.
*/
et_tick_ratio_t et_pal_ticks_to_ns_multiplier(void)
ET_INTERNAL_PLATFORM_WEAKNESS;
using pal_ticks_to_ns_multiplier_method = et_tick_ratio_t (*)();
/**
* Severity level of a log message. Values must map to printable 7-bit ASCII
* uppercase letters.
*/
typedef enum {
kDebug = 'D',
kInfo = 'I',
kError = 'E',
kFatal = 'F',
kUnknown = '?', // Exception to the "uppercase letter" rule.
} et_pal_log_level_t;
/**
* Emit a log message via platform output (serial port, console, etc).
*
* @param[in] timestamp Timestamp of the log event in system ticks since boot.
* @param[in] level Severity level of the message. Must be a printable 7-bit
* ASCII uppercase letter.
* @param[in] filename Name of the file that created the log event.
* @param[in] function Name of the function that created the log event.
* @param[in] line Line in the source file where the log event was created.
* @param[in] message Message string to log.
* @param[in] length Message string length.
*/
void et_pal_emit_log_message(
et_timestamp_t timestamp,
et_pal_log_level_t level,
const char* filename,
const char* function,
size_t line,
const char* message,
size_t length) ET_INTERNAL_PLATFORM_WEAKNESS;
using pal_emit_log_message_method = void (*)(
et_timestamp_t timestamp,
et_pal_log_level_t level,
const char* filename,
const char* function,
size_t line,
const char* message,
size_t length);
/**
* NOTE: Core runtime code must not call this directly. It may only be called by
* a MemoryAllocator wrapper.
*
* Allocates size bytes of memory.
*
* @param[in] size Number of bytes to allocate.
* @returns the allocated memory, or nullptr on failure. Must be freed using
* et_pal_free().
*/
void* et_pal_allocate(size_t size) ET_INTERNAL_PLATFORM_WEAKNESS;
using pal_allocate_method = void* (*)(size_t size);
/**
* Frees memory allocated by et_pal_allocate().
*
* @param[in] ptr Pointer to memory to free. May be nullptr.
*/
void et_pal_free(void* ptr) ET_INTERNAL_PLATFORM_WEAKNESS;
using pal_free_method = void (*)(void* ptr);
} // extern "C"
namespace executorch::runtime {
/**
* Table of pointers to platform abstraction layer functions.
*/
struct PalImpl {
// Note that this struct cannot contain constructors in order to ensure that
// the singleton instance can be initialized without relying on a global
// constructor. If it does require a global constructor, there can be a race
// between the init of the default PAL and the user static registration code.
static PalImpl create(
pal_emit_log_message_method emit_log_message,
const char* source_filename);
static PalImpl create(
pal_init_method init,
pal_abort_method abort,
pal_current_ticks_method current_ticks,
pal_ticks_to_ns_multiplier_method ticks_to_ns_multiplier,
pal_emit_log_message_method emit_log_message,
pal_allocate_method allocate,
pal_free_method free,
const char* source_filename);
pal_init_method init = nullptr;
pal_abort_method abort = nullptr;
pal_current_ticks_method current_ticks = nullptr;
pal_ticks_to_ns_multiplier_method ticks_to_ns_multiplier = nullptr;
pal_emit_log_message_method emit_log_message = nullptr;
pal_allocate_method allocate = nullptr;
pal_free_method free = nullptr;
// An optional metadata field, indicating the name of the source
// file that registered the PAL implementation.
const char* source_filename;
};
/**
* Override the PAL functions with user implementations. Any null entries in the
* table are unchanged and will keep the default implementation.
*
* Returns true if the registration was successful, false otherwise.
*/
bool register_pal(PalImpl impl);
/**
* Returns the PAL function table, which contains function pointers to the
* active implementation of each PAL function.
*/
const PalImpl* get_pal_impl();
/**
* Initialize the platform abstraction layer.
*
* This function should be called before any other function provided by the PAL
* to initialize any global state. Typically overridden by PAL implementer.
*/
void pal_init();
/**
* Immediately abort execution, setting the device into an error state, if
* available.
*/
ET_NORETURN void pal_abort();
/**
* Return a monotonically non-decreasing timestamp in system ticks.
*
* @retval Timestamp value in system ticks.
*/
et_timestamp_t pal_current_ticks();
/**
* Return the conversion rate from system ticks to nanoseconds as a fraction.
* To convert a system ticks to nanoseconds, multiply the tick count by the
* numerator and then divide by the denominator:
* nanoseconds = ticks * numerator / denominator
*
* The utility method executorch::runtime::ticks_to_ns(et_timestamp_t) can also
* be used to perform the conversion for a given tick count. It is defined in
* torch/executor/runtime/platform/clock.h.
*
* @retval The ratio of nanoseconds to system ticks.
*/
et_tick_ratio_t pal_ticks_to_ns_multiplier();
/**
* Severity level of a log message. Values must map to printable 7-bit ASCII
* uppercase letters.
*/
void pal_emit_log_message(
et_timestamp_t timestamp,
et_pal_log_level_t level,
const char* filename,
const char* function,
size_t line,
const char* message,
size_t length);
/**
* NOTE: Core runtime code must not call this directly. It may only be called by
* a MemoryAllocator wrapper.
*
* Allocates size bytes of memory.
*
* @param[in] size Number of bytes to allocate.
* @returns the allocated memory, or nullptr on failure. Must be freed using
* et_pal_free().
*/
void* pal_allocate(size_t size);
/**
* Frees memory allocated by et_pal_allocate().
*
* @param[in] ptr Pointer to memory to free. May be nullptr.
*/
void pal_free(void* ptr);
} // namespace executorch::runtime