From 3084d662d2646ab7eb58daf0dc32cf3f9a74eec7 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Sat, 14 Feb 2009 00:45:29 +0200 Subject: Cleanups to the code that detects the amount of RAM and the number of CPU cores. Added support for using sysinfo() on Linux systems whose libc lacks appropriate sysconf() support (at least dietlibc). The Autoconf macros were split into separate files, and CPU core count detection was moved from hardware.c to cpucores.h. The core count isn't used for anything real for now, so a problematic part in process.c was commented out. --- src/xz/args.c | 4 ++-- src/xz/hardware.c | 50 ++++++++++++++++++++++---------------------------- src/xz/hardware.h | 11 ++++++++--- src/xz/message.c | 5 +++-- src/xz/process.c | 2 ++ 5 files changed, 37 insertions(+), 35 deletions(-) (limited to 'src/xz') diff --git a/src/xz/args.c b/src/xz/args.c index 29abf971..f8732000 100644 --- a/src/xz/args.c +++ b/src/xz/args.c @@ -135,8 +135,8 @@ parse_real(args_info *args, int argc, char **argv) break; case 'T': - opt_threads = str_to_uint64("threads", optarg, - 1, SIZE_MAX); + hardware_threadlimit_set(str_to_uint64( + "threads", optarg, 1, SIZE_MAX)); break; // --version diff --git a/src/xz/hardware.c b/src/xz/hardware.c index 0c372726..0695ccb1 100644 --- a/src/xz/hardware.c +++ b/src/xz/hardware.c @@ -19,11 +19,12 @@ #include "private.h" #include "physmem.h" +#include "cpucores.h" /// Maximum number of free *coder* threads. This can be set with /// the --threads=NUM command line option. -size_t opt_threads = 1; +static uint32_t threads_max; /// Memory usage limit for encoding @@ -40,38 +41,31 @@ static uint64_t memlimit_custom = 0; /// Get the number of CPU cores, and set opt_threads to default to that value. /// User can then override this with --threads command line option. static void -hardware_cores(void) +hardware_threadlimit_init(void) { -#if defined(HAVE_NUM_PROCESSORS_SYSCONF) - const long cpus = sysconf(_SC_NPROCESSORS_ONLN); - if (cpus > 0) - opt_threads = (size_t)(cpus); - -#elif defined(HAVE_NUM_PROCESSORS_SYSCTL) - int name[2] = { CTL_HW, HW_NCPU }; - int cpus; - size_t cpus_size = sizeof(cpus); - if (!sysctl(name, &cpus, &cpus_size, NULL, NULL) - && cpus_size == sizeof(cpus) && cpus > 0) - opt_threads = (size_t)(cpus); -#endif - - // Limit opt_threads so that maximum number of threads doesn't exceed. - -#if defined(_SC_THREAD_THREADS_MAX) - const long threads_max = sysconf(_SC_THREAD_THREADS_MAX); - if (threads_max > 0 && (size_t)(threads_max) < opt_threads) - opt_threads = (size_t)(threads_max); - -#elif defined(PTHREAD_THREADS_MAX) - if (opt_threads > PTHREAD_THREADS_MAX) - opt_threads = PTHREAD_THREADS_MAX; -#endif + threads_max = cpucores(); + if (threads_max == 0) + threads_max = 1; return; } +extern void +hardware_threadlimit_set(uint32_t threadlimit) +{ + threads_max = threadlimit; + return; +} + + +extern uint32_t +hardware_threadlimit_get(void) +{ + return threads_max; +} + + static void hardware_memlimit_init(void) { @@ -117,6 +111,6 @@ extern void hardware_init(void) { hardware_memlimit_init(); - hardware_cores(); + hardware_threadlimit_init(); return; } diff --git a/src/xz/hardware.h b/src/xz/hardware.h index a6d91d78..36761fd1 100644 --- a/src/xz/hardware.h +++ b/src/xz/hardware.h @@ -17,14 +17,19 @@ // /////////////////////////////////////////////////////////////////////////////// -extern size_t opt_threads; - - /// Initialize some hardware-specific variables, which are needed by other /// hardware_* functions. extern void hardware_init(void); +/// Set custom value for maximum number of coder threads. +extern void hardware_threadlimit_set(uint32_t threadlimit); + +/// Get the maximum number of coder threads. Some additional helper threads +/// are allowed on top of this). +extern uint32_t hardware_threadlimit_get(void); + + /// Set custom memory usage limit. This is used for both encoding and /// decoding. Zero indicates resetting the limit back to defaults. extern void hardware_memlimit_set(uint64_t memlimit); diff --git a/src/xz/message.c b/src/xz/message.c index fd519c8f..544572b8 100644 --- a/src/xz/message.c +++ b/src/xz/message.c @@ -1029,8 +1029,9 @@ message_help(bool long_help) hardware_memlimit_encoder() / (1024 * 1024), hardware_memlimit_decoder() / (1024 * 1024)); printf(N_(" * one thread for (de)compression.\n\n", - " * %'" PRIu64 " threads for (de)compression.\n\n", - (uint64_t)(opt_threads)), (uint64_t)(opt_threads)); + " * %'" PRIu32 " threads for (de)compression.\n\n", + hardware_threadlimit_get()), + hardware_threadlimit_get()); } printf(_("Report bugs to <%s> (in English or Finnish).\n"), diff --git a/src/xz/process.c b/src/xz/process.c index 9b966546..efe363ce 100644 --- a/src/xz/process.c +++ b/src/xz/process.c @@ -246,6 +246,7 @@ coder_set_compression_settings(void) memory_limit >> 20); } +/* // Limit the number of worker threads so that memory usage // limit isn't exceeded. assert(memory_usage > 0); @@ -255,6 +256,7 @@ coder_set_compression_settings(void) if (opt_threads > thread_limit) opt_threads = thread_limit; +*/ return; } -- cgit v1.2.3