Skip to content

Commit 8ed403f

Browse files
authored
Merge pull request #146 from gngpp/feat
feat: Support observing daemon process memory usage
2 parents 3c5a79e + 4c3c926 commit 8ed403f

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tokio-util = "0.7.10"
3232
jsonwebtokens = "1.2.0"
3333
mimalloc = { version = "0.1.39", default-features = false, optional = true }
3434
daemonize = "0.5.0"
35+
sysinfo = { version = "0.30.7", default-features = false }
3536
tracing = "0.1.40"
3637
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
3738
tower-http = { version = "0.4.4", default-features = false, features = ["trace"]}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ Commands:
3636
run Run thunder
3737
start Start thunder daemon
3838
stop Stop thunder daemon
39-
status Show the Http server daemon process
4039
log Show the Http server daemon log
40+
ps Show the Http server daemon process
4141
help Print this message or the help of the given subcommand(s)
4242

4343
Options:
@@ -73,7 +73,7 @@ thunder start
7373
thunder stop
7474

7575
# 查看运行状态
76-
thunder status
76+
thunder ps
7777

7878
# 查看运行日志
7979
thunder log

src/daemon.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ pub fn check_root() {
2323
}
2424

2525
/// Get the pid of the daemon
26-
pub fn get_pid() -> Option<String> {
26+
pub fn get_pid() -> Option<i32> {
2727
if let Ok(data) = std::fs::read(PID_PATH) {
2828
let binding = String::from_utf8(data).expect("pid file is not utf8");
29-
return Some(binding.trim().to_string());
29+
return Some(binding.trim().parse().expect("pid file is not a number"));
3030
}
3131
None
3232
}
3333

3434
/// Start the daemon
3535
pub fn start() -> Result<()> {
3636
if let Some(pid) = get_pid() {
37-
println!("Thunder is already running with pid: {}", pid);
37+
println!("Thunder is already running with pid: {pid}");
3838
return Ok(());
3939
}
4040

@@ -81,7 +81,6 @@ pub fn stop() -> Result<()> {
8181
check_root();
8282

8383
if let Some(pid) = get_pid() {
84-
let pid = pid.parse::<i32>()?;
8584
for _ in 0..360 {
8685
if signal::kill(Pid::from_raw(pid), signal::SIGINT).is_err() {
8786
break;
@@ -96,9 +95,30 @@ pub fn stop() -> Result<()> {
9695

9796
/// Show the status of the daemon
9897
pub fn status() -> Result<()> {
98+
use sysinfo::System;
9999
match get_pid() {
100-
Some(pid) => println!("Thunder is running with pid: {}", pid),
101-
None => println!("Thunder is not running"),
100+
Some(pid) => {
101+
let mut sys = System::new();
102+
103+
// First we update all information of our `System` struct.
104+
sys.refresh_all();
105+
106+
// Display processes ID
107+
let process = sys
108+
.processes()
109+
.into_iter()
110+
.find(|(raw_pid, _)| raw_pid.as_u32().eq(&(pid as u32)))
111+
.ok_or_else(|| anyhow::anyhow!("openai is not running"))?;
112+
113+
println!("{:<6} {:<6} {:<6}", "PID", "CPU(%)", "MEM(MB)");
114+
println!(
115+
"{:<6} {:<6.1} {:<6.1}",
116+
process.0,
117+
process.1.cpu_usage(),
118+
(process.1.memory() as f64) / 1024.0 / 1024.0
119+
);
120+
}
121+
None => println!("openai is not running"),
102122
}
103123
Ok(())
104124
}

src/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ pub enum Commands {
3939
Start(ServeConfig),
4040
/// Stop thunder daemon
4141
Stop,
42-
/// Show the Http server daemon process
43-
Status,
4442
/// Show the Http server daemon log
4543
Log,
44+
/// Show the Http server daemon process
45+
PS,
4646
}
4747

4848
#[derive(Args, Clone)]
@@ -190,19 +190,21 @@ fn main() -> Result<()> {
190190
}
191191
Commands::Run(server_config) => {
192192
let install_config = InstallConfig::read_from_file()?;
193+
#[cfg(target_os = "linux")]
193194
before_action(&install_config)?;
194195
serve::Serve::new(server_config, install_config).run()?;
195196
}
196197
Commands::Start(server_config) => {
197198
let install_config = InstallConfig::read_from_file()?;
199+
#[cfg(target_os = "linux")]
198200
before_action(&install_config)?;
199201
daemon::start()?;
200202
serve::Serve::new(server_config, install_config).run()?;
201203
}
202204
Commands::Stop => {
203205
daemon::stop()?;
204206
}
205-
Commands::Status => {
207+
Commands::PS => {
206208
daemon::status()?;
207209
}
208210
Commands::Log => {
@@ -213,13 +215,11 @@ fn main() -> Result<()> {
213215
}
214216

215217
/// Running before the daemon starts, execute the following code
218+
#[cfg(target_os = "linux")]
216219
fn before_action(install_config: &InstallConfig) -> Result<()> {
217-
#[cfg(target_os = "linux")]
218220
use nix::mount::MsFlags;
219221

220-
#[cfg(target_os = "linux")]
221222
let _ = nix::mount::umount(&install_config.mount_bind_download_path);
222-
#[cfg(target_os = "linux")]
223223
if nix::mount::mount(
224224
Some(&install_config.download_path),
225225
&install_config.mount_bind_download_path,

0 commit comments

Comments
 (0)