3 ms·
All else being equal, the one thing Java can do that the OS can’t is avoid paying for context switches. There is a cost to crossing the user space/kernel space
by pdpi 3y ago
All else being equal, the one thing Java can do that the OS can’t is avoid paying for context switches. There is a cost to crossing the user space/kernel space boundary that you can avoid with user space lightweight threads.
- otabdeveloper4 3y agoFalse. The context will need to be switched regardless of where you're doing it. (Userspace or kernel.) The cost of crossing the userspace/kernel boundary happens on any system call and has nothing to do with context switching. (Modern OS's typically context switch during system calls for i/o, exactly the same as userspace threads.)
- gpderetta 3y agoUser/kernel space switch is a different kind of switch. Also when you do it cooperatively, context switches can be a bit more efficient as the runtime has better knowledge about the set of live registers at the switch point.
- otabdeveloper4 3y agoThe "different kind of switch" you talk about doesn't exist on any OS of the past 30 years.
- gpderetta 3y agoI'm talking about raising a process privilege level, via whatever syscall mechanism. Which is a completely different code path than a process/thread context switch and, among other things, doesn't require going through the scheduler.
- Sesse__ 3y agoThe page tables change in a kernel context switch, whereas they don't in a user context switch. Depending on your CPU and OS, this may or may not matter; e.g., modern CPUs typically have caches that are tagged with the context (e.g. PCID on Intel), but the Spectre flavor of the day may require that you simply flush the entire L1 cache on context switch anyway if you're unlucky. The biggest cost of a context switch, though, is typically that your CPU goes to do something else, which reduces the efficiency of any caches (including branch prediction etc.). This is the same whether you have kernel or userspace context switches, assuming the threads you're switching between touch different code and/or data.
- gpderetta 3y agoWell, page tables don't change on a thread context switch, that is relevant for this discussion.
- PhilipRoman 3y agoWhy do page tables need to change in a context switch between two threads? AFAIK the kernel is mapped in every process, just not accessible in user mode.
- ninkendo 3y agoGreen threads can be switched between in a single time slice without ever doing a single syscall. This is trivially true: you can make a simple coroutine implementation in C that can switch back and forth between routines thousands of times in a single timeslice.
- otabdeveloper4 3y agoTechnically true, but nobody ever does this because it is a massive waste of CPU cycles. Both OS and "green" threads are meant to switch when doing i/o.
- gpderetta 3y agoUserspace IO is a thing though. Even with traditional kernel side IO, you might not want to attempt a syscall if the socket is known not to be ready. Also when implementing purely userspace synchronization primitives, being able to swap to a different context purely in userspace is nice.
- fulafel 3y agoBoth of course pay something, but virtual threads may pay less. Is this significant for virtual thread use cases? OS threads are cheap since there's no virtual memory context switch.