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 for compression
22 static uint64_t memlimit_compress;
24 /// Memory usage limit for decompression
25 static uint64_t memlimit_decompress;
27 /// Total amount of physical RAM
28 static uint64_t total_ram;
32 hardware_threadlimit_set(uint32_t new_threadlimit)
34 if (new_threadlimit == 0) {
35 // The default is the number of available CPU cores.
36 threadlimit = tuklib_cpucores();
40 threadlimit = new_threadlimit;
48 hardware_threadlimit_get(void)
55 hardware_memlimit_set(uint64_t new_memlimit,
56 bool set_compress, bool set_decompress, bool is_percentage)
59 assert(new_memlimit > 0);
60 assert(new_memlimit <= 100);
61 new_memlimit = (uint32_t)new_memlimit * total_ram / 100;
65 memlimit_compress = new_memlimit;
68 memlimit_decompress = new_memlimit;
75 hardware_memlimit_get(enum operation_mode mode)
77 // Zero is a special value that indicates the default. Currently
78 // the default simply disables the limit. Once there is threading
79 // support, this might be a little more complex, because there will
80 // probably be a special case where a user asks for "optimal" number
81 // of threads instead of a specific number (this might even become
82 // the default mode). Each thread may use a significant amount of
83 // memory. When there are no memory usage limits set, we need some
84 // default soft limit for calculating the "optimal" number of
86 const uint64_t memlimit = mode == MODE_COMPRESS
87 ? memlimit_compress : memlimit_decompress;
88 return memlimit != 0 ? memlimit : UINT64_MAX;
92 /// Helper for hardware_memlimit_show() to print one human-readable info line.
94 memlimit_show(const char *str, uint64_t value)
96 // The memory usage limit is considered to be disabled if value
97 // is 0 or UINT64_MAX. This might get a bit more complex once there
98 // is threading support. See the comment in hardware_memlimit_get().
99 if (value == 0 || value == UINT64_MAX)
100 printf("%s %s\n", str, _("Disabled"));
102 printf("%s %s MiB (%s B)\n", str,
103 uint64_to_str(round_up_to_mib(value), 0),
104 uint64_to_str(value, 1));
111 hardware_memlimit_show(void)
114 printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram,
115 memlimit_compress, memlimit_decompress);
117 // TRANSLATORS: Test with "xz --info-memory" to see if
118 // the alignment looks nice.
119 memlimit_show(_("Total amount of physical memory (RAM): "),
121 memlimit_show(_("Memory usage limit for compression: "),
123 memlimit_show(_("Memory usage limit for decompression: "),
124 memlimit_decompress);
127 tuklib_exit(E_SUCCESS, E_ERROR, message_verbosity_get() != V_SILENT);
134 // Get the amount of RAM. If we cannot determine it,
135 // use the assumption defined by the configure script.
136 total_ram = lzma_physmem();
138 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
141 hardware_memlimit_set(0, true, true, false);
142 hardware_threadlimit_set(0);