Merge pull request #5100 from 21pages/fix

fix win cpu monitor run once
This commit is contained in:
RustDesk 2023-07-24 15:40:35 +08:00 committed by GitHub
commit 5639ce82bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 23 deletions

View File

@ -42,7 +42,7 @@ impl Drop for RAIIPDHQuery {
} }
} }
pub unsafe fn start_cpu_performance_monitor() { pub fn start_cpu_performance_monitor() {
// Code from: // Code from:
// https://learn.microsoft.com/en-us/windows/win32/perfctrs/collecting-performance-data // https://learn.microsoft.com/en-us/windows/win32/perfctrs/collecting-performance-data
// https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhcollectquerydataex // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhcollectquerydataex
@ -50,7 +50,7 @@ pub unsafe fn start_cpu_performance_monitor() {
// https://aaron-margosis.medium.com/task-managers-cpu-numbers-are-all-but-meaningless-2d165b421e43 // https://aaron-margosis.medium.com/task-managers-cpu-numbers-are-all-but-meaningless-2d165b421e43
// Therefore we should compare with Precess Explorer rather than taskManager // Therefore we should compare with Precess Explorer rather than taskManager
std::thread::spawn(|| { let f = || unsafe {
// load avg or cpu usage, test with prime95. // load avg or cpu usage, test with prime95.
// Prefer cpu usage because we can get accurate value from Precess Explorer. // Prefer cpu usage because we can get accurate value from Precess Explorer.
// const COUNTER_PATH: &'static str = "\\System\\Processor Queue Length\0"; // const COUNTER_PATH: &'static str = "\\System\\Processor Queue Length\0";
@ -80,7 +80,7 @@ pub unsafe fn start_cpu_performance_monitor() {
let mut counter_value: PDH_FMT_COUNTERVALUE = std::mem::zeroed(); let mut counter_value: PDH_FMT_COUNTERVALUE = std::mem::zeroed();
let event = CreateEventA(std::ptr::null_mut(), FALSE, FALSE, std::ptr::null() as _); let event = CreateEventA(std::ptr::null_mut(), FALSE, FALSE, std::ptr::null() as _);
if event.is_null() { if event.is_null() {
log::error!("CreateEventA failed: 0x{:X}", ret); log::error!("CreateEventA failed");
return; return;
} }
let _event: RAIIHandle = RAIIHandle(event); let _event: RAIIHandle = RAIIHandle(event);
@ -126,6 +126,11 @@ pub unsafe fn start_cpu_performance_monitor() {
queue.push_back(counter_value.u.doubleValue().clone()); queue.push_back(counter_value.u.doubleValue().clone());
recent_valid.push_back(true); recent_valid.push_back(true);
} }
};
use std::sync::Once;
static ONCE: Once = Once::new();
ONCE.call_once(|| {
std::thread::spawn(f);
}); });
} }

View File

@ -2,7 +2,7 @@ use docopt::Docopt;
use hbb_common::env_logger::{init_from_env, Env, DEFAULT_FILTER_ENV}; use hbb_common::env_logger::{init_from_env, Env, DEFAULT_FILTER_ENV};
use scrap::{ use scrap::{
aom::{AomDecoder, AomEncoder, AomEncoderConfig}, aom::{AomDecoder, AomEncoder, AomEncoderConfig},
codec::{codec_thread_num, EncoderApi, EncoderCfg, Quality as Q}, codec::{EncoderApi, EncoderCfg, Quality as Q},
Capturer, Display, TraitCapturer, VpxDecoder, VpxDecoderConfig, VpxEncoder, VpxEncoderConfig, Capturer, Display, TraitCapturer, VpxDecoder, VpxDecoderConfig, VpxEncoder, VpxEncoderConfig,
VpxVideoCodecId::{self, *}, VpxVideoCodecId::{self, *},
STRIDE_ALIGN, STRIDE_ALIGN,
@ -202,6 +202,7 @@ mod hw {
RateControl::*, RateControl::*,
}; };
use scrap::{ use scrap::{
codec::codec_thread_num,
convert::{ convert::{
hw::{hw_bgra_to_i420, hw_bgra_to_nv12}, hw::{hw_bgra_to_i420, hw_bgra_to_nv12},
i420_to_bgra, i420_to_bgra,

View File

@ -514,10 +514,11 @@ pub fn base_bitrate(width: u32, height: u32) -> u32 {
pub fn codec_thread_num() -> usize { pub fn codec_thread_num() -> usize {
let max: usize = num_cpus::get(); let max: usize = num_cpus::get();
let mut res = 0; let mut res;
let info; let info;
#[cfg(windows)] #[cfg(windows)]
{ {
res = 0;
let percent = hbb_common::platform::windows::cpu_uage_one_minute(); let percent = hbb_common::platform::windows::cpu_uage_one_minute();
info = format!("cpu usage:{:?}", percent); info = format!("cpu usage:{:?}", percent);
if let Some(pecent) = percent { if let Some(pecent) = percent {
@ -545,7 +546,7 @@ pub fn codec_thread_num() -> usize {
None => true, None => true,
}; };
if log { if log {
log::info!("cpu num: {max}, {info}, codec thread: {res}"); log::info!("cpu num:{max}, {info}, codec thread:{res}");
*THREAD_LOG_TIME.lock().unwrap() = Some(Instant::now()); *THREAD_LOG_TIME.lock().unwrap() = Some(Instant::now());
} }
res res

View File

@ -364,8 +364,8 @@ pub fn check_config() {
pub fn check_config_process() { pub fn check_config_process() {
use hbb_common::sysinfo::{ProcessExt, System, SystemExt}; use hbb_common::sysinfo::{ProcessExt, System, SystemExt};
use std::sync::Once;
std::thread::spawn(move || { let f = || {
// Clear to avoid checking process errors // Clear to avoid checking process errors
// But when the program is just started, the configuration file has not been updated, and the new connection will read an empty configuration // But when the program is just started, the configuration file has not been updated, and the new connection will read an empty configuration
HwCodecConfig::clear(); HwCodecConfig::clear();
@ -393,7 +393,9 @@ pub fn check_config_process() {
allow_err!(child.kill()); allow_err!(child.kill());
std::thread::sleep(std::time::Duration::from_millis(30)); std::thread::sleep(std::time::Duration::from_millis(30));
match child.try_wait() { match child.try_wait() {
Ok(Some(status)) => log::info!("Check hwcodec config, exit with: {status}"), Ok(Some(status)) => {
log::info!("Check hwcodec config, exit with: {status}")
}
Ok(None) => { Ok(None) => {
log::info!( log::info!(
"Check hwcodec config, status not ready yet, let's really wait" "Check hwcodec config, status not ready yet, let's really wait"
@ -409,5 +411,9 @@ pub fn check_config_process() {
} }
} }
}; };
};
static ONCE: Once = Once::new();
ONCE.call_once(|| {
std::thread::spawn(f);
}); });
} }

View File

@ -84,6 +84,10 @@ pub fn core_main() -> Option<Vec<String>> {
std::env::remove_var(k); std::env::remove_var(k);
} }
} }
#[cfg(windows)]
if args.contains(&"--connect".to_string()) {
hbb_common::platform::windows::start_cpu_performance_monitor();
}
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
if _is_flutter_invoke_new_connection { if _is_flutter_invoke_new_connection {
return core_main_invoke_new_connection(std::env::args()); return core_main_invoke_new_connection(std::env::args());

View File

@ -362,7 +362,10 @@ pub async fn start_server(is_server: bool) {
log::info!("DISPLAY={:?}", std::env::var("DISPLAY")); log::info!("DISPLAY={:?}", std::env::var("DISPLAY"));
log::info!("XAUTHORITY={:?}", std::env::var("XAUTHORITY")); log::info!("XAUTHORITY={:?}", std::env::var("XAUTHORITY"));
} }
call_once_each_process(); #[cfg(feature = "hwcodec")]
scrap::hwcodec::check_config_process();
#[cfg(windows)]
hbb_common::platform::windows::start_cpu_performance_monitor();
if is_server { if is_server {
crate::common::set_server_running(true); crate::common::set_server_running(true);
@ -523,16 +526,3 @@ async fn sync_and_watch_config_dir() {
} }
log::warn!("skipped config sync"); log::warn!("skipped config sync");
} }
fn call_once_each_process() {
use std::sync::Once;
static ONCE: Once = Once::new();
ONCE.call_once(|| {
#[cfg(feature = "hwcodec")]
scrap::hwcodec::check_config_process();
#[cfg(windows)]
unsafe {
hbb_common::platform::windows::start_cpu_performance_monitor();
}
})
}