test_java_symbol.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. # Test java symbol
  3. # SPDX-License-Identifier: GPL-2.0
  4. # Leo Yan <leo.yan@linaro.org>, 2022
  5. # skip if there's no jshell
  6. if ! [ -x "$(command -v jshell)" ]; then
  7. echo "skip: no jshell, install JDK"
  8. exit 2
  9. fi
  10. PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
  11. PERF_INJ_DATA=$(mktemp /tmp/__perf_test.perf.data.inj.XXXXX)
  12. cleanup_files()
  13. {
  14. echo "Cleaning up files..."
  15. rm -f ${PERF_DATA}
  16. rm -f ${PERF_INJ_DATA}
  17. }
  18. trap cleanup_files exit term int
  19. PERF_DIR=$(dirname "$(which perf)")
  20. if [ -e "$PWD/tools/perf/libperf-jvmti.so" ]; then
  21. LIBJVMTI=$PWD/tools/perf/libperf-jvmti.so
  22. elif [ -e "$PWD/libperf-jvmti.so" ]; then
  23. LIBJVMTI=$PWD/libperf-jvmti.so
  24. elif [ -e "$PERF_DIR/libperf-jvmti.so" ]; then
  25. LIBJVMTI=$PERF_DIR/libperf-jvmti.so
  26. elif [ -e "$PREFIX/lib64/libperf-jvmti.so" ]; then
  27. LIBJVMTI=$PREFIX/lib64/libperf-jvmti.so
  28. elif [ -e "$PREFIX/lib/libperf-jvmti.so" ]; then
  29. LIBJVMTI=$PREFIX/lib/libperf-jvmti.so
  30. elif [ -e "/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so" ]; then
  31. LIBJVMTI=/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so
  32. else
  33. echo "Fail to find libperf-jvmti.so"
  34. # JVMTI is a build option, skip the test if fail to find lib
  35. exit 2
  36. fi
  37. cat <<EOF | perf record -k 1 -o $PERF_DATA jshell -s -J-agentpath:$LIBJVMTI
  38. int fib(int x) {
  39. return x > 1 ? fib(x - 2) + fib(x - 1) : 1;
  40. }
  41. int q = 0;
  42. for (int i = 0; i < 10; i++)
  43. q += fib(i);
  44. System.out.println(q);
  45. EOF
  46. if [ $? -ne 0 ]; then
  47. echo "Fail to record for java program"
  48. exit 1
  49. fi
  50. if ! DEBUGINFOD_URLS='' perf inject -i $PERF_DATA -o $PERF_INJ_DATA -j; then
  51. echo "Fail to inject samples"
  52. exit 1
  53. fi
  54. # Below is an example of the instruction samples reporting:
  55. # 8.18% jshell jitted-50116-29.so [.] Interpreter
  56. # 0.75% Thread-1 jitted-83602-1670.so [.] jdk.internal.jimage.BasicImageReader.getString(int)
  57. perf report --stdio -i ${PERF_INJ_DATA} 2>&1 | \
  58. grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" > /dev/null 2>&1
  59. if [ $? -ne 0 ]; then
  60. echo "Fail to find java symbols"
  61. exit 1
  62. fi
  63. exit 0