fix win cpu monitor run once

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-07-24 14:17:09 +08:00
parent dc0f21298e
commit 02456280d4
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:
// 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);
});
}

View File

@ -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,

View File

@ -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

View File

@ -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);
});
}

View File

@ -84,6 +84,10 @@ pub fn core_main() -> Option<Vec<String>> {
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());

View File

@ -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();
}
})
}