Commit e7e5ec8
feat(instrument): Add --heap-trace flag for stable memory-free profiling (#107)
* feat(instrument): Add --heap-trace flag for stable memory-free profiling
This adds a new `--heap-trace` flag to the `instrument` command that stores
profiling traces in WASM linear memory instead of stable memory.
## Problem
The current `instrument` command assumes exclusive use of stable memory for
storing profiling logs. Canisters built with Emscripten, Idris2, AssemblyScript,
or other toolchains that use stable memory for their own purposes will trap
when calling `__get_profiling` because it attempts to read incompatible memory
structures.
## Solution
The `--heap-trace` flag enables profiling while avoiding stable memory entirely:
- Allocates a dedicated trace buffer at the end of WASM linear memory
- Uses `memory.grow` to extend the heap by `--heap-pages` (default: 64 pages = 4MB)
- Stores function entry/exit traces in the heap buffer
- `__get_profiling` reads from heap memory instead of stable memory
- Skips pre_upgrade/post_upgrade persistence (traces don't survive upgrades)
## Usage
```bash
ic-wasm input.wasm -o output.wasm instrument --heap-trace
ic-wasm input.wasm -o output.wasm instrument --heap-trace --heap-pages 128
```
## Trade-offs
- Traces do not persist across canister upgrades
- Buffer size is limited by available heap memory
- Detailed function traces are preserved (unlike --heap-only in PR #106)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(heap-trace): Increase memory maximum to allow memory.grow
When using --heap-trace, the WASM memory maximum must be increased
to accommodate the trace buffer. Without this fix, memory.grow fails
silently when the module's max equals its initial size.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* docs: Update instrument documentation to reflect heap-trace and stub-wasi features
- Restructure Instrument section to show execution tracing with two modes
(stable memory and heap memory) and WASI compatibility as separate capabilities
- Add dedicated "Heap-based tracing" subsection with usage and key differences
- Update usage examples to include --heap-trace and --heap-pages flags
- Consolidate CHANGELOG entries into single enhancement with sub-points
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Linwei Shang <linwei.shang@dfinity.org>1 parent 9bcb02a commit e7e5ec8
File tree
10 files changed
+292
-9
lines changed- src
- bin
- tests/ok
10 files changed
+292
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
131 | 133 | | |
132 | 134 | | |
133 | | - | |
| 135 | + | |
134 | 136 | | |
135 | 137 | | |
136 | 138 | | |
| |||
207 | 209 | | |
208 | 210 | | |
209 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
210 | 227 | | |
211 | 228 | | |
212 | 229 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
104 | 114 | | |
105 | 115 | | |
106 | 116 | | |
| |||
167 | 177 | | |
168 | 178 | | |
169 | 179 | | |
| 180 | + | |
| 181 | + | |
170 | 182 | | |
171 | 183 | | |
172 | 184 | | |
173 | 185 | | |
174 | 186 | | |
175 | 187 | | |
176 | 188 | | |
| 189 | + | |
| 190 | + | |
177 | 191 | | |
178 | 192 | | |
179 | 193 | | |
| |||
0 commit comments