Skip to content

Commit

Permalink
fix: better cpu alignment and layout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
graelo committed Aug 9, 2023
1 parent 1d0a463 commit 8dd0d71
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 59 deletions.
34 changes: 17 additions & 17 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,30 @@ impl Metrics {
)));
}

let mut iterator = sysinfo_metrics.iter();
// For sysinfo cpu metrics, create a hashmap of cpu ID -> active ratio. This is necessary
// because the order of the CPUs is not guaranteed to be the same as ours, especially on
// Ultra chips.
let sysinfo_metrics = sysinfo_metrics
.iter()
.map(|m| (m.id, m.active_ratio))
.collect::<std::collections::HashMap<_, _>>();

// let mut iterator = sysinfo_metrics.iter();

for e_cluster in &mut self.e_clusters {
for cpu in &mut e_cluster.cpus {
let update = iterator.next().unwrap();
if cpu.id != update.id {
return Err(Error::MisalignedCpuId(format!(
"CPU id misalignment: {} != {}",
cpu.id, update.id
)));
}
cpu.active_ratio = update.active_ratio as f64;
let sysinfo_active_ratio = sysinfo_metrics.get(&cpu.id).ok_or_else(|| {
Error::MisalignedCpuId(format!("CPU id not found: {}", cpu.id))
})?;
cpu.active_ratio = *sysinfo_active_ratio as f64;
}
}
for p_cluster in &mut self.p_clusters {
for cpu in &mut p_cluster.cpus {
let update = iterator.next().unwrap();
if cpu.id != update.id {
return Err(Error::MisalignedCpuId(format!(
"CPU id misalignment: {} != {}",
cpu.id, update.id
)));
}
cpu.active_ratio = update.active_ratio as f64;
let update_active_ratio = sysinfo_metrics.get(&cpu.id).ok_or_else(|| {
Error::MisalignedCpuId(format!("CPU id not found: {}", cpu.id))
})?;
cpu.active_ratio = *update_active_ratio as f64;
}
}

Expand Down
103 changes: 61 additions & 42 deletions src/ui/tab_overview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,51 +180,70 @@ fn draw_cpu_clusters_usage_block<B>(

let mut clu_area_iter = cpu_cluster_chunks.iter();

// TODO: refactor this.
// Draw the metrics for the Efficiency cluster (or clusters).
let area = clu_area_iter.next().unwrap();
if metrics.e_clusters.len() == 1 {
draw_cluster_overall_metrics(
f,
&metrics.e_clusters[0],
history,
accent_color,
gauge_bg_color,
*area,
);
} else {
draw_cluster_pair_overall_metrics(
f,
&metrics.e_clusters[0],
&metrics.e_clusters[1],
history,
accent_color,
gauge_bg_color,
*area,
);
for clu_slice in metrics.e_clusters.chunks(2) {
let area = clu_area_iter.next().unwrap();

match clu_slice.len() {
1 => {
let cluster = &clu_slice[0];
draw_cluster_overall_metrics(
f,
cluster,
history,
accent_color,
gauge_bg_color,
*area,
);
}
2 => {
let (left_cluster, right_cluster) = (&clu_slice[0], &clu_slice[1]);
draw_cluster_pair_overall_metrics(
f,
left_cluster,
right_cluster,
history,
accent_color,
gauge_bg_color,
*area,
);
}
_ => unreachable!(),
}
}

// Draw the metrics for the Performance cluster (or clusters).
let area = clu_area_iter.next().unwrap();
if metrics.p_clusters.len() == 1 {
draw_cluster_overall_metrics(
f,
&metrics.p_clusters[0],
history,
accent_color,
gauge_bg_color,
*area,
);
} else {
draw_cluster_pair_overall_metrics(
f,
&metrics.p_clusters[0],
&metrics.p_clusters[1],
history,
accent_color,
gauge_bg_color,
*area,
);
// Yes this is duplicate code, but the alternative is to have a function with many arguments
// which is just used here.
for clu_slice in metrics.p_clusters.chunks(2) {
let area = clu_area_iter.next().unwrap();

match clu_slice.len() {
1 => {
let cluster = &clu_slice[0];
draw_cluster_overall_metrics(
f,
cluster,
history,
accent_color,
gauge_bg_color,
*area,
);
}
2 => {
let (left_cluster, right_cluster) = (&clu_slice[0], &clu_slice[1]);
draw_cluster_pair_overall_metrics(
f,
left_cluster,
right_cluster,
history,
accent_color,
gauge_bg_color,
*area,
);
}
_ => unreachable!(),
}
}
}

Expand Down Expand Up @@ -536,7 +555,7 @@ fn draw_thermal_pressure_block<B>(
f.render_widget(paragraph, area);
}

/// Compute the number of blocks for a given cluster.
/// Compute the number of blocks for a given set of clusters.
fn num_blocks_for(count: usize) -> usize {
(count as f32 / 2.0).ceil() as usize
}

0 comments on commit 8dd0d71

Please sign in to comment.