buildstats-plot.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env 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. #
  22. # Produces script data to be consumed by gnuplot. There are two possible plots
  23. # depending if either the -S parameter is present or not:
  24. #
  25. # * without -S: Produces a histogram listing top N recipes/tasks versus
  26. # stats. The first stat defined in the -s parameter is the one taken
  27. # into account for ranking
  28. # * -S: Produces a histogram listing tasks versus stats. In this case,
  29. # the value of each stat is the sum for that particular stat in all recipes found.
  30. # Stats values are in descending order defined by the first stat defined on -s
  31. #
  32. # EXAMPLES
  33. #
  34. # 1. Top recipes' tasks taking into account utime
  35. #
  36. # $ buildstats-plot.sh -s utime | gnuplot -p
  37. #
  38. # 2. Tasks versus utime:stime
  39. #
  40. # $ buildstats-plot.sh -s utime:stime -S | gnuplot -p
  41. #
  42. # 3. Tasks versus IO write_bytes:IO read_bytes
  43. #
  44. # $ buildstats-plot.sh -s 'IO write_bytes:IO read_bytes' -S | gnuplot -p
  45. #
  46. # AUTHORS
  47. # Leonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com>
  48. #
  49. set -o nounset
  50. set -o errexit
  51. BS_DIR="tmp/buildstats"
  52. N=10
  53. STATS="utime"
  54. SUM=""
  55. OUTDATA_FILE="$PWD/buildstats-plot.out"
  56. function usage {
  57. CMD=$(basename $0)
  58. cat <<EOM
  59. Usage: $CMD [-b buildstats_dir] [-t do_task]
  60. -b buildstats The path where the folder resides
  61. (default: "$BS_DIR")
  62. -n N Top N recipes to display. Ignored if -S is present
  63. (default: "$N")
  64. -s stats The stats to be matched. If more that one stat, units
  65. should be the same because data is plot as histogram.
  66. (see buildstats.sh -h for all options) or any other defined
  67. (build)stat separated by colons, i.e. stime:utime
  68. (default: "$STATS")
  69. -S Sum values for a particular stat for found recipes
  70. -o Output data file.
  71. (default: "$OUTDATA_FILE")
  72. -h Display this help message
  73. EOM
  74. }
  75. # Parse and validate arguments
  76. while getopts "b:n:s:o:Sh" OPT; do
  77. case $OPT in
  78. b)
  79. BS_DIR="$OPTARG"
  80. ;;
  81. n)
  82. N="$OPTARG"
  83. ;;
  84. s)
  85. STATS="$OPTARG"
  86. ;;
  87. S)
  88. SUM="y"
  89. ;;
  90. o)
  91. OUTDATA_FILE="$OPTARG"
  92. ;;
  93. h)
  94. usage
  95. exit 0
  96. ;;
  97. *)
  98. usage
  99. exit 1
  100. ;;
  101. esac
  102. done
  103. # Get number of stats
  104. IFS=':'; statsarray=(${STATS}); unset IFS
  105. nstats=${#statsarray[@]}
  106. # Get script folder, use to run buildstats.sh
  107. CD=$(dirname $0)
  108. # Parse buildstats recipes to produce a single table
  109. OUTBUILDSTATS="$PWD/buildstats.log"
  110. $CD/buildstats.sh -H -s "$STATS" -H > $OUTBUILDSTATS
  111. # Get headers
  112. HEADERS=$(cat $OUTBUILDSTATS | sed -n -e '1s/ /-/g' -e '1s/:/ /gp')
  113. echo -e "set boxwidth 0.9 relative"
  114. echo -e "set style data histograms"
  115. echo -e "set style fill solid 1.0 border lt -1"
  116. echo -e "set xtics rotate by 45 right"
  117. # Get output data
  118. if [ -z "$SUM" ]; then
  119. cat $OUTBUILDSTATS | sed -e '1d' | sort -k3 -n -r | head -$N > $OUTDATA_FILE
  120. # include task at recipe column
  121. sed -i -e "1i\
  122. ${HEADERS}" $OUTDATA_FILE
  123. echo -e "set title \"Top task/recipes\""
  124. echo -e "plot for [COL=3:`expr 3 + ${nstats} - 1`] '${OUTDATA_FILE}' using COL:xtic(stringcolumn(1).' '.stringcolumn(2)) title columnheader(COL)"
  125. else
  126. # Construct datatamash sum argument (sum 3 sum 4 ...)
  127. declare -a sumargs
  128. j=0
  129. for i in `seq $nstats`; do
  130. sumargs[j]=sum; j=$(( $j + 1 ))
  131. sumargs[j]=`expr 3 + $i - 1`; j=$(( $j + 1 ))
  132. done
  133. # Do the processing with datamash
  134. cat $OUTBUILDSTATS | sed -e '1d' | datamash -t ' ' -g1 ${sumargs[*]} | sort -k2 -n -r > $OUTDATA_FILE
  135. # Include headers into resulted file, so we can include gnuplot xtics
  136. HEADERS=$(echo $HEADERS | sed -e 's/recipe//1')
  137. sed -i -e "1i\
  138. ${HEADERS}" $OUTDATA_FILE
  139. # Plot
  140. echo -e "set title \"Sum stats values per task for all recipes\""
  141. echo -e "plot for [COL=2:`expr 2 + ${nstats} - 1`] '${OUTDATA_FILE}' using COL:xtic(1) title columnheader(COL)"
  142. fi