1#!/usr/bin/env bash
2#
3# Copyright (c) 2011, Intel Corporation.
4#
5# SPDX-License-Identifier: GPL-2.0-or-later
6#
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
42RECIPE=""
43TASKS="compile:configure:fetch:install:patch:populate_lic:populate_sysroot:unpack"
44STATS="utime"
45ACCUMULATE=""
46SUM=""
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")
57  -r recipe     The recipe mask to be searched
58  -t tasks      The tasks to be computed
59                (default: "$TASKS")
60  -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")
65  -a            Accumulate all stats values for found recipes
66  -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
74while 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
109done
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"
120$CD/buildstats.sh -b "$BS_DIR" -s "$STATS" -t "$TASKS" $RECIPE $ACCUMULATE -H > $OUTBUILDSTATS
121
122# Get headers
123HEADERS=$(cat $OUTBUILDSTATS | sed -n -e 's/\(.*\)/"\1"/' -e '1s/ /\\\\\\\\ /g' -e 's/_/\\\\\\\\_/g' -e '1s/:/" "/gp')
124
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
132    cat $OUTBUILDSTATS | sed -e '1d' -e 's/_/\\\\_/g' | sort -k3 -n -r | head -$N > $OUTDATA_FILE
133    # 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
144        sumargs[j]=sum; j=$(( $j + 1 ))
145        sumargs[j]=`expr 3 + $i - 1`;  j=$(( $j + 1 ))
146    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
161