Debian Codegraph @

cdist/conf/explorer/memory

raw symbols SCIP
1 #!/bin/sh -e
2 #
3 # 2014 Daniel Heule (hda at sfs.biz)
4 # 2014 Thomas Oettli (otho at sfs.biz)
5 # Copyright 2017, Philippe Gregoire <[email protected]>
6 # 2020 Dennis Camera <dennis.camera at ssrq-sds-fds.ch>
7 #
8 # This file is part of cdist.
9 #
10 # cdist is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # cdist is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with cdist. If not, see <http://www.gnu.org/licenses/>.
22 #
23 # Returns the amount of memory physically installed in the system, or if that
24 # cannot be determined the amount available to the operating system kernel,
25 # in kibibytes (kiB).
26
27 str2bytes() {
28 awk -F' ' '
29 $2 == "B" || !$2 { print $1 }
30 $2 == "kB" { printf "%.f\n", ($1 * 1000) }
31 $2 == "MB" { printf "%.f\n", ($1 * 1000 * 1000) }
32 $2 == "GB" { printf "%.f\n", ($1 * 1000 * 1000 * 1000) }
33 $2 == "TB" { printf "%.f\n", ($1 * 1000 * 1000 * 1000 * 1000) }
34 $2 == "kiB" { printf "%.f\n", ($1 * 1024) }
35 $2 == "MiB" { printf "%.f\n", ($1 * 1024 * 1024) }
36 $2 == "GiB" { printf "%.f\n", ($1 * 1024 * 1024 * 1024) }
37 $2 == "TiB" { printf "%.f\n", ($1 * 1024 * 1024 * 1024 * 1024) }'
38 }
39
40 bytes2kib() {
41 awk '$0 > 0 { printf "%.f\n", ($0 / 1024) }'
42 }
43
44
45 case $(uname -s)
46 in
47 (Darwin)
48 sysctl -n hw.memsize | bytes2kib
49 ;;
50 (FreeBSD)
51 sysctl -n hw.realmem | bytes2kib
52 ;;
53 (NetBSD|OpenBSD)
54 # NOTE: This reports "usable" memory, not physically installed memory.
55 command -p sysctl -n hw.physmem | bytes2kib
56 ;;
57 (SunOS)
58 # Make sure that awk from xpg4 is used for the scripts to work
59 export PATH="/usr/xpg4/bin:${PATH}"
60 prtconf \
61 | awk -F ': ' '
62 $1 == "Memory size" { sub(/Megabytes/, "MiB", $2); print $2 }
63 /^$/ { exit }' \
64 | str2bytes \
65 | bytes2kib
66 ;;
67 (Linux)
68 if test -d /sys/devices/system/memory
69 then
70 # Use memory blocks if the architecture (e.g. x86, PPC64, s390)
71 # supports them (they denote physical memory)
72 num_mem_blocks=$(cat /sys/devices/system/memory/memory[0-9]*/state | grep -cxF online)
73 mem_block_size=$(cat /sys/devices/system/memory/block_size_bytes)
74
75 echo $((num_mem_blocks * 0x$mem_block_size)) | bytes2kib && exit
76 fi
77 if test -r /proc/meminfo
78 then
79 # Fall back to meminfo file on other architectures (e.g. ARM, MIPS,
80 # PowerPC)
81 # NOTE: This is "usable" memory, not physically installed memory.
82 awk -F ': +' '$1 == "MemTotal" { sub(/B$/, "iB", $2); print $2 }' /proc/meminfo \
83 | str2bytes \
84 | bytes2kib
85 fi
86 ;;
87 (*)
88 printf "Your kernel (%s) is currently not supported by the memory explorer\n" "$(uname -s)" >&2
89 printf "Please contribute an implementation for it if you can.\n" >&2
90 exit 1
91 ;;
92 esac