diff options
author | Andrew Cady <d@cryptonomic.net> | 2021-10-31 11:58:04 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2021-10-31 11:58:04 -0400 |
commit | 6aae90784e59d104098b9b52fc9c4785e0826f71 (patch) | |
tree | a2fe18859af8cd632a15302689c76fd37cce49a1 | |
parent | eed3c0fb3497fc65d4fd46204f8d2bc1049b1d96 (diff) |
make ioslay work as service with ioslay-mgr
-rw-r--r-- | Makefile | 2 | ||||
-rwxr-xr-x | cgroup-show-each-new-process | 51 | ||||
-rwxr-xr-x | ioslay-firefox | 76 | ||||
-rwxr-xr-x | ioslay-mgr.sh | 51 |
4 files changed, 141 insertions, 39 deletions
@@ -1,5 +1,5 @@ | |||
1 | BINDIR = /usr/local/bin | 1 | BINDIR = /usr/local/bin |
2 | BINARIES = sliceweasel fireslay ioslay-firefox | 2 | BINARIES = sliceweasel fireslay ioslay-firefox cgroup-show-each-new-process ioslay-mgr.sh |
3 | SUDO != [ "$$(id -u)" = 0 ] || echo sudo | 3 | SUDO != [ "$$(id -u)" = 0 ] || echo sudo |
4 | 4 | ||
5 | .PHONY: install install-bin | 5 | .PHONY: install install-bin |
diff --git a/cgroup-show-each-new-process b/cgroup-show-each-new-process new file mode 100755 index 0000000..d1473f1 --- /dev/null +++ b/cgroup-show-each-new-process | |||
@@ -0,0 +1,51 @@ | |||
1 | #!/bin/bash | ||
2 | cgroup=${1:-firefox~$(id -u)} | ||
3 | |||
4 | show_new_pids() | ||
5 | { | ||
6 | if [ -e "$procs" ] | ||
7 | then | ||
8 | sort < "$procs" > pids | ||
9 | if [ -e old-pids ] | ||
10 | then | ||
11 | comm -13 old-pids pids | ||
12 | else | ||
13 | cat pids | ||
14 | fi | ||
15 | mv pids old-pids | ||
16 | fi | ||
17 | } | ||
18 | |||
19 | show_pid_comm() | ||
20 | { | ||
21 | local pid comm | ||
22 | pid=$1 | ||
23 | if 2>/dev/null read comm < /proc/$pid/comm | ||
24 | then | ||
25 | echo "$pid $comm" | ||
26 | else | ||
27 | echo $pid '(disappeared)' >&2 | ||
28 | fi | ||
29 | } | ||
30 | |||
31 | each_line() | ||
32 | { | ||
33 | local line | ||
34 | while read line | ||
35 | do "$@" "$line" | ||
36 | done | ||
37 | } | ||
38 | |||
39 | RUNTIME_DIR=$(mktemp -d) || exit | ||
40 | trap 'cd /; rm -rf "$RUNTIME_DIR"' EXIT | ||
41 | cd "$RUNTIME_DIR" | ||
42 | |||
43 | enable -f /usr/lib/bash/sleep sleep | ||
44 | procs=/sys/fs/cgroup/$cgroup/cgroup.procs | ||
45 | |||
46 | while true | ||
47 | do | ||
48 | show_new_pids | ||
49 | sleep 1 | ||
50 | done | each_line show_pid_comm | ||
51 | |||
diff --git a/ioslay-firefox b/ioslay-firefox index 21ea81b..96b3280 100755 --- a/ioslay-firefox +++ b/ioslay-firefox | |||
@@ -1,40 +1,4 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | if [ "$UID" != 0 ] | ||
3 | then | ||
4 | sudo -- "$0" "$@" | ||
5 | exit | ||
6 | fi | ||
7 | |||
8 | while [ $# -gt 0 ] | ||
9 | do | ||
10 | case "$1" in | ||
11 | -q | --quiet ) | ||
12 | exec 2>/dev/null | ||
13 | shift | ||
14 | ;; | ||
15 | -a | --all ) | ||
16 | IOSLAY_KILL_ALL=y | ||
17 | shift | ||
18 | ;; | ||
19 | -- ) | ||
20 | shift | ||
21 | break | ||
22 | ;; | ||
23 | -* ) | ||
24 | exit 1 | ||
25 | ;; | ||
26 | * ) | ||
27 | break | ||
28 | ;; | ||
29 | esac | ||
30 | done | ||
31 | |||
32 | if [ $# -gt 0 ] | ||
33 | then | ||
34 | echo "$0: Error: usage" >&2 | ||
35 | exit 1 | ||
36 | fi | ||
37 | |||
38 | web_content_pids() | 2 | web_content_pids() |
39 | { | 3 | { |
40 | top -w512 -b -o '%MEM' | { | 4 | top -w512 -b -o '%MEM' | { |
@@ -166,7 +130,7 @@ iotop_reader() | |||
166 | log=$(last 10 $log 0) | 130 | log=$(last 10 $log 0) |
167 | fi | 131 | fi |
168 | case "$log" in | 132 | case "$log" in |
169 | *1* ) | 133 | *1* | * ) |
170 | ( | 134 | ( |
171 | set -x | 135 | set -x |
172 | : $log | 136 | : $log |
@@ -184,4 +148,40 @@ iotop_reader() | |||
184 | done | 148 | done |
185 | } | 149 | } |
186 | 150 | ||
187 | relaunch_iotop | 151 | if [ "$UID" != 0 ] |
152 | then | ||
153 | sudo -- "$0" "$@" | ||
154 | exit | ||
155 | fi | ||
156 | |||
157 | while [ $# -gt 0 ] | ||
158 | do | ||
159 | case "$1" in | ||
160 | -q | --quiet ) | ||
161 | exec 2>/dev/null | ||
162 | shift | ||
163 | ;; | ||
164 | -a | --all ) | ||
165 | IOSLAY_KILL_ALL=y | ||
166 | shift | ||
167 | ;; | ||
168 | -- ) | ||
169 | shift | ||
170 | break | ||
171 | ;; | ||
172 | -* ) | ||
173 | exit 1 | ||
174 | ;; | ||
175 | * ) | ||
176 | break | ||
177 | ;; | ||
178 | esac | ||
179 | done | ||
180 | |||
181 | # if [ $# -gt 0 ] | ||
182 | # then | ||
183 | # echo "$0: Error: usage" >&2 | ||
184 | # exit 1 | ||
185 | # fi | ||
186 | |||
187 | iotop -qq -b $(printf '\55\55pid=%s\n' "$@") | iotop_reader | ||
diff --git a/ioslay-mgr.sh b/ioslay-mgr.sh new file mode 100755 index 0000000..d4f545c --- /dev/null +++ b/ioslay-mgr.sh | |||
@@ -0,0 +1,51 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | ioslay= | ||
4 | each_new_process() | ||
5 | { | ||
6 | while read pid _ | ||
7 | do | ||
8 | set -- "$@" $pid | ||
9 | |||
10 | # filter non-existent processes | ||
11 | for _ in "$@" | ||
12 | do | ||
13 | if [ -e /proc/"$1" ] | ||
14 | then | ||
15 | set -- "$@" "$1" | ||
16 | fi | ||
17 | shift | ||
18 | done | ||
19 | |||
20 | if [ "$ioslay" ] | ||
21 | then | ||
22 | kill $(pgrep -P $ioslay) | ||
23 | wait $ioslay | ||
24 | fi | ||
25 | |||
26 | if [ $# -gt 0 ] | ||
27 | then | ||
28 | ioslay-firefox "$@" & | ||
29 | ioslay=$! | ||
30 | fi | ||
31 | done | ||
32 | } | ||
33 | |||
34 | get_uid() | ||
35 | { | ||
36 | ( | ||
37 | if [ "$SUDO_USER" ] | ||
38 | then | ||
39 | IFS=: | ||
40 | set -- $(getent passwd "${SUDO_USER}") || return | ||
41 | echo $3 | ||
42 | else | ||
43 | id -un | ||
44 | fi | ||
45 | ) | ||
46 | } | ||
47 | |||
48 | # group=firefox~$(get_uid) | ||
49 | group=firefox~1000 | ||
50 | |||
51 | cgroup-show-each-new-process "$group" | grep --line-buffered 'Web Content' | each_new_process | ||