1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Overview tab.

use ratatui::{
    backend::Backend,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Style},
    symbols,
    text::{Line, Span, Text},
    widgets::{Block, Borders, Gauge, Paragraph, Sparkline},
    Frame,
};

use crate::{
    app::{App, History},
    metrics,
    modules::soc::SocInfo,
    units,
};

const CLUSTER_SPACING: u16 = 1; // Space between CPU blocks.
const SPARKLINE_HEIGHT: u16 = 3;
const SPARKLINE_MAX_OVERSHOOT: f32 = 1.05; // Prevent sparklines from touching gauges.
const GAUGE_HEIGHT: u16 = 2;
const PKG_TEXT_HEIGHT: u16 = 1;

/// Draw the Overview tab.
///
/// Pumas v0.0.8                                                                      Apple M2 Max (cores: 4E+8P+38GPU)
/// ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
/// │ Overview │ CPU │ GPU │ SoC                                                                                       │
/// └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
/// ┌ CPU Clusters: 󱐋 87.70 mW (peak: 570.75 mW)───────────────────────────────────────────────────────────────────────┐
/// │E-Cluster: 5.0 % @ 1029 MHz (peak: 22.2 %)                                                                        │
/// │------------------------------------------------------- 5% -------------------------------------------------------│
/// │                                                                                                                  │
/// │                                                                                                                  │
/// │   ▁▁▂   ▂▂▁ ▁▃▁ ▁ ▁  ▁  ▁   ▁             ▂▁        ▁ ▁▁▂▂▂▂▁▁▂▁ ▁▁▂▂▂▂▂▄▂▁▁▁▁▁ ▁▁▁▂▂▂▃▃▃▁▁▅▁▁ ▁▁▁               │
/// │                                                                                                                  │
/// │P0-Cluster: 1.5 % @ 1634 MHz (peak: 10.4 %)                P1-Cluster: 0.0 % @ 1690 MHz (peak: 1.0 %)             │
/// │-------------------------- 1% ---------------------------  ------------------------- 0% --------------------------│
/// │                                                                                                                  │
/// │                                                                                                                  │
/// │ ▂▂▃        ▂▁▁ ▃    ▄    ▁      ▁                                                                                │
/// └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
/// ┌ GPU & ANE ───────────────────────────────────────────────────────────────────────────────────────────────────────┐
/// │GPU: 1.3 % @ 444 MHz 󱐋 13.80 mW (peak: 16.5 % 󱐋 422.55 mW  ANE: 0.0 % 󱐋 0.00 W (peak: 0.0 % 󱐋 0.00 W)             │
/// │-------------------------- 1% ---------------------------  ------------------------- 0% --------------------------│
/// │                                                                                                                  │
/// │                                                                                                                  │
/// │                              ▁              ▂▃ ▁▃                                                                │
/// └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
/// ┌ Package ─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
/// │CPU+GPU+ANE: 󱐋 101.49 mW (peak: 629.25 mW)                                                                        │
/// │                                                                                                                  │
/// │                                                                                                                  │
/// │▃▃▂▂▃▂▂▁▂▂▅▃▃▂▃▃▂▃▃▄▃▁▂▃▁▁▁▂▃▃▁█▅▁▂▂▁▂▂▃▂▂▁                                                                       │
/// └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
/// ┌ Thermals ────────────────────────────────────────────────────────────────────────────────────────────────────────┐
/// │Thermal Pressure: Nominal                                                                                         │
/// └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
///
pub(crate) fn draw_overview_tab<B>(f: &mut Frame<B>, app: &App, area: Rect)
where
    B: Backend,
{
    let metrics = match &app.metrics {
        Some(metrics) => metrics,
        None => return,
    };

    // Number of horizontal blocks for the CPU clusters.
    let num_clusters_blocks = (num_blocks_for(metrics.e_clusters.len())
        + num_blocks_for(metrics.p_clusters.len())) as u16;

    let cls_block_height = GAUGE_HEIGHT + SPARKLINE_HEIGHT;
    let cpu_block_height =
        cls_block_height * num_clusters_blocks + (num_clusters_blocks - 1) * CLUSTER_SPACING;
    let gpu_block_height = GAUGE_HEIGHT + SPARKLINE_HEIGHT;
    let pkg_block_height = PKG_TEXT_HEIGHT + SPARKLINE_HEIGHT;

    let vertical_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(2 + cpu_block_height), // Borders & CPU clusters blocks
            Constraint::Length(2 + gpu_block_height), // Borders & GPU ANE block
            Constraint::Length(2 + pkg_block_height), // Borders & Package block
            Constraint::Min(0),
        ])
        .split(area);
    let cpu_area = vertical_chunks[0];
    let gpu_area = vertical_chunks[1];
    let pkg_area = vertical_chunks[2];

    draw_cpu_clusters_usage_block(
        f,
        metrics,
        &app.history,
        app.accent_color(),
        app.gauge_bg_color(),
        cpu_area,
    );
    draw_gpu_ane_usage_block(
        f,
        metrics,
        &app.soc_info,
        &app.history,
        app.accent_color(),
        app.gauge_bg_color(),
        gpu_area,
    );
    draw_package_block(f, metrics, &app.history, app.accent_color(), pkg_area);
}

/// Draw the CPU clusters usage block.
///
/// On Apple Silicon, a CPU has at least one CPU efficiency cluster (the efficiency cores) and at
/// least one CPU performance cluster (the perf. cores). M1, M1 Pro, M1 Max, M2, M2 Pro and M2 Max
/// have one CPU cluster of each, while M1 Ultra has two of these pairs.
///
/// In this block, for each CPU, we draw both the efficiency cluster metrics and the performance
/// cluster metrics.
///
/// ┌ CPU: 124.32 mW ─────────────────────────────────────────────────────────────────────────┐
/// │E0-Cluster: 25.6 % @ 1027 MHz                 E1-Cluster: 7.0 % @ 1729 MHz               │
/// │------------------- 26% --------------------  ------------------- 7% --------------------│
/// │   ▄▃▅▆▂▁ ▆▇▇▃▄▅▅▅▆  ▆▃                                                                  │
/// │▁▂▄██████▇█████████▇▃███▄▂▁█   █                                                         │
/// │████████████████████████████▇▅▆█▆▄▅▅▆▄▆▅▅▇▇▅  ▂▄▃▂▄▃▂▂▁▃▃▁▂▁▂▂▂▃▂ ▂▁▃▂▂▂▂▁ ▂▁▁▁  ▁▁▁▁▁▁▁▁│
/// │                                                                                         │
/// │P0-Cluster: 25.6 % @ 1027 MHz                 P1-Cluster: 7.0 % @ 1729 MHz               │
/// │------------------- 26% --------------------  ------------------- 7% --------------------│
/// │   ▄▃▅▆▂▁ ▆▇▇▃▄▅▅▅▆  ▆▃                                                                  │
/// │▁▂▄██████▇█████████▇▃███▄▂▁█   █                                                         │
/// │████████████████████████████▇▅▆█▆▄▅▅▆▄▆▅▅▇▇▅  ▂▄▃▂▄▃▂▂▁▃▃▁▂▁▂▂▂▃▂ ▂▁▃▂▂▂▂▁ ▂▁▁▁  ▁▁▁▁▁▁▁▁│
/// └─────────────────────────────────────────────────────────────────────────────────────────┘
///
fn draw_cpu_clusters_usage_block<B>(
    f: &mut Frame<B>,
    metrics: &metrics::Metrics,
    history: &History,
    accent_color: Color,
    gauge_bg_color: Color,
    area: Rect,
) where
    B: Backend,
{
    let num_cluster_blocks =
        num_blocks_for(metrics.e_clusters.len()) + num_blocks_for(metrics.p_clusters.len());

    let sig = history.get("cpu_w").unwrap();
    let title = "CPU Clusters";
    let title_with_power = format!(
        " {title}: 󱐋 {} (peak: {})",
        units::watts2(metrics.consumption.cpu_w),
        units::watts2(sig.peak)
    );
    let block = Block::default()
        .title(title_with_power)
        .borders(Borders::ALL);
    f.render_widget(block, area);

    let constraints = (0..num_cluster_blocks)
        .map(|k| {
            Constraint::Length(
                GAUGE_HEIGHT
                    + SPARKLINE_HEIGHT
                    + if k < num_cluster_blocks - 1 {
                        CLUSTER_SPACING
                    } else {
                        0
                    },
            )
        }) // block height
        .collect::<Vec<_>>();
    let cpu_cluster_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .margin(1)
        .split(area);

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

    // 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,
        );
    }
}

/// Draw the overall metrics for a single CPU cluster.
///
/// E0-Cluster: 26.3 % @ 1009 MHz
/// ------------------------------------------- 26% -----------------------------------------
///
///  ▁ ▄▅▄ ▁    ▂   ▃▃                                          ▃   ▅
/// ██▅█████▄▆▄▅█▄▆███▆▇▅▇▅▅▅█▆█▃▅▃▅▄▅▅▅▅█▄▅▃▅▅▆█▅▃▄▁▂▁▄▃▁▇▃▂▃▁▆█▂▃▆█▂▂▂▂▂▃▁▂▁▁ ▂▂▂▂▂▃▂▁▁▂▂▃▂
///
fn draw_cluster_overall_metrics<B>(
    f: &mut Frame<B>,
    cluster: &metrics::ClusterMetrics,
    history: &History,
    accent_color: Color,
    gauge_bg_color: Color,
    area: Rect,
) where
    B: Backend,
{
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(GAUGE_HEIGHT),
            Constraint::Length(SPARKLINE_HEIGHT),
            Constraint::Max(CLUSTER_SPACING),
        ])
        .split(area);
    let top_area = chunks[0];
    let bottom_area = chunks[1];

    // Cluster cores Usage Gauge.
    let sig_name = format!("{}_active_percent", cluster.name);
    let sig = history.get(&sig_name).unwrap();
    let title = format!(
        "{}: {} @ {} (peak: {})",
        cluster.name,
        units::percent1(cluster.active_ratio() * 100.0),
        units::mhz(cluster.freq_mhz),
        units::percent1(sig.peak)
    );
    let gauge = Gauge::default()
        .block(Block::default().title(title))
        .gauge_style(Style::default().fg(accent_color).bg(gauge_bg_color))
        .ratio(cluster.active_ratio() as f64);

    f.render_widget(gauge, top_area);

    // Cluster cores Sparklines.
    let sparkline = Sparkline::default()
        .style(Style::default().fg(accent_color))
        .bar_set(symbols::bar::NINE_LEVELS)
        .data(sig.as_slice_last_n(bottom_area.width as usize))
        .max((SPARKLINE_MAX_OVERSHOOT * sig.max) as u64);
    f.render_widget(sparkline, bottom_area);
}

/// Draw the overall metrics for a pair of CPU clusters.
///
/// For instance:
///
/// E0-Cluster: 26.3 % @ 1009 MHz                 E1-Cluster: 12.1 % @ 1873 MHz
/// ------------------- 26% --------------------  ------------------- 12% -------------------
///
///  ▁ ▄▅▄ ▁    ▂   ▃▃                                          ▃   ▅
/// ██▅█████▄▆▄▅█▄▆███▆▇▅▇▅▅▅█▆█▃▅▃▅▄▅▅▅▅█▄▅▃▅▅▆  ▃▄▁▂▁▄▃▁▇▃▂▃▁▆█▂▃▆█▂▂▂▂▂▃▁▂▁▁ ▂▂▂▂▂▃▂▁▁▂▂▃▂
///
fn draw_cluster_pair_overall_metrics<B>(
    f: &mut Frame<B>,
    left_cluster: &metrics::ClusterMetrics,
    right_cluster: &metrics::ClusterMetrics,
    history: &History,
    accent_color: Color,
    gauge_bg_color: Color,
    area: Rect,
) where
    B: Backend,
{
    let horizontal_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Ratio(1, 2),
            Constraint::Length(2), // space
            Constraint::Ratio(1, 2),
        ])
        // .horizontal_margin(1)
        .split(area);
    let left_area = horizontal_chunks[0];
    let right_area = horizontal_chunks[2];

    draw_cluster_overall_metrics(
        f,
        left_cluster,
        history,
        accent_color,
        gauge_bg_color,
        left_area,
    );
    draw_cluster_overall_metrics(
        f,
        right_cluster,
        history,
        accent_color,
        gauge_bg_color,
        right_area,
    );
}

/// Draw the GPU & ANE usage block.
///
/// ┌ GPU & ANE ────────────────────────────────────────────────────────────────────┐
/// │GPU Usage: 4.5 % @ 711 MHz ⚡️12.84 mW    ANE Usage: 0.0 % ⚡️0.00 W             │
/// │----------------- 4% -----------------   ----------------- 0% -----------------│
/// │                                                                               │
/// │                                                                               │
/// │                  ▁          ▄▅▄▅▄▅▂ ▁                                         │
/// └───────────────────────────────────────────────────────────────────────────────┘
///
fn draw_gpu_ane_usage_block<B>(
    f: &mut Frame<B>,
    metrics: &metrics::Metrics,
    soc_info: &SocInfo,
    history: &History,
    accent_color: Color,
    gauge_bg_color: Color,
    area: Rect,
) where
    B: Backend,
{
    let block = Block::default().title(" GPU & ANE ").borders(Borders::ALL);
    f.render_widget(block, area);

    let horizontal_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Ratio(1, 2),
            Constraint::Length(2), // space
            Constraint::Ratio(1, 2),
        ])
        .margin(1)
        .split(area);
    let left_area = horizontal_chunks[0];
    let right_area = horizontal_chunks[2];

    let left_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Length(9)])
        .split(left_area);
    let top_left_area = left_chunks[0];
    let bottom_left_area = left_chunks[1];

    let right_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Length(9)])
        .split(right_area);
    let top_right_area = right_chunks[0];
    let bottom_right_area = right_chunks[1];

    // left: GPU.
    let gpu = &metrics.gpu;
    let sig = history.get("gpu_active_percent").unwrap();
    let sig_gpu_power = history.get("gpu_w").unwrap();
    let title = format!(
        "GPU: {} @ {} 󱐋 {} (peak: {} 󱐋 {})",
        units::percent1(gpu.active_ratio * 100.0),
        units::mhz(gpu.freq_mhz),
        units::watts2(metrics.consumption.gpu_w),
        units::percent1(sig.peak),
        units::watts2(sig_gpu_power.peak)
    );
    let gauge = Gauge::default()
        .block(Block::default().title(title))
        .gauge_style(
            Style::default().fg(accent_color).bg(gauge_bg_color),
            // .add_modifier(Modifier::ITALIC | Modifier::BOLD),
        )
        .ratio(gpu.active_ratio);

    f.render_widget(gauge, top_left_area);

    // GPU Usage Sparklines.
    let sparkline = Sparkline::default()
        .style(Style::default().fg(accent_color))
        .bar_set(symbols::bar::NINE_LEVELS)
        .data(sig.as_slice_last_n(bottom_left_area.width as usize))
        .max((SPARKLINE_MAX_OVERSHOOT * sig.max) as u64);
    f.render_widget(sparkline, bottom_left_area);

    // Right: ANE.
    let ane_active_ratio = metrics.consumption.ane_w as f64 / soc_info.max_ane_w;
    let sig = history.get("ane_active_percent").unwrap();
    let sig_ane_power = history.get("ane_w").unwrap();
    let title = format!(
        "ANE: {} 󱐋 {} (peak: {} 󱐋 {})",
        units::percent1(ane_active_ratio * 100.0),
        units::watts2(metrics.consumption.ane_w),
        units::percent1(sig.peak),
        units::watts2(sig_ane_power.peak)
    );
    let gauge = Gauge::default()
        .block(Block::default().title(title))
        .gauge_style(Style::default().fg(accent_color).bg(gauge_bg_color))
        .ratio(ane_active_ratio);

    f.render_widget(gauge, top_right_area);

    // Sparklines for the ANE usage.
    let sparkline = Sparkline::default()
        .style(Style::default().fg(accent_color))
        .bar_set(symbols::bar::NINE_LEVELS)
        .data(sig.as_slice_last_n(bottom_right_area.width as usize))
        .max((SPARKLINE_MAX_OVERSHOOT * sig.max) as u64);
    f.render_widget(sparkline, bottom_right_area);
}

/// Draw the Package block (power and thermals).
fn draw_package_block<B>(
    f: &mut Frame<B>,
    metrics: &metrics::Metrics,
    history: &History,
    accent_color: Color,
    area: Rect,
) where
    B: Backend,
{
    let horizontal_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Ratio(7, 10), Constraint::Ratio(3, 10)])
        .split(area);
    let pkg_area = horizontal_chunks[0];
    let thr_area = horizontal_chunks[1];

    draw_package_power_block(f, metrics, history, accent_color, pkg_area);
    draw_thermal_pressure_block(f, metrics, accent_color, thr_area);
}

/// Draw the Package power block.
///
/// ┌ Package ────────────────────────────────────────────────────────────────────────────────┐
/// │CPU+GPU+ANE: ⚡️95.48 mW (peak: 3.17 W)                                                   │
/// │                                                                                         │
/// │                                                                                         │
/// │                                    ▁                                           ▁▁▁▁▁▁▁  │
/// └─────────────────────────────────────────────────────────────────────────────────────────┘
///
fn draw_package_power_block<B>(
    f: &mut Frame<B>,
    metrics: &metrics::Metrics,
    history: &History,
    accent_color: Color,
    area: Rect,
) where
    B: Backend,
{
    let block = Block::default().title(" Package ").borders(Borders::ALL);
    f.render_widget(block, area);

    let vertical_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(1), Constraint::Length(SPARKLINE_HEIGHT)])
        .margin(1)
        .split(area);
    let title_area = vertical_chunks[0];
    let sparkline_area = vertical_chunks[1];

    let sig = history.get("package_w").unwrap();
    let title = format!(
        "CPU+GPU+ANE: 󱐋 {} (peak: {})",
        units::watts2(metrics.consumption.package_w),
        units::watts2(sig.peak)
    );
    let text = Paragraph::new(Text::from(title));
    f.render_widget(text, title_area);

    // Sparklines for the Package total usage.
    let sparkline = Sparkline::default()
        .style(Style::default().fg(accent_color))
        .bar_set(symbols::bar::NINE_LEVELS)
        .data(sig.as_slice_last_n(sparkline_area.width as usize))
        .max(sig.max as u64);
    f.render_widget(sparkline, sparkline_area);
}

/// Draw the Thermal Pressure block.
///
/// ┌ Thermals ───────────────────────────────────────────────────────────────────────────────┐
/// │Thermal Pressure: Nominal                                                                │
/// └─────────────────────────────────────────────────────────────────────────────────────────┘
///
fn draw_thermal_pressure_block<B>(
    f: &mut Frame<B>,
    metrics: &metrics::Metrics,
    accent_color: Color,
    area: Rect,
) where
    B: Backend,
{
    let color = match metrics.thermal_pressure.as_str() {
        "Nominal" => accent_color,
        _ => Color::Yellow,
    };
    let text = Line::from(vec![
        Span::raw("Pressure: "),
        Span::styled(&metrics.thermal_pressure, Style::default().fg(color)),
    ]);
    let paragraph =
        Paragraph::new(text).block(Block::default().title(" Thermals ").borders(Borders::ALL));
    f.render_widget(paragraph, area);
}

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