Hacker News new | ask | show | jobs
by skavi 75 days ago
does anyone familiar with the art have thoughts on why only tcmalloc switched from thread caches to cpu caches? would it make linux behavior diverge too much from other platforms?
2 comments

(Not an expert but ...) unless you pin threads to cores, which is not the default and somewhat awkward in Linux for user applications, having a per-thread cache doesn't really make sense as your thread could be moved to another core and then your cache will no longer be local to the physical cache.
Don't you just take the current cpuid when you go to access the cache again? Then you mutex and access the per-cpu state. There is a tiny race window but 99.99% of the time you will be hitting the same cpu's cache as you just identified, and there will be ~zero contention.

The main issue is thread preemption while you're holding a per-core cache mutex. Some other thread can't do meaningful work using the cache while the holder is sleeping.

With restartable sequences in Linux, there is no need for mutexes or RMW atomics. The mutual exclusion is guaranteed by the kernel.
Fair enough.
i think we agree that per cpu caching seems superior. i’m looking for the other side of this. most allocators seem to have stuck with per thread.
If you use a thread-local data structure, your allocator can pretend that it is running on a single-core, single-task system.

If you use a CPU-local data structure, you must handle the case where, mid-way through a call to your allocator, the CPU runs a second thread that makes another call to your allocator (and that, too, can get interrupted by another thread that allocates memory, etc.)

That makes thread-local easier to implement and likely faster (it doesn’t require any memory barriers in the fast path)

Also, good schedulers try to avoid moving threads between CPUs. The better they manage to do that, the lower the cost of having per thread data structures (there likely still is a price, as there most of the time are more threads than CPUs on a system)

https://google.github.io/tcmalloc/rseq.html

i don’t believe rseq based cpu local caches require memory barriers on the fast path.

https://lwn.net/Articles/1033957/:

  rseq_cs

   The rseq_cs field is a pointer to a struct rseq_cs. Is is NULL when no
   rseq assembly block critical section is active for the registered
   thread. Setting it to point to a critical section descriptor (struct
   rseq_cs) marks the beginning of the critical section.
I’m not sure I fully understand that man page (it never seems to say callers have to clear that field at the end of a critical section, for example), but doesn’t that mean the caller has to guarantee setting rseq_cs happens_before any code in the critical section? That’s a memory barrier.
That is because you do not need to clear the field at the end of a critical section. It contains the contiguous instruction range where it fires so there is no problem with leaving it active forever unless you have another critical section where you want to use it.

No explicit memory barrier is required anywhere as the value is only read in supervisor mode and a privilege switch implicitly issues a LS-LS barrier on all major architectures. Even if you did not want to rely on that, you would only need a single S-LS barrier when you store the control structure the very first time.

TCMalloc TL here :)

The code is executed by a single thread. Everything retires in program order. There is no need for a memory barrier between starting the critical section and its body.

It's just a compiler barrier (signal fence), not a memory barrier that concerns the CPU. The CPU is free to reorder loads and stores.
Setting aside whether or not you can pull off a lock free approach here we can be certain of a couple things. There will be at least some overhead that must be paid somewhere even if that's on a separate management thread. And there will be a lot of additional complexity because that's just how concurrency always is.

Meanwhile the better the scheduler performs the more competitive the thread local approach becomes.

https://docs.kernel.org/userspace-api/rseq.html

cost for interruption in an rseq critical section is that the PC gets overwritten to the rseq abort entry point before the task is rescheduled. no management thread necessary.

should be fairly minimal cost, especially assuming interruptions in the critical section are rare.

Exactly. You want memory arenas that are hot in this CPU's caches. If your thread moves, its per-thread caches are now elsewhere. Original TCMalloc was developed in the days of 2-4 core servers. Current TCMalloc was an evolution in the context of 32+ core servers.
my feeling is that the space efficiency gains are probably more significant than the reduction in core migration costs. many applications have far more threads than the system has cores.
Sure, also true that the per-CPU scheme co-evolved with the proliferation of services with thread-per-request architectures having way more TIDs than cores.
I think tcmalloc gains on thread churn and oversubscription by going the cpu-cache route on Linux. on other platforms, I am not so sure but that can be offset by say a treiber-stack like setup for cross-thread frees/teardowns. So lesser code for Linux for similar fastpath design I guess.
> on other platforms, I am not so sure

tcmalloc only works on Linux.