1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Detection of available hardware resources
6 // Copyright (C) 2007 Lasse Collin
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
25 /// Maximum number of free *coder* threads. This can be set with
26 /// the --threads=NUM command line option.
27 static uint32_t threads_max;
30 /// Memory usage limit for encoding
31 static uint64_t memlimit_encoder;
33 /// Memory usage limit for decoding
34 static uint64_t memlimit_decoder;
36 /// Memory usage limit given on the command line or environment variable.
37 /// Zero indicates the default (memlimit_encoder or memlimit_decoder).
38 static uint64_t memlimit_custom = 0;
41 /// Get the number of CPU cores, and set opt_threads to default to that value.
42 /// User can then override this with --threads command line option.
44 hardware_threadlimit_init(void)
46 threads_max = cpucores();
55 hardware_threadlimit_set(uint32_t threadlimit)
57 threads_max = threadlimit;
63 hardware_threadlimit_get(void)
70 hardware_memlimit_init(void)
72 uint64_t mem = physmem();
74 // If we cannot determine the amount of RAM, assume 32 MiB. Maybe
75 // even that is too much on some systems. But on most systems it's
76 // far too little, and can be annoying.
78 mem = UINT64_C(32) * 1024 * 1024;
80 // Use at maximum of 90 % of RAM when encoding and 33 % when decoding.
81 memlimit_encoder = mem - mem / 10;
82 memlimit_decoder = mem / 3;
89 hardware_memlimit_set(uint64_t memlimit)
91 memlimit_custom = memlimit;
97 hardware_memlimit_encoder(void)
99 return memlimit_custom != 0 ? memlimit_custom : memlimit_encoder;
104 hardware_memlimit_decoder(void)
106 return memlimit_custom != 0 ? memlimit_custom : memlimit_decoder;
113 hardware_memlimit_init();
114 hardware_threadlimit_init();