|
| 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 | +} |
0 commit comments