Skip to content

Commit

Permalink
*: reduce sys_getdents syscall (tikv#7306)
Browse files Browse the repository at this point in the history
Signed-off-by: Breezewish <[email protected]>
  • Loading branch information
breezewish authored Apr 17, 2020
1 parent 6411ffe commit 7d13ca0
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 115 deletions.
33 changes: 0 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ slog = { version = "2.3", features = ["max_level_trace", "release_max_level_debu
slog-global = { version = "0.1", git = "https://github.com/breeswish/slog-global.git", rev = "0e23a5baff302a9d7bccd85f8f31e43339c2f2c1" }
slog_derive = "0.2"
parking_lot = "0.10"
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
sst_importer = { path = "components/sst_importer" }
sysinfo = { git = "https://github.com/tikv/sysinfo.git", branch = "tikv"}
tempfile = "3.0"
Expand Down
2 changes: 1 addition & 1 deletion cmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ libc = "0.2"
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
nix = "0.11"
pd_client = { path = "../components/pd_client" }
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
protobuf = "2.8"
raft = { version = "0.6.0-alpha", default-features = false }
raftstore = { path = "../components/raftstore" }
Expand Down
4 changes: 3 additions & 1 deletion cmd/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ pub fn initial_logger(config: &TiKvConfig) {

#[allow(dead_code)]
pub fn initial_metric(cfg: &MetricConfig, node_id: Option<u64>) {
tikv_util::metrics::monitor_memory()
.unwrap_or_else(|e| fatal!("failed to start memory monitor: {}", e));
tikv_util::metrics::monitor_threads("tikv")
.unwrap_or_else(|e| fatal!("failed to start monitor thread: {}", e));
.unwrap_or_else(|e| fatal!("failed to start thread monitor: {}", e));
tikv_util::metrics::monitor_allocator_stats("tikv")
.unwrap_or_else(|e| fatal!("failed to monitor allocator stats: {}", e));

Expand Down
2 changes: 1 addition & 1 deletion components/backup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ hex = "0.4"
keys = { path = "../keys" }
kvproto = { git = "https://github.com/pingcap/kvproto.git", default-features = false }
lazy_static = "1.3"
prometheus = { version = "0.8", default-features = false, features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", default-features = false, features = ["nightly", "push"] }
raft = { version = "0.6.0-alpha", default-features = false }
raftstore = { path = "../raftstore" }
serde = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion components/cdc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tokio-threadpool = "0.1"
txn_types = { path = "../txn_types" }
fail = "0.3"
lazy_static = "1.3"
prometheus = { version = "0.8", default-features = false, features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", default-features = false, features = ["nightly", "push"] }

[dev-dependencies]
engine = { path = "../engine" }
Expand Down
2 changes: 1 addition & 1 deletion components/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ encryption = { path = "../encryption" }
engine_traits = { path = "../engine_traits" }
hex = "0.4"
lazy_static = "1.3"
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
protobuf = "2.8"
serde = "1.0"
serde_derive = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion components/engine_rocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ encryption = { path = "../encryption" }
engine = { path = "../engine" }
engine_traits = { path = "../engine_traits" }
keys = { path = "../keys" }
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
slog = { version = "2.3", features = ["max_level_trace", "release_max_level_debug"] }
slog-global = { version = "0.1", git = "https://github.com/breeswish/slog-global.git", rev = "0e23a5baff302a9d7bccd85f8f31e43339c2f2c1" }
sysinfo = { git = "https://github.com/tikv/sysinfo.git", branch = "tikv"}
Expand Down
2 changes: 1 addition & 1 deletion components/pd_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ hex = "0.4"
kvproto = { git = "https://github.com/pingcap/kvproto.git", default-features = false }
lazy_static = "1.3"
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
quick-error = "1.2.2"
serde = "1.0"
serde_derive = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion components/raftstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ lazy_static = "1.3"
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
log_wrappers = { path = "../log_wrappers" }
pd_client = { path = "../pd_client" }
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
prost = { version = "0.6", optional = true }
protobuf = "2.8"
quick-error = "1.2.2"
Expand Down
2 changes: 1 addition & 1 deletion components/tidb_query_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tikv_util = { path = "../tikv_util" }
#tidb_query_datatype = { path = "../tidb_query_datatype" }
kvproto = { git = "https://github.com/pingcap/kvproto.git", default-features = false }
tipb = { git = "https://github.com/pingcap/tipb.git", default-features = false }
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
lazy_static = "1.3"

[dependencies.prometheus-static-metric]
Expand Down
2 changes: 1 addition & 1 deletion components/tikv_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lazy_static = "1.3"
libc = "0.2"
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
openssl = "0.10"
prometheus = { version = "0.8", features = ["nightly", "push", "process"] }
prometheus = { version = "0.8", features = ["nightly", "push"] }
quick-error = "1.2.2"
rand = "0.7"
serde = "1.0"
Expand Down
5 changes: 5 additions & 0 deletions components/tikv_util/src/metrics/memory_dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

pub fn monitor_memory() -> std::io::Result<()> {
Ok(())
}
76 changes: 76 additions & 0 deletions components/tikv_util/src/metrics/memory_linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

//! This module is a subset of rust-prometheus's process collector, without the fd collector
//! to avoid memory fragmentation issues when open fd is large.
use std::io::{Error, ErrorKind, Result};

use libc;
use procinfo::pid as pid_info;

use prometheus::core::{Collector, Desc};
use prometheus::{proto, Gauge, Opts};

/// Monitors memory of the current process.
pub fn monitor_memory() -> Result<()> {
let pid = unsafe { libc::getpid() };
let tc = MemoryCollector::new(pid);
prometheus::register(Box::new(tc)).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}

/// A collector to collect memory metrics.
pub struct MemoryCollector {
pid: libc::pid_t,
descs: Vec<Desc>,
vsize: Gauge,
rss: Gauge,
}

impl MemoryCollector {
pub fn new(pid: libc::pid_t) -> Self {
let mut descs = Vec::new();

let vsize = Gauge::with_opts(Opts::new(
"process_virtual_memory_bytes",
"Virtual memory size in bytes.",
))
.unwrap();
descs.extend(vsize.desc().into_iter().cloned());

let rss = Gauge::with_opts(Opts::new(
"process_resident_memory_bytes",
"Resident memory size in bytes.",
))
.unwrap();
descs.extend(rss.desc().into_iter().cloned());

Self {
pid,
descs,
vsize,
rss,
}
}
}

impl Collector for MemoryCollector {
fn desc(&self) -> Vec<&Desc> {
self.descs.iter().collect()
}

fn collect(&self) -> Vec<proto::MetricFamily> {
if let Ok(statm) = pid_info::statm(self.pid) {
self.vsize.set(statm.size as f64 * *PAGESIZE);
self.rss.set(statm.resident as f64 * *PAGESIZE);
}

let mut mfs = Vec::with_capacity(2);
mfs.extend(self.vsize.collect());
mfs.extend(self.rss.collect());
mfs
}
}

lazy_static! {
static ref PAGESIZE: f64 = { unsafe { libc::sysconf(libc::_SC_PAGESIZE) as f64 } };
}
10 changes: 10 additions & 0 deletions components/tikv_util/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ mod threads_linux;
#[cfg(target_os = "linux")]
pub use self::threads_linux::{cpu_total, get_thread_ids, monitor_threads, ThreadInfoStatistics};

#[cfg(target_os = "linux")]
mod memory_linux;
#[cfg(target_os = "linux")]
pub use self::memory_linux::monitor_memory;

#[cfg(not(target_os = "linux"))]
mod threads_dummy;
#[cfg(not(target_os = "linux"))]
pub use self::threads_dummy::{monitor_threads, ThreadInfoStatistics};

#[cfg(not(target_os = "linux"))]
mod memory_dummy;
#[cfg(not(target_os = "linux"))]
pub use self::memory_dummy::monitor_memory;

pub use self::allocator_metrics::monitor_allocator_stats;

pub mod allocator_metrics;
Expand Down
Loading

0 comments on commit 7d13ca0

Please sign in to comment.