Notes about linux swapper task.

What

The swapper task is the task running, if no other task is runnable. It has the lowest possible priority, so that’s why it’s running if no other task is runnable.

Why

Programmatic reason

This simplifies process scheduling a lot, because you don’t have to care about the special case: “What happens if no task is runnable?”, because there always is at least one task runnable, the idle task. Also you can count the amount of cpu time used per task. Without the idle task, which task gets the cpu-time accounted no one needs?

Historical reason

Before we had cpus which are able to step-down or go into power saving modes, it had to run on full speed at any time. It ran a series of NOP-instructions, if no tasks were runnable. Today the scheduling of the idle task usually steps down the cpu by using HLT-instructions (halt), so power is saved. So there is a functionality somehow in the idle task in our days.

Demo

How can you believe that swapper (PID 0) even exists? if you can’t see it using ps. I am going to use bpftrace for demonstrating that. In the demo I am going to trace the kernel function hrtimer_wakeup which is responsible for waking up a process and move it to the set of runnable processes. During the trace I am going to print the pid of the calling process and the executable name (the comm field of the task_struct [/include/linux/sched.h]). Here is the command:

1
sudo bpftrace -e ‘kfunc:hrtimer_wakeup { printf(“%s:%d\n”,curtask->comm,curtask->pid); }’

From the output we can see we have 3 instances of swapper: swapper/0, swapper/1 and swapper/2 all of them with PID 0. The reason we have three is because my VM has 3 virtual CPUs and there is a swapper process for each one of them — see the output of the command in the image below.


参考资料:

  1. Why do we need a swapper task in linux?
  2. Deep understanding of swapper processes in perf reports
  3. The Linux Process Journey — PID 0 (swapper)