summaryrefslogtreecommitdiff
path: root/src/ioslay-firefox
blob: 41aa6a41ab7bbd06296b269339b205dc5fb638a9 (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
#!/bin/bash
sum()
{
    local total=0 n
    for n in $*
    do
        total=$((total + n))
    done
    echo $total
}

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

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=
                if [ "$over" ]
                then
                    log=$(last 10 $log 1)
                    over=
                    sum=$(sum $log)
                    if [ "$(sum $log)" -gt 4 ]
                    then
                        log= # this is year zero; all past history is erased.
                        (
                            set -x
                            kill $pids
                        )
                    fi
                else
                    log=$(last 10 $log 0)
                fi
                case "$log" in
                    *1* )
                        echo IO '!!' $log >&2
                        ;;
                    # * )
                    #     echo IO OK $log >&2
                    #     ;;
                esac
                ;;
            "Current DISK"*) continue ;;
            *)
                set -- $line
                pct=${10%.*}
                pct_log="$pct_log $pct"
                ;;
        esac
    done
}

sudo_iotop()
{
    if [ "$UID" = 0 ]
    then
        command iotop "$@"
    else
        sudo iotop "$@"
    fi
}

if [ $# -le 0 ]
then
    exit
fi

pids=$*

sudo_iotop -qq -b $(printf '\55\55pid=%s\n' "$@") | iotop_reader