Make dumpstate vibrate immediately.

Previously, the vibration was not performed until after stacks
were gathered which takes a long time.  Moved the vibration
to happen earlier so we provide better user feedback for the
three-button salute when collecting a bug report.

Deleted some dead code for playing begin/end sounds.

Improved the timing measurement code to help track down why
bug reports are so slow.  (They take over a minute now which
can cause us to lose valuable diagnostic information.)

Bug: 17474152
Change-Id: Iac73f7993d7dc85196aad96f459b22fd4a710f94
diff --git a/cmds/dumpstate/dumpstate.c b/cmds/dumpstate/dumpstate.c
index ffc8714..f142095 100644
--- a/cmds/dumpstate/dumpstate.c
+++ b/cmds/dumpstate/dumpstate.c
@@ -119,7 +119,6 @@
     dump_file("BUDDYINFO", "/proc/buddyinfo");
     dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
 
-
     dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
     dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
     dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
@@ -134,6 +133,8 @@
     do_dmesg();
 
     run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
+    for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
+    for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
 
     if (screenshot_path[0]) {
         ALOGI("taking screenshot\n");
@@ -141,9 +142,6 @@
         ALOGI("wrote screenshot: %s\n", screenshot_path);
     }
 
-    for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
-    for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
-
     // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
     run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
     run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
@@ -382,14 +380,17 @@
     _exit(EXIT_FAILURE);
 }
 
+static void vibrate(FILE* vibrator, int ms) {
+    fprintf(vibrator, "%d\n", ms);
+    fflush(vibrator);
+}
+
 int main(int argc, char *argv[]) {
     struct sigaction sigact;
     int do_add_date = 0;
     int do_compress = 0;
     int do_vibrate = 1;
     char* use_outfile = 0;
-    char* begin_sound = 0;
-    char* end_sound = 0;
     int use_socket = 0;
     int do_fb = 0;
     int do_broadcast = 0;
@@ -402,14 +403,14 @@
         // correct program.
         return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
     }
+
     ALOGI("begin\n");
 
-
+    /* clear SIGPIPE handler */
     memset(&sigact, 0, sizeof(sigact));
     sigact.sa_handler = sigpipe_handler;
     sigaction(SIGPIPE, &sigact, NULL);
 
-
     /* set as high priority, and protect from OOM killer */
     setpriority(PRIO_PROCESS, 0, -20);
     FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
@@ -418,15 +419,11 @@
         fclose(oom_adj);
     }
 
-    /* very first thing, collect stack traces from Dalvik and native processes (needs root) */
-    dump_traces_path = dump_traces();
-
+    /* parse arguments */
     int c;
-    while ((c = getopt(argc, argv, "b:de:ho:svqzpB")) != -1) {
+    while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) {
         switch (c) {
-            case 'b': begin_sound = optarg;  break;
             case 'd': do_add_date = 1;       break;
-            case 'e': end_sound = optarg;    break;
             case 'o': use_outfile = optarg;  break;
             case 's': use_socket = 1;        break;
             case 'v': break;  // compatibility no-op
@@ -441,11 +438,14 @@
         }
     }
 
+    /* open the vibrator before dropping root */
     FILE *vibrator = 0;
     if (do_vibrate) {
-        /* open the vibrator before dropping root */
         vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
-        if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
+        if (vibrator) {
+            fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
+            vibrate(vibrator, 150);
+        }
     }
 
     /* read /proc/cmdline before dropping root */
@@ -455,14 +455,18 @@
         fclose(cmdline);
     }
 
+    /* collect stack traces from Dalvik and native processes (needs root) */
+    dump_traces_path = dump_traces();
+
+    /* Get the tombstone fds here while we are running as root. */
+    get_tombstone_fds(tombstone_data);
+
+    /* ensure we will keep capabilities when we drop root */
     if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
         ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
         return -1;
     }
 
-    /* Get the tombstone fds here while we are running as root. */
-    get_tombstone_fds(tombstone_data);
-
     /* switch to non-root user and group */
     gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
             AID_MOUNT, AID_INET, AID_NET_BW_STATS };
@@ -496,6 +500,7 @@
         return -1;
     }
 
+    /* redirect output if needed */
     char path[PATH_MAX], tmp_path[PATH_MAX];
     pid_t gzip_pid = -1;
 
@@ -520,22 +525,12 @@
         gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
     }
 
-    if (begin_sound) {
-        play_sound(begin_sound);
-    } else if (vibrator) {
-        fputs("150", vibrator);
-        fflush(vibrator);
-    }
-
     dumpstate();
 
-    if (end_sound) {
-        play_sound(end_sound);
-    } else if (vibrator) {
-        int i;
-        for (i = 0; i < 3; i++) {
-            fputs("75\n", vibrator);
-            fflush(vibrator);
+    /* done */
+    if (vibrator) {
+        for (int i = 0; i < 3; i++) {
+            vibrate(vibrator, 75);
             usleep((75 + 50) * 1000);
         }
         fclose(vibrator);
@@ -552,6 +547,7 @@
         fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
     }
 
+    /* tell activity manager we're done */
     if (do_broadcast && use_outfile && do_fb) {
         run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
                 "-a", "android.intent.action.BUGREPORT_FINISHED",