How to make the CPU "pause" ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pikasoo
Member
Member
Posts: 30
Joined: Sun Sep 02, 2012 11:04 am
Contact:

How to make the CPU "pause" ?

Post 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?
User avatar
Luis
Member
Member
Posts: 39
Joined: Sat Feb 16, 2013 11:19 am

Re: How to make the CPU "pause" ?

Post by Luis »

Not sure if it works but I would try a deeper sleep state.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How to make the CPU "pause" ?

Post 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%.
Attachments
Screen Shot 2013-05-06 at 1.36.17 AM.png
Screen Shot 2013-05-06 at 1.36.17 AM.png (7.27 KiB) Viewed 2820 times
pikasoo
Member
Member
Posts: 30
Joined: Sun Sep 02, 2012 11:04 am
Contact:

Re: How to make the CPU "pause" ?

Post 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.....
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How to make the CPU "pause" ?

Post 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.
pikasoo
Member
Member
Posts: 30
Joined: Sun Sep 02, 2012 11:04 am
Contact:

Re: How to make the CPU "pause" ?

Post 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!
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How to make the CPU "pause" ?

Post 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.
pikasoo
Member
Member
Posts: 30
Joined: Sun Sep 02, 2012 11:04 am
Contact:

Re: How to make the CPU "pause" ?

Post 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?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How to make the CPU "pause" ?

Post 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
pikasoo
Member
Member
Posts: 30
Joined: Sun Sep 02, 2012 11:04 am
Contact:

Re: How to make the CPU "pause" ?

Post 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!
Post Reply