| #!/bin/sh | |
| # Copyright 2012 The ChromiumOS Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| # This isn't the exact copy that will be used in production, but it's better | |
| # than pointing shellcheck at /dev/null. | |
| # shellcheck source=../../scripts/lib/shflags/shflags | |
| . /usr/share/misc/shflags || exit 1 | |
| DEFINE_string directory /run/bootstat "Bootstat archive directory" d | |
| FLAGS "$@" || exit 1 | |
| eval set -- "${FLAGS_ARGV}" | |
| NCPU="$(grep -c '^processor' /proc/cpuinfo)" | |
| readonly NCPU | |
| # shellcheck disable=SC2016 # We want a literal awk script. | |
| SUMMARIZE_TIME=' | |
| BEGIN { | |
| printf "%8s %4s %8s %4s %s\n", "time", "%cpu", "dt", "%dt", "event" | |
| } | |
| { | |
| # input lines are like this: | |
| # $1 = time since boot | |
| # $2 = idle time since boot (normalized for NCPU) | |
| # $3 = event name | |
| # input lines are sorted by $1 | |
| uptime = $1 ; idle = $2 | |
| cum_util = (200 * (uptime - idle) / uptime + 1) / 2 | |
| delta = uptime - prev | |
| 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 | |
| } | |
| ' | |
| # If there are no arguments on the command line, summarize all the | |
| # events. If there are arguments, summarize only the named events. | |
| if [ $# -eq 0 ]; then | |
| set -- "${FLAGS_directory}/uptime-"* | |
| else | |
| max=$# | |
| i=0 | |
| while [ $((i += 1)) -le "${max}" ]; do | |
| set -- "$@" "${FLAGS_directory}/uptime-$1" | |
| shift | |
| done | |
| fi | |
| max=$# | |
| i=0 | |
| # Catch some basic errors so awk doesn't choke. | |
| while [ $((i += 1)) -le "${max}" ]; do | |
| if [ -r "$1" ]; then | |
| set -- "$@" "$1" | |
| else | |
| echo "Error: file $1 is not readable" 1>&2 | |
| fi | |
| shift | |
| done | |
| # Pipeline explained: | |
| # 1st awk program: print times as milliseconds and normalize idle time | |
| # by NCPU. | |
| # sort: sort the events by the uptime timestamp. | |
| # sed: remove '/run/bootstat/uptime-' from the event name. | |
| # 2nd awk program: produce the summarized output | |
| awk 'BEGIN{OFMT="%f"} | |
| {print 1000*$1, 1000*$2/'"${NCPU}"', FILENAME}' "$@" | | |
| sort -k 1n | sed 's=[^ ]*uptime-==' | awk "${SUMMARIZE_TIME}" |