diff --git a/libs/hbb_common/src/platform/windows.rs b/libs/hbb_common/src/platform/windows.rs index 250ca8d3e..26cd907ab 100644 --- a/libs/hbb_common/src/platform/windows.rs +++ b/libs/hbb_common/src/platform/windows.rs @@ -42,7 +42,7 @@ impl Drop for RAIIPDHQuery { } } -pub unsafe fn start_cpu_performance_monitor() { +pub fn start_cpu_performance_monitor() { // Code from: // 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 @@ -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 // Therefore we should compare with Precess Explorer rather than taskManager - std::thread::spawn(|| { + let f = || unsafe { // load avg or cpu usage, test with prime95. // Prefer cpu usage because we can get accurate value from Precess Explorer. // 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 event = CreateEventA(std::ptr::null_mut(), FALSE, FALSE, std::ptr::null() as _); if event.is_null() { - log::error!("CreateEventA failed: 0x{:X}", ret); + log::error!("CreateEventA failed"); return; } let _event: RAIIHandle = RAIIHandle(event); @@ -126,6 +126,11 @@ pub unsafe fn start_cpu_performance_monitor() { queue.push_back(counter_value.u.doubleValue().clone()); recent_valid.push_back(true); } + }; + use std::sync::Once; + static ONCE: Once = Once::new(); + ONCE.call_once(|| { + std::thread::spawn(f); }); } diff --git a/libs/scrap/examples/benchmark.rs b/libs/scrap/examples/benchmark.rs index 18e636d0c..f8f2e9436 100644 --- a/libs/scrap/examples/benchmark.rs +++ b/libs/scrap/examples/benchmark.rs @@ -2,7 +2,7 @@ use docopt::Docopt; use hbb_common::env_logger::{init_from_env, Env, DEFAULT_FILTER_ENV}; use scrap::{ 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, VpxVideoCodecId::{self, *}, STRIDE_ALIGN, @@ -202,6 +202,7 @@ mod hw { RateControl::*, }; use scrap::{ + codec::codec_thread_num, convert::{ hw::{hw_bgra_to_i420, hw_bgra_to_nv12}, i420_to_bgra, diff --git a/libs/scrap/src/common/codec.rs b/libs/scrap/src/common/codec.rs index b6847dba0..84eb1c43e 100644 --- a/libs/scrap/src/common/codec.rs +++ b/libs/scrap/src/common/codec.rs @@ -514,10 +514,11 @@ pub fn base_bitrate(width: u32, height: u32) -> u32 { pub fn codec_thread_num() -> usize { let max: usize = num_cpus::get(); - let mut res = 0; + let mut res; let info; #[cfg(windows)] { + res = 0; let percent = hbb_common::platform::windows::cpu_uage_one_minute(); info = format!("cpu usage:{:?}", percent); if let Some(pecent) = percent { @@ -545,7 +546,7 @@ pub fn codec_thread_num() -> usize { None => true, }; 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()); } res diff --git a/libs/scrap/src/common/hwcodec.rs b/libs/scrap/src/common/hwcodec.rs index 94c30447e..f84f3bb7f 100644 --- a/libs/scrap/src/common/hwcodec.rs +++ b/libs/scrap/src/common/hwcodec.rs @@ -364,8 +364,8 @@ pub fn check_config() { pub fn check_config_process() { use hbb_common::sysinfo::{ProcessExt, System, SystemExt}; - - std::thread::spawn(move || { + use std::sync::Once; + let f = || { // 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 HwCodecConfig::clear(); @@ -393,7 +393,9 @@ pub fn check_config_process() { allow_err!(child.kill()); std::thread::sleep(std::time::Duration::from_millis(30)); 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) => { log::info!( "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); }); } diff --git a/src/core_main.rs b/src/core_main.rs index cc0a4834f..2644fe157 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -84,6 +84,10 @@ pub fn core_main() -> Option> { std::env::remove_var(k); } } + #[cfg(windows)] + if args.contains(&"--connect".to_string()) { + hbb_common::platform::windows::start_cpu_performance_monitor(); + } #[cfg(feature = "flutter")] if _is_flutter_invoke_new_connection { return core_main_invoke_new_connection(std::env::args()); diff --git a/src/server.rs b/src/server.rs index a0e842d42..c296e60be 100644 --- a/src/server.rs +++ b/src/server.rs @@ -362,7 +362,10 @@ pub async fn start_server(is_server: bool) { log::info!("DISPLAY={:?}", std::env::var("DISPLAY")); 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 { crate::common::set_server_running(true); @@ -523,16 +526,3 @@ async fn sync_and_watch_config_dir() { } 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(); - } - }) -}