Perf Monitoring for rdtsc dynamically

272 Views Asked by At

Is there a way to monitor for assembly instructions in "real-time" dynamically using perf? I have seen that if I use perf record /perf top and then click on the recorded functions, I see the assembly instructions, but can I directly monitor specific assembly instructions e.g., rdtsc or clflush e.g., how often they are called by a process within certain period using perf?

I am using Debian 9 on Skylake and also on Haswell.

sudo uname -a 
Linux bla 4.9.0-amd64 #1 SMP Debian 4.9.130-2 (2018-10-27) x86_64 GNU/Linux

sudo /proc/config.gz

returns command not found.

Any help/ideas are appreciated.

1

There are 1 best solutions below

1
On

Yes, you could certainly build something that dynamically samples instructions running on the host.

The basic idea is to periodically sample the processes you are interested in (which could be "all of them"), and examine the area around the sampled instruction pointer to determine the instructions that must have been executed for such a sample to have existed: for example, by disassembling until the next conditional branch, or perhaps just until the end of the basic block.

Doing this repeatedly you'll get a histogram of executed instructions, and you can then estimate how often rdtsc or any other instruction of interest is operating.

This isn't even actually all that difficult: most of the logic already exists in perf top, perf record and perf report: just combine the sampling code from perf top with the annotation code from perf top and/or perf report as described above. Perhaps you can even do it after the fact: use perf record --all-cpus to gather the samples and then run perf script or otherwise parse the file to monitor the instructions.

Each sample will only give you a small window of executed instructions, so if you need to catch the occasional rdtsc accurately, this won't work at all.

You could extend the "window" for each sample by exploiting the "last branch record" feature to essentially go back in time based on the most recent branches, and disassemble all those basic blocks, which would extend your coverage per sample by a lot.