I have the following program to time an operation (which happens to be rdpmc). The program is run on x86_64 VM, in a resource group of two Linux VMs, the other one is always hungry for processing power.
When the program is run in the idle Linux VM (unlimited CPU), two output values are always the same (ie. elapsed real time and apparent one are the same). Which is understandable, two VMs use different CPU cores.
In unlimited mode, the resource group takes ~5 GHz. Now I limit CPU capacity of the group down to 1 GHz. This time two values increase, but still the same. I think when CPU is shared, real timer counter must go must faster than apparent counter, because one VM may sleep when the other one runs, not at the same speed. Am I missing something?
#include <stdio.h>
#include <stdint.h>
inline uint64_t counter(int c)
{
uint32_t low, high;
__asm__ __volatile__("rdpmc" : "=a" (low), "=d" (high) : "c" (c));
return (uint64_t)high << 32 | (uint64_t)low;
}
int main(int argc, char** argv)
{
int i;
uint64_t t1, t2, t3, t4;
while (1)
{
t1 = counter(0x10001);
t2 = counter(0x10002);
for (i = 0; i < 1000000; i++)
t3 = counter(0x10002);
t4 = counter(0x10001);
printf("%lu %lu\n", (t4 - t1)/1000000, (t3 - t2)/1000000);
}
return 0;
}