summaryrefslogtreecommitdiffstats
path: root/scripts/contrib/bb-perf/buildstats.sh
blob: e45cfc146d26371ab42e88fadc8e8088898aacbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
#
# Copyright (c) 2011, Intel Corporation.
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# DESCRIPTION
# Given 'buildstats' data (generate by bitbake when setting
# USER_CLASSES ?= "buildstats" on local.conf), task names and a stats values
# (these are the ones preset on the buildstats files), outputs
# '<task> <recipe> <value_1> <value_2> ... <value_n>'. The units are the ones
# defined at buildstats, which in turn takes data from /proc/[pid] files
#
# Some useful pipelines
#
# 1. Tasks with largest stime (Amount of time that this process has been scheduled
#    in kernel mode) values
# $ buildstats.sh -b <buildstats> -s stime | sort -k3 -n -r | head
#
# 2. Min, max, sum utime (Amount  of  time  that  this process has been scheduled
#    in user mode) per task (in needs GNU datamash)
# $ buildstats.sh -b <buildstats> -s utime | datamash -t' ' -g1 min 3 max 3 sum 3 | sort -k4 -n -r
#
# AUTHORS
# Leonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com>
#

# Stats, by type
TIME="utime:stime:cutime:cstime"
IO="IO wchar:IO write_bytes:IO syscr:IO read_bytes:IO rchar:IO syscw:IO cancelled_write_bytes"
RUSAGE="rusage ru_utime:rusage ru_stime:rusage ru_maxrss:rusage ru_minflt:rusage ru_majflt:\
rusage ru_inblock:rusage ru_oublock:rusage ru_nvcsw:rusage ru_nivcsw"

CHILD_RUSAGE="Child rusage ru_utime:Child rusage ru_stime:Child rusage ru_maxrss:Child rusage ru_minflt:\
Child rusage ru_majflt:Child rusage ru_inblock:Child rusage ru_oublock:Child rusage ru_nvcsw:\
Child rusage ru_nivcsw"

BS_DIR="tmp/buildstats"
RECIPE=""
TASKS="compile:configure:fetch:install:patch:populate_lic:populate_sysroot:unpack"
STATS="$TIME"
ACCUMULATE=""
HEADER="" # No header by default

function usage {
CMD=$(basename $0)
cat <<EOM
Usage: $CMD [-b buildstats_dir] [-t do_task]
  -b buildstats The path where the folder resides
                (default: "$BS_DIR")
  -r recipe     The recipe to be computed
  -t tasks      The tasks to be computed
                (default: "$TASKS")
  -s stats      The stats to be matched. Options: TIME, IO, RUSAGE, CHILD_RUSAGE
                or any other defined buildstat separated by colons, i.e. stime:utime
                (default: "$STATS")
                Default stat sets:
                    TIME=$TIME
                    IO=$IO
                    RUSAGE=$RUSAGE
                    CHILD_RUSAGE=$CHILD_RUSAGE
  -a            Accumulate all stats values for found recipes
  -h            Display this help message
EOM
}

# Parse and validate arguments
while getopts "b:r:t:s:aHh" OPT; do
    case $OPT in
    b)
        BS_DIR="$OPTARG"
        ;;
    r)
        RECIPE="$OPTARG"
        ;;
    t)
        TASKS="$OPTARG"
        ;;
    s)
        STATS="$OPTARG"
        ;;
    a)
        ACCUMULATE="y"
        ;;
    H)
        HEADER="y"
        ;;
    h)
        usage
        exit 0
        ;;
    *)
        usage
        exit 1
        ;;
    esac
done

# Ensure the buildstats folder exists
if [ ! -d "$BS_DIR" ]; then
    echo "ERROR: $BS_DIR does not exist"
    usage
    exit 1
fi

stats=""
IFS=":"
for stat in ${STATS}; do
    case $stat in
        TIME)
            stats="${stats}:${TIME}"
            ;;
        IO)
            stats="${stats}:${IO}"
            ;;
        RUSAGE)
            stats="${stats}:${RUSAGE}"
            ;;
        CHILD_RUSAGE)
            stats="${stats}:${CHILD_RUSAGE}"
            ;;
        *)
            stats="${STATS}"
            ;;
    esac
done

# remove possible colon at the beginning
stats="$(echo "$stats" | sed -e 's/^://1')"

# Provide a header if required by the user
if [ -n "$HEADER" ] ; then
    if [ -n "$ACCUMULATE" ]; then
        echo "task:recipe:accumulated(${stats//:/;})"
    else
        echo "task:recipe:$stats"
    fi
fi

for task in ${TASKS}; do
    task="do_${task}"
    for file in $(find ${BS_DIR} -type f -path *${RECIPE}*/${task} | awk 'BEGIN{ ORS=""; OFS=":" } { print $0,"" }'); do
        recipe="$(basename $(dirname $file))"
        times=""
        for stat in ${stats}; do
            [ -z "$stat" ] && { echo "empty stats"; }
            time=$(sed -n -e "s/^\($stat\): \\(.*\\)/\\2/p" $file)
            # in case the stat is not present, set the value as NA
            [ -z "$time" ] && { time="NA"; }
            # Append it to times
            if [ -z "$times" ]; then
                times="${time}"
            else
                times="${times} ${time}"
            fi
        done
        if [ -n "$ACCUMULATE" ]; then
            IFS=' '; valuesarray=(${times}); IFS=':'
            times=0
            for value in "${valuesarray[@]}"; do
                [ "$value" == "NA" ] && { echo "ERROR: stat is not present."; usage; exit 1; }
                times=$(( $times + $value ))
            done
        fi
        echo "${task} ${recipe} ${times}"
    done
done