[K/N] Add signposts to safepoints ^KT-62689

Disabled by default.

Merge-request: KT-MR-12679
Merged-by: Alexander Shabalin <Alexander.Shabalin@jetbrains.com>
This commit is contained in:
Alexander Shabalin
2023-11-06 23:10:08 +00:00
committed by Space Team
parent b309786353
commit 4c36538646
8 changed files with 64 additions and 4 deletions
@@ -32,6 +32,7 @@ RUNTIME_WEAK int32_t Kotlin_appStateTracking = 0;
RUNTIME_WEAK int32_t Kotlin_mimallocUseDefaultOptions = 1;
RUNTIME_WEAK int32_t Kotlin_mimallocUseCompaction = 0;
RUNTIME_WEAK int32_t Kotlin_objcDisposeOnMain = 0;
RUNTIME_WEAK int32_t Kotlin_enableSafepointSignposts = 0;
ALWAYS_INLINE compiler::DestroyRuntimeMode compiler::destroyRuntimeMode() noexcept {
return static_cast<compiler::DestroyRuntimeMode>(Kotlin_destroyRuntimeMode);
@@ -83,3 +84,7 @@ ALWAYS_INLINE bool compiler::mimallocUseCompaction() noexcept {
ALWAYS_INLINE bool compiler::objcDisposeOnMain() noexcept {
return Kotlin_objcDisposeOnMain != 0;
}
ALWAYS_INLINE bool compiler::enableSafepointSignposts() noexcept {
return Kotlin_enableSafepointSignposts != 0;
}
@@ -113,6 +113,7 @@ int getSourceInfo(void* addr, SourceInfo *result, int result_size) noexcept;
bool mimallocUseDefaultOptions() noexcept;
bool mimallocUseCompaction() noexcept;
bool objcDisposeOnMain() noexcept;
bool enableSafepointSignposts() noexcept;
#ifdef KONAN_ANDROID
bool printToAndroidLogcat() noexcept;
@@ -13,6 +13,16 @@
#include "ThreadData.hpp"
#include "ThreadState.hpp"
// TODO: Remove after the bootstrap that brings changes in ClangArgs.kt
#ifndef KONAN_SUPPORTS_SIGNPOSTS
#define KONAN_SUPPORTS_SIGNPOSTS KONAN_MACOSX || KONAN_IOS || KONAN_WATCHOS || KONAN_TVOS
#endif
#if KONAN_SUPPORTS_SIGNPOSTS
#include <os/log.h>
#include <os/signpost.h>
#endif
using namespace kotlin;
namespace {
@@ -21,11 +31,45 @@ namespace {
int64_t activeCount = 0;
std::atomic<void (*)(mm::ThreadData&)> safePointAction = nullptr;
#if KONAN_SUPPORTS_SIGNPOSTS
#define SAFEPOINT_SIGNPOST_NAME "Safepoint" // signpost API requires strings be literals
class SafePointSignpostInterval : private Pinned {
public:
explicit SafePointSignpostInterval(mm::ThreadData& threadData) noexcept : id_(os_signpost_id_make_with_pointer(logObject, &threadData)) {
os_signpost_interval_begin(logObject, id_, SAFEPOINT_SIGNPOST_NAME, "thread id: %d", threadData.threadId());
}
~SafePointSignpostInterval() {
os_signpost_interval_end(logObject, id_, SAFEPOINT_SIGNPOST_NAME);
}
private:
static os_log_t logObject;
uint64_t id_;
};
#undef SAFEPOINT_SIGNPOST_NAME
// static
os_log_t SafePointSignpostInterval::logObject = os_log_create("org.kotlinlang.native.runtime", "safepoint");
#else
class SafePointSignpostInterval : private Pinned {
public:
explicit SafePointSignpostInterval(mm::ThreadData& threadData) noexcept {}
};
#endif
void safePointActionImpl(mm::ThreadData& threadData) noexcept {
static thread_local bool recursion = false;
RuntimeAssert(!recursion, "Recursive safepoint");
AutoReset guard(&recursion, true);
std::optional<SafePointSignpostInterval> signpost;
if (compiler::enableSafepointSignposts()) {
signpost.emplace(threadData);
}
threadData.gcScheduler().safePoint();
threadData.gc().safePoint();
threadData.suspensionData().suspendIfRequested();