How big is (approximately) an I/O syscall overhead on Linux from C
program, I mean how bad is running e.g. many small read / write
operations compared with read / write
on large buffers (on regular files or network sockets)? App is strongly multithreaded.
A system call involves the following actions, which do not occur during a simple procedure call, and thus entails a high overhead: A context switch. A trap to a specific location in the interrupt vector.
syscalls are really not that expensive. The hardware overhead to cross the privilege boundary both times is only like 100 nanoseconds on modern hardware with maybe a few hundred nanoseconds with full speculation mitigations enabled. Essentially everything else is work that someone in the system needs to do.
System calls look like procedure calls when they appear in a program, but transfer control to the kernel when they are invoked at run time. ( read is an example of a system call in Unix.) A system call invokes a trap as discussed below. A trap is a hardware event that invokes a trap handler in the kernel.
Syscalls take at least 1-2 microseconds on most modern machines just for the syscall overhead, and much more time if they're doing anything complex that could block or sleep. Expect at least 20 microseconds and up to the order of milliseconds for IO.
Syscalls take at least 1-2 microseconds on most modern machines just for the syscall overhead, and much more time if they're doing anything complex that could block or sleep. Expect at least 20 microseconds and up to the order of milliseconds for IO. Compare this with a tiny function call or macro that reads a byte from a userspace buffer, which is likely to complete in a matter of nanoseconds (maybe 200 ns on a bad day).
You can measure this yourself. Just open /dev/zero
and do some reading and writing while measuring the time. Also vary the number of bytes you put into each call - e.g. 1 bytes, 2 bytes, 128 bytes, .. 4096bytes. Also take care to use the read(2)
and write(2)
syscalls and not anything using internal buffers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With