kern.sched.quantum: Creepy, sadistic scheduler
pmc at citylink.dinoex.sub.org
Wed Apr 4 15:13:35 UTC 2018
Alban Hertroys wrote:
>> Occasionally I noticed that the system would not quickly process the
>> tasks i need done, but instead prefer other, longrunning tasks. I
>> figured it must be related to the scheduler, and decided it hates me.
> If it hated you, it would behave much worse.
Thats encouraging :) But I would say, running a job 100 times slower
than expected is quite an amount of hate for my taste.
>> A closer look shows the behaviour as follows (single CPU):
> A single CPU? That's becoming rare! Is that a VM? Old hardware? Something really specific?
I don't plug in another CPU because there is no need to. Yes, its old
CPU: Intel Pentium III (945.02-MHz 686-class CPU)
ACPI APIC Table: <ASUS CUV4XDLS>
If I had bought new hardware, this one would now rot in Africa, and I
would have new hardware idling along that is spectre/meltdown affected
>> Lets run an I/O-active task, e.g, postgres VACUUM that would
> And you're running a multi-process database server on it no less. That > is going to hurt,
I'm running a lot more than only that on it. But it's all private use,
idling most of the time.
> no matter how well the scheduler works.
Maybe. But this post is not about my personal expectations on over-all
performance - it is about a specific behaviour that is not how a
scheduler is expected to behave - no matter if we're on a PDP-11 or on a
>> Now, as usual, the "root-cause" questions arise: What exactly does
>> this "quantum"? Is this solution a workaround, i.e. actually something
>> else is wrong, and has it tradeoff in other situations? Or otherwise,
>> why is such a default value chosen, which appears to be ill-deceived?
>> The docs for the quantum parameter are a bit unsatisfying - they say
>> its the max num of ticks a process gets - and what happens when
>> they're exhausted? If by default the endless loop is actually allowed
>> to continue running for 94k ticks (or 94ms, more likely) uninterrupted,
>> then that explains the perceived behaviour - buts thats certainly not
>> what a scheduler should do when other procs are ready to run.
> I can answer this from the operating systems course I followed recently. This does not apply to FreeBSD specifically, it is general job scheduling theory. I still need to read up on SCHED_ULE to see how the details were implemented there. Or are you using the older SCHED_4BSD?
I'm using the default scheduler, which is ULE. I would not go
non-default without reason. (But it seems, a reason is just appering now.)
> Now, that would cause a much worse situation in your example case. The endless loop would keep running once it gets the CPU and would never release it. No other process would ever get a turn again. You wouldn't even be able to get into such a system in that state using remote ssh.
> That is why the scheduler has this "quantum", which limits the maximum time the CPU will be assigned to a specific job. Once the quantum has expired (with the job unfinished), the scheduler removes the job from the CPU, puts it back on the ready queue and assigns the next job from that queue to the CPU.
> That's why you seem to get better performance with a smaller value for the quantum; the endless loop gets forcibly interrupted more often.
Good description. Only my (old-fashioned) understanding was that this is
the purpose of the HZ value: to give control back to the kernel, so that
a new decision can be made.
So, I would not have been surpized to see 200 I/Os for postgres
(kern.hz=200), but what I see is 9 I/Os (which indeed figures to a
"quantum" of 94ms).
But then, we were able to do all this nicely on single-CPU machines for
almost four decades. It does not make sense to me if now we state that
we cannot do it anymore because single-CPU is uncommon today.
(Yes indeed, we also cannot fly to the moon anymore, because today
nobody seems to recall how that stuff was built. *headbangwall*)
> This changing of the active job however, involves a context switch for the CPU. Memory, registers, file handles, etc. that were required by the previous job needs to be put aside and replaced by any such resources related to the new job to be run. That uses up time and does nothing to progress the jobs that are waiting for the CPU. Hence, you don't want the quantum to be too small either, or you'll end up spending significant time switching contexts.
Yepp. My understanding was that I can influence this behaviour via the
HZ value, so to tradeoff responsiveness against performance. Obviousely
that was wrong.
From Your writing, it seems the "quantum" is indeed the correct place
to tune this. (But I will still have to ponder a while about the knob
mentioned by Stefan, concerning preemption, which seems to magically
resolve the issue.)
> That said, SCHED_ULE (the default scheduler for quite a while now) was designed with multi-CPU configurations in mind and there are claims that SCHED_4BSD works better for single-CPU configurations. You may give that a try, if you're not already on SCHED_4BSD.
I'll try this at next code update.
> A much better option in your case would be to put the database on a multi-core machine.
I could plug in the second CPU, but it would mostly just heat the room.
So, a modern low-energy CPU would do better - but then, try to get a
modern CPU (+board!) that supports ECC-ram, and you'll end in the
high-end department. This old one does, so it's just perfect for 24/365.
Nevertheless, this is a software issue, and fixing it via new hardware
should be only the last resort.
>> A pure-I/O job without compute load, like "dd", does not show
>> this behaviour. Also, when other tasks are running, the unjust
>> behaviour is not so stongly pronounced.
> That is probably because dd has the decency to give the reins back to the scheduler at regular intervals.
No, rather the other way round: running dd against the piglet (aka
endless loop), both run full-speed. Running postgres VACUUM against the
piglet, postgres starves.
My (rather vague) explanation: when an I/O for dd comes back, dd
immediately requests the next one. When an I/O for postgres comes back,
postgres needs to compute their transaction ID stuff, competes against
the piglet for CPU, and looses.
More information about the freebsd-stable