[patch] Auto-setting hz to 100 inside QEMU/VMWare

Mike Silbersack silby at silby.com
Fri Dec 28 00:03:57 PST 2007


When running FreeBSD inside QEMU / VMWare, one of my pet peeves is that 
time is completely inaccurate.  This seems to be cured by setting 
kern.hz=100 (down from the default of 1000) in /boot/loader.conf, but I'm 
getting sick of doing that every time I set up a virtual box.

So, here's a patch to have the kernel auto-detect that it's running inside 
one of those two environments and automatically make that adjustment.

If you're running FreeBSD inside any other virtual environments 
(Parallels?  Microsoft Virtual PC?) and can detect them via data in kenv, 
I'll be happy to add them to the patch as well.

I have only tested this patch in QEMU so far, I will test inside VMWare 
tomorrow.

If anyone could review and/or test this, it would be appreciated.

Thanks,

Mike "Silby" Silbersack
-------------- next part --------------
--- /usr/src/sys.old/kern/subr_param.c	2007-12-16 01:37:33.000000000 -0600
+++ kern/subr_param.c	2007-12-28 02:51:28.000000000 -0600
@@ -108,8 +108,29 @@
 void
 init_param1(void)
 {
+	char *envptr;
 
 	hz = HZ;
+	/* Neither QEMU or VMWare can keep up with guests running at
+	 * 1000 hz.  This leads to highly inaccurate timekeeping by
+	 * the guest.  To work around this, force 100 hz when we
+	 * detect that we are a guest of one of these two.
+	 */
+	envptr = getenv("hint.acpi.0.oem");
+	if (envptr) {
+		if (memcmp(envptr, "QEMU", 4) == 0)
+			hz = 100;
+		freeenv(envptr);
+		envptr = NULL;
+	}
+	envptr = getenv("smbios.system.maker");
+	if (envptr) {
+		if (memcmp(envptr, "VMware, Inc.", 12) == 0)
+			hz = 100;
+		freeenv(envptr);
+		envptr = NULL;
+	}
+		
 	TUNABLE_INT_FETCH("kern.hz", &hz);
 	tick = 1000000 / hz;
 


More information about the freebsd-current mailing list