]> icculus.org git repositories - icculus/xz.git/blob - src/xz/hardware.c
0695ccb1acb7358c31243b94e1bfa1a2a989454b
[icculus/xz.git] / src / xz / hardware.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       hardware.c
4 /// \brief      Detection of available hardware resources
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "private.h"
21 #include "physmem.h"
22 #include "cpucores.h"
23
24
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;
28
29
30 /// Memory usage limit for encoding
31 static uint64_t memlimit_encoder;
32
33 /// Memory usage limit for decoding
34 static uint64_t memlimit_decoder;
35
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;
39
40
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.
43 static void
44 hardware_threadlimit_init(void)
45 {
46         threads_max = cpucores();
47         if (threads_max == 0)
48                 threads_max = 1;
49
50         return;
51 }
52
53
54 extern void
55 hardware_threadlimit_set(uint32_t threadlimit)
56 {
57         threads_max = threadlimit;
58         return;
59 }
60
61
62 extern uint32_t
63 hardware_threadlimit_get(void)
64 {
65         return threads_max;
66 }
67
68
69 static void
70 hardware_memlimit_init(void)
71 {
72         uint64_t mem = physmem();
73
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.
77         if (mem == 0)
78                 mem = UINT64_C(32) * 1024 * 1024;
79
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;
83
84         return;
85 }
86
87
88 extern void
89 hardware_memlimit_set(uint64_t memlimit)
90 {
91         memlimit_custom = memlimit;
92         return;
93 }
94
95
96 extern uint64_t
97 hardware_memlimit_encoder(void)
98 {
99         return memlimit_custom != 0 ? memlimit_custom : memlimit_encoder;
100 }
101
102
103 extern uint64_t
104 hardware_memlimit_decoder(void)
105 {
106         return memlimit_custom != 0 ? memlimit_custom : memlimit_decoder;
107 }
108
109
110 extern void
111 hardware_init(void)
112 {
113         hardware_memlimit_init();
114         hardware_threadlimit_init();
115         return;
116 }