0

I want to use a simple program to measure the number of cycles required by VMFUNC instruction in x86 architecture on a Linux Ubuntu 18.04 machine. Is there any fast and accurate method? Thanks!

hsyhhh
  • 11
  • 3
  • Timing it in a loop would let you use `perf stat`. If you want to measure a single instance, `rdtsc` ; `vmfunc` ; `lfence` ; `rdtsc` may work well, for a known core clock frequency so you can adjust for core clocks vs. reference cycles. And control for the timing overhead of rdtsc, like subtract the time for an empty timed interval on your CPU including `lfence`. (`rdtscp` instead of `lfence`+`rdtsc` at the bottom of the timed region can also work.) – Peter Cordes Jun 18 '22 at 12:12
  • If you have VM pass-through for the hardware PMU, programming one of its counters to count the core clock cycles event would let you use `rdpmc` before and `lfence`;`rdpmc` after. – Peter Cordes Jun 18 '22 at 12:16
  • Re: timing instructions in general, see [RDTSCP in NASM always returns the same value (timing a single instruction)](https://stackoverflow.com/q/54621381) – Peter Cordes Jun 18 '22 at 12:17
  • If `vmfunc` isn't already more or less serializing, then you'll want to time stuff like its throughput impact on other code, not just a single instance between rdtsc. I had been thinking it would be like `vmcall` that would have to drain the out-of-order back-end anyway, but that might not be the case. If it's "just" invoking microcode, the usual techniques like https://uops.info/ uses are applicable to measuring its throughput and latency (if it has inputs/outputs in general-purpose registers or memory). – Peter Cordes Jun 18 '22 at 12:21
  • Thank you! But how can I run VMFUNC command? VMFUNC needs to be run in the guest VM and also affects the page table. – hsyhhh Jun 18 '22 at 13:16
  • Oh, so the EAX value (function selector) you want to test is one that might require some setup to be testable? Hand-written asm can contain any instruction you want. And yes, I was picturing you'd run your microbenchmark in a guest VM, so you can run code that contains `vmfunc`. If it's privileged, you might need to put the microbenchmark in a kernel module or bootloader. I'm not familiar with the details of what the instruction does, beyond the very general docs https://www.felixcloutier.com/x86/vmfunc which just say the details depend on the function selected by EAX. – Peter Cordes Jun 18 '22 at 20:47

0 Answers0