Reliability of clock_gettime for thread CPU time inside VMs?

41 Views Asked by At

I have a C program where I need to measure CPU time on a per-thread basis.

The basic mechanism I am planning to use is

clock_gettime(CLOCK_THREAD_CPUTIME_ID,...)

from https://linux.die.net/man/3/clock_gettime .

But I haven't found clear statements about whether that's reliable in case my app runs inside a VM. Consider the following scenario.

The app's VM has a physical CPU. A thread in the app gets the vCPU (of the VM) backed by the physiscal CPU for 10 secs, it's the first time the thread runs. Then the VM looses the physical CPU for 10 s. Then, it acquires it again. If, upon re-acquisition of the physical CPU from the VM, the thread still had the vCPU and called

clock_gettime(CLOCK_THREAD_CPUTIME_ID,...)

would the thread see that it had the CPU for 10secs (meaning that the clock didn't keep counting while the VM didn't have the physical CPU) , or 20secs (meaning that the clock kept counting while the VM didn't have the physical CPU)?

Note: I know the answer might depend on the specific hypervisor and version, but I'm trying to understand if there's regular behavior or not.

The guest OS is ubuntu 18.04.

0

There are 0 best solutions below