Skip to content

Commit

Permalink
Merge pull request #83 from elFarto/max_instances
Browse files Browse the repository at this point in the history
Add option to limit maximum instances
  • Loading branch information
elFarto authored May 14, 2022
2 parents 14e15d9 + cd34fb4 commit 62a571c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ meson setup build
meson install -C build
```

# Debugging
# Environment Variables

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).
| Variable | Purpose |
|---|---|
|`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).|
|`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. |

# Firefox

Expand Down
24 changes: 24 additions & 0 deletions src/vabackend.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

#include <time.h>

pthread_mutex_t concurrency_mutex;
static uint32_t instances;
static uint32_t max_instances = 0;

static CudaFunctions *cu;
static CuvidFunctions *cv;

Expand Down Expand Up @@ -58,6 +62,11 @@ static void init() {
gpu = atoi(nvdGpu);
}

char *nvdMaxInstances = getenv("NVD_MAX_INSTANCES");
if (nvdMaxInstances != NULL) {
max_instances = atoi(nvdMaxInstances);
}

//try to detect the Firefox sandbox and skip loading CUDA if detected
int fd = open("/proc/version", O_RDONLY);
if (fd < 0) {
Expand Down Expand Up @@ -1752,6 +1761,11 @@ static VAStatus nvTerminate( VADriverContextP ctx )

cu->cuCtxDestroy(drv->cudaContext);

pthread_mutex_lock(&concurrency_mutex);
instances--;
LOG("Now have %d (%d max) instances", instances, max_instances);
pthread_mutex_unlock(&concurrency_mutex);

return VA_STATUS_SUCCESS;
}

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

pthread_mutex_lock(&concurrency_mutex);
LOG("Now have %d (%d max) instances", instances, max_instances);
if (max_instances > 0 && instances >= max_instances) {
pthread_mutex_unlock(&concurrency_mutex);
return VA_STATUS_ERROR_HW_BUSY;
}
instances++;
pthread_mutex_unlock(&concurrency_mutex);


if (gpu == -1 && (ctx->display_type & VA_DISPLAY_MAJOR_MASK) != VA_DISPLAY_DRM) {
LOG("Non-DRM display type detected, defaulting to GPU ID 0. Use NVD_GPU to pick a specific GPU.");
gpu = 0;
Expand Down

0 comments on commit 62a571c

Please sign in to comment.