test_java_symbol.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if [ -e "$PWD/tools/perf/libperf-jvmti.so" ]; then
  20. LIBJVMTI=$PWD/tools/perf/libperf-jvmti.so
  21. elif [ -e "$PWD/libperf-jvmti.so" ]; then
  22. LIBJVMTI=$PWD/libperf-jvmti.so
  23. elif [ -e "$PREFIX/lib64/libperf-jvmti.so" ]; then
  24. LIBJVMTI=$PREFIX/lib64/libperf-jvmti.so
  25. elif [ -e "$PREFIX/lib/libperf-jvmti.so" ]; then
  26. LIBJVMTI=$PREFIX/lib/libperf-jvmti.so
  27. elif [ -e "/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so" ]; then
  28. LIBJVMTI=/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so
  29. else
  30. echo "Fail to find libperf-jvmti.so"
  31. # JVMTI is a build option, skip the test if fail to find lib
  32. exit 2
  33. fi
  34. cat <<EOF | perf record -k 1 -o $PERF_DATA jshell -s -J-agentpath:$LIBJVMTI
  35. int fib(int x) {
  36. return x > 1 ? fib(x - 2) + fib(x - 1) : 1;
  37. }
  38. int q = 0;
  39. for (int i = 0; i < 10; i++)
  40. q += fib(i);
  41. System.out.println(q);
  42. EOF
  43. if [ $? -ne 0 ]; then
  44. echo "Fail to record for java program"
  45. exit 1
  46. fi
  47. if ! DEBUGINFOD_URLS='' perf inject -i $PERF_DATA -o $PERF_INJ_DATA -j; then
  48. echo "Fail to inject samples"
  49. exit 1
  50. fi
  51. # Below is an example of the instruction samples reporting:
  52. # 8.18% jshell jitted-50116-29.so [.] Interpreter
  53. # 0.75% Thread-1 jitted-83602-1670.so [.] jdk.internal.jimage.BasicImageReader.getString(int)
  54. perf report --stdio -i ${PERF_INJ_DATA} 2>&1 | \
  55. grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" > /dev/null 2>&1
  56. if [ $? -ne 0 ]; then
  57. echo "Fail to find java symbols"
  58. exit 1
  59. fi
  60. exit 0