I'd like to statistically profile my C code at the instruction level. I need to know how many additions, multiplications, divisions, etc I'm performing.
This is not your usual run of the mill code profiling requirement. I'm an algorithm developer and I want to estimate the cost of converting my code to hardware implementations. For this, I'm being asked the instruction call breakdown during run-time (parsing the compiled assembly isn't sufficient as it doesn't consider loops in the code).
After looking around, it seems VMware may offer a possible solution, but I still couldn't find the specific feature that will allow me to trace the instruction call stream of my process.
Are you aware of any profiling tools which enable this?
The Linux tool
perf
will give you a good deal of profiling information; specifically,perf annotate
will give you per-instruction relative counts.You can use pin-instat which is a PIN tool. It's a bit over kill as it records more information than the instruction count. It still should be more efficient than your gdb approach through.
Disclaimer: I'm the author of pin-instat.
I eventually used a trivial yet effective solution.
display/i $pc
Configured a simple gdb script that breaks in the function I need to analyze and proceeds to step instruction by instruction:
Executed gdb with my script dumping output into a log file:
gdb -x script a.out > log.txt
Analyzed the log to count specific instruction calls.
Crude, but it works...
The valgrind tool cachegrind can be used to get execution counts of each line in the compiled assembly (the
Ir
value in the first column).