From 02ee700893f19730084018685fa275c64d5be4d8 Mon Sep 17 00:00:00 2001 From: Peter Haag Date: Mon, 25 Sep 2023 11:02:09 +0200 Subject: [PATCH] Fix #467. Rework number of workers setting. Set max to 64 --- src/lib/nffile.c | 30 ++++++++++++++++++++---------- src/lib/nffile.h | 2 +- src/nfdump/nfdump.c | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/lib/nffile.c b/src/lib/nffile.c index 130298a6..d7008cee 100644 --- a/src/lib/nffile.c +++ b/src/lib/nffile.c @@ -163,23 +163,33 @@ int Init_nffile(int workers, queue_t *fileList) { #endif atomic_init(&blocksInUse, 0); - long CoresOnline; - if (workers) - CoresOnline = workers; - else - CoresOnline = sysconf(_SC_NPROCESSORS_ONLN); + // get conf value for maxworkers + int confMaxWorkers = ConfGetValue("maxworkers"); + if (confMaxWorkers == 0) confMaxWorkers = DEFAULTWORKERS; + + // set to default if not set + if (workers == 0) workers = confMaxWorkers; + + long CoresOnline = sysconf(_SC_NPROCESSORS_ONLN); if (CoresOnline < 0) { - LogError("sysconf() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno)); + LogError("sysconf(_SC_NPROCESSORS_ONLN) error in %s line %d: %s", __FILE__, __LINE__, strerror(errno)); CoresOnline = DEFAULTWORKERS; } - int confMaxWorkers = ConfGetValue("maxworkers"); - dbg_printf("MAXWORKERS: %d\n", confMaxWorkers); + // no more than cores online + if (workers > CoresOnline) { + LogError("Number of workers should not be greater than number of cores online. %d is > %d", workers, CoresOnline); + workers = CoresOnline; + } - if (confMaxWorkers <= 0) confMaxWorkers = MAXWORKERS; + // no more than internal array limit + if (workers > MAXWORKERS) { + LogError("Number of workers is limited to %s", MAXWORKERS); + workers = MAXWORKERS; + } - NumWorkers = CoresOnline > confMaxWorkers ? confMaxWorkers : CoresOnline; + NumWorkers = workers; return 1; } // End of Init_nffile diff --git a/src/lib/nffile.h b/src/lib/nffile.h index bd6f2674..e0914d92 100644 --- a/src/lib/nffile.h +++ b/src/lib/nffile.h @@ -184,7 +184,7 @@ typedef struct data_block_header_s { } data_block_headerV1_t; // allocate space for this number of workers -#define MAXWORKERS 16 +#define MAXWORKERS 64 // If number of CPUs can not be determined #define DEFAULTWORKERS 4 /* diff --git a/src/nfdump/nfdump.c b/src/nfdump/nfdump.c index 85b36a4e..755ced33 100644 --- a/src/nfdump/nfdump.c +++ b/src/nfdump/nfdump.c @@ -847,7 +847,7 @@ int main(int argc, char **argv) { case 'W': CheckArgLen(optarg, 16); worker = atoi(optarg); - if (worker <= 0 || worker > MAXWORKERS) { + if (worker < 0 || worker > MAXWORKERS) { LogError("Number of working threads out of range 1..%d", MAXWORKERS); exit(EXIT_FAILURE); }