|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +X-Profiler is a Node.js native addon for runtime profiling and performance monitoring. It outputs performance logs at regular intervals and enables real-time runtime state sampling via external commands (xprofctl CLI). |
| 8 | + |
| 9 | +**Supported Platforms:** Windows, Linux (x64/arm64), macOS (x64/arm64) |
| 10 | +**Node.js Versions:** v18.x, v20.x, v22.x, v24.x (LTS only) |
| 11 | + |
| 12 | +## Build Commands |
| 13 | + |
| 14 | +```bash |
| 15 | +# Build the native addon (requires C++ toolchain) |
| 16 | +npm run build |
| 17 | + |
| 18 | +# Build with pre-built binary download (fallback to build from source) |
| 19 | +npm install |
| 20 | + |
| 21 | +# Run all tests |
| 22 | +npm run test |
| 23 | + |
| 24 | +# Run a single test file |
| 25 | +npm run test-single test/config.test.js |
| 26 | + |
| 27 | +# Generate coverage report |
| 28 | +npm run cov |
| 29 | + |
| 30 | +# Run coverage for a single test |
| 31 | +npm run cov-single test/config.test.js |
| 32 | + |
| 33 | +# Full CI suite (lint + build + coverage) |
| 34 | +npm run ci |
| 35 | + |
| 36 | +# Format C++ code (requires clang-format) |
| 37 | +npm run format |
| 38 | + |
| 39 | +# Lint JavaScript code |
| 40 | +npm run lint |
| 41 | +``` |
| 42 | + |
| 43 | +## Build System & C++ Compilation |
| 44 | + |
| 45 | +**CRITICAL:** Node.js v24+ requires C++20. The binding.gyp must use: |
| 46 | +- Linux: `-std=c++20` in `cflags` |
| 47 | +- macOS: `-std=c++20` in `xcode_settings.OTHER_CFLAGS` |
| 48 | + |
| 49 | +The project uses: |
| 50 | +- **node-gyp** for native compilation |
| 51 | +- **@mapbox/node-pre-gyp** for binary distribution via GitHub Releases |
| 52 | +- **NAN (Native Abstractions for Node.js)** for V8 API compatibility |
| 53 | + |
| 54 | +Build output: `build/binding/Release/node-v{abi}-{platform}-{arch}/xprofiler.node` |
| 55 | + |
| 56 | +## Architecture |
| 57 | + |
| 58 | +### Multi-Threading Model |
| 59 | + |
| 60 | +The addon runs **three concurrent threads**: |
| 61 | + |
| 62 | +1. **Main Thread** - JavaScript execution, configuration, API calls |
| 63 | +2. **LogBypass Thread** - Periodic statistics collection (separate libuv loop) |
| 64 | +3. **CommandsListener Thread** - IPC socket server accepting xprofctl commands |
| 65 | + |
| 66 | +### Code Organization |
| 67 | + |
| 68 | +**JavaScript Layer** (`/lib/`, `/patch/`, root): |
| 69 | +- `xprofiler.js` - Main entry point and public API |
| 70 | +- `lib/configure.js` - Configuration merging (env vars → defaults → user config) |
| 71 | +- `patch/http.js` - HTTP module patching via diagnostics_channel (Node.js v18+) |
| 72 | + |
| 73 | +**Native C++ Layer** (`/src/`): |
| 74 | + |
| 75 | +**Core:** |
| 76 | +- `xprofiler.cc` - Module initialization, NAN bindings export |
| 77 | +- `environment_data.h/cc` - Per-isolate profiling state |
| 78 | +- `environment_registry.h/cc` - Multi-isolate management |
| 79 | + |
| 80 | +**JavaScript Bindings** (`/src/jsapi/`): |
| 81 | +- Bridge between JS and C++ (export_configure, export_logger, export_http, etc.) |
| 82 | + |
| 83 | +**Statistics Collection** (`/src/logbypass/`): |
| 84 | +- Runs in separate thread, samples at `log_interval` (default 60s) |
| 85 | +- Collects: CPU, heap, GC, libuv handles, HTTP metrics |
| 86 | + |
| 87 | +**Remote Commands** (`/src/commands/`): |
| 88 | +- IPC listener accepts commands from `xprofctl` CLI |
| 89 | +- Profiling: CPU profiling, heap profiling, heap snapshots, GC profiling |
| 90 | +- Diagnostics: Reports (JS/native stacks, heap stats, UV stats, system info) |
| 91 | +- Simple: version, config get/set |
| 92 | + |
| 93 | +**Platform-Specific** (`/src/platform/`): |
| 94 | +- `unix/` - Linux/macOS (Unix sockets, /proc/stat, getrusage) |
| 95 | +- `win/` - Windows (named pipes, Performance Counters, DbgHelp) |
| 96 | + |
| 97 | +**V8 Hooks** (`/src/hooks/`): |
| 98 | +- `fatal_error.cc` - FatalError handler (can trigger diagnostics/coredump) |
| 99 | +- `heap_limit.cc` - Auto-increment heap limit on OOM |
| 100 | + |
| 101 | +## Configuration System |
| 102 | + |
| 103 | +**3-Level Priority:** |
| 104 | +``` |
| 105 | +User Config (xprofiler.start({...})) [highest] |
| 106 | + ↓ |
| 107 | +Environment Variables (XPROFILER_*) |
| 108 | + ↓ |
| 109 | +Default Config (configuration.js) [lowest] |
| 110 | +``` |
| 111 | + |
| 112 | +**Key Configurations:** |
| 113 | +- `log_dir` - Output directory (default: `os.tmpdir()`) |
| 114 | +- `log_interval` - Sampling interval in seconds (default: 60) |
| 115 | +- `log_format_alinode` - Alinode format compatibility (default: false) |
| 116 | +- `patch_http` - Enable HTTP patching (default: true) |
| 117 | +- `enable_fatal_error_hook` - FatalError handling (default: true) |
| 118 | +- `enable_auto_incr_heap_limit` - Auto-grow heap on OOM (default: false) |
| 119 | + |
| 120 | +See `configuration.js` for full list and README.md for environment variable names. |
| 121 | + |
| 122 | +## Testing |
| 123 | + |
| 124 | +**Framework:** Mocha + expect.js |
| 125 | +**Coverage:** nyc (Istanbul) |
| 126 | + |
| 127 | +**Test Structure:** |
| 128 | +- `/test/*.test.js` - Unit tests for core features |
| 129 | +- `/test/patch/*.test.js` - Module patching tests |
| 130 | +- `/test/fixtures/` - Test helpers and utilities |
| 131 | + |
| 132 | +**Important:** Tests use `mm` library for mocking. Always call `mm.restore()` in afterEach hooks. |
| 133 | + |
| 134 | +## Common Development Tasks |
| 135 | + |
| 136 | +### Adding a New Configuration Option |
| 137 | + |
| 138 | +1. Add default value to `configuration.js` |
| 139 | +2. Add environment variable parsing to `lib/configure.js` |
| 140 | +3. Update native config struct in `src/configure.h` |
| 141 | +4. Add JS binding in `src/jsapi/export_configure.cc` |
| 142 | +5. Add test to `test/config.test.js` |
| 143 | + |
| 144 | +### Adding a New xprofctl Command |
| 145 | + |
| 146 | +1. Define command in `src/commands/parser.cc` |
| 147 | +2. Implement handler in appropriate `src/commands/*/` subdirectory |
| 148 | +3. Add command output logic in `src/commands/dump.cc` |
| 149 | +4. Update `bin/xprofctl` CLI with new command/args |
| 150 | +5. Add test to `test/commands.test.js` |
| 151 | + |
| 152 | +### Modifying Statistics Collection |
| 153 | + |
| 154 | +1. Update data structures in `src/library/common.h` |
| 155 | +2. Modify collection logic in `src/logbypass/*.cc` |
| 156 | +3. Update output formatting in logger |
| 157 | +4. Add test to `test/logbypass.test.js` |
| 158 | + |
| 159 | +## Platform-Specific Considerations |
| 160 | + |
| 161 | +**Linux:** |
| 162 | +- CPU stats via `/proc/stat` parsing |
| 163 | +- Coredump support via `src/platform/unix/core/linux/` |
| 164 | +- glibc mallopt configuration |
| 165 | + |
| 166 | +**macOS:** |
| 167 | +- CPU stats via `getrusage()` |
| 168 | +- Stack traces via macOS-specific APIs |
| 169 | +- No coredump support |
| 170 | + |
| 171 | +**Windows:** |
| 172 | +- Named pipes for IPC (not Unix sockets) |
| 173 | +- Windows Performance Counters for CPU |
| 174 | +- DbgHelp.dll for stack traces |
| 175 | +- Different path handling (`\\.\pipe\xprofiler-ctl`) |
| 176 | + |
| 177 | +## IPC Protocol |
| 178 | + |
| 179 | +**Socket Path:** |
| 180 | +- Unix: `<log_dir>/xprofiler-ctl-uds-path.sock` |
| 181 | +- Windows: `\\.\pipe\xprofiler-ctl` |
| 182 | + |
| 183 | +**Command Format:** JSON messages between xprofctl and CommandsListener thread |
| 184 | + |
| 185 | +**Output Location:** Profiling artifacts saved to `log_dir` with timestamps |
| 186 | + |
| 187 | +## CI/CD |
| 188 | + |
| 189 | +**GitHub Actions:** `.github/workflows/nodejs.yml` |
| 190 | +- Matrix: 3 OS × 4 Node.js versions |
| 191 | +- Steps: Build → Test → Coverage → Upload to codecov |
| 192 | +- Runs on every commit |
| 193 | + |
| 194 | +**Binary Distribution:** |
| 195 | +- Pre-built binaries released to GitHub Releases |
| 196 | +- node-pre-gyp downloads matching binary during npm install |
| 197 | +- Fallback to source compilation if binary unavailable |
| 198 | + |
| 199 | +## Important Notes |
| 200 | + |
| 201 | +- **Thread Safety:** Use `xpf_mutex.h` for synchronization between threads |
| 202 | +- **V8 Isolation:** Use `RequestInterrupt()` for safe cross-thread V8 access |
| 203 | +- **NAN Deprecations:** Some NAN APIs are deprecated in Node.js v24 (warnings are non-critical) |
| 204 | +- **Log Format:** Two formats supported (xprofiler native vs Alinode compatibility) |
| 205 | +- **Process Tracking:** Uses `~/.xprofiler` file to track running processes with xprofiler enabled |
0 commit comments