Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,35 @@ if(BUILD_TESTING)
--compiler-arg=--sdk-path
--compiler-arg=${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src
)
if(TARGET aperture)
add_executable(nil_vm_memory_profile_tests tests/vm_memory_profile_tests.cpp)
target_link_libraries(nil_vm_memory_profile_tests PRIVATE nilcompiler_core)
target_compile_features(nil_vm_memory_profile_tests PRIVATE cxx_std_17)
foreach(profile_mode IN ITEMS restart failure stepping abandoned)
add_test(NAME aperture.vm-profile-${profile_mode}
COMMAND $<TARGET_FILE:nil_vm_memory_profile_tests> ${profile_mode})
set_tests_properties(aperture.vm-profile-${profile_mode}
PROPERTIES LABELS "aperture;memory;interpreter" TIMEOUT 30)
endforeach()
add_executable(nil_memory_profile_probe tests/tools/memory_profile_probe.cpp nilcompiler/src/memory_profile.cpp)
target_include_directories(nil_memory_profile_probe PRIVATE nilcompiler/src)
target_compile_features(nil_memory_profile_probe PRIVATE cxx_std_17)
target_link_libraries(nil_memory_profile_probe PRIVATE Threads::Threads)
add_test(NAME aperture.memory-collector-contract
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/memory_profile_contract.py"
--probe $<TARGET_FILE:nil_memory_profile_probe>)
set_tests_properties(aperture.memory-collector-contract PROPERTIES LABELS "aperture;memory" TIMEOUT 30)
if(UNIX)
add_test(NAME aperture.memory-contract
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/aperture_memory_contract.py"
--aperture $<TARGET_FILE:aperture>
--compiler $<TARGET_FILE:nilcompiler>
--interpreter $<TARGET_FILE:nilinterpreter>
--stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src"
--llvm "${NIL_ENABLE_LLVM}")
set_tests_properties(aperture.memory-contract PROPERTIES LABELS "aperture;memory;routes" TIMEOUT 180)
endif()
endif()
add_test(
NAME nil.analysis
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/analysis_contract.py"
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ The framework includes:
workspace snapshot. Compiler-resolved source bindings support Go to Definition
for local variables, parameters and imported functions across both languages.
Certified local variables also support references and checked rename.
- **Aperture — unified analysis suite.** Code exploration, memory profiling,
and performance tracing in a single tool designed for both human developers
and AI agents. See [aperture/README.md](aperture/README.md).
- **Aperture runtime diagnostics.** Memory profiles record runtime allocations,
frees, retained storage, peaks and source locations for VM and native programs.
Saved JSON reports support inspection and automated analysis. See
[aperture/README.md](aperture/README.md) for coverage and planned capabilities.

See the [standard-library reference](docs/standard-library/index.md),
[analysis API](docs/analysis-api.md), and [tool reference](docs/tools/index.md)
Expand Down
156 changes: 84 additions & 72 deletions aperture/README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,93 @@
# Aperture — NIL Framework Analysis & Debugging Suite
# Aperture

Aperture is an all-in-one static and dynamic analysis environment for the NIL
framework. It unifies code exploration, memory profiling, performance tracing,
and interactive debugging in a single tool designed for both human developers
and AI agents.
Aperture is the NIL framework's runtime diagnostics tool. Its first implemented
capability records memory activity from Ny and Matrix programs running in the
interpreter or as native executables, then connects allocation totals to source
locations.

## Vision
## Record a memory profile

Aperture brings together what JetBrains split across dotPeek (decompilation and
exploration), dotMemory (memory profiling), and dotTrace (performance tracing) —
but with the ability for AI agents to drive analysis, correlate findings, and
surface actionable insights.
Aperture ships with the SDK and uses the compiler, interpreter, assembler, and
linker installed beside it.

## Capabilities
```sh
# Compile and run source in the VM, saving a new report.
sdk/aperture memory --output vm-memory.json example.matrix

| Module | Purpose | JetBrains Analog |
|--------|---------|-----------------|
| `aperture-explore` | Code navigation, decompilation, call graphs | dotPeek |
| `aperture-memory` | Heap profiling, allocation tracing, leak detection | dotMemory |
| `aperture-trace` | CPU profiling, flame graphs, hotspot analysis | dotTrace |
| `aperture-agent` | AI-driven analysis and recommendation engine | — |
# Compile the same source with source metadata, then assemble and link natively.
sdk/aperture memory --native --output native-memory.json example.matrix

## Architecture
# Profile an existing NIL artifact or an instrumented native executable.
sdk/aperture memory --output memory.json example.nil
sdk/aperture memory --output native.json ./example

# Inspect a saved report without executing the program again.
sdk/aperture memory --report memory.json
sdk/aperture memory --report memory.json --json
```
aperture/
├── CMakeLists.txt # Build configuration
├── README.md # This file
├── src/ # Core source
│ ├── explore/ # Code exploration engine
│ ├── memory/ # Memory profiling engine
│ ├── trace/ # Performance tracing engine
│ ├── agent/ # AI agent interface
│ ├── core/ # Shared utilities and types
│ └── main.cpp # CLI entry point
└── docs/ # Design documentation
```

## Building

Aperture is built as part of the NIL SDK:

```bash
./scripts/build-sdk.sh
```

## Usage

```bash
# Explore code structure and generate call graphs
sdk/aperture explore --call-graph example.nil

# Profile memory allocations
sdk/aperture memory --profile example.nil

# Trace execution performance
sdk/aperture trace --flame example.nil

# Agent-assisted analysis (interactive)
sdk/aperture agent analyze --target example.nil
```

## AI Agent Integration

Aperture exposes structured analysis output (JSON, NDJSON) designed for
machine consumption. The `agent` mode provides a conversational interface
where an AI agent can:

1. **Query the codebase** — ask about call graphs, dependencies, or types
2. **Correlate findings** — combine memory and trace data to spot patterns
3. **Generate reports** — produce structured analysis summaries
4. **Recommend fixes** — suggest code changes based on detected issues

This is Aperture's differentiator: analysis that doesn't stop at the human
screen. An AI agent can operate Aperture autonomously, producing insights
that no single human analyst could gather in the same time.

## Status

Prototype. Core architecture is being defined alongside the NIL 1.0 release.
Recording preserves the program's standard input, output, error stream, working
directory, and exit status. The profile summary goes to standard error. `--json`
prints the full report there while recording; use `--output` or saved-report
inspection for a separate machine-readable stream. Program arguments follow
`--`, for example `aperture memory ./example -- "input file.txt"`.

`--output` creates a new file and refuses to overwrite existing files, including
symlinks. A missing, malformed, or incomplete runtime profile makes recording
fail; a nonzero program exit remains the command's exit status. A valid report
can still be saved after a program failure. Abrupt termination may prevent the
runtime from finishing its report.

For source compilation, `--sdk-path <directory>` selects the standard-library
search root. This is a single-file workflow; project builds and dependency
configuration remain the responsibility of `nilbuild`. Programs compiled
separately need `--debug-info` in their NIL to retain source paths and line
numbers. Native source recording goes through that annotated NIL before native
assembly and linking. Older native programs must be rebuilt against the updated
runtime to produce profiles.

`--memory-mode safe|unsafe` selects the interpreter memory mode, or the mode used
to compile source natively. Without it, each tool keeps its normal default. The
mode of an existing native executable cannot be changed by the profiler.

## What the report measures

Reports use the versioned `aperture.memory.v1` JSON schema. They contain:

- Cumulative allocations and frees, current live storage, peak live storage, and
resize counts, in objects and runtime logical bytes.
- Allocation sites with source file, function, line, and allocation kind.
- Garbage-collection count, elapsed collection time, and reclaimed storage.
- Completion state, instrumentation errors, and scope limitations.

Logical bytes describe storage tracked by each execution runtime. They are not
process RSS, allocator overhead, or a guarantee of identical byte counts between
VM and native execution. Live totals are captured before runtime teardown. An
object remaining live at that point is **not a proven memory leak**.

Source attribution identifies the allocating instruction; it does not yet retain
full call stacks or ownership/retention paths. Reports aggregate allocation sites,
not individual allocation events. Reports include absolute source paths and
function names, so review them before sharing outside your project.

## Current scope

Recording is supported on POSIX hosts. Saved JSON reports can be inspected
without executing their targets. The current profiler covers the NIL runtime
allocations described in each report's `scope`. VM coverage currently includes
typed arrays, raw buffers, and iterator payloads; VM text and record storage,
container capacity, and `Value` overhead are excluded. Native coverage includes
runtime-managed allocations, array descriptors and capacity, and runtime buffer
storage. Native unmanaged instructions that allocate directly through libc are
excluded, including raw allocations, records, variants, and string concatenation.
External library allocations, OS resources, and unrelated process memory are
outside that accounting.

Heap snapshots and comparison, retained-object graphs, allocation rates and
lifetimes, invalid-access diagnostics, CPU profiling, debugger capture, and an
interactive analysis interface are planned. The `explore`, `trace`, and `agent`
commands currently return an explicit unsupported-command error.

Build with the framework or generate a complete SDK using
`./scripts/build-sdk.sh`. See [design notes](docs/DESIGN.md) for the runtime
contract and integration boundaries.
Loading
Loading