1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Detection of available hardware resources
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
18 /// Maximum number of free *coder* threads. This can be set with
19 /// the --threads=NUM command line option.
20 static uint32_t threads_max;
23 /// Memory usage limit for encoding
24 static uint64_t memlimit_encoder;
26 /// Memory usage limit for decoding
27 static uint64_t memlimit_decoder;
29 /// Memory usage limit given on the command line or environment variable.
30 /// Zero indicates the default (memlimit_encoder or memlimit_decoder).
31 static uint64_t memlimit_custom = 0;
34 /// Get the number of CPU cores, and set opt_threads to default to that value.
35 /// User can then override this with --threads command line option.
37 hardware_threadlimit_init(void)
39 threads_max = cpucores();
48 hardware_threadlimit_set(uint32_t threadlimit)
50 threads_max = threadlimit;
56 hardware_threadlimit_get(void)
63 hardware_memlimit_init(void)
65 uint64_t mem = physmem();
67 // If we cannot determine the amount of RAM, assume 32 MiB. Maybe
68 // even that is too much on some systems. But on most systems it's
69 // far too little, and can be annoying.
71 mem = UINT64_C(32) * 1024 * 1024;
73 // Use at maximum of 90 % of RAM when encoding and 33 % when decoding.
74 memlimit_encoder = mem - mem / 10;
75 memlimit_decoder = mem / 3;
82 hardware_memlimit_set(uint64_t memlimit)
84 memlimit_custom = memlimit;
90 hardware_memlimit_encoder(void)
92 return memlimit_custom != 0 ? memlimit_custom : memlimit_encoder;
97 hardware_memlimit_decoder(void)
99 return memlimit_custom != 0 ? memlimit_custom : memlimit_decoder;
106 hardware_memlimit_init();
107 hardware_threadlimit_init();