[vbox-dev] About Virtual Box VM Scheduling mechanism
Knut St. Osmundsen
bird at sun.com
Mon Jul 5 02:53:46 PDT 2010
Suk Kyu Lee wrote:
> I'm sorry if you received this email twice. Somehow the email was
> rejected. Thank you.
> Here is the original context of the email:
> Thank you very much for your reply.
> Maybe I have misunderstood the context of the email. But I was
> wondering within the VMM, if there is any scheduling mechanism that
> might coordinate the work of each EMT that belongs to the VM. For
> example, EMT1 controls the CPU and while EMT1 is working, EMT2 is
> sleeping and etc.
> Maybe my knowledge is not sufficient, but what do you mean by the
> host OS schedules the EMTs? Is there any mechanism that coordinates
> the EMTs within the host machine?
> Likewise, within the Xen, I found out that there is a "Credit"
> Scheduler where it coordinates the CPU/IO resource amongst the VMs
> within the Xen VMM.
> Because when I was looking over the VirtualBox code, I couldn't find
> the code that coordinates the EMT threads. I even disabled the
> HALT/Yield function within the VMM.cpp but it looks like there hasn't
> been any effect with/without these functions.
I'm not quite sure if I understand what you're searching after, but if
it's a thread scheduler you won't find it. VirtualBox is, as Christian
already pointed out, just an application and it is the host Operative
System's job to schedule its threads.
As you've probably figured out already, each EMT (EMulation Thread)
corresponds to exactly one virtual guest CPU and the main task of an EMT
is to execute guest code as that particular CPU.
As part of the CPU virtualization, the EMTs will handle virtualization
exceptions/exits and inject interrupts/exceptions/traps/faults. These
tasks are mostly handled in the inner loops in HWVMXR0.cpp and
HWSVMR0.cpp, and in the trap handlers in TRPMGC.cpp (raw-mode only).
The EMTs have some other tasks which is performed in the work loops
found in EM.cpp, EMRaw.cpp and EMHwaccm.cpp. These include running the
timers (TM.cpp), work deferred from ring-0/raw-mode context, VM state
changes, and more.
The VMR3WaitHalted function is used by an EMT when the guest executes a
HLT instruction. All this does is to block the thread on some
synchronization object provided by the host OS. By default this is a
kernel object, so it involves an ioctl() into VMMR0/GVMMR0.cpp. The EMT
will continue executing guest code when an emulated device raises an
interrupt (usually via a timer or async I/O thread). Timers are
processed regularly while in the HLT state. When async I/O threads
raises interrupts, they will call VMR3NotifyCpuFFU/VMR3NotifyGlobalFFU
and wake up the targeted EMTs, and the EMTs will inject the interrupts
when scheduled by the host OS scheduler.
Hope this was helpful.
More information about the vbox-dev