From aeb0850441f7bf3f0f01f2db69146295d9204ef3 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sat, 11 Sep 2021 15:43:29 +0200 Subject: [PATCH] threading: respect SC_MAX_CPUS envvar --- src/util-cpu.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/util-cpu.c b/src/util-cpu.c index f21a338879..18223b8e10 100644 --- a/src/util-cpu.c +++ b/src/util-cpu.c @@ -58,8 +58,7 @@ uint16_t UtilCpuGetNumProcessorsConfigured(void) { #ifdef SYSCONF_NPROCESSORS_CONF_COMPAT - long nprocs = -1; - nprocs = sysconf(_SC_NPROCESSORS_CONF); + long nprocs = sysconf(_SC_NPROCESSORS_CONF); if (nprocs < 1) { SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus " "configured (%s)", strerror(errno)); @@ -75,9 +74,9 @@ uint16_t UtilCpuGetNumProcessorsConfigured(void) return (uint16_t)nprocs; #elif OS_WIN32 - long nprocs = -1; - const char* envvar = getenv("NUMBER_OF_PROCESSORS"); - nprocs = (NULL != envvar) ? atoi(envvar) : 0; + long nprocs = -1; + const char* envvar = getenv("NUMBER_OF_PROCESSORS"); + nprocs = (NULL != envvar) ? atoi(envvar) : 0; if (nprocs < 1) { SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus " "configured from the NUMBER_OF_PROCESSORS environment variable"); @@ -98,28 +97,33 @@ uint16_t UtilCpuGetNumProcessorsConfigured(void) */ uint16_t UtilCpuGetNumProcessorsOnline(void) { + long nprocs = 0; + const char *envvar = getenv("SC_MAX_CPUS"); + nprocs = (NULL != envvar) ? atoi(envvar) : 0; + if (nprocs > 1) { + return (uint16_t)nprocs; + } #ifdef SYSCONF_NPROCESSORS_ONLN_COMPAT - long nprocs = -1; nprocs = sysconf(_SC_NPROCESSORS_ONLN); if (nprocs < 1) { SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus " - "online (%s)", strerror(errno)); + "online (%s)", strerror(errno)); return 0; } if (nprocs > UINT16_MAX) { SCLogDebug("It seems that there are more than %d CPUs online. " - "You can modify util-cpu.{c,h} to use uint32_t to " - "support it", UINT16_MAX); + "You can modify util-cpu.{c,h} to use uint32_t to " + "support it", UINT16_MAX); return UINT16_MAX; } return nprocs; #elif OS_WIN32 - return UtilCpuGetNumProcessorsConfigured(); + return UtilCpuGetNumProcessorsConfigured(); #else SCLogError(SC_ERR_SYSCONF, "Couldn't retrieve the number of cpus online, " - "synconf macro unavailable"); + "synconf macro unavailable"); return 0; #endif } -- 2.47.2