I want to obtain the percentage of memory read-write instructions in a test program, preferably dynamically. Apart from counting instructions in the gdb asm dump, which is static anyway, is there an easier way to obtain it? Valgrind provides total heap usage. Perf has some nice features but does not support WSL. Pin has an instruction count capability but it I am not sure if it supports WSL.
2 Answers
(Update: PIN reportedly doesn't work under WSL. But it doesn't require perf counters so it's still useful in VMs or whatever. It might work in WSL2, which is a VM running a real Linux kernel.)
Have you tried PIN, or SDE which is built on top of PIN and does what you want? https://software.intel.com/en-us/articles/intel-software-development-emulator
sde64 -mix -- ./my-program
will tell you if PIN works, and might solve your problem all on its own. SDE's instruction mix is by mnemonic IIRC, maybe not by memory destination vs. src, but at least you'll know that PIN will work before you sink any time into writing a custom PIN tool.
PIN is based on dynamic runtime instrumentation, not performance counters. It probably doesn't even cath SIGILL (not that WSL would have a prob. with that); it has to emulate lzcnt
on CPUs where it decodes as rep bsr
, and stuff like that.
So I doubt PIN needs much kernel support beyond the ability to JIT (which stuff like Java and web browser JS engines need, and is just mmap(PROT_EXEC)
). It might use ptrace, but if GDB works under WSL then hopefully PIN will.
Related: How do I determine the number of x86 machine instructions executed in a C program? isn't WSL-specific, but mentions sde64 -mix
.

- 328,167
- 45
- 605
- 847
-
I don't have WSL (or Windows), so I don't know for *certain* that this answer will work. Comment if necessary! It's at least useful for things other than WSL, so might help future readers. – Peter Cordes Oct 05 '19 at 03:58
-
1I receive the error: **E: Fork for injector launcher failed: Bad address** To work around it, #1 I ran **echo 0|sudo tee /proc/sys/kernel/yama/ptrace_scope** #2 I ran **export PATH=$PATH:/home/myusername/sde**, I tried #3 **chmod -R 777 .** and finally I tried #4 **export PATH=$PATH:/home/myusername/sde/intel64/pin_lib/injector** yet it still failed. xed64 works just fine. – Husrev Oct 05 '19 at 06:36
-
It seems to be a WSL related problem as SDE64 works in virtualbox. Thanks. – Husrev Oct 05 '19 at 16:05
-
WSL1 and WLS2 are different; I wouldn't be surprised if it works under WSL2, which is a true Linux kernel in a VM, rather than extensions to the Windows kernel to provide Linux APIs. – Peter Cordes Aug 23 '22 at 14:35
valgrind tools are doing a lot more than providing the total heap usage.
See valgrind user manual http://www.valgrind.org/docs/manual/manual.html
In particular, look at the sections describing the valgrind tools callgrind and cachegrind.

- 3,669
- 1
- 11
- 12