5.3.16 · D1 · Coding › Build Systems & Toolchain › Profiling — gprof, perf, Valgrind - Callgrind
Kisi program ki runtime uske poore code mein evenly spread nahi hoti — ek bahut chota sa hissa almost saara kaam karta hai , aur profiling simply yeh count karna hai ki seconds actually kahan girte hain — guess karne ke bajaye. Parent page ki saari baatein — samples, IPC, cache misses, call graphs — sirf alag-alag rulers hain usi ek hisse ko measure karne ke liye.
Isse pehle ki tum koi perf report ya gprof table padh sako, tumhe vocabulary chahiye. Yeh page har symbol aur word build karta hai jis par parent note rely karta hai — shuru karta hai un cheezoon se jo ek 12-saal ka bachcha bhi jaanta hai (ek ghadi, ek tally sheet, ek stopwatch) aur exactly wahin khatam hota hai jahan parent note shuru hota hai.
Profiling mein sab kuch time ke baare mein hai, isliye humein precise rehna hoga ki kaun sa time.
Definition Wall-clock time
Wall-clock time woh time hai jo ek asli deewar ki ghadi program ke chalte waqt dikhayegi — shuru se ant tak, sab kuch milake. Picture yeh hai: ek stopwatch jise tum launch par press karte ho aur exit par stop karte ho.
Hum poore run ka wall time T total run likhte hain. Subscript sirf ek name tag hai; iska matlab multiplication nahi hai. Jab bhi baad mein koi symbol jaise T self dikhe, use zor se padho "T-sub-self" = "woh time jo self label hai".
Intuition Hum sirf ek stopwatch kyun nahi use kar sakte
Ek stopwatch batata hai ki poore program mein 5 second lage. Yeh nahi batata ki kin 5 seconds kis function ne khaaye. 5 seconds ko functions mein baantne ke liye humein bahut saari choti-choti measurements chahiye — aur yahi se sampling aati hai (Section 4).
Ek function code ka ek named block hota hai jise tum invoke kar sakte ho. Jab function main normalize ke andar kuch code chalata hai, hum kehte hain ki main ne normalize ko call kiya.
Definition Caller, callee, edge
Caller = woh function jo call kar raha hai (main).
Callee = woh function jise call kiya ja raha hai (normalize).
Edge = arrow caller → callee . Arrow ko padho "calls" ke roop mein.
Har function ko ek box aur har "calls" relationship ko ek arrow ki tarah draw karo, aur tumhare paas ek call graph aa jayega — ek map ki kaun kise call karta hai.
Definition Call graph aur call count
Call graph poori boxes-aur-arrows ki picture hai. Call count yeh hai ki koi particular edge kitni baar travel ki gayi — jaise main ne compute_dist ko 1 0 6 baar call kiya. Hum ek million ko 1 0 6 likhte hain (ek 1 ke baad chheh zeros).
Intuition Time ke liye graph kyun matter karta hai
Agar koi function slow hai lekin sirf ek baar call hoti hai, shayad tum parwah na karo. Agar woh fast hai lekin ek million baar call hoti hai, toh count dushman hai, per-call cost nahi. Profiling ko dono — time aur count — chahiye taaki bataye ki kaun sa lever kheencho (har call sasta banao vs. kam calls karo).
Yeh distinction almost sabko trip karti hai, isliye hum ise ek picture se anchor karte hain.
Bada Σ symbol (∑ ) ka matlab hai "jodte jao over" — yahan, har function g ke total times jodte jao jo f call karta hai. Ise padho: "f ka total time = uska apna time, plus jo kuch bhi usne call kiya unke saare total times ka sum."
main slow kyun lagta hai par hota nahi
main mein almost hamesha sabse zyada total time hota hai — kyunki uska subtree poora program hi hai . Lekin uska self time near zero hota hai: woh sirf doosri cheezein call karta hai. Optimize karo self time se; total time ka use sirf yeh spot karne ke liye karo ki tree ki kaun si branch mein ghusna hai.
Common mistake Total time ko "slow function" ki tarah padhna
Kyun sahi lagta hai: sabse bada number hi culprit hona chahiye. Trap: sabse bada total-time number usually main ya koi coordinator hota hai jo khud kuch nahi karta. Fix: real hot code dhundhne ke liye self time se sort karo.
Hum program ko har line time karne ke liye ek billion baar pause nahi kar sakte — yeh jo hum measure kar rahe hain use hi badal dega. Isliye hum sample karte hain.
Definition Sampling aur sampling interval
Δ t
Sampling = ek timer program ko ek fixed rhythm par interrupt karta hai aur likh leta hai ki abhi kaun sa instruction chal raha hai . Do snapshots ke beech ka gap sampling interval Δ t hai (Greek letter delta, Δ , ka matlab "ek choti change/step in"; yahan time mein ek step).
Agar timer 100 baar per second fire karta hai, toh Δ t = 100 1 = 0.01 seconds.
Har snapshot ek tally mark hai us function ke liye jo us waqt pakda gaya.
Intuition Stop-motion camera
Ek kitchen ko 1000 baar per second photograph karo. Agar chulha 700 photos mein aata hai, toh chulha ~70% time busy tha. Tumne kabhi chulhe ko directly time nahi kiya — tumne appearances count kiye aur photos ke beech ke gap se multiply kiya. Yahi exactly sampling estimate hai jo neeche hai.
Worked example Sampling math sanity check
Timer 100 Hz par (Δ t = 0.01 s). 5-second run mein N = 500 snapshots milte hain. Agar compute_dist 360 baar pakda gaya:
T self ≈ 360 × 0.01 = 3.6 s = 500 360 × 5 = 0.72 × 5 = 3.6 s . ✓
Dono forms agree karte hain — yahi two-sided formula ka poora point hai.
Data gather karne ke do tarike hain, aur woh opposite directions mein khainchte hain.
Definition Instrumentation
Instrumentation = tool tumhare program mein extra counting code insert karta hai (jaise -pg har function entry par mcount() ka call add karta hai). Result: exact call counts — lekin extra code time cost karta hai, isliye yeh usi timing ko distort karta hai jo tum measure karna chahte the.
Overhead = woh extra time/work jo ek measurement tool program ki real work ke upar add karta hai. "20–100×" overhead (Callgrind) ka matlab hai program measure hote waqt 20 se 100 baar slower chalta hai.
Intuition Unavoidable trade-off
count accuracy ↑ ⇒ overhead ↑ ⇒ measured timing true timing se drift karti hai
Exact counting ke liye har event ko touch karna padta hai (slow). Sasta measuring ke liye events skip karne padte hain aur statistically guess karna padta hai (fast, fuzzy). Koi tool is se nahi bach sakta; har tool bas line par ek jagah chuntta hai. Callgrind "exact + bahut slow" par baith ta hai; perf "statistical + ~1–5% overhead" par.
perf padhne ke liye tumhe woh chahiye jo CPU natively count karta hai.
Definition Cycle aur instruction
Ek cycle CPU ki internal clock ki ek tick hai — processor ki schedule karne ki smallest unit. Ek 3 GHz CPU mein 3 × 1 0 9 cycles per second hote hain.
Ek instruction ek machine operation hai (add, load, compare). "Retired " ka matlab hai ek instruction puri tarah complete ho gayi (sirf start nahi hui aur throw away nahi hui).
Definition Cache, reference, miss
Ek cache CPU ke paas ek choti, bahut fast memory hai jo recently used data rakhti hai. Ek cache reference har baar hota hai jab CPU cache se data maangta hai. Ek cache miss tab hota hai jab data wahan nahi hota aur use door DRAM se fetch karna padta hai (~100+ cycles vs ~4 hit ke liye).
Profiling ek decision par khatam hoti hai: kya yeh optimize karna worth it hai? Uske liye do symbols chahiye.
Definition Optimizable fraction
p aur local speedup s
p = total runtime ka fraction jo us hisse mein bita jise tum optimize karne ki plan kar rahe ho (0 ≤ p ≤ 1 ; jaise p = 0.72 ka matlab 72%).
s = tum us hisse ko kitne times faster banate ho (jaise s = 4 matlab 4× faster).
p = 0.72 ke saath, best possible total speedup (infinite s ) hai 1 − 0.72 1 = 0.28 1 ≈ 3.57 × . Profiling pehle yeh ceiling batati hai isse pehle ki tum code mein din lagao.
Instrumentation vs sampling
Cache references and misses
Profiling gprof perf Callgrind
Har foundation box profiling topic ko feed karta hai: sampling tumhe self time deti hai, call graph counts deta hai, IPC aur miss rate batate hain ki function slow kyun hai, aur Amdahl batata hai ki bother karna chahiye ya nahi .
Right side cover karo aur khud ko test karo — tum ready ho jab har line instant ho.
T total run kya measure karta hai?Poore program ka wall-clock time, shuru se ant tak, jo kuch bhi call karta hai sab milake.
Self time vs total time ek sentence mein? Self = function ki apni lines ke andar seconds; total = self plus un functions mein bita saara time jo usne call kiye.
main usually kissi mein highest hota hai, aur iska matlab woh slow hai kyun nahi?Total time mein highest, kyunki uska subtree poora program hai; uska apna self time near zero hota hai.
Sampling mein Δ t kya represent karta hai? Sampling interval — do consecutive PC snapshots ke beech ka time gap (jaise 0.01 s at 100 Hz).
Kisi function ke self time ka sampling formula do. T self ( f ) ≈ n f Δ t = N n f T total run .
Instrumentation vs sampling — core trade kya hai? Instrumentation = exact counts lekin high overhead jo timing distort karta hai; sampling = low overhead lekin statistical estimates.
IPC aur CPI define karo. IPC = instructions retired ÷ cycles; CPI = 1/IPC = cycles ÷ instructions retired.
IPC 0.5 ka matlab kya hai? CPU stall kar raha hai — zyada tar cycles wait kar raha hai, likely cache misses ya branch mispredictions ki wajah se, real kaam nahi kar raha.
Cache miss rate ka formula? miss rate = cache-misses ÷ cache-references.
Amdahl's Law mein p aur s ka kya matlab hai? p = optimized hisse mein runtime ka fraction; s = tum us hisse ko kitne times faster banate ho.
p = 0.72 hone par best possible speedup kya hai?1 − 0.72 1 = 0.28 1 ≈ 3.57 × .