kspalaiologos / bzip3

A better and stronger spiritual successor to BZip2.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: Change the behavior when `-j` is `0`

sorairolake opened this issue · comments

-T of xz and zstd uses as many worker threads as there are physical CPU cores when the value is 0. So, I propose that -j behaves similarly to these.

bzip3 -j 0 README.md
# equivalent to
bzip3 -j "$(nproc)" README.md

We want a C analogue of std::thread::hardware_concurrency(). Maybe determine the amount of CPUs by task affinity (sched_getaffinity - Linux-specific), sysconf (GNU-only), get_nprocs (also GNU), or maybe read /proc/cpuinfo... Another possibility is pthread_getaffinity_np, or NetBSD 5+ GNU's sched_getaffinity_np, on Windows we would want GetProcessAffinityMask. In practice, the sysconf appears to work on glibc, Mac OS X 10.5, FreeBSD, AIX, OSF/1, Solaris, Cygwin, Haiku. HP-UX would require pstat_getdynamic, IRIX uses sysmp; as a fallback on Windows platforms we could use GetSystemInfo. I will look into it.

sysctl() may also be used for this purpose on BSDs.12 Also, I think it should return 1 if the number of CPU cores cannot be detected.

References

Footnotes

  1. https://man.freebsd.org/cgi/man.cgi?sysctl(3)

  2. https://man.openbsd.org/sysctl.2