Some cleanup of bootstat scripts

Cleaned up boostat_summary output so that it no longer prints huge
negative numbers when the delta is zero, and so that the columns
overflow less often.

Added millisecond output to bootstat_get_last (with "time-ms"
argument) so that scripts using it can use builtin bash integer
arithmetic instead of having to invoke awk to manipulate times (makes
the client code simpler). (this is used by code that fixes referenced
bug).

BUG=chromium:271068
TEST=ran on device (Pixel)

Change-Id: Ieb98c6779dd06310c0c2b3bb2dd4c9b48af13bc5
Reviewed-on: https://gerrit.chromium.org/gerrit/65753
Tested-by: Greg Spencer <gspencer@chromium.org>
Reviewed-by: Richard Barnette <jrbarnette@chromium.org>
Commit-Queue: Greg Spencer <gspencer@chromium.org>
diff --git a/bootstat_get_last b/bootstat_get_last
index 2dc5592..b21d4cb 100755
--- a/bootstat_get_last
+++ b/bootstat_get_last
@@ -8,6 +8,12 @@
   echo $1
 }
 
+# Converts a floating point number of seconds to a properly rounded
+# number of integer milliseconds.
+seconds_to_ms() {
+  echo $1 | awk '{ print int($1 * 1000.0 + 0.5)}'
+}
+
 EVENT=$1
 shift
 if [ $# -eq 0 ]; then
@@ -20,6 +26,12 @@
       TAG=uptime
       FIELD=1
     ;;
+    # Emit time in milliseconds instead of fractional seconds.
+    time-ms)
+      TAG=uptime
+      FIELD=1
+      EMIT_MS=1
+    ;;
     read-sectors)
       TAG=disk
       FIELD=3
@@ -38,7 +50,11 @@
   # the requested event doesn't exist.
   EVENTFILE="/tmp/${TAG}-${EVENT}"
   if [ -f "$EVENTFILE" ]; then
-    getfield $FIELD $(tail -1 "$EVENTFILE")
+    result=$(getfield $FIELD $(tail -1 "$EVENTFILE"))
+    if [ -n "$EMIT_MS" ]; then
+      result=$(seconds_to_ms $result)
+    fi
+    echo $result
   fi
   shift
 done
diff --git a/bootstat_summary b/bootstat_summary
index 05c1672..1878690 100755
--- a/bootstat_summary
+++ b/bootstat_summary
@@ -7,7 +7,7 @@
 
 SUMMARIZE_TIME='
   BEGIN {
-    printf "%5s %4s %5s %4s  %s\n", "time", "%cpu", "dt", "%dt", "event"
+    printf "%8s %4s %8s %4s  %s\n", "time", "%cpu", "dt", "%dt", "event"
   }
 
   {
@@ -19,9 +19,13 @@
     uptime = $1 ; idle = $2
     cum_util = (200 * (uptime - idle) / uptime + 1) / 2
     delta = uptime - prev
-    util = (200 * (delta - idle + prev_idle) / delta + 1) / 2
+    if (delta != 0) {
+      util = (200 * (delta - idle + prev_idle) / delta + 1) / 2
+    } else {
+      util = 100
+    }
+    printf "%8d %3d%% %8d %3d%%  %s\n", uptime, cum_util, delta, util, $3
     prev = uptime ; prev_idle = idle
-    printf "%5d %3d%% %5d %3d%%  %s\n", uptime, cum_util, delta, util, $3
   }
 '