blob: 45c27d0b9727321f9be5633f066a17c417956994 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#!/usr/bin/env bash
2#
3# Copyright (c) 2011, Intel Corporation.
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-or-later
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006#
7# DESCRIPTION
8#
9# Produces script data to be consumed by gnuplot. There are two possible plots
10# depending if either the -S parameter is present or not:
11#
12# * without -S: Produces a histogram listing top N recipes/tasks versus
13# stats. The first stat defined in the -s parameter is the one taken
14# into account for ranking
15# * -S: Produces a histogram listing tasks versus stats. In this case,
16# the value of each stat is the sum for that particular stat in all recipes found.
17# Stats values are in descending order defined by the first stat defined on -s
18#
19# EXAMPLES
20#
21# 1. Top recipes' tasks taking into account utime
22#
23# $ buildstats-plot.sh -s utime | gnuplot -p
24#
25# 2. Tasks versus utime:stime
26#
27# $ buildstats-plot.sh -s utime:stime -S | gnuplot -p
28#
29# 3. Tasks versus IO write_bytes:IO read_bytes
30#
31# $ buildstats-plot.sh -s 'IO write_bytes:IO read_bytes' -S | gnuplot -p
32#
33# AUTHORS
34# Leonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com>
35#
36
37set -o nounset
38set -o errexit
39
40BS_DIR="tmp/buildstats"
41N=10
Andrew Geissler82c905d2020-04-13 13:39:40 -050042RECIPE=""
43TASKS="compile:configure:fetch:install:patch:populate_lic:populate_sysroot:unpack"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044STATS="utime"
Andrew Geissler82c905d2020-04-13 13:39:40 -050045ACCUMULATE=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046SUM=""
47OUTDATA_FILE="$PWD/buildstats-plot.out"
48
49function usage {
50 CMD=$(basename $0)
51 cat <<EOM
52Usage: $CMD [-b buildstats_dir] [-t do_task]
53 -b buildstats The path where the folder resides
54 (default: "$BS_DIR")
55 -n N Top N recipes to display. Ignored if -S is present
56 (default: "$N")
Andrew Geissler82c905d2020-04-13 13:39:40 -050057 -r recipe The recipe mask to be searched
58 -t tasks The tasks to be computed
59 (default: "$TASKS")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 -s stats The stats to be matched. If more that one stat, units
61 should be the same because data is plot as histogram.
62 (see buildstats.sh -h for all options) or any other defined
63 (build)stat separated by colons, i.e. stime:utime
64 (default: "$STATS")
Andrew Geissler82c905d2020-04-13 13:39:40 -050065 -a Accumulate all stats values for found recipes
Brad Bishop6e60e8b2018-02-01 10:27:11 -050066 -S Sum values for a particular stat for found recipes
67 -o Output data file.
68 (default: "$OUTDATA_FILE")
69 -h Display this help message
70EOM
71}
72
73# Parse and validate arguments
Andrew Geissler82c905d2020-04-13 13:39:40 -050074while getopts "b:n:r:t:s:o:aSh" OPT; do
75 case $OPT in
76 b)
77 BS_DIR="$OPTARG"
78 ;;
79 n)
80 N="$OPTARG"
81 ;;
82 r)
83 RECIPE="-r $OPTARG"
84 ;;
85 t)
86 TASKS="$OPTARG"
87 ;;
88 s)
89 STATS="$OPTARG"
90 ;;
91 a)
92 ACCUMULATE="-a"
93 ;;
94 S)
95 SUM="y"
96 ;;
97 o)
98 OUTDATA_FILE="$OPTARG"
99 ;;
100 h)
101 usage
102 exit 0
103 ;;
104 *)
105 usage
106 exit 1
107 ;;
108 esac
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109done
110
111# Get number of stats
112IFS=':'; statsarray=(${STATS}); unset IFS
113nstats=${#statsarray[@]}
114
115# Get script folder, use to run buildstats.sh
116CD=$(dirname $0)
117
118# Parse buildstats recipes to produce a single table
119OUTBUILDSTATS="$PWD/buildstats.log"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500120$CD/buildstats.sh -b "$BS_DIR" -s "$STATS" -t "$TASKS" $RECIPE $ACCUMULATE -H > $OUTBUILDSTATS
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121
122# Get headers
Andrew Geissler82c905d2020-04-13 13:39:40 -0500123HEADERS=$(cat $OUTBUILDSTATS | sed -n -e 's/\(.*\)/"\1"/' -e '1s/ /\\\\\\\\ /g' -e 's/_/\\\\\\\\_/g' -e '1s/:/" "/gp')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124
125echo -e "set boxwidth 0.9 relative"
126echo -e "set style data histograms"
127echo -e "set style fill solid 1.0 border lt -1"
128echo -e "set xtics rotate by 45 right"
129
130# Get output data
131if [ -z "$SUM" ]; then
Andrew Geissler82c905d2020-04-13 13:39:40 -0500132 cat $OUTBUILDSTATS | sed -e '1d' -e 's/_/\\\\_/g' | sort -k3 -n -r | head -$N > $OUTDATA_FILE
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133 # include task at recipe column
134 sed -i -e "1i\
135${HEADERS}" $OUTDATA_FILE
136 echo -e "set title \"Top task/recipes\""
137 echo -e "plot for [COL=3:`expr 3 + ${nstats} - 1`] '${OUTDATA_FILE}' using COL:xtic(stringcolumn(1).' '.stringcolumn(2)) title columnheader(COL)"
138else
139
140 # Construct datatamash sum argument (sum 3 sum 4 ...)
141 declare -a sumargs
142 j=0
143 for i in `seq $nstats`; do
Andrew Geissler82c905d2020-04-13 13:39:40 -0500144 sumargs[j]=sum; j=$(( $j + 1 ))
145 sumargs[j]=`expr 3 + $i - 1`; j=$(( $j + 1 ))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 done
147
148 # Do the processing with datamash
149 cat $OUTBUILDSTATS | sed -e '1d' | datamash -t ' ' -g1 ${sumargs[*]} | sort -k2 -n -r > $OUTDATA_FILE
150
151 # Include headers into resulted file, so we can include gnuplot xtics
152 HEADERS=$(echo $HEADERS | sed -e 's/recipe//1')
153 sed -i -e "1i\
154${HEADERS}" $OUTDATA_FILE
155
156 # Plot
157 echo -e "set title \"Sum stats values per task for all recipes\""
158 echo -e "plot for [COL=2:`expr 2 + ${nstats} - 1`] '${OUTDATA_FILE}' using COL:xtic(1) title columnheader(COL)"
159fi
160