Gprof instrumentation

Is there a way to get zig to compile with gprof instrumentation (-pg)? I’ve been trying to get tracy to work but all the examples I see online are out of date and not working. I’m used to gprof and it seems like it would just be so much easier.

There is no gprof instrumentation in zig.

If you are on linux consider using perf, it is the linux perf_events profiler that does not need instrumentation.
An awesome tutorial is Julia Evans zine: Profiling & tracing with perf
Other perf links:

I thought perf only did event counts. I need the call graph / function timings. It’s very course sampling it seems like. Very course. Trying to cut micros off something.

perf isn’t cutting it. It is only getting like 3 functions and then flooded with other processes.

To get a flame graph with cpu timings of stack traces for program run:

sudo perf record program
perf script | stack-collapse-perf.pl | flamegraph.pl > graph.svh

Just follow the tutorial, it’s all there (pages 4-9).


By the way exactly the same functionality is available in windows using PerfView.

no stack counts found.

edit: fixed, needed -g and with -F 999 I stilll get very coarse grained results. Sampling profilers might be good for basic stuff, but instrumentation is really needed to useful fine grained results.

There are flags for limiting the recording to a single process and increasing the sampling rate.

1 Like

tried it, still not getting any useful results. The tracy tutorial I was using for zig was just out of date with respect to the build system. Is there no way to pass -pg to llvm and have it instrument for gprof?

Increase the sampling frequency more. You might like --call-graph dwarf. Run perf record --call-graph dwarf ./yourprogram. You can use -F max to use the maximum allowed frequency, to get that, use sudo sysctl kernel.perf_event_max_sample_rate.

1 Like

I tried al that. I got best results from valgrind on a much smaller test file. The results are good enough to know I need to repalce the std hashtable with something better. gprof would just be so much easier. I’m surprised at the total lack of good profiling tool for zig.

I’m probably just write some code and do by hand source instrumentation. I already have a benchmark timing library that uses RDTSC so shouldn’t be much more work to record it to a file with @src and then write something to analyze that file.