Commit 5efc4727 authored by Dave Tapuska's avatar Dave Tapuska Committed by Commit Bot

Use TLS to stash the thread id on linux.

Appears calling the syscall over and over can be slow.

Use TLS to stash the result of the syscall.

Benchmarks:

Without change:
taskset -c 0 base_perftests --gtest_filter=SequenceManagerPerfTest.RunTenThousandImmediateTasks_OneQueue*
*RESULT task: run 10000 immediate tasks with one queue SequenceManager with message loop= 23483.009389671362 us/run
*RESULT task: run 10000 immediate tasks with one queue SequenceManager with message pump= 17142.489726027397 us/run
*RESULT task: run 10000 immediate tasks with one queue message loop= 5126.6598360655735 us/run
*RESULT task: run 10000 immediate tasks with one queue single thread in WorkerPool= 9817.776470588235 us/run

With change:
taskset -c 0 base_perftests --gtest_filter=SequenceManagerPerfTest.RunTenThousandImmediateTasks_OneQueue*
*RESULT task: run 10000 immediate tasks with one queue SequenceManager with message loop= 15673.59375 us/run
*RESULT task: run 10000 immediate tasks with one queue SequenceManager with message pump= 8832.218694885361 us/run
*RESULT task: run 10000 immediate tasks with one queue message loop= 5149.919670442842 us/run
*RESULT task: run 10000 immediate tasks with one queue single thread in WorkerPool= 9926.059523809523 us/run

BUG=898294

Change-Id: I414ff18fd0c92a3837b50e20165bf295b4f63082
Reviewed-on: https://chromium-review.googlesource.com/c/1291710Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602430}
parent 88389529
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "base/debug/activity_tracker.h" #include "base/debug/activity_tracker.h"
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h"
#include "base/threading/platform_thread_internal_posix.h" #include "base/threading/platform_thread_internal_posix.h"
#include "base/threading/scoped_blocking_call.h" #include "base/threading/scoped_blocking_call.h"
#include "base/threading/thread_id_name_manager.h" #include "base/threading/thread_id_name_manager.h"
...@@ -134,6 +135,30 @@ bool CreateThread(size_t stack_size, ...@@ -134,6 +135,30 @@ bool CreateThread(size_t stack_size,
return success; return success;
} }
#if defined(OS_LINUX)
// Store the thread ids in local storage since calling the SWI can
// expensive and PlatformThread::CurrentId is used liberally. Clear
// the stored value after a fork() because forking changes the thread
// id. Forking without going through fork() (e.g. clone()) is not
// supported, but there is no known usage. Using thread_local is
// fine here (despite being banned) since it is going to be allowed
// but is blocked on a clang bug for Mac (https://crbug.com/829078)
// and we can't use ThreadLocalStorage because of re-entrancy due to
// CHECK/DCHECKs.
thread_local pid_t g_thread_id = -1;
void ClearTidCache() {
g_thread_id = -1;
}
class InitAtFork {
public:
InitAtFork() { pthread_atfork(nullptr, nullptr, ClearTidCache); }
};
#endif // defined(OS_LINUX)
} // namespace } // namespace
// static // static
...@@ -143,7 +168,16 @@ PlatformThreadId PlatformThread::CurrentId() { ...@@ -143,7 +168,16 @@ PlatformThreadId PlatformThread::CurrentId() {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
return pthread_mach_thread_np(pthread_self()); return pthread_mach_thread_np(pthread_self());
#elif defined(OS_LINUX) #elif defined(OS_LINUX)
return syscall(__NR_gettid); static NoDestructor<InitAtFork> init_at_fork;
if (g_thread_id == -1) {
g_thread_id = syscall(__NR_gettid);
} else {
DCHECK_EQ(g_thread_id, syscall(__NR_gettid))
<< "Thread id stored in TLS is different from thread id returned by "
"the system. It is likely that the process was forked without going "
"through fork().";
}
return g_thread_id;
#elif defined(OS_ANDROID) #elif defined(OS_ANDROID)
return gettid(); return gettid();
#elif defined(OS_FUCHSIA) #elif defined(OS_FUCHSIA)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment