WIP: choom
This commit is contained in:
parent
a030c07dde
commit
d66c2bcf45
10
Cargo.lock
generated
10
Cargo.lock
generated
@ -969,6 +969,7 @@ dependencies = [
|
|||||||
"tempfile",
|
"tempfile",
|
||||||
"textwrap",
|
"textwrap",
|
||||||
"uu_blockdev",
|
"uu_blockdev",
|
||||||
|
"uu_choom",
|
||||||
"uu_ctrlaltdel",
|
"uu_ctrlaltdel",
|
||||||
"uu_dmesg",
|
"uu_dmesg",
|
||||||
"uu_fsfreeze",
|
"uu_fsfreeze",
|
||||||
@ -994,6 +995,15 @@ dependencies = [
|
|||||||
"uucore",
|
"uucore",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uu_choom"
|
||||||
|
version = "0.0.1"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
"linux-raw-sys 0.9.2",
|
||||||
|
"uucore",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uu_ctrlaltdel"
|
name = "uu_ctrlaltdel"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
|
@ -27,6 +27,7 @@ uudoc = []
|
|||||||
|
|
||||||
feat_common_core = [
|
feat_common_core = [
|
||||||
"blockdev",
|
"blockdev",
|
||||||
|
"choom",
|
||||||
"ctrlaltdel",
|
"ctrlaltdel",
|
||||||
"dmesg",
|
"dmesg",
|
||||||
"fsfreeze",
|
"fsfreeze",
|
||||||
@ -72,6 +73,7 @@ uucore = { workspace = true }
|
|||||||
|
|
||||||
#
|
#
|
||||||
blockdev = { optional = true, version = "0.0.1", package = "uu_blockdev", path = "src/uu/blockdev" }
|
blockdev = { optional = true, version = "0.0.1", package = "uu_blockdev", path = "src/uu/blockdev" }
|
||||||
|
choom = { optional = true, version = "0.0.1", package = "uu_choom", path = "src/uu/choom" }
|
||||||
ctrlaltdel = { optional = true, version = "0.0.1", package = "uu_ctrlaltdel", path = "src/uu/ctrlaltdel" }
|
ctrlaltdel = { optional = true, version = "0.0.1", package = "uu_ctrlaltdel", path = "src/uu/ctrlaltdel" }
|
||||||
dmesg = { optional = true, version = "0.0.1", package = "uu_dmesg", path = "src/uu/dmesg" }
|
dmesg = { optional = true, version = "0.0.1", package = "uu_dmesg", path = "src/uu/dmesg" }
|
||||||
fsfreeze = { optional = true, version = "0.0.1", package = "uu_fsfreeze", path = "src/uu/fsfreeze" }
|
fsfreeze = { optional = true, version = "0.0.1", package = "uu_fsfreeze", path = "src/uu/fsfreeze" }
|
||||||
|
16
src/uu/choom/Cargo.toml
Normal file
16
src/uu/choom/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "uu_choom"
|
||||||
|
version = "0.0.1"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "src/choom.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "choom"
|
||||||
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = { workspace = true }
|
||||||
|
linux-raw-sys = { workspace = true }
|
||||||
|
uucore = { workspace = true }
|
9
src/uu/choom/choom.md
Normal file
9
src/uu/choom/choom.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# choom
|
||||||
|
|
||||||
|
```
|
||||||
|
choom [OPTION] -p pid
|
||||||
|
choom [OPTION] -n number -p pid
|
||||||
|
choom [OPTION] -n number [--] command [args...]]
|
||||||
|
```
|
||||||
|
|
||||||
|
Display and adjust OOM-killer score.
|
97
src/uu/choom/src/choom.rs
Normal file
97
src/uu/choom/src/choom.rs
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
// This file is part of the uutils util-linux package.
|
||||||
|
//
|
||||||
|
// For the full copyright and license information, please view the LICENSE
|
||||||
|
// file that was distributed with this source code.
|
||||||
|
|
||||||
|
use clap::{crate_version, Arg, ArgAction, ArgGroup, Command};
|
||||||
|
use std::io::Write;
|
||||||
|
use uucore::{error::UResult, format_usage, help_about, help_usage};
|
||||||
|
|
||||||
|
const ABOUT: &str = help_about!("choom.md");
|
||||||
|
const USAGE: &str = help_usage!("choom.md");
|
||||||
|
|
||||||
|
mod options {
|
||||||
|
pub const PID: &str = "pid";
|
||||||
|
pub const ADJUST: &str = "adjust";
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
#[uucore::main]
|
||||||
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
let matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?;
|
||||||
|
if let Some(adjust) = matches.get_one::<i32>(options::ADJUST) {
|
||||||
|
let pid = match matches.get_one::<u32>(options::PID) {
|
||||||
|
Some(pid) => *pid,
|
||||||
|
None => std::process::id(),
|
||||||
|
};
|
||||||
|
adjust_oom_score(pid, *adjust)?;
|
||||||
|
} else if let Some(pid) = matches.get_one::<u32>(options::PID) {
|
||||||
|
print_oom_score(*pid)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn adjust_oom_score(pid: u32, adjust: i32) -> UResult<()> {
|
||||||
|
let path = format!("/proc/{}/oom_score_adj", pid);
|
||||||
|
let mut file = std::fs::OpenOptions::new().write(true).open(path)?;
|
||||||
|
file.write_all(adjust.to_string().as_bytes())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_oom_score(pid: u32) -> UResult<()> {
|
||||||
|
let path = format!("/proc/{}/oom_score", pid);
|
||||||
|
let score = std::fs::read_to_string(path)?;
|
||||||
|
println!("pid {}'s current OOM score: {}", pid, score.trim());
|
||||||
|
let path = format!("/proc/{}/oom_score_adj", pid);
|
||||||
|
let score = std::fs::read_to_string(path)?;
|
||||||
|
println!(
|
||||||
|
"pid {}'s current OOM score adjust value: {}",
|
||||||
|
pid,
|
||||||
|
score.trim()
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn uu_app() -> Command {
|
||||||
|
Command::new(uucore::util_name())
|
||||||
|
.version(crate_version!())
|
||||||
|
.about(ABOUT)
|
||||||
|
.override_usage(format_usage(USAGE))
|
||||||
|
.infer_long_args(true)
|
||||||
|
.arg(
|
||||||
|
Arg::new(options::ADJUST)
|
||||||
|
.short('n')
|
||||||
|
.long(options::ADJUST)
|
||||||
|
.help("specify the adjust score value")
|
||||||
|
.action(ArgAction::Set)
|
||||||
|
.value_name("num")
|
||||||
|
.value_parser(clap::value_parser!(i32)),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new(options::PID)
|
||||||
|
.short('p')
|
||||||
|
.long(options::PID)
|
||||||
|
.help("process ID")
|
||||||
|
.action(ArgAction::Set)
|
||||||
|
.value_name("num")
|
||||||
|
.value_parser(clap::value_parser!(u32)),
|
||||||
|
)
|
||||||
|
.group(
|
||||||
|
ArgGroup::new("action")
|
||||||
|
.args([options::ADJUST, options::PID])
|
||||||
|
.required(true)
|
||||||
|
.multiple(true),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
#[uucore::main]
|
||||||
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
let _matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?;
|
||||||
|
|
||||||
|
Err(uucore::error::USimpleError::new(
|
||||||
|
1,
|
||||||
|
"`choom` is available only on Linux.",
|
||||||
|
))
|
||||||
|
}
|
1
src/uu/choom/src/main.rs
Normal file
1
src/uu/choom/src/main.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
uucore::bin!(uu_choom);
|
Loading…
x
Reference in New Issue
Block a user