5.3.16 · D1 · HinglishBuild Systems & Toolchain

FoundationsProfiling — gprof, perf, Valgrind - Callgrind

2,375 words11 min read↑ Read in English

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

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.


1. Time, aur hum ise kaise kaate hain

Profiling mein sab kuch time ke baare mein hai, isliye humein precise rehna hoga ki kaun sa time.

Hum poore run ka wall time likhte hain. Subscript sirf ek name tag hai; iska matlab multiplication nahi hai. Jab bhi baad mein koi symbol jaise dikhe, use zor se padho "T-sub-self" = "woh time jo self label hai".


2. Functions, callers, aur call graph

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.

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.


3. Self time vs total time — symbols aur

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 ke total times jodte jao jo 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."


4. Sampling: stopwatch ko tally sheet mein badalna

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.

Har snapshot ek tally mark hai us function ke liye jo us waqt pakda gaya.


5. Instrumentation vs sampling — accuracy/overhead trade

Data gather karne ke do tarike hain, aur woh opposite directions mein khainchte hain.


6. Cycles, instructions, IPC aur CPI

perf padhne ke liye tumhe woh chahiye jo CPU natively count karta hai.


7. Cache references, misses, aur miss rate


8. Fractions aur speedup — , , aur Amdahl symbols

Profiling ek decision par khatam hoti hai: kya yeh optimize karna worth it hai? Uske liye do symbols chahiye.


Prerequisite map

Wall-clock time

Self vs total time

Functions and calls

Call graph and counts

Sampling estimate

Instrumentation vs sampling

Cycles and instructions

IPC and CPI

Cache references and misses

Profiling gprof perf Callgrind

Fraction p and speedup s

Amdahl Law

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.


Equipment checklist

Right side cover karo aur khud ko test karo — tum ready ho jab har line instant ho.

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 kya represent karta hai?
Sampling interval — do consecutive PC snapshots ke beech ka time gap (jaise s at 100 Hz).
Kisi function ke self time ka sampling formula do.
.
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 aur ka kya matlab hai?
= optimized hisse mein runtime ka fraction; = tum us hisse ko kitne times faster banate ho.
hone par best possible speedup kya hai?
.