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