summaryrefslogtreecommitdiff
path: root/sliceweasel.lib.sh
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-11-29 12:00:08 -0500
committerAndrew Cady <d@cryptonomic.net>2022-11-29 12:00:08 -0500
commit61564e4d18058031d96e207862f72f0628934ea3 (patch)
tree1a43ee54623384f0b09387c68062905aeefe36f5 /sliceweasel.lib.sh
parent42a4367a83645b2bca1f0a4d48917fb9ec3bbf5b (diff)
move files to src; add README.txt
Diffstat (limited to 'sliceweasel.lib.sh')
-rwxr-xr-xsliceweasel.lib.sh154
1 files changed, 0 insertions, 154 deletions
diff --git a/sliceweasel.lib.sh b/sliceweasel.lib.sh
deleted file mode 100755
index 549e1e8..0000000
--- a/sliceweasel.lib.sh
+++ /dev/null
@@ -1,154 +0,0 @@
1#!/bin/sh
2die()
3{
4 printf '%s: Error: %s\n' "$0" "$*" >&2
5 exit 1
6}
7
8get_age()
9{
10 local mtime now
11 mtime=$(stat --format=%Y "$1") || return
12 now=$(date +%s) || return
13 echo $(( now - mtime ))
14}
15
16get_filesystem()
17{
18 echo /
19}
20
21get_total_memory()
22{
23 free -b | {
24 read _
25 read _ total _
26 echo $total
27 }
28}
29
30math()
31{
32 printf '%s\n' "$*" | bc -lq
33}
34
35get_total_io()
36{
37 results=$IO_ROOT_DIR/io.test.result
38 zeroes=$IO_ROOT_DIR/io.test
39 if [ -e "$results" ]
40 then
41 age=$(get_age "$results") || return
42 if [ "$age" -gt $((60 * 60 * 24 * 7)) ]
43 then
44 rm "$results"
45 fi
46 fi
47
48 if ! [ -e "$results" ]
49 then
50 MEGS=128
51 then=$(date +%s.%N) || return
52 dd if=/dev/zero of="$zeroes" bs=${MEGS}M count=1 || return
53 now=$(date +%s.%N) || return
54 rm "$zeroes"
55 speed=$(math $now - $then / $((MEGS * 1024 * 1024))) || return
56 echo $speed > "$results"
57 fi
58 read total < "$results" || return
59 echo ${total%.*}
60}
61
62root_write()
63{
64 $AS_ROOT sh -c 'cat > "$1"' sh "$1" || true
65}
66
67join_group()
68{
69 GROUP_DIR="$1"
70 [ -d "$GROUP_DIR" ] || $AS_ROOT mkdir "$GROUP_DIR"
71 echo $$ | root_write "$GROUP_DIR"/cgroup.procs
72}
73
74add_subtree_controller()
75{
76 local group="$1" controller="$2" control_file
77 control_file="$group/cgroup.subtree_control"
78
79 [ "$group" ] && [ "$controller" ] && [ -d "$group" ] && [ -e "$control_file" ] || return
80
81 case "$group" in
82 /sys/fs/cgroup) ;;
83 *) add_subtree_controller "$(realpath "$group/..")" "$2" ;;
84 esac
85
86 if ! grep -qe "\\b${controller}\\b" "$control_file"
87 then
88 echo +"$controller" | root_write "$control_file"
89 fi
90}
91
92set_max()
93{
94 max_file=$group/$2.max
95 if [ ! -e "$max_file" ]
96 then
97 add_subtree_controller "$1"/.. "$2"
98 [ -e "$max_file" ] || return
99 fi
100 printf '%s\n' "$3" | root_write "$max_file"
101}
102
103set_max_ratio()
104{
105 local group="$1" controller="$2" ratio="$3" limit
106 total=$(get_total_$controller)
107 n=${ratio%%/*}
108 d=${ratio#$n/}
109 [ "$d" = "${d%/*}" ] || return
110 limit=$(( total * n / d ))
111 [ "$limit" ] || return
112 [ "$limit" -gt 0 ] || return
113 case "$controller" in
114 io)
115 fsroot=$(get_filesystem "$IO_ROOT_DIR") || return
116 # TODO: Do not use lsblk, because it fails to report the correct
117 # MOUNTPOINT when a bind mount is present.
118 majmin=$(lsblk -o 'MOUNTPOINT,MAJ:MIN' | sed -ne "s?^$fsroot *??p") || return
119 case "$majmin" in
120 *:*) ;;
121 *) echo "Error: majmin=$majmin" >&2; return 1 ;;
122 esac
123 set_max "$group" "$controller" "$majmin wbps=$limit rbps=$limit"
124 ;;
125 *) set_max "$group" "$controller" "$limit"
126 esac
127}
128
129get_uid()
130{
131 (
132 if [ "$SUDO_USER" ]
133 then
134 IFS=:
135 set -- $(getent passwd "${SUDO_USER}") || return
136 echo $3
137 else
138 id -u
139 fi
140 )
141}
142
143get_firefox_cgroup_procs()
144{
145 uid=$(get_uid)
146 [ "$uid" ] || return
147 echo /sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service/app.slice/firefox.service/cgroup.procs
148}
149
150get_current_group()
151{
152 read g < /proc/$$/cgroup
153 echo /sys/fs/cgroup/${g#0::/}
154}