blob: e9ec2d476a73785be1ce76616970fd8e5100b9c5 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/bash
2#
3# Copyright (c) 2011, Intel Corporation.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-or-later
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
7# DESCRIPTION
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008# Given 'buildstats' data (generate by bitbake when setting
9# USER_CLASSES ?= "buildstats" on local.conf), task names and a stats values
10# (these are the ones preset on the buildstats files), outputs
11# '<task> <recipe> <value_1> <value_2> ... <value_n>'. The units are the ones
12# defined at buildstats, which in turn takes data from /proc/[pid] files
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013#
14# Some useful pipelines
15#
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016# 1. Tasks with largest stime (Amount of time that this process has been scheduled
17# in kernel mode) values
18# $ buildstats.sh -b <buildstats> -s stime | sort -k3 -n -r | head
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019#
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020# 2. Min, max, sum utime (Amount of time that this process has been scheduled
21# in user mode) per task (in needs GNU datamash)
22# $ buildstats.sh -b <buildstats> -s utime | datamash -t' ' -g1 min 3 max 3 sum 3 | sort -k4 -n -r
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023#
24# AUTHORS
25# Leonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com>
26#
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027
28# Stats, by type
29TIME="utime:stime:cutime:cstime"
30IO="IO wchar:IO write_bytes:IO syscr:IO read_bytes:IO rchar:IO syscw:IO cancelled_write_bytes"
31RUSAGE="rusage ru_utime:rusage ru_stime:rusage ru_maxrss:rusage ru_minflt:rusage ru_majflt:\
32rusage ru_inblock:rusage ru_oublock:rusage ru_nvcsw:rusage ru_nivcsw"
33
34CHILD_RUSAGE="Child rusage ru_utime:Child rusage ru_stime:Child rusage ru_maxrss:Child rusage ru_minflt:\
35Child rusage ru_majflt:Child rusage ru_inblock:Child rusage ru_oublock:Child rusage ru_nvcsw:\
36Child rusage ru_nivcsw"
37
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038BS_DIR="tmp/buildstats"
39TASKS="compile:configure:fetch:install:patch:populate_lic:populate_sysroot:unpack"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050040STATS="$TIME"
41HEADER="" # No header by default
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
43function usage {
44CMD=$(basename $0)
45cat <<EOM
46Usage: $CMD [-b buildstats_dir] [-t do_task]
47 -b buildstats The path where the folder resides
48 (default: "$BS_DIR")
49 -t tasks The tasks to be computed
50 (default: "$TASKS")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 -s stats The stats to be matched. Options: TIME, IO, RUSAGE, CHILD_RUSAGE
52 or any other defined buildstat separated by colons, i.e. stime:utime
53 (default: "$STATS")
54 Default stat sets:
55 TIME=$TIME
56 IO=$IO
57 RUSAGE=$RUSAGE
58 CHILD_RUSAGE=$CHILD_RUSAGE
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 -h Display this help message
60EOM
61}
62
63# Parse and validate arguments
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064while getopts "b:t:s:Hh" OPT; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 case $OPT in
66 b)
67 BS_DIR="$OPTARG"
68 ;;
69 t)
70 TASKS="$OPTARG"
71 ;;
Brad Bishop6e60e8b2018-02-01 10:27:11 -050072 s)
73 STATS="$OPTARG"
74 ;;
75 H)
76 HEADER="y"
77 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 h)
79 usage
80 exit 0
81 ;;
82 *)
83 usage
84 exit 1
85 ;;
86 esac
87done
88
89# Ensure the buildstats folder exists
90if [ ! -d "$BS_DIR" ]; then
91 echo "ERROR: $BS_DIR does not exist"
92 usage
93 exit 1
94fi
95
Brad Bishop6e60e8b2018-02-01 10:27:11 -050096stats=""
97IFS=":"
98for stat in ${STATS}; do
99 case $stat in
100 TIME)
101 stats="${stats}:${TIME}"
102 ;;
103 IO)
104 stats="${stats}:${IO}"
105 ;;
106 RUSAGE)
107 stats="${stats}:${RUSAGE}"
108 ;;
109 CHILD_RUSAGE)
110 stats="${stats}:${CHILD_RUSAGE}"
111 ;;
112 *)
113 stats="${STATS}"
114 esac
115done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117# remove possible colon at the beginning
118stats="$(echo "$stats" | sed -e 's/^://1')"
119
120# Provide a header if required by the user
121[ -n "$HEADER" ] && { echo "task:recipe:$stats"; }
122
123for task in ${TASKS}; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 task="do_${task}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500125 for file in $(find ${BS_DIR} -type f -name ${task} | awk 'BEGIN{ ORS=""; OFS=":" } { print $0,"" }'); do
126 recipe="$(basename $(dirname $file))"
127 times=""
128 for stat in ${stats}; do
129 [ -z "$stat" ] && { echo "empty stats"; }
130 time=$(sed -n -e "s/^\($stat\): \\(.*\\)/\\2/p" $file)
131 # in case the stat is not present, set the value as NA
132 [ -z "$time" ] && { time="NA"; }
133 # Append it to times
134 if [ -z "$times" ]; then
135 times="${time}"
136 else
137 times="${times} ${time}"
138 fi
139 done
140 echo "${task} ${recipe} ${times}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 done
142done