2 ms·
> Those are only saved/restored when switching between processes not between threads. No, those registers are per-thread. The only things that are per-process
by amluto 9y ago
> Those are only saved/restored when switching between processes not between threads.
No, those registers are per-thread. The only things that are per-process are the page tables and, in special cases, CR4 and LDTR. If you want to see the code on Linux, look at switch_mm_irqs_off().
> Context switching when dealing with threads is typically done in software, not in hardware
Hardware context switches (the x86 "task" mechanism) have been obsolete for a long time and are unavailable in long mode.
> precisely so that registers that are not used (like the segment registers) don't get needlessly saved & restored.
And that's the problem: the OS doesn't know that the registers are unused until it saves them.
> Saving/restoring registers isn't even the expensive part of a context switch anyway. Switching address spaces, cache flushes/misses, etc... are all more expensive than saving/restoring registers.
Go compare WRMSR to MSR_FS_BASE to the cost of a CR3 (page table) write. You might be surprised. And that CR3 write is about to get considerably faster, maybe in Linux 4.13, when my PCID patch set lands. (Yes, Linux will eventually use WRFSBASE instead, but that's a minefield and will take a while.)
- mee_too 9y agoThis person switches threads :) But in practice, I guess this legacy architecture adds a nanosecond or two to a thread switch. Is that a big deal? How many switches does a desktop PC do in a second? How about an AWS/Azure/GCP server? I've never experienced lag in a game or app or web server due to slow thread switch. Server side async is on the rise anyway, minimizing number of threads and switches.
- amluto 9y ago> But in practice, I guess this legacy architecture adds a nanosecond or two to a thread switch. Is that a big deal? I would guess the total wasted time is more like 500 cycles. This won't make your game lag, but it does mean that extremely low overhead synchronous IPC can't happen on x86.
- mee_too 9y agoWhat types of programs are impacted by the lack of low overhead synchronous IPC?
- amluto 9y agoAs an example, there are a bunch of things that work well on microkernels on non-x86 architectures that involve splitting drivers across processes. On Windows, a lot of the windowing system is in the kernel in win32k.sys. Microsoft didn't do this for fun -- they did it because the old way (NT3.5?) was too slow.