hydro_deploy/rust_crate/tracing_options.rs
1#![allow(clippy::too_many_arguments, reason = "buildstructor")]
2#![allow(
3 unexpected_cfgs,
4 reason = "https://github.com/BrynCooke/buildstructor/issues/192"
5)]
6
7use std::borrow::Cow;
8use std::path::PathBuf;
9
10use inferno::collapse::perf::Options as PerfOptions;
11
12type FlamegraphOptions = inferno::flamegraph::Options<'static>;
13
14/// `Cow<'static, str>`.
15///
16/// `buildstructor` doesn't support `Into<_>` for types with parameters (like `Cow<'static, str>`),
17/// so we trick it by defining a type alias.
18pub type CowStr = Cow<'static, str>;
19
20#[derive(Clone, buildstructor::Builder)]
21#[non_exhaustive] // Prevent direct construction.
22pub struct TracingOptions {
23 /// Samples per second.
24 pub frequency: u32,
25
26 /// Output filename for `samply`. Example: `my_worker.profile`.
27 pub samply_outfile: Option<PathBuf>,
28
29 /// Output filename for the raw data emitted by `perf record`. Example: `my_worker.perf.data`.
30 pub perf_raw_outfile: Option<PathBuf>,
31
32 // /// Output filename for `perf script -i <`[`Self::perf_raw_outfile`]`>`. Example: `my_worker.perf`.
33 // pub perf_script_outfile: Option<PathBuf>,
34 /// If set, what the write the folded output to.
35 pub fold_outfile: Option<PathBuf>,
36 pub fold_perf_options: Option<PerfOptions>,
37 /// If set, what to write the output flamegraph SVG file to.
38 pub flamegraph_outfile: Option<PathBuf>,
39 // This type is super annoying and isn't `clone` and has a lifetime... so wrap in fn pointer for now.
40 pub flamegraph_options: Option<fn() -> FlamegraphOptions>,
41
42 /// Command to setup tracing before running the command, i.e. to install `perf` or set kernel flags.
43 ///
44 /// NOTE: Currently is only run for remote/cloud ssh hosts, not local hosts.
45 ///
46 /// Example: see [`DEBIAN_PERF_SETUP_COMMAND`].
47 pub setup_command: Option<CowStr>,
48}
49
50/// A command to run on Debian-based systems to set up `perf` for tracing.
51///
52/// Uses `apt` to install `linux-perf` and `binutils`, sets kernel parameters to allow tracing, and disables `kptr_restrict`.
53pub const DEBIAN_PERF_SETUP_COMMAND: &str = "sudo sh -c 'apt update && apt install -y linux-perf binutils && echo -1 > /proc/sys/kernel/perf_event_paranoid && echo 0 > /proc/sys/kernel/kptr_restrict'";