Part 5 of 8 · 3 min read
Measuring
Think of timing a lift ride by riding it a million times and dividing. One ride is too quick to time, and many rides give a steady average. The same trick measures a system call. The analogy stops in one place: a lift takes the same time every ride, and a system call varies with caches and load.
In plain words: A system call can be timed by repeating it a million times and dividing the total by one million. On the test machine one call of getppid took about 87 nanoseconds. The shell command time splits the run time into real, user and system time, and a program that only makes system calls shows more system time than user time.
The key lines.
Page 2 estimated that a system call costs much more than a plain function call. You can measure it. The program below calls getppid one million times, which is a system call that always goes to the kernel, and divides the elapsed time by one million:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(void) {
enum { N = 1000000 };
struct timespec a, b;
clock_gettime(CLOCK_MONOTONIC, &a);
long sum = 0;
for (int i = 0; i < N; i++) sum += getppid();
clock_gettime(CLOCK_MONOTONIC, &b);
double ns = ((b.tv_sec - a.tv_sec) * 1e9 + (b.tv_nsec - a.tv_nsec)) / N;
printf("%d getppid calls, about %.0f ns per call (sum %ld)\n", N, ns, sum);
return 0;
}
On the test machine:
1000000 getppid calls, about 87 ns per call (sum 577000000)
A worked example.
So a system call took about 87 nanoseconds here. The figure depends on the processor, the kernel and the security settings, so your machine will differ. It is still thousands of times more than a plain addition, which takes a fraction of a nanosecond. The variable sum is only there so the compiler cannot throw the loop away.
The shell command time splits the run time into three parts:
real 0m0.098s
user 0m0.039s
sys 0m0.059s
- real is the elapsed clock time, 0.098 seconds.
- user is the processor time spent running the program's own code in user mode, 0.039 seconds.
- sys is the processor time spent in the kernel on the program's behalf, 0.059 seconds.
The program did almost nothing but call the kernel, and more of its time went to sys than to user, as expected. Check: one million calls at 87 ns is 0.087 seconds, close to the 0.098 real time (which also includes the start-up of the process). A program that computes most of the time shows the reverse, a large user and a tiny sys. Use the split to see whether a slow program is slow in its own code or in the kernel.
User time and system time
User time is the processor time a process spends executing its own instructions in user mode. System time is the processor time the kernel spends in kernel mode doing work on that process's behalf, such as handling its system calls.
Example: a program that loops over a million system calls spends most of its processor time as system time.
Check yourself:
1. Which part of time rises when a program makes many system calls?
2. Why does the program print sum?
3. About how long would 10 million calls take at 87 ns each?