Skip to content

Commit 62a571c

Browse files
authored
Merge pull request #83 from elFarto/max_instances
Add option to limit maximum instances
2 parents 14e15d9 + cd34fb4 commit 62a571c

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ meson setup build
3333
meson install -C build
3434
```
3535

36-
# Debugging
36+
# Environment Variables
3737

38-
The `NVD_LOG` environment variable can be used to control logging, `NVD_LOG=1` will log to stdout, and `NVD_LOG=<filename>` will append to the specified file (or stdout if the file can't be opened).
38+
| Variable | Purpose |
39+
|---|---|
40+
|`NVD_LOG`|This environment variable can be used to control logging, if it's set to `1` it will log to stdout. If it is set to anything else, it will use that as the filename to append to (or stdout if the file can't be opened).|
41+
|`NVD_MAX_INSTANCES`|This environment variable controls the maximum concurrent instances of the driver will be allowed per-process. This option is only really useful for older GPUs with not much VRAM, especially with Firefox on video heavy websites. |
3942

4043
# Firefox
4144

src/vabackend.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
#include <time.h>
2929

30+
pthread_mutex_t concurrency_mutex;
31+
static uint32_t instances;
32+
static uint32_t max_instances = 0;
33+
3034
static CudaFunctions *cu;
3135
static CuvidFunctions *cv;
3236

@@ -58,6 +62,11 @@ static void init() {
5862
gpu = atoi(nvdGpu);
5963
}
6064

65+
char *nvdMaxInstances = getenv("NVD_MAX_INSTANCES");
66+
if (nvdMaxInstances != NULL) {
67+
max_instances = atoi(nvdMaxInstances);
68+
}
69+
6170
//try to detect the Firefox sandbox and skip loading CUDA if detected
6271
int fd = open("/proc/version", O_RDONLY);
6372
if (fd < 0) {
@@ -1752,6 +1761,11 @@ static VAStatus nvTerminate( VADriverContextP ctx )
17521761

17531762
cu->cuCtxDestroy(drv->cudaContext);
17541763

1764+
pthread_mutex_lock(&concurrency_mutex);
1765+
instances--;
1766+
LOG("Now have %d (%d max) instances", instances, max_instances);
1767+
pthread_mutex_unlock(&concurrency_mutex);
1768+
17551769
return VA_STATUS_SUCCESS;
17561770
}
17571771

@@ -1760,6 +1774,16 @@ VAStatus __vaDriverInit_1_0(VADriverContextP ctx)
17601774
{
17611775
LOG("Initialising NVIDIA VA-API Driver: %p %X", ctx, ctx->display_type);
17621776

1777+
pthread_mutex_lock(&concurrency_mutex);
1778+
LOG("Now have %d (%d max) instances", instances, max_instances);
1779+
if (max_instances > 0 && instances >= max_instances) {
1780+
pthread_mutex_unlock(&concurrency_mutex);
1781+
return VA_STATUS_ERROR_HW_BUSY;
1782+
}
1783+
instances++;
1784+
pthread_mutex_unlock(&concurrency_mutex);
1785+
1786+
17631787
if (gpu == -1 && (ctx->display_type & VA_DISPLAY_MAJOR_MASK) != VA_DISPLAY_DRM) {
17641788
LOG("Non-DRM display type detected, defaulting to GPU ID 0. Use NVD_GPU to pick a specific GPU.");
17651789
gpu = 0;

0 commit comments

Comments
 (0)