how does KVM virtualize performance counter (PMC)? - virtual-machine

I am using KVM-QEMU in Intel platform.
And I am wondering how is the PMC/PMU being virtualized in KVM?
My understanding of vPMC is as follows.
Since PMU is a shared physical resource, so hypervisor shall context save/restore PMC configurations and counters during VMExit/VMEnter.
But in my testing, I got confused.
(In my host Linux, the MSR of IA32_PERF_FIXed_CTRL is set to 0xb0)
First of all, in a launched VM,
I 'wrmsr IA32_PERF_FIXed_CTRL 0x0b', to enable FIXed_CTR0 in VM.
Then, I logged the MSR of IA32_PERF_FIXed_CTRL at VMExit, and found FIXed_CTR0 is not enabled.
I think it is because when VMExit happens, it is already in the host context, so the MSR of IA32_PERF_FIXed_CTRL is the one of host, not the host.
But the question is where is that MSR for the VM being saved in host, and when and where the VM's MSR is being restored to CPU during VMEnter???
Hope my question is clear.
Thanks,

KVM implements a virtual PMU that utilizes Linux's perf subsystem for core PMU operations. It does not expose the host PMU directly to the guest.
When you do wrmsr IA32_PERF_FIXed_CTRL 0x0b, you are not writing to the hardware MSR (Model-specific register), KVM traps the MSR write and makes an update in its representation of the virtual PMU. Look for kvm_set_msr_common() in x86.c, this will ultimately call intel_pmu_set_msr() in pmu_intel.c that does the real magic. Also look at struct kvm_pmu() which is KVM's representation of the PMU.
The MSR doesn't need to be restored like typical VMCS fields because KVM saves guest specific PMU settings in struct kvm_pmu() per vCPU (struct kvm_vcpu).

Related

How to memory map address space on host from KVM/QEMU guest

I have an embedded application running on a Xilinx ZynqMp SoC. The application running on the PS (processor) memory maps the PL (FPGA) of the SoC over an AXI bus via /dev/mem at some base physical address.
I would like to run this application in a KVM/QEMU VM running on the PS. This means I will need to somehow expose that memory window available via /dev/mem on the host to the guest VM.
Through some research I thought that virtio-mmio would be the method to do this. I made some attempts using virtio-mmio but hit a wall, so I asked a question: Memory map address space on host from KVM/QEMU guest using virtio-mmio
The response seems to indicate that virtio-mmio is not the method I should be using for this.
If that is the case, what is the method used for exposing a memory space available on the host to a guest VM? I do not need any sort of device driver/layer on top of this. I just need raw memory access.

Openstack Compute node performance

Am starting to learn openstack. As per my understanding (after reading all the docs) is that the compute nodes run a host OS (ubuntu or other linux) and on top of that you have your hypervisor (like KVM) and then the VMs run on top of it i.e HW -> OS -> Hypervisor -> VMs . This is similar to having a VM running on Virtualbox which runs on a host operating system i.e HW-> Host OS ->VBox -> VMs.Please correct me if my understanding is incorrect.
Assuming my first understanding is correct, How will the performance of the VMs on the this architecture be compared to running the VMs directly on hypervisor i.e HW-> Hypervisor (KVM)->VMs ?
Comparing this with VMWare openstack architecture where Nova speaking to VMWare vCenter and then vCenter manages the ESXi nodes (vCenter and ESXi are on different nodes). This way my VMs are directly running on top of hypervisor connected to HW (HW->ESXi->VMs).And all the overlay networking is handled by NSX. This looks much more performant compared to the other architecture. Am i missing something here ?
Thanks in advance.
~exp8
Since kvm runs on linux kernel, and runs instructions directly on the cpu, it is the hypervisor (HW -> HyperVisor -> VM). On the VMware side, there is a tiny proprietary tuned version of linux as hypervisor.
To find out which one is more efficient, you should do benchmarking. But if you think Vmware's linux consumes less resources (less process, memory, cpu), it may be better.

Difference between virtual machine process and host os process?

Suppose in my pc I have Ubuntu as Host OS. Now I installed a Virtual Machine say VirtualBox (hypervisor) and then deployed a centos and a redhat os inside that as guest OS.
Suppose CentOS and redhat has 2 processes running and Ubuntu is running 3 processes. So following are my questions:
There are how many processes that Ubuntu is having?
Is there any difference between GuestOS and HostOS processes?
If all guestos runs as a process then they will get less time as compared to other process running on host os.
Please clear my doubts here.
Thank you.
Well let me clear your doubts,
First of all there aren't any specific number of process for an OS, its called as cores or threads, technically you can define how many cores or threads you want to use on your virtual machine and it depends on the system configuration you use.
Secondly Guest OS is what you have created in the virtual machine and host is what your laptop or pc actually run. Host OS uses the actual hardware for the working whereas the Guest OS uses the virtual hardware like number of cores and type and size of hard drive defined by the user while adding a virtual machine.
Third, as I mentioned earlier Guest and Host OS works on the configurations used by you, if you user higher amount of cores/ threads in setting your virtual machine the Guest OS will get higher speed.
Ideally the virtual machines are used to test and create some functionality of the Operating Systems without affecting the internal OS, so you can think of it as a your parents house where you can live and grow but at the end you cannot go away from the fact that their contribution is more and so you cannot go beyond their features without leaving it and making your own home.
Linux operating systems are multi-threaded operating system. The host OS would consider virtual box as a thread. You can define number of cores and virtual hard disk size for guest OS by using virtual box.
Since virtual box runs in separate thread and other operations of host OS runs in separate threads, there would be less effect on speed of processing. But I've observed big variances in processing speed in systems which have low memory. Each and every thread needs specific allocation of memory for its smooth operation. So systems having more than 2 GB RAM managed virtual box very well.

Difference between "process virtual machine" with "system virtual machine"

What's the difference between process virtual machine with system virtual machine?
My guess is that process VM is not providing a kind of an operating system for the whole application for that OS, rather providing an environment for some specific application.
And system VM is providing an environment for an OS to be installed just like VirtualBox.
Am I getting it correct?
Another question is the difference between the two different implementation of system VM: hosted vs. stand-alone.
I'm a beginner studying OS, so easy and understandable answer would be greatly appreciated :)
A Process virtual machine, sometimes called an application virtual machine, runs as a normal application inside a host OS and supports a single process. It is created when that process is started and destroyed when it exits. Its purpose is to provide a platform-independent programming environment that abstracts away details of the underlying hardware or operating system, and allows a program to execute in the same way on any platform.
A System virtual machine provides a complete system platform which supports the execution of a complete operating system (OS),Just like you said VirtualBox is one example.
A Host virtual machine is the server component of a virtual machine , which provides computing resources in the underlying hardware to support guest virtual machine (guest VM).
The following is from http://airccse.org/journal/jcsit/5113ijcsit11.pdf :
System Virtual Machines
A System Virtual Machine gives a complete virtual hardware platform with support for execution
of a complete operating system (OS).
The advantage of using System VM are:
Multiple Operating System environments can run in parallel on the same piece of
hardware in strong isolation from each other.
The VM can provide an instruction set architecture (ISA) that is slightly different from
that of the real machine
The main draw backs are:
Since the VM indirectly accesses the same hardware the efficiency is compromised.
Multiply VMs running in parallel on the same physical machine may result in varied
performance depending on the workload imposed on the system. Implementing proper
isolation techniques may address this drawback.

How KVM handle physical interrupt?

i am working on KVM optimization for VMs' IO. I have read the KVM codes, usually all the physical interrupt will cause the VMexit and enter into KVM. Then the host's IDT will handle the corresponding physical interrupt. My question is that how KVM decide whether to inject a virtual interrupt into the guest or not? and under what situation it will inject a virtual interrupt to the guest?
Thanks
In the Documentation of kvm this is what is told about when the virtual interupt can be injected . Heres the link http://os1a.cs.columbia.edu/lxr/source/Documentation/kvm/api.txt
look at line number 905.
The struct kvm_run structure i think gives control to the application on how it makes the VM
behave.Use cscope and search for the string request_interrupt_window in the source code, You will understand how the kvm see when to enter the guest for injecting an interupt.Also go through the api.txt file it is very helpful.
Cheers
EDITED
Here's, one example of the host injecting interupts into the guest.
Assume that there was a page fault in the GUEST VM
this causes a VMEXIT
Hypervisor/KVM handles the VMEXIT
Its sees the reason for VMEXIT through VMCS control structure and find that there was page fault.
The host/KVM is responsible for memory virtualization, so it check to see if the page fault was caused
because the page was not allocated to the GUEST in which case it calls alloc_page in the HOST kernel and does a VMENTRY to resume GUEST execution.
Or the mapping was removed by the GUEST OS, in this case the KVM uses a VMCS control structure as a communication medium to inject a virtual interupt no 14 which causes the GUEST kernel to handle page fault.
This is one example of the host inserting virtual interupt. Ofcourse there are plenty of other ways/reasons to do so.
You can infact configure the VMCS to make the guest do a VMEXIT after executing EVERY INSTRUCTION this can be done using the MONITOR TRAP FLAG.
I guess you refer to assigned device interrupts (and not emulated interrupts or virt-IO interrupts which are not directly forwarded from the physical device to the guest).
For each irq of the assigned device, request_threaded_irq is called and registers kvm_assigned_dev_thread to be called upon every interrupt. As you can see kvm_set_irq is then called, and as described the only coalescing that takes place if the interrupt is masked. In x86 interrupts can be masked by rflags.if, mov-SS, due to TPR that does not allow the interrupt to be delivered or due to interrupt in service with higher priority. KVM is bound to follow the architecture definition in order not to surprise the guest.