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"
|
|
|
|
#include "physmem.h"
|
2009-02-13 17:45:29 -05:00
|
|
|
#include "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-02-13 17:45:29 -05:00
|
|
|
static uint32_t threads_max;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
/// Memory usage limit for encoding
|
|
|
|
static uint64_t memlimit_encoder;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
/// Memory usage limit for decoding
|
|
|
|
static uint64_t memlimit_decoder;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
/// Memory usage limit given on the command line or environment variable.
|
|
|
|
/// Zero indicates the default (memlimit_encoder or memlimit_decoder).
|
|
|
|
static uint64_t memlimit_custom = 0;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
/// Get the number of CPU cores, and set opt_threads to default to that value.
|
|
|
|
/// User can then override this with --threads command line option.
|
|
|
|
static void
|
2009-02-13 17:45:29 -05:00
|
|
|
hardware_threadlimit_init(void)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2009-02-13 17:45:29 -05:00
|
|
|
threads_max = cpucores();
|
|
|
|
if (threads_max == 0)
|
|
|
|
threads_max = 1;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-13 17:45:29 -05:00
|
|
|
extern void
|
|
|
|
hardware_threadlimit_set(uint32_t threadlimit)
|
|
|
|
{
|
|
|
|
threads_max = threadlimit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern uint32_t
|
|
|
|
hardware_threadlimit_get(void)
|
|
|
|
{
|
|
|
|
return threads_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
static void
|
|
|
|
hardware_memlimit_init(void)
|
|
|
|
{
|
|
|
|
uint64_t mem = physmem();
|
|
|
|
|
|
|
|
// If we cannot determine the amount of RAM, assume 32 MiB. Maybe
|
|
|
|
// even that is too much on some systems. But on most systems it's
|
|
|
|
// far too little, and can be annoying.
|
|
|
|
if (mem == 0)
|
2009-02-07 10:07:52 -05:00
|
|
|
mem = UINT64_C(32) * 1024 * 1024;
|
2008-11-19 13:46:52 -05:00
|
|
|
|
|
|
|
// Use at maximum of 90 % of RAM when encoding and 33 % when decoding.
|
|
|
|
memlimit_encoder = mem - mem / 10;
|
|
|
|
memlimit_decoder = mem / 3;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern void
|
|
|
|
hardware_memlimit_set(uint64_t memlimit)
|
|
|
|
{
|
|
|
|
memlimit_custom = memlimit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern uint64_t
|
|
|
|
hardware_memlimit_encoder(void)
|
|
|
|
{
|
|
|
|
return memlimit_custom != 0 ? memlimit_custom : memlimit_encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern uint64_t
|
|
|
|
hardware_memlimit_decoder(void)
|
|
|
|
{
|
|
|
|
return memlimit_custom != 0 ? memlimit_custom : memlimit_decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 17:42:33 -05:00
|
|
|
extern void
|
|
|
|
hardware_init(void)
|
|
|
|
{
|
2008-11-19 13:46:52 -05:00
|
|
|
hardware_memlimit_init();
|
2009-02-13 17:45:29 -05:00
|
|
|
hardware_threadlimit_init();
|
2007-12-08 17:42:33 -05:00
|
|
|
return;
|
|
|
|
}
|