EGL: Severe per-frame CPU overhead on NVIDIA when apps unbind GLX context
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 79/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- cpp, linux
- Ambito
- backend, performance
Direzione di ricerca
Leggi server/backend.cpp, in particolare backend::makeCurrent() intorno alle righe 949–968, e confronta il relativo percorso di unbind EGL con le chiamate di rilascio in server/TempContext.h e server/TempContextEGL.h. Riproduci il ritardo per frame con lo snippet Python sotto Xvfb e vglrun -d egl, quindi verifica che l’unbind non provochi più aperture ripetute del dispositivo NVIDIA, mentre la pulizia del contesto temporaneo rimane intatta.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hi!
While working on an OpenGL audio plugin (built with DPF / Pugl) running headless under Xvfb + vglrun -d egl, I ran into ~80% CPU usage on the UI thread. Almost all of it (>90%) was spent in Linux kernel ioctls. Switching to the GLX back end (vglrun -d :0) immediately dropped the same binary to ~10% CPU load.
What's happening
Toolkits like Pugl/DPF (used by many LV2/VST3 audio plugins) bind and unbind their GLX context on every frame like so:
glXMakeCurrent(dpy, win, ctx);
// render ...
glXSwapBuffers(dpy, win);
glXMakeCurrent(dpy, None, NULL);
In commit 297b5d948fe8c61cac1d0b11e47ad9711b937139 (fixing #272), backend::makeCurrent() started calling _eglReleaseThread() whenever ctx == NULL:
On NVIDIA's proprietary EGL driver (tested on RTX 3080 Ti, driver 620.73.04, VirtualGL 3.1.5), calling eglReleaseThread() tears down the thread's NVIDIA device/modeset state (~10 ms), which forces the very next eglMakeCurrent() on the next frame to reopen /dev/nvidiactl, /dev/nvidia0, and /dev/nvidia-modeset from scratch (~30 ms).
Running strace -e openat shows ~20 /dev/nvidia* opens per frame (~40 ms of pure kernel overhead per frame). Without eglReleaseThread(), eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) and re-attaching takes ~0.1 ms total with zero /dev/nvidia* opens.
Why fixing this shouldn't break #272 or cause regressions
Issue #272 was specifically about non-OpenGL apps (like gedit or nm-applet) holding onto ~2 MB of GPU memory after VirtualGL's temporary GL_MAX_SAMPLES probe in glxvisual::buildCfgAttribTable(). That path is already handled cleanly by the _eglReleaseThread() calls in:
Those can stay as-is!
For real OpenGL applications calling glXMakeCurrent(dpy, None, NULL) in backend::makeCurrent(), the app still owns its live EGLContext anyway. So per-frame eglReleaseThread() doesn't actually free the app's context memory, it just thrashes the driver on every frame. Per the EGL spec (§3.12), eglReleaseThread() is intended for when a thread is finished using EGL, not for hot-loop context unbinds.
Simply removing _eglReleaseThread() from backend::makeCurrent() (and optionally calling it in backend::destroyContext() if the thread no longer has a current context) keeps #272 completely fixed while restoring normal frame times (~140× faster in this pattern) with no behavioral downside.
Minimal reproducer
No plugin or external deps needed. Just Xvfb, vglrun -d egl, and Python 3:
# repro_egl_detach.py
# Run with:
# Xvfb :197 -screen 0 640x480x24 &
# DISPLAY=:197 vglrun -d egl python3 repro_egl_detach.py
import ctypes
import time
x11 = ctypes.CDLL("libX11.so.6")
glx = ctypes.CDLL("libGLX.so.0")
x11.XOpenDisplay.restype = ctypes.c_void_p
x11.XDefaultScreen.argtypes = [ctypes.c_void_p]
x11.XRootWindow.argtypes = [ctypes.c_void_p, ctypes.c_int]
x11.XRootWindow.restype = ctypes.c_ulong
x11.XCreateSimpleWindow.argtypes = [
ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_int,
ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_ulong, ctypes.c_ulong,
]
x11.XCreateSimpleWindow.restype = ctypes.c_ulong
x11.XMapWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong]
x11.XSync.argtypes = [ctypes.c_void_p, ctypes.c_int]
class XVisualInfo(ctypes.Structure):
_fields_ = [
("visual", ctypes.c_void_p), ("visualid", ctypes.c_ulong),
("screen", ctypes.c_int), ("depth", ctypes.c_int),
("c_class", ctypes.c_int), ("red_mask", ctypes.c_ulong),
("green_mask", ctypes.c_ulong), ("blue_mask", ctypes.c_ulong),
("colormap_size", ctypes.c_int), ("bits_per_rgb", ctypes.c_int),
]
glx.glXChooseVisual.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
glx.glXChooseVisual.restype = ctypes.POINTER(XVisualInfo)
glx.glXCreateContext.argtypes = [ctypes.c_void_p, ctypes.POINTER(XVisualInfo), ctypes.c_void_p, ctypes.c_int]
glx.glXCreateContext.restype = ctypes.c_void_p
glx.glXMakeCurrent.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_void_p]
glx.glXSwapBuffers.argtypes = [ctypes.c_void_p, ctypes.c_ulong]
dpy = x11.XOpenDisplay(None)
scr = x11.XDefaultScreen(dpy)
vi = glx.glXChooseVisual(dpy, scr, (ctypes.c_int * 5)(4, 5, 12, 24, 0)) # RGBA, DOUBLEBUFFER, DEPTH_SIZE=24
win = x11.XCreateSimpleWindow(dpy, x11.XRootWindow(dpy, scr), 0, 0, 320, 240, 0, 0, 0)
x11.XMapWindow(dpy, win)
x11.XSync(dpy, 0)
ctx = glx.glXCreateContext(dpy, vi, None, 1)
for i in range(4):
t0 = time.monotonic()
glx.glXMakeCurrent(dpy, win, ctx)
t1 = time.monotonic()
glx.glXSwapBuffers(dpy, win)
t2 = time.monotonic()
glx.glXMakeCurrent(dpy, 0, None)
t3 = time.monotonic()
print(f"with detach frame {i}: attach={(t1-t0)*1000:5.2f}ms swap={(t2-t1)*1000:4.2f}ms detach={(t3-t2)*1000:5.2f}ms")
for i in range(4):
t0 = time.monotonic()
glx.glXMakeCurrent(dpy, win, ctx)
t1 = time.monotonic()
glx.glXSwapBuffers(dpy, win)
t2 = time.monotonic()
print(f"keep current frame {i}: attach={(t1-t0)*1000:5.2f}ms swap={(t2-t1)*1000:4.2f}ms")
Observed output (VirtualGL 3.1.5, NVIDIA 620.73.04, RTX 3080 Ti)
with detach frame 0: attach=74.10ms swap=2.81ms detach=11.18ms
with detach frame 1: attach=27.91ms swap=1.24ms detach=12.73ms
with detach frame 2: attach=27.10ms swap=1.06ms detach=10.97ms
with detach frame 3: attach=26.26ms swap=1.25ms detach=10.30ms
keep current frame 0: attach=26.95ms swap=1.21ms
keep current frame 1: attach= 0.06ms swap=0.22ms
keep current frame 2: attach= 0.10ms swap=1.02ms
keep current frame 3: attach= 0.06ms swap=0.22ms
(Same result with -d egl, -d egl0, and -d /dev/dri/card1.)
- Lingua principale
- C++
- Stelle
- 830
- Fork
- 116
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di VirtualGL/virtualgl
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
-
enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 18/100
Tutte le issue di VirtualGL/virtualgl
Issue simili
-
TransformInteract: a circle drag through the centre sets Scale to its minimum, which can't grow backApertadifficulty:easy prio:low type:bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
NatronGitHub/openfx-supportext#311 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
CachyOS/packageinstaller#211 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
OpenEmu-Silicon/OpenEmu-Silicon#920 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
documentation project: clr project: hip
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
ROCm/rocm-systems#17194 ·
I maintainer di solito rispondono entro 2 giorni