|
7 | 7 | #include <worklets/WorkletRuntime/WorkletRuntime.h> |
8 | 8 | #include <worklets/WorkletRuntime/WorkletRuntimeDecorator.h> |
9 | 9 |
|
| 10 | +#ifdef ANDROID |
| 11 | +#include <android/trace.h> |
| 12 | +#endif |
| 13 | + |
| 14 | +#if defined(__APPLE__) |
| 15 | +#include <os/trace_base.h> |
| 16 | +#if OS_LOG_TARGET_HAS_10_15_FEATURES |
| 17 | +#include <os/log.h> |
| 18 | +#include <os/signpost.h> |
| 19 | +#endif |
| 20 | +#endif |
| 21 | + |
10 | 22 | #include <memory> |
11 | 23 | #include <string> |
12 | 24 | #include <utility> |
13 | 25 | #include <vector> |
14 | 26 |
|
| 27 | +#if defined(__APPLE__) && OS_LOG_TARGET_HAS_10_15_FEATURES |
| 28 | +static os_log_t workletsInstrumentsLogHandle = nullptr; |
| 29 | +static thread_local os_signpost_id_t tls_signpostId = OS_SIGNPOST_ID_INVALID; |
| 30 | +static thread_local std::string tls_signpostName; |
| 31 | + |
| 32 | +static os_log_t getWorkletsInstrumentsLogHandle() { |
| 33 | + if (!workletsInstrumentsLogHandle) { |
| 34 | + workletsInstrumentsLogHandle = os_log_create("dev.worklets.instruments", OS_LOG_CATEGORY_POINTS_OF_INTEREST); |
| 35 | + } |
| 36 | + return workletsInstrumentsLogHandle; |
| 37 | +} |
| 38 | +#endif |
| 39 | + |
15 | 40 | namespace worklets { |
16 | 41 |
|
17 | 42 | static inline double performanceNow() { |
@@ -87,6 +112,33 @@ void WorkletRuntimeDecorator::decorate( |
87 | 112 | return jsi::String::createFromUtf8(rt, stringifyJSIValue(rt, value)); |
88 | 113 | }); |
89 | 114 |
|
| 115 | + jsi_utils::installJsiFunction(rt, "_beginSection", [](jsi::Runtime &rt, const jsi::Value &nameValue) { |
| 116 | +#ifdef ANDROID |
| 117 | + ATrace_beginSection(nameValue.asString(rt).utf8(rt).c_str()); |
| 118 | +#elif defined(__APPLE__) && OS_LOG_TARGET_HAS_10_15_FEATURES |
| 119 | + os_log_t logHandle = getWorkletsInstrumentsLogHandle(); |
| 120 | + if (os_signpost_enabled(logHandle)) { |
| 121 | + tls_signpostName = nameValue.asString(rt).utf8(rt); |
| 122 | + tls_signpostId = os_signpost_id_make_with_pointer(logHandle, &tls_signpostId); |
| 123 | + os_signpost_interval_begin(logHandle, tls_signpostId, "Worklets", "%s", tls_signpostName.c_str()); |
| 124 | + } |
| 125 | +#endif |
| 126 | + return jsi::Value::undefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + jsi_utils::installJsiFunction(rt, "_endSection", [](jsi::Runtime &rt) { |
| 130 | +#ifdef ANDROID |
| 131 | + ATrace_endSection(); |
| 132 | +#elif defined(__APPLE__) && OS_LOG_TARGET_HAS_10_15_FEATURES |
| 133 | + os_log_t logHandle = getWorkletsInstrumentsLogHandle(); |
| 134 | + if (os_signpost_enabled(logHandle) && tls_signpostId != OS_SIGNPOST_ID_INVALID) { |
| 135 | + os_signpost_interval_end(logHandle, tls_signpostId, "Worklets", "%s end", tls_signpostName.c_str()); |
| 136 | + tls_signpostId = OS_SIGNPOST_ID_INVALID; |
| 137 | + } |
| 138 | +#endif |
| 139 | + return jsi::Value::undefined(); |
| 140 | + }); |
| 141 | + |
90 | 142 | jsi_utils::installJsiFunction( |
91 | 143 | rt, "_createSerializable", [](jsi::Runtime &rt, const jsi::Value &value, const jsi::Value &nativeStateSource) { |
92 | 144 | auto shouldRetainRemote = jsi::Value::undefined(); |
|
0 commit comments