0

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.

osgx
  • 90,338
  • 53
  • 357
  • 513
  • If you want to limit your self to a process, use a BDI tool (like frida, dynamoRio or even rr). System-wide I don't think it's possible with reasonable performance. – Margaret Bloom Nov 20 '18 at 10:04
  • 1
    sounds very much like [this question](https://stackoverflow.com/questions/53359764/monitoring-of-the-clflush-assembly-instruction) – Zulan Nov 20 '18 at 11:25
  • Thanks for the answers. However, I do not have a specific process and do not want to do binary instrumentation. If I use performance counters monitoring e.g., with perf I can see the rdtsc during runtime, but only if I manually click on the function that calls it. That is why I was thinking that there might be a way.. setting some registers or whatever – assembly_question Nov 20 '18 at 11:47
  • There aren't perf events for most specific instructions. For division, there's `arith.divider_active`. You might find microcoded instructions with `idq.ms_cycles`, but that isn't specific to `rdtsc` or `clflush`. (I'm not sure `clflush` is more than 4 uops, though.) – Peter Cordes Nov 20 '18 at 17:26
  • No, you can't do that using `perf`. – Hadi Brais Nov 20 '18 at 19:34

1 Answers1

1

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.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • Hi, thanks for the answer! The thing is that I do not know how to get to this instruction to count it. I see that rdtsc has been used if I use perf top and click on the process that has called her and see the corresponding assembly code and there I see the instruction, but if I want to derive statistics for that with the --all-cpus option, I do not know what should I parse. With the perf script it was not showing it directly. Could you give me a tip about that? – assembly_question Nov 30 '18 at 13:41