Skip to content

Commit 8782315

Browse files
committed
feat: add runner-shared crate
1 parent c760612 commit 8782315

10 files changed

Lines changed: 120 additions & 139 deletions

File tree

Cargo.lock

Lines changed: 29 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ object = "0.36.7"
5353
linux-perf-data = "0.11.0"
5454
debugid = "0.8.0"
5555
memmap2 = "0.9.5"
56-
nix = { version = "0.29.0", features = ["fs", "user"] }
56+
nix = { version = "0.29.0", features = ["fs", "time", "user"] }
5757
futures = "0.3.31"
58+
runner-shared = { path = "crates/runner-shared" }
5859

5960
[target.'cfg(target_os = "linux")'.dependencies]
6061
procfs = "0.17.0"
@@ -67,6 +68,9 @@ rstest = { version = "0.25.0", default-features = false }
6768
rstest_reuse = "0.7.0"
6869
shell-quote = "0.7.2"
6970

71+
[workspace]
72+
members = ["crates/runner-shared"]
73+
7074
[workspace.metadata.release]
7175
sign-tag = true
7276
sign-commit = true

crates/runner-shared/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "runner-shared"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
anyhow = "1.0.100"
8+
serde = { version = "1.0.225", features = ["derive"] }
9+
serde_json = "1.0.145"

crates/runner-shared/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This will be accessed from the `parse_callgraph` lambda.
2+
3+
pub mod fifo;
4+
pub mod metadata;
5+
pub mod unwind_data;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use core::{
2+
fmt::Debug,
3+
hash::{Hash, Hasher},
4+
};
5+
use serde::{Deserialize, Serialize};
6+
use std::{hash::DefaultHasher, ops::Range};
7+
8+
/// Unwind data for a single module.
9+
#[derive(Serialize, Deserialize)]
10+
pub struct UnwindData {
11+
pub path: String,
12+
13+
pub avma_range: Range<u64>,
14+
pub base_avma: u64,
15+
16+
pub eh_frame_hdr: Vec<u8>,
17+
pub eh_frame_hdr_svma: Range<u64>,
18+
19+
pub eh_frame: Vec<u8>,
20+
pub eh_frame_svma: Range<u64>,
21+
}
22+
23+
impl Debug for UnwindData {
24+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25+
let eh_frame_hdr_hash = {
26+
let mut hasher = DefaultHasher::new();
27+
self.eh_frame_hdr.hash(&mut hasher);
28+
hasher.finish()
29+
};
30+
let eh_frame_hash = {
31+
let mut hasher = DefaultHasher::new();
32+
self.eh_frame.hash(&mut hasher);
33+
hasher.finish()
34+
};
35+
36+
f.debug_struct("UnwindData")
37+
.field("path", &self.path)
38+
.field("avma_range", &format_args!("{:x?}", self.avma_range))
39+
.field("base_avma", &format_args!("{:x}", self.base_avma))
40+
.field(
41+
"eh_frame_hdr_svma",
42+
&format_args!("{:x?}", self.eh_frame_hdr_svma),
43+
)
44+
.field("eh_frame_hdr_hash", &format_args!("{eh_frame_hdr_hash:x}"))
45+
.field("eh_frame_hash", &format_args!("{eh_frame_hash:x}"))
46+
.field("eh_frame_svma", &format_args!("{:x?}", self.eh_frame_svma))
47+
.finish()
48+
}
49+
}

src/run/runner/wall_time/perf/fifo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use super::{FifoCommand, RUNNER_ACK_FIFO, RUNNER_CTL_FIFO};
1+
use super::FifoCommand;
2+
use runner_shared::fifo::{RUNNER_ACK_FIFO, RUNNER_CTL_FIFO};
23
use std::path::PathBuf;
34
use tokio::io::{AsyncReadExt, AsyncWriteExt};
45
use tokio::net::unix::pipe::OpenOptions as TokioPipeOpenOptions;

src/run/runner/wall_time/perf/helpers.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/run/runner/wall_time/perf/jit_dump.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use crate::{
22
prelude::*,
33
run::runner::wall_time::perf::{
44
perf_map::{ModuleSymbols, Symbol},
5-
unwind_data::UnwindData,
5+
unwind_data::UnwindDataExt,
66
},
77
};
88
use linux_perf_data::jitdump::{JitDumpReader, JitDumpRecord};
9+
use runner_shared::unwind_data::UnwindData;
910
use std::{
1011
collections::HashSet,
1112
path::{Path, PathBuf},

src/run/runner/wall_time/perf/unwind_data.rs

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,29 @@
22
33
use crate::run::runner::wall_time::perf::elf_helper;
44
use anyhow::{Context, bail};
5-
use core::{
6-
fmt::Debug,
7-
hash::{Hash, Hasher},
8-
};
95
use debugid::CodeId;
106
use object::{Object, ObjectSection};
11-
use serde::{Deserialize, Serialize};
12-
use std::{hash::DefaultHasher, ops::Range};
7+
use runner_shared::unwind_data::UnwindData;
8+
use std::ops::Range;
139

14-
/// Unwind data for a single module.
15-
#[derive(Serialize, Deserialize)]
16-
pub struct UnwindData {
17-
pub path: String,
18-
19-
pub avma_range: Range<u64>,
20-
pub base_avma: u64,
21-
22-
pub eh_frame_hdr: Vec<u8>,
23-
pub eh_frame_hdr_svma: Range<u64>,
24-
25-
pub eh_frame: Vec<u8>,
26-
pub eh_frame_svma: Range<u64>,
27-
}
28-
29-
impl Debug for UnwindData {
30-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31-
let eh_frame_hdr_hash = {
32-
let mut hasher = DefaultHasher::new();
33-
self.eh_frame_hdr.hash(&mut hasher);
34-
hasher.finish()
35-
};
36-
let eh_frame_hash = {
37-
let mut hasher = DefaultHasher::new();
38-
self.eh_frame.hash(&mut hasher);
39-
hasher.finish()
40-
};
10+
pub trait UnwindDataExt {
11+
fn new(
12+
path_slice: &[u8],
13+
mapping_start_file_offset: u64,
14+
mapping_start_avma: u64,
15+
mapping_size: u64,
16+
build_id: Option<&[u8]>,
17+
) -> anyhow::Result<Self>
18+
where
19+
Self: Sized;
4120

42-
f.debug_struct("UnwindData")
43-
.field("path", &self.path)
44-
.field("avma_range", &format_args!("{:x?}", self.avma_range))
45-
.field("base_avma", &format_args!("{:x}", self.base_avma))
46-
.field(
47-
"eh_frame_hdr_svma",
48-
&format_args!("{:x?}", self.eh_frame_hdr_svma),
49-
)
50-
.field("eh_frame_hdr_hash", &format_args!("{eh_frame_hdr_hash:x}"))
51-
.field("eh_frame_hash", &format_args!("{eh_frame_hash:x}"))
52-
.field("eh_frame_svma", &format_args!("{:x?}", self.eh_frame_svma))
53-
.finish()
54-
}
21+
fn save_to<P: AsRef<std::path::Path>>(&self, folder: P, pid: u32) -> anyhow::Result<()>;
22+
fn to_file<P: AsRef<std::path::Path>>(&self, path: P) -> anyhow::Result<()>;
5523
}
5624

57-
impl UnwindData {
25+
impl UnwindDataExt for UnwindData {
5826
// Based on this: https://github.com/mstange/linux-perf-stuff/blob/22ca6531b90c10dd2a4519351c843b8d7958a451/src/main.rs#L747-L893
59-
pub fn new(
27+
fn new(
6028
path_slice: &[u8],
6129
runtime_file_offset: u64,
6230
runtime_start_addr: u64,
@@ -80,18 +48,12 @@ impl UnwindData {
8048
let file_build_id = CodeId::from_binary(file_build_id);
8149
let expected_build_id = CodeId::from_binary(build_id);
8250
bail!(
83-
"File {:?} has non-matching build ID {} (expected {})",
84-
path,
85-
file_build_id,
86-
expected_build_id
51+
"File {path:?} has non-matching build ID {file_build_id} (expected {expected_build_id})"
8752
);
8853
}
8954
}
9055
(Some(_), Err(_)) | (Some(_), Ok(None)) => {
91-
bail!(
92-
"File {:?} does not contain a build ID, but we expected it to have one",
93-
path
94-
);
56+
bail!("File {path:?} does not contain a build ID, but we expected it to have one");
9557
}
9658
_ => {
9759
// No build id to check
@@ -135,7 +97,7 @@ impl UnwindData {
13597
})
13698
}
13799

138-
pub fn save_to<P: AsRef<std::path::Path>>(&self, folder: P, pid: u32) -> anyhow::Result<()> {
100+
fn save_to<P: AsRef<std::path::Path>>(&self, folder: P, pid: u32) -> anyhow::Result<()> {
139101
let unwind_data_path = folder.as_ref().join(format!(
140102
"{}_{:x}_{:x}.unwind",
141103
pid, self.avma_range.start, self.avma_range.end

0 commit comments

Comments
 (0)