1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Detection of available hardware resources
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
14 #include "tuklib_cpucores.h"
17 /// Maximum number of free *coder* threads. This can be set with
18 /// the --threads=NUM command line option.
19 static uint32_t threadlimit;
21 /// Memory usage limit
22 static uint64_t memlimit;
24 /// Total amount of physical RAM
25 static uint64_t total_ram;
29 hardware_threadlimit_set(uint32_t new_threadlimit)
31 if (new_threadlimit == 0) {
32 // The default is the number of available CPU cores.
33 threadlimit = tuklib_cpucores();
37 threadlimit = new_threadlimit;
45 hardware_threadlimit_get(void)
52 hardware_memlimit_set(uint64_t new_memlimit)
54 if (new_memlimit != 0) {
55 memlimit = new_memlimit;
57 // The default depends on the amount of RAM but so that
58 // on "low-memory" systems the relative limit is higher
59 // to make it more likely that files created with "xz -9"
60 // will still decompress without overriding the limit
63 // If 40 % of RAM is 80 MiB or more, use 40 % of RAM as
65 memlimit = 40 * total_ram / 100;
66 if (memlimit < UINT64_C(80) * 1024 * 1024) {
67 // If 80 % of RAM is less than 80 MiB,
68 // use 80 % of RAM as the limit.
69 memlimit = 80 * total_ram / 100;
70 if (memlimit > UINT64_C(80) * 1024 * 1024) {
71 // Otherwise use 80 MiB as the limit.
72 memlimit = UINT64_C(80) * 1024 * 1024;
82 hardware_memlimit_set_percentage(uint32_t percentage)
84 assert(percentage > 0);
85 assert(percentage <= 100);
87 memlimit = percentage * total_ram / 100;
93 hardware_memlimit_get(void)
102 // Get the amount of RAM. If we cannot determine it,
103 // use the assumption defined by the configure script.
104 total_ram = lzma_physmem();
106 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
109 hardware_memlimit_set(0);
110 hardware_threadlimit_set(0);