Skip to content

Commit 2acf066

Browse files
committedMar 5, 2025··
Last commit did not include all the files, oops
1 parent 9959a4e commit 2acf066

File tree

4 files changed

+117
-87
lines changed

4 files changed

+117
-87
lines changed
 

‎src/utils/app.rs

+13-35
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use gtk::{
1212
};
1313
use lazy_regex::{Lazy, Regex, lazy_regex};
1414
use log::{debug, info, trace};
15-
use process_data::{Containerization, GpuIdentifier, ProcessData};
15+
use process_data::{Containerization, GpuIdentifier, GpuUsageStats, ProcessData};
1616

1717
use crate::i18n::i18n;
1818

1919
use super::{
20-
FiniteOr, boot_time,
20+
boot_time,
2121
process::{Process, ProcessAction},
2222
};
2323

@@ -548,16 +548,8 @@ impl AppsContext {
548548
_ => None,
549549
})
550550
.map(|(new, old, timestamp, timestamp_last)| {
551-
if new.nvidia {
552-
new.gfx as f32 / 100.0
553-
} else if old.gfx == 0 {
554-
0.0
555-
} else {
556-
((new.gfx.saturating_sub(old.gfx) as f32)
557-
/ (timestamp.saturating_sub(timestamp_last) as f32))
558-
.finite_or_default()
559-
/ 1_000_000.0
560-
}
551+
let time_delta = timestamp.saturating_sub(timestamp_last);
552+
new.gfx_fraction(old, time_delta).unwrap_or_default()
561553
})
562554
.sum::<f32>()
563555
.clamp(0.0, 1.0)
@@ -586,16 +578,8 @@ impl AppsContext {
586578
_ => None,
587579
})
588580
.map(|(new, old, timestamp, timestamp_last)| {
589-
if new.nvidia {
590-
new.enc as f32 / 100.0
591-
} else if old.enc == 0 {
592-
0.0
593-
} else {
594-
((new.enc.saturating_sub(old.enc) as f32)
595-
/ (timestamp.saturating_sub(timestamp_last) as f32))
596-
.finite_or_default()
597-
/ 1_000_000.0
598-
}
581+
let time_delta = timestamp.saturating_sub(timestamp_last);
582+
new.enc_fraction(old, time_delta).unwrap_or_default()
599583
})
600584
.sum::<f32>()
601585
.clamp(0.0, 1.0)
@@ -624,16 +608,8 @@ impl AppsContext {
624608
_ => None,
625609
})
626610
.map(|(new, old, timestamp, timestamp_last)| {
627-
if new.nvidia {
628-
new.dec as f32 / 100.0
629-
} else if old.dec == 0 {
630-
0.0
631-
} else {
632-
((new.dec.saturating_sub(old.dec) as f32)
633-
/ (timestamp.saturating_sub(timestamp_last) as f32))
634-
.finite_or_default()
635-
/ 1_000_000.0
636-
}
611+
let time_delta = timestamp.saturating_sub(timestamp_last);
612+
new.dec_fraction(old, time_delta).unwrap_or_default()
637613
})
638614
.sum::<f32>()
639615
.clamp(0.0, 1.0)
@@ -778,16 +754,18 @@ impl AppsContext {
778754

779755
// this is awkward: since AppsContext is the only object around that knows what GPUs have combined media
780756
// engines, it is here where we have to manipulate the GpuUsageStats objects with PciSlots of those GPUs
781-
// whose media engine is combined (e.g. no discrimination between enc and dec stats)
757+
// whose media engine is combined (i.e. no discrimination between enc and dec stats)
782758
process_data
783759
.gpu_usage_stats
784760
.iter_mut()
785761
.filter(|(pci_slot, _)| self.gpus_with_combined_media_engine.contains(pci_slot))
786762
.for_each(|(pci_slot, stats)| {
787763
trace!("Manually adjusting GPU stats of {} for {pci_slot} due to combined media engine", process_data.pid);
788764

789-
stats.dec = u64::max(stats.dec, stats.enc);
790-
stats.enc = u64::max(stats.dec, stats.enc);
765+
if let GpuUsageStats::AmdgpuStats { gfx_ns: _, enc_ns, dec_ns, mem_bytes: _ } = stats {
766+
*enc_ns = u64::max(*enc_ns, *dec_ns);
767+
*dec_ns = u64::max(*enc_ns, *dec_ns);
768+
}
791769
});
792770

793771
// refresh our old processes

‎src/utils/gpu/intel.rs

+88-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
use anyhow::{Result, bail};
22
use process_data::GpuIdentifier;
3+
use strum_macros::{Display, EnumString};
34

4-
use std::path::PathBuf;
5+
use std::{path::PathBuf, str::FromStr};
56

67
use crate::utils::pci::Device;
78

89
use super::GpuImpl;
910

10-
#[derive(Debug, Clone, Default)]
11+
pub const DRIVER_NAMES: &[&str] = &["i915", "xe"];
12+
13+
#[derive(Debug, Clone, Display, EnumString, PartialEq, PartialOrd, Eq, Ord)]
14+
#[strum(ascii_case_insensitive)]
15+
#[strum(serialize_all = "lowercase")]
16+
pub enum IntelGpuDriver {
17+
I915,
18+
Xe,
19+
#[strum(transparent)]
20+
#[strum(default)]
21+
Other(String),
22+
}
23+
24+
impl Default for IntelGpuDriver {
25+
fn default() -> Self {
26+
Self::Other(String::new())
27+
}
28+
}
29+
30+
#[derive(Debug, Clone)]
1131

1232
pub struct IntelGpu {
1333
pub device: Option<&'static Device>,
1434
pub gpu_identifier: GpuIdentifier,
15-
pub driver: String,
35+
pub driver: IntelGpuDriver,
1636
sysfs_path: PathBuf,
1737
first_hwmon_path: Option<PathBuf>,
1838
}
@@ -28,7 +48,7 @@ impl IntelGpu {
2848
Self {
2949
device,
3050
gpu_identifier,
31-
driver,
51+
driver: IntelGpuDriver::from_str(&driver).unwrap_or_default(),
3252
sysfs_path,
3353
first_hwmon_path,
3454
}
@@ -45,7 +65,7 @@ impl GpuImpl for IntelGpu {
4565
}
4666

4767
fn driver(&self) -> String {
48-
self.driver.clone()
68+
self.driver.to_string()
4969
}
5070

5171
fn sysfs_path(&self) -> PathBuf {
@@ -89,11 +109,21 @@ impl GpuImpl for IntelGpu {
89109
}
90110

91111
fn power_usage(&self) -> Result<f64> {
92-
self.hwmon_power_usage()
112+
match self.driver {
113+
IntelGpuDriver::Xe => {
114+
Ok(self.read_sysfs_int("tile0/gt0/freq0/cur_freq")? as f64 * 1_000_000.0)
115+
}
116+
_ => Ok(self.read_sysfs_int("gt_cur_freq_mhz")? as f64 * 1_000_000.0),
117+
}
93118
}
94119

95120
fn core_frequency(&self) -> Result<f64> {
96-
Ok(self.read_sysfs_int("gt_cur_freq_mhz")? as f64 * 1_000_000.0)
121+
match self.driver {
122+
IntelGpuDriver::Xe => {
123+
Ok(self.read_sysfs_int("tile0/gt0/freq0/cur_freq")? as f64 * 1_000_000.0)
124+
}
125+
_ => Ok(self.read_sysfs_int("gt_cur_freq_mhz")? as f64 * 1_000_000.0),
126+
}
97127
}
98128

99129
fn vram_frequency(&self) -> Result<f64> {
@@ -108,3 +138,54 @@ impl GpuImpl for IntelGpu {
108138
self.hwmon_power_cap_max()
109139
}
110140
}
141+
142+
#[cfg(test)]
143+
mod test {
144+
use std::str::FromStr;
145+
146+
use super::IntelGpuDriver;
147+
use pretty_assertions::assert_eq;
148+
149+
const I915_NAME: &str = "i915";
150+
const XE_NAME: &str = "xe";
151+
const OTHER_NAME: &str = "other_driver";
152+
153+
#[test]
154+
fn i915_driver_from_str() {
155+
let driver = IntelGpuDriver::from_str(I915_NAME).unwrap();
156+
157+
assert_eq!(driver, IntelGpuDriver::I915);
158+
}
159+
160+
#[test]
161+
fn xe_driver_from_str() {
162+
let driver = IntelGpuDriver::from_str(XE_NAME).unwrap();
163+
164+
assert_eq!(driver, IntelGpuDriver::Xe);
165+
}
166+
167+
#[test]
168+
fn other_driver_from_str() {
169+
let driver = IntelGpuDriver::from_str(OTHER_NAME).unwrap();
170+
171+
assert_eq!(driver, IntelGpuDriver::Other(OTHER_NAME.to_string()));
172+
}
173+
174+
#[test]
175+
fn i915_driver_to_str() {
176+
assert_eq!(IntelGpuDriver::I915.to_string(), I915_NAME);
177+
}
178+
179+
#[test]
180+
fn xe_driver_to_str() {
181+
assert_eq!(IntelGpuDriver::Xe.to_string(), XE_NAME);
182+
}
183+
184+
#[test]
185+
fn other_driver_to_str() {
186+
assert_eq!(
187+
IntelGpuDriver::Other(OTHER_NAME.to_string()).to_string(),
188+
OTHER_NAME
189+
);
190+
}
191+
}

‎src/utils/gpu/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ pub trait GpuImpl {
202202
fn hwmon_power_usage(&self) -> Result<f64> {
203203
Ok(self
204204
.read_hwmon_int("power1_average")
205-
.or_else(|_| self.read_hwmon_int("power1_input"))? as f64
205+
.or_else(|_| self.read_hwmon_int("power1_input"))
206+
.or_else(|_| self.read_hwmon_int("power1"))? as f64
206207
/ 1_000_000.0)
207208
}
208209

@@ -311,7 +312,7 @@ impl Gpu {
311312
)),
312313
"AMD",
313314
)
314-
} else if vid == VID_INTEL || driver == "i915" {
315+
} else if vid == VID_INTEL || intel::DRIVER_NAMES.contains(&driver.as_str()) {
315316
(
316317
Gpu::Intel(IntelGpu::new(
317318
device,

‎src/utils/process.rs

+13-43
Original file line numberDiff line numberDiff line change
@@ -399,20 +399,10 @@ impl Process {
399399
let mut returned_gpu_usage = 0.0;
400400
for (gpu, usage) in &self.data.gpu_usage_stats {
401401
if let Some(old_usage) = self.gpu_usage_stats_last.get(gpu) {
402-
let this_gpu_usage = if usage.nvidia {
403-
usage.gfx as f32 / 100.0
404-
} else if old_usage.gfx == 0 {
405-
0.0
406-
} else {
407-
((usage.gfx.saturating_sub(old_usage.gfx) as f32)
408-
/ (self.data.timestamp.saturating_sub(self.timestamp_last) as f32)
409-
.finite_or_default())
410-
/ 1_000_000.0
411-
};
412-
413-
if this_gpu_usage > returned_gpu_usage {
414-
returned_gpu_usage = this_gpu_usage;
415-
}
402+
let time_delta = self.data.timestamp.saturating_sub(self.timestamp_last);
403+
returned_gpu_usage += usage
404+
.gfx_fraction(old_usage, time_delta)
405+
.unwrap_or_default();
416406
}
417407
}
418408

@@ -424,20 +414,10 @@ impl Process {
424414
let mut returned_gpu_usage = 0.0;
425415
for (gpu, usage) in &self.data.gpu_usage_stats {
426416
if let Some(old_usage) = self.gpu_usage_stats_last.get(gpu) {
427-
let this_gpu_usage = if usage.nvidia {
428-
usage.enc as f32 / 100.0
429-
} else if old_usage.enc == 0 {
430-
0.0
431-
} else {
432-
((usage.enc.saturating_sub(old_usage.enc) as f32)
433-
/ (self.data.timestamp.saturating_sub(self.timestamp_last) as f32)
434-
.finite_or_default())
435-
/ 1_000_000.0
436-
};
437-
438-
if this_gpu_usage > returned_gpu_usage {
439-
returned_gpu_usage = this_gpu_usage;
440-
}
417+
let time_delta = self.data.timestamp.saturating_sub(self.timestamp_last);
418+
returned_gpu_usage += usage
419+
.enc_fraction(old_usage, time_delta)
420+
.unwrap_or_default();
441421
}
442422
}
443423

@@ -449,20 +429,10 @@ impl Process {
449429
let mut returned_gpu_usage = 0.0;
450430
for (gpu, usage) in &self.data.gpu_usage_stats {
451431
if let Some(old_usage) = self.gpu_usage_stats_last.get(gpu) {
452-
let this_gpu_usage = if usage.nvidia {
453-
usage.dec as f32 / 100.0
454-
} else if old_usage.dec == 0 {
455-
0.0
456-
} else {
457-
((usage.dec.saturating_sub(old_usage.dec) as f32)
458-
/ (self.data.timestamp.saturating_sub(self.timestamp_last) as f32)
459-
.finite_or_default())
460-
/ 1_000_000.0
461-
};
462-
463-
if this_gpu_usage > returned_gpu_usage {
464-
returned_gpu_usage = this_gpu_usage;
465-
}
432+
let time_delta = self.data.timestamp.saturating_sub(self.timestamp_last);
433+
returned_gpu_usage += usage
434+
.dec_fraction(old_usage, time_delta)
435+
.unwrap_or_default();
466436
}
467437
}
468438

@@ -474,7 +444,7 @@ impl Process {
474444
self.data
475445
.gpu_usage_stats
476446
.values()
477-
.map(|stats| stats.mem)
447+
.map(|stats| stats.mem().unwrap_or_default())
478448
.sum()
479449
}
480450

0 commit comments

Comments
 (0)
Please sign in to comment.