Skip to content

Visual Studio Code, rebuilt in Rust

Rust
7.3 KB
Raw Blame
//! perfviewEditor.ts: Developer: Startup Performance opens the read-only markdown model `perf:Startup Performance`
//! with the system, the port's startup marks, the extension activation times and the raw marks of each source.

use std::path::PathBuf;

use gpui::{Context, Window};

use super::Workbench;
use crate::commands::Outcome;
use crate::diagnostics::Machine;
use crate::editor::TextDocument;

pub const PERF_SCHEME: &str = "perf";
const PERF_PATH: &str = "Startup Performance";

impl Workbench {
    pub(super) fn register_perf_view(&mut self) {
        self.commands.register("perfview.show", |workbench, _, window, cx| workbench.show_perf_view(window, cx));
    }

    /// timerService.ts `editorIds`: the ids of the visible editor panes.
    pub(crate) fn visible_editor_ids(&self) -> Vec<String> {
        let pane = |scheme: &str| match scheme {
            "file" => "workbench.editors.files.textFileEditor".to_owned(),
            "untitled" => "workbench.editors.textResourceEditor".to_owned(),
            super::welcome_page::WALKTHROUGH_SCHEME => "gettingStartedPage".to_owned(),
            scheme => scheme.to_owned(),
        };
        self.editors.active().map(|editor| pane(&editor.document().scheme)).into_iter().collect()
    }

    /// Opens the model pinned, or refreshes it in its open editor, as `PerfModelContentProvider._updateModel` does.
    fn show_perf_view(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Outcome {
        let text = self.perf_markdown();
        let path = PathBuf::from(PERF_PATH);
        self.leave_locked_group(PERF_SCHEME, &path);
        match self.editors.models.iter().position(|model| model.document().is(PERF_SCHEME, &path)) {
            Some(index) => {
                self.replace_model_text(PERF_SCHEME, &path, text, true, cx);
                self.editors.show_model(index, true);
            }
            None => {
                let document = TextDocument::from_text(PERF_SCHEME, &path, &text, "utf8".into(), Some("markdown"), &self.languages, &self.settings.lock().unwrap());
                let index = self.editors.open_model(document);
                self.editors.show_model(index, true);
            }
        }
        window.focus(&self.editor_focus, cx);
        self.editors_changed(cx);
        Outcome::done()
    }

    fn perf_markdown(&self) -> String {
        let mut md = String::new();
        heading(&mut md, "System Info");
        for line in system_info(&crate::diagnostics::machine(), super::accessibility::screen_reader_running(), self.workspace().is_none()) {
            md += &format!("* {line}\n");
        }
        md += "\n";
        heading(&mut md, "Performance Marks");
        let rows = crate::timer::summary_rows(crate::perf::between, &crate::perf::startup_editors());
        md += &table(&["What", "Duration", "Process", "Info"], &rows);
        md += "\n";
        let activations = crate::perf::activations();
        if !activations.is_empty() {
            heading(&mut md, "Extension Activation Stats");
            md += &table(&["Extension", "Eager", "Load Code", "Call Activate", "Finish Activate", "Event", "By"], &activations);
        }
        md += "\n";
        for (source, marks) in crate::perf::performance_marks() {
            heading(&mut md, &format!("Raw Perf Marks: {source}"));
            md += "```\nName\tTimestamp\tDelta\tTotal\n";
            let (mut last, mut total) = (None, 0.);
            for (name, time) in marks {
                let delta = last.map_or(0., |last| time - last);
                total += delta;
                md += &format!("{name}\t{time}\t{delta}\t{total}\n");
                last = Some(time);
            }
            md += "```\n";
        }
        md
    }
}

fn heading(md: &mut String, value: &str) {
    *md += &format!("## {value}\n\n");
}

/// `MarkdownBuilder.toMarkdownTable`: cells padded to their column's widest.
fn table(header: &[&str], rows: &[Vec<String>]) -> String {
    let mut widths: Vec<usize> = header.iter().map(|cell| cell.chars().count()).collect();
    for row in rows {
        for (column, cell) in row.iter().enumerate() {
            widths[column] = widths[column].max(cell.chars().count());
        }
    }
    let line = |cells: Vec<String>| cells.iter().zip(&widths).map(|(cell, width)| format!("| {cell}{} ", " ".repeat(width - cell.chars().count()))).collect::<String>() + "|\n";
    let mut text = line(header.iter().map(|cell| cell.to_string()).collect());
    text += &line(widths.iter().map(|width| "-".repeat(*width)).collect());
    for row in rows {
        text += &line(row.clone());
    }
    text
}

/// `_addSummary` with timerService.ts `_extendStartupInfo`, which leaves `meminfo` out in 1.147 (#344240).
fn system_info(machine: &Machine, screen_reader: bool, empty_workspace: bool) -> Vec<String> {
    let product = |key: &str| crate::product::get(key).as_str().unwrap_or_default().to_owned();
    let commit = Some(product("commit")).filter(|commit| !commit.is_empty()).unwrap_or("0000000".into());
    let gb = 1024. * 1024. * 1024.;
    vec![
        format!("{}: {} ({commit})", product("nameShort"), crate::product::display_version()),
        format!("OS: darwin({})", machine.release),
        format!("CPUs: {}({} x {})", machine.cpu_model, machine.cpu_count, machine.cpu_speed),
        format!("Memory(System): {:.2} GB({:.2}GB free)", machine.total_memory / gb, machine.free_memory / gb),
        format!("VM(likelihood): {}%", (machine.vm_hint * 100.).round()),
        "Initial Startup: true".into(),
        "Has 0 other windows".into(),
        format!("Screen Reader Active: {screen_reader}"),
        format!("Empty Workspace: {empty_workspace}"),
    ]
}

/// `os.freemem()` on macOS: free pages times the page size (libuv's `uv_get_free_memory`).
#[cfg(target_os = "macos")]
pub(crate) fn free_memory() -> f64 {
    let vm_stat = std::process::Command::new("vm_stat").output().map(|output| String::from_utf8_lossy(&output.stdout).into_owned()).unwrap_or_default();
    let page_size = vm_stat.split("page size of ").nth(1).and_then(|rest| rest.split(' ').next()).and_then(|size| size.parse::<f64>().ok()).unwrap_or(16384.);
    let free_pages = vm_stat.lines().find(|line| line.starts_with("Pages free:")).and_then(|line| line.split(':').nth(1)).and_then(|pages| pages.trim().trim_end_matches('.').parse::<f64>().ok()).unwrap_or_default();
    free_pages * page_size
}

#[cfg(test)]
mod tests {
    use super::{Machine, system_info, table};

    #[test]
    fn the_system_info_lists_the_cpu_speed_and_vm_likelihood_without_process_memory() {
        let machine = Machine { os_type: "Darwin".into(), arch: "arm64".into(), release: "25.0.0".into(), cpu_model: "Apple M2".into(), cpu_count: 8, cpu_speed: 2400, total_memory: 16. * 1024. * 1024. * 1024., free_memory: 4. * 1024. * 1024. * 1024., load_average: [0.; 3], vm_hint: 0.5 };
        let lines = system_info(&machine, false, true);
        assert_eq!(lines[1..], ["OS: darwin(25.0.0)", "CPUs: Apple M2(8 x 2400)", "Memory(System): 16.00 GB(4.00GB free)", "VM(likelihood): 50%", "Initial Startup: true", "Has 0 other windows", "Screen Reader Active: false", "Empty Workspace: true"]);
    }

    #[test]
    fn the_table_pads_each_column_to_its_widest_cell() {
        let rows = vec![vec!["vscode.git".to_owned(), "12".to_owned()]];
        assert_eq!(table(&["Extension", "Load Code"], &rows), "| Extension  | Load Code |\n| ---------- | --------- |\n| vscode.git | 12        |\n");
    }
}