diff options
Diffstat (limited to 'ioslay-firefox')
-rwxr-xr-x | ioslay-firefox | 147 |
1 files changed, 128 insertions, 19 deletions
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 | ||
3 | if [ "$1" = -q ] | 3 | if [ "$UID" != 0 ] |
4 | then | 4 | then |
5 | exec 2>/dev/null | 5 | sudo -- "$0" "$@" |
6 | shift | 6 | exit |
7 | fi | ||
8 | |||
9 | while [ $# -gt 0 ] | ||
10 | do | ||
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 | ||
31 | done | ||
32 | |||
33 | if [ $# -gt 0 ] | ||
34 | then | ||
35 | echo "$0: Error: usage" >&2 | ||
36 | exit 1 | ||
7 | fi | 37 | fi |
8 | 38 | ||
9 | web_content_pids() | 39 | web_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 | ||
20 | set -- $(web_content_pids | tac) | ||
21 | |||
22 | set -- $(printf "\055\055pid=%s " $*) | ||
23 | |||
24 | |||
25 | sum() | 66 | sum() |
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 | ||
47 | log= | 88 | new_pids() |
48 | pct_log= | 89 | { |
49 | sudo -- 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 | |||
96 | unset IOTOP_PID | ||
97 | relaunch_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 | |||
122 | kill_iotop() | ||
123 | { | ||
124 | if [ "$IOTOP_PID" ] | ||
125 | then | ||
126 | ( | ||
127 | set -x | ||
128 | kill "$IOTOP_PID" | ||
129 | ) | ||
130 | fi | ||
131 | } | ||
132 | |||
133 | launch_iotop() | ||
134 | { | ||
135 | set -- $(printf '%s\n' "$@" | tac | sed 's/^/--pid=/') | ||
136 | (set -x; iotop -qq -b "$@") | iotop_reader & | ||
137 | echo $! | ||
138 | } | ||
139 | |||
140 | iotop_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 | |||
185 | relaunch_iotop | ||