Skip to content

Commit b730648

Browse files
committed
chore: only run sudo cmds when needed
1 parent 4fba6a1 commit b730648

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/run/runner/wall_time/perf.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,32 @@ impl PerfFifo {
5555
}
5656

5757
pub fn setup_environment() {
58+
// sysctl kernel.kptr_restrict=0
59+
60+
let sysctl_read = |name: &str| -> Option<u64> {
61+
let output = std::process::Command::new("sysctl")
62+
.arg(name)
63+
.output()
64+
.unwrap();
65+
let output = String::from_utf8(output.stdout).unwrap();
66+
output
67+
.split(" = ")
68+
.last()
69+
.unwrap()
70+
.trim()
71+
.parse::<u64>()
72+
.ok()
73+
};
74+
5875
// Allow access to kernel symbols
59-
run_with_sudo(&["sysctl", "-w", "kernel.kptr_restrict=0"]).unwrap();
76+
let kptr_restrict = sysctl_read("kernel.kptr_restrict");
77+
if kptr_restrict != Some(0) {
78+
run_with_sudo(&["sysctl", "-w", "kernel.kptr_restrict=0"]).unwrap();
79+
}
6080

6181
// Allow non-root profiling
62-
run_with_sudo(&["sysctl", "-w", "kernel.perf_event_paranoid=1"]).unwrap();
82+
let perf_event_paranoid = sysctl_read("kernel.perf_event_paranoid");
83+
if perf_event_paranoid != Some(1) {
84+
run_with_sudo(&["sysctl", "-w", "kernel.perf_event_paranoid=1"]).unwrap();
85+
}
6386
}

0 commit comments

Comments
 (0)