diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2025-02-27 11:54:49 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2025-02-27 11:54:49 -0500 |
commit | 5798978f77ef8c3efb3c99fa7fb9538c5c597024 (patch) | |
tree | 32d3fbcff0910b4c17a7b4b6048d2e0631d2abfe | |
parent | a561d782265c2157f270f0ead6b88db24e433d92 (diff) | |
download | bfs-5798978f77ef8c3efb3c99fa7fb9538c5c597024.tar.xz |
bfstd: New nproc() function
-rw-r--r-- | src/bfstd.c | 10 | ||||
-rw-r--r-- | src/bfstd.h | 5 | ||||
-rw-r--r-- | src/ctx.c | 21 |
3 files changed, 21 insertions, 15 deletions
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 <wchar.h> /** @@ -24,20 +24,6 @@ #include <time.h> #include <unistd.h> -/** 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); |