summaryrefslogtreecommitdiff
path: root/ioslay-firefox
blob: dcae98741c6df0f510d54f758c5bba3ebf090021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash

if [ "$UID" != 0 ]
then
    sudo -- "$0" "$@"
    exit
fi

while [ $# -gt 0 ]
do
    case "$1" in
        -q | --quiet )
            exec 2>/dev/null
            shift
            ;;
        -a | --all )
            IOSLAY_KILL_ALL=y
            shift
            ;;
        -- )
            shift
            break
            ;;
        -* )
            exit 1
            ;;
        * )
            break
            ;;
    esac
done

if [ $# -gt 0 ]
then
    echo "$0: Error: usage" >&2
    exit 1
fi

web_content_pids()
{
    top -w512 -b -o '%MEM' | {
        unset PIDS
        while read line
        do
            case "$line" in
                top*)
                    if [ "${PIDS+is_set}" ]
                    then
                        echo $PIDS
                    fi
                    while [ "$line" ]
                    do read line
                    done
                    PIDS=
                    ;;
                *)
                    set -- $line
                    [ "${12} ${13}" = "Web Content" ] || continue
                    PIDS="$PIDS $1"
                    ;;
                esac
        done
    }
}

sum()
{
    local total=0 n
    for n in $*
    do
        total=$((total + n))
    done
    echo $total
}

keep()
{
    local n=$1
    shift
    set -- $*
    while [ $# -ge "$n" ]
    do
        shift
    done
    printf '%s\n' "$*"
}

new_pids()
{
    # This is not correct because the correct code is not implemented yet.
    # But this is OK for testing the other parts.  Fixing this is only an
    # optimization.
    [ "$1" != "$2" ]
}

unset IOTOP_PID
relaunch_iotop()
{
    if [ "$IOTOP_PID" ]
    then
        kill "$IOTOP_PID"
    fi

    web_content_pids | (
        while read pids
        do
            set -- $pids
            if [ $# = 0 ]
            then
                kill_iotop
            elif new_pids "$pids" "$oldpids"
            then
                kill_iotop
                IOTOP_PID=$(launch_iotop $pids)
            fi
            sleep 1
            oldpids=$pids
        done
    )
}

kill_iotop()
{
    if [ "$IOTOP_PID" ]
    then
        (
            set -x
            kill "$IOTOP_PID"
        )
    fi
}

launch_iotop()
{
    set -- $(printf '%s\n' "$@" | tac | sed 's/^/--pid=/')
    (set -x; iotop -qq -b "$@") | iotop_reader &
    echo $!
}

iotop_reader()
{
    log=
    pct_log=
    while read line
    do
        case "$line" in
            "Total DISK"*)
                pct=$(sum $pct_log)
                [ "$pct" -ge 85 ] && over=y || over=
                pct_log=
                log=$(keep 9 $log)
                if [ "$over" ]
                then
                    log="$log 1"
                    over=
                    if [ "$(sum $log)" -ge 3 ]
                    then
                        (
                            set -x
                            fireslay -y
                        )
                    fi
                else
                    log="$log 0"
                fi
                case "$log" in
                    *1* )
                        (
                            set -x
                            : $log
                        )
                        ;;
                esac
                ;;
            "Current DISK"*) continue ;;
            *)
                set -- $line
                pct=${10%.*}
                pct_log="$pct_log $pct"
                ;;
        esac
    done
}

relaunch_iotop