2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file hardware.c
|
|
|
|
/// \brief Detection of available hardware resources
|
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "private.h"
|
|
|
|
|
|
|
|
|
2011-04-09 14:11:13 +02:00
|
|
|
/// Maximum number of worker threads. This can be set with
|
2007-12-08 23:42:33 +01:00
|
|
|
/// the --threads=NUM command line option.
|
2011-04-09 14:11:13 +02:00
|
|
|
static uint32_t threads_max = 1;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
/// Memory usage limit for compression
|
|
|
|
static uint64_t memlimit_compress;
|
|
|
|
|
|
|
|
/// Memory usage limit for decompression
|
|
|
|
static uint64_t memlimit_decompress;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2010-03-07 12:29:28 +01:00
|
|
|
/// Total amount of physical RAM
|
|
|
|
static uint64_t total_ram;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2009-05-22 10:29:50 +02:00
|
|
|
extern void
|
2011-04-09 14:11:13 +02:00
|
|
|
hardware_threads_set(uint32_t n)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2011-04-09 14:11:13 +02:00
|
|
|
if (n == 0) {
|
|
|
|
// Automatic number of threads was requested.
|
2014-06-18 21:07:06 +02:00
|
|
|
// If threading support was enabled at build time,
|
|
|
|
// use the number of available CPU cores. Otherwise
|
|
|
|
// use one thread since disabling threading support
|
|
|
|
// omits lzma_cputhreads() from liblzma.
|
|
|
|
#ifdef MYTHREAD_ENABLED
|
|
|
|
threads_max = lzma_cputhreads();
|
2011-04-09 14:11:13 +02:00
|
|
|
if (threads_max == 0)
|
|
|
|
threads_max = 1;
|
2014-06-18 21:07:06 +02:00
|
|
|
#else
|
|
|
|
threads_max = 1;
|
|
|
|
#endif
|
2009-05-22 10:29:50 +02:00
|
|
|
} else {
|
2011-04-09 14:11:13 +02:00
|
|
|
threads_max = n;
|
2009-05-22 10:29:50 +02:00
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 10:29:50 +02:00
|
|
|
extern uint32_t
|
2011-04-09 14:11:13 +02:00
|
|
|
hardware_threads_get(void)
|
2009-02-13 23:45:29 +01:00
|
|
|
{
|
2011-04-09 14:11:13 +02:00
|
|
|
return threads_max;
|
2009-02-13 23:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 10:29:50 +02:00
|
|
|
extern void
|
2010-08-07 19:45:18 +02:00
|
|
|
hardware_memlimit_set(uint64_t new_memlimit,
|
|
|
|
bool set_compress, bool set_decompress, bool is_percentage)
|
2009-02-13 23:45:29 +01:00
|
|
|
{
|
2010-08-07 19:45:18 +02:00
|
|
|
if (is_percentage) {
|
|
|
|
assert(new_memlimit > 0);
|
|
|
|
assert(new_memlimit <= 100);
|
|
|
|
new_memlimit = (uint32_t)new_memlimit * total_ram / 100;
|
2009-05-22 10:29:50 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
if (set_compress)
|
|
|
|
memlimit_compress = new_memlimit;
|
|
|
|
|
|
|
|
if (set_decompress)
|
|
|
|
memlimit_decompress = new_memlimit;
|
|
|
|
|
2009-05-22 10:29:50 +02:00
|
|
|
return;
|
2009-02-13 23:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
extern uint64_t
|
|
|
|
hardware_memlimit_get(enum operation_mode mode)
|
2008-11-19 19:46:52 +01:00
|
|
|
{
|
2010-08-07 19:45:18 +02:00
|
|
|
// Zero is a special value that indicates the default. Currently
|
|
|
|
// the default simply disables the limit. Once there is threading
|
|
|
|
// support, this might be a little more complex, because there will
|
|
|
|
// probably be a special case where a user asks for "optimal" number
|
|
|
|
// of threads instead of a specific number (this might even become
|
|
|
|
// the default mode). Each thread may use a significant amount of
|
|
|
|
// memory. When there are no memory usage limits set, we need some
|
|
|
|
// default soft limit for calculating the "optimal" number of
|
|
|
|
// threads.
|
|
|
|
const uint64_t memlimit = mode == MODE_COMPRESS
|
|
|
|
? memlimit_compress : memlimit_decompress;
|
|
|
|
return memlimit != 0 ? memlimit : UINT64_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Helper for hardware_memlimit_show() to print one human-readable info line.
|
|
|
|
static void
|
|
|
|
memlimit_show(const char *str, uint64_t value)
|
|
|
|
{
|
|
|
|
// The memory usage limit is considered to be disabled if value
|
|
|
|
// is 0 or UINT64_MAX. This might get a bit more complex once there
|
|
|
|
// is threading support. See the comment in hardware_memlimit_get().
|
|
|
|
if (value == 0 || value == UINT64_MAX)
|
|
|
|
printf("%s %s\n", str, _("Disabled"));
|
|
|
|
else
|
|
|
|
printf("%s %s MiB (%s B)\n", str,
|
|
|
|
uint64_to_str(round_up_to_mib(value), 0),
|
|
|
|
uint64_to_str(value, 1));
|
2009-05-22 10:29:50 +02:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
extern void
|
|
|
|
hardware_memlimit_show(void)
|
2008-11-19 19:46:52 +01:00
|
|
|
{
|
2010-08-07 19:45:18 +02:00
|
|
|
if (opt_robot) {
|
|
|
|
printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram,
|
|
|
|
memlimit_compress, memlimit_decompress);
|
|
|
|
} else {
|
2010-09-07 11:31:40 +02:00
|
|
|
// TRANSLATORS: Test with "xz --info-memory" to see if
|
|
|
|
// the alignment looks nice.
|
2010-08-07 19:45:18 +02:00
|
|
|
memlimit_show(_("Total amount of physical memory (RAM): "),
|
|
|
|
total_ram);
|
|
|
|
memlimit_show(_("Memory usage limit for compression: "),
|
|
|
|
memlimit_compress);
|
|
|
|
memlimit_show(_("Memory usage limit for decompression: "),
|
|
|
|
memlimit_decompress);
|
|
|
|
}
|
|
|
|
|
|
|
|
tuklib_exit(E_SUCCESS, E_ERROR, message_verbosity_get() != V_SILENT);
|
2008-11-19 19:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
extern void
|
|
|
|
hardware_init(void)
|
|
|
|
{
|
2010-03-07 12:29:28 +01:00
|
|
|
// Get the amount of RAM. If we cannot determine it,
|
|
|
|
// use the assumption defined by the configure script.
|
|
|
|
total_ram = lzma_physmem();
|
|
|
|
if (total_ram == 0)
|
|
|
|
total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
|
|
|
|
|
|
|
|
// Set the defaults.
|
2010-08-07 19:45:18 +02:00
|
|
|
hardware_memlimit_set(0, true, true, false);
|
2007-12-08 23:42:33 +01:00
|
|
|
return;
|
|
|
|
}
|