Skip to content

Instantly share code, notes, and snippets.

@kwilczynski
Created October 9, 2026 01:49
Show Gist options
  • Select an option

  • Save kwilczynski/f402ead3a1548951de899b1ebc6bfbfa to your computer and use it in GitHub Desktop.

Select an option

Save kwilczynski/f402ead3a1548951de899b1ebc6bfbfa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os, re, sys
def kib(v):
n, u = (v.split() + ['B'])[:2]
return int(n) * {'B': 1 / 1024, 'KiB': 1, 'MiB': 1024, 'GiB': 1024 * 1024}[u]
clients = {}
for pid in filter(str.isdigit, os.listdir('/proc')):
try:
fds = os.listdir('/proc/%s/fdinfo' % pid)
comm = open('/proc/%s/comm' % pid).read().strip()
except OSError:
continue
for fd in fds:
try:
t = open('/proc/%s/fdinfo/%s' % (pid, fd)).read()
except OSError:
continue
if 'drm-driver:\txe' not in t:
continue
kv = dict((k.strip(), v.strip()) for k, v in (l.split(':', 1) for l in t.splitlines() if ':' in l))
c = clients.setdefault(kv['drm-client-id'], {'pids': set(), 'mem': {}})
c['pids'].add('%s/%s' % (pid, comm))
for k, v in kv.items():
m = re.match(r'drm-(total|resident|shared|active|purgeable)-(?!cycles-)(\S+)$', k)
if m:
c['mem'][(m.group(1), m.group(2))] = kib(v)
regions = sorted({r for c in clients.values() for _, r in c['mem']})
cols = [(s, r) for r in regions for s in ('total', 'resident')]
print('%-6s %-34s ' % ('client', 'pid/comm') + ' '.join('%14s' % ('%s-%s' % (r, s[:3])) for s, r in cols) + ' (MiB)')
tot = dict.fromkeys(cols, 0.0)
for cid, c in sorted(clients.items(), key=lambda x: -sum(x[1]['mem'].get(('total', r), 0) for r in regions)):
for col in cols:
tot[col] += c['mem'].get(col, 0)
print('%-6s %-34s ' % (cid, ','.join(sorted(c['pids']))[:34]) + ' '.join('%14.1f' % (c['mem'].get(col, 0) / 1024) for col in cols))
print('%-6s %-34s ' % ('sum', '') + ' '.join('%14.1f' % (tot[col] / 1024) for col in cols))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment