From 5798978f77ef8c3efb3c99fa7fb9538c5c597024 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 27 Feb 2025 11:54:49 -0500 Subject: bfstd: New nproc() function --- src/bfstd.c | 10 ++++++++++ src/bfstd.h | 5 +++++ src/ctx.c | 21 ++++++--------------- 3 files changed, 21 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/bfstd.c b/src/bfstd.c index 219b8d0..aee6930 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -775,6 +775,16 @@ long xsysconf(int name) { return ret; } +long nproc(void) { + long nproc = xsysconf(_SC_NPROCESSORS_ONLN); + + if (nproc < 1) { + return 1; + } else { + return nproc; + } +} + size_t asciilen(const char *str) { return asciinlen(str, strlen(str)); } diff --git a/src/bfstd.h b/src/bfstd.h index 84f92ec..28f473e 100644 --- a/src/bfstd.h +++ b/src/bfstd.h @@ -463,6 +463,11 @@ long xsysconf(int name); #define sysoption(name) \ (_POSIX_##name == 0 ? xsysconf(_SC_##name) : _POSIX_##name) +/** + * Get the number of CPU threads available to the current process. + */ +long nproc(void); + #include /** diff --git a/src/ctx.c b/src/ctx.c index 2c55a35..d92d8ba 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -24,20 +24,6 @@ #include #include -/** Get the initial value for ctx->threads (-j). */ -static int bfs_nproc(void) { - long nproc = xsysconf(_SC_NPROCESSORS_ONLN); - - if (nproc < 1) { - nproc = 1; - } else if (nproc > 8) { - // Not much speedup after 8 threads - nproc = 8; - } - - return nproc; -} - struct bfs_ctx *bfs_ctx_new(void) { struct bfs_ctx *ctx = ZALLOC(struct bfs_ctx); if (!ctx) { @@ -50,9 +36,14 @@ struct bfs_ctx *bfs_ctx_new(void) { ctx->maxdepth = INT_MAX; ctx->flags = BFTW_RECOVER; ctx->strategy = BFTW_BFS; - ctx->threads = bfs_nproc(); ctx->optlevel = 3; + ctx->threads = nproc(); + if (ctx->threads > 8) { + // Not much speedup after 8 threads + ctx->threads = 8; + } + trie_init(&ctx->files); ctx->umask = umask(0); -- cgit v1.2.3