Skip to content

Commit 84588f4

Browse files
add options and docs
1 parent 774b813 commit 84588f4

File tree

7 files changed

+50
-1
lines changed

7 files changed

+50
-1
lines changed

libc/config/config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
"LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE": {
6060
"value": 1073741824,
6161
"doc": "Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB)."
62+
},
63+
},
64+
"unistd": {
65+
"LIBC_CONF_ENABLE_TID_CACHE": {
66+
"value": true,
67+
"doc": "Enable caching mechanism for gettid to avoid syscall (only effective in fullbuild mode, default to true). Please refer to Undefined Behavior documentation for implications."
68+
},
69+
"LIBC_CONF_ENABLE_PID_CACHE": {
70+
"value": true,
71+
"doc": "Enable caching mechanism for getpid to avoid syscall (default to true). Please refer to Undefined Behavior documentation for implications."
6272
}
6373
}
6474
}

libc/docs/configure.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ to learn about the defaults for your platform and target.
4343
* **"string" options**
4444
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
4545
- ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen.
46+
* **"unistd" options**
47+
- ``LIBC_CONF_ENABLE_PID_CACHE``: Enable caching mechanism for getpid to avoid syscall (default to true). Please refer to Undefined Behavior documentation for implications.
48+
- ``LIBC_CONF_ENABLE_TID_CACHE``: Enable caching mechanism for gettid to avoid syscall (only effective in fullbuild mode, default to true). Please refer to Undefined Behavior documentation for implications.

libc/docs/dev/undefined_behavior.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ Cached ``getpid/gettid``
9595
Since version ``2.25``, glibc removes its cache mechanism for ``getpid/gettid``
9696
(See the history section in https://man7.org/linux/man-pages/man2/getpid.2.html).
9797
LLVM's libc still implements the cache as it is useful for fast deadlock detection.
98-
The cache mechanism is also implemented in MUSL and bionic.
98+
The cache mechanism is also implemented in MUSL and bionic. The tid/pid cache can
99+
be disabled by setting ``LIBC_CONF_ENABLE_TID_CACHE`` and ``LIBC_CONF_ENABLE_PID_CACHE``
100+
to ``false`` respectively.
99101

100102
Unwrapped ``SYS_clone/SYS_fork/SYS_vfork``
101103
------------------------------------------

libc/src/__support/OSUtil/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ add_object_library(
1616
${target_os_util}
1717
)
1818

19+
if (LIBC_CONF_ENABLE_PID_CACHE)
20+
set(libc_copt_enable_pid_cache 1)
21+
else()
22+
set(libc_copt_enable_pid_cache 0)
23+
endif()
24+
1925
if(TARGET libc.src.__support.OSUtil.${LIBC_TARGET_OS}.pid)
2026
add_object_library(
2127
pid
2228
ALIAS
2329
DEPENDS
2430
.${LIBC_TARGET_OS}.pid
31+
COMPILE_OPTIONS
32+
-DLIBC_COPT_ENABLE_PID_CACHE=${libc_copt_enable_pid_cache}
2533
)
2634
endif()

libc/src/__support/OSUtil/pid.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
#include "hdr/types/pid_t.h"
1212
#include "src/__support/macros/attributes.h"
1313
#include "src/__support/macros/optimization.h"
14+
15+
#ifndef LIBC_COPT_ENABLE_PID_CACHE
16+
#define LIBC_COPT_ENABLE_PID_CACHE 1
17+
#endif
18+
1419
namespace LIBC_NAMESPACE {
1520

1621
class ProcessIdentity {
@@ -23,9 +28,13 @@ class ProcessIdentity {
2328
LIBC_INLINE static void end_fork() { fork_inflight = false; }
2429
LIBC_INLINE static void refresh_cache() { cache = get_uncached(); }
2530
LIBC_INLINE static pid_t get() {
31+
#if LIBC_COPT_ENABLE_PID_CACHE
2632
if (LIBC_UNLIKELY(fork_inflight))
2733
return get_uncached();
2834
return cache;
35+
#else
36+
return get_uncached();
37+
#endif
2938
}
3039
};
3140

libc/src/__support/threads/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.mutex)
3535
)
3636
endif()
3737

38+
if (LIBC_CONF_ENABLE_TID_CACHE)
39+
set(libc_copt_enable_tid_cache 1)
40+
else()
41+
set(libc_copt_enable_tid_cache 0)
42+
endif()
43+
3844
add_header_library(
3945
thread_common
4046
HDRS
@@ -46,6 +52,8 @@ add_header_library(
4652
libc.src.__support.CPP.string_view
4753
libc.src.__support.CPP.stringstream
4854
libc.hdr.types.pid_t
55+
COMPILE_OPTIONS
56+
-DLIBC_COPT_ENABLE_TID_CACHE=${libc_copt_enable_tid_cache}
4957
)
5058

5159
if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.thread)

libc/src/__support/threads/thread.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_THREAD_H
1010
#define LLVM_LIBC_SRC___SUPPORT_THREADS_THREAD_H
1111

12+
#ifndef LIBC_COPT_ENABLE_TID_CACHE
13+
#define LIBC_COPT_ENABLE_TID_CACHE 1
14+
#endif
15+
1216
#include "hdr/types/pid_t.h"
1317
#include "src/__support/CPP/atomic.h"
1418
#include "src/__support/CPP/optional.h"
@@ -233,10 +237,15 @@ struct Thread {
233237

234238
LIBC_INLINE void refresh_tid() { this->attrib->tid = get_uncached_tid(); }
235239
LIBC_INLINE void invalidate_tid() { this->attrib->tid = -1; }
240+
236241
LIBC_INLINE pid_t get_tid() {
242+
#if LIBC_COPT_ENABLE_TID_CACHE
237243
if (LIBC_UNLIKELY(this->attrib->tid < 0))
238244
return get_uncached_tid();
239245
return this->attrib->tid;
246+
#else
247+
return get_uncached_tid();
248+
#endif
240249
}
241250
};
242251

0 commit comments

Comments
 (0)