Skip to content

Commit 62737d3

Browse files
committed
add benchmark utility to profile memory usage
1 parent 8674454 commit 62737d3

File tree

7 files changed

+102
-2
lines changed

7 files changed

+102
-2
lines changed

Cargo.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmarks/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mimalloc = { version = "0.1", optional = true, default-features = false }
4545
object_store = { workspace = true }
4646
parquet = { workspace = true, default-features = true }
4747
rand = { workspace = true }
48+
regex.workspace = true
4849
serde = { version = "1.0.219", features = ["derive"] }
4950
serde_json = { workspace = true }
5051
snmalloc-rs = { version = "0.3", optional = true }
@@ -53,5 +54,8 @@ test-utils = { path = "../test-utils/", version = "0.1.0" }
5354
tokio = { workspace = true, features = ["rt-multi-thread", "parking_lot"] }
5455
tokio-util = { version = "0.7.15" }
5556

57+
[target.'cfg(target_os = "linux")'.dependencies]
58+
procfs = "0.17.0"
59+
5660
[dev-dependencies]
5761
datafusion-proto = { workspace = true }

benchmarks/src/clickbench.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::fs;
1919
use std::io::ErrorKind;
2020
use std::path::{Path, PathBuf};
2121

22-
use crate::util::{BenchmarkRun, CommonOpt, QueryResult};
22+
use crate::util::{print_memory_stats, BenchmarkRun, CommonOpt, QueryResult};
2323
use datafusion::logical_expr::{ExplainFormat, ExplainOption};
2424
use datafusion::{
2525
error::{DataFusionError, Result},
@@ -192,6 +192,10 @@ impl RunOpt {
192192
}
193193
let avg = millis.iter().sum::<f64>() / millis.len() as f64;
194194
println!("Query {query_id} avg time: {avg:.2} ms");
195+
196+
if self.common.memory_stat_enabled {
197+
print_memory_stats();
198+
}
195199
Ok(query_results)
196200
}
197201

benchmarks/src/tpch/run.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::sync::Arc;
2121
use super::{
2222
get_query_sql, get_tbl_tpch_table_schema, get_tpch_table_schema, TPCH_TABLES,
2323
};
24-
use crate::util::{BenchmarkRun, CommonOpt, QueryResult};
24+
use crate::util::{print_memory_stats, BenchmarkRun, CommonOpt, QueryResult};
2525

2626
use arrow::record_batch::RecordBatch;
2727
use arrow::util::pretty::{self, pretty_format_batches};
@@ -184,6 +184,10 @@ impl RunOpt {
184184
let avg = millis.iter().sum::<f64>() / millis.len() as f64;
185185
println!("Query {query_id} avg time: {avg:.2} ms");
186186

187+
if self.common.memory_stat_enabled {
188+
print_memory_stats();
189+
}
190+
187191
Ok(query_results)
188192
}
189193

benchmarks/src/util/memory.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use datafusion::execution::memory_pool::human_readable_size;
19+
20+
#[derive(Debug)]
21+
pub struct MemoryStats {
22+
pub vm_rss_kb: Option<u64>,
23+
pub vm_hwm_kb: Option<u64>,
24+
pub vm_size_kb: Option<u64>,
25+
pub vm_peak_kb: Option<u64>,
26+
}
27+
28+
pub fn print_memory_stats() {
29+
#[cfg(target_os = "linux")]
30+
{
31+
use procfs::process::Process;
32+
33+
let pid = std::process::id();
34+
let process = Process::new(pid as i32).unwrap();
35+
let statm = process.statm().unwrap();
36+
let status = process.status().unwrap();
37+
let page_size = procfs::page_size();
38+
39+
let resident_bytes = (statm.resident * page_size) as usize;
40+
let vmpeak_bytes = status.vmpeak.map(|kb| (kb * 1024) as usize);
41+
let vmhwm_bytes = status.vmhwm.map(|kb| (kb * 1024) as usize);
42+
43+
println!(
44+
"VmPeak: {}, VmHWM: {}, RSS: {}",
45+
vmpeak_bytes
46+
.map(human_readable_size)
47+
.unwrap_or_else(|| "N/A".to_string()),
48+
vmhwm_bytes
49+
.map(human_readable_size)
50+
.unwrap_or_else(|| "N/A".to_string()),
51+
human_readable_size(resident_bytes)
52+
);
53+
}
54+
}

benchmarks/src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
// under the License.
1717

1818
//! Shared benchmark utilities
19+
mod memory;
1920
mod options;
2021
mod run;
2122

23+
pub use memory::{print_memory_stats, MemoryStats};
2224
pub use options::CommonOpt;
2325
pub use run::{BenchQuery, BenchmarkRun, QueryResult};

benchmarks/src/util/options.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ pub struct CommonOpt {
6161
/// Activate debug mode to see more details
6262
#[structopt(short, long)]
6363
pub debug: bool,
64+
65+
/// Enable memory profiling to see VmPeak, VmHwm for running benchmark.
66+
/// See more details in TODO
67+
#[structopt(long)]
68+
pub memory_stat_enabled: bool,
6469
}
6570

6671
impl CommonOpt {

0 commit comments

Comments
 (0)