Skip to content

Commit 0eb307d

Browse files
captain5050namhyung
authored andcommitted
perf tests buildid: Add purge and remove testing
Add testing for the purge and remove commands. Use the noploop workload rather than just a return to avoid missing samples in the workload in perf record. Tidy up the cleanup code to cleanup when signals happen. Signed-off-by: Ian Rogers <irogers@google.com> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent ac88100 commit 0eb307d

File tree

1 file changed

+177
-26
lines changed

1 file changed

+177
-26
lines changed

tools/perf/tests/shell/buildid.sh

Lines changed: 177 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,69 @@ if [ ${run_pe} -eq 1 ]; then
3636
unset WAYLAND_DISPLAY
3737
fi
3838

39-
ex_md5=$(mktemp /tmp/perf.ex.MD5.XXX)
40-
ex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX)
39+
build_id_dir=
40+
ex_source=$(mktemp /tmp/perf_buildid_test.ex.XXX.c)
41+
ex_md5=$(mktemp /tmp/perf_buildid_test.ex.MD5.XXX)
42+
ex_sha1=$(mktemp /tmp/perf_buildid_test.ex.SHA1.XXX)
4143
ex_pe=$(dirname $0)/../pe-file.exe
44+
data=$(mktemp /tmp/perf_buildid_test.data.XXX)
45+
log_out=$(mktemp /tmp/perf_buildid_test.log.out.XXX)
46+
log_err=$(mktemp /tmp/perf_buildid_test.log.err.XXX)
4247

43-
echo 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c -
44-
echo 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c -
48+
cleanup() {
49+
rm -f ${ex_source} ${ex_md5} ${ex_sha1} ${data} ${log_out} ${log_err}
50+
if [ ${run_pe} -eq 1 ]; then
51+
rm -r ${wineprefix}
52+
fi
53+
if [ -d ${build_id_dir} ]; then
54+
rm -rf ${build_id_dir}
55+
fi
56+
trap - EXIT TERM INT
57+
}
58+
59+
trap_cleanup() {
60+
echo "Unexpected signal in ${FUNCNAME[1]}"
61+
cleanup
62+
exit 1
63+
}
64+
trap trap_cleanup EXIT TERM INT
65+
66+
# Test program based on the noploop workload.
67+
cat <<EOF > ${ex_source}
68+
#include <stdlib.h>
69+
#include <signal.h>
70+
#include <unistd.h>
4571
72+
static volatile sig_atomic_t done;
73+
74+
static void sighandler(int sig)
75+
{
76+
(void)sig;
77+
done = 1;
78+
}
79+
80+
int main(int argc, const char **argv)
81+
{
82+
int sec = 1;
83+
84+
if (argc > 1)
85+
sec = atoi(argv[1]);
86+
87+
signal(SIGINT, sighandler);
88+
signal(SIGALRM, sighandler);
89+
alarm(sec);
90+
91+
while (!done)
92+
continue;
93+
94+
return 0;
95+
}
96+
EOF
97+
cc -Wl,--build-id=sha1 ${ex_source} -o ${ex_sha1} -x c -
98+
cc -Wl,--build-id=md5 ${ex_source} -o ${ex_md5} -x c -
4699
echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}"
47100

48-
check()
101+
get_build_id()
49102
{
50103
case $1 in
51104
*.exe)
@@ -64,6 +117,15 @@ check()
64117
id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
65118
;;
66119
esac
120+
echo ${id}
121+
}
122+
123+
check()
124+
{
125+
file=$1
126+
perf_data=$2
127+
128+
id=$(get_build_id $file)
67129
echo "build id: ${id}"
68130

69131
id_file=${id#??}
@@ -76,45 +138,53 @@ check()
76138
exit 1
77139
fi
78140

79-
file=${build_id_dir}/.build-id/$id_dir/`readlink ${link}`/elf
80-
echo "file: ${file}"
141+
cached_file=${build_id_dir}/.build-id/$id_dir/`readlink ${link}`/elf
142+
echo "file: ${cached_file}"
81143

82144
# Check for file permission of original file
83145
# in case of pe-file.exe file
84146
echo $1 | grep ".exe"
85147
if [ $? -eq 0 ]; then
86-
if [ -x $1 ] && [ ! -x $file ]; then
87-
echo "failed: file ${file} executable does not exist"
148+
if [ -x $1 ] && [ ! -x $cached_file ]; then
149+
echo "failed: file ${cached_file} executable does not exist"
88150
exit 1
89151
fi
90152

91-
if [ ! -x $file ] && [ ! -e $file ]; then
92-
echo "failed: file ${file} does not exist"
153+
if [ ! -x $cached_file ] && [ ! -e $cached_file ]; then
154+
echo "failed: file ${cached_file} does not exist"
93155
exit 1
94156
fi
95-
elif [ ! -x $file ]; then
96-
echo "failed: file ${file} does not exist"
157+
elif [ ! -x $cached_file ]; then
158+
echo "failed: file ${cached_file} does not exist"
97159
exit 1
98160
fi
99161

100-
diff ${file} ${1}
162+
diff ${cached_file} ${1}
101163
if [ $? -ne 0 ]; then
102-
echo "failed: ${file} do not match"
164+
echo "failed: ${cached_file} do not match"
103165
exit 1
104166
fi
105167

106-
${perf} buildid-cache -l | grep ${id}
168+
${perf} buildid-cache -l | grep -q ${id}
107169
if [ $? -ne 0 ]; then
108170
echo "failed: ${id} is not reported by \"perf buildid-cache -l\""
109171
exit 1
110172
fi
111173

174+
if [ -n "${perf_data}" ]; then
175+
${perf} buildid-list -i ${perf_data} | grep -q ${id}
176+
if [ $? -ne 0 ]; then
177+
echo "failed: ${id} is not reported by \"perf buildid-list -i ${perf_data}\""
178+
exit 1
179+
fi
180+
fi
181+
112182
echo "OK for ${1}"
113183
}
114184

115185
test_add()
116186
{
117-
build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
187+
build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
118188
perf="perf --buildid-dir ${build_id_dir}"
119189

120190
${perf} buildid-cache -v -a ${1}
@@ -128,12 +198,88 @@ test_add()
128198
rm -rf ${build_id_dir}
129199
}
130200

201+
test_remove()
202+
{
203+
build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
204+
perf="perf --buildid-dir ${build_id_dir}"
205+
206+
${perf} buildid-cache -v -a ${1}
207+
if [ $? -ne 0 ]; then
208+
echo "failed: add ${1} to build id cache"
209+
exit 1
210+
fi
211+
212+
id=$(get_build_id ${1})
213+
if ! ${perf} buildid-cache -l | grep -q ${id}; then
214+
echo "failed: ${id} not in cache"
215+
exit 1
216+
fi
217+
218+
${perf} buildid-cache -v -r ${1}
219+
if [ $? -ne 0 ]; then
220+
echo "failed: remove ${id} from build id cache"
221+
exit 1
222+
fi
223+
224+
if ${perf} buildid-cache -l | grep -q ${id}; then
225+
echo "failed: ${id} still in cache after remove"
226+
exit 1
227+
fi
228+
229+
echo "remove: OK"
230+
rm -rf ${build_id_dir}
231+
}
232+
233+
test_purge()
234+
{
235+
build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
236+
perf="perf --buildid-dir ${build_id_dir}"
237+
238+
id1=$(get_build_id ${ex_sha1})
239+
${perf} buildid-cache -v -a ${ex_sha1}
240+
if ! ${perf} buildid-cache -l | grep -q ${id1}; then
241+
echo "failed: ${id1} not in cache"
242+
exit 1
243+
fi
244+
245+
id2=$(get_build_id ${ex_md5})
246+
${perf} buildid-cache -v -a ${ex_md5}
247+
if ! ${perf} buildid-cache -l | grep -q ${id2}; then
248+
echo "failed: ${id2} not in cache"
249+
exit 1
250+
fi
251+
252+
# Purge by path
253+
${perf} buildid-cache -v -p ${ex_sha1}
254+
if [ $? -ne 0 ]; then
255+
echo "failed: purge build id cache of ${ex_sha1}"
256+
exit 1
257+
fi
258+
259+
${perf} buildid-cache -v -p ${ex_md5}
260+
if [ $? -ne 0 ]; then
261+
echo "failed: purge build id cache of ${ex_md5}"
262+
exit 1
263+
fi
264+
265+
# Verify both are gone
266+
if ${perf} buildid-cache -l | grep -q ${id1}; then
267+
echo "failed: ${id1} still in cache after purge"
268+
exit 1
269+
fi
270+
271+
if ${perf} buildid-cache -l | grep -q ${id2}; then
272+
echo "failed: ${id2} still in cache after purge"
273+
exit 1
274+
fi
275+
276+
echo "purge: OK"
277+
rm -rf ${build_id_dir}
278+
}
279+
131280
test_record()
132281
{
133-
data=$(mktemp /tmp/perf.data.XXX)
134-
build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
135-
log_out=$(mktemp /tmp/perf.log.out.XXX)
136-
log_err=$(mktemp /tmp/perf.log.err.XXX)
282+
build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
137283
perf="perf --buildid-dir ${build_id_dir}"
138284

139285
echo "running: perf record $*"
@@ -145,7 +291,7 @@ test_record()
145291
fi
146292

147293
args="$*"
148-
check ${args##* }
294+
check ${args##* } ${data}
149295

150296
rm -f ${log_out} ${log_err}
151297
rm -rf ${build_id_dir}
@@ -166,10 +312,15 @@ if [ ${run_pe} -eq 1 ]; then
166312
test_record wine ${ex_pe}
167313
fi
168314

169-
# cleanup
170-
rm ${ex_sha1} ${ex_md5}
171-
if [ ${run_pe} -eq 1 ]; then
172-
rm -r ${wineprefix}
315+
# remove binaries manually via perf buildid-cache -r
316+
test_remove ${ex_sha1}
317+
test_remove ${ex_md5}
318+
if [ ${add_pe} -eq 1 ]; then
319+
test_remove ${ex_pe}
173320
fi
174321

322+
# purge binaries manually via perf buildid-cache -p
323+
test_purge
324+
325+
cleanup
175326
exit 0

0 commit comments

Comments
 (0)