cpp_code_analysis 0.4.0

The analysis layer of cpp_ls: the preprocessor, symbol scopes, type inference and the semantic checks.
Documentation
//! **Where a cold start's memory goes, in live bytes** — the measurement that names the spike instead of inferring it.
//!
//! ```text
//! $env:CPPLS_REAL_PROJECT = "C:\Users\zc\Desktop\test_cpp"
//! cargo test --release -p cpp_code_analysis --test cold_start_memory_probe -- --ignored --nocapture
//! ```
//!
//! # The claim this exists to check
//!
//! "The server uses four and a half gigabytes" is the report every memory change in this repository was written
//! against, and none of them measured it: the counter they were read from **only counted allocations**, so a stage
//! that allocated a gigabyte and freed it read exactly like one that kept it. See
//! [`cpp_code_analysis::alloc`]. This probe installs an allocator that counts both sides, drives the session the way
//! the pump does — a cold index, then one program read after another — and prints what the process is **holding** at
//! each step.
//!
//! # What it prints
//!
//! ```text
//! slice   live / peak / churn, and the session's own tables, once per `advance`
//! read    live / peak before and after each whole-program read
//! final   the report `Session::memory_report` keeps, which is what the tables cost
//! ```
//!
//! The difference between `live` and the tables is the transient working set: a live number that climbs while the
//! tables stay flat is a stage holding something it should have freed, and that is the question this asks.
//!
//! # It stops itself
//!
//! A live number over [`THE_CEILING`] ends the process with a line saying so. A probe that reproduces a five-gigabyte
//! spike must not take the machine down to report it.

use std::alloc::{GlobalAlloc, Layout, System};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Instant;

use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};

/// Live bytes, as the process holds them.
static LIVE: AtomicUsize = AtomicUsize::new(0);
/// The high-water mark of [`LIVE`].
static PEAK: AtomicUsize = AtomicUsize::new(0);
/// Every byte ever asked for — churn, and the number the old counter reported.
static TOTAL: AtomicUsize = AtomicUsize::new(0);

struct Counting;

unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        TOTAL.fetch_add(layout.size(), Ordering::Relaxed);
        let now = LIVE.fetch_add(layout.size(), Ordering::Relaxed) + layout.size();
        PEAK.fetch_max(now, Ordering::Relaxed);
        unsafe { System.alloc(layout) }
    }

    unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
        LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
        unsafe { System.dealloc(pointer, layout) }
    }

    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        TOTAL.fetch_add(layout.size(), Ordering::Relaxed);
        let now = LIVE.fetch_add(layout.size(), Ordering::Relaxed) + layout.size();
        PEAK.fetch_max(now, Ordering::Relaxed);
        unsafe { System.alloc_zeroed(layout) }
    }

    unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        TOTAL.fetch_add(new_size, Ordering::Relaxed);
        if new_size >= layout.size() {
            let grown = new_size - layout.size();
            let now = LIVE.fetch_add(grown, Ordering::Relaxed) + grown;
            PEAK.fetch_max(now, Ordering::Relaxed);
        } else {
            LIVE.fetch_sub(layout.size() - new_size, Ordering::Relaxed);
        }
        unsafe { System.realloc(pointer, layout, new_size) }
    }
}

#[global_allocator]
static GLOBAL: Counting = Counting;

/// **Where the probe gives up.** Not a product decision: a measurement that can crash the machine is not a
/// measurement, and the report is worth as much at one and a half gigabytes as at five.
const THE_CEILING: usize = 1536 * 1024 * 1024;

fn live() -> usize {
    LIVE.load(Ordering::Relaxed)
}

fn peak() -> usize {
    PEAK.load(Ordering::Relaxed)
}

fn mb(bytes: usize) -> usize {
    bytes / (1024 * 1024)
}

fn guard(where_: &str) {
    if live() > THE_CEILING {
        println!("STOPPED at {where_}: holding {} MB", mb(live()));
        std::process::exit(3);
    }
}

fn project() -> PathBuf {
    PathBuf::from(
        std::env::var("CPPLS_REAL_PROJECT")
            .unwrap_or_else(|_| r"C:\Users\zc\Desktop\test_cpp".to_string()),
    )
}

fn the_sources(root: &Path) -> Vec<PathBuf> {
    // **The directories the project keeps its code in** — `src` by default, `CPPLS_PROBE_DIRS` for a project that
    // names them differently (`EmmyLuaCodeStyle` has `CodeFormatCore`, `LuaParser`, …). Without this the probe
    // measured an empty project for that one.
    let directories: Vec<String> = std::env::var("CPPLS_PROBE_DIRS")
        .map(|dirs| dirs.split(',').map(|dir| dir.trim().to_string()).collect())
        .unwrap_or_else(|_| vec!["src".to_string()]);
    let mut files: Vec<PathBuf> = directories
        .iter()
        .flat_map(|directory| {
            std::fs::read_dir(root.join(directory))
                .into_iter()
                .flatten()
                .flatten()
                .map(|entry| entry.path())
                .collect::<Vec<_>>()
        })
        .filter(|path| path.extension().is_some_and(|extension| extension == "cpp"))
        .collect();
    // **Every directory's files, and the nested ones too**, because a project keeps its units under a `src` of their
    // own: one level is what this used to read, and it counted 0 files for anything deeper.
    if files.is_empty() {
        for directory in &directories {
            let mut pending = vec![root.join(directory)];
            while let Some(next) = pending.pop() {
                let Ok(entries) = std::fs::read_dir(&next) else {
                    continue;
                };
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.is_dir() {
                        pending.push(path);
                    } else if path.extension().is_some_and(|extension| extension == "cpp") {
                        files.push(path);
                    }
                }
            }
        }
    }
    files.sort();
    files
}

#[test]
#[ignore = "a probe: opens a real project and reports instead of asserting"]
fn where_a_cold_start_puts_its_memory() {
    let root = project();
    let sources = the_sources(&root);
    println!(
        "project    {} ({} source(s))",
        root.display(),
        sources.len()
    );

    let mut session = Session::open(
        root.clone(),
        SessionFiles::new(OpenDocuments::new(), DiskFiles),
        WatchFilter::new(&root),
    );
    session.add_project_files(sources.clone());

    println!("\n# the cold index");
    let started = Instant::now();
    session.index_everything();
    // **A bound the operator sets**: the loop exits on pending_work(), and a backlog holding a file no cook can
    // serve never reaches zero. The bound makes the memory question answerable without waiting out the livelock.
    let steps: usize = std::env::var("CPPLS_PROBE_STEPS")
        .ok()
        .and_then(|value| value.parse().ok())
        .unwrap_or(64);
    let limit: usize = std::env::var("CPPLS_PROBE_SLICES")
        .ok()
        .and_then(|value| value.parse().ok())
        .unwrap_or(1000);
    let mut slices = 0usize;
    let mut read_ahead_peak = 0usize;
    while session.pending_work() > 0 && slices < limit {
        session.advance(steps);
        slices += 1;
        guard("a cold slice");
        let report = session.memory_report();
        read_ahead_peak = read_ahead_peak.max(report.prefetched_bytes);
        println!(
            "slice {slices:3}  live {:5} MB  peak {:5} MB  churn {:6} MB  | tables {:5} MB (summaries {} / cooked {} / \
             units {} / renderings {})",
            mb(live()),
            mb(peak()),
            mb(TOTAL.load(Ordering::Relaxed)),
            mb(report.summary_bytes + report.cooked_bytes + report.view_bytes + report.unit_bytes),
            report.summaries,
            report.cooked_files,
            report.units,
            report.renderings,
        );
    }
    println!(
        "settled after {slices} slice(s) in {:?}; live {} MB, peak {} MB, read-ahead peak {} MB",
        started.elapsed(),
        mb(live()),
        mb(peak()),
        mb(read_ahead_peak)
    );

    println!("\n# the pump, with the reader's files open");
    // **Which documents to open**, because the interesting case is one or two files: a hang that two documents
    // reproduce is a finer experiment than one that needs eighty-two.
    let opening: Vec<PathBuf> = match std::env::var("CPPLS_PROBE_OPEN") {
        Ok(names) => {
            let names: Vec<&str> = names.split(',').map(str::trim).collect();
            sources
                .iter()
                .filter(|path| {
                    path.file_name()
                        .is_some_and(|file| names.contains(&file.to_string_lossy().as_ref()))
                })
                .cloned()
                .collect()
        }
        Err(_) => sources.clone(),
    };
    for path in &opening {
        let text = std::fs::read_to_string(path).unwrap_or_default();
        session.did_open(path, &text);
    }
    let started = Instant::now();
    // **A bound the operator sets**: the loop exits on pending_work(), and a backlog holding a file no cook can
    // serve never reaches zero. The bound makes the memory question answerable without waiting out the livelock.
    let limit: usize = std::env::var("CPPLS_PROBE_SLICES")
        .ok()
        .and_then(|value| value.parse().ok())
        .unwrap_or(1000);
    let mut slices = 0usize;
    let mut read_ahead_peak = 0usize;
    while session.pending_work() > 0 && slices < limit {
        session.advance(steps);
        slices += 1;
        guard("an open-file slice");
        let report = session.memory_report();
        read_ahead_peak = read_ahead_peak.max(report.prefetched_bytes);
        println!(
            "slice {slices:3}  live {:5} MB  peak {:5} MB  churn {:6} MB  | tables {:5} MB (summaries {} / cooked {} / \
             units {} / renderings {})",
            mb(live()),
            mb(peak()),
            mb(TOTAL.load(Ordering::Relaxed)),
            mb(report.summary_bytes + report.cooked_bytes + report.view_bytes + report.unit_bytes),
            report.summaries,
            report.cooked_files,
            report.units,
            report.renderings,
        );
    }
    println!(
        "settled after {slices} slice(s) in {:?}; live {} MB, peak {} MB, units {:?}, pending {}, read-ahead peak {} MB",
        started.elapsed(),
        mb(live()),
        mb(peak()),
        session.unit_stats(),
        session.pending_work(),
        mb(read_ahead_peak)
    );
    println!(
        "breakdown: queue {}, cooking {}, refinements {}, work {}",
        session.pending(),
        session.pending_cooking(),
        session.refinements_wanted(),
        session.pending_work()
    );

    println!("\n# one whole-program read at a time, the way the pump takes them");
    let mut reads = 0usize;
    for path in &sources {
        let before = live();
        let started = Instant::now();
        let read = session.read_the_unit(path);
        reads += 1;
        guard("a program read");
        let report = session.memory_report();
        read_ahead_peak = read_ahead_peak.max(report.prefetched_bytes);
        println!(
            "read {reads:3}  {:5} MB -> {:5} MB in {:>7.1} ms  peak {:5} MB  | tables {:5} MB  | {} | {}",
            mb(before),
            mb(live()),
            started.elapsed().as_secs_f64() * 1000.0,
            mb(peak()),
            mb(report.summary_bytes + report.cooked_bytes + report.view_bytes + report.unit_bytes),
            path.file_name().unwrap_or_default().to_string_lossy(),
            read.map_or_else(
                || "declined".to_string(),
                |reading| format!(
                    "{} file(s), {} token(s), {} error(s)",
                    reading.files, reading.tokens, reading.errors
                )
            ),
        );
    }

    let report = session.memory_report();
    println!("\n# final");
    println!(
        "live {} MB, peak {} MB, churn {} MB",
        mb(live()),
        mb(peak()),
        mb(TOTAL.load(Ordering::Relaxed))
    );
    println!("{report}");
}