|
| 1 | +# Tracegrind Implementation Plan |
| 2 | + |
| 3 | +## Overview |
| 4 | +Create a new Valgrind tool called **tracegrind** by forking callgrind. Tracegrind uses the same cache simulation and trapdoor mechanisms but outputs **streaming CSV trace data** instead of an accumulated callgraph. A "sample" (CSV row) is emitted at every function entry/exit with delta counters since the last sample. |
| 5 | + |
| 6 | +## Commit Strategy |
| 7 | +1. **Commit 1**: Pure mechanical fork — copy callgrind → tracegrind, rename symbols, register in build system, verify it compiles and runs identically to callgrind. |
| 8 | +2. **Commit 2+**: Behavioral changes — replace dump with CSV tracing output. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Phase 1: Fork callgrind → tracegrind (Commit 1) |
| 13 | + |
| 14 | +### 1.1 Copy directory |
| 15 | +``` |
| 16 | +cp -r callgrind/ tracegrind/ |
| 17 | +``` |
| 18 | + |
| 19 | +### 1.2 Rename symbol prefixes |
| 20 | +In **all `.c` and `.h` files** in `tracegrind/`: |
| 21 | +- `CLG_(str)` macro → `TG_(str)` expanding to `VGAPPEND(vgTracegrind_, str)` |
| 22 | +- `CLG_` prefix (constants like `CLG_ENABLE_DEBUG`, `CLG_EXPERIMENTAL`, `CLG_ASSERT`, `CLG_DEBUG`, `CLG_DEBUGIF`, `CLG_MALLOC`) → `TG_` equivalents |
| 23 | +- Header guards: `CLG_GLOBAL` → `TG_GLOBAL`, `CLG_EVENTS` → `TG_EVENTS`, `CLG_COSTS` → `TG_COSTS` |
| 24 | +- `callgrind.h` → `tracegrind.h` (public header) |
| 25 | +- Tool name strings: `"Callgrind"` → `"Tracegrind"`, descriptions updated |
| 26 | +- File comment headers: `Callgrind` → `Tracegrind` |
| 27 | +- Client request base: keep `'C','T'` for compatibility (same trapdoor macros work) |
| 28 | +- Macro names in public header: `CALLGRIND_*` → `TRACEGRIND_*` |
| 29 | + |
| 30 | +### 1.3 Create `tracegrind/Makefile.am` |
| 31 | +Based on `callgrind/Makefile.am`, replace all `callgrind` → `tracegrind`, `CALLGRIND` → `TRACEGRIND`. Keep `TRACEGRIND_CFLAGS_COMMON = -I$(top_srcdir)/cachegrind`. |
| 32 | + |
| 33 | +### 1.4 Register in build system |
| 34 | +- **`Makefile.am`** (root): Add `tracegrind` to `TOOLS` list |
| 35 | +- **`configure.ac`**: Add `tracegrind/Makefile` to `AC_CONFIG_FILES` |
| 36 | + |
| 37 | +### 1.5 Verify |
| 38 | +- Run `autogen.sh && ./configure && make` — tracegrind binary should be produced |
| 39 | +- Run `valgrind --tool=tracegrind ls` — should produce callgrind-format output (still a clone at this point) |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Phase 2: Add CSV tracing infrastructure |
| 44 | + |
| 45 | +### 2.1 New tracing state in `global.h` |
| 46 | +Add to the per-thread or global state: |
| 47 | +```c |
| 48 | +/* Tracing output state */ |
| 49 | +typedef struct { |
| 50 | + Int fd; /* Output file descriptor */ |
| 51 | + ULong seq; /* Global sequence counter */ |
| 52 | + ULong* last_sample_cost; /* Snapshot at last sample emission */ |
| 53 | + Bool initialized; |
| 54 | +} trace_output; |
| 55 | +``` |
| 56 | +Add a global `trace_output` and per-thread `last_sample_cost` arrays. |
| 57 | + |
| 58 | +### 2.2 CSV output functions in `dump.c` (heavily modified) |
| 59 | +Replace the callgraph dump logic with: |
| 60 | + |
| 61 | +**`trace_open_output()`** — Open CSV file, write header row: |
| 62 | +```csv |
| 63 | +seq,tid,event,fn,obj,file,line,Ir,I1mr,ILmr,Dr,D1mr,DLmr,Dw,D1mw,DLmw,Bc,Bcm,Bi,Bim,sysCount,sysTime |
| 64 | +``` |
| 65 | +(Columns depend on which event groups are active. `obj` = shared object/binary name.) |
| 66 | + |
| 67 | +**`trace_emit_sample(tid, event_type, fn_node)`** — Core function: |
| 68 | +1. Compute delta: `current_cost[i] - last_sample_cost[i]` for each active event |
| 69 | +2. Resolve function name, obj (from `fn_node->from->obj->name`), file, line from debug info |
| 70 | +3. Write CSV row with: seq++, tid, event (ENTER/EXIT), fn, obj, file, line, deltas... |
| 71 | +4. Update `last_sample_cost = current_cost` (copy) |
| 72 | + |
| 73 | +**`trace_close_output()`** — Write a final summary/totals row (for verification), close fd. |
| 74 | + |
| 75 | +### 2.3 Output file naming |
| 76 | +Output file: `tracegrind.out.<pid>` (or configurable via `--tracegrind-out-file=<path>`) |
| 77 | + |
| 78 | +### 2.4 Counter delta tracking |
| 79 | +The key mechanism: |
| 80 | +- `TG_(current_state).cost` is the cumulative cost pointer (same as callgrind) |
| 81 | +- Maintain a per-thread `last_sample_cost[]` array (same size as full event set) |
| 82 | +- At each sample point: `delta[i] = current_cost[i] - last_sample_cost[i]` |
| 83 | +- After emitting: `last_sample_cost[i] = current_cost[i]` |
| 84 | +- Sum of all deltas across all samples = total cost (matches callgrind) |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## Phase 3: Hook context switches to emit samples |
| 89 | + |
| 90 | +### 3.1 Modify `callstack.c` — `TG_(push_call_stack)()` |
| 91 | +After the existing enter_cost copy (line ~212) and before incrementing sp, add: |
| 92 | +```c |
| 93 | +if (!skip && TG_(current_state).collect) { |
| 94 | + fn_node* to_fn = to->cxt->fn[0]; |
| 95 | + trace_emit_sample(TG_(current_tid), "ENTER", to_fn); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### 3.2 Modify `callstack.c` — `TG_(pop_call_stack)()` |
| 100 | +After computing the cost diff (line ~345) and before restoring context, add: |
| 101 | +```c |
| 102 | +if (jcc && TG_(current_state).collect) { |
| 103 | + fn_node* to_fn = jcc->to->cxt->fn[0]; |
| 104 | + trace_emit_sample(TG_(current_tid), "EXIT", to_fn); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### 3.3 Thread switch handling |
| 109 | +In `threads.c` — `TG_(switch_thread)()`: |
| 110 | +- Save/restore per-thread `last_sample_cost` along with other per-thread state |
| 111 | +- Ensure the trace output fd is shared (single file, all threads) |
| 112 | + |
| 113 | +### 3.4 Keep jCC/BBCC accumulation |
| 114 | +Keep the existing cost accumulation in jCCs and BBCCs — this ensures the totals computed by the existing dump mechanism can be compared for correctness verification. The tracing is an additional output layer on top. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## Phase 4: Strip/simplify callgraph dump |
| 119 | + |
| 120 | +### 4.1 Simplify `dump.c` |
| 121 | +- Remove the complex callgraph output functions (`fprint_bbcc`, `fprint_jcc`, `print_bbccs`, etc.) |
| 122 | +- Keep `TG_(dump_profile)` but make it emit a totals summary instead of full callgraph |
| 123 | +- The totals summary serves as a verification mechanism |
| 124 | + |
| 125 | +### 4.2 Simplify or remove unused features |
| 126 | +- Remove `callgrind_annotate` and `callgrind_control` from tracegrind build (not applicable) |
| 127 | +- Keep all command-line options that affect instrumentation behavior |
| 128 | +- Remove dump-specific options (`--dump-instr`, `--dump-line`, `--compress-strings`, etc.) |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## Phase 5: Verification |
| 133 | + |
| 134 | +### 5.1 Totals verification |
| 135 | +- Run both callgrind and tracegrind on the same program |
| 136 | +- Sum all delta columns from tracegrind CSV |
| 137 | +- Compare with callgrind totals line — should match exactly |
| 138 | + |
| 139 | +### 5.2 Test with existing test programs |
| 140 | +- `callgrind/tests/clreq.c` — test START/STOP/TOGGLE trapdoor mechanisms |
| 141 | +- Simple test programs with known call structures |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## Key Files to Modify (after fork) |
| 146 | + |
| 147 | +| File | Changes | |
| 148 | +|------|---------| |
| 149 | +| `tracegrind/global.h` | Add `trace_output` struct, per-thread sample state | |
| 150 | +| `tracegrind/dump.c` | Replace callgraph output with CSV tracing + totals | |
| 151 | +| `tracegrind/callstack.c` | Hook `push_call_stack`/`pop_call_stack` to emit samples | |
| 152 | +| `tracegrind/threads.c` | Save/restore per-thread `last_sample_cost` | |
| 153 | +| `tracegrind/main.c` | Update tool name/description, init/fini for trace output | |
| 154 | +| `tracegrind/clo.c` | Add `--tracegrind-out-file`, remove callgraph-only options | |
| 155 | +| `tracegrind/Makefile.am` | Renamed from callgrind references | |
| 156 | +| `Makefile.am` (root) | Add `tracegrind` to TOOLS | |
| 157 | +| `configure.ac` | Add `tracegrind/Makefile` to AC_CONFIG_FILES | |
| 158 | + |
| 159 | +## Existing code to reuse (NOT rewrite) |
| 160 | +- **Cache simulation**: `tracegrind/sim.c` (unchanged from callgrind, includes `cachegrind/cg_arch.c`) |
| 161 | +- **Event/cost infrastructure**: `tracegrind/events.c`, `tracegrind/costs.c` (unchanged) |
| 162 | +- **BB/BBCC management**: `tracegrind/bb.c`, `tracegrind/bbcc.c` (unchanged) |
| 163 | +- **Function/jump tracking**: `tracegrind/fn.c`, `tracegrind/jumps.c` (unchanged) |
| 164 | +- **Client requests**: `tracegrind/main.c` handler (unchanged trapdoor logic) |
| 165 | +- **Instrumentation**: `tracegrind/main.c` instrument function (unchanged) |
| 166 | + |
| 167 | +## CSV Output Example |
| 168 | +```csv |
| 169 | +seq,tid,event,fn,obj,file,line,Ir,I1mr,ILmr,Dr,D1mr,DLmr,Dw,D1mw,DLmw,Bc,Bcm,Bi,Bim,sysCount,sysTime |
| 170 | +1,1,ENTER,main,/usr/bin/myapp,main.c,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 171 | +2,1,ENTER,compute,/usr/bin/myapp,math.c,42,1523,12,3,487,8,2,102,4,1,95,3,0,0,0,0 |
| 172 | +3,1,EXIT,compute,/usr/bin/myapp,math.c,42,8734,45,11,2104,31,7,512,15,4,401,12,2,1,0,0 |
| 173 | +4,1,ENTER,write,/lib/x86_64-linux-gnu/libc.so.6,io.c,10,234,2,0,89,1,0,45,1,0,12,0,0,0,0,0 |
| 174 | +5,1,EXIT,write,/lib/x86_64-linux-gnu/libc.so.6,io.c,10,1456,8,2,534,5,1,267,3,1,78,2,1,0,2,15000 |
| 175 | +6,1,EXIT,main,/usr/bin/myapp,main.c,5,312,3,1,98,2,0,34,1,0,15,1,0,0,0,0 |
| 176 | +``` |
| 177 | +- Verification: sum of all delta rows across all columns should match callgrind's summary totals |
0 commit comments