libps_mem/
lib.rs

1/*!
2The process memory size listing.
3
4`ps-mem` command is listing all process memory size.
5
6# Feature
7
8- minimum support rustc 1.65.0 (897e37553 2022-11-02)
9
10# Command help
11
12```text
13ps-mem --help
14```
15
16```text
17Usage:
18  ps-mem [options]
19  ps-mem [options] <command> {<arg1> <arg2> ...}
20
21print processes memory by sort,
22or print one processe memory
23
24Options:
25  -a, --all             all pid (include kernel threads)
26  --sort <order>        sort by <order>: rss|swap|total
27  --pid <number>        output only selected pid
28  --sleep <number>      sleep <number> milli second
29  -l, --cmdline         view command line
30
31  -H, --help        display this help and exit
32  -V, --version     display version information and exit
33  -X <x-options>    x options. try -X help
34
35Examples:
36  Show all prosesses memory:
37    ps-mem --all
38  Show one prosess memory:
39    ps-mem --pid 1234
40  Invoke and show one prosess memory:
41    ps-mem -- find / -type f
42```
43
44# Quick install
45
461. you can install this into cargo bin path:
47
48```text
49cargo install ps-mem
50```
51
522. you can build debian package:
53
54```text
55cargo deb
56```
57
58and install **.deb** into your local repository of debian package.
59
60
61# Examples
62
63## Example 1: simple
64
65```text
66sudo ps-mem
67```
68
69## Example 2: the largest finder
70
71you can see a largest memory process.
72
73```text
74sudo ps-mem --sort=total | tail
75```
76
77or
78
79```text
80sudo ps-mem --sort=total | aki-unbody -t 10
81```
82
83## Example 3: show one process memory
84
85You see the process memory size of that pid is 1234.
86
87```text
88ps-mem --pid 1234
89```
90
91## Example 4: show invoked one prosess memory
92
93You see the process memory size of the invoked find command.
94
95```text
96ps-mem find / -type f
97```
98
99*/
100
101#[macro_use]
102extern crate anyhow;
103
104pub mod conf;
105mod run;
106mod util;
107
108use flood_tide::HelpVersion;
109use runnel::RunnelIoe;
110
111const TRY_HELP_MSG: &str = "Try --help for help.";
112
113/// execute ps-mem
114///
115/// params:
116///   - sioe: stream in/out/err
117///   - program: program name. etc. "ps-mem"
118///   - args: parameter arguments.
119///
120/// return:
121///   - ok: ()
122///   - err: anyhow
123///
124pub fn execute<I, S>(sioe: &RunnelIoe, prog_name: &str, args: I) -> anyhow::Result<()>
125where
126    I: IntoIterator<Item = S>,
127    S: AsRef<std::ffi::OsStr>,
128{
129    let args: Vec<String> = args
130        .into_iter()
131        .map(|s| s.as_ref().to_string_lossy().into_owned())
132        .collect();
133    let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
134    match conf::parse_cmdopts(prog_name, &args_str) {
135        Ok(conf) => run::run(sioe, &conf),
136        Err(errs) => {
137            if let Some(err) = errs.iter().find(|e| e.is_help() || e.is_version()) {
138                sioe.pg_out().write_line(err.to_string())?;
139                Ok(())
140            } else {
141                Err(anyhow!("{errs}\n{TRY_HELP_MSG}"))
142            }
143        }
144    }
145}