Files
taskmgr/tests/model_test.zi

786 lines
30 KiB
Ziran

using Model :: #import "model";
Gpu :: #import "gpu_processes";
GpuDrm :: #import "gpu_drm";
Shortcuts :: #import "app_shortcuts";
Files :: #import "std/file";
test_libc :: #system_library "libc";
TestFork :: () -> s32 #foreign test_libc "fork";
TestPause :: () -> s32 #foreign test_libc "pause";
TestExit :: (status: s32) -> void #foreign test_libc "_exit";
TestKill :: (pid: s32, signal: s32) -> s32 #foreign test_libc "kill";
TestWait :: (pid: s32, status: *s32, options: s32) -> s32 #foreign test_libc "waitpid";
TestSleep :: (micros: u32) -> s32 #foreign test_libc "usleep";
TestUnlink :: (path: *u8) -> s32 #foreign test_libc "unlink";
BytesOf :: (text: string, output: []u8) -> []u8 {
count: s32 = 0
for byte: text {
if count >= cast(s32)output.count { break }
output[count] = byte
count += 1
}
return output[0:count]
}
// Native model tests: no display and no window, only /proc reads, sorting,
// formatting, and the signal binding. Returns nonzero on the first failure.
ParserTest :: () -> bool {
cpu := "cpu 100 20 30 400 5 6 7 8 90 10\n"
cpu_bytes: [64]u8
busy, total, ok := Model.CpuTotals(BytesOf(cpu, cpu_bytes[:]))
if !ok || busy != 171 || total != 576 {
print("parse: cpu guest accounting wrong\n")
return false
}
stat := "4321 (worker (stage) name) S 1 2 3 4 -1 6 7 8 9 10 11 12 13 14 15 -5 17 18 19 20 21\n"
stat_bytes: [192]u8
process: Model.Process
if !Model.ParseStat(BytesOf(stat, stat_bytes[:]), *process) {
print("parse: process stat rejected\n")
return false
}
if TextView(process.name[0:process.name_count]) != "worker (stage) name" ||
process.state != Model.STATE_SLEEPING || process.ticks != 23 ||
process.memory_bytes == 0 || process.threads != 17 || process.ppid != 1 {
print("parse: process stat fields wrong\n")
return false
}
if Model.CpuKernelTicks(BytesOf(cpu, cpu_bytes[:])) != 43 {
print("parse: kernel cpu ticks wrong\n")
return false
}
short := "cpu 1 2 3\n"
short_bytes: [32]u8
if Model.CpuKernelTicks(BytesOf(short, short_bytes[:])) != 0 {
print("parse: short cpu line accepted\n")
return false
}
meminfo := "MemTotal: 32000000 kB\nMemFree: 1000 kB\nMemAvailable: 12000000 kB\nBuffers: 500 kB\nCached: 7000 kB\nSwapCached: 99 kB\nSwapTotal: 4000 kB\nSwapFree: 3000 kB\nSReclaimable: 800 kB\nSUnreclaim: 200 kB\nCommitLimit: 20000000 kB\nCommitted_AS: 15000000 kB\n"
meminfo_bytes: [512]u8
memory := Model.ParseMemory(BytesOf(meminfo, meminfo_bytes[:]))
if memory.total_kb != 32000000 || memory.available_kb != 12000000 ||
memory.cache_kb != 7500 || memory.commit_kb != 15000000 ||
memory.commit_limit_kb != 20000000 || memory.kernel_paged_kb != 800 ||
memory.kernel_nonpaged_kb != 200 || memory.swap_total_kb != 4000 ||
memory.swap_free_kb != 3000 {
print("parse: meminfo fields wrong\n")
return false
}
missing := "MemTotal: 10 kB\nSwapCached: 5 kB\n"
missing_bytes: [64]u8
partial := Model.ParseMemory(BytesOf(missing, missing_bytes[:]))
if partial.cache_kb != -1 || partial.commit_kb != -1 {
print("parse: absent meminfo entries not reported as missing\n")
return false
}
file_nr := "2848\t0\t9223372036854775807\n"
file_nr_bytes: [64]u8
if Model.FirstNumber(BytesOf(file_nr, file_nr_bytes[:])) != 2848 {
print("parse: file handle count wrong\n")
return false
}
return true
}
ScanTest :: () -> bool {
Model.Refresh()
Model.Refresh()
snapshot := Model.Current()
if snapshot.count <= 0 {
print("scan: no processes found\n")
return false
}
if snapshot.memory_total <= 0 {
print("scan: memory total missing\n")
return false
}
if snapshot.cpu_percent < 0.0 || snapshot.cpu_percent > 100.0 {
print("scan: total cpu percent out of range\n")
return false
}
if snapshot.gpu_supported_count > 0 &&
(snapshot.gpu_percent < 0.0 || snapshot.gpu_percent > 100.0) {
print("scan: GPU percent out of range\n")
return false
}
if snapshot.vram_total > 0 && snapshot.vram_used > snapshot.vram_total {
print("scan: VRAM usage exceeds total\n")
return false
}
if snapshot.gpu_count > Model.MAX_GPUS {
print("scan: too many GPU devices\n")
return false
}
if snapshot.gpu_count > 0 {
previous: s32 = -1
for gpu_index: 0..snapshot.gpu_count - 1 {
device := snapshot.gpus[gpu_index]
if device.card_index <= previous {
print("scan: GPU device order is not stable\n")
return false
}
previous = device.card_index
if device.busy_supported &&
(device.busy_percent < 0.0 || device.busy_percent > 100.0) {
print("scan: per-device GPU percent out of range\n")
return false
}
if device.vram_supported &&
(device.vram_total == 0 || device.vram_used > device.vram_total) {
print("scan: per-device VRAM counters invalid\n")
return false
}
}
}
mine := Model.CurrentProcessId()
index := Model.FindProcess(snapshot, mine)
if index < 0 {
print("scan: own process missing\n")
return false
}
entry := snapshot.processes[index]
if entry.threads < 1 {
print("scan: own process thread count missing\n")
return false
}
if snapshot.threads < cast(s64)snapshot.count {
print("scan: thread total below process count\n")
return false
}
if snapshot.memory.total_kb <= 0 || snapshot.memory.commit_kb < 0 {
print("scan: meminfo details missing\n")
return false
}
if entry.name_count <= 0 {
print("scan: own process name empty\n")
return false
}
if entry.memory_bytes == 0 {
print("scan: own process memory zero\n")
return false
}
if entry.state == 0 {
print("scan: own process state missing\n")
return false
}
return true
}
GpuFixtureTest :: () -> bool {
snapshot: Model.Snapshot
busy_75: [8]u8
used_25: [8]u8
total_100: [8]u8
busy_0: [8]u8
used_0: [8]u8
total_800: [8]u8
empty: [1]u8
bad_used: [8]u8
bad_total: [8]u8
// Add out of order to prove that card labels and histories are stable.
Model.AddGpuCounters(*snapshot, 1, BytesOf("75\n", busy_75[:]),
BytesOf("25\n", used_25[:]), BytesOf("100\n", total_100[:]))
Model.AddGpuCounters(*snapshot, 0, BytesOf("0\n", busy_0[:]),
BytesOf("0\n", used_0[:]), BytesOf("800\n", total_800[:]))
Model.AddGpuCounters(*snapshot, 2, empty[0:0],
BytesOf("101\n", bad_used[:]), BytesOf("100\n", bad_total[:]))
Model.FinalizeGpuStats(*snapshot)
if snapshot.gpu_count != 3 || snapshot.gpu_supported_count != 2 ||
snapshot.gpus[0].card_index != 0 || snapshot.gpus[1].card_index != 1 ||
snapshot.gpus[2].card_index != 2 || snapshot.gpu_percent != 37.5 {
print("gpu: detection, stable ordering or aggregate busy usage wrong\n")
return false
}
if !snapshot.gpus[0].vram_supported ||
Model.GpuVramPercent(*snapshot.gpus[0]) != 0.0 {
print("gpu: zero VRAM usage did not remain zero\n")
return false
}
if Model.GpuVramPercent(*snapshot.gpus[1]) != 25.0 ||
snapshot.gpus[2].vram_supported ||
Model.GpuVramPercent(*snapshot.gpus[2]) != -1.0 {
print("gpu: VRAM percentage or unsupported state wrong\n")
return false
}
if snapshot.vram_used != 25 || snapshot.vram_total != 900 {
print("gpu: aggregate VRAM counters wrong\n")
return false
}
Model.PushGpuDeviceHistories(*snapshot)
Model.PushHistoryExtended(0.0, 0.0, 0.0, snapshot.gpu_percent,
100.0 * cast(float64)snapshot.vram_used / cast(float64)snapshot.vram_total)
values: [Model.HISTORY_LENGTH]float32
last := Model.HISTORY_LENGTH - 1
if Model.GpuHistory(Model.HISTORY_GPU, 0, values[:])[last] != 0.0 ||
Model.GpuHistory(Model.HISTORY_GPU, 1, values[:])[last] != 75.0 ||
Model.GpuHistory(Model.HISTORY_VRAM, 0, values[:])[last] != 0.0 ||
Model.GpuHistory(Model.HISTORY_VRAM, 1, values[:])[last] != 25.0 {
print("gpu: per-device histories wrong\n")
return false
}
return true
}
ShortcutTest :: () -> bool {
if Shortcuts.TabForShortcut(49, Shortcuts.CONTROL_MODIFIER) != 0 ||
Shortcuts.TabForShortcut(50, Shortcuts.CONTROL_MODIFIER) != 1 ||
Shortcuts.TabForShortcut(51, Shortcuts.CONTROL_MODIFIER) != 2 {
print("shortcut: Ctrl+1/2/3 tab order wrong\n")
return false
}
if Shortcuts.TabForShortcut(49, 0) != -1 ||
Shortcuts.TabForShortcut(52, Shortcuts.CONTROL_MODIFIER) != -1 {
print("shortcut: missing Control or unrelated key accepted\n")
return false
}
return true
}
SetProcessName :: (process: *Model.Process, name: string) {
process.name_count = cast(s32)name.count
for index: 0..process.name_count - 1 { process.name[index] = name[index] }
}
GroupingTest :: () -> bool {
source: Model.Snapshot
grouped: Model.GroupedSnapshot
source.count = 4
SetProcessName(*source.processes[0], "codex")
SetProcessName(*source.processes[1], "codex-linux-x64")
SetProcessName(*source.processes[2], "Bash")
SetProcessName(*source.processes[3], "bash")
source.processes[0].pid = 10; source.processes[0].cpu_percent = 1.5
source.processes[0].memory_bytes = 100; source.processes[0].ticks = 20
source.processes[1].pid = 11; source.processes[1].cpu_percent = 2.5
source.processes[1].memory_bytes = 200; source.processes[1].ticks = 30
source.processes[2].pid = 20; source.processes[2].memory_bytes = 300
source.processes[3].pid = 21; source.processes[3].memory_bytes = 400
Model.BuildGroupedSnapshot(*source, *grouped)
if grouped.count != 2 {
print("grouping: expected two image-name groups\n")
return false
}
codex := Model.GroupProcess(*source, *grouped.groups[0])
if TextView(codex.name[0:codex.name_count]) != "codex" ||
codex.group_count != 2 || codex.memory_bytes != 300 ||
codex.ticks != 50 || codex.cpu_percent != 4.0 {
print("grouping: Codex family did not aggregate under codex\n")
return false
}
shell := Model.GroupProcess(*source, *grouped.groups[1])
if shell.group_count != 2 || shell.memory_bytes != 700 {
print("grouping: case-insensitive image group is wrong\n")
return false
}
order: [Model.MAX_PROCESSES]s32
Model.SortGroupOrder(*source, *grouped, order[:], Model.COLUMN_NAME, 1)
if order[0] != 1 || order[1] != 0 {
print("grouping: aggregate name ordering is wrong\n")
return false
}
Model.SortGroupOrder(*source, *grouped, order[:], Model.COLUMN_CPU, -1)
if order[0] != 0 || order[1] != 1 {
print("grouping: aggregate CPU ordering is wrong\n")
return false
}
Model.SortGroupOrder(*source, *grouped, order[:], Model.COLUMN_MEMORY, -1)
if order[0] != 1 || order[1] != 0 {
print("grouping: aggregate memory ordering is wrong\n")
return false
}
return true
}
SortTest :: () -> bool {
data: Model.Snapshot
data.count = 3
data.processes[0].pid = 30
data.processes[0].cpu_percent = 1.5
data.processes[0].memory_bytes = 300
data.processes[0].name_count = 1
data.processes[0].name[0] = 99
data.processes[1].pid = 10
data.processes[1].cpu_percent = 3.5
data.processes[1].memory_bytes = 100
data.processes[1].name_count = 1
data.processes[1].name[0] = 97
data.processes[2].pid = 20
data.processes[2].cpu_percent = 2.5
data.processes[2].memory_bytes = 200
data.processes[2].name_count = 1
data.processes[2].name[0] = 98
order: [Model.MAX_PROCESSES]s32
Model.SortOrder(*data, order[:], 1, 1)
if order[0] != 1 || order[1] != 2 || order[2] != 0 {
print("sort: pid ascending wrong\n")
return false
}
Model.SortOrder(*data, order[:], 1, -1)
if order[0] != 0 || order[1] != 2 || order[2] != 1 {
print("sort: pid descending wrong\n")
return false
}
Model.SortOrder(*data, order[:], 2, -1)
if order[0] != 1 || order[1] != 2 || order[2] != 0 {
print("sort: cpu descending wrong\n")
return false
}
Model.SortOrder(*data, order[:], Model.COLUMN_MEMORY, -1)
if order[0] != 0 || order[1] != 2 || order[2] != 1 {
print("sort: memory descending wrong\n")
return false
}
Model.SortOrder(*data, order[:], 0, 1)
if order[0] != 1 || order[1] != 2 || order[2] != 0 {
print("sort: name ascending wrong\n")
return false
}
data.processes[0].ticks = 500
data.processes[1].ticks = 900
data.processes[2].ticks = 100
Model.SortOrder(*data, order[:], Model.COLUMN_CPU_TIME, 1)
if order[0] != 2 || order[1] != 0 || order[2] != 1 {
print("sort: cpu time ascending wrong\n")
return false
}
Model.SortOrder(*data, order[:], Model.COLUMN_MEMORY, 0)
if order[0] != 0 || order[1] != 1 || order[2] != 2 {
print("sort: natural order wrong\n")
return false
}
return true
}
FormatTest :: () -> bool {
buffer: [32]u8
if Model.FormatBytes(1536, buffer[:]) != "1.5 KB" {
print("format: 1536 bytes wrong\n")
return false
}
if Model.FormatBytes(0, buffer[:]) != "0 B" {
print("format: zero bytes wrong\n")
return false
}
if Model.FormatBytes(cast(u64)3 * 1024 * 1024 * 1024, buffer[:]) != "3 GB" {
print("format: 3 GiB wrong\n")
return false
}
if Model.StateName(Model.STATE_RUNNING) != "Running" {
print("format: running state wrong\n")
return false
}
if Model.FormatThousands(1234567, buffer[:]) != "1,234,567" ||
Model.FormatThousands(999, buffer[:]) != "999" ||
Model.FormatThousands(1000, buffer[:]) != "1,000" ||
Model.FormatThousands(0, buffer[:]) != "0" ||
Model.FormatThousands(-1, buffer[:]) != "n/a" {
print("format: thousands separators wrong\n")
return false
}
if Model.FormatKilobytesBytes(1048576, buffer[:]) != "1 GB" ||
Model.FormatKilobytesBytes(1536, buffer[:]) != "1.5 MB" ||
Model.FormatKilobytesBytes(-1, buffer[:]) != "n/a" {
print("format: adaptive meminfo units wrong\n")
return false
}
if Model.FormatCpu(7.4, buffer[:]) != "7.4" || Model.FormatCpu(42.6, buffer[:]) != "43" ||
Model.FormatCpu(0.0, buffer[:]) != "0.0" || Model.FormatCpu(0.04, buffer[:]) != "0.0" ||
Model.FormatCpu(0.36, buffer[:]) != "0.4" || Model.FormatCpu(9.96, buffer[:]) != "10" ||
Model.FormatCpu(100.0, buffer[:]) != "100" {
print("format: cpu column wrong\n")
return false
}
ticks := cast(u64)(3725.0 * Model.ClockTicks())
if Model.FormatCpuTime(ticks, buffer[:]) != "1:02:05" ||
Model.FormatCpuTime(0, buffer[:]) != "0:00:00" {
print("format: cpu time wrong\n")
return false
}
return true
}
HistoryTest :: () -> bool {
Model.PushHistory(10.0, 2.0, 40.0)
Model.PushHistory(150.0, -5.0, 41.5)
Model.PushHistory(30.0, 3.0, 42.0)
values: [Model.HISTORY_LENGTH]float32
last := Model.HISTORY_LENGTH - 1
cpu := Model.History(Model.HISTORY_CPU, values[:])
if cast(s32)cpu.count != Model.HISTORY_LENGTH || cpu[last] != 30.0 ||
cpu[last - 1] != 100.0 || cpu[last - 2] != 10.0 {
print("history: cpu samples wrong or unclamped\n")
return false
}
kernel_values: [Model.HISTORY_LENGTH]float32
kernel := Model.History(Model.HISTORY_KERNEL, kernel_values[:])
if kernel[last] != 3.0 || kernel[last - 1] != 0.0 {
print("history: kernel samples wrong\n")
return false
}
memory_values: [Model.HISTORY_LENGTH]float32
memory := Model.History(Model.HISTORY_MEMORY, memory_values[:])
if memory[last] != 42.0 || memory[last - 1] != 41.5 {
print("history: memory samples wrong\n")
return false
}
Model.PushHistoryExtended(11.0, 4.0, 43.0, 67.0, 81.0)
gpu_values: [Model.HISTORY_LENGTH]float32
gpu := Model.History(Model.HISTORY_GPU, gpu_values[:])
if gpu[last] != 67.0 {
print("history: GPU sample wrong\n")
return false
}
vram_values: [Model.HISTORY_LENGTH]float32
vram := Model.History(Model.HISTORY_VRAM, vram_values[:])
if vram[last] != 81.0 {
print("history: VRAM sample wrong\n")
return false
}
// More samples than the ring holds keep only the newest, in order.
for step: 0..Model.HISTORY_LENGTH + 4 {
Model.PushHistory(cast(float64)(step % 100), 0.0, 0.0)
}
wrapped_values: [Model.HISTORY_LENGTH]float32
wrapped := Model.History(Model.HISTORY_CPU, wrapped_values[:])
newest := cast(float32)((Model.HISTORY_LENGTH + 4) % 100)
if wrapped[last] != newest || wrapped[0] != newest - cast(float32)(Model.HISTORY_LENGTH - 1) {
print("history: wrapped samples out of order\n")
return false
}
short: [4]float32
if Model.History(Model.HISTORY_CPU, short[:]).count != 4 {
print("history: short output not respected\n")
return false
}
return true
}
StartTaskTest :: () -> bool {
if Model.StartTask("") || Model.StartTask(" ") {
print("start: empty command accepted\n")
return false
}
path := "build/start-task-probe"
path_bytes: [64]u8
count: s32 = 0
for byte: path { path_bytes[count] = byte; count += 1 }
path_bytes[count] = 0
unused TestUnlink(*path_bytes[0])
if !Model.StartTask("printf started > build/start-task-probe") {
print("start: shell did not start\n")
return false
}
found := false
waited: s32 = 0
while waited < 50 && !found {
file := Files.OpenRead(path)
if file.valid {
Files.CloseFile(file)
found = true
} else {
unused TestSleep(100000)
waited += 1
}
}
unused TestUnlink(*path_bytes[0])
if !found {
print("start: background command never ran\n")
return false
}
return true
}
SignalTest :: () -> bool {
if Model.EndTask(0) {
print("signal: nonpositive pid accepted\n")
return false
}
if Model.EndTask(2147483647) {
print("signal: nonexistent pid reported as stopped\n")
return false
}
child := TestFork()
if child < 0 {
print("signal: could not create test child\n")
return false
}
if child == 0 {
unused TestPause()
TestExit(70)
return false
}
if !Model.EndTask(child) {
unused TestKill(child, 9)
cleanup_status: s32
unused TestWait(child, *cleanup_status, 0)
print("signal: SIGTERM request failed\n")
return false
}
status: s32
if TestWait(child, *status, 0) != child {
print("signal: test child was not reaped\n")
return false
}
if (status & 127) != Model.STOP_SIGNAL {
print("signal: test child did not receive SIGTERM\n")
return false
}
return true
}
GroupSignalTest :: () -> bool {
first := TestFork()
if first < 0 {
print("group signal: could not create first child\n")
return false
}
if first == 0 {
unused TestPause()
TestExit(71)
return false
}
second := TestFork()
if second < 0 {
unused TestKill(first, 9)
cleanup_status: s32
unused TestWait(first, *cleanup_status, 0)
print("group signal: could not create second child\n")
return false
}
if second == 0 {
unused TestPause()
TestExit(72)
return false
}
source: Model.Snapshot
group: Model.Process
source.count = 2
source.processes[0].pid = first
source.processes[1].pid = second
SetProcessName(*source.processes[0], "group-signal-test")
SetProcessName(*source.processes[1], "GROUP-SIGNAL-TEST")
SetProcessName(*group, "group-signal-test")
result := Model.EndProcessGroup(*source, *group)
first_status: s32
second_status: s32
first_wait := TestWait(first, *first_status, 0)
second_wait := TestWait(second, *second_status, 0)
if result.requested != 2 || result.sent != 2 || result.failed != 0 ||
first_wait != first || second_wait != second ||
(first_status & 127) != Model.STOP_SIGNAL ||
(second_status & 127) != Model.STOP_SIGNAL {
print("group signal: did not stop every matching process\n")
return false
}
return true
}
AddProcess :: (snapshot: *Model.Snapshot, pid: s32, ppid: s32, cpu: float64, memory: u64) {
entry := *snapshot.processes[snapshot.count]
entry.pid = pid
entry.ppid = ppid
entry.cpu_percent = cpu
entry.memory_bytes = memory
entry.group_count = 1
snapshot.count += 1
}
// Window rows own their process tree; the nearest listed ancestor wins, so a
// browser started from a terminal is not counted twice.
AppUsageTest :: () -> bool {
snapshot: Model.Snapshot
// Deliberately out of PID order: /proc order is only usually ascending.
AddProcess(*snapshot, 500, 1, 1.0, 1000) // terminal (window 0)
AddProcess(*snapshot, 501, 500, 2.0, 200) // shell under the terminal
AddProcess(*snapshot, 900, 501, 10.0, 5000) // browser (window 1), from the shell
AddProcess(*snapshot, 901, 900, 20.0, 3000) // browser renderer
AddProcess(*snapshot, 902, 901, 5.0, 100) // grandchild of the browser
AddProcess(*snapshot, 700, 1, 50.0, 9999) // unrelated daemon
AddProcess(*snapshot, 1, 0, 0.0, 10) // init
window_pids: [5]s32 = .[500, 900, 0, 4242, 900]
gpu_pids: [3]s32 = .[901, 902, 700]
gpu_percent: [3]float64 = .[30.0, 4.0, 60.0]
usage: [5]Model.AppUsage
Model.AppUsages(*snapshot, window_pids[:], gpu_pids[:], gpu_percent[:], true, usage[:])
terminal := usage[0]
if !terminal.found || terminal.processes != 2 || terminal.cpu_percent != 3.0 ||
terminal.memory_bytes != 1200 || terminal.gpu_percent != 0.0 || !terminal.gpu_known {
print("apps: terminal tree totals wrong\n")
return false
}
browser := usage[1]
if !browser.found || browser.processes != 3 || browser.cpu_percent != 35.0 ||
browser.memory_bytes != 8100 || browser.gpu_percent != 34.0 {
print("apps: browser tree totals wrong\n")
return false
}
if usage[2].found || usage[3].found {
print("apps: windows without a live PID reported usage\n")
return false
}
if !usage[4].found || usage[4].processes != 3 || usage[4].memory_bytes != 8100 {
print("apps: second window of one process did not share its totals\n")
return false
}
Model.AppUsages(*snapshot, window_pids[:], gpu_pids[0:0], gpu_percent[0:0], false, usage[:])
if usage[1].gpu_known || usage[1].gpu_percent != 0.0 || usage[1].cpu_percent != 35.0 {
print("apps: missing GPU source not reported as unknown\n")
return false
}
return true
}
// NVIDIA cards get their whole-card readings from the helper, matched to the
// DRM card by the PCI address in its uevent file.
ExternalGpuTest :: () -> bool {
uevent := "DRIVER=nvidia\nPCI_CLASS=30000\nPCI_SLOT_NAME=0000:0a:1f.0\nMODALIAS=pci:v000010DE\n"
bytes: [128]u8
domain, bus, slot, function, ok := Model.PciSlotFromUevent(BytesOf(uevent, bytes[:]))
if !ok || domain != 0 || bus != 10 || slot != 31 || function != 0 {
print("external gpu: PCI slot parsed wrong\n")
return false
}
unused_domain, unused_bus, unused_slot, unused_function, bad := Model.PciSlotFromUevent(
BytesOf("XPCI_SLOT_NAME=0000:01:00.0\n", bytes[:]))
if bad {
print("external gpu: label matched mid-line\n")
return false
}
readings: [2]Model.GpuExternal
readings[0] = Model.GpuExternal.{domain = 0, bus = 1, slot = 0, busy = 37,
vram_used = 512, vram_total = 2048}
readings[1] = Model.GpuExternal.{domain = 0, bus = 10, slot = 31, busy = -1,
vram_used = -1, vram_total = -1}
Model.SetExternalGpus(readings[:])
if Model.ExternalGpuFor(0, 1, 0) != 0 || Model.ExternalGpuFor(0, 10, 31) != 1 ||
Model.ExternalGpuFor(1, 1, 0) != -1 {
print("external gpu: PCI match wrong\n")
return false
}
snapshot: Model.Snapshot
Model.AddGpuValues(*snapshot, 1, readings[0].busy, readings[0].vram_used, readings[0].vram_total)
Model.AddGpuValues(*snapshot, 0, readings[1].busy, readings[1].vram_used, readings[1].vram_total)
Model.FinalizeGpuStats(*snapshot)
if snapshot.gpu_count != 2 || snapshot.gpus[0].card_index != 0 ||
snapshot.gpus[0].busy_supported || snapshot.gpus[0].vram_supported ||
!snapshot.gpus[1].busy_supported || snapshot.gpus[1].busy_percent != 37.0 ||
snapshot.vram_used != 512 || snapshot.vram_total != 2048 || snapshot.gpu_percent != 37.0 {
print("external gpu: readings not applied\n")
return false
}
Model.SetExternalGpus(readings[0:0])
if Model.ExternalGpuFor(0, 1, 0) != -1 {
print("external gpu: cleared readings still matched\n")
return false
}
return true
}
// AMD/Intel per-process GPU use comes from DRM fdinfo counters.
DrmFdinfoTest :: () -> bool {
amd := "pos:\t0\nflags:\t02100002\ndrm-driver:\tamdgpu\ndrm-pdev:\t0000:03:00.0\ndrm-client-id:\t42\ndrm-memory-vram:\t1024 KiB\ndrm-engine-gfx:\t1500000 ns\ndrm-engine-compute:\t0 ns\n"
bytes: [256]u8
reading: GpuDrm.FdinfoClient
if !GpuDrm.ParseFdinfo(BytesOf(amd, bytes[:]), *reading) || reading.client != 42 ||
reading.count != 2 || reading.busy[0] != 1500000 || reading.capacity[0] != 1 {
print("drm: amdgpu fdinfo parsed wrong\n")
return false
}
intel := "drm-driver:\ti915\ndrm-client-id:\t7\ndrm-engine-capacity-video:\t2\ndrm-engine-video:\t900 ns\n"
video: GpuDrm.FdinfoClient
if !GpuDrm.ParseFdinfo(BytesOf(intel, bytes[:]), *video) || video.count != 1 ||
video.capacity[0] != 2 || video.busy[0] != 900 {
print("drm: engine capacity parsed wrong\n")
return false
}
none := "pos:\t0\nflags:\t02\n"
empty: GpuDrm.FdinfoClient
if GpuDrm.ParseFdinfo(BytesOf(none, bytes[:]), *empty) {
print("drm: non-DRM fdinfo accepted\n")
return false
}
// 250 ms busy in 1000 ms, then shared by two engine instances.
if GpuDrm.EnginePercent(0, 250000000, 0, 0, 1000, 1) != 25.0 ||
GpuDrm.EnginePercent(0, 250000000, 0, 0, 1000, 2) != 12.5 ||
GpuDrm.EnginePercent(100, 50, 0, 0, 1000, 1) != 0.0 ||
GpuDrm.EnginePercent(0, 30, 100, 200, 1000, 1) != 30.0 {
print("drm: engine percent wrong\n")
return false
}
return true
}
GpuSharesTest :: () -> bool {
Gpu.GpuSharesReset()
if Gpu.GpuSharesReady() {
print("gpu: ready before any sample\n")
return false
}
first := "T\n2677 1\n552"
rest := "68 12\nbogus line\n9 \nE\n"
bytes: [128]u8
Gpu.GpuSharesFeed(BytesOf("T\nD 0 1 0 3 574619648 17103781888\nD 0 2 0 - - -\nD 0 x\nE\n", bytes[:]))
devices := Gpu.GpuDevicesCurrent()
if !Gpu.GpuSharesReady() || devices.count != 2 || devices.items[0].bus != 1 ||
devices.items[0].busy != 3 || devices.items[0].vram_used != 574619648 ||
devices.items[0].vram_total != 17103781888 || devices.items[1].bus != 2 ||
devices.items[1].busy != -1 || devices.items[1].vram_total != -1 {
print("gpu: device lines parsed wrong\n")
return false
}
Gpu.GpuSharesReset()
Gpu.GpuSharesFeed(BytesOf(first, bytes[:]))
if Gpu.GpuSharesReady() {
print("gpu: incomplete block published\n")
return false
}
Gpu.GpuSharesFeed(BytesOf(rest, bytes[:]))
shares := Gpu.GpuSharesCurrent()
if !Gpu.GpuSharesReady() || shares.count != 2 || shares.pids[0] != 2677 ||
shares.percent[0] != 1.0 || shares.pids[1] != 55268 || shares.percent[1] != 12.0 {
print("gpu: sample block parsed wrong\n")
return false
}
// An idle tick replaces the previous block with no processes.
Gpu.GpuSharesFeed(BytesOf("T\nE\n", bytes[:]))
if !Gpu.GpuSharesReady() || Gpu.GpuSharesCurrent().count != 0 {
print("gpu: idle sample kept stale shares\n")
return false
}
Gpu.GpuSharesFeed(BytesOf("X\n", bytes[:]))
if Gpu.GpuSharesReady() {
print("gpu: unusable source still ready\n")
return false
}
Gpu.GpuSharesReset()
return true
}
main :: (argc: s32, argv: **u8) -> s32 {
if argc != 1 {
print("usage: model_test\n")
return 64
}
if !ParserTest() { print("parser fixture test failed\n"); return 65 }
if !GpuFixtureTest() { print("GPU fixture test failed\n"); return 74 }
if !ShortcutTest() { print("shortcut test failed\n"); return 75 }
if !ScanTest() { print("scan/snapshot test failed\n"); return 66 }
if !GroupingTest() { print("process grouping test failed\n"); return 72 }
if !AppUsageTest() { print("application usage test failed\n"); return 76 }
if !GpuSharesTest() { print("GPU shares test failed\n"); return 77 }
if !ExternalGpuTest() { print("external GPU test failed\n"); return 78 }
if !DrmFdinfoTest() { print("DRM fdinfo test failed\n"); return 79 }
if !SortTest() { print("sort test failed\n"); return 67 }
if !FormatTest() { print("format test failed\n"); return 68 }
if !SignalTest() { print("signal binding test failed\n"); return 69 }
if !GroupSignalTest() { print("group signal test failed\n"); return 73 }
if !HistoryTest() { print("history test failed\n"); return 70 }
if !StartTaskTest() { print("start task test failed\n"); return 71 }
print("taskmgr model tests passed\n")
return 0
}