summaryrefslogtreecommitdiffstats
path: root/desktop/modules/perf/vram.sh
blob: 8ba61fb1934c3b88447e1cd3e2851af10df9ab6b (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
#!/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.

# VRAM in bytes from the fdinfo scan: drm-resident-vram0 of every xe fd,
# counted once per drm-client-id because a client holds several fds. Prints
# nothing when no xe client is found. With a file argument it reads that
# instead of scanning, for test_vram.sh.
#
# Summed here rather than in JS: a process holding thousands of DRM fds
# makes the scan a few hundred KB, and parsing that in QML every 2 s drove
# Qt's unmanaged-heap counter to underflow, after which the V4 engine ran a
# GC on every allocation and the shell sat at 100% CPU (AGENTS.md).
#
# 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' \
        /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
            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\n", total
    }'