aboutsummaryrefslogtreecommitdiff
path: root/src/lzma/hardware.c
blob: 6cb3cdfc3eb45caf3ae7f40cd874da4a591de70f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
///////////////////////////////////////////////////////////////////////////////
//
/// \file       hardware.c
/// \brief      Detection of available hardware resources
//
//  Copyright (C) 2007 Lasse Collin
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "private.h"
#include "physmem.h"


/// Maximum number of free *coder* threads. This can be set with
/// the --threads=NUM command line option.
size_t opt_threads = 1;


/// Number of bytes of memory to use at maximum (only a rough limit).
/// This can be set with the --memory=NUM command line option.
/// If no better value can be determined, the default is 14 MiB, which
/// should be quite safe even for older systems while still allowing
/// reasonable compression ratio.
size_t opt_memory = 14 * 1024 * 1024;


/// Get the amount of physical memory, and set opt_memory to 1/3 of it.
/// User can then override this with --memory command line option.
static void
hardware_memory(void)
{
	uint64_t mem = physmem();
	if (mem != 0) {
		mem /= 3;

#if UINT64_MAX > SIZE_MAX
		if (mem > SIZE_MAX)
			mem = SIZE_MAX;
#endif

		opt_memory = mem;
	}

	return;
}


/// Get the number of CPU cores, and set opt_threads to default to that value.
/// User can then override this with --threads command line option.
static void
hardware_cores(void)
{
#if defined(HAVE_NUM_PROCESSORS_SYSCONF)
	const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
	if (cpus > 0)
		opt_threads = (size_t)(cpus);

#elif defined(HAVE_NUM_PROCESSORS_SYSCTL)
	int name[2] = { CTL_HW, HW_NCPU };
	int cpus;
	size_t cpus_size = sizeof(cpus);
	if (!sysctl(name, &cpus, &cpus_size, NULL, NULL)
			&& cpus_size == sizeof(cpus) && cpus > 0)
		opt_threads = (size_t)(cpus);
#endif

	// Limit opt_threads so that maximum number of threads doesn't exceed.

#if defined(_SC_THREAD_THREADS_MAX)
	const long threads_max = sysconf(_SC_THREAD_THREADS_MAX);
	if (threads_max > 0 && (size_t)(threads_max) < opt_threads)
		opt_threads = (size_t)(threads_max);

#elif defined(PTHREAD_THREADS_MAX)
	if (opt_threads > PTHREAD_THREADS_MAX)
		opt_threads = PTHREAD_THREADS_MAX;
#endif

	return;
}


extern void
hardware_init(void)
{
	hardware_memory();
	hardware_cores();
	return;
}