How per-process CPU-usage percentage is computed.
Every hz tick, the scheduler increments a CPU tick counter of the currently running thread. This tick counter is used for CPU usage percentage estimation for every thread. This scheduler works with a 5 second long timing window. The CPU usage percentage estimate is computed over this 5 second window. That means, if a thread has its tick counter equal to half of the hz * 5, then the CPU usage for this thread is reported to be 50%. (Where hz is the system clock’s frequency)
Let us now have a look at how this tick counter is updated. There are two ways how this counter can be changed.
- Every hz tick the the value of the CPU tick counter of the currently running thread is compared with the value 5 * hz. If the counter value is less, it is incremented.
- If a context switch happens from an old thread to a new thread, the timestamp of the context switch is saved into the old thread. When later a context is switched back to our old thread, the counter of the thread (here referred to as the old one) is decremented by the length of the time the thread was taken off the CPU.
So the whole algorithm is quite simple when compared to the 4BSD scheduler. The estimate is not affected by the CPU utilization prior to the last 5 second timing window. The estimate is determined solely by the CPU usage in the latest timing window. When a thread is taken off the processor for some time, the counter for that thread is decremented proportionally to that time. This should lead to the expected behaviour that threads not utilizing the CPU will get 0%. And if a thread is using almost all CPU time, it will get 100%.
This is not the way how ULE or the 4BSD schedulers handle it. I firstly thought I will implement it in the way how ULE does it – because it seemed quite simple to me on the first sight. But when I looked at it in more detail and started thinking how and when the parameters involved can change, I decided I will use even a simpler approach – and that’s it.
What I will be working on this week.
I now want to add the cpuset functionality to my scheduler. In FreeBSD, you have the ability to restrict certain tasks to certain CPUs. This scheduler ignores it for now. So I want to add support for it.