Rizin
unix-like reverse engineering framework and cli tools
hardware.c
Go to the documentation of this file.
1 //
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 //
12 
13 #include "private.h"
14 
15 
18 static uint32_t threads_max = 1;
19 
22 
25 
28 
29 
30 extern void
32 {
33  if (n == 0) {
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();
41  if (threads_max == 0)
42  threads_max = 1;
43 #else
44  threads_max = 1;
45 #endif
46  } else {
47  threads_max = n;
48  }
49 
50  return;
51 }
52 
53 
54 extern uint32_t
56 {
57  return threads_max;
58 }
59 
60 
61 extern void
63  bool set_compress, bool set_decompress, bool is_percentage)
64 {
65  if (is_percentage) {
66  assert(new_memlimit > 0);
67  assert(new_memlimit <= 100);
68  new_memlimit = (uint32_t)new_memlimit * total_ram / 100;
69  }
70 
71  if (set_compress) {
72  memlimit_compress = new_memlimit;
73 
74 #if SIZE_MAX == UINT32_MAX
75  // FIXME?
76  //
77  // When running a 32-bit xz on a system with a lot of RAM and
78  // using a percentage-based memory limit, the result can be
79  // bigger than the 32-bit address space. Limiting the limit
80  // below SIZE_MAX for compression (not decompression) makes
81  // xz lower the compression settings (or number of threads)
82  // to a level that *might* work. In practice it has worked
83  // when using a 64-bit kernel that gives full 4 GiB address
84  // space to 32-bit programs. In other situations this might
85  // still be too high, like 32-bit kernels that may give much
86  // less than 4 GiB to a single application.
87  //
88  // So this is an ugly hack but I will keep it here while
89  // it does more good than bad.
90  //
91  // Use a value less than SIZE_MAX so that there's some room
92  // for the xz program and so on. Don't use 4000 MiB because
93  // it could look like someone mixed up base-2 and base-10.
94  const uint64_t limit_max = UINT64_C(4020) << 20;
95 
96  // UINT64_MAX is a special case for the string "max" so
97  // that has to be handled specially.
99  && memlimit_compress > limit_max)
100  memlimit_compress = limit_max;
101 #endif
102  }
103 
104  if (set_decompress)
105  memlimit_decompress = new_memlimit;
106 
107  return;
108 }
109 
110 
111 extern uint64_t
113 {
114  // Zero is a special value that indicates the default. Currently
115  // the default simply disables the limit. Once there is threading
116  // support, this might be a little more complex, because there will
117  // probably be a special case where a user asks for "optimal" number
118  // of threads instead of a specific number (this might even become
119  // the default mode). Each thread may use a significant amount of
120  // memory. When there are no memory usage limits set, we need some
121  // default soft limit for calculating the "optimal" number of
122  // threads.
125  return memlimit != 0 ? memlimit : UINT64_MAX;
126 }
127 
128 
130 static void
132 {
133  // The memory usage limit is considered to be disabled if value
134  // is 0 or UINT64_MAX. This might get a bit more complex once there
135  // is threading support. See the comment in hardware_memlimit_get().
136  if (value == 0 || value == UINT64_MAX)
137  printf("%s %s\n", str, _("Disabled"));
138  else
139  printf("%s %s MiB (%s B)\n", str,
141  uint64_to_str(value, 1));
142 
143  return;
144 }
145 
146 
147 extern void
149 {
150  if (opt_robot) {
151  printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram,
153  } else {
154  // TRANSLATORS: Test with "xz --info-memory" to see if
155  // the alignment looks nice.
156  memlimit_show(_("Total amount of physical memory (RAM): "),
157  total_ram);
158  memlimit_show(_("Memory usage limit for compression: "),
160  memlimit_show(_("Memory usage limit for decompression: "),
162  }
163 
165 }
166 
167 
168 extern void
170 {
171  // Get the amount of RAM. If we cannot determine it,
172  // use the assumption defined by the configure script.
173  total_ram = lzma_physmem();
174  if (total_ram == 0)
175  total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
176 
177  // Set the defaults.
178  hardware_memlimit_set(0, true, true, false);
179  return;
180 }
bool opt_robot
Definition: args.c:24
#define ASSUME_RAM
Definition: config.h:7
static int value
Definition: cmd_api.c:93
operation_mode
Definition: coder.h:13
@ MODE_COMPRESS
Definition: coder.h:14
uint64_t memlimit
Definition: container.h:537
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
void hardware_memlimit_set(uint64_t new_memlimit, bool set_compress, bool set_decompress, bool is_percentage)
Definition: hardware.c:62
void hardware_threads_set(uint32_t n)
Set the maximum number of worker threads.
Definition: hardware.c:31
static uint64_t total_ram
Total amount of physical RAM.
Definition: hardware.c:27
void hardware_init(void)
Definition: hardware.c:169
static uint64_t memlimit_compress
Memory usage limit for compression.
Definition: hardware.c:21
uint32_t hardware_threads_get(void)
Get the maximum number of worker threads.
Definition: hardware.c:55
void hardware_memlimit_show(void)
Display the amount of RAM and memory usage limits and exit.
Definition: hardware.c:148
static uint32_t threads_max
Definition: hardware.c:18
static void memlimit_show(const char *str, uint64_t value)
Helper for hardware_memlimit_show() to print one human-readable info line.
Definition: hardware.c:131
static uint64_t memlimit_decompress
Memory usage limit for decompression.
Definition: hardware.c:24
uint64_t hardware_memlimit_get(enum operation_mode mode)
Get the current memory usage limit for compression or decompression.
Definition: hardware.c:112
const char int mode
Definition: ioapi.h:137
#define PRIu64
Definition: macros.h:18
@ E_SUCCESS
Definition: main.h:15
assert(limit<=UINT32_MAX/2)
enum message_verbosity message_verbosity_get(void)
Get the current verbosity level.
Definition: message.c:181
@ V_SILENT
No messages.
Definition: message.h:15
int n
Definition: mipsasm.c:19
#define _(String)
Definition: opintl.h:53
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
#define UINT64_MAX
#define UINT64_C(val)
Common includes, definitions, and prototypes.
const char * uint64_to_str(uint64_t value, uint32_t slot)
Convert uint64_t to a string.
Definition: util.c:171
uint64_t round_up_to_mib(uint64_t n)
Round an integer up to the next full MiB and convert to MiB.
Definition: util.c:139
@ E_ERROR
Definition: transport.h:23
#define tuklib_exit
Definition: tuklib_exit.h:20