memusage.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #! @BASH@
  2. # Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. # This file is part of the GNU C Library.
  4. # Contributed by Ulrich Drepper <drepper@gnu.org>, 1999.
  5. # The GNU C Library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. # The GNU C Library 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 GNU
  12. # Lesser General Public License for more details.
  13. # You should have received a copy of the GNU Lesser General Public
  14. # License along with the GNU C Library; if not, see
  15. # <http://www.gnu.org/licenses/>.
  16. memusageso='@SLIBDIR@/libmemusage.so'
  17. memusagestat='@BINDIR@/memusagestat'
  18. TEXTDOMAIN=libc
  19. # Print usage message.
  20. do_usage() {
  21. printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" memusage memusage
  22. exit 1
  23. }
  24. # Message for missing argument.
  25. do_missing_arg() {
  26. printf >&2 $"%s: option '%s' requires an argument\n" memusage "$1"
  27. do_usage
  28. }
  29. # Print help message
  30. do_help() {
  31. echo $"Usage: memusage [OPTION]... PROGRAM [PROGRAMOPTION]...
  32. Profile memory usage of PROGRAM.
  33. -n,--progname=NAME Name of the program file to profile
  34. -p,--png=FILE Generate PNG graphic and store it in FILE
  35. -d,--data=FILE Generate binary data file and store it in FILE
  36. -u,--unbuffered Don't buffer output
  37. -b,--buffer=SIZE Collect SIZE entries before writing them out
  38. --no-timer Don't collect additional information through timer
  39. -m,--mmap Also trace mmap & friends
  40. -?,--help Print this help and exit
  41. --usage Give a short usage message
  42. -V,--version Print version information and exit
  43. The following options only apply when generating graphical output:
  44. -t,--time-based Make graph linear in time
  45. -T,--total Also draw graph of total memory use
  46. --title=STRING Use STRING as title of the graph
  47. -x,--x-size=SIZE Make graphic SIZE pixels wide
  48. -y,--y-size=SIZE Make graphic SIZE pixels high
  49. Mandatory arguments to long options are also mandatory for any corresponding
  50. short options.
  51. "
  52. printf $"For bug reporting instructions, please see:\\n%s.\\n" \
  53. "@REPORT_BUGS_TO@"
  54. exit 0
  55. }
  56. do_version() {
  57. echo 'memusage @PKGVERSION@@VERSION@'
  58. printf $"Copyright (C) %s Free Software Foundation, Inc.
  59. This is free software; see the source for copying conditions. There is NO
  60. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  61. " "2019"
  62. printf $"Written by %s.
  63. " "Ulrich Drepper"
  64. exit 0
  65. }
  66. # These variables are local
  67. buffer=
  68. data=
  69. memusagestat_args=
  70. notimer=
  71. png=
  72. progname=
  73. tracemmap=
  74. # Process arguments. But stop as soon as the program name is found.
  75. while test $# -gt 0; do
  76. case "$1" in
  77. -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
  78. do_version
  79. ;;
  80. -\? | --h | --he | --hel | --help)
  81. do_help
  82. ;;
  83. --us | --usa | --usag | --usage)
  84. echo $"Syntax: memusage [--data=FILE] [--progname=NAME] [--png=FILE] [--unbuffered]
  85. [--buffer=SIZE] [--no-timer] [--time-based] [--total]
  86. [--title=STRING] [--x-size=SIZE] [--y-size=SIZE]
  87. PROGRAM [PROGRAMOPTION]..."
  88. exit 0
  89. ;;
  90. -n | --pr | --pro | --prog | --progn | --progna | --prognam | --progname)
  91. if test $# -eq 1; then
  92. do_missing_arg $1
  93. fi
  94. shift
  95. progname="$1"
  96. ;;
  97. --pr=* | --pro=* | --prog=* | --progn=* | --progna=* | --prognam=* | --progname=*)
  98. progname=${1##*=}
  99. ;;
  100. -p | --pn | --png)
  101. if test $# -eq 1; then
  102. do_missing_arg $1
  103. fi
  104. shift
  105. png="$1"
  106. ;;
  107. --pn=* | --png=*)
  108. png=${1##*=}
  109. ;;
  110. -d | --d | --da | --dat | --data)
  111. if test $# -eq 1; then
  112. do_missing_arg $1
  113. fi
  114. shift
  115. data="$1"
  116. ;;
  117. --d=* | --da=* | --dat=* | --data=*)
  118. data=${1##*=}
  119. ;;
  120. -u | --un | --unb | --unbu | --unbuf | --unbuff | --unbuffe | --unbuffer | --unbuffere | --unbuffered)
  121. buffer=1
  122. ;;
  123. -b | --b | --bu | --buf | --buff | --buffe | --buffer)
  124. if test $# -eq 1; then
  125. do_missing_arg $1
  126. fi
  127. shift
  128. buffer="$1"
  129. ;;
  130. --b=* | --bu=* | --buf=* | --buff=* | --buffe=* | --buffer=*)
  131. buffer=${1##*=}
  132. ;;
  133. --n | --no | --no- | --no-t | --no-ti | --no-tim | --no-time | --no-timer)
  134. notimer=yes
  135. ;;
  136. -m | --m | --mm | --mma | --mmap)
  137. tracemmap=yes
  138. ;;
  139. -t | --tim | --time | --time- | --time-b | --time-ba | --time-bas | --time-base | --time-based)
  140. memusagestat_args="$memusagestat_args -t"
  141. ;;
  142. -T | --to | --tot | --tota | --total)
  143. memusagestat_args="$memusagestat_args -T"
  144. ;;
  145. --tit | --titl | --title)
  146. if test $# -eq 1; then
  147. do_missing_arg $1
  148. fi
  149. shift
  150. memusagestat_args="$memusagestat_args -s $1"
  151. ;;
  152. --tit=* | --titl=* | --title=*)
  153. memusagestat_args="$memusagestat_args -s ${1##*=}"
  154. ;;
  155. -x | --x | --x- | --x-s | --x-si | --x-siz | --x-size)
  156. if test $# -eq 1; then
  157. do_missing_arg $1
  158. fi
  159. shift
  160. memusagestat_args="$memusagestat_args -x $1"
  161. ;;
  162. --x=* | --x-=* | --x-s=* | --x-si=* | --x-siz=* | --x-size=*)
  163. memusagestat_args="$memusagestat_args -x ${1##*=}"
  164. ;;
  165. -y | --y | --y- | --y-s | --y-si | --y-siz | --y-size)
  166. if test $# -eq 1; then
  167. do_missing_arg $1
  168. fi
  169. shift
  170. memusagestat_args="$memusagestat_args -y $1"
  171. ;;
  172. --y=* | --y-=* | --y-s=* | --y-si=* | --y-siz=* | --y-size=*)
  173. memusagestat_args="$memusagestat_args -y ${1##*=}"
  174. ;;
  175. --p | --p=* | --t | --t=* | --ti | --ti=* | --u)
  176. echo >&2 $"memusage: option \`${1##*=}' is ambiguous"
  177. do_usage
  178. ;;
  179. --)
  180. # Stop processing arguments.
  181. shift
  182. break
  183. ;;
  184. --*)
  185. echo >&2 $"memusage: unrecognized option \`$1'"
  186. do_usage
  187. ;;
  188. *)
  189. # Unknown option. This means the rest is the program name and parameters.
  190. break
  191. ;;
  192. esac
  193. shift
  194. done
  195. # See whether any arguments are left.
  196. if test $# -eq 0; then
  197. echo >&2 $"No program name given"
  198. do_usage
  199. fi
  200. # This will be in the environment.
  201. add_env="LD_PRELOAD=$memusageso"
  202. # Generate data file name.
  203. datafile=
  204. if test -n "$data"; then
  205. datafile="$data"
  206. elif test -n "$png"; then
  207. datafile=$(mktemp -t memusage.XXXXXX) || exit
  208. trap 'rm -f "$datafile"; exit 1' HUP INT QUIT TERM PIPE
  209. fi
  210. if test -n "$datafile"; then
  211. add_env="$add_env MEMUSAGE_OUTPUT=$datafile"
  212. fi
  213. # Set program name.
  214. if test -n "$progname"; then
  215. add_env="$add_env MEMUSAGE_PROG_NAME=$progname"
  216. fi
  217. # Set buffer size.
  218. if test -n "$buffer"; then
  219. add_env="$add_env MEMUSAGE_BUFFER_SIZE=$buffer"
  220. fi
  221. # Disable timers.
  222. if test -n "$notimer"; then
  223. add_env="$add_env MEMUSAGE_NO_TIMER=yes"
  224. fi
  225. # Trace mmap.
  226. if test -n "$tracemmap"; then
  227. add_env="$add_env MEMUSAGE_TRACE_MMAP=yes"
  228. fi
  229. # Execute the program itself.
  230. eval $add_env '"$@"'
  231. result=$?
  232. # Generate the PNG data file if wanted and there is something to generate
  233. # it from.
  234. if test -n "$png" -a -n "$datafile" -a -s "$datafile"; then
  235. # Append extension .png if it isn't already there.
  236. case $png in
  237. *.png) ;;
  238. *) png="$png.png" ;;
  239. esac
  240. $memusagestat $memusagestat_args "$datafile" "$png"
  241. fi
  242. if test -z "$data" -a -n "$datafile"; then
  243. rm -f "$datafile"
  244. fi
  245. exit $result
  246. # Local Variables:
  247. # mode:ksh
  248. # End: