optimize runtime performance of macOS --service

This commit is contained in:
rustdesk 2024-04-22 21:31:21 +08:00
parent 4f47d4482b
commit 4c62d8c1b2

View File

@ -485,21 +485,31 @@ pub fn lock_screen() {
pub fn start_os_service() { pub fn start_os_service() {
crate::platform::macos::hide_dock(); crate::platform::macos::hide_dock();
let exe = std::env::current_exe().unwrap_or_default();
log::info!("Username: {}", crate::username()); log::info!("Username: {}", crate::username());
log::info!("Startime: {:?}", get_server_start_time()); let mut sys = System::new();
sys.refresh_processes_specifics(ProcessRefreshKind::new());
let path =
std::fs::canonicalize(std::env::current_exe().unwrap_or_default()).unwrap_or_default();
let my_start_time = sys
.process((std::process::id() as usize).into())
.map(|p| p.start_time())
.unwrap_or_default() as i64;
log::info!(
"Startime: {my_start_time} vs {:?}",
get_server_start_time(&mut sys, &path)
);
std::thread::spawn(move || loop { std::thread::spawn(move || loop {
loop { loop {
std::thread::sleep(std::time::Duration::from_secs(1)); std::thread::sleep(std::time::Duration::from_secs(1));
let Some(start_time) = get_server_start_time() else { let Some(start_time) = get_server_start_time(&mut sys, &path) else {
continue; continue;
}; };
if start_time.0 <= start_time.1 {
if my_start_time <= start_time {
// I tried add delegate (using tao and with its main loop0, but it works in normal mode, but not work as daemon // I tried add delegate (using tao and with its main loop0, but it works in normal mode, but not work as daemon
log::info!( log::info!(
"Agent start later, {:?}, will restart --service to make delegate work", "Agent start later, {my_start_time} vs {start_time}, will restart --service to make delegate work",
start_time
); );
std::process::exit(0); std::process::exit(0);
} }
@ -604,36 +614,25 @@ pub fn hide_dock() {
} }
} }
fn get_server_start_time() -> Option<(i64, i64)> { use hbb_common::sysinfo::{ProcessRefreshKind, System};
use hbb_common::sysinfo::System; #[inline]
let mut sys = System::new(); fn get_server_start_time(sys: &mut System, path: &PathBuf) -> Option<i64> {
sys.refresh_processes(); sys.refresh_processes_specifics(ProcessRefreshKind::new());
let mut path = std::env::current_exe().unwrap_or_default();
if let Ok(linked) = path.read_link() {
path = linked;
}
let path = path.to_string_lossy().to_lowercase();
let Some(my_start_time) = sys
.process((std::process::id() as usize).into())
.map(|p| p.start_time())
else {
return None;
};
for (_, p) in sys.processes() { for (_, p) in sys.processes() {
let mut cur_path = p.exe().to_path_buf(); let cmd = p.cmd();
if let Ok(linked) = cur_path.read_link() { if cmd.len() <= 1 {
cur_path = linked;
}
if cur_path.to_string_lossy().to_lowercase() != path {
continue; continue;
} }
if p.pid().as_u32() == std::process::id() { if &cmd[1] != "--server" {
continue; continue;
} }
let parg = if p.cmd().len() <= 1 { "" } else { &p.cmd()[1] }; let Ok(cur) = std::fs::canonicalize(p.exe()) else {
if parg == "--server" { continue;
return Some((my_start_time as _, p.start_time() as _)); };
if &cur != path {
continue;
} }
return Some(p.start_time() as _);
} }
None None
} }