slabinfo-gnuplot.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/bin/sh
  2. # Sergey Senozhatsky, 2015
  3. # sergey.senozhatsky.work@gmail.com
  4. #
  5. # This software is licensed under the terms of the GNU General Public
  6. # License version 2, as published by the Free Software Foundation, and
  7. # may be copied, distributed, and modified under those terms.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # This program is intended to plot a `slabinfo -X' stats, collected,
  14. # for example, using the following command:
  15. # while [ 1 ]; do slabinfo -X >> stats; sleep 1; done
  16. #
  17. # Use `slabinfo-gnuplot.sh stats' to pre-process collected records
  18. # and generate graphs (totals, slabs sorted by size, slabs sorted
  19. # by size).
  20. #
  21. # Graphs can be [individually] regenerate with different ranges and
  22. # size (-r %d,%d and -s %d,%d options).
  23. #
  24. # To visually compare N `totals' graphs, do
  25. # slabinfo-gnuplot.sh -t FILE1-totals FILE2-totals ... FILEN-totals
  26. #
  27. min_slab_name_size=11
  28. xmin=0
  29. xmax=0
  30. width=1500
  31. height=700
  32. mode=preprocess
  33. usage()
  34. {
  35. echo "Usage: [-s W,H] [-r MIN,MAX] [-t|-l] FILE1 [FILE2 ..]"
  36. echo "FILEs must contain 'slabinfo -X' samples"
  37. echo "-t - plot totals for FILE(s)"
  38. echo "-l - plot slabs stats for FILE(s)"
  39. echo "-s %d,%d - set image width and height"
  40. echo "-r %d,%d - use data samples from a given range"
  41. }
  42. check_file_exist()
  43. {
  44. if [ ! -f "$1" ]; then
  45. echo "File '$1' does not exist"
  46. exit 1
  47. fi
  48. }
  49. do_slabs_plotting()
  50. {
  51. local file=$1
  52. local out_file
  53. local range="every ::$xmin"
  54. local xtic=""
  55. local xtic_rotate="norotate"
  56. local lines=2000000
  57. local wc_lines
  58. check_file_exist "$file"
  59. out_file=`basename "$file"`
  60. if [ $xmax -ne 0 ]; then
  61. range="$range::$xmax"
  62. lines=$((xmax-xmin))
  63. fi
  64. wc_lines=`cat "$file" | wc -l`
  65. if [ $? -ne 0 ] || [ "$wc_lines" -eq 0 ] ; then
  66. wc_lines=$lines
  67. fi
  68. if [ "$wc_lines" -lt "$lines" ]; then
  69. lines=$wc_lines
  70. fi
  71. if [ $((width / lines)) -gt $min_slab_name_size ]; then
  72. xtic=":xtic(1)"
  73. xtic_rotate=90
  74. fi
  75. gnuplot -p << EOF
  76. #!/usr/bin/env gnuplot
  77. set terminal png enhanced size $width,$height large
  78. set output '$out_file.png'
  79. set autoscale xy
  80. set xlabel 'samples'
  81. set ylabel 'bytes'
  82. set style histogram columnstacked title textcolor lt -1
  83. set style fill solid 0.15
  84. set xtics rotate $xtic_rotate
  85. set key left above Left title reverse
  86. plot "$file" $range u 2$xtic title 'SIZE' with boxes,\
  87. '' $range u 3 title 'LOSS' with boxes
  88. EOF
  89. if [ $? -eq 0 ]; then
  90. echo "$out_file.png"
  91. fi
  92. }
  93. do_totals_plotting()
  94. {
  95. local gnuplot_cmd=""
  96. local range="every ::$xmin"
  97. local file=""
  98. if [ $xmax -ne 0 ]; then
  99. range="$range::$xmax"
  100. fi
  101. for i in "${t_files[@]}"; do
  102. check_file_exist "$i"
  103. file="$file"`basename "$i"`
  104. gnuplot_cmd="$gnuplot_cmd '$i' $range using 1 title\
  105. '$i Memory usage' with lines,"
  106. gnuplot_cmd="$gnuplot_cmd '' $range using 2 title \
  107. '$i Loss' with lines,"
  108. done
  109. gnuplot -p << EOF
  110. #!/usr/bin/env gnuplot
  111. set terminal png enhanced size $width,$height large
  112. set autoscale xy
  113. set output '$file.png'
  114. set xlabel 'samples'
  115. set ylabel 'bytes'
  116. set key left above Left title reverse
  117. plot $gnuplot_cmd
  118. EOF
  119. if [ $? -eq 0 ]; then
  120. echo "$file.png"
  121. fi
  122. }
  123. do_preprocess()
  124. {
  125. local out
  126. local lines
  127. local in=$1
  128. check_file_exist "$in"
  129. # use only 'TOP' slab (biggest memory usage or loss)
  130. let lines=3
  131. out=`basename "$in"`"-slabs-by-loss"
  132. `cat "$in" | grep -A "$lines" 'Slabs sorted by loss' |\
  133. egrep -iv '\-\-|Name|Slabs'\
  134. | awk '{print $1" "$4+$2*$3" "$4}' > "$out"`
  135. if [ $? -eq 0 ]; then
  136. do_slabs_plotting "$out"
  137. fi
  138. let lines=3
  139. out=`basename "$in"`"-slabs-by-size"
  140. `cat "$in" | grep -A "$lines" 'Slabs sorted by size' |\
  141. egrep -iv '\-\-|Name|Slabs'\
  142. | awk '{print $1" "$4" "$4-$2*$3}' > "$out"`
  143. if [ $? -eq 0 ]; then
  144. do_slabs_plotting "$out"
  145. fi
  146. out=`basename "$in"`"-totals"
  147. `cat "$in" | grep "Memory used" |\
  148. awk '{print $3" "$7}' > "$out"`
  149. if [ $? -eq 0 ]; then
  150. t_files[0]=$out
  151. do_totals_plotting
  152. fi
  153. }
  154. parse_opts()
  155. {
  156. local opt
  157. while getopts "tlr::s::h" opt; do
  158. case $opt in
  159. t)
  160. mode=totals
  161. ;;
  162. l)
  163. mode=slabs
  164. ;;
  165. s)
  166. array=(${OPTARG//,/ })
  167. width=${array[0]}
  168. height=${array[1]}
  169. ;;
  170. r)
  171. array=(${OPTARG//,/ })
  172. xmin=${array[0]}
  173. xmax=${array[1]}
  174. ;;
  175. h)
  176. usage
  177. exit 0
  178. ;;
  179. \?)
  180. echo "Invalid option: -$OPTARG" >&2
  181. exit 1
  182. ;;
  183. :)
  184. echo "-$OPTARG requires an argument." >&2
  185. exit 1
  186. ;;
  187. esac
  188. done
  189. return $OPTIND
  190. }
  191. parse_args()
  192. {
  193. local idx=0
  194. local p
  195. for p in "$@"; do
  196. case $mode in
  197. preprocess)
  198. files[$idx]=$p
  199. idx=$idx+1
  200. ;;
  201. totals)
  202. t_files[$idx]=$p
  203. idx=$idx+1
  204. ;;
  205. slabs)
  206. files[$idx]=$p
  207. idx=$idx+1
  208. ;;
  209. esac
  210. done
  211. }
  212. parse_opts "$@"
  213. argstart=$?
  214. parse_args "${@:$argstart}"
  215. if [ ${#files[@]} -eq 0 ] && [ ${#t_files[@]} -eq 0 ]; then
  216. usage
  217. exit 1
  218. fi
  219. case $mode in
  220. preprocess)
  221. for i in "${files[@]}"; do
  222. do_preprocess "$i"
  223. done
  224. ;;
  225. totals)
  226. do_totals_plotting
  227. ;;
  228. slabs)
  229. for i in "${files[@]}"; do
  230. do_slabs_plotting "$i"
  231. done
  232. ;;
  233. *)
  234. echo "Unknown mode $mode" >&2
  235. usage
  236. exit 1
  237. ;;
  238. esac