Skip to content

Commit 19b706d

Browse files
authored
Add experimental config to write Mountpoint PID to file (#1261)
When investigating performance, we wanted to automate the collection of profiler captures using a tool like `perf`. To do this, we needed the process ID of Mountpoint. By writing out the PID to a file, scripts could automatically record profiles for the lifetime of Mountpoint by providing its PID to `perf`. This change adds the ability to write Mountpoint's PID to a file under an experimental/unstable environment variable. Since its unclear if we want to expose this properly such as providing a CLI argument, we are taking the unstable environment variable approach to make clear this configuration may change or be removed in future. ### Does this change impact existing behavior? This change adds a new experimental feature to write Mountpoint's PID to a file. ### Does this change need a changelog entry? Does it require a version change? Since this is adding an experimental feature, no changelog entry is required. No minor version patch is required, as this is not a stable feature addition. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and I agree to the terms of the [Developer Certificate of Origin (DCO)](https://developercertificate.org/). --------- Signed-off-by: Daniel Carl Jones <[email protected]>
1 parent e56d343 commit 19b706d

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

mountpoint-s3/src/cli.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env;
22
use std::ffi::OsString;
33
use std::fmt::Debug;
4-
use std::fs::File;
4+
use std::fs::{self, File};
55
use std::io::{Read, Write};
66
use std::num::NonZeroUsize;
77
use std::os::fd::{AsRawFd, RawFd};
@@ -605,6 +605,8 @@ where
605605

606606
let _metrics = metrics::install();
607607

608+
create_pid_file()?;
609+
608610
// mount file system as a foreground process
609611
let session = mount(args, client_builder)?;
610612

@@ -635,6 +637,8 @@ where
635637

636638
let _metrics = metrics::install();
637639

640+
create_pid_file()?;
641+
638642
let session = mount(args, client_builder);
639643

640644
// close unused file descriptor, we only write from this end.
@@ -1251,6 +1255,22 @@ fn create_client_for_bucket(
12511255
}
12521256
}
12531257

1258+
/// Creates PID file at location specified by env var, writing the PID of the Mountpoint process.
1259+
///
1260+
/// The written PID may not match the PID visible in your namespace.
1261+
/// This can happen, for example, when using it from the host when Mountpoint runs in a container.
1262+
///
1263+
/// PID file configuration is available for attaching debug tooling to Mountpoint, and may be removed in the future.
1264+
fn create_pid_file() -> anyhow::Result<()> {
1265+
const ENV_PID_FILENAME: &str = "UNSTABLE_MOUNTPOINT_PID_FILE";
1266+
if let Some(val) = std::env::var_os(ENV_PID_FILENAME) {
1267+
let pid = std::process::id();
1268+
fs::write(&val, pid.to_string()).context("failed to write PID to file")?;
1269+
tracing::trace!("PID ({pid}) written to file {val:?}");
1270+
}
1271+
Ok(())
1272+
}
1273+
12541274
fn parse_perm_bits(perm_bit_str: &str) -> Result<u16, anyhow::Error> {
12551275
let perm = u16::from_str_radix(perm_bit_str, 8).map_err(|_| anyhow!("must be a valid octal number"))?;
12561276
if perm > 0o777 {

0 commit comments

Comments
 (0)