Photo by Jack Millard on Unsplash
Hi! Do you want to know which part of the code is taking more time to run? Profiling is the technique to collect runtime data that shows exactly that. We did that manually in the previous labs by adding the elapsed time for the function under analysis – this is called instrumentation. The other way is to interrupt the execution multiple times, taking snapshots along the way – this is called sampling.
Sampling doesn’t change the binary, but it might not get all data. Let’s say that if a task starts and finishes between the snapshots, we won’t get it in the report. On the other hand, the instrumentation will get everything, but it has to change the executable. As a result, we will not test the final version. We have to keep that in mind to use the right tool for the situation.
Speaking about tools, here they are gprof and perf.
The gprof does sampling and instrumentation, while perf only does sampling.
To use gprof, we need to pass -pg to the compiler, so it will add the necessary tools to collect our runtime data. Below are the steps to add the instrumentation into the executable, run it and see the profiling report.
# Generate the binary with instrumentation
> ./configure CFLAGS=”-g -Og -pg”
> make
# Run and produce the gmon.out
> ./gzip </tmp/services1000 >/dev/null
# Text report
> gprof ./gzip | less
# Graphical report
> gprof ./gzip | gprof2dot | dot -T x11
> gprof ./gzip | gprof2dot | dot -T png -o profile.png
To use perf, we don’t need the -pg parameter. So, we can use the original executable. Here are the steps to use it.
# Record the execution
> perf record ./gzip </tmp/services1000 >/dev/null
# Text report
> perf report | less
# Interactive mode
> perf report
Comments
Post a Comment