5.3.16 · HinglishBuild Systems & Toolchain

Profiling — gprof, perf, Valgrind - Callgrind

2,103 words10 min readRead in English

5.3.16 · Coding › Build Systems & Toolchain


Profile kyon karein?

DO strategies kyon? Ek tradeoff hai:


Teen tools ek nazar mein

Tool Mechanism Kya measure karta hai Overhead Recompile chahiye?
gprof instrumentation + sampling self/total time, call graph medium haan (-pg)
perf hardware counters + sampling cycles, cache misses, branches ~1–5% nahi (-g helpful hai)
Callgrind (Valgrind) full CPU simulation exact instr counts, cache sim 20–100× nahi

gprof — KYA & KAISE

KAISE kaam karta hai (model derive karo):

  1. -pg compiler se mcount() ka call har function ki entry par insert karaata hai. Yeh caller→callee edge record karta hai, call graph aur call counts banata hai (instrumentation → exact counts).
  2. Ek periodic timer (SIGPROF, ~100 Hz) PC sample karta hai taaki self time per function estimate ho sake (sampling → statistical time).

Self time vs total (cumulative) time — key distinction:

  • Self time = function ke apne body ke andar spend kiya gaya time.
  • Total time = self time + uske dwara call ki gayi sab cheezein ka time.
$ gcc -pg -O2 prog.c -o prog
$ ./prog            # gmon.out likhta hai
$ gprof ./prog gmon.out > report.txt

Flat profile columns: % time | self seconds | calls | self ms/call | name.


perf — KYA & KAISE

KAISE (sampling logic derive karo): PMU ko ek event count karne ke liye program kiya ja sakta hai (jaise CPU cycles). Har events ke baad yeh ek interrupt raise karta hai; perf us waqt PC record karta hai. Yeh event-based sampling hai — jahan program zyaada kaam karta hai wahan samples dense hote hain.

$ perf stat ./prog              # summary counters (cycles, IPC, cache-misses)
$ perf record -g ./prog         # -g = call graph capture karo
$ perf report                   # interactive TUI
$ perf annotate                 # per-instruction hotness

Valgrind / Callgrind — KYA & KAISE

$ valgrind --tool=callgrind ./prog     # callgrind.out.<pid> likhta hai
$ callgrind_annotate callgrind.out.<pid>
$ kcachegrind callgrind.out.<pid>      # GUI call graph

20–100× slowdown ke bawajood use kyon karein?

  • Deterministic: same input → identical counts. Koi timer noise nahi, CI regression tests ke liye perfect.
  • Instructions/calls exactly count karta hai, isliye cost ek single line tak attribute ki ja sakti hai.
  • --cache-sim=yes L1/LL caches model karta hai; --branch-sim=yes branch prediction model karta hai — bina PMU access ke.
Figure — Profiling — gprof, perf, Valgrind - Callgrind

Worked examples



Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho tum homework karte waqt apna time time karo. Kaunsa subject sabse slow hai yeh guess karne ke bajaye, har minute likhte ho ki abhi kya kar rahe ho. Ek ghante baad count karo: "math = 40 tally marks, reading = 5." Ab tum jaante ho ki math tumhara time kha raha hai, toh wahan tumhe faster hona chahiye. Profilers programs ke liye yahi karte hain — woh program ko baar baar tap karte hain aur poochte hain "abhi tum kya kar rahe ho?" aur jawab tally karte hain. Lazy tarika (perf) bas kabhi kabhi jhaankta hai; careful slow tarika (Callgrind) har ek second dekhta hai lekin bahut zyaada time leta hai.


Flashcards

Optimization ka cardinal rule kya hai?
Optimize karne se pehle measure (profile) karo — bottlenecks ke baare mein intuition usually galat hota hai.
Instrumentation vs sampling profiling?
Instrumentation counters insert karta hai (exact counts, high overhead, timing distort hoti hai); sampling periodically interrupt karke PC record karta hai (low overhead, statistical estimate).
gprof ke liye kaunsa flag compile karta hai aur running se kaunsi file banti hai?
-pg; running se gmon.out banti hai.
gprof self time vs total/cumulative time?
Self = function ke apne body mein time; total = self + uski sab calls mein time.
Samples se function ka self time estimate karne ka formula?
— samples ka fraction × total runtime.
perf events measure karne ke liye kya use karta hai?
Kernel perf_events subsystem jo CPU ke hardware PMU (performance counters) ko drive karta hai, event-based sampling ke zariye.
IPC define karo aur low IPC kya imply karta hai.
IPC = instructions retired / CPU cycles; low IPC (jaise <1) matlab CPU stall kar raha hai, often cache misses ya branch mispredictions ki wajah se.
1% L1 cache miss potentially dominant kyon hota hai?
DRAM tak ek miss ~100+ cycles cost karti hai vs L1 hit ke liye ~4, isliye thodi si misses bhi bahut time add karti hain.
Callgrind ka main downside aur main upside?
Downside: 20–100× slowdown aur yeh simulate karta hai (real wall-clock nahi). Upside: deterministic, bit-exact instruction/call counts, CI ke liye reproducible.
Fraction p ko s× speed up karne par Amdahl's law ka speedup formula?
; ceiling hai.
Optimized (-O2) build profile karna kyon zaroori hai, -O0 nahi?
Optimizer inline/vectorize karta hai; -O0 profile aisi code ki taraf point karta hai jo release binary mein exist hi nahi karti.
Quick counter summary vs call-graph record ke liye perf command?
Summary ke liye perf stat ./prog; call graph ke liye perf record -g ./prog phir perf report.

Connections

Concept Map

goal

rule

strategy

strategy

exact counts but

low overhead

used by

used by

used by

needs -pg

distinguishes

reads

via

cost

Profiling

Find hot path

Measure before optimize

Instrumentation

Sampling

High overhead distorts timing

Statistical estimate

gprof

perf

mcount builds call graph

Self vs total time

Hardware counters cache misses

Callgrind

Full CPU simulation exact counts

20-100x slowdown