[vm] Optimize get current thread id in Fuchsia.

OSThread::GetCurrentThreadId() is used in many places throughout
the vm, particularly in facilities that are used heavily, such as
SafepointRwLock. The high frequency of invocations means that the
runtime overhead of getting the current thread id has a significant
impact on the overall performance of the vm.

The current Fuchsia implemention of OSThread::GetCurrentThreadId()
makes a syscall to get the koid of the current thread's handle,
resulting in hundreds of nanoseconds of unnecessary overhead per
invocation.

This patch changes to using the thread's handle as the id, which is
guaranteed to be unique for the lifetime of the thread, unless it
is closed prematurely (nothing in the vm does this). This approach
is similar to what MUSL libc (the libc used by Fuchsia) does for
the same purpose and has significant mileage.

Bug: b/182183059
TEST=existing ci test suite; manually deployed into Fuchsia build.

Change-Id: I274a793a823a717c8dd206b396001c6acb897b9a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193643
Reviewed-by: Chase Latta <chaselatta@google.com>
Commit-Queue: Chase Latta <chaselatta@google.com>
This commit is contained in:
Corey Tabaka 2021-03-31 20:51:32 +00:00 committed by commit-bot@chromium.org
parent bddded13aa
commit db0fa84d59
2 changed files with 3 additions and 11 deletions

View file

@ -133,7 +133,7 @@ int OSThread::Start(const char* name,
return 0;
}
const ThreadId OSThread::kInvalidThreadId = ZX_KOID_INVALID;
const ThreadId OSThread::kInvalidThreadId = ZX_HANDLE_INVALID;
const ThreadJoinId OSThread::kInvalidThreadJoinId =
static_cast<ThreadJoinId>(0);
@ -163,15 +163,7 @@ intptr_t OSThread::GetMaxStackSize() {
}
ThreadId OSThread::GetCurrentThreadId() {
zx_info_handle_basic_t info;
zx_handle_t thread_handle = thrd_get_zx_handle(thrd_current());
zx_status_t status =
zx_object_get_info(thread_handle, ZX_INFO_HANDLE_BASIC, &info,
sizeof(info), nullptr, nullptr);
if (status != ZX_OK) {
FATAL1("Failed to get thread koid: %s\n", zx_status_get_string(status));
}
return info.koid;
return thrd_get_zx_handle(thrd_current());
}
#ifdef SUPPORT_TIMELINE

View file

@ -18,7 +18,7 @@
namespace dart {
typedef pthread_key_t ThreadLocalKey;
typedef zx_koid_t ThreadId;
typedef zx_handle_t ThreadId;
typedef pthread_t ThreadJoinId;
static const ThreadLocalKey kUnsetThreadLocalKey =