summaryrefslogtreecommitdiff
path: root/LoopbackApp/app/src/main/res/raw/loopback_listener
blob: a29b0c9ed992557a88993a4d1c07e6de285b8af2 (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
#!/system/bin/sh

####
# Continuously checks for presence of signal file in 1 second intervals
# Reads either a filename prefix or termination signal from file when it exists
# Writes output of atrace and bugreport to files with supplied prefix
####

SYSTRACE_SUFFIX=".trace";
BUGREPORT_SUFFIX="_bugreport.txt.gz";
TERMINATE_SIGNAL="QUIT";
SIGNAL_FILE="/sdcard/Loopback/loopback_signal"
TRACE_CATEGORIES="sched audio $@"
BUFFER_KB="8000"

function exitListener {
    # Exit atrace, remove signal file, and exit

    echo "LOOPBACK LISTENER: stopping trace before exiting"
    rm $SIGNAL_FILE
    atrace --async_stop -z > /dev/null
    echo "LOOPBACK LISTENER: exiting"
    exit 1
}

# Begin an asynchronous systrace writing into a circular buffer of size BUFFER_KB
echo "LOOPBACK LISTENER: starting trace"
atrace --async_start -z -c -b $BUFFER_KB $TRACE_CATEGORIES
echo " "

# Remove signal file erroneously left behind from previous tests
if [ -e "$SIGNAL_FILE" ]; then rm $SIGNAL_FILE; fi

while true
do
    #echo "LOOPBACK LISTENER: checking for file $SIGNAL_FILE"
    if [ -e "$SIGNAL_FILE" ] && [ -s "$SIGNAL_FILE" ]
    then
        contents=$(cat $SIGNAL_FILE)

        # Ensure that if more than one listener is running only one will consume signal
        > $SIGNAL_FILE

        if [ "$contents" == $TERMINATE_SIGNAL ]
        then
            exitListener
        else
            for filename in $contents
            do
                case $filename in
                *$SYSTRACE_SUFFIX)
                    echo "LOOPBACK LISTENER: dumping systrace to file $filename"
                    atrace --async_dump -z -c -b $BUFFER_KB $TRACE_CATEGORIES > $filename
                    ;;

                *$BUGREPORT_SUFFIX)
                    echo "LOOPBACK LISTENER: dumping bugreport to file $filename"
                    bugreport | gzip > $filename
                    ;;

                esac
            done

            echo "LOOPBACK LISTENER: Finished capture"

            rm $SIGNAL_FILE
        fi
    fi
    sleep 1
done