Master the fundamental concepts of profiling & measurement through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksValgrind's Callgrind simulates every instruction, so results are repeatable across machines even when hardware perf events are blocked in containers. KCachegrind turns Callgrind output into inclusive and exclusive cost views. It is slower than perf by 10x/50x, but invaluable when you need exact call counts on a laptop without perf permissions.
Inclusive cost counts time in a function plus everything it called. Exclusive cost is time spent inside the function body only. A high inclusive, low exclusive node means the callee is the real problem.
bashLoading…
callgrind.out files with callgrind_annotate --diffKeep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will profile a C program with Callgrind and interpret the annotated output. This exercise asks you to name the top three inclusive-cost functions and explain what each one does in your call graph.
Callgrind counts exact instructions (Ir) per function, plus how often each function calls each other one. callgrind_annotate and KCachegrind then show self cost (instructions in the function's own code) and inclusive cost (self plus everything it calls). Given self costs and call counts, compute inclusive costs, produce the annotate table, and show where a function's time goes among its callees.
cLoading…
Functions are created on first mention with a self cost of 0.
round(incl(g) * C / calls_into(g)) (half up), where calls_into(g) sums all edges into g. Then incl(f) = self(f) + the charges for all of f's outgoing edges.annotate: recursion cycle detected (callgrind would merge it into a cycle group) (or callees: recursion cycle detected).fn NAME: rejected (negative cost), call A B: rejected (COUNT < 1), and callees NAME: unknown function.cLoading…
Table rows use "%9ld %3ld.%ld%% %8ld %3ld.%ld%% %6ld %s", and percentages are rounded half up to 1 decimal.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover a helper shared by several callers, a deep call chain, a function with zero self cost, ties in the sort order, recursion, and an unknown function in callees.