Skip to content

Commit

Permalink
Add experimental config to write Mountpoint PID to file (#1261)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dannycjones authored Feb 11, 2025
1 parent e56d343 commit 19b706d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion mountpoint-s3/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::ffi::OsString;
use std::fmt::Debug;
use std::fs::File;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::num::NonZeroUsize;
use std::os::fd::{AsRawFd, RawFd};
Expand Down Expand Up @@ -605,6 +605,8 @@ where

let _metrics = metrics::install();

create_pid_file()?;

// mount file system as a foreground process
let session = mount(args, client_builder)?;

Expand Down Expand Up @@ -635,6 +637,8 @@ where

let _metrics = metrics::install();

create_pid_file()?;

let session = mount(args, client_builder);

// close unused file descriptor, we only write from this end.
Expand Down Expand Up @@ -1251,6 +1255,22 @@ fn create_client_for_bucket(
}
}

/// Creates PID file at location specified by env var, writing the PID of the Mountpoint process.
///
/// The written PID may not match the PID visible in your namespace.
/// This can happen, for example, when using it from the host when Mountpoint runs in a container.
///
/// PID file configuration is available for attaching debug tooling to Mountpoint, and may be removed in the future.
fn create_pid_file() -> anyhow::Result<()> {
const ENV_PID_FILENAME: &str = "UNSTABLE_MOUNTPOINT_PID_FILE";
if let Some(val) = std::env::var_os(ENV_PID_FILENAME) {
let pid = std::process::id();
fs::write(&val, pid.to_string()).context("failed to write PID to file")?;
tracing::trace!("PID ({pid}) written to file {val:?}");
}
Ok(())
}

fn parse_perm_bits(perm_bit_str: &str) -> Result<u16, anyhow::Error> {
let perm = u16::from_str_radix(perm_bit_str, 8).map_err(|_| anyhow!("must be a valid octal number"))?;
if perm > 0o777 {
Expand Down

0 comments on commit 19b706d

Please sign in to comment.