OLD | NEW |
1 /////////////////////////////////////////////////////////////////////////////// | 1 /////////////////////////////////////////////////////////////////////////////// |
2 // | 2 // |
3 /// \file hardware.c | 3 /// \file hardware.c |
4 /// \brief Detection of available hardware resources | 4 /// \brief Detection of available hardware resources |
5 // | 5 // |
6 // Author: Lasse Collin | 6 // Author: Lasse Collin |
7 // | 7 // |
8 // This file has been put into the public domain. | 8 // This file has been put into the public domain. |
9 // You can do whatever you want with this file. | 9 // You can do whatever you want with this file. |
10 // | 10 // |
11 /////////////////////////////////////////////////////////////////////////////// | 11 /////////////////////////////////////////////////////////////////////////////// |
12 | 12 |
13 #include "private.h" | 13 #include "private.h" |
14 #include "tuklib_cpucores.h" | 14 #include "tuklib_cpucores.h" |
15 | 15 |
16 | 16 |
17 /// Maximum number of free *coder* threads. This can be set with | 17 /// Maximum number of free *coder* threads. This can be set with |
18 /// the --threads=NUM command line option. | 18 /// the --threads=NUM command line option. |
19 static uint32_t threadlimit; | 19 static uint32_t threadlimit; |
20 | 20 |
21 /// Memory usage limit | 21 /// Memory usage limit for compression |
22 static uint64_t memlimit; | 22 static uint64_t memlimit_compress; |
| 23 |
| 24 /// Memory usage limit for decompression |
| 25 static uint64_t memlimit_decompress; |
23 | 26 |
24 /// Total amount of physical RAM | 27 /// Total amount of physical RAM |
25 static uint64_t total_ram; | 28 static uint64_t total_ram; |
26 | 29 |
27 | 30 |
28 extern void | 31 extern void |
29 hardware_threadlimit_set(uint32_t new_threadlimit) | 32 hardware_threadlimit_set(uint32_t new_threadlimit) |
30 { | 33 { |
31 if (new_threadlimit == 0) { | 34 if (new_threadlimit == 0) { |
32 // The default is the number of available CPU cores. | 35 // The default is the number of available CPU cores. |
33 threadlimit = tuklib_cpucores(); | 36 threadlimit = tuklib_cpucores(); |
34 if (threadlimit == 0) | 37 if (threadlimit == 0) |
35 threadlimit = 1; | 38 threadlimit = 1; |
36 } else { | 39 } else { |
37 threadlimit = new_threadlimit; | 40 threadlimit = new_threadlimit; |
38 } | 41 } |
39 | 42 |
40 return; | 43 return; |
41 } | 44 } |
42 | 45 |
43 | 46 |
44 extern uint32_t | 47 extern uint32_t |
45 hardware_threadlimit_get(void) | 48 hardware_threadlimit_get(void) |
46 { | 49 { |
47 return threadlimit; | 50 return threadlimit; |
48 } | 51 } |
49 | 52 |
50 | 53 |
51 extern void | 54 extern void |
52 hardware_memlimit_set(uint64_t new_memlimit) | 55 hardware_memlimit_set(uint64_t new_memlimit, |
| 56 » » bool set_compress, bool set_decompress, bool is_percentage) |
53 { | 57 { |
54 » if (new_memlimit != 0) { | 58 » if (is_percentage) { |
55 » » memlimit = new_memlimit; | 59 » » assert(new_memlimit > 0); |
56 » } else { | 60 » » assert(new_memlimit <= 100); |
57 » » // The default depends on the amount of RAM but so that | 61 » » new_memlimit = (uint32_t)new_memlimit * total_ram / 100; |
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 | |
61 » » // manually. | |
62 » » // | |
63 » » // If 40 % of RAM is 80 MiB or more, use 40 % of RAM as | |
64 » » // the limit. | |
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; | |
73 » » » } | |
74 » » } | |
75 } | 62 } |
76 | 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 |
77 return; | 106 return; |
78 } | 107 } |
79 | 108 |
80 | 109 |
81 extern void | 110 extern void |
82 hardware_memlimit_set_percentage(uint32_t percentage) | 111 hardware_memlimit_show(void) |
83 { | 112 { |
84 » assert(percentage > 0); | 113 » if (opt_robot) { |
85 » assert(percentage <= 100); | 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 » } |
86 | 126 |
87 » memlimit = percentage * total_ram / 100; | 127 » tuklib_exit(E_SUCCESS, E_ERROR, message_verbosity_get() != V_SILENT); |
88 » return; | |
89 } | 128 } |
90 | 129 |
91 | 130 |
92 extern uint64_t | |
93 hardware_memlimit_get(void) | |
94 { | |
95 return memlimit; | |
96 } | |
97 | |
98 | |
99 extern void | 131 extern void |
100 hardware_init(void) | 132 hardware_init(void) |
101 { | 133 { |
102 // Get the amount of RAM. If we cannot determine it, | 134 // Get the amount of RAM. If we cannot determine it, |
103 // use the assumption defined by the configure script. | 135 // use the assumption defined by the configure script. |
104 total_ram = lzma_physmem(); | 136 total_ram = lzma_physmem(); |
105 if (total_ram == 0) | 137 if (total_ram == 0) |
106 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024; | 138 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024; |
107 | 139 |
108 // Set the defaults. | 140 // Set the defaults. |
109 » hardware_memlimit_set(0); | 141 » hardware_memlimit_set(0, true, true, false); |
110 hardware_threadlimit_set(0); | 142 hardware_threadlimit_set(0); |
111 return; | 143 return; |
112 } | 144 } |
OLD | NEW |