-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).
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.
KAISE (sampling logic derive karo): PMU ko ek event count karne ke liye program kiya ja sakta hai (jaise CPU cycles). Har P 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.
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.
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?
Tself≈nfΔt=(nf/N)Ttotal — 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?