3 ms·
Good IPC requires tight coupling between the IPC mechanism and the scheduling mechanism. In QNX, you call MsgSend, which sends a message and blocks. On the re
by Animats 11y ago
Good IPC requires tight coupling between the IPC mechanism and the scheduling mechanism. In QNX, you call MsgSend, which sends a message and blocks. On the receive side, a process waiting in a MsgReceive gets the message and unblocks. The fast path for this, when there's a thread waiting for a message, does not go through CPU dispatching or CPU switching on a multiprocessor. This is crucial to performance.
You do pay a penalty for copying. For short messages, it's low. For anything that fits in cache, that you just created and will use immediately on the receive side, it's very low. Really big messages may add 10%-20% overhead. This is the main problem with drivers in user space, because they don't do anything with the data, they just move it around.
Incidentally, copying under QNX is preemptable by high-priority tasks. There's a hard upper bound on the longest uninterpretable kernel operation, and it's measured in microseconds. That's why you can use QNX for hard real time.
Contrast what happens on Linux. You have a few choices (System V IPC, pipes, sockets), but they all work like I/O operations. You send something, and you don't block when you do. If there's someone waiting on the queue or pipe, their thread becomes active, which means a trip through the scheduler to get them going.
Since the sending thread isn't blocked, if there's a free CPU, the receiving thread starts up on a different CPU, which means lots of cache misses. The sending thread probably blocks shortly after the send by reading something or waiting for a message, but the OS doesn't know that's going to happen. So when it does, there's another trip through the dispatcher for work to do.
In a compute-bound environment, this may mean that each IPC operation sends you to the end of the line for CPU time. Trying to use IPC operations like subroutine calls is painfully slow in such systems.
Write something that does short IPCs and waits for replies, and benchmark it. Then add some compute-bound tasks, enough to keep all the CPUs busy. If the IPC task performance drops by orders of magnitude, the IPC mechanism is badly designed.
L4 is very impressive, but I think they went a little too far by using shared memory as the main interprocess communication mechanism. This gets copying out of the kernel, but means that one process can screw up the format of the memory region it shares with another process. The OS may be robust and secure, but attacks on the process you're talking to by messing with pointers in the shared area may be possible.
The big problem with QNX is that it costs money. Linux is free, and Windows is basically given away with hardware and trialware.
I hope that someone writes a QNX-like microkernel in Rust. We need that.
- aidenn0 11y agoI know it makes me heterodox in the microkernel community, but I like the choice of shared-memory there. There are well known data-structures that work with shared memory with well-understood behaviors with a malicious writer. They are tricky to get right, but only need to be implemented once, verified and reused many times Furthermore, as soon as you are communicating with another process, you have to deal with receiving garbage if that process misbehaves, so it's not all sunshine and roses even if you use a more traditional IPC mechanism.
- filereaper 11y ago"There are well known data-structures that work with shared memory with well-understood behaviors with a malicious writer." I'm very interested in learning about these shared memory data structures, would you mind providing a few links or mention some papers I could read. We implemented a real-time microkernel in university where QNX has its roots [0]. It used traditional copy based IPC however. I'd like to see how these shared-memory data structures could have been used. Thanks. [0] https://www.student.cs.uwaterloo.ca/~cs452/ https://www.student.cs.uwaterloo.ca/~cs452/
- aidenn0 11y agoI'm actually struggling to find this in the literature, but I've seen it used more than once. Here's the most simple version: Minimum two pages, each readable from both sides and one is writable from each side. You need this even for a single-directional queue. Have a simple post/ack counter that is modulo something much larger than the size of the queue (i.e. an unsigned long on most architectures). If (post - ack) is ever greater than the size of the queue, the queue should be considered no longer safe to use; both readers and writers should check this anytime they access these values. Otherwise there are (post - ack) bytes pending in the queue. To write, write bytes at (post MOD size) and increment post; to read, read from (ack MOD size) and increment ack. That is a simple one-to-one byte-stream queue; fixed-size message queues are an obvious change, non-fixed size message queues are significantly more complex, but proceed from the same idea. Many-to-one queues are a lot more complex, and one-to-many is more complex if you want reliable data transfer, but much simpler if you don't (just eliminate the ack completely) If all you care about is throughput and you have more than 2 cores, then just spinning on post/ack is sufficient. Otherwise some synchronization primitive is needed. On many real-world architectures, particularly SMP ones there are a lot of fiddly details to get right with the MMU &ct. but the underlying idea is simple.