summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2021-10-22 09:38:29 -0400
committerAndrew Cady <d@cryptonomic.net>2021-10-22 09:38:29 -0400
commit4e93a2d8b757b554acbe5c791d3effe0abd43716 (patch)
tree151e4f5a41f930666677c706d74b9c6ebac5f84f
parent07e0a818e68b65e60fdae86d0f16b9aeb70dc082 (diff)
automatic restarting of "iotop" to pick up new firefox pids
-rwxr-xr-xfireslay89
-rwxr-xr-xioslay-firefox147
2 files changed, 191 insertions, 45 deletions
diff --git a/fireslay b/fireslay
index 0b26c86..1926e19 100755
--- a/fireslay
+++ b/fireslay
@@ -1,10 +1,67 @@
1#!/bin/sh 1#!/bin/sh
2 2
3if [ "$1" = -q ] 3die()
4then 4{
5 exec 2>/dev/null 5 printf '%s: Error: %s\n' "$0" "$*" >&2
6 shift 6 exit 1
7fi 7}
8
9parseopts()
10{
11 while [ $# -gt 0 ]
12 do
13 case "$1" in
14 -q | --quiet )
15 exec 2>/dev/null
16 ;;
17 -y | --kill-last-process )
18 FIRESLAY_KILL_LAST_PROCESS=y
19 ;;
20 --)
21 shift
22 break
23 ;;
24 -* )
25 die unknown option
26 ;;
27 *)
28 break
29 ;;
30 esac
31 shift
32 done
33 [ $# = 0 ] || die usage
34}
35
36main()
37{
38 parseopts "$@"
39
40 set -- $(web_content_pids)
41
42 (
43 case $# in
44 0 ) echo "$0: Warning: Not slaying: no Web Content process found." >&2
45 exit
46 ;;
47 1 ) if ! [ "$FIRESLAY_KILL_LAST_PROCESS" ]
48 then
49 echo "$0: Warning: Not slaying: only one 'Web Content' process. Specify -y to kill it." >&2
50 exit
51 fi
52 ;;
53 * ) shift
54 ;;
55 esac
56 set -x
57 ps $* >&2
58 kill $*
59 )
60
61 r=$?
62 ps $* >&2
63 exit $r
64}
8 65
9web_content_pids() 66web_content_pids()
10{ 67{
@@ -18,25 +75,5 @@ web_content_pids()
18 } 75 }
19} 76}
20 77
21set -- $(web_content_pids | tac) 78main "$@"
22 79
23case $# in
24 0)
25 echo "$0: Warning: Not slaying: no Web Content process found." >&2
26 exit
27 ;;
28 1)
29 echo "$0: Warning: Not slaying: only one Web Content process found." >&2
30 ps $* >&2
31 exit
32 ;;
33esac
34
35(
36 set -x
37 ps $1 >&2
38 kill $1
39)
40r=$?
41ps $* >&2
42exit $r
diff --git a/ioslay-firefox b/ioslay-firefox
index 36f9346..dcae987 100755
--- a/ioslay-firefox
+++ b/ioslay-firefox
@@ -1,27 +1,68 @@
1#!/bin/sh 1#!/bin/bash
2 2
3if [ "$1" = -q ] 3if [ "$UID" != 0 ]
4then 4then
5 exec 2>/dev/null 5 sudo -- "$0" "$@"
6 shift 6 exit
7fi
8
9while [ $# -gt 0 ]
10do
11 case "$1" in
12 -q | --quiet )
13 exec 2>/dev/null
14 shift
15 ;;
16 -a | --all )
17 IOSLAY_KILL_ALL=y
18 shift
19 ;;
20 -- )
21 shift
22 break
23 ;;
24 -* )
25 exit 1
26 ;;
27 * )
28 break
29 ;;
30 esac
31done
32
33if [ $# -gt 0 ]
34then
35 echo "$0: Error: usage" >&2
36 exit 1
7fi 37fi
8 38
9web_content_pids() 39web_content_pids()
10{ 40{
11 top -w512 -bn1 -o '%MEM' | sed -e '1,/^$/d' | { 41 top -w512 -b -o '%MEM' | {
42 unset PIDS
12 while read line 43 while read line
13 do set -- $line 44 do
14 [ "${12} ${13}" = "Web Content" ] || continue 45 case "$line" in
15 echo $1 46 top*)
47 if [ "${PIDS+is_set}" ]
48 then
49 echo $PIDS
50 fi
51 while [ "$line" ]
52 do read line
53 done
54 PIDS=
55 ;;
56 *)
57 set -- $line
58 [ "${12} ${13}" = "Web Content" ] || continue
59 PIDS="$PIDS $1"
60 ;;
61 esac
16 done 62 done
17 } 63 }
18} 64}
19 65
20set -- $(web_content_pids | tac)
21
22set -- $(printf "\055\055pid=%s " $*)
23
24
25sum() 66sum()
26{ 67{
27 local total=0 n 68 local total=0 n
@@ -44,14 +85,68 @@ keep()
44 printf '%s\n' "$*" 85 printf '%s\n' "$*"
45} 86}
46 87
47log= 88new_pids()
48pct_log= 89{
49sudo -- iotop -qq -b "$@" | 90 # This is not correct because the correct code is not implemented yet.
91 # But this is OK for testing the other parts. Fixing this is only an
92 # optimization.
93 [ "$1" != "$2" ]
94}
95
96unset IOTOP_PID
97relaunch_iotop()
98{
99 if [ "$IOTOP_PID" ]
100 then
101 kill "$IOTOP_PID"
102 fi
103
104 web_content_pids | (
105 while read pids
106 do
107 set -- $pids
108 if [ $# = 0 ]
109 then
110 kill_iotop
111 elif new_pids "$pids" "$oldpids"
112 then
113 kill_iotop
114 IOTOP_PID=$(launch_iotop $pids)
115 fi
116 sleep 1
117 oldpids=$pids
118 done
119 )
120}
121
122kill_iotop()
123{
124 if [ "$IOTOP_PID" ]
125 then
126 (
127 set -x
128 kill "$IOTOP_PID"
129 )
130 fi
131}
132
133launch_iotop()
134{
135 set -- $(printf '%s\n' "$@" | tac | sed 's/^/--pid=/')
136 (set -x; iotop -qq -b "$@") | iotop_reader &
137 echo $!
138}
139
140iotop_reader()
141{
142 log=
143 pct_log=
50 while read line 144 while read line
51 do 145 do
52 case "$line" in 146 case "$line" in
53 "Total DISK"*) 147 "Total DISK"*)
54 [ "$(sum $pct_log)" -ge 85 ] && over=y || over= 148 pct=$(sum $pct_log)
149 [ "$pct" -ge 85 ] && over=y || over=
55 pct_log= 150 pct_log=
56 log=$(keep 9 $log) 151 log=$(keep 9 $log)
57 if [ "$over" ] 152 if [ "$over" ]
@@ -60,12 +155,23 @@ sudo -- iotop -qq -b "$@" |
60 over= 155 over=
61 if [ "$(sum $log)" -ge 3 ] 156 if [ "$(sum $log)" -ge 3 ]
62 then 157 then
63 fireslay 158 (
159 set -x
160 fireslay -y
161 )
64 fi 162 fi
65 else 163 else
66 log="$log 0" 164 log="$log 0"
67 fi 165 fi
68 ;; 166 case "$log" in
167 *1* )
168 (
169 set -x
170 : $log
171 )
172 ;;
173 esac
174 ;;
69 "Current DISK"*) continue ;; 175 "Current DISK"*) continue ;;
70 *) 176 *)
71 set -- $line 177 set -- $line
@@ -74,3 +180,6 @@ sudo -- iotop -qq -b "$@" |
74 ;; 180 ;;
75 esac 181 esac
76 done 182 done
183}
184
185relaunch_iotop