MCPcopy Create free account
hub / github.com/CodSpeedHQ/codspeed / memtrack_path

Function memtrack_path

src/executor/memory/setup.rs:47–84  ·  view source on GitHub ↗

The binary that carries the eBPF capabilities: a copy of this executable, so the runner itself never runs in glibc's secure-execution mode, which strips `LD_*` and similar variables from the environment benchmarks inherit. Granted `+ep`, not inheritable, so a benchmark spawned from it does not receive them. Keyed by a hash of this executable, and copied there on first use. The copy's directory mt

()

Source from the content-addressed store, hash-verified

45/// Keyed by a hash of this executable, and copied there on first use. The copy's
46/// directory mtime records its last use.
47pub fn memtrack_path() -> Result<PathBuf> {
48 static PATH: OnceLock<PathBuf> = OnceLock::new();
49 if let Some(path) = PATH.get() {
50 return Ok(path.clone());
51 }
52
53 let exe = self_exe()?;
54 let hash =
55 sha256::try_digest(&exe).with_context(|| format!("failed to hash {}", exe.display()))?;
56 let cache_dir = memtrack_cache_dir()?;
57 let dir = cache_dir.join(hash);
58 let path = dir.join("codspeed");
59 if path.exists() {
60 let _ = std::fs::File::open(&dir).and_then(|dir| dir.set_modified(SystemTime::now()));
61 } else {
62 std::fs::create_dir_all(&dir)
63 .with_context(|| format!("failed to create {}", dir.display()))?;
64 // Linked into place: `path.exists()` must never accept an interrupted copy,
65 // and a concurrent install must not replace a copy that may already hold
66 // its capabilities.
67 let tmp = dir.join(format!("codspeed.{}.tmp", std::process::id()));
68 std::fs::copy(&exe, &tmp)
69 .with_context(|| format!("failed to copy {} to {}", exe.display(), tmp.display()))?;
70 let linked = std::fs::hard_link(&tmp, &path);
71 let _ = std::fs::remove_file(&tmp);
72 match linked {
73 Err(e) if e.kind() != std::io::ErrorKind::AlreadyExists => {
74 return Err(e).with_context(|| {
75 format!("failed to move {} to {}", tmp.display(), path.display())
76 });
77 }
78 _ => {}
79 }
80 prune_stale_copies(&cache_dir, &dir);
81 }
82
83 Ok(PATH.get_or_init(|| path).clone())
84}
85
86fn prune_stale_copies(cache_dir: &Path, current: &Path) {
87 let Ok(entries) = std::fs::read_dir(cache_dir) else {

Callers 4

executeMethod · 0.85

Calls 3

self_exeFunction · 0.85
memtrack_cache_dirFunction · 0.85
prune_stale_copiesFunction · 0.85

Tested by

no test coverage detected