forked from tikv/tikv
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmod.rs
More file actions
154 lines (130 loc) · 4.57 KB
/
mod.rs
File metadata and controls
154 lines (130 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2017 TiKV Project Authors. Licensed under Apache-2.0.
#[cfg(target_os = "linux")]
mod cgroup;
pub mod cpu_time;
pub mod disk;
pub mod inspector;
pub mod thread;
// re-export some traits for ease of use
use crate::config::{ReadableSize, KIB};
use fail::fail_point;
#[cfg(target_os = "linux")]
use lazy_static::lazy_static;
use std::sync::atomic::{AtomicU64, Ordering};
use sysinfo::RefreshKind;
pub use sysinfo::{DiskExt, NetworkExt, ProcessExt, ProcessorExt, SystemExt};
pub const HIGH_PRI: i32 = -1;
const CPU_CORES_QUOTA_ENV_VAR_KEY: &str = "TIKV_CPU_CORES_QUOTA";
static GLOBAL_MEMORY_USAGE: AtomicU64 = AtomicU64::new(0);
static MEMORY_USAGE_HIGH_WATER: AtomicU64 = AtomicU64::new(u64::MAX);
#[cfg(target_os = "linux")]
lazy_static! {
static ref SELF_CGROUP: cgroup::CGroupSys = cgroup::CGroupSys::new().unwrap_or_default();
}
pub struct SysQuota;
impl SysQuota {
#[cfg(target_os = "linux")]
pub fn cpu_cores_quota() -> f64 {
let mut cpu_num = num_cpus::get() as f64;
let cpuset_cores = SELF_CGROUP.cpuset_cores().len() as f64;
let cpu_quota = SELF_CGROUP.cpu_quota().unwrap_or(0.);
if cpuset_cores != 0. {
cpu_num = cpu_num.min(cpuset_cores);
}
if cpu_quota != 0. {
cpu_num = cpu_num.min(cpu_quota);
}
limit_cpu_cores_quota_by_env_var(cpu_num)
}
#[cfg(not(target_os = "linux"))]
pub fn cpu_cores_quota() -> f64 {
let cpu_num = num_cpus::get() as f64;
limit_cpu_cores_quota_by_env_var(cpu_num)
}
#[cfg(target_os = "linux")]
pub fn memory_limit_in_bytes() -> u64 {
let total_mem = Self::sysinfo_memory_limit_in_bytes();
if let Some(cgroup_memory_limit) = SELF_CGROUP.memory_limit_in_bytes() {
std::cmp::min(total_mem, cgroup_memory_limit)
} else {
total_mem
}
}
#[cfg(not(target_os = "linux"))]
pub fn memory_limit_in_bytes() -> u64 {
Self::sysinfo_memory_limit_in_bytes()
}
pub fn log_quota() {
#[cfg(target_os = "linux")]
info!(
"cgroup quota: memory={:?}, cpu={:?}, cores={:?}",
SELF_CGROUP.memory_limit_in_bytes(),
SELF_CGROUP.cpu_quota(),
SELF_CGROUP.cpuset_cores(),
);
info!(
"memory limit in bytes: {}, cpu cores quota: {}",
Self::memory_limit_in_bytes(),
Self::cpu_cores_quota()
);
}
fn sysinfo_memory_limit_in_bytes() -> u64 {
let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory());
system.get_total_memory() * KIB
}
}
/// Get the current global memory usage in bytes. Users need to call `record_global_memory_usage`
/// to refresh it periodically.
pub fn get_global_memory_usage() -> u64 {
GLOBAL_MEMORY_USAGE.load(Ordering::Acquire)
}
/// Record the current global memory usage of the process.
#[cfg(target_os = "linux")]
pub fn record_global_memory_usage() {
let s = procinfo::pid::statm_self().unwrap();
let usage = s.resident * page_size::get();
GLOBAL_MEMORY_USAGE.store(usage as u64, Ordering::Release);
}
#[cfg(not(target_os = "linux"))]
pub fn record_global_memory_usage() {
GLOBAL_MEMORY_USAGE.store(0, Ordering::Release);
}
/// Register the high water mark so that `memory_usage_reaches_high_water` is available.
pub fn register_memory_usage_high_water(mark: u64) {
MEMORY_USAGE_HIGH_WATER.store(mark, Ordering::Release);
}
pub fn memory_usage_reaches_high_water(usage: &mut u64) -> bool {
fail_point!("memory_usage_reaches_high_water", |_| true);
*usage = get_global_memory_usage();
*usage >= MEMORY_USAGE_HIGH_WATER.load(Ordering::Acquire)
}
fn limit_cpu_cores_quota_by_env_var(quota: f64) -> f64 {
match std::env::var(CPU_CORES_QUOTA_ENV_VAR_KEY)
.ok()
.and_then(|value| value.parse().ok())
{
Some(env_var_quota) if quota.is_sign_positive() => f64::min(quota, env_var_quota),
_ => quota,
}
}
fn read_size_in_cache(level: usize, field: &str) -> Option<u64> {
std::fs::read_to_string(format!(
"/sys/devices/system/cpu/cpu0/cache/index{}/{}",
level, field
))
.ok()
.and_then(|s| s.parse::<ReadableSize>().ok())
.map(|s| s.0)
}
/// Gets the size of given level cache.
///
/// It will only return `Some` on Linux.
pub fn cache_size(level: usize) -> Option<u64> {
read_size_in_cache(level, "size")
}
/// Gets the size of given level cache line.
///
/// It will only return `Some` on Linux.
pub fn cache_line_size(level: usize) -> Option<u64> {
read_size_in_cache(level, "coherency_line_size")
}