Sunday, May 30, 2010

Compressed RAM disk for Windows, The Virtual Way!

Recently, I developed Linux kernel driver which creates generic RAM based compressed block devices (called zram). Being RAM disks, they do not provide persistent storage but there are many use cases where persistence is not required: /tmp, various caches under /var, swap disks etc. These cases can benefit greatly from high speed RAM disks along with savings which compression brings!
However, all this seems to be completely Linux centric. But with virtualization, zram can be used for Windows too! The trick is a expose zram as a ‘raw disk’ to Windows running inside a Virtual Machine (VM). I will be using VirtualBox as example but exposing raw disks should be supported by other Virtualization solutions like VMware, KVM too.
Of course, you need to have Linux as the host and have the zram driver loaded. Here are the steps we need to do:
  • Get zram sources, compile and load the driver
  • Set zram disksize
  • Create VMDK file with raw disk set as /dev/zram0
  • Add this disk to Windows VM
Once this much is done, the disk will be detected in Windows as ‘VBox HardDisk’ and after disk initialization (as is needed for any new harddisk), you can format it with NTFS (or any other) filesystem.

Get zram sources, compile and load the driver

zram is not yet available as a downloadable tarball, so you need to checkout the source directly from repository:
hg clone https://compcache.googlecode.com/hg/ compcache
Now, to compile it against your running kernel, just run:
make
If you get lots of compilation errors, then you are probably missing kernel-devel and kernel-headers package. This driver has been well tested with Linux kernel 2.6.33-xx which ships with Fedora 13 (x86_64).If compilation went fine, you should now have the zram.ko driver. Load it along with its dependencies:
modprobe lzo_compress
modprobe lzo_decompress
insmod ./zram.ko

Set zram disk size

Disksize is set using zramconfig userspace utility which is compiled along with the driver. You can find it in <sourcedir>/sub-projects/zramconfig/zramconfig. Following sets disksize as 2GB and initializes the disk:
zramconfig /dev/zram0 –-disksize_kb=2097152 --init

Create VMDK file with raw disk set as /dev/zram0

Now we will create a VMDK file which simply points to /dev/zram0 device as its data source. This VMDK will later be added to Windows VM in VirtualBox.
VBoxManage internalcommands createrawvmdk -filename ~/temp/zram.vmdk -rawdisk /dev/zram0 –register
This command creates VMDK file in ~/temp (you can replace it with any other location) and also registers as one of the harddisks in VirtualBox.

Normal (non-root) users cannot do direct I/O to /dev/zram devices but you certainly don’t want to run VirtualBox as root! So, as a workaround, you can ‘chown’ the device:
chown username:username /dev/zram0
Of course, you need to run this as root. This gives you the ownership of the device, so you will not have to run VirtualBox as root.

Add this disk to Windows VM

This VMDK disk (~/temp/zram.vmdk in our example), can be added to any VM, be it Linux or Windows. But for now, we will stick with Windows. Go to VM’s storage configuration and add this disk. You will then get storage configuration like this:

zram_vbox_storage_config
Now poweron the VM. Windows will detect this disk as ‘VBox HardDisk’ and you need to ‘initialize’ the disk within Windows before you can start using it (as is needed for any new harddisk). To initialize the disk, goto: Control Panel –> Administrative Tools –> Computer Management –> Disk Management. Here you will automatically get a wizard to help to initialize the disk and assign it a drive letter. Make sure you set Block Size as 4096 and keep NTFS filesystem compression disabled (default) -- otherwise, you will get suboptimal performance!

After the disk initialization wizard finishes, zram disk should show up in My Computer:

zram_drive_my_comp
Above shows zram drive highlighted, formatted with NTFS filesystem and size of about 2GB. You can now use it as any other disk.

Apart from use as a generic disk, an interesting use case it to have Windows swap file on this disk. This way, whatever is swapped from Windows goes to host (Linux) where it is compressed and stored in memory itself! In a way this is like dynamically giving more memory to a VM. Reading/Writing to this disk is way faster than rotation disks, so it should also improve your VM performance.

To setup Windows to swap on zram disk, goto: Control Panel –> System –> Advanced –> Settings (Under Performance) –> Advanced –> Change (under Virtual Memory)

Now you should get screen like this, showing target disks for swap file:

zram_swap_windows

Select zram as swap target, select System managed size and click Set. You can also disable swap file in C: to make sure all swapping goes to zram only.

At any time, you can monitor zram I/O stats on (Linux) host using zramconfig. Following also shows sample output stats from my system:

[ngupta@vflare ~]$ zramconfig /dev/zram0 --stats
DiskSize:        2097152 kB
NumReads:        4214012
NumWrites:       3941172
FailedReads:           0
FailedWrites:          0
InvalidIO:             0
NotifyFree:            0
ZeroPages:        393297
GoodCompress:         98 %
NoCompress:            1 %
PagesStored:        3399
PagesUsed:           105
OrigDataSize:      13596 kB
ComprDataSize:       393 kB
MemUsedTotal:        420 kB

That’s it!
If you do try it out, do let me know! :)

9 comments:

  1. Cool! Have you done any performance benchmarking with zram?

    ReplyDelete
  2. No benchmarks are done (yet) for use of zram inside Windows guests. But there are some numbers when it is used natively on a Linux system:
    http://code.google.com/p/compcache/wiki/zramperf

    ReplyDelete
  3. Hi,

    Just one ask, how much memory your linux is using? And how much memory your Windows VM is using?

    Could you build a "very tiny" linux with your compcache included and share the image to us?

    ReplyDelete
  4. What happens when the host is rebooted?

    ReplyDelete
    Replies
    1. You will get data loss if you reboot the host without first detaching zram devices from any guests.

      Delete
  5. First of all, thanks! I am already using it on linux and the performance is unreal.
    So, do you have any plans to port it to windows in the future? . Perhaps there is something useful in imdisk/devio http://reboot.pro/forum/59-imdisk/ .
    I wouldn't mind doint some work myself, I know gnu C and some C# but I am inexperienced with kernel/drivers programming and I wouldn't know where to begin.

    ReplyDelete
  6. I am adept to porting it to windows too. I am looking for it a long time.

    ReplyDelete
  7. i stumble to your blog. I've try using zram, not as windows cache, but as whole vmdk file for C drive copied to zram. I tested it with fresh XP installation, and it was not that superb.
    The XP OS it self has the bottleneck, i test the boot time and application loading time, not much different. If you compare XP vmdk on zram and small Linux Distro that has ability to run from RAM, Linux is faster.
    I've done several research on zram / compcache, you can read it on my blog (indonesian language)
    http://betweenmeandlinux.wordpress.com/tag/zram/

    ReplyDelete
  8. Today exist high quality SSD disks, and I have been playing with caching (not compressed anyway), but, SSD become very cheap, and I bought many, 6 units, and mounted a RAID 0, each disk has roughly 500Mb/s of speed, so saying, it is almost 3Gb/s in RAID mode (it is near the same speed of RAM!), of data being exchanged with main RAM... then I used a software that find the most used files in the main drive [the main drive is a 4Tb HDD] and then cache it to the SSD array... but I think if it was possible to compress data, It would give the possibility to put more data in the SSD array to cache and load to main RAM... in other words, this project would still be interesting... :-D

    ReplyDelete