aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/perf/gpu.sh
blob: 31f8a6e50a8ba9230fdd1eb53c199cbc06e55cef (plain)
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
#!/bin/sh
#
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# GPU from the fdinfo scan, every xe fd counted once per drm-client-id
# because a client holds several fds. Prints one line, "vram rcs_busy
# rcs_clock ccs_busy ccs_clock": VRAM in bytes (drm-resident-vram0), then
# per engine the busy cycles summed over clients and the GPU's cycle
# counter, which Perf.js turns into a percentage between two scans. rcs is
# render (games), ccs compute (local LLMs). Prints nothing when no xe
# client is found. With a file argument it reads that instead of scanning,
# for test_gpu.sh.
#
# Summed here rather than in JS: a process holding thousands of DRM fds
# makes the scan a few hundred KB, which is a lot of string churn to parse
# in QML every 2 s. It was moved here while chasing a GC underflow that
# turned out to be the sound OSD's createImageData (AGENTS.md), not this.
#
# Fds of a repeated client id with different values: the first one awk's
# for-in reaches counts, where Perf.js took the first in scan order. They
# share one client, so the values agree in practice.

scan() {
    if [ -n "$1" ]; then cat "$1"
    else grep -sH -e '^drm-driver' -e '^drm-client-id' -e '^drm-resident-vram0' \
        -e '^drm-cycles-[rc]cs' -e '^drm-total-cycles-[rc]cs' \
        /proc/[0-9]*/fdinfo/* 2>/dev/null
    fi
}

scan "$1" | awk -F: '
    NF >= 3 && $2 ~ /^drm-/ {
        v = $3
        for (i = 4; i <= NF; i++) v = v ":" $i
        sub(/^[ \t]+/, "", v)
        d[$1, $2] = v
        fd[$1]
    }
    END {
        for (p in fd) {
            id = d[p, "drm-client-id"]
            if (d[p, "drm-driver"] != "xe" || id == "" || (id in seen)) continue
            if (split(d[p, "drm-resident-vram0"], n, " ") < 1 || n[1] !~ /^[0-9]+$/) continue
            for (i = 1; i <= 2; i++) {
                e = i == 1 ? "rcs" : "ccs"
                busy[e] += d[p, "drm-cycles-" e]
                c = d[p, "drm-total-cycles-" e] + 0
                if (c > clock[e]) clock[e] = c
            }
            seen[id]
            u = n[2] == "KiB" ? 1024 : n[2] == "MiB" ? 1048576 : n[2] == "GiB" ? 1073741824 : 1
            total += n[1] * u
            any = 1
        }
        if (any) printf "%.0f %.0f %.0f %.0f %.0f\n", total, busy["rcs"], clock["rcs"], busy["ccs"], clock["ccs"]
    }'