1#!/bin/bash
2
3# TO DO
4#   This should be re-written in python.
5
6# Complain about dereferencing unset variables
7set -u
8typeset -i STARTTIME ENDTIME DURATION START_SEC START_USEC DUR_SEC DUR_USEC
9
10if [ "$#" -ne 1 ]
11then
12    echo "Usage: mmc_trace_reduce <trace_file>" >&2
13    exit 1
14fi
15
16exec < "$1"
17
18SAVED_START_LINE=""
19
20while read LINE
21do
22    # Skip comment lines
23    if [ -z "${LINE###*}" ]
24    then
25        continue
26    fi
27
28    # Fix up lines with nuisance spaces
29    LINE=${LINE/AsyncTask /AsyncTask-}
30
31    set $LINE
32
33    if [ "${5##*mmc_blk_*_}" = "start:" ]
34    then
35        if [ ! -z "$SAVED_START_LINE" ]
36        then
37            echo "Ignoring consecutive start line" >&2
38            continue
39        fi
40        SAVED_START_LINE="$LINE"
41
42        # Found a start line.  Extract the interesting bits
43        TMP=${4%:}
44        START_SEC="10#${TMP%%.*}"
45        STARTTIME=START_SEC*1000000
46        START_USEC="10#${TMP##*.}"
47        STARTTIME=STARTTIME+START_USEC
48
49        STARTPARMS="$6"
50        STARTCMD=${STARTPARMS%%,addr=*}
51        STARTCMD=${STARTCMD##*cmd=}
52        STARTADDR=${STARTPARMS%%,size=*}
53        STARTADDR=${STARTADDR##*addr=}
54        STARTSIZE=${STARTPARMS##*size=}
55
56    elif [ "${5##*mmc_blk_*_}" = "end:" ]
57    then
58        # Found an end line. Extract the interesting bits,
59        # then make sure it matches with the saved start line,
60        # Finally, do the math and emit a single reduced line
61        TMP=${4%:}
62        ENDTIME="${TMP%%.*}"
63        ENDTIME=ENDTIME*1000000
64        ENDTIME=ENDTIME+10#${TMP##*.}
65
66        ENDPARMS="$6"
67        ENDCMD=${ENDPARMS%%,addr=*}
68        ENDCMD=${ENDCMD##*cmd=}
69        ENDADDR=${ENDPARMS%%,size=*}
70        ENDADDR=${ENDADDR##*addr=}
71        ENDSIZE=${ENDPARMS##*size=}
72
73        if [ "$ENDCMD" != "$STARTCMD" ]
74        then
75            echo "End cmd doesn't match start cmd, ignoring both" >&2
76            SAVED_START_LINE=""
77            continue
78        fi
79        if [ "$ENDADDR" != "$STARTADDR" ]
80        then
81            echo "End addr doesn't match start addr, ignoring both" >&2
82            SAVED_START_LINE=""
83            continue
84        fi
85        if [ "$ENDSIZE" != "$STARTSIZE" ]
86        then
87            echo "End size doesn't match start size, ignoring both" >&2
88            SAVED_START_LINE=""
89            continue
90        fi
91
92        # Turn the command number into a command the flash analysis tool
93        # understands.  The tool doesn't differentiate between the different
94        # forms erase, so just emit "discard" for all of them.  Also, ignore
95        # the secure_trim2 and sanitize commands as the tool doesn't know
96        # about them either.
97        if [ "$ENDCMD" -eq 18 ]
98        then
99            ENDCMD="read"
100        elif [ "$ENDCMD" -eq 25 ]
101        then
102            ENDCMD="write"
103        elif [ "$ENDCMD" -eq 32 ]
104        then
105            ENDCMD="flush"
106            continue
107        elif [ "$ENDCMD" -eq 0 ]
108        then
109            ENDCMD="discard"
110        elif [ "$ENDCMD" -eq 1 ]
111        then
112            ENDCMD="discard"
113        elif [ "$ENDCMD" -eq 3 ]
114        then
115            ENDCMD="discard"
116        elif [ "$ENDCMD" -eq 2147483648 ] # 0x80000000
117        then
118            ENDCMD="discard"
119        elif [ "$ENDCMD" -eq 2147483649 ] # 0x80000001
120        then
121            ENDCMD="discard"
122        elif [ "$ENDCMD" -eq 2147516416 ] # 0x80008000
123        then
124            # Ignore, as the analysis tool doesn't deal with this
125            # ENDCMD="secure_trim2"
126            SAVED_START_LINE=""
127            continue
128        elif [ "$ENDCMD" -eq 165 ]
129        then
130            # Ignore, as the analysis tool doesn't deal with this
131            # ENDCMD="sanitize"
132            SAVED_START_LINE=""
133            continue
134        else
135            echo "Unrecognized command $ENDCMD, ignoring" >&2
136            SAVED_START_LINE=""
137            continue
138        fi
139
140        DURATION=ENDTIME-STARTTIME
141        DUR_SEC=DURATION/1000000
142        DUR_USEC=DURATION%1000000
143
144        printf "$%s,%s,%s,%d.%06d,%d.%06d\n" "$ENDCMD" "$ENDADDR" "$ENDSIZE" "$START_SEC" "$START_USEC" "$DUR_SEC" "$DUR_USEC"
145
146        SAVED_START_LINE=""
147    fi
148
149    # Ignore unknown lines and continue
150
151done
152
153