Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit malloc metrics #1214

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mountpoint-s3/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,25 @@ impl CliArgs {
}
}

#[cfg(target_os = "linux")]
fn configure_malloc() {
unsafe {
// Disable dynamic MMAP_THRESHOLD and set it to 4MiB: https://github.com/bminor/glibc/blob/release/2.26/master/malloc/malloc.c#L1739

// NOTE: M_MMAP_MAX is 65536. With 8MiB allocations it allows up to 512GiB of simultaneously allocated memory via mmap, which should fit for most of the cases. For more info:
// https://github.com/bminor/glibc/blob/release/2.26/master/malloc/malloc.c#L983
libc::mallopt(libc::M_MMAP_THRESHOLD, 4194304);
}
}

pub fn main<ClientBuilder, Client, Runtime>(client_builder: ClientBuilder) -> anyhow::Result<()>
where
ClientBuilder: FnOnce(&CliArgs) -> anyhow::Result<(Client, Runtime, S3Personality)>,
Client: ObjectClient + Clone + Send + Sync + 'static,
Runtime: Spawn + Clone + Send + Sync + 'static,
{
#[cfg(target_os = "linux")]
configure_malloc();
let args = CliArgs::parse();
let successful_mount_msg = format!(
"{} is mounted at {}",
Expand Down
23 changes: 23 additions & 0 deletions mountpoint-s3/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub fn install() -> MetricsSinkHandle {
Ok(()) | Err(RecvTimeoutError::Disconnected) => break,
Err(RecvTimeoutError::Timeout) => {
poll_process_metrics(&mut sys);
#[cfg(target_os = "linux")]
poll_malloc_metrics();
inner.publish()
}
}
Expand All @@ -52,6 +54,8 @@ pub fn install() -> MetricsSinkHandle {
// any new metrics data after the sink shuts down, but we assume a clean shutdown
// stops generating new metrics before shutting down the sink.
poll_process_metrics(&mut sys);
#[cfg(target_os = "linux")]
poll_malloc_metrics();
inner.publish();
})
};
Expand Down Expand Up @@ -87,6 +91,25 @@ fn poll_process_metrics(sys: &mut System) {
}
}

#[cfg(target_os = "linux")]
fn poll_malloc_metrics() {
unsafe {
let mallinfo = libc::mallinfo();
// Space wasted due to fragmentation: `process.mallinfo.arena - process.mallinfo.fordblks`
metrics::gauge!("process.mallinfo.arena").set(mallinfo.arena as f64); /* non-mmapped space allocated from system */
metrics::gauge!("process.mallinfo.ordblks").set(mallinfo.ordblks as f64); /* number of free chunks */
metrics::gauge!("process.mallinfo.smblks").set(mallinfo.smblks as f64); /* number of fastbin blocks */
metrics::gauge!("process.mallinfo.hblks").set(mallinfo.hblks as f64); /* number of mmapped regions */
metrics::gauge!("process.mallinfo.hblkhd").set(mallinfo.hblkhd as f64); /* space in mmapped regions */
metrics::gauge!("process.mallinfo.usmblks").set(mallinfo.usmblks as f64); /* always 0, preserved for backwards compatibility */
metrics::gauge!("process.mallinfo.fsmblks").set(mallinfo.fsmblks as f64); /* space available in freed fastbin blocks */
metrics::gauge!("process.mallinfo.uordblks").set(mallinfo.uordblks as f64); /* total allocated space */
metrics::gauge!("process.mallinfo.fordblks").set(mallinfo.fordblks as f64); /* total free space */
/* top-most, releasable (via malloc_trim) space */
metrics::gauge!("process.mallinfo.keepcost").set(mallinfo.keepcost as f64);
}
}

#[derive(Debug)]
struct MetricsSink {
metrics: DashMap<Key, Metric>,
Expand Down
Loading