diff options
Diffstat (limited to 'sliceweasel.lib.sh')
-rwxr-xr-x | sliceweasel.lib.sh | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/sliceweasel.lib.sh b/sliceweasel.lib.sh new file mode 100755 index 0000000..24221e2 --- /dev/null +++ b/sliceweasel.lib.sh | |||
@@ -0,0 +1,120 @@ | |||
1 | #!/bin/sh | ||
2 | get_age() | ||
3 | { | ||
4 | local mtime now | ||
5 | mtime=$(stat --format=%Y "$1") || return | ||
6 | now=$(date +%s) || return | ||
7 | echo $(( now - mtime )) | ||
8 | } | ||
9 | |||
10 | get_filesystem() | ||
11 | { | ||
12 | echo / | ||
13 | } | ||
14 | |||
15 | get_total_memory() | ||
16 | { | ||
17 | free -b | { | ||
18 | read _ | ||
19 | read _ total _ | ||
20 | echo $total | ||
21 | } | ||
22 | } | ||
23 | |||
24 | math() | ||
25 | { | ||
26 | printf '%s\n' "$*" | bc -lq | ||
27 | } | ||
28 | |||
29 | get_total_io() | ||
30 | { | ||
31 | results=$IO_ROOT_DIR/io.test.result | ||
32 | zeroes=$IO_ROOT_DIR/io.test | ||
33 | if [ -e "$results" ] | ||
34 | then | ||
35 | age=$(get_age "$results") || return | ||
36 | if [ "$age" -gt $((60 * 60 * 24 * 7)) ] | ||
37 | then | ||
38 | rm "$results" | ||
39 | fi | ||
40 | fi | ||
41 | |||
42 | if ! [ -e "$results" ] | ||
43 | then | ||
44 | MEGS=128 | ||
45 | then=$(date +%s.%N) || return | ||
46 | dd if=/dev/zero of="$zeroes" bs=${MEGS}M count=1 || return | ||
47 | now=$(date +%s.%N) || return | ||
48 | rm "$zeroes" | ||
49 | speed=$(math $now - $then / $((MEGS * 1024 * 1024))) || return | ||
50 | echo $speed > "$results" | ||
51 | fi | ||
52 | read total < "$results" || return | ||
53 | echo ${total%.*} | ||
54 | } | ||
55 | |||
56 | root_write() | ||
57 | { | ||
58 | $AS_ROOT sh -c 'cat > "$1"' sh "$1" | ||
59 | } | ||
60 | |||
61 | join_group() | ||
62 | { | ||
63 | GROUP_DIR=/sys/fs/cgroup/"$1" | ||
64 | [ -d "$GROUP_DIR" ] || $AS_ROOT mkdir "$GROUP_DIR" | ||
65 | echo $$ | root_write "$GROUP_DIR"/cgroup.procs | ||
66 | } | ||
67 | |||
68 | add_subtree_controller() | ||
69 | { | ||
70 | local group="$1" controller="$2" control_file | ||
71 | control_file=$(realpath -e "/sys/fs/cgroup/$group/cgroup.subtree_control") || return | ||
72 | case "$control_file" in | ||
73 | /sys/fs/cgroup/cgroup.subtree_control) ;; | ||
74 | *) add_subtree_controller "$group"/.. "$2" ;; | ||
75 | esac | ||
76 | |||
77 | [ "$1" ] && [ "$2" ] && [ -e "$control_file" ] || return | ||
78 | if ! grep -qe "\\b${controller}\\b" "$control_file" | ||
79 | then | ||
80 | echo +"$controller" | root_write "$control_file" | ||
81 | fi | ||
82 | } | ||
83 | |||
84 | set_max() | ||
85 | { | ||
86 | add_subtree_controller "$1"/.. "$2" | ||
87 | max_file=/sys/fs/cgroup/$group/$2.max | ||
88 | [ -e "$max_file" ] || return | ||
89 | printf '%s\n' "$3" | root_write "$max_file" | ||
90 | } | ||
91 | |||
92 | set_max_ratio() | ||
93 | { | ||
94 | local group="$1" controller="$2" ratio="$3" limit | ||
95 | total=$(get_total_$controller) | ||
96 | n=${ratio%%/*} | ||
97 | d=${ratio#$n/} | ||
98 | [ "$d" = "${d%/*}" ] || return | ||
99 | limit=$(( total * n / d )) | ||
100 | [ "$limit" ] || return | ||
101 | [ "$limit" -gt 0 ] || return | ||
102 | case "$controller" in | ||
103 | io) | ||
104 | fsroot=$(get_filesystem "$IO_ROOT_DIR") || return | ||
105 | majmin=$(lsblk -o 'MOUNTPOINT,MAJ:MIN' | sed -ne "s?^$fsroot *??p") || return | ||
106 | case "$majmin" in | ||
107 | *:*) ;; | ||
108 | *) return 1 ;; | ||
109 | esac | ||
110 | set_max "$group" "$controller" "$majmin wbps=$limit rbps=$limit" | ||
111 | ;; | ||
112 | *) set_max "$group" "$controller" "$limit" | ||
113 | esac | ||
114 | } | ||
115 | |||
116 | get_current_group() | ||
117 | { | ||
118 | read g < /proc/$$/cgroup | ||
119 | echo ${g#0::/} | ||
120 | } | ||