]> icculus.org git repositories - icculus/xz.git/blob - src/xz/hardware.c
Fix signal handling for --list.
[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_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
25 extern void
26 hardware_threadlimit_set(uint32_t new_threadlimit)
27 {
28         if (new_threadlimit == 0) {
29                 // The default is the number of available CPU cores.
30                 threadlimit = tuklib_cpucores();
31                 if (threadlimit == 0)
32                         threadlimit = 1;
33         } else {
34                 threadlimit = new_threadlimit;
35         }
36
37         return;
38 }
39
40
41 extern uint32_t
42 hardware_threadlimit_get(void)
43 {
44         return threadlimit;
45 }
46
47
48 extern void
49 hardware_memlimit_set(uint64_t new_memlimit)
50 {
51         if (new_memlimit == 0) {
52                 // The default is 40 % of total installed physical RAM.
53                 hardware_memlimit_set_percentage(40);
54         } else {
55                 memlimit = new_memlimit;
56         }
57
58         return;
59 }
60
61
62 extern void
63 hardware_memlimit_set_percentage(uint32_t percentage)
64 {
65         assert(percentage > 0);
66         assert(percentage <= 100);
67
68         uint64_t mem = lzma_physmem();
69
70         // If we cannot determine the amount of RAM, use the assumption
71         // defined by the configure script.
72         if (mem == 0)
73                 mem = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
74
75         memlimit = percentage * mem / 100;
76         return;
77 }
78
79
80 extern uint64_t
81 hardware_memlimit_get(void)
82 {
83         return memlimit;
84 }
85
86
87 extern void
88 hardware_init(void)
89 {
90         hardware_memlimit_set(0);
91         hardware_threadlimit_set(0);
92         return;
93 }