Page 1 of 1

How to make the CPU "pause" ?

Posted: Sun May 05, 2013 8:52 am
by pikasoo
I would like to know how do linux and windows os are able to drop the cpu use in Virtual Machine....

i use Microsoft virtual pc and when i load a os like windows xp, once it booted to the desktop the CPU use goes to 0% on the VM process. then it may go up and drop again depending of the need on the virtualised computer.

i would like to be able to do the same on my os, i tested a few thing and the cpu use always stay at 25%(yes i have a quad core pc)

thing i tested:

go to the vm bios = 25%

boot my os = 25%

fake boot with a simple asm code looking like this:

cli
hlt <---------- this was to stop the cpu and force it to stay like that

result : 25%


so if the asm opcode hlt doesnt realy pause the cpu (at least on VM) what do i need to do to lower the cpu use of the VM?

Re: How to make the CPU "pause" ?

Posted: Sun May 05, 2013 8:58 am
by Luis
Not sure if it works but I would try a deeper sleep state.

Re: How to make the CPU "pause" ?

Posted: Sun May 05, 2013 11:40 am
by bluemoon
a loop of HLT (without CLI) works, and it's how Windows 2000 do. Modern windows also adjust cpu frequency and power states.

For my OS, with an idle thread running HLT loop:
With 20Hz PIT (for testing purpose) uses 0~1% CPU usage.
With 1000Hz PIT, the CPU goes to 15%.

Re: How to make the CPU "pause" ?

Posted: Sun May 05, 2013 2:10 pm
by pikasoo
thx for the reply,

i tested the loop of hlt and it work on boot but not in the os...

im testing to see what make the cpu raise. so far it happen in the early stage of the booting... im not sure if it's an interupt or simply setting a vesa mode...

will find out soon or later.....

Re: How to make the CPU "pause" ?

Posted: Sun May 05, 2013 10:42 pm
by bluemoon
pikasoo wrote:im not sure if it's an interupt or simply setting a vesa mode...
If you're setting VESA mode by going to real mode can call BIOS, the BIOS won't do HLT for you and it probably just busy loop before return.
It was not a culture to do HLT back in the 80s, when most of the BIOS (or VESA BIOS) are developed.

Re: How to make the CPU "pause" ?

Posted: Mon May 06, 2013 8:52 am
by pikasoo
If you're setting VESA mode by going to real mode can call BIOS, the BIOS won't do HLT for you and it probably just busy loop before return.
actualy i dont mind if the code goes to 100% cpu for a bit, i am using v86 to change the resolution and gatter the needed information if it has vesa 2 or 3 and the pmode entry point for the bank switch but i finaly found out what keep my VM busy...

With 1000Hz PIT and int 0 masked (so just firering it and going back to the hlt loop i get 100% cpu
With 900Hz PIT i get 0% use with a hlt loop and 1 - 15% with os running

so my code is probably fine for a real pc but in a VM with my computer 900 is probably the limit (950 = 50% but with the os i get 100%)

now i need to manage the task priority and sleep time more efficiently...

thx for the tips and info!

Re: How to make the CPU "pause" ?

Posted: Mon May 06, 2013 12:21 pm
by bluemoon
pikasoo wrote:so my code is probably fine for a real pc but in a VM with my computer 900 is probably the limit
Not a perfect solution. There is still overhead on the 900 IRQ per seconds - and that's just what magnifies on VM/emulator.
A better solution is to use APIC if available, and adjust one-shot timer instead of fixed period.

Re: How to make the CPU "pause" ?

Posted: Mon May 06, 2013 8:23 pm
by pikasoo
if i understand it correctly, the idea is to avoid useless interupt by setting the PIC to trigger only when i need to switch task.

you said that i need the APIC for that but in the 8253 Channel Modes you have this:
Mode 0: Interrupt on Terminal Count

this should do the same right?

Re: How to make the CPU "pause" ?

Posted: Tue May 07, 2013 2:55 am
by bluemoon
Yes the logic is same but setting up PIT can cause huge overhead due to bus activity and the controller's slow response itself, compared to APIC timer

Re: How to make the CPU "pause" ?

Posted: Tue May 07, 2013 5:46 am
by pikasoo
Yes the logic is same but setting up PIT can cause huge overhead due to bus activity and the controller's slow response itself, compared to APIC timer
ok, thanks for the information and the tips!