5.3.16 · D4 · HinglishBuild Systems & Toolchain

ExercisesProfiling — gprof, perf, Valgrind - Callgrind

4,312 words20 min read↑ Read in English

5.3.16 · D4 · Coding › Build Systems & Toolchain › Profiling — gprof, perf, Valgrind - Callgrind

Jo recurring maths tools yahan use honge, unhe ek baar plain words mein naam dena zaroori hai — har symbol defined ho, har unit stated ho — taaki koi bhi problem unhe use kare usse pehle.

Figure — Profiling — gprof, perf, Valgrind - Callgrind
Figure — Profiling — gprof, perf, Valgrind - Callgrind

Level 1 — Recognition

Recall Solution

(a) gprof-pg flag compiler ko mcount() counter weave in karne par majboor karta hai, jo instrumentation hai (exact call counts, lekin timing change ho jaati hai). (b) Callgrind (ek Valgrind tool) — poora program ek simulated CPU par execute hota hai, toh counts deterministic hain lekin yeh 20–100× slower chalata hai. (c) perf — event-based sampling real PMU counters se driven, ~1–5% overhead.

Recall Solution

Sirf gprof strictly recompile maangta hai (tumhe -pg add karna hi hoga). perf existing binary par kaam karta hai (-g aur -fno-omit-frame-pointer add karna sirf symbol/call-graph quality improve karta hai). Callgrind ko bhi koi recompile nahi chahiye — woh binary ko run time par Valgrind VM ke through instrument karta hai. -g add karna sirf nicer source-line attribution deta hai.

Recall Solution

main ka total time = uska apna body plus jo bhi woh call karta hai. Near-zero self time ka matlab hai main khud almost kuch nahi karta — uska bada total sirf uske children ka sum hai. main ko optimize mat karo. Tum self time se optimize karte ho (jahan CPU actually baithta hai); total time sirf ek expensive subtree dhundhne ke liye hai jisme drill in karna ho.


Level 2 — Application

Recall Solution

(a) Har sample wall time ka "represent" karta hai. Total run time . (b) Function ka owned fraction . Toh jo sirf bhi hai. Dono routes agree karte hain, jaisa hona chahiye.

Recall Solution

Per-call cost . Itne bade call count ke saath, pehla sawaal "har call ko faster banao" nahi balki "kya main fewer calls kar sakta hoon?" hai — repeated inputs ko memoize karo, call ko loop se hoist karo, ya vectorize karo. Calls ko se reduce karna poore s par attack karta hai, jabki har call ko chip karna sirf constant par kaam karta hai.

Recall Solution

IPC = Instructions Per Cycle, core har clock tick mein kitni instructions finish karta hai: CPI = Cycles Per Instruction, exact inverse — har instruction average par kitne clock ticks cost karti hai: IPC of is far below the ~4 ceiling (equivalently CPI matlab har instruction 3 se zyada cycles kheenchti hai), toh core zyaadatar cycles stalling mein spend karta hai (idle, waiting). Yeh memory ya branch problems point karta hai — L3 dekho — na ki ek cleverer algorithm ki zaroorat hai.


Level 3 — Analysis

Recall Solution

(a) lene par: term , toh No matter what, untouched tumhe par cap kar deta hai. (b) ke saath: Toh ek 4× local win ek 2.17× global win deta hai — ceiling se kaafi kam, jo batata hai ki is ek function par aur effort ke sharply diminishing returns hain.

Recall Solution

Branch predictor theek kaam kar raha hai (1%). 12% cache miss rate culprit hai: ek DRAM miss ~100+ cycles cost karta hai vs L1 hit ke ~4 cycles, toh woh misses low IPC easily explain kar dete hain. Fix Cache Hierarchy & Locality of Reference mein rehta hai — locality improve karo (struct-of-arrays, loop blocking) — na ki algorithmic complexity mein. Branch Prediction & Pipelining se compare karo, jo yahan problem nahi hai.

Recall Solution

Misses se lost cycles cycles. Sab cycles ka fraction . ~72% cycles plausibly memory stalls se consume ho rahe hain, toh cache behaviour akele slowdown ki vast majority account karta hai — memory waqai ek sufficient explanation hai, aur locality work sahi lever hai. Neeche di gayi figure yeh arithmetic cycle budget kaahan jaata hai iska picture banati hai.

Figure — Profiling — gprof, perf, Valgrind - Callgrind

Level 4 — Synthesis

Recall Solution

(a) Callgrind — yeh deterministic hai (same input → identical instruction counts), toh ek >2% threshold machines across stable hai bina kisi timer noise ke. Slowness (20–100×) CI mein acceptable hai. (b) gprof — quick aur classic; ek -pg rebuild plus ek command ek flat profile deta hai jo "roughly kaun sa function" ke liye kaafi hai. (c) perf — real PMU hardware counters ~1–5% overhead par, release binary ka koi recompile nahi, aur yeh real CPU par true cache misses report karta hai. Callgrind disqualify hai kyunki uska simulated timing real-hardware timing nahi hai.

Recall Solution

(a) . (Sanity: s ✓ stated run se match karta hai.) (b) (c) Naya time . Yeh forecast-then-verify discipline hai: edit karne se pehle s predict karo, phir confirm karne ke liye re-profile karo — agar measured time s ke paas nahi hai, toh tumhara model (ya tumhari assumption ki sirf serialize badla) galat hai. Dekho Benchmarking & Microbenchmark Pitfalls.

Recall Solution

Har candidate par Amdahl formula alag apply karo. A (, ): B (, ): B choose karo (). Lesson: ek chota slice ek bade achievable speedup ke saath ek bade slice ko beat kar sakta hai jise tum mushkil se improve kar sako — Amdahl shrink hone se reward karta hai, toh aur dono matter karte hain, sirf nahi.


Level 5 — Mastery

Recall Solution

-O2 par optimizer ne tiny_helper ko uske callers mein inline kar diya (dekho Compiler Optimization Flags (-O2, -O3, inlining)). Ek baar inline hone par, woh ek distinct function nahi raha jiske paas ek entry point ho, toh gprof ka mcount()/sample attribution usse time pin karne ki koi jagah nahi tha — uska cost ab caller ke self time mein fused ho gaya hai. Tumhara -O0 profile us code ki taraf point kar raha tha jo shipped binary mein exist hi nahi karta. Sahi method: wahi optimization level profile karo jo tum ship karte ho (-O2/-O3), source symbols ke liye -g add karo, aur -fno-omit-frame-pointer taaki perf abhi bhi call graphs build kar sake. Kabhi -O0 profile se conclusions mat nikalo.

Recall Solution

Koi contradiction nahi — woh alag alag cheezein measure karte hain.

  • Callgrind instructions executed count karta hai: f few instructions chalata hai (8%).
  • Lekin perf dikhata hai f 90% cache misses trigger karta hai; har miss core ko ~100+ cycles stall karta hai bina kisi instructions ke, IPC ko 0.4 tak crush karta hai.
  • gprof wall-clock self time measure karta hai, jismein woh stall cycles bhi hain, toh f few instructions ke bawajood real time ka 40% khaata hai. Coherent story: f memory-bound hai — instructions mein cheap, time mein expensive kyunki woh DRAM ka intezaar karta hai. Yahi wajah hai ki mistake "Callgrind real seconds batata hai" dangerous hai: low instruction count ne ek real-time hog chhupaaya. Yahan timing truth ke liye perf use karo.
Recall Solution

(a) (10%). (b) Toh , yaani true fraction plausibly mein hai. (Pehle normal-approximation conditions check karo: aur , toh bell-curve interval yahan trustworthy hai.) (c) Poora interval 5% se upar baitha hai, toh pessimistic end par bhi g budget exceed karta hai — yeh conclude karne ke liye enough hai "over budget". Agar interval 5% ke aas paas hota toh tum longer run karte: ki tarah girta hai, toh error halve karne ke liye tumhe samples chahiye. Isliye short profiling runs jittery hot-lists dete hain.

Recall Solution

(a) Instruction count fixed hone par, q ka run time cycles , toh q ka speedup . (b) (c) Rewrite ke baad, perf re-run karo aur confirm karo ki q ka measured self time waqai ~8× drop kiya (equivalently uske cycles/miss-rate ne model ke anusaar giraavat dekhi). Agar whole-program speedup ke paas nahi aaya, toh ya toh IPC 2.8 tak nahi pahuncha ya koi aur function naya bottleneck ban gaya — dobara profile karo. Yeh measure → model → change → re-measure loop close karta hai.


Recall Quick self-quiz (cloze)

Amdahl's speedup formula ::: Self time from samples ::: Standard deviation of a sampled fraction ::: IPC of 0.3 on a 4-wide core ka matlab CPU mostly ::: stalling (waiting) hai, likely cache misses par CPI kya stand karta hai aur kya equal hai? ::: Cycles Per Instruction, Deterministic, machine-independent counts wala tool ::: Callgrind Real hardware par ~1–5% overhead wala tool ::: perf Tum kaun se time column se optimize karte ho, total se nahi? ::: self time