]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/xz/src/xz/hardware.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / xz / 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_cpucores.h"
15
16
17 /// Maximum number of free *coder* threads. This can be set with
18 /// the --threads=NUM command line option.
19 static uint32_t threadlimit;
20
21 /// Memory usage limit
22 static uint64_t memlimit;
23
24 /// Total amount of physical RAM
25 static uint64_t total_ram;
26
27
28 extern void
29 hardware_threadlimit_set(uint32_t new_threadlimit)
30 {
31         if (new_threadlimit == 0) {
32                 // The default is the number of available CPU cores.
33                 threadlimit = tuklib_cpucores();
34                 if (threadlimit == 0)
35                         threadlimit = 1;
36         } else {
37                 threadlimit = new_threadlimit;
38         }
39
40         return;
41 }
42
43
44 extern uint32_t
45 hardware_threadlimit_get(void)
46 {
47         return threadlimit;
48 }
49
50
51 extern void
52 hardware_memlimit_set(uint64_t new_memlimit)
53 {
54         if (new_memlimit != 0) {
55                 memlimit = new_memlimit;
56         } else {
57                 // The default depends on the amount of RAM but so that
58                 // on "low-memory" systems the relative limit is higher
59                 // to make it more likely that files created with "xz -9"
60                 // will still decompress without overriding the limit
61                 // manually.
62                 //
63                 // If 40 % of RAM is 80 MiB or more, use 40 % of RAM as
64                 // the limit.
65                 memlimit = 40 * total_ram / 100;
66                 if (memlimit < UINT64_C(80) * 1024 * 1024) {
67                         // If 80 % of RAM is less than 80 MiB,
68                         // use 80 % of RAM as the limit.
69                         memlimit = 80 * total_ram / 100;
70                         if (memlimit > UINT64_C(80) * 1024 * 1024) {
71                                 // Otherwise use 80 MiB as the limit.
72                                 memlimit = UINT64_C(80) * 1024 * 1024;
73                         }
74                 }
75         }
76
77         return;
78 }
79
80
81 extern void
82 hardware_memlimit_set_percentage(uint32_t percentage)
83 {
84         assert(percentage > 0);
85         assert(percentage <= 100);
86
87         memlimit = percentage * total_ram / 100;
88         return;
89 }
90
91
92 extern uint64_t
93 hardware_memlimit_get(void)
94 {
95         return memlimit;
96 }
97
98
99 extern void
100 hardware_init(void)
101 {
102         // Get the amount of RAM. If we cannot determine it,
103         // use the assumption defined by the configure script.
104         total_ram = lzma_physmem();
105         if (total_ram == 0)
106                 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
107
108         // Set the defaults.
109         hardware_memlimit_set(0);
110         hardware_threadlimit_set(0);
111         return;
112 }