aboutsummaryrefslogtreecommitdiff
path: root/src/xz/hardware.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/xz/hardware.c43
1 files changed, 31 insertions, 12 deletions
diff --git a/src/xz/hardware.c b/src/xz/hardware.c
index d91b4cee..74742fce 100644
--- a/src/xz/hardware.c
+++ b/src/xz/hardware.c
@@ -21,6 +21,9 @@ static uint32_t threadlimit;
/// Memory usage limit
static uint64_t memlimit;
+/// Total amount of physical RAM
+static uint64_t total_ram;
+
extern void
hardware_threadlimit_set(uint32_t new_threadlimit)
@@ -48,11 +51,27 @@ hardware_threadlimit_get(void)
extern void
hardware_memlimit_set(uint64_t new_memlimit)
{
- if (new_memlimit == 0) {
- // The default is 40 % of total installed physical RAM.
- hardware_memlimit_set_percentage(40);
- } else {
+ if (new_memlimit != 0) {
memlimit = new_memlimit;
+ } else {
+ // The default depends on the amount of RAM but so that
+ // on "low-memory" systems the relative limit is higher
+ // to make it more likely that files created with "xz -9"
+ // will still decompress without overriding the limit
+ // manually.
+ //
+ // If 40 % of RAM is 80 MiB or more, use 40 % of RAM as
+ // the limit.
+ memlimit = 40 * total_ram / 100;
+ if (memlimit < UINT64_C(80) * 1024 * 1024) {
+ // If 80 % of RAM is less than 80 MiB,
+ // use 80 % of RAM as the limit.
+ memlimit = 80 * total_ram / 100;
+ if (memlimit > UINT64_C(80) * 1024 * 1024) {
+ // Otherwise use 80 MiB as the limit.
+ memlimit = UINT64_C(80) * 1024 * 1024;
+ }
+ }
}
return;
@@ -65,14 +84,7 @@ hardware_memlimit_set_percentage(uint32_t percentage)
assert(percentage > 0);
assert(percentage <= 100);
- uint64_t mem = lzma_physmem();
-
- // If we cannot determine the amount of RAM, use the assumption
- // defined by the configure script.
- if (mem == 0)
- mem = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
-
- memlimit = percentage * mem / 100;
+ memlimit = percentage * total_ram / 100;
return;
}
@@ -87,6 +99,13 @@ hardware_memlimit_get(void)
extern void
hardware_init(void)
{
+ // Get the amount of RAM. If we cannot determine it,
+ // use the assumption defined by the configure script.
+ total_ram = lzma_physmem();
+ if (total_ram == 0)
+ total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
+
+ // Set the defaults.
hardware_memlimit_set(0);
hardware_threadlimit_set(0);
return;