Sha256: c75ca9431787b918b9dbe19761182bb6f4e905451312a8135263fca51b7d25df

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

use crate::prelude::*;
use crate::profiling_agent::ProfilingAgent;
use std::io::{self, BufWriter, Write};
use std::process;
use std::{fs::File, sync::Mutex};

/// Process-wide perf map file. Perf only reads a unique file per process.
static PERFMAP_FILE: Mutex<Option<BufWriter<File>>> = Mutex::new(None);

/// Interface for driving the creation of jitdump files
struct PerfMapAgent;

/// Initialize a JitDumpAgent and write out the header.
pub fn new() -> Result<Box<dyn ProfilingAgent>> {
    let mut file = PERFMAP_FILE.lock().unwrap();
    if file.is_none() {
        let filename = format!("/tmp/perf-{}.map", process::id());
        *file = Some(BufWriter::new(File::create(filename)?));
    }
    Ok(Box::new(PerfMapAgent))
}

impl PerfMapAgent {
    fn make_line(writer: &mut dyn Write, name: &str, code: &[u8]) -> io::Result<()> {
        // Format is documented here: https://github.com/torvalds/linux/blob/master/tools/perf/Documentation/jit-interface.txt
        // Try our best to sanitize the name, since wasm allows for any utf8 string in there.
        let sanitized_name = name.replace('\n', "_").replace('\r', "_");
        write!(
            writer,
            "{:p} {:x} {}\n",
            code.as_ptr(),
            code.len(),
            sanitized_name
        )?;
        writer.flush()?;
        Ok(())
    }
}

impl ProfilingAgent for PerfMapAgent {
    fn register_function(&self, name: &str, code: &[u8]) {
        let mut file = PERFMAP_FILE.lock().unwrap();
        let file = file.as_mut().unwrap();
        if let Err(err) = Self::make_line(file, name, code) {
            eprintln!("Error when writing import trampoline info to the perf map file: {err}");
        }
    }
}

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/wasmtime-30.0.2/src/profiling_agent/perfmap.rs
wasmtime-29.0.0 ./ext/cargo-vendor/wasmtime-29.0.0/src/profiling_agent/perfmap.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wasmtime-28.0.0/src/profiling_agent/perfmap.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wasmtime-27.0.0/src/profiling_agent/perfmap.rs
wasmtime-26.0.0 ./ext/cargo-vendor/wasmtime-26.0.0/src/profiling_agent/perfmap.rs
wasmtime-25.0.2 ./ext/cargo-vendor/wasmtime-25.0.2/src/profiling_agent/perfmap.rs
wasmtime-25.0.1 ./ext/cargo-vendor/wasmtime-25.0.1/src/profiling_agent/perfmap.rs
wasmtime-25.0.0 ./ext/cargo-vendor/wasmtime-25.0.0/src/profiling_agent/perfmap.rs
wasmtime-24.0.0 ./ext/cargo-vendor/wasmtime-24.0.0/src/profiling_agent/perfmap.rs