1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 /// \file hardware.c 4 /// \brief Detection of available hardware resources 5 // 6 // Author: Lasse Collin 7 // 8 // This file has been put into the public domain. 9 // You can do whatever you want with this file. 10 // 11 /////////////////////////////////////////////////////////////////////////////// 12 13 #include "private.h" 14 #include "tuklib_cpucores.h" 15 16 17 /// Maximum number of free *coder* threads. This can be set with 18 /// the --threads=NUM command line option. 19 static uint32_t threadlimit; 20 21 /// Memory usage limit for compression 22 static uint64_t memlimit_compress; 23 24 /// Memory usage limit for decompression 25 static uint64_t memlimit_decompress; 26 27 /// Total amount of physical RAM 28 static uint64_t total_ram; 29 30 31 extern void 32 hardware_threadlimit_set(uint32_t new_threadlimit) 33 { 34 if (new_threadlimit == 0) { 35 // The default is the number of available CPU cores. 36 threadlimit = tuklib_cpucores(); 37 if (threadlimit == 0) 38 threadlimit = 1; 39 } else { 40 threadlimit = new_threadlimit; 41 } 42 43 return; 44 } 45 46 47 extern uint32_t 48 hardware_threadlimit_get(void) 49 { 50 return threadlimit; 51 } 52 53 54 extern void 55 hardware_memlimit_set(uint64_t new_memlimit, 56 bool set_compress, bool set_decompress, bool is_percentage) 57 { 58 if (is_percentage) { 59 assert(new_memlimit > 0); 60 assert(new_memlimit <= 100); 61 new_memlimit = (uint32_t)new_memlimit * total_ram / 100; 62 } 63 64 if (set_compress) 65 memlimit_compress = new_memlimit; 66 67 if (set_decompress) 68 memlimit_decompress = new_memlimit; 69 70 return; 71 } 72 73 74 extern uint64_t 75 hardware_memlimit_get(enum operation_mode mode) 76 { 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 85 // threads. 86 const uint64_t memlimit = mode == MODE_COMPRESS 87 ? memlimit_compress : memlimit_decompress; 88 return memlimit != 0 ? memlimit : UINT64_MAX; 89 } 90 91 92 /// Helper for hardware_memlimit_show() to print one human-readable info line. 93 static void 94 memlimit_show(const char *str, uint64_t value) 95 { 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")); 101 else 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)); 105 106 return; 107 } 108 109 110 extern void 111 hardware_memlimit_show(void) 112 { 113 if (opt_robot) { 114 printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram, 115 memlimit_compress, memlimit_decompress); 116 } else { 117 // TRANSLATORS: Test with "xz --info-memory" to see if 118 // the alignment looks nice. 119 memlimit_show(_("Total amount of physical memory (RAM): "), 120 total_ram); 121 memlimit_show(_("Memory usage limit for compression: "), 122 memlimit_compress); 123 memlimit_show(_("Memory usage limit for decompression: "), 124 memlimit_decompress); 125 } 126 127 tuklib_exit(E_SUCCESS, E_ERROR, message_verbosity_get() != V_SILENT); 128 } 129 130 131 extern void 132 hardware_init(void) 133 { 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(); 137 if (total_ram == 0) 138 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024; 139 140 // Set the defaults. 141 hardware_memlimit_set(0, true, true, false); 142 hardware_threadlimit_set(0); 143 return; 144 } 145