File: getAvailableMemory

package info (click to toggle)
assimp 5.4.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 100,104 kB
  • sloc: cpp: 164,162; cobol: 65,664; ansic: 16,520; xml: 11,246; python: 5,311; java: 2,303; sh: 398; objc: 122; pascal: 100; makefile: 66
file content (45 lines) | stat: -rwxr-xr-x 1,000 bytes parent folder | download | duplicates (3)
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
#!/bin/sh

error() {
    echo "$@" 1>&2
}

get_procmeminfo() {
    for f in "${@}"; do
        [ -f "${f}" ] || continue
        cat "${f}"  | egrep "^MemAvailable:" | while read _ chunks size; do
            case "${size}" in
                kB)
                    echo $((chunks * 1024))
                    ;;
                *)
                    error "ignoring unknown size specifier '${size}"
                    ;;
            esac
        done
    done
}

get_sysfs_cgroups() {
    for f in "${@}"; do
        [ -f "${f}" ] || continue
        cat "${f}"
    done
}

get_memlimits() {
    get_procmeminfo /proc/meminfo
    get_sysfs_cgroups /sys/fs/cgroup/memory/memory.limit_in_bytes /sys/fs/cgroup/memory.max
}

filter_memlimits() {
    local MB=$((1024*1024))
    egrep "^[0-9]*$" | grep . | while read num; do
        if [ ${num} -ge ${MB} ]; then
            echo $num
            error "limit ${num} bytes"
        fi
    done
}

get_memlimits | filter_memlimits | sort -n | head -1