Rizin
unix-like reverse engineering framework and cli tools
hardware.h File Reference

Detection of available hardware resources. More...

Go to the source code of this file.

Functions

void hardware_init (void)
 
void hardware_threads_set (uint32_t threadlimit)
 Set the maximum number of worker threads. More...
 
uint32_t hardware_threads_get (void)
 Get the maximum number of worker threads. More...
 
void hardware_memlimit_set (uint64_t new_memlimit, bool set_compress, bool set_decompress, bool is_percentage)
 
uint64_t hardware_memlimit_get (enum operation_mode mode)
 Get the current memory usage limit for compression or decompression. More...
 
void hardware_memlimit_show (void) lzma_attribute((__noreturn__))
 Display the amount of RAM and memory usage limits and exit. More...
 

Detailed Description

Detection of available hardware resources.

Definition in file hardware.h.

Function Documentation

◆ hardware_init()

void hardware_init ( void  )

Initialize some hardware-specific variables, which are needed by other hardware_* functions.

Definition at line 169 of file hardware.c.

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 }
#define ASSUME_RAM
Definition: config.h:7
void hardware_memlimit_set(uint64_t new_memlimit, bool set_compress, bool set_decompress, bool is_percentage)
Definition: hardware.c:62
static uint64_t total_ram
Total amount of physical RAM.
Definition: hardware.c:27
unsigned long uint64_t
Definition: sftypes.h:28

References ASSUME_RAM, hardware_memlimit_set(), and total_ram.

Referenced by main().

◆ hardware_memlimit_get()

uint64_t hardware_memlimit_get ( enum operation_mode  mode)

Get the current memory usage limit for compression or decompression.

Definition at line 112 of file hardware.c.

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 }
@ MODE_COMPRESS
Definition: coder.h:14
uint64_t memlimit
Definition: container.h:537
static uint64_t memlimit_compress
Memory usage limit for compression.
Definition: hardware.c:21
static uint64_t memlimit_decompress
Memory usage limit for decompression.
Definition: hardware.c:24
const char int mode
Definition: ioapi.h:137
#define UINT64_MAX

References memlimit, memlimit_compress, memlimit_decompress, MODE_COMPRESS, and UINT64_MAX.

Referenced by coder_init(), message_mem_needed(), and parse_indexes().

◆ hardware_memlimit_set()

void hardware_memlimit_set ( uint64_t  new_memlimit,
bool  set_compress,
bool  set_decompress,
bool  is_percentage 
)

Set the memory usage limit. There are separate limits for compression and decompression (the latter includes also –list), one or both can be set with a single call to this function. Zero indicates resetting the limit back to the defaults. The limit can also be set as a percentage of installed RAM; the percentage must be in the range [1, 100].

Definition at line 62 of file hardware.c.

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 }
assert(limit<=UINT32_MAX/2)
unsigned int uint32_t
Definition: sftypes.h:29
#define UINT64_C(val)

References assert(), memlimit_compress, memlimit_decompress, total_ram, UINT64_C, and UINT64_MAX.

Referenced by hardware_init(), and parse_memlimit().

◆ hardware_memlimit_show()

void hardware_memlimit_show ( void  )

Display the amount of RAM and memory usage limits and exit.

Definition at line 148 of file hardware.c.

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 }
bool opt_robot
Definition: args.c:24
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
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
#define PRIu64
Definition: macros.h:18
@ E_SUCCESS
Definition: main.h:15
enum message_verbosity message_verbosity_get(void)
Get the current verbosity level.
Definition: message.c:181
@ V_SILENT
No messages.
Definition: message.h:15
#define _(String)
Definition: opintl.h:53
@ E_ERROR
Definition: transport.h:23
#define tuklib_exit
Definition: tuklib_exit.h:20

References _, E_ERROR, E_SUCCESS, memlimit_compress, memlimit_decompress, memlimit_show(), message_verbosity_get(), opt_robot, printf(), PRIu64, total_ram, tuklib_exit, and V_SILENT.

Referenced by parse_real().

◆ hardware_threads_get()

uint32_t hardware_threads_get ( void  )

Get the maximum number of worker threads.

Definition at line 55 of file hardware.c.

56 {
57  return threads_max;
58 }
static uint32_t threads_max
Definition: hardware.c:18

References threads_max.

Referenced by coder_init(), coder_normal(), and split_block().

◆ hardware_threads_set()

void hardware_threads_set ( uint32_t  threadlimit)

Set the maximum number of worker threads.

Definition at line 31 of file hardware.c.

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 }
int n
Definition: mipsasm.c:19

References n, and threads_max.

Referenced by coder_init(), and parse_real().