Show available RAM and measured CPU usage for devices
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//! uninstall commands; registry entries are evidence, not verified MSI state.
|
||||
use anyhow::{Result, ensure};
|
||||
use chrono::Utc;
|
||||
use rmm_domain::inventory_document::DeviceType;
|
||||
use rmm_domain::inventory_document::{CpuTimes, DeviceType};
|
||||
use serde_json::{Value, json};
|
||||
use std::{
|
||||
mem::{size_of, zeroed},
|
||||
@@ -10,8 +10,12 @@ use std::{
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use windows_sys::Win32::{
|
||||
Foundation::{ERROR_FILE_NOT_FOUND, ERROR_NO_MORE_ITEMS, ERROR_SUCCESS},
|
||||
System::{Registry::*, SystemInformation::*},
|
||||
Foundation::{ERROR_FILE_NOT_FOUND, ERROR_NO_MORE_ITEMS, ERROR_SUCCESS, FILETIME},
|
||||
System::{
|
||||
Registry::*,
|
||||
SystemInformation::*,
|
||||
Threading::{GetActiveProcessorGroupCount, GetSystemTimes},
|
||||
},
|
||||
};
|
||||
|
||||
struct Registry(HKEY);
|
||||
@@ -141,21 +145,53 @@ fn machine_type() -> Option<DeviceType> {
|
||||
)
|
||||
}
|
||||
|
||||
fn hardware() -> Result<Value> {
|
||||
let mut system: SYSTEM_INFO = unsafe { zeroed() };
|
||||
unsafe {
|
||||
GetNativeSystemInfo(&mut system);
|
||||
}
|
||||
fn memory_status() -> Result<MEMORYSTATUSEX> {
|
||||
let mut memory: MEMORYSTATUSEX = unsafe { zeroed() };
|
||||
memory.dwLength = size_of::<MEMORYSTATUSEX>() as u32;
|
||||
ensure!(
|
||||
unsafe { GlobalMemoryStatusEx(&mut memory) } != 0
|
||||
&& system.dwNumberOfProcessors > 0
|
||||
&& memory.ullTotalPhys > 0
|
||||
&& memory.ullAvailPhys <= memory.ullTotalPhys
|
||||
&& memory.dwMemoryLoad <= 100,
|
||||
"NATIVE_HARDWARE_QUERY"
|
||||
);
|
||||
Ok(memory)
|
||||
}
|
||||
|
||||
pub fn physical_memory() -> Option<(u64, u64)> {
|
||||
let memory = memory_status().ok()?;
|
||||
Some((memory.ullAvailPhys, memory.ullTotalPhys))
|
||||
}
|
||||
|
||||
pub fn cpu_times() -> Option<CpuTimes> {
|
||||
// GetSystemTimes on a multi-group machine only reports the caller's group.
|
||||
if unsafe { GetActiveProcessorGroupCount() } != 1 {
|
||||
return None;
|
||||
}
|
||||
let (mut idle, mut kernel, mut user) = (
|
||||
FILETIME::default(),
|
||||
FILETIME::default(),
|
||||
FILETIME::default(),
|
||||
);
|
||||
if unsafe { GetSystemTimes(&mut idle, &mut kernel, &mut user) } == 0 {
|
||||
return None;
|
||||
}
|
||||
let ticks =
|
||||
|time: FILETIME| (u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime);
|
||||
// Windows includes idle time in the kernel counter.
|
||||
Some(CpuTimes {
|
||||
total: ticks(kernel).checked_add(ticks(user))?,
|
||||
idle: ticks(idle),
|
||||
})
|
||||
}
|
||||
|
||||
fn hardware() -> Result<Value> {
|
||||
let mut system: SYSTEM_INFO = unsafe { zeroed() };
|
||||
unsafe {
|
||||
GetNativeSystemInfo(&mut system);
|
||||
}
|
||||
ensure!(system.dwNumberOfProcessors > 0, "NATIVE_HARDWARE_QUERY");
|
||||
let memory = memory_status()?;
|
||||
let cpu_model = open(
|
||||
HKEY_LOCAL_MACHINE,
|
||||
r"HARDWARE\DESCRIPTION\System\CentralProcessor\0",
|
||||
|
||||
Reference in New Issue
Block a user