Wednesday, October 22, 2014

The Paravirtualization Spectrum

As OS virtualization techniques evolved from Full Virtualization to Hardware assisted and then to Para Virtualized modes, all of these norms seems quite confusing and difficult to understand. As a beginner, I've searched a bit on this topic, and found a good graphical representation that describes all these terms, much more intuitively. Please note that, all terms are evolved based on Xen Hypervisor. We can also see, how KVM (Kernel Based Virtual Machine) fits in to this.

pv-spectrum-grid

Full Virtualization (FV)


What is virtualized?
Everything.

Everything from BIOS, motherboard, disk, network, CPU Privileged instructions etc.

Performance?
Least.

As you can see, every hardware elements will be virtualized. This is the least performing mode. It requires frequent context switches.

Windows Guest Support?
Yes.

As it does not requires Kernel recompilation.

Hypervisors?
Xen and KVM.

*Hardware Virtualization (HVM) - Hardware Extensions to Full Virtualization


What is virtualized?
Everything, except CPU privileged instructions.

Fully virtualized, but instead of virtualizing privileged instructions, they can be directly run with physical CPU, using Intel-VT/AMD-V extensions.


i.e CPU's with these extensions, provides 3 rings (instead of 2), where 'Ring -1' will be used by Hypervisor, So guest can directly execute privileged instructions in Ring0 (Supervisor Mode)

Performance?
Better than Full Virtualization.

As running privileged instructions can be run directly under physical CPU, no emulation or context switch required and hence the performance improvement.

Windows Guest Support?
Yes.

As it does not requires Kernel recompilation.

Hypervisors?
Xen and KVM.


This one is not included in the above figure, as it is most often used with para virtualized drivers (network/disk), for high performance.
 

Full Virtualization with Paravirtualized Drivers(PV on HVM)


What is virtualized?
Everything, except CPU privileged instructions + Disk + Network.

Para virtualized implementations have been provided for Disk and Network, and supporting drivers have made available to guests.

Performance?
Better than HVM and Full Virtualization.

As additionally disk and network interfaces has been paravirtualized, this modes provides a huge performance improvement, as most of the time disk/network performances are the major bottle necks.

Windows Guest Support?
Yes.

As it does not requires Kernel recompilation. Paravirtualized network and disk been supported by drivers specifically developed for windows guests.

Hypervisors?
Xen and KVM
 
 

Para virtualized Interrupt Controllers with HVM (PVHVM)


What is virtualized?
Everything, except CPU privileged instructions + Disk + Network + APIC/IOAPIC.

Interrupt controllers and CPU timers have been paravirtualized here.

Performance?
Better than PV on HVM, HVM, Full Virtualization.


In addition to paravirtualized disk and network, it also paravirtualizes interrupts and timers.

Windows Guest Support?
No.

To support paravirtualzed APIC and IOAPIC, kernel should be recompiled and only linux based guest's are supported in this mode with kernel recompilations.

Hypervisors?
Xen only.
 
 

Almost Paravirtualized (PVH)


What is virtualized?
Only memory/pagetables have been virtualized.

There are no emulated devices of any kind, like no Motherboard, PCI and BIOS or legacy boot.

Performance?
Far better than PVHVM, PV on HVM, HVM, Full Virtualization.
As virtually everything except memory access have been paravirtualized.

Windows Guest Support?
No
.
This extensive paravirtualization support requires kernel recompilations.

Hypervisors?
Xen only.
 
 

Fully Paravirtualized (PV)


What is virtualized?
None. Everything paravirtualized.


Performance?
Highest performance.

As virtually everything have been paravirtualized.

Windows Guest Support?
No.

This extensive paravirtualization support requires kernel recompilations.

Hypervisors?
Xen only.
 
 

Conclusion


Now I know why even there is a comparison between Xen and KVM, though Xen have advanced high performing modes like PVM and PVHVM. Those modes can only run modified Linux Distros not windows guests.

Most often comparison will happen for windows guests, that will only supported in Full Virtualization, HVM and PV on HVM modes, that are common to both KVM and Xen.

So KVM may be performing equal or slightly better than, PV on HVM mode.

Otherwise Xen will simply outperforms KVM, as it having more advanced modes like PVM.

Original post
here

Review


I request the readers to put any suggestions , corrections, if found as comments, so that I can update this article to be as accurate as possible.

Lambda Expressions - A Comparison between C# and Java

When we came to know about ‘Lambda Expression’ feature in C#, I really became obsessive with it. Using this feature, you can represent an anonymous function in a very crisp and compact way.

C#: "A lambda expression is an anonymous function that you can use to create delegates (Or simply Type safe function references)"

We've explored this feature in C# first, and lately in Java as well. From early on, C# (.NET 3.5) supports delegates and anonymous functions, So the 'Lambda Expression' feature addition, seems quite natural and intuitive in the C# language.

It seems like Java inspired by this enhancement in C#, and introduced their own version of 'Lambda Expression' in latest Java Language (From Java8 onwards). But the problem, is they have to incorporate this to an existing java specification, that revolves around only Class and interfaces. In java there is no concept of 'Delegates Or Function references', hence 'Lambda Expression' feature addition seems less intuitive in terms of implementation.

So
in C#, the lambda expression revolves around 'Delegates' with anonymous functions and in java it is implemented through 'interfaces - that contains a single function prototype' with anonymous classes


Lets see an example below to have a comparison between the two.

Build Your Lambda in Steps

C#
(Delegate/Anonymous function based)
Java
(Interface/Anonymous class based)

1. conceive your concrete function first

Lets take a simple function, that validates an even number. It returns 'true' for an even number and 'false' for an odd number. Let's say this is our concrete function, which we will convert to 'lambda expression' through subsequent steps.
 
C#:
bool IsEven(int number)
{   
    return number % 2 == 0;
}

C# supports functions inside any class. No interface implementation required.

Java:
class EvenValidator implements Proto
{   
    bool IsEven(int number)
    {       
        return number % 2 == 0;   
    }
}

Java requires the function to be enclosed inside a class, that implements the interface prototype 

2. Define a Function Prototype to your concrete function 

C#:
delegate bool Proto(int number);

C# requires a delegate, A type safe function reference. No interface implementations!

Java:
interface Proto
{   
    bool IsEven(int number);
}

Java requires the function prototype to be wrapped inside an interface. Extra overhead!

3. Assign concrete function to Function Prototype 

C#:
Proto objDlgte = new Proto(IsEven);

Clean and natural way of wrapping functions.

Java:
Proto objDlgte = new EvenValidator();

Java does this at the old way. 


4. Anonymous Declaration

As we're referring the function (IsEven) method body, only inside the Function Reference
and nowhere else, why don't we get rid of the function definition altogether, and inline the method body along with the Function reference.

This is exactly an anonymous function or class does. 
C#:
Proto objDlgte = (Proto) delegate(int number)
{
    return number % 2 == 0;
}

C# using Anonymous function.

Java:
Proto objDlgte = new Proto()
{   
    bool IsEven(int number)   
    {       
        return number % 2 == 0;   
    }
}

Java using anonymous class. 


5. Finally arrive at Lambda Expression 

C#:
Proto objDlgte = number => number % 2 == 0;  

Java:
Proto objDlgte = number -> number % 2 == 0;  


6. Usage/Inovke 

C#:
//true
bool isEven = objDlgte(20);

Seems very natural way for invoking a predicate, as a function call.

Java:
//false
bool isEven = objDlgte.IsEven(35);

Less intuitive, as you've to mention both the interface and the method to invoke it. 

Advanced Features - C# (Advantage)  Java (Limitations)

There are some feature limitations to Java, compared to C#. Explained below.

Capturing Variables 
C#:
C# allows us to capture variables inside lambda expressions, defined in the parent scope.
Very convenient.

Java: Java only allows to capture final or effectively final variables inside the lambda expressions, defined in the parent scope.Very Restricting.

Example

C#:
void TestFunction()
{
    int outNumber = 5;
    int evenDivider = 2;
    Proto objDlgte = number =>
    {
       return outNumber % evenDivider == 0;
    };
}

C# support capturing almost all type of variables from outer scope.

Java:
void TestFunction()
{
    final int outNumber = 5;
    final int evenDivider = 2;
    Proto objDlgte = number ->
    {
        return outNumber % evenDivider == 0;
    };
}

You can only refer final/effectively final outer variables inside lambda.So you need to declare the variables as final. A major drawback. 

Conclusion:

Lambda expression is a nice feature addition to high level languages like C# and java. C# lambda expression built with delegate and anonymous functions. Java lately added this feature, inspired from C#. Java's implementation seems not as clean as the way C# does it and having limitations while accessing variables from the declaring scope.

Sunday, October 5, 2014

Para virtualizing KVM - An Ultimate Virtualization Experience from your Desktop/Laptop

Recently, We’d gone for some research, on how to improve virtualization performance with KVM. As a result, we made KVM as our default choice for Virtualization due to it’s better performance and stability.

My friends talked about the performance issues with KVM, while working with windows guests. Especially the graphics performance. Mouse movement is painfully slow and irregular. Also the network and disk performances are not any better.

Upon analysis, its concluded that by default KVM (Or any other Hypervisor for that reason), uses ‘Full Virtualization’, to run the guests. In such a scenario, Guest OS’s run as unmodified (Full Virtualization), where they are not aware about their underneath virtual environment. They simply assume, as they are running under a normal hardware platform. Such a ‘Fully Virtualized’ guest, demands more hardware resources and memory from the Hypervisor as they are not fine tuned for the virtualized environment, simply because unmodified guest OSs have been built with native hardware in mind.

KMV also supports ‘Para Virtualization’. ‘Para Virtualized’ guests are very well aware of their virtualized environment and adjust themselves accordingly. They put a less demand on resources from their hosted hypervisor. In other words, guest OS know it is running under a virtualized environment and cooperate with Host, to relinquish resources and memory to  more demanding guests.

The two scenarios have been detailed in the below figure.

image

‘Para Virtualization’ techniques, make use of ‘device drivers’ (Para Virtualized Drivers – PVDs) at two levels. One set will be installed inside ‘Guests’ and other will be used inside the Host OS/Hypervisor. These two sets of ‘device drivers’ cooperate each other, and work cohesively as a single unit, to provide the optimizations.

To make a Windows guest, Para virtualized, drivers are available for respective Hypervisor. Once installed your windows guest are become virtualization aware. For KVM these are called ‘VirtIO’ drivers.

When applied in our environments, the performance gain is unbelievable. It is running with a ‘Near Native’ performance, something similar to Xen or VMWare and outperforms other Type2 hypervisors like Virtual Box. We are presenting that as a case study in the below sections.

A Case Study:

Physical Host Specs:

CPU: Intel Pentium Dual Core CPU (2.6 GHZ, 2.6  GHZ)

               Intel-VT/AMD-V has been enabled in BIOS, to get the Hardware assisted virtualization support.

RAM: 4GB

HDD: (2 hard disks)

               SeaGate 250 GB (For Host OS installation, Swap space)

               Western Digital 1TB (To save guest disk images)

Host OS: Lubuntu 14.04

               Boot Time: 13 Seconds

               Startup Memory Usage: 175MB

               Shutdown Time: 4 Seconds

               Desktop: LXDE

One thing to note here that, your Host OS should be thinner as possible. We’ve chosen the best balance between ease of use and lightweightness, by selecting ‘Lubuntu14.04’ as the Host OS. Lubuntu is the lighter version of Ubuntu and one of the fastest Linux distribution.

Also it is better to have a secondary hard disk to keep your guest disk images, as Host OS does not have to compete with Guest OS’s while seeking data from hard disks. This gives the highest performance and long life to your drives, as it minimizes Read/Write head movements.

1. Install KVM and Management Tools

“sudo apt-get install qemu-kvm libvirt-bin python-spice-client-gtk virt-manager bridge-utils”

Add the current user to the ‘libvirtd’ group.

“sudo adduser `id -un` libvirtd”

2. Enable Nested Virtualization In KVM

echo 'options kvm_intel nested=1' >> /etc/modprobe.d/qemu-system-x86.conf

Download Para Virtualized Drivers for Windows:

Download SPICE Guest Tools here.

Purpose: Support Seamless integration with Host desktop, QXL GPU Para Virtualized drivers and guest tools.

Download Virtio Drivers (As ISO) here.

Purpose: Provides Para Virtualized Drivers for

Network Adapters (VirtIO Ethernet Adapter)

Block Storage         (VirtIO SCSI Driver)

PCI  Bus                    (VirtIO Serial)

RAM                          (VirtIO Memory Balloon Driver)

Create a Para Virtualized Windows Guest:

Open ‘Virt-Manager’ and start creating a windows guest.

Add ‘Nested Virtualization’ support.

image

Use ‘Virtio’ as Diskbus.

image

Use ‘Virtio’ as Network Adapter Device Model.

image

Use ‘Spice’ as Display Device and QXL as GPU.

image

Use PCI and Virtio Serial Bus.

image

To get the ‘VirtIO’ stuff work, you need to install Para Virtualized Drivers to the Windows Guest Machine. To accomplish that, first install the ‘SPICE Guest Tools’ EXE (You’ve downloaded earlier) inside the windows guest.

Once done, install other ‘VirtIO’ drivers, from the VirtIO ISO file (Downloaded earlier) for other devices (Network, Storage etc). Once completed you can open the ‘Device Manager’ to see the Para Virtualized Device Drivers (VirtIO) are in action. See the below figure.

image

When comes to performance, it truly runs with ‘Near Native’ speed. Opening applications like ‘Visual Studio' 2012’, Office 2007 are blazing fast. The folder/file browsing is so quick that, they will get opened as soon as you release your finger from the left mouse button. VirtualBox had took a noticeable amount of delay while performing the same operations. Also as number of guest increases, KVM simply outperform Virtualbox in terms of stability, speed and demand on hardware resources.

Especially you can run more number of guests with KVM, whose combined Virtual RAM requirement is more than the actual installed physical RAM, through a technique called ‘Memory Ballooning/Memory Overcommitting’. I’m planning to provide that idea in a separate article.