3 ms·
The author states: >"In practice context switching is expensive because it screws up the CPU caches (L1, L2, L3 if you have one, and the TLB – don't forget the
by bogomipz 10y ago
The author states:
>"In practice context switching is expensive because it screws up the CPU caches (L1, L2, L3 if you have one, and the TLB – don't forget the TLB!)."
How does a context switch "screw up" the L1 through L3 caches? Yes when there is context switch the TLB gets flushed and the kernel then needs to "walk" the page tables which is expensive and yes I can see the L1 needing to flush some lines but is that really screwing up the L1, L2 and L3 caches, the later two being fairly large these days?
- JoeAltmaier 10y agoIts understood that different threads, especially in different processes, will quickly rewrite the caches to suit their own needs. Returning to a thread, especially after any additional delay, threads will find their own data essentially gone. Its always going to create better cache utility if a thread can run to completion before a switch.
- gpderetta 10y agoThe biggest cache trashing on a thread switch are possibly the lines containing the thread stack which are very unlikely to be shared between threads. Also thread are likely to be working on distinct data. BTW, TLB doesn't need to be flushed on a thread switch, only on a process switch, and even then more modern systems can tag TLB entries by process id, reducing the need for flushing.
- bogomipz 10y agoI should have specified I was talking about a process context switch not a thread. >"The biggest cache trashing on a thread switch are possibly the lines containing the thread stack which are very unlikely to be shared between threads. Also thread are likely to be working on distinct data." Sure, I don't think they are every shared are they? >"BTW, TLB doesn't need to be flushed on a thread switch, only on a process switch, and even then more modern systems can tag TLB entries by process id," Indeed and this often gets left out of the discussion, its not mentioned in the blog post certainly. I believe on x86-64 this is called ASID and on ARM chips its TPID.
- Someone 10y agoA context switch changes the virtual-to-physical mapping. So, _if_ a cache uses virtual addresses for determining whether data is in the cache a context switch has to flush the cache. It looks like (1) x86 is virtually indexed and physically tagged (i.e. you need a virtual address to find the cache line data might be in, and the physical address to check whether that line actually holds the data. See https://en.wikipedia.org/wiki/CPU_cache#Address_translation https://en.wikipedia.org/wiki/CPU_cache#Address_translation for details)) (1) http://www.realworldtech.com/sandy-bridge/7/ http://www.realworldtech.com/sandy-bridge/7/ but that may have changed, they might use different strategies for the various cache levels, or they may by now use some other tricks. Address translation is very hairy in multi-core systems.
- bogomipz 10y ago>" So, _if_ a cache uses virtual addresses for determining whether data ..." I was curious about your "if" caveat. I believe nearly all modern CPUs uses the virtual address as it doesn't know about the physical addresses no?
- pm215 10y agoThe CPU does know the physical address, because the MMU is part of the CPU; so you can have a physically indexed cache if you want. The problem is just that you can't do the cache lookup in parallel with the MMU physical-to-virtual lookup, so it's slower than if you used a virtually-indexed cache.
- bogomipz 10y agoSure, I didn't articulate my comment very well. I meant to say that the L1-3 caches operate on virtual addresses because they don't know the physical address but the MMU does since all memory access must transit through the MMU.
- Someone 10y agoThe reason for the "_if_" is that I know too little about current hardware to know whether that's the norm, the exception, or extinct, so I tried to be careful. Also, I'm using CPU for (CPU+MMU) because that's the case on all modern CPUs that have a MMU, as far as I know.