ANRdaemon: add README and a bash script to easily get trace.

Bug: 28989601
Change-Id: I5c9b819a6b05003e68d26bfacccf4c539ddab7ae
diff --git a/ANRdaemon/ b/ANRdaemon/
new file mode 100755
index 0000000..be4062c
--- /dev/null
+++ b/ANRdaemon/
@@ -0,0 +1,39 @@
+if [ $# -eq 1 ]; then
+    DEVICE=$(echo "-s $1")
+    DEVICE=""
+PID=$(adb $DEVICE shell "ps | grep anrd")
+if [ $? -ne 0 ]; then
+    echo "FAILED. ADB failed or Daemon is not running."
+    exit 1
+PID=$(echo "$PID" | awk '{ print $2 }')
+adb $DEVICE shell "kill -s SIGUSR1 $PID"
+TRACE_FILE=$(adb $DEVICE shell "ls $TRACE_DIR | tail -n1" | tr -d '\r')
+# Wiat the trace file generation to complete
+adb $DEVICE shell "lsof $PID" | grep $TRACE_FILE > /dev/null
+while [ $? -eq 0 ];
+    sleep 1
+    adb $DEVICE shell "lsof $PID" | grep "$TRACE_FILE" > /dev/null
+if [ -z "$TRACE_FILE" ]; then
+    echo "FAILED. Trace file not created"
+echo SUCCEED!
+echo Trace stored at ${CURRENT_DIR}/${TRACE_FILE}
diff --git a/ANRdaemon/README b/ANRdaemon/README
new file mode 100644
index 0000000..57ed594
--- /dev/null
+++ b/ANRdaemon/README
@@ -0,0 +1,30 @@
+ANRdaemon is a daemon to help analyze ANR due to CPU starvation by logging system
+activity when CPU usage is very high. The daemon uses debugfs underlying for
+logging. Trace are configured ahead by setting different modules in /d/tracing.
+Depending on the CPU usage level, the trace is turn on/off by writting to the
+global control /d/trace/trace_on. The raw trace file is stored at
+The daemon will be started at boot time and will be running with the following
+$ ANRdaemon -t 9990 sched gfx am
+This means tracing will be enabled above 99.90% CPU utilization and will trace
+sched, gfx and am modules (See -h for more info).
+Use [device serial] to dump and fetch the compressed trace file.
+The compressed trace file can be parsed using systrace:
+$ --from-file=<path to compressed trace file>
+Known issue: in the systrace output, anrdaemon will show up when the trace is
+not running. This is because the daemon process turns off tracing when CPU usage
+drops, the last entry it leaves in the raw trace file is the scheduler switched
+from some other process to the daemon. Then sometime later (say 20 secs later),
+when the CPU usage becomes high and the daemon process turn on tracing again,
+the first entry in /d/tracing/trace logged by sched is switching away from the
+daemon process to some other process. Due to this artifact, when the raw trace
+file is parsed by, the daemon process is shown as running for the
+whole 20secs (because from systrace's view, the two 20 sec apart sched trace
+entries regarding the daemon process indicates the daemon process ran continuously
+for all 20sec). However, this will not affect the actual captured trace during
+high CPU usage case.