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 ///////////////////////////////////////////////////////////////////////////////
16 /// Maximum number of worker threads. This can be set with
17 /// the --threads=NUM command line option.
18 static uint32_t threads_max = 1;
20 /// Memory usage limit for compression
21 static uint64_t memlimit_compress;
23 /// Memory usage limit for decompression
24 static uint64_t memlimit_decompress;
26 /// Total amount of physical RAM
27 static uint64_t total_ram;
31 hardware_threads_set(uint32_t n)
34 // Automatic number of threads was requested.
35 // If threading support was enabled at build time,
36 // use the number of available CPU cores. Otherwise
37 // use one thread since disabling threading support
38 // omits lzma_cputhreads() from liblzma.
39 #ifdef MYTHREAD_ENABLED
40 threads_max = lzma_cputhreads();
55 hardware_threads_get(void)
62 hardware_memlimit_set(uint64_t new_memlimit,
63 bool set_compress, bool set_decompress, bool is_percentage)
66 assert(new_memlimit > 0);
67 assert(new_memlimit <= 100);
68 new_memlimit = (uint32_t)new_memlimit * total_ram / 100;
72 memlimit_compress = new_memlimit;
75 memlimit_decompress = new_memlimit;
82 hardware_memlimit_get(enum operation_mode mode)
84 // Zero is a special value that indicates the default. Currently
85 // the default simply disables the limit. Once there is threading
86 // support, this might be a little more complex, because there will
87 // probably be a special case where a user asks for "optimal" number
88 // of threads instead of a specific number (this might even become
89 // the default mode). Each thread may use a significant amount of
90 // memory. When there are no memory usage limits set, we need some
91 // default soft limit for calculating the "optimal" number of
93 const uint64_t memlimit = mode == MODE_COMPRESS
94 ? memlimit_compress : memlimit_decompress;
95 return memlimit != 0 ? memlimit : UINT64_MAX;
99 /// Helper for hardware_memlimit_show() to print one human-readable info line.
101 memlimit_show(const char *str, uint64_t value)
103 // The memory usage limit is considered to be disabled if value
104 // is 0 or UINT64_MAX. This might get a bit more complex once there
105 // is threading support. See the comment in hardware_memlimit_get().
106 if (value == 0 || value == UINT64_MAX)
107 printf("%s %s\n", str, _("Disabled"));
109 printf("%s %s MiB (%s B)\n", str,
110 uint64_to_str(round_up_to_mib(value), 0),
111 uint64_to_str(value, 1));
118 hardware_memlimit_show(void)
121 printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram,
122 memlimit_compress, memlimit_decompress);
124 // TRANSLATORS: Test with "xz --info-memory" to see if
125 // the alignment looks nice.
126 memlimit_show(_("Total amount of physical memory (RAM): "),
128 memlimit_show(_("Memory usage limit for compression: "),
130 memlimit_show(_("Memory usage limit for decompression: "),
131 memlimit_decompress);
134 tuklib_exit(E_SUCCESS, E_ERROR, message_verbosity_get() != V_SILENT);
141 // Get the amount of RAM. If we cannot determine it,
142 // use the assumption defined by the configure script.
143 total_ram = lzma_physmem();
145 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
148 hardware_memlimit_set(0, true, true, false);