Virtual machines and time drift

VMWare: “Timekeeping in VMWare Virtual Machines

Because virtual machines work by time-sharing host physical hardware, they cannot exactly duplicate the timing activity of physical machines. VMware® virtual machines use several techniques to minimize and conceal differences in timing performance, but the differences can still sometimes cause timekeeping inaccuracies and other problems in software running in a virtual machine.

When this occurs, it’s called time drift. At work, I recently ran into a software bug that I eventually traced to time drift within my Vagrant-powered virtual machine; it was running almost 15 minutes slow. I do use VMWare for some projects, but for the one in question, I was using VirtualBox.

There is a simple fix for this sort of problem, but I needed something more robust. I did not want to install NTP because I have more VMs to fix than I do patience.

Some research led to this gem, which was posted by someone on the internet:

What VirtualBox does every 10 seconds is just slight adjustment (something like 0.005 seconds). Only when the time difference reaches a threshold (20 minutes by default) a “real” resync is done.

In other words, Virtualbox lets time drift by up to 20 minutes before it corrects it. Fabulous. 🙄

The solution I’m currently testing is to set the aforementioned resync threshold to 1 second with a one-liner in my Vagrantfile. Definitely a hack, but so far so good.

Maybe it’s just time to use real hardware for software development, again?