Skip to content

Commit

Permalink
Add NVC_CONCURRENT_JOBS environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jun 30, 2024
1 parent 750a922 commit 0758f79
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
(#900).
- Fixed a crash when branch coverage is enabled and an if-statement
contains a `return` (#903).
- The `NVC_CONCURRENT_JOBS` environment variable can be used to scale
the number of worker threads NVC creates based on the number of
concurrently executing simulations.

## Version 1.12.2 - 2024-05-15
- Fixed a crash when `'transaction` is used with a record type.
Expand Down
16 changes: 15 additions & 1 deletion nvc.1
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,15 @@ arguments. Additionally foreign subprograms must not retain any
pointers passed as arguments after the subprogram returns. Violating
these rules will result in unpredictable and hard to debug behaviour.
.Sh ENVIRONMENT
.Bl -tag -width "NVC_COLORS"
.Bl -tag -width "NVC_CONCURRENT_JOBS"
.It Ev NVC_CONCURRENT_JOBS
Provides a hint for the number of concurrently executing simulations.
This allows
.Nm
to scale its worker thread count to avoid overloading the system.
This is set automatically by frameworks such as VUnit.
See
.Ev NVC_MAX_THREADS .
.It Ev NVC_COLORS
Controls whether
.Nm
Expand All @@ -1175,6 +1183,12 @@ and
which enables colour if stdout is connected to a terminal.
The default is
.Cm auto .
.It Ev NVC_MAX_THREADS
Limit the number of worker threads
.Nm
can create.
The default is either eight or the number of available CPUs, whichever
is smaller.
.El
.\" .Sh FILES
.\" .Sh EXIT STATUS
Expand Down
20 changes: 16 additions & 4 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -498,11 +499,22 @@ void thread_init(void)

assert(my_thread->id == 0);

const char *env = getenv("NVC_MAX_THREADS");
if (env != NULL)
max_workers = MAX(1, MIN(atoi(env), MAX_THREADS));
const char *max_env = getenv("NVC_MAX_THREADS");
if (max_env != NULL)
max_workers = MAX(1, MIN(atoi(max_env), MAX_THREADS));
else
max_workers = MIN(nvc_nprocs(), DEFAULT_THREADS);
max_workers = DEFAULT_THREADS;

const int num_cpus = nvc_nprocs();
max_workers = MIN(num_cpus, max_workers);

const char *jobs_env = getenv("NVC_CONCURRENT_JOBS");
if (jobs_env != NULL) {
const int num_jobs = MAX(1, atoi(jobs_env));
const int limit = (int)round((double)num_cpus / (double)num_jobs);
max_workers = MAX(1, MIN(max_workers, limit));
}

assert(max_workers > 0);

#ifdef DEBUG
Expand Down

0 comments on commit 0758f79

Please sign in to comment.