162306a36Sopenharmony_ci#!/bin/bash
262306a36Sopenharmony_ci# Test java symbol
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
562306a36Sopenharmony_ci# Leo Yan <leo.yan@linaro.org>, 2022
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci# skip if there's no jshell
862306a36Sopenharmony_ciif ! [ -x "$(command -v jshell)" ]; then
962306a36Sopenharmony_ci	echo "skip: no jshell, install JDK"
1062306a36Sopenharmony_ci	exit 2
1162306a36Sopenharmony_cifi
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciPERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
1462306a36Sopenharmony_ciPERF_INJ_DATA=$(mktemp /tmp/__perf_test.perf.data.inj.XXXXX)
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cicleanup_files()
1762306a36Sopenharmony_ci{
1862306a36Sopenharmony_ci	echo "Cleaning up files..."
1962306a36Sopenharmony_ci	rm -f ${PERF_DATA}
2062306a36Sopenharmony_ci	rm -f ${PERF_INJ_DATA}
2162306a36Sopenharmony_ci}
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_citrap cleanup_files exit term int
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciif [ -e "$PWD/tools/perf/libperf-jvmti.so" ]; then
2662306a36Sopenharmony_ci	LIBJVMTI=$PWD/tools/perf/libperf-jvmti.so
2762306a36Sopenharmony_cielif [ -e "$PWD/libperf-jvmti.so" ]; then
2862306a36Sopenharmony_ci	LIBJVMTI=$PWD/libperf-jvmti.so
2962306a36Sopenharmony_cielif [ -e "$PREFIX/lib64/libperf-jvmti.so" ]; then
3062306a36Sopenharmony_ci	LIBJVMTI=$PREFIX/lib64/libperf-jvmti.so
3162306a36Sopenharmony_cielif [ -e "$PREFIX/lib/libperf-jvmti.so" ]; then
3262306a36Sopenharmony_ci	LIBJVMTI=$PREFIX/lib/libperf-jvmti.so
3362306a36Sopenharmony_cielif [ -e "/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so" ]; then
3462306a36Sopenharmony_ci	LIBJVMTI=/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so
3562306a36Sopenharmony_cielse
3662306a36Sopenharmony_ci	echo "Fail to find libperf-jvmti.so"
3762306a36Sopenharmony_ci	# JVMTI is a build option, skip the test if fail to find lib
3862306a36Sopenharmony_ci	exit 2
3962306a36Sopenharmony_cifi
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cicat <<EOF | perf record -k 1 -o $PERF_DATA jshell -s -J-agentpath:$LIBJVMTI
4262306a36Sopenharmony_ciint fib(int x) {
4362306a36Sopenharmony_ci	return x > 1 ? fib(x - 2) + fib(x - 1) : 1;
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ciint q = 0;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cifor (int i = 0; i < 10; i++)
4962306a36Sopenharmony_ci	q += fib(i);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciSystem.out.println(q);
5262306a36Sopenharmony_ciEOF
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciif [ $? -ne 0 ]; then
5562306a36Sopenharmony_ci	echo "Fail to record for java program"
5662306a36Sopenharmony_ci	exit 1
5762306a36Sopenharmony_cifi
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciif ! DEBUGINFOD_URLS='' perf inject -i $PERF_DATA -o $PERF_INJ_DATA -j; then
6062306a36Sopenharmony_ci	echo "Fail to inject samples"
6162306a36Sopenharmony_ci	exit 1
6262306a36Sopenharmony_cifi
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci# Below is an example of the instruction samples reporting:
6562306a36Sopenharmony_ci#   8.18%  jshell           jitted-50116-29.so    [.] Interpreter
6662306a36Sopenharmony_ci#   0.75%  Thread-1         jitted-83602-1670.so  [.] jdk.internal.jimage.BasicImageReader.getString(int)
6762306a36Sopenharmony_ciperf report --stdio -i ${PERF_INJ_DATA} 2>&1 | \
6862306a36Sopenharmony_ci	grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" > /dev/null 2>&1
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciif [ $? -ne 0 ]; then
7162306a36Sopenharmony_ci	echo "Fail to find java symbols"
7262306a36Sopenharmony_ci	exit 1
7362306a36Sopenharmony_cifi
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciexit 0
76