Scheduling extremely inefficient

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Scheduling extremely inefficient

Post by mariuszp »

My OS is working extremely inefficiently, and I think it might be because of how the scheduler works. By inefficnently I mean it takes a few seconds to copy 1-2MB of data (for example when swapping a framebuffer to screen). When a new process is being loaded, it takes a few seconds. In Bochs, fork() is much faster than on real hardware and VirtualBox for some reason, but loading-on-demand and copy-on-writing in the child process (and the parent ofcourse) then takes a long time anyway.

My scheduler is triggered by the PIT, or by system calls. I've been using a setting of 1000 Hz for some time, and it is at that frequency that I get those unacceptable speeds. Changing it to 10 000 Hz causes it to speed up incredibly, then crash at random moments (on 1000 Hz it crashes at random moments too, but much less frequently). At 100 Hz it works much faster in VirtualBox but much slower in Bochs, still with unacceptable speeds in both, it freezes frequently but then unfreezes, as if other processes (that are not writing to stdout) were getting way too much CPU time.

Has anyone else had such strange effects? Could it be resolved by, for example, implementing APIC timing? If there is any code that you need to see just ask, I am completely confused about what might be going on here. If you want to search through the code yourself, the link is in my sig.

Any hints or tips? Thank you.
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: scheduling extremely inefficient

Post by eryjus »

mariuszp wrote:Any hints or tips? Thank you.
What I am finding missing from your discussion is the quantum you are giving to each process. If you are executing a process switch on each PIT interrupt, the overhead associated with that is going to kill performance (stated from experience).

As an example, I have 5 set priorities that a process can be scheduled under: 30 for kernel processes, 20 for high priority process, 10 for a normal process, 5 for a low priority process, 1 for the idle process. These priority values are copied to the quantum for each process when it gets the CPU. Therefore, a normal process will be given 10 PIT "clicks" before it is preempted (unless it blocks for some reason).
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: scheduling extremely inefficient

Post by Brendan »

Hi,
mariuszp wrote:Any hints or tips? Thank you.
One of the first tests that should be done (for any/all schedulers) is to have a high priority interactive task (e.g. a simple shell) and a low priority CPU hog. When the user presses a key the OS should immediately pre-empt the low priority task and switch to the high priority task, and when the high priority task is finished (e.g. blocked waiting for the next key) the OS should switch back to the low priority task. A scheduler passes this test if it's impossible for the user to determine if the interactive task is the only task that's running (or if there are one or many CPU hogs running in the background) by looking at the interactive task's responsiveness/lag alone (without looking statistics from the scheduler or measuring CPU temperature or something).

If a scheduler fails this test, then it is faulty - either higher priority tasks aren't pre-empting lower priority tasks properly, or there's a design flaw (like, there's no task priorities in the OS design).

Also note that with only one CPU hog the timer does nothing - either the higher priority task runs (until it blocks), or the lower priority task runs (until its pre-empted). This is why it's one of the first tests that should be done (as it can be done before you even bother implementing the timer IRQ, etc).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: scheduling extremely inefficient

Post by alexfru »

It's not just the context switching overhead and priorities. One should also consider implementing apps (or libc) appropriately. Apps shouldn't be burning CPU cycles while doing nothing other than waiting for some event/input. If you run 4 apps of the same priority and all they do is sit in an infinite loop, there should be no surprise in that they will leave no more than 1/5th of CPU time to a 5th app. And it may be useful to favor the app that's interacting with the user (active window or receiving keyboard/mouse input).
rdos
Member
Member
Posts: 3297
Joined: Wed Oct 01, 2008 1:55 pm

Re: scheduling extremely inefficient

Post by rdos »

Brendan wrote: One of the first tests that should be done (for any/all schedulers) is to have a high priority interactive task (e.g. a simple shell) and a low priority CPU hog. When the user presses a key the OS should immediately pre-empt the low priority task and switch to the high priority task, and when the high priority task is finished (e.g. blocked waiting for the next key) the OS should switch back to the low priority task. A scheduler passes this test if it's impossible for the user to determine if the interactive task is the only task that's running (or if there are one or many CPU hogs running in the background) by looking at the interactive task's responsiveness/lag alone (without looking statistics from the scheduler or measuring CPU temperature or something).
If you design the scheduler with null threads, then this test is automatically done.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: scheduling extremely inefficient

Post by Combuster »

rdos wrote:If you design the scheduler with null threads, then this test is automatically done.
Proof wanted.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
rdos
Member
Member
Posts: 3297
Joined: Wed Oct 01, 2008 1:55 pm

Re: scheduling extremely inefficient

Post by rdos »

eryjus wrote:
mariuszp wrote:Any hints or tips? Thank you.
What I am finding missing from your discussion is the quantum you are giving to each process. If you are executing a process switch on each PIT interrupt, the overhead associated with that is going to kill performance (stated from experience).

As an example, I have 5 set priorities that a process can be scheduled under: 30 for kernel processes, 20 for high priority process, 10 for a normal process, 5 for a low priority process, 1 for the idle process. These priority values are copied to the quantum for each process when it gets the CPU. Therefore, a normal process will be given 10 PIT "clicks" before it is preempted (unless it blocks for some reason).
Yes, the quantum would be important, especially if the threads used doesn't block, but rather act as CPU hogs.

What is 10 PIT *clicks"? I use a time-quantum of 1ms, and it doesn't affect performance a lot on modern CPUs.
rdos
Member
Member
Posts: 3297
Joined: Wed Oct 01, 2008 1:55 pm

Re: scheduling extremely inefficient

Post by rdos »

Combuster wrote:
rdos wrote:If you design the scheduler with null threads, then this test is automatically done.
Proof wanted.
Why? If the null thread exists at the lowest priority, and runs as the "default" thread, then if your application that blocks works as it should then any other lower priority thread should work the same.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Scheduling extremely inefficient

Post by Combuster »

So the bottom line is, a null thread has nothing to do with it and you were posting nonsense as usual.

Also, merely having priorities is no guarantee that they get used in the correct fashion to make UI threads act appropriately, so even your new argument, while closer to the truth is bogus as well.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
rdos
Member
Member
Posts: 3297
Joined: Wed Oct 01, 2008 1:55 pm

Re: Scheduling extremely inefficient

Post by rdos »

Combuster wrote:So the bottom line is, a null thread has nothing to do with it and you were posting nonsense as usual.

Also, merely having priorities is no guarantee that they get used in the correct fashion to make UI threads act appropriately, so even your new argument, while closer to the truth is bogus as well.
1. In any sane design, the null thread has bottom priority and is only run when no higher priority (IOW, any other thread) is ready to rum

2. UI threads shouldn't have higher priority than anything else
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Scheduling extremely inefficient

Post by Combuster »

I think that's enough proof that you're unable to write an OS that passes Brendan's test.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Scheduling extremely inefficient

Post by bluemoon »

rdos wrote:2. UI threads shouldn't have higher priority than anything else
This is a design choice, whereas some people including Apple and Google may decide UI should be responsive (aka perspective performance) than doing actual works.
User avatar
Geri
Member
Member
Posts: 442
Joined: Sun Jul 14, 2013 6:01 pm

Re: Scheduling extremely inefficient

Post by Geri »

you should have linked an ISO, so we could debug it.
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Scheduling extremely inefficient

Post by mariuszp »

Geri wrote:you should have linked an ISO, so we could debug it.
https://github.com/madd-games/glidix/bl ... glidix.iso

Also, indeed, my scheduler does not support priorities, and the PIT is fixed at a frequency of 1000 Hz, even though some processes call yield() to give up their CPU time... but then the next process would get much less CPU time than normal... that does sound like a pretty bad design. And drivers like the IDE driver and the keyboard driver are just hanging there waiting for commands and still getting CPU time.

I'm gonna be adding APIC support and here's a question: if I set APIC to single-shot mode, and that "shot" happens when interrupts are disabled, is it missed or does it get sent as soon as I enable interrupts? What about if interrupts are disabled, the shot happens, then I program the APIC again, and then enable interrupts? (I'll need to do that for calls such as wait() which would need to trigger the scheduler to schedule another task).

(EDIT: I may also add that calling memcpy() while interrupts are disabled also causes the swap to screen to take a few seconds, is that a Bochs problem or could the memcpy() implementation I 'stole' from PDCLib be really bad and I should use "rep stosb" instead?)
User avatar
Geri
Member
Member
Posts: 442
Joined: Sun Jul 14, 2013 6:01 pm

Re: Scheduling extremely inefficient

Post by Geri »

is there a 32-bit version available?
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html
Post Reply