summarylogtreecommitdiffstats
path: root/analyze-shutdown
blob: 0e304a1b76f25c36663cbe99558b8024f8a34df2 (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
#!/bin/bash
[ "$BASH" ] || { echo "Use bash >= 4"; exit 2; }
((BASH_VERSINFO[0] >= 4)) || { echo "Use bash >= 4"; exit 2; }

declare -A kill_time
declare -A exit_time
declare -a exit_seq
declare -A pid_comm
declare -A wait_time
declare -A killer
declare -A recv_sig

# ts, pid, signo
set_kill_time()
{
    if [[ "${kill_time[$2]}" ]]; then
        return
    fi

    case "$3" in
        0 | 1 | 2 | 3 | 6 | 9 | 15)
            kill_time[$2]=$1
        ;;
    esac
}

# ts, pid
set_exit_time()
{
    if [[ "${exit_time[$2]}" ]]; then
        return
    fi

    exit_time[$2]=$1
    exit_seq+=("$pid")
}

# pid, target_pid, signo
add_killer()
{
    killer[$1]+=" $3>$target_pid"
}

#ts, pid, signo
add_signal()
{
    recv_sig[$2]+=" $3($1)"
}

# https://kernel.org/doc/Documentation/trace/ftrace.txt
declare -i lineno=0
while read -r line; do
    let lineno++
    if [[ $line == '' || $line == '#'* ]]; then
        continue
    fi

    read -r -a cols <<< "$line"
    comm=${cols[0]%-*}
    pid=${cols[0]#$comm-}
    ts=${cols[3]%:}
    ts=${ts//./}
    func=${cols[4]%(*}
    func=${func%:}

    if [[ -z "$shut_time" ]]; then
        shut_time=$ts
        ts=0
    else
        zeros=000000
        ((ts-=shut_time))
        if (( 6 >= ${#ts} )); then
            ts=${zeros:0:7-${#ts}}$ts
        fi
        ts=${ts:0:${#ts}-6}.${ts:${#ts}-6}
    fi

    case "$func" in
        sched_process_exit)
            set_exit_time "$ts" "$pid"
            comm="${cols[5]#comm=}"
        ;;
        sys_kill | sys_tgkill)
            if [[ '->' == "${cols[5]}" ]]; then
                if [[ '0x0' != "${cols[6]}" ]]; then
                    target_pid=${killer[$pid]##*>}
                    if [[ "$target_pid" ]]; then
                        add_killer "$pid" "$target_pid" "$signo"
                        set_exit_time "$ts" "$target_pid"
                    fi
                fi
            else
                let target_pid=0x${cols[5]%,}
                if [[ 'sys_kill' == "$func" ]]; then
                    let signo=0x${cols[7]%)}
                else
                    let signo=0x${cols[9]%)}
                fi
                set_kill_time "$ts" "$target_pid" "$signo"
                add_killer "$pid" "$target_pid" "$signo"
            fi
        ;;
        signal_deliver)
            let signo=${cols[5]#sig=}
            add_signal "$ts" "$pid" "$signo"
        ;;
        sys_exit)
            set_kill_time "$ts" "$pid" -
        ;;
    esac

    if [[ -z "${pid_comm[$pid]}" || '<...>' == "${pid_comm[$pid]}" ]]; then
        pid_comm[$pid]=$comm
    fi
done

for pid in "${exit_seq[@]}"; do
    printf "%15s(%4s) exited at %10ss, got signals: %s\n" "${pid_comm[$pid]}" "$pid" "${exit_time[$pid]}" "${recv_sig[$pid]}"
done