Files
mac-amdgpu/tests/mtop_render_test.cpp
geramyloveless 32b9c675a2 amdgpu_mtop: make the top charts adaptive so the dashboard fits the terminal
c5b0313 rendered both top charts at the full 20 braille text rows each
(5 cell rows x 4 sub-rows), which made the dashboard ~86 rows tall. On a
normal 40-50 row terminal that pushed the GFX/UMC usage rows, VRAM,
power/sensor state, GRBM/GRBM2, and the processes panel below the fold,
leaving a screen that is mostly graphs.

The chart height is now computed from the terminal instead of hardcoded:

  chart budget = terminal_rows - (rows needed by everything else)

The "everything else" count is measured from the actual render:
device-list header (3 rows single GPU) + VRAM/POWER block (8) + GRBM
block (17, 16 when hidden) + processes block (8) + footer (1) + blank
separators (4, 3 hidden) = 41 rows. The remaining rows are split evenly
between the two charts and clamped to a floor of 4 text rows (one braille
cell row, still a chart) and a ceiling of 20 (five cell rows, the full
axis). At 50 rows the charts get 4 text rows each and the whole
dashboard fits in one screen; at 80+ rows they grow back to the full
20 rows.

Y-axis labels degrade gracefully with the chart height: each quarter
(100/75/50/25/0%) labels the row nearest its position, matching the
original 20-row chart's convention; 4-row charts show only 100%/0% so a
label never sits at the wrong height. Captions are unchanged.

The UMC source caption fallback (no plotted point yet) now follows the
source the next live sample will actually prefer (fresh+plausible SMU
field vs MMHUB PERFCTR delta) instead of assuming the SMU table.

No data sources, sampling, or observer protocol changes; TUI layout only.

The render test now asserts the user-visible regression directly: an
interactive 50-row frame contains both charts and every panel (VRAM,
power/sensor, GRBM/GRBM2, processes) with the footer as the last row,
plus a tall-terminal case (80 and 100 rows) that asserts the charts grow
back to the full quarter-labeled axis.
2026-09-25 22:13:11 -07:00

295 lines
14 KiB
C++

// Exercise the actual renderers with a synthetic driver response; no IOKit.
// The TUI is a modern btop-style layout (rounded borders, Braille chart,
// sub-cell bars, truecolor gradients). Assertions check structural invariants
// and the documented n/a panels.
#define main mtop_program_main
#include "../amdgpu_mtop/main.cpp"
#undef main
#include <cassert>
namespace mtop {
std::vector<Device> discover(std::string &) { return {}; }
}
// Strip ANSI escapes so we can assert on visible text.
static std::string strip(const std::string &s) {
std::string out;
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] == '\033' && i + 1 < s.size() && s[i + 1] == '[') {
i += 2;
while (i < s.size() && !(s[i] >= '@' && s[i] <= '~')) ++i;
continue;
}
out += s[i];
}
return out;
}
int main() {
using namespace amdgpu;
using namespace vram_accounting;
// Minimal device: just the identity fields. The dashboard handles
// missing sensors gracefully (renders n/a rows).
mtop::Device d;
d.registry = 1; d.build = 195; d.stage = 15;
d.gfx[0] = 11; d.gfx[1] = 0; d.gfx[2] = 0;
// No accounting/clock/telemetry: the dashboard renders n/a rows.
const auto now=clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
// Telemetry: power, temp, fan, clocks.
d.telemetrySupported=true;
d.metrics.version=kSMUMetricsSnapshotVersion;d.metrics.size=sizeof(d.metrics);
d.metrics.driverInterface=metrics::kCompatibleInterface;
d.metrics.flags=kSMUMetricsValid|kSMUMetricsLinuxCompatible;
d.metrics.collectedAtNs=now;
d.metrics.validFields=(uint64_t(1)<<metrics::Count)-1;
d.metrics.values[metrics::GfxActivityPercent]=45;
d.metrics.values[metrics::UmcActivityPercent]=37;
d.metrics.values[metrics::GfxClockMHz]=1820;
d.metrics.values[metrics::MemoryClockMHz]=1250;
d.metrics.values[metrics::SocketPowerMilliwatts]=105000;
d.metrics.values[metrics::BoardPowerMilliwatts]=355000;
d.metrics.values[metrics::FanRPM]=1240;
d.metrics.values[metrics::EdgeTemperatureMillicelsius]=46000;
d.metrics.values[metrics::HotspotTemperatureMillicelsius]=58000;
d.metrics.values[metrics::MemoryTemperatureMillicelsius]=54000;
// Software counters: leave unset for this render test. The GRBM panel
// renders the documented n/a rows; a dedicated engine test covers the
// per-engine delta path with real counter data.
// Device spec (CUs / SEs) for the header.
d.spec.valid = true;
d.spec.words[12] = 64; // CUs
d.spec.words[4] = 4; // SEs
mtop::Selection selected;
selected.registry = 1;
Histories histories;
// Minimal history: one point so the dashboard has data to render.
mtop::ActivityPoint p;
p.timeNs = now;
p.busyPercent = 50.0;
p.allocatedGiB = 0.5;
p.temperatureC = 46.0;
p.gfxPercent = 45.0;
p.umcPercent = 37.0;
histories[d.registry].points.push_back(p);
// The render test doesn't exercise the engine delta path; leave
// previousBusy unset so engineBusyPercent returns n/a (documented).
// A dedicated mtop-engine-test covers the delta computation.
std::ostringstream captured;
auto *previous = std::cout.rdbuf(captured.rdbuf());
mtop::Frame layout;
mtop::tui::Ui ui;
dashboard({d}, selected, {}, false, false, &histories, {}, 120, 50, now,
mtop::Graphics::Text, &layout, false, &ui);
std::cout.rdbuf(previous);
auto output = captured.str();
auto visible = strip(output);
// Header: chip ID, CU/SE count, build, stage, sensors.
assert(visible.find("AMDGPU gfx11.0.0") != std::string::npos);
assert(visible.find("64 CUs / 4 SEs") != std::string::npos);
assert(visible.find("build 195") != std::string::npos);
assert(visible.find("initialized") != std::string::npos);
// Section 2: GPU CORE LOAD with Braille chart. At 50 rows the chart
// budget is small (4 text rows), so the Y-axis degrades to 100%/0%.
assert(visible.find("GPU CORE LOAD") != std::string::npos);
assert(visible.find("100%") != std::string::npos);
assert(visible.find("0%") != std::string::npos);
assert(visible.find("1m") != std::string::npos);
assert(visible.find("45s") != std::string::npos);
assert(visible.find("30s") != std::string::npos);
assert(visible.find("15s") != std::string::npos);
assert(visible.find("0s") != std::string::npos);
// Tall terminal: charts grow back to the full 5-cell-row height and the
// Y-axis shows all quarter labels (100/75/50/25/0%).
captured.str(""); captured.clear();
previous = std::cout.rdbuf(captured.rdbuf());
mtop::Frame tallLayout;
dashboard({d}, selected, {}, false, false, &histories, {}, 120, 100, now,
mtop::Graphics::Text, &tallLayout, false, &ui);
std::cout.rdbuf(previous);
auto tallVisible = strip(captured.str());
assert(tallVisible.find("GPU CORE LOAD") != std::string::npos);
assert(tallVisible.find("100%") != std::string::npos);
assert(tallVisible.find("75%") != std::string::npos);
assert(tallVisible.find("50%") != std::string::npos);
assert(tallVisible.find("25%") != std::string::npos);
assert(tallVisible.find("0%") != std::string::npos);
// SMU MEMORY ACTIVITY (UMC) chart: a real memory-side 0-100% counter,
// not gfx activity and not a bare n/a. The chart is labeled and cites the
// SMU source; the header shows the current UMC %.
assert(visible.find("SMU MEMORY ACTIVITY (UMC)") != std::string::npos);
assert(visible.find("UmcActivityPercent") != std::string::npos);
assert(visible.find("UMC busy 0-100%") != std::string::npos);
// The chart's min/max/avg footer is rendered.
assert(visible.find("min") != std::string::npos);
assert(visible.find("max") != std::string::npos);
assert(visible.find("avg") != std::string::npos);
// VRAM / GTT meters.
assert(visible.find("VRAM USAGE") != std::string::npos);
assert(visible.find("VRAM:") != std::string::npos);
assert(visible.find("GTT:") != std::string::npos);
// Power & sensor state.
assert(visible.find("POWER & SENSOR STATE") != std::string::npos);
assert(visible.find("Pwr:") != std::string::npos);
assert(visible.find("Temp:") != std::string::npos);
assert(visible.find("Junc:") != std::string::npos);
assert(visible.find("Fan:") != std::string::npos);
assert(visible.find("SCLK") != std::string::npos);
assert(visible.find("MCLK") != std::string::npos);
// Section 3: GRBM / GRBM2.
assert(visible.find("PERFORMANCE COUNTERS (GRBM / GRBM2)") != std::string::npos);
assert(visible.find("[GRBM Status]") != std::string::npos);
assert(visible.find("[GRBM2 Status]") != std::string::npos);
assert(visible.find("Graphics Pipe (GFX)") != std::string::npos);
assert(visible.find("Compute Engine 0") != std::string::npos);
assert(visible.find("Compute Engine 1") != std::string::npos);
assert(visible.find("SDMA Engine (DMA)") != std::string::npos);
assert(visible.find("VCN (Video Decode)") != std::string::npos);
assert(visible.find("JPEG Engine") != std::string::npos);
assert(visible.find("Command Processor (CPF)") != std::string::npos);
assert(visible.find("Texture Cache (TCC)") != std::string::npos);
assert(visible.find("Depth Block (DB)") != std::string::npos);
assert(visible.find("Color Block (CB)") != std::string::npos);
assert(visible.find("Shader Pipe (SPI)") != std::string::npos);
assert(visible.find("Primitive Assembly (PA)") != std::string::npos);
// Section 4: GPU PROCESSES.
assert(visible.find("GPU PROCESSES (fdinfo)") != std::string::npos);
assert(visible.find("PID") != std::string::npos);
assert(visible.find("USER") != std::string::npos);
assert(visible.find("PROCESS NAME") != std::string::npos);
assert(visible.find("CPU%") != std::string::npos);
assert(visible.find("GPU%") != std::string::npos);
assert(visible.find("GFX/COMP") != std::string::npos);
assert(visible.find("MEDIA") != std::string::npos);
assert(visible.find("VRAM USAGE") != std::string::npos);
assert(visible.find("VRAM BAR") != std::string::npos);
// Documented n/a for per-process accounting.
assert(visible.find("per-process GPU accounting not exposed by macOS driver") != std::string::npos);
// Footer hotkeys.
assert(visible.find("[q] Quit") != std::string::npos);
assert(visible.find("[h] Interval:") != std::string::npos);
assert(visible.find("[p] Sort PID") != std::string::npos);
assert(visible.find("[m] Sort VRAM") != std::string::npos);
assert(visible.find("[g] Sort GPU") != std::string::npos);
assert(visible.find("[r] Toggle GRBM") != std::string::npos);
// Structural (interactive): the frame is exactly `rows` lines, each
// clipped to `columns-1` visible cells.
mtop::Frame structLayout;
captured.str(""); captured.clear();
previous = std::cout.rdbuf(captured.rdbuf());
dashboard({d}, selected, {}, true, false, &histories, {}, 120, 50, now,
mtop::Graphics::Text, &structLayout, false, &ui);
std::cout.rdbuf(previous);
assert(structLayout.previous.size() == 50);
for (const auto &row : structLayout.previous)
assert(mtop::clipColumns(row, 119) == row);
// Rounded borders are present (top-left, top-right, mid-left, mid-right,
// horizontal, vertical). The bottom border is only drawn when the
// dashboard content fills the screen; the section dividers use \u251C/\u2524.
assert(output.find("\xE2\x94\xAD") != std::string::npos); // \u256D
assert(output.find("\xE2\x94\xAE") != std::string::npos); // \u256E
assert(output.find("\xE2\x94\x9C") != std::string::npos); // \u251C
assert(output.find("\xE2\x94\xA4") != std::string::npos); // \u2524
assert(output.find("\xE2\x94\x80") != std::string::npos); // \u2500
assert(output.find("\xE2\x94\x82") != std::string::npos); // \u2502
// With a single history point, the Braille chart may be mostly empty.
// The chart structure (Y-axis labels, X-axis caption) is what matters
// for the layout test. A dedicated mtop-tui-test covers Braille
// dot-matrix correctness with a full 60s window.
// Sub-cell bars: full block glyph present (the VRAM meter uses it).
assert(output.find("\xE2\x96\x88") != std::string::npos); // \u2588
// Truecolor: the gradient palette emits \033[38;2;R;G;Bm sequences.
assert(output.find("\033[38;2;") != std::string::npos);
assert(visible.find("n/a (no in-flight sample window yet)") != std::string::npos);
// --- Interactive mode at 50 rows: the full dashboard must fit without
// clipping. This is the user-visible regression from c5b0313 (40-row
// charts pushed every panel below the fold on a normal terminal).
// Both charts are present (adaptive height >= 4 text rows), and every
// panel from the e8e7c49 rework is visible in the 50-row frame. ---
captured.str(""); captured.clear();
previous = std::cout.rdbuf(captured.rdbuf());
mtop::Frame fit50;
ui.sort.kind = mtop::tui::Sort::Vram;
dashboard({d}, selected, {}, true, false, &histories, mtop::RefreshMode{true},
120, 50, now, mtop::Graphics::Text, &fit50, false, &ui);
std::cout.rdbuf(previous);
auto fit50Visible = strip(captured.str());
// Frame is exactly 50 lines, footer last.
assert(fit50.previous.size() == 50);
assert(strip(fit50.previous.back()).find("[q] Quit") != std::string::npos);
assert(strip(fit50.previous.back()).find("Interval: 10ms") != std::string::npos);
// Sort label reflects the ui state.
assert(fit50Visible.find("sort: VRAM") != std::string::npos);
// Both charts are present with their axis captions.
assert(fit50Visible.find("GPU CORE LOAD") != std::string::npos);
assert(fit50Visible.find("SMU MEMORY ACTIVITY (UMC)") != std::string::npos);
assert(fit50Visible.find("100%") != std::string::npos);
assert(fit50Visible.find("0%") != std::string::npos);
assert(fit50Visible.find("0s") != std::string::npos);
// Every panel below the charts is still on screen.
assert(fit50Visible.find("VRAM USAGE") != std::string::npos);
assert(fit50Visible.find("POWER & SENSOR STATE") != std::string::npos);
assert(fit50Visible.find("PERFORMANCE COUNTERS (GRBM / GRBM2)") != std::string::npos);
assert(fit50Visible.find("[GRBM2 Status]") != std::string::npos);
assert(fit50Visible.find("Primitive Assembly (PA)") != std::string::npos);
assert(fit50Visible.find("GPU PROCESSES (fdinfo)") != std::string::npos);
// --- Tall terminal (80 rows): charts grow back toward the ceiling, all
// panels remain present, footer is the last row. ---
captured.str(""); captured.clear();
previous = std::cout.rdbuf(captured.rdbuf());
mtop::Frame fit80;
dashboard({d}, selected, {}, true, false, &histories, mtop::RefreshMode{true},
120, 80, now, mtop::Graphics::Text, &fit80, false, &ui);
std::cout.rdbuf(previous);
auto fit80Visible = strip(captured.str());
assert(fit80.previous.size() == 80);
assert(strip(fit80.previous.back()).find("[q] Quit") != std::string::npos);
assert(fit80Visible.find("GPU CORE LOAD") != std::string::npos);
assert(fit80Visible.find("SMU MEMORY ACTIVITY (UMC)") != std::string::npos);
assert(fit80Visible.find("VRAM USAGE") != std::string::npos);
assert(fit80Visible.find("PERFORMANCE COUNTERS (GRBM / GRBM2)") != std::string::npos);
assert(fit80Visible.find("GPU PROCESSES (fdinfo)") != std::string::npos);
// --- Engine toggle: 'r' hides the GRBM section ---
ui.enginesVisible = false;
captured.str(""); captured.clear();
previous = std::cout.rdbuf(captured.rdbuf());
dashboard({d}, selected, {}, false, false, &histories, {}, 120, 50, now,
mtop::Graphics::Text, &layout, false, &ui);
std::cout.rdbuf(previous);
auto hiddenVisible = strip(captured.str());
assert(hiddenVisible.find("(hidden; press r to toggle)") != std::string::npos);
assert(hiddenVisible.find("Graphics Pipe (GFX)") == std::string::npos);
// --- Unavailable sensors: stale telemetry clears the headline ---
d.metrics.collectedAtNs = now - kSMUMetricsStaleAfterNs - 1;
captured.str(""); captured.clear();
previous = std::cout.rdbuf(captured.rdbuf());
dashboard({d}, selected, {}, false, false, &histories, {}, 120, 50, now,
mtop::Graphics::Text, &layout, false, &ui);
std::cout.rdbuf(previous);
auto staleVisible = strip(captured.str());
// Power should be n/a when stale.
assert(staleVisible.find("Pwr: n/a") != std::string::npos);
return 0;
}