aboutsummaryrefslogtreecommitdiff
path: root/src/xz/hardware.c
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2009-05-22 11:29:50 +0300
committerLasse Collin <lasse.collin@tukaani.org>2009-05-22 11:29:50 +0300
commitb0063023f8adb06ea735ec4af5c6f5b7bdb8e84d (patch)
tree7a875e927ad12b63bba7e3f56d3240ab7e691734 /src/xz/hardware.c
parentSupport special value "max" where xz and xzdec accept an integer. (diff)
downloadxz-b0063023f8adb06ea735ec4af5c6f5b7bdb8e84d.tar.xz
Make the default memory usage limit 40 % of RAM for both
compressing and decompressing. This should be OK now that xz automatically scales down the compression settings if they would exceed the memory usage limit (earlier, the limit for compression was increased to 90 % because low limit broke scripts that used "xz -9" on systems with low RAM). Support spcifying the memory usage limit as a percentage of RAM (e.g. --memory=50%). Support --threads=0 to reset the thread limit to the default value (number of available CPU cores). Use UINT32_MAX instead of SIZE_MAX as the maximum in args.c. hardware.c was already expecting uint32_t value. Cleaned up the output of --help and --long-help.
Diffstat (limited to 'src/xz/hardware.c')
-rw-r--r--src/xz/hardware.c86
1 files changed, 36 insertions, 50 deletions
diff --git a/src/xz/hardware.c b/src/xz/hardware.c
index 8d206b2e..72dc2cb1 100644
--- a/src/xz/hardware.c
+++ b/src/xz/hardware.c
@@ -17,51 +17,55 @@
/// Maximum number of free *coder* threads. This can be set with
/// the --threads=NUM command line option.
-static uint32_t threads_max;
+static uint32_t threadlimit;
+/// Memory usage limit
+static uint64_t memlimit;
-/// Memory usage limit for encoding
-static uint64_t memlimit_encoder;
-/// Memory usage limit for decoding
-static uint64_t memlimit_decoder;
-
-/// Memory usage limit given on the command line or environment variable.
-/// Zero indicates the default (memlimit_encoder or memlimit_decoder).
-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_threadlimit_init(void)
+extern void
+hardware_threadlimit_set(uint32_t new_threadlimit)
{
- threads_max = cpucores();
- if (threads_max == 0)
- threads_max = 1;
+ if (new_threadlimit == 0) {
+ // The default is the number of available CPU cores.
+ threadlimit = cpucores();
+ if (threadlimit == 0)
+ threadlimit = 1;
+ } else {
+ threadlimit = new_threadlimit;
+ }
return;
}
-extern void
-hardware_threadlimit_set(uint32_t threadlimit)
+extern uint32_t
+hardware_threadlimit_get(void)
{
- threads_max = threadlimit;
- return;
+ return threadlimit;
}
-extern uint32_t
-hardware_threadlimit_get(void)
+extern void
+hardware_memlimit_set(uint64_t new_memlimit)
{
- return threads_max;
+ if (new_memlimit == 0) {
+ // The default is 40 % of total installed physical RAM.
+ hardware_memlimit_set_percentage(40);
+ } else {
+ memlimit = new_memlimit;
+ }
+
+ return;
}
-static void
-hardware_memlimit_init(void)
+extern void
+hardware_memlimit_set_percentage(uint32_t percentage)
{
+ assert(percentage > 0);
+ assert(percentage <= 100);
+
uint64_t mem = physmem();
// If we cannot determine the amount of RAM, assume 32 MiB. Maybe
@@ -70,40 +74,22 @@ hardware_memlimit_init(void)
if (mem == 0)
mem = UINT64_C(32) * 1024 * 1024;
- // Use at maximum of 90 % of RAM when encoding and 33 % when decoding.
- memlimit_encoder = mem - mem / 10;
- memlimit_decoder = mem / 3;
-
+ memlimit = percentage * mem / 100;
return;
}
-extern void
-hardware_memlimit_set(uint64_t memlimit)
-{
- memlimit_custom = memlimit;
- return;
-}
-
-
-extern uint64_t
-hardware_memlimit_encoder(void)
-{
- return memlimit_custom != 0 ? memlimit_custom : memlimit_encoder;
-}
-
-
extern uint64_t
-hardware_memlimit_decoder(void)
+hardware_memlimit_get(void)
{
- return memlimit_custom != 0 ? memlimit_custom : memlimit_decoder;
+ return memlimit;
}
extern void
hardware_init(void)
{
- hardware_memlimit_init();
- hardware_threadlimit_init();
+ hardware_memlimit_set(0);
+ hardware_threadlimit_set(0);
return;
}