Add new bootstat_last command

BUG=chromium-os:9701
TEST=None

Review URL: http://codereview.chromium.org/5265008

Change-Id: I60f506485a0b64f3d516611f3edac7098730a34b
diff --git a/README b/README
index 787e5f0..a14c992 100644
--- a/README
+++ b/README
@@ -6,8 +6,8 @@
 to generate timestamps and other performance statistics during
 system boot.
 
-==== Functional specification
-Usage:
+==== Command specifications
+'bootstat' command usage:
     bootstat <event-name>
 
 Summary:  The command gathers and records the contents of
@@ -15,6 +15,26 @@
 not the boot partition), and associates the data with the passed
 in <event-name>.
 
+----
+'bootstat_get_last' command usage:
+    bootstat_get_last <event-name> [ <stat> ... ]
+
+Summary:  Print on standard output the value of selected statistics
+recorded when a specified event occurred.  These are the available
+statistics:
+    time:  Total time since kernel startup at the time of the event.
+    read-sectors:  Total sectors read from any partition of the boot
+        device since kernel startup.
+    write-sectors:  Total sectors written to any partition of the
+        boot device since kernel startup.
+
+If multiple statistics requested, they are reported in order, one
+per line.  If no statistics are listed on the command line, the
+default is to report 'time'.
+
+If an event has occurred more than once since kernel startup, only
+the statistics from the last occurrence are reported.
+
 ==== API specification
 The C and C++ API is defined in "bootstat.h".  See that header for
 specification details.
diff --git a/bootstat_get_last b/bootstat_get_last
new file mode 100755
index 0000000..04b4781
--- /dev/null
+++ b/bootstat_get_last
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+getfield() {
+  shift $1
+  echo $1
+}
+
+EVENT=$1
+shift
+if [ $# -eq 0 ]; then
+  set -- time
+fi
+
+while [ $# -gt 0 ]; do
+  case "$1" in
+    time)
+      TAG=uptime
+      FIELD=1
+    ;;
+    read-sectors)
+      TAG=disk
+      FIELD=3
+    ;;
+    write-sectors)
+      TAG=disk
+      FIELD=7
+    ;;
+    *)
+      shift
+      continue
+    ;;
+  esac
+  getfield $FIELD $(tail -1 /tmp/${TAG}-${EVENT})
+  shift
+done