feat: update the cpu graph async

This commit is contained in:
electria 2026-08-15 14:38:06 -07:00
commit 2314f67349
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
3 changed files with 72 additions and 32 deletions

41
Cargo.lock generated
View file

@ -139,6 +139,17 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "async-fs"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5"
dependencies = [
"async-lock",
"blocking",
"futures-lite",
]
[[package]] [[package]]
name = "async-io" name = "async-io"
version = "2.6.0" version = "2.6.0"
@ -168,6 +179,17 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
] ]
[[package]]
name = "async-net"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7"
dependencies = [
"async-io",
"blocking",
"futures-lite",
]
[[package]] [[package]]
name = "async-process" name = "async-process"
version = "2.5.0" version = "2.5.0"
@ -1211,6 +1233,7 @@ dependencies = [
"iced_core", "iced_core",
"log", "log",
"rustc-hash 2.1.3", "rustc-hash 2.1.3",
"smol",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"wasmtimer", "wasmtimer",
] ]
@ -1357,6 +1380,7 @@ name = "itop"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"iced", "iced",
"smol",
"sysinfo", "sysinfo",
] ]
@ -2740,6 +2764,23 @@ dependencies = [
"wayland-backend", "wayland-backend",
] ]
[[package]]
name = "smol"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a33bd3e260892199c3ccfc487c88b2da2265080acb316cd920da72fdfd7c599f"
dependencies = [
"async-channel",
"async-executor",
"async-fs",
"async-io",
"async-lock",
"async-net",
"async-process",
"blocking",
"futures-lite",
]
[[package]] [[package]]
name = "smol_str" name = "smol_str"
version = "0.2.2" version = "0.2.2"

View file

@ -5,8 +5,9 @@ edition = "2024"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
[dependencies] [dependencies]
smol = "2.0.2"
sysinfo = "0.39.6" sysinfo = "0.39.6"
[dependencies.iced] [dependencies.iced]
version = "0.14.0" version = "0.14.0"
features = [ "canvas" ] features = [ "canvas", "smol" ]

View file

@ -1,48 +1,51 @@
use std::time::Instant;
use iced::{ use iced::{
Color, Element, Length, Renderer, Subscription, Task, Theme, application, theme, widget, window, Color, Element, Length, Renderer, Task, Theme, application, futures::SinkExt, stream::channel,
theme, widget,
}; };
use smol::Timer;
use crate::app::cpu_graph::CpuGraph; use crate::app::cpu_graph::CpuGraph;
mod cpu_graph; mod cpu_graph;
enum Message { enum Message {
Frame(Instant), Cpu(f32),
} }
struct State { struct State {
sys: sysinfo::System,
refreshes: sysinfo::RefreshKind,
last_refresh: Instant,
cpu_graph: CpuGraph, cpu_graph: CpuGraph,
} }
impl State { impl State {
fn new() -> Self { fn new() -> (Self, Task<Message>) {
(
Self { Self {
sys: sysinfo::System::new(),
refreshes: sysinfo::RefreshKind::nothing()
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage()),
cpu_graph: Default::default(), cpu_graph: Default::default(),
last_refresh: Instant::now(), },
Task::run(
channel(1, async |mut sender| {
let mut sys = sysinfo::System::new();
let refreshes = sysinfo::RefreshKind::nothing()
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage());
sys.refresh_specifics(refreshes);
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
loop {
sys.refresh_specifics(refreshes);
Timer::after(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL).await;
sender.send(sys.global_cpu_usage()).await.unwrap();
} }
}),
Message::Cpu,
),
)
} }
fn update(&mut self, message: Message) -> Task<Message> { fn update(&mut self, message: Message) -> Task<Message> {
match message { match message {
Message::Frame(instant) => { Message::Cpu(value) => {
if instant.duration_since(self.last_refresh) >= sysinfo::MINIMUM_CPU_UPDATE_INTERVAL self.cpu_graph.update(value);
{
self.sys.refresh_specifics(self.refreshes);
self.cpu_graph.update(self.sys.global_cpu_usage());
self.cpu_graph.cache.clear(); self.cpu_graph.cache.clear();
self.last_refresh = instant;
}
} }
} }
@ -54,15 +57,10 @@ impl State {
.height(Length::Fill) .height(Length::Fill)
.into() .into()
} }
fn subscription(&self) -> Subscription<Message> {
window::frames().map(Message::Frame)
}
} }
pub fn run() -> Result<(), impl std::error::Error> { pub fn run() -> Result<(), impl std::error::Error> {
application(State::new, State::update, State::view) application(State::new, State::update, State::view)
.subscription(State::subscription)
.theme(theme::Theme::custom( .theme(theme::Theme::custom(
"high-contrast-dark", "high-contrast-dark",
theme::Palette { theme::Palette {