18c2ecf20Sopenharmony_ci#!/bin/sh
28c2ecf20Sopenharmony_ci# build id cache operations
38c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci# skip if there's no readelf
68c2ecf20Sopenharmony_ciif ! [ -x "$(command -v readelf)" ]; then
78c2ecf20Sopenharmony_ci	echo "failed: no readelf, install binutils"
88c2ecf20Sopenharmony_ci	exit 2
98c2ecf20Sopenharmony_cifi
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci# skip if there's no compiler
128c2ecf20Sopenharmony_ciif ! [ -x "$(command -v cc)" ]; then
138c2ecf20Sopenharmony_ci	echo "failed: no compiler, install gcc"
148c2ecf20Sopenharmony_ci	exit 2
158c2ecf20Sopenharmony_cifi
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciex_md5=$(mktemp /tmp/perf.ex.MD5.XXX)
188c2ecf20Sopenharmony_ciex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciecho 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c -
218c2ecf20Sopenharmony_ciecho 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c -
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciecho "test binaries: ${ex_sha1} ${ex_md5}"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cicheck()
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	echo "build id: ${id}"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	link=${build_id_dir}/.build-id/${id:0:2}/${id:2}
328c2ecf20Sopenharmony_ci	echo "link: ${link}"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	if [ ! -h $link ]; then
358c2ecf20Sopenharmony_ci		echo "failed: link ${link} does not exist"
368c2ecf20Sopenharmony_ci		exit 1
378c2ecf20Sopenharmony_ci	fi
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	file=${build_id_dir}/.build-id/${id:0:2}/`readlink ${link}`/elf
408c2ecf20Sopenharmony_ci	echo "file: ${file}"
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if [ ! -x $file ]; then
438c2ecf20Sopenharmony_ci		echo "failed: file ${file} does not exist"
448c2ecf20Sopenharmony_ci		exit 1
458c2ecf20Sopenharmony_ci	fi
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	diff ${file} ${1}
488c2ecf20Sopenharmony_ci	if [ $? -ne 0 ]; then
498c2ecf20Sopenharmony_ci		echo "failed: ${file} do not match"
508c2ecf20Sopenharmony_ci		exit 1
518c2ecf20Sopenharmony_ci	fi
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	echo "OK for ${1}"
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_citest_add()
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
598c2ecf20Sopenharmony_ci	perf="perf --buildid-dir ${build_id_dir}"
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	${perf} buildid-cache -v -a ${1}
628c2ecf20Sopenharmony_ci	if [ $? -ne 0 ]; then
638c2ecf20Sopenharmony_ci		echo "failed: add ${1} to build id cache"
648c2ecf20Sopenharmony_ci		exit 1
658c2ecf20Sopenharmony_ci	fi
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	check ${1}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	rm -rf ${build_id_dir}
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_citest_record()
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	data=$(mktemp /tmp/perf.data.XXX)
758c2ecf20Sopenharmony_ci	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
768c2ecf20Sopenharmony_ci	perf="perf --buildid-dir ${build_id_dir}"
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	${perf} record --buildid-all -o ${data} ${1}
798c2ecf20Sopenharmony_ci	if [ $? -ne 0 ]; then
808c2ecf20Sopenharmony_ci		echo "failed: record ${1}"
818c2ecf20Sopenharmony_ci		exit 1
828c2ecf20Sopenharmony_ci	fi
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	check ${1}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	rm -rf ${build_id_dir}
878c2ecf20Sopenharmony_ci	rm -rf ${data}
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci# add binaries manual via perf buildid-cache -a
918c2ecf20Sopenharmony_citest_add ${ex_sha1}
928c2ecf20Sopenharmony_citest_add ${ex_md5}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci# add binaries via perf record post processing
958c2ecf20Sopenharmony_citest_record ${ex_sha1}
968c2ecf20Sopenharmony_citest_record ${ex_md5}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci# cleanup
998c2ecf20Sopenharmony_cirm ${ex_sha1} ${ex_md5}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciexit ${err}
102