mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 23:13:34 +00:00
3609f65de3
If pthread_create() is linked into the binary, then the cosmo runtime will create an independent dlmalloc arena for each core. Whenever the malloc() function is used it will index `g_heaps[sched_getcpu() / 2]` to find the arena with the greatest hyperthread / numa locality. This may be configured via an environment variable. For example if you say `export COSMOPOLITAN_HEAP_COUNT=1` then you can restore the old ways. Your process may be configured to have anywhere between 1 - 128 heaps We need this revision because it makes multithreaded C++ applications faster. For example, an HTTP server I'm working on that makes extreme use of the STL went from 16k to 2000k requests per second, after this change was made. To understand why, try out the malloc_test benchmark which calls malloc() + realloc() in a loop across many threads, which sees a a 250x improvement in process clock time and 200x on wall time The tradeoff is this adds ~25ns of latency to individual malloc calls compared to MODE=tiny, once the cosmo runtime has transitioned into a fully multi-threaded state. If you don't need malloc() to be scalable then cosmo provides many options for you. For starters the heap count variable above can be set to put the process back in single heap mode plus you can go even faster still, if you include tinymalloc.inc like many of the programs in tool/build/.. are already doing since that'll shave tens of kb off your binary footprint too. Theres also MODE=tiny which is configured to use just 1 plain old dlmalloc arena by default Another tradeoff is we need more memory now (except in MODE=tiny), to track the provenance of memory allocation. This is so allocations can be freely shared across threads, and because OSes can reschedule code to different CPUs at any time.
89 lines
2.5 KiB
C
89 lines
2.5 KiB
C
#ifndef COSMOPOLITAN_LIBC_THREAD_TLS_H_
|
|
#define COSMOPOLITAN_LIBC_THREAD_TLS_H_
|
|
|
|
#define TLS_ALIGNMENT 64
|
|
|
|
#define TIB_FLAG_VFORKED 1
|
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
COSMOPOLITAN_C_START_
|
|
|
|
struct CosmoFtrace { /* 16 */
|
|
char ft_once; /* 0 */
|
|
char ft_noreentry; /* 1 */
|
|
int ft_skew; /* 4 */
|
|
int64_t ft_lastaddr; /* 8 */
|
|
};
|
|
|
|
/* cosmopolitan thread information block (512 bytes) */
|
|
/* NOTE: update aarch64 libc/errno.h if sizeof changes */
|
|
/* NOTE: update aarch64 libc/proc/vfork.S if sizeof changes */
|
|
/* NOTE: update aarch64 libc/nexgen32e/gc.S if sizeof changes */
|
|
struct CosmoTib {
|
|
struct CosmoTib *tib_self; /* 0x00 */
|
|
struct CosmoFtrace tib_ftracer; /* 0x08 */
|
|
void *tib_garbages; /* 0x18 */
|
|
intptr_t tib_locale; /* 0x20 */
|
|
intptr_t tib_pthread; /* 0x28 */
|
|
struct CosmoTib *tib_self2; /* 0x30 */
|
|
_Atomic(int32_t) tib_tid; /* 0x38 transitions -1 → tid → 0 */
|
|
int32_t tib_errno; /* 0x3c */
|
|
uint64_t tib_flags; /* 0x40 */
|
|
int tib_ftrace; /* inherited */
|
|
int tib_strace; /* inherited */
|
|
_Atomic(uint64_t) tib_sigmask; /* inherited */
|
|
_Atomic(uint64_t) tib_sigpending;
|
|
_Atomic(uint64_t) tib_syshand; /* win32=kThread, xnusilicon=pthread_t */
|
|
char *tib_sigstack_addr;
|
|
uint32_t tib_sigstack_size;
|
|
uint32_t tib_sigstack_flags;
|
|
void *tib_nsync;
|
|
void *tib_keys[48];
|
|
} __attribute__((__aligned__(64)));
|
|
|
|
extern int __threaded;
|
|
extern char __tls_morphed;
|
|
extern unsigned __tls_index;
|
|
|
|
char *_mktls(struct CosmoTib **) libcesque;
|
|
void __bootstrap_tls(struct CosmoTib *, char *) libcesque;
|
|
|
|
#ifdef __x86_64__
|
|
extern char __tls_enabled;
|
|
#define __tls_enabled_set(x) __tls_enabled = x
|
|
#elif defined(__aarch64__)
|
|
#define __tls_enabled true
|
|
#define __tls_enabled_set(x) (void)0
|
|
#else
|
|
#error "unsupported architecture"
|
|
#endif
|
|
|
|
void __set_tls(struct CosmoTib *) libcesque;
|
|
|
|
/**
|
|
* Returns location of thread information block.
|
|
*
|
|
* This can't be used in privileged functions.
|
|
*/
|
|
forceinline pureconst struct CosmoTib *__get_tls(void) {
|
|
#ifdef __chibicc__
|
|
return 0;
|
|
#elif __x86_64__
|
|
struct CosmoTib *__tib;
|
|
__asm__("mov\t%%gs:0x30,%0" : "=r"(__tib));
|
|
return __tib;
|
|
#elif defined(__aarch64__)
|
|
register struct CosmoTib *__tls __asm__("x28");
|
|
return __tls - 1;
|
|
#endif
|
|
}
|
|
|
|
#ifdef __x86_64__
|
|
#define __adj_tls(tib) (tib)
|
|
#elif defined(__aarch64__)
|
|
#define __adj_tls(tib) ((struct CosmoTib *)(tib) + 1)
|
|
#endif
|
|
|
|
COSMOPOLITAN_C_END_
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
#endif /* COSMOPOLITAN_LIBC_THREAD_TLS_H_ */
|