[go: up one dir, main page]

File: zramswap

package info (click to toggle)
zram-tools 0.3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72 kB
  • sloc: sh: 65; makefile: 2
file content (100 lines) | stat: -rwxr-xr-x 2,826 bytes parent folder | download
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
100
#!/bin/bash
# This script does the following:
# zramswap start:
#  * Space is assigned to each zram device, then swap is initialized on
#    there
# zramswap stop:
#  * Undo start
#  * Also attempts to remove zram module at the end
# TODO:
# * Migrate to using zramctl from util-linux for the setup,
#   (this will close debian bug #917643):
#   then also:
#   - add option for compression algorythm
#   - ammount of compression streams
#   - Make use of the zramctl stats too

function start {
    #Set some defaults:
    ALLOCATION=256 # ZRAM Swap you want assigned, in MiB
    PRIORITY=100   # Swap priority, see swapon(2) for more details

    # Get amount of available CPU cores, set to 1 if not detected correctly
    if [ ! -f /proc/cpuinfo ]; then
        echo "WARNING: Can't find /proc/cpuinfo, is proc mounted?"
        echo "         Using a single core for zramswap..."
        CORES=1
    else
        CORES=$(grep -c ^processor /proc/cpuinfo)
    fi

    # Override  above from config file, if it exists
    if [ -f /etc/default/zramswap ]; then
        . /etc/default/zramswap
    fi

    ALLOCATION=$((ALLOCATION * 1024 * 1024)) # convert amount from MiB to bytes

    if [ -n "$PERCENTAGE" ]; then
        totalmemory=$(awk '/MemTotal/{print $2}' /proc/meminfo) # in KiB
        ALLOCATION=$((totalmemory * 1024 * $PERCENTAGE / 100))
    fi

    # Initialize zram devices
    modprobe zram num_devices=$CORES

    # Assign memory to zram devices, initialize swap and activate
    # Decrementing $CORE, because cores start counting at 0
    for CORE in $(seq 0 $(($CORES - 1))); do
        echo $(($ALLOCATION / $CORES)) > /sys/block/zram$CORE/disksize
        mkswap /dev/zram$CORE
        swapon -p $PRIORITY /dev/zram$CORE
    done
}

function status {
    orig_data_size="0"
    for file in /sys/block/zram*/*_data_size ; do
        if [ $file = "/sys/block/zram*/*_data_size" ]; then
            compress_ratio="0"
            break
        fi
        read file_content < $file
        what=$(basename $file)
        eval "$what=\$(($what + $file_content))"
        compress_ratio=$(echo "scale=2; $orig_data_size / $compr_data_size" | bc)
    done
    echo "compr_data_size: $((compr_data_size / 1024)) KiB"
    echo "orig_data_size:  $((orig_data_size  / 1024)) KiB"
    echo "compression-ratio: $compress_ratio"
}

function stop {
    for swapspace in $(swapon -s | awk '/zram/{print $1}'); do
        swapoff $swapspace
    done
    modprobe -r zram
}

function usage {
    echo "Usage:"
    echo "   zramswap start - start zram swap"
    echo "   zramswap stop - stop zram swap"
    echo "   zramswap status - prints some statistics"
}

if [ "$1" = "start" ]; then
    start
fi

if [ "$1" = "stop" ]; then
    stop
fi

if [ "$1" = "status" ]; then
    status
fi

if [ "$1" = "" ]; then
    usage
fi