1#!/bin/bash
2#
3# Copyright (c) 2011, Intel Corporation.
4# All rights reserved.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19#
20# DESCRIPTION
21# Given 'buildstats' data (generate by bitbake when setting
22# USER_CLASSES ?= "buildstats" on local.conf), task names and a stats values
23# (these are the ones preset on the buildstats files), outputs
24# '<task> <recipe> <value_1> <value_2> ... <value_n>'. The units are the ones
25# defined at buildstats, which in turn takes data from /proc/[pid] files
26#
27# Some useful pipelines
28#
29# 1. Tasks with largest stime (Amount of time that this process has been scheduled
30#    in kernel mode) values
31# $ buildstats.sh -b <buildstats> -s stime | sort -k3 -n -r | head
32#
33# 2. Min, max, sum utime (Amount  of  time  that  this process has been scheduled
34#    in user mode) per task (in needs GNU datamash)
35# $ buildstats.sh -b <buildstats> -s utime | datamash -t' ' -g1 min 3 max 3 sum 3 | sort -k4 -n -r
36#
37# AUTHORS
38# Leonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com>
39#
40
41# Stats, by type
42TIME="utime:stime:cutime:cstime"
43IO="IO wchar:IO write_bytes:IO syscr:IO read_bytes:IO rchar:IO syscw:IO cancelled_write_bytes"
44RUSAGE="rusage ru_utime:rusage ru_stime:rusage ru_maxrss:rusage ru_minflt:rusage ru_majflt:\
45rusage ru_inblock:rusage ru_oublock:rusage ru_nvcsw:rusage ru_nivcsw"
46
47CHILD_RUSAGE="Child rusage ru_utime:Child rusage ru_stime:Child rusage ru_maxrss:Child rusage ru_minflt:\
48Child rusage ru_majflt:Child rusage ru_inblock:Child rusage ru_oublock:Child rusage ru_nvcsw:\
49Child rusage ru_nivcsw"
50
51BS_DIR="tmp/buildstats"
52TASKS="compile:configure:fetch:install:patch:populate_lic:populate_sysroot:unpack"
53STATS="$TIME"
54HEADER="" # No header by default
55
56function usage {
57CMD=$(basename $0)
58cat <<EOM
59Usage: $CMD [-b buildstats_dir] [-t do_task]
60  -b buildstats The path where the folder resides
61                (default: "$BS_DIR")
62  -t tasks      The tasks to be computed
63                (default: "$TASKS")
64  -s stats      The stats to be matched. Options: TIME, IO, RUSAGE, CHILD_RUSAGE
65                or any other defined buildstat separated by colons, i.e. stime:utime
66                (default: "$STATS")
67                Default stat sets:
68                    TIME=$TIME
69                    IO=$IO
70                    RUSAGE=$RUSAGE
71                    CHILD_RUSAGE=$CHILD_RUSAGE
72  -h            Display this help message
73EOM
74}
75
76# Parse and validate arguments
77while getopts "b:t:s:Hh" OPT; do
78	case $OPT in
79	b)
80		BS_DIR="$OPTARG"
81		;;
82	t)
83		TASKS="$OPTARG"
84		;;
85	s)
86		STATS="$OPTARG"
87		;;
88	H)
89	        HEADER="y"
90	        ;;
91	h)
92		usage
93		exit 0
94		;;
95	*)
96		usage
97		exit 1
98		;;
99	esac
100done
101
102# Ensure the buildstats folder exists
103if [ ! -d "$BS_DIR" ]; then
104	echo "ERROR: $BS_DIR does not exist"
105	usage
106	exit 1
107fi
108
109stats=""
110IFS=":"
111for stat in ${STATS}; do
112	case $stat in
113	    TIME)
114		stats="${stats}:${TIME}"
115		;;
116	    IO)
117		stats="${stats}:${IO}"
118		;;
119	    RUSAGE)
120		stats="${stats}:${RUSAGE}"
121		;;
122	    CHILD_RUSAGE)
123		stats="${stats}:${CHILD_RUSAGE}"
124		;;
125	    *)
126		stats="${STATS}"
127	esac
128done
129
130# remove possible colon at the beginning
131stats="$(echo "$stats" | sed -e 's/^://1')"
132
133# Provide a header if required by the user
134[ -n "$HEADER" ] && { echo "task:recipe:$stats"; }
135
136for task in ${TASKS}; do
137    task="do_${task}"
138    for file in $(find ${BS_DIR} -type f -name ${task} | awk 'BEGIN{ ORS=""; OFS=":" } { print $0,"" }'); do
139        recipe="$(basename $(dirname $file))"
140	times=""
141	for stat in ${stats}; do
142	    [ -z "$stat" ] && { echo "empty stats"; }
143	    time=$(sed -n -e "s/^\($stat\): \\(.*\\)/\\2/p" $file)
144	    # in case the stat is not present, set the value as NA
145	    [ -z "$time" ] && { time="NA"; }
146	    # Append it to times
147	    if [ -z "$times" ]; then
148		times="${time}"
149	    else
150		times="${times} ${time}"
151	    fi
152	done
153        echo "${task} ${recipe} ${times}"
154    done
155done
156