summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2021-12-25 16:15:04 -0500
committerAndrew Cady <d@cryptonomic.net>2021-12-25 16:15:04 -0500
commit03d676f924adb59156a132d1098dce52a76154ee (patch)
treeace6a34680946ac3b86dc326b6b6992cb62d9209
parent7d83d858a73cd6530d09bddacaa7a1253d8ba807 (diff)
factor out functions into lib file; fixes
-rw-r--r--Makefile2
-rwxr-xr-xioslay-firefox2
-rwxr-xr-xioslay-mgr.sh2
-rwxr-xr-xsliceuser12
-rwxr-xr-xsliceweasel134
-rwxr-xr-xsliceweasel.lib.sh120
6 files changed, 138 insertions, 134 deletions
diff --git a/Makefile b/Makefile
index 77a6412..85cca10 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
1BINDIR = /usr/local/bin 1BINDIR = /usr/local/bin
2BINARIES = sliceweasel fireslay ioslay-firefox cgroup-show-each-new-process ioslay-mgr.sh 2BINARIES = sliceweasel sliceweasel.lib.sh sliceuser fireslay ioslay-firefox cgroup-show-each-new-process ioslay-mgr.sh
3SUDO != [ "$$(id -u)" = 0 ] || echo sudo 3SUDO != [ "$$(id -u)" = 0 ] || echo sudo
4 4
5.PHONY: install install-bin 5.PHONY: install install-bin
diff --git a/ioslay-firefox b/ioslay-firefox
index 96b3280..bebcd11 100755
--- a/ioslay-firefox
+++ b/ioslay-firefox
@@ -130,7 +130,7 @@ iotop_reader()
130 log=$(last 10 $log 0) 130 log=$(last 10 $log 0)
131 fi 131 fi
132 case "$log" in 132 case "$log" in
133 *1* | * ) 133 *1* )
134 ( 134 (
135 set -x 135 set -x
136 : $log 136 : $log
diff --git a/ioslay-mgr.sh b/ioslay-mgr.sh
index d7ce1cb..6765d83 100755
--- a/ioslay-mgr.sh
+++ b/ioslay-mgr.sh
@@ -74,7 +74,7 @@ do
74 fi 74 fi
75 done 75 done
76 76
77 echo "pids: ($*|$(echo $procs))" >&2 77 # echo "pids: ($*|$(echo $procs))" >&2
78 if [ "$lastargs" != "$*" ] 78 if [ "$lastargs" != "$*" ]
79 then 79 then
80 [ -z "$ioslay" ] || kill $ioslay $(pgrep -P $ioslay) 80 [ -z "$ioslay" ] || kill $ioslay $(pgrep -P $ioslay)
diff --git a/sliceuser b/sliceuser
new file mode 100755
index 0000000..9d80789
--- /dev/null
+++ b/sliceuser
@@ -0,0 +1,12 @@
1#!/bin/sh
2. sliceweasel.lib.sh
3
4set -e
5IO_ROOT_DIR=$HOME/.cache/mozilla/firefox/
6[ -d "$IO_ROOT_DIR" ]
7[ "$(id -un)" = 0 ] && AS_ROOT= || AS_ROOT='sudo --'
8
9group=$(get_current_group)
10set_max_ratio "$group" memory 3/4
11set_max_ratio "$group" io 8/10
12
diff --git a/sliceweasel b/sliceweasel
index 1b34d67..ebca97f 100755
--- a/sliceweasel
+++ b/sliceweasel
@@ -1,137 +1,9 @@
1#!/bin/sh 1#!/bin/sh
2 2. sliceweasel.lib.sh
3# HAHAHAH Bro it finally works. It fucking works. It finally fucking works.
4
5# It just needs to monitor IO with iotop and start killing firefox processes
6# from the bottom (least memory usage) up. This allows any one tab access to the
7# full memory available to Firefox! And integrate it into the your-fired.sh
8# script. And it's truly fixed. And integrate the firefox updater.
9
10IO_ROOT_DIR=$HOME/.cache/mozilla/firefox/
11
12[ -d "$IO_ROOT_DIR" ] || exit
13
14get_age()
15{
16 local mtime now
17 mtime=$(stat --format=%Y "$1") || return
18 now=$(date +%s) || return
19 echo $(( now - mtime ))
20}
21
22get_filesystem()
23{
24 echo /
25}
26
27get_total_memory()
28{
29 free -b | {
30 read _
31 read _ total _
32 echo $total
33 }
34}
35
36math()
37{
38 printf '%s\n' "$*" | bc -lq
39}
40
41get_total_io()
42{
43 results=$IO_ROOT_DIR/io.test.result
44 zeroes=$IO_ROOT_DIR/io.test
45 if [ -e "$results" ]
46 then
47 age=$(get_age "$results") || return
48 if [ "$age" -gt $((60 * 60 * 24 * 7)) ]
49 then
50 rm "$results"
51 fi
52 fi
53
54 if ! [ -e "$results" ]
55 then
56 MEGS=128
57 then=$(date +%s.%N) || return
58 dd if=/dev/zero of="$zeroes" bs=${MEGS}M count=1 || return
59 now=$(date +%s.%N) || return
60 rm "$zeroes"
61 speed=$(math $now - $then / $((MEGS * 1024 * 1024))) || return
62 echo $speed > "$results"
63 fi
64 read total < "$results" || return
65 echo ${total%.*}
66}
67
68root_write()
69{
70 $AS_ROOT sh -c 'cat > "$1"' sh "$1"
71}
72
73join_group()
74{
75 GROUP_DIR=/sys/fs/cgroup/"$1"
76 [ -d "$GROUP_DIR" ] || $AS_ROOT mkdir "$GROUP_DIR"
77 echo $$ | root_write "$GROUP_DIR"/cgroup.procs
78}
79
80add_subtree_controller()
81{
82 local group="$1" controller="$2" control_file
83 control_file=$(realpath -e "/sys/fs/cgroup/$group/cgroup.subtree_control") || return
84 case "$control_file" in
85 /sys/fs/cgroup/cgroup.subtree_control) ;;
86 *) add_subtree_controller "$group"/.. "$2" ;;
87 esac
88
89 [ "$1" ] && [ "$2" ] && [ -e "$control_file" ] || return
90 if ! grep -q "\\b${controller}\\b" "$control_file"
91 then
92 echo +"$controller" | root_write "$control_file"
93 fi
94}
95
96set_max()
97{
98 add_subtree_controller "$1"/.. "$2"
99 [ -e "$GROUP_DIR"/"$2".max ] || return
100 printf '%s\n' "$3" | root_write "$GROUP_DIR"/"$2".max
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 majmin=$(lsblk -o 'MOUNTPOINT,MAJ:MIN' | sed -ne "s?^$fsroot *??p") || return
117 case "$majmin" in
118 *:*) ;;
119 *) return 1 ;;
120 esac
121 set_max "$group" "$controller" "$majmin wbps=$limit rbps=$limit"
122 ;;
123 *) set_max "$group" "$controller" "$limit"
124 esac
125}
126
127get_current_group()
128{
129 read g < /proc/$$/cgroup
130 echo ${g#0::/}
131}
132 3
133set -e 4set -e
134 5IO_ROOT_DIR=$HOME/.cache/mozilla/firefox/
6[ -d "$IO_ROOT_DIR" ]
135[ "$(id -un)" = 0 ] && AS_ROOT= || AS_ROOT='sudo --' 7[ "$(id -un)" = 0 ] && AS_ROOT= || AS_ROOT='sudo --'
136 8
137group=user.slice/user-$(id -u).slice/firefox 9group=user.slice/user-$(id -u).slice/firefox
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
2get_age()
3{
4 local mtime now
5 mtime=$(stat --format=%Y "$1") || return
6 now=$(date +%s) || return
7 echo $(( now - mtime ))
8}
9
10get_filesystem()
11{
12 echo /
13}
14
15get_total_memory()
16{
17 free -b | {
18 read _
19 read _ total _
20 echo $total
21 }
22}
23
24math()
25{
26 printf '%s\n' "$*" | bc -lq
27}
28
29get_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
56root_write()
57{
58 $AS_ROOT sh -c 'cat > "$1"' sh "$1"
59}
60
61join_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
68add_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
84set_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
92set_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
116get_current_group()
117{
118 read g < /proc/$$/cgroup
119 echo ${g#0::/}
120}