]> icculus.org git repositories - icculus/xz.git/blob - src/lzma/hardware.c
Imported to git.
[icculus/xz.git] / src / lzma / 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
23
24 /// Maximum number of free *coder* threads. This can be set with
25 /// the --threads=NUM command line option.
26 size_t opt_threads = 1;
27
28
29 /// Number of bytes of memory to use at maximum (only a rough limit).
30 /// This can be set with the --memory=NUM command line option.
31 /// If no better value can be determined, the default is 14 MiB, which
32 /// should be quite safe even for older systems while still allowing
33 /// reasonable compression ratio.
34 size_t opt_memory = 14 * 1024 * 1024;
35
36
37 /// Get the amount of physical memory, and set opt_memory to 1/3 of it.
38 /// User can then override this with --memory command line option.
39 static void
40 hardware_memory(void)
41 {
42         uint64_t mem = physmem();
43         if (mem != 0) {
44                 mem /= 3;
45
46 #if UINT64_MAX > SIZE_MAX
47                 if (mem > SIZE_MAX)
48                         mem = SIZE_MAX;
49 #endif
50
51                 opt_memory = mem;
52         }
53
54         return;
55 }
56
57
58 /// Get the number of CPU cores, and set opt_threads to default to that value.
59 /// User can then override this with --threads command line option.
60 static void
61 hardware_cores(void)
62 {
63 #if defined(HAVE_NUM_PROCESSORS_SYSCONF)
64         const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
65         if (cpus > 0)
66                 opt_threads = (size_t)(cpus);
67
68 #elif defined(HAVE_NUM_PROCESSORS_SYSCTL)
69         int name[2] = { CTL_HW, HW_NCPU };
70         int cpus;
71         size_t cpus_size = sizeof(cpus);
72         if (!sysctl(name, &cpus, &cpus_size, NULL, NULL)
73                         && cpus_size == sizeof(cpus) && cpus > 0)
74                 opt_threads = (size_t)(cpus);
75 #endif
76
77         // Limit opt_threads so that maximum number of threads doesn't exceed.
78
79 #if defined(_SC_THREAD_THREADS_MAX)
80         const long threads_max = sysconf(_SC_THREAD_THREADS_MAX);
81         if (threads_max > 0 && (size_t)(threads_max) < opt_threads)
82                 opt_threads = (size_t)(threads_max);
83
84 #elif defined(PTHREAD_THREADS_MAX)
85         if (opt_threads > PTHREAD_THREADS_MAX)
86                 opt_threads = PTHREAD_THREADS_MAX;
87 #endif
88
89         return;
90 }
91
92
93 extern void
94 hardware_init(void)
95 {
96         hardware_memory();
97         hardware_cores();
98         return;
99 }