]> icculus.org git repositories - icculus/xz.git/blob - src/xz/hardware.c
Add support for --enable-assume-ram=SIZE.
[icculus/xz.git] / src / xz / hardware.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       hardware.c
4 /// \brief      Detection of available hardware resources
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "private.h"
14 #include "tuklib_physmem.h"
15 #include "tuklib_cpucores.h"
16
17
18 /// Maximum number of free *coder* threads. This can be set with
19 /// the --threads=NUM command line option.
20 static uint32_t threadlimit;
21
22 /// Memory usage limit
23 static uint64_t memlimit;
24
25
26 extern void
27 hardware_threadlimit_set(uint32_t new_threadlimit)
28 {
29         if (new_threadlimit == 0) {
30                 // The default is the number of available CPU cores.
31                 threadlimit = tuklib_cpucores();
32                 if (threadlimit == 0)
33                         threadlimit = 1;
34         } else {
35                 threadlimit = new_threadlimit;
36         }
37
38         return;
39 }
40
41
42 extern uint32_t
43 hardware_threadlimit_get(void)
44 {
45         return threadlimit;
46 }
47
48
49 extern void
50 hardware_memlimit_set(uint64_t new_memlimit)
51 {
52         if (new_memlimit == 0) {
53                 // The default is 40 % of total installed physical RAM.
54                 hardware_memlimit_set_percentage(40);
55         } else {
56                 memlimit = new_memlimit;
57         }
58
59         return;
60 }
61
62
63 extern void
64 hardware_memlimit_set_percentage(uint32_t percentage)
65 {
66         assert(percentage > 0);
67         assert(percentage <= 100);
68
69         uint64_t mem = tuklib_physmem();
70
71         // If we cannot determine the amount of RAM, use the assumption
72         // defined by the configure script.
73         if (mem == 0)
74                 mem = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
75
76         memlimit = percentage * mem / 100;
77         return;
78 }
79
80
81 extern uint64_t
82 hardware_memlimit_get(void)
83 {
84         return memlimit;
85 }
86
87
88 extern void
89 hardware_init(void)
90 {
91         hardware_memlimit_set(0);
92         hardware_threadlimit_set(0);
93         return;
94 }