-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpucount.functions
83 lines (69 loc) · 2.57 KB
/
cpucount.functions
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
# This file is part of shellfire cpucount. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/cpucount/master/COPYRIGHT. No part of shellfire cpucount, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire cpucount. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/cpucount/master/COPYRIGHT.
# Finds number of hyperthreaded CPUs (ie double number of CPUs) - ignores cpuset, etc
core_dependency_requires '*' grep uname
# core_dependency_requires macosx
cpucount_findNumberOfHyperthreadedCpus()
{
# Windows, eg Msys
if core_variable_isSet NUMBER_OF_PROCESSORS; then
case "$NUMBER_OF_PROCESSORS" in
''|*[!0-9]*)
:
;;
*)
printf '%s' "$NUMBER_OF_PROCESSORS"
return 9
;;
esac
fi
# Should work under Linux and Cygwin
if [ -f /proc/cpuinfo ]; then
grep -E -c '^processor[[:space:]]*:[[:space:]]' /proc/cpuinfo
return 0
fi
local os="$(uname)"
case "$os" in
Darwin)
# machdep.cpu.core_count is number of CPUs
# In practice, all modern Macs are hyperthreaded
sysctl -n machdep.cpu.thread_count
;;
FreeBSD)
sysctl -n hw.ncpu
;;
*)
core_message NOTICE "Could not determine hyperthreaded CPU count for your operating system '$os'"
;;
esac
}
cpucount_computeMakeJobsAndLoadAverage()
{
# converts strings to 0, ie 'max' goes to 0
local configuredCpuCount=$1
local configuredLoadAverageCorrection=$2
local cpucount_discoveredNumberOfCpus="$(cpucount_findNumberOfHyperthreadedCpus)"
if [ $configuredCpuCount -eq 0 ]; then
cpucount_makeJobs=$cpucount_discoveredNumberOfCpus
elif [ $configuredCpuCount -lt 0 ]; then
# reduce number of cpus
local -i potentialCpus=$((cpucount_discoveredNumberOfCpus + configuredCpuCount))
if [ $potentialCpus -lt 1 ]; then
cpucount_makeJobs=1
else
cpucount_makeJobs=$potentialCpus
fi
else
cpucount_makeJobs=$cpucount_discoveredNumberOfCpus
fi
if [ $configuredLoadAverageCorrection -eq 0 ]; then
cpucount_makeLoadAverage="${cpucount_makeJobs}.00"
else
local remainingLoadAveragePercentage=$((100 - configuredLoadAverageCorrection))
local reducedCpus=$((cpucount_makeJobs - 1))
cpucount_makeLoadAverage="${reducedCpus}.${remainingLoadAveragePercentage}"
if [ $cpucount_makeLoadAverage = '0.00' ]; then
cpucount_makeLoadAverage=0.01
fi
fi
}