2007-12-08 17:42:33 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file hardware.c
|
|
|
|
/// \brief Detection of available hardware resources
|
|
|
|
//
|
2009-04-13 04:27:40 -04:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 17:42:33 -05:00
|
|
|
//
|
2009-04-13 04:27:40 -04:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 17:42:33 -05:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "private.h"
|
2009-09-19 02:47:30 -04:00
|
|
|
#include "tuklib_cpucores.h"
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
/// Maximum number of free *coder* threads. This can be set with
|
|
|
|
/// the --threads=NUM command line option.
|
2009-05-22 04:29:50 -04:00
|
|
|
static uint32_t threadlimit;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2009-05-22 04:29:50 -04:00
|
|
|
/// Memory usage limit
|
|
|
|
static uint64_t memlimit;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2010-03-07 06:29:28 -05:00
|
|
|
/// Total amount of physical RAM
|
|
|
|
static uint64_t total_ram;
|
|
|
|
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2009-05-22 04:29:50 -04:00
|
|
|
extern void
|
|
|
|
hardware_threadlimit_set(uint32_t new_threadlimit)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2009-05-22 04:29:50 -04:00
|
|
|
if (new_threadlimit == 0) {
|
|
|
|
// The default is the number of available CPU cores.
|
2009-09-19 02:47:30 -04:00
|
|
|
threadlimit = tuklib_cpucores();
|
2009-05-22 04:29:50 -04:00
|
|
|
if (threadlimit == 0)
|
|
|
|
threadlimit = 1;
|
|
|
|
} else {
|
|
|
|
threadlimit = new_threadlimit;
|
|
|
|
}
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 04:29:50 -04:00
|
|
|
extern uint32_t
|
|
|
|
hardware_threadlimit_get(void)
|
2009-02-13 17:45:29 -05:00
|
|
|
{
|
2009-05-22 04:29:50 -04:00
|
|
|
return threadlimit;
|
2009-02-13 17:45:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 04:29:50 -04:00
|
|
|
extern void
|
|
|
|
hardware_memlimit_set(uint64_t new_memlimit)
|
2009-02-13 17:45:29 -05:00
|
|
|
{
|
2010-03-07 06:29:28 -05:00
|
|
|
if (new_memlimit != 0) {
|
2009-05-22 04:29:50 -04:00
|
|
|
memlimit = new_memlimit;
|
2010-03-07 06:29:28 -05:00
|
|
|
} else {
|
|
|
|
// The default depends on the amount of RAM but so that
|
|
|
|
// on "low-memory" systems the relative limit is higher
|
|
|
|
// to make it more likely that files created with "xz -9"
|
|
|
|
// will still decompress without overriding the limit
|
|
|
|
// manually.
|
|
|
|
//
|
|
|
|
// If 40 % of RAM is 80 MiB or more, use 40 % of RAM as
|
|
|
|
// the limit.
|
|
|
|
memlimit = 40 * total_ram / 100;
|
|
|
|
if (memlimit < UINT64_C(80) * 1024 * 1024) {
|
|
|
|
// If 80 % of RAM is less than 80 MiB,
|
|
|
|
// use 80 % of RAM as the limit.
|
|
|
|
memlimit = 80 * total_ram / 100;
|
|
|
|
if (memlimit > UINT64_C(80) * 1024 * 1024) {
|
|
|
|
// Otherwise use 80 MiB as the limit.
|
|
|
|
memlimit = UINT64_C(80) * 1024 * 1024;
|
|
|
|
}
|
|
|
|
}
|
2009-05-22 04:29:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2009-02-13 17:45:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 04:29:50 -04:00
|
|
|
extern void
|
|
|
|
hardware_memlimit_set_percentage(uint32_t percentage)
|
2008-11-19 13:46:52 -05:00
|
|
|
{
|
2009-05-22 04:29:50 -04:00
|
|
|
assert(percentage > 0);
|
|
|
|
assert(percentage <= 100);
|
|
|
|
|
2010-03-07 06:29:28 -05:00
|
|
|
memlimit = percentage * total_ram / 100;
|
2008-11-19 13:46:52 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern uint64_t
|
2009-05-22 04:29:50 -04:00
|
|
|
hardware_memlimit_get(void)
|
2008-11-19 13:46:52 -05:00
|
|
|
{
|
2009-05-22 04:29:50 -04:00
|
|
|
return memlimit;
|
2008-11-19 13:46:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 17:42:33 -05:00
|
|
|
extern void
|
|
|
|
hardware_init(void)
|
|
|
|
{
|
2010-03-07 06:29:28 -05: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.
|
2009-05-22 04:29:50 -04:00
|
|
|
hardware_memlimit_set(0);
|
|
|
|
hardware_threadlimit_set(0);
|
2007-12-08 17:42:33 -05:00
|
|
|
return;
|
|
|
|
}
|