1bdc35cbcSAlexey Budankov#!/bin/sh
2bdc35cbcSAlexey Budankov# Zstd perf.data compression/decompression
3bdc35cbcSAlexey Budankov
45875cf4cSArnaldo Carvalho de Melo# SPDX-License-Identifier: GPL-2.0
55875cf4cSArnaldo Carvalho de Melo
6bdc35cbcSAlexey Budankovtrace_file=$(mktemp /tmp/perf.data.XXX)
7bdc35cbcSAlexey Budankovperf_tool=perf
8bdc35cbcSAlexey Budankov
9bdc35cbcSAlexey Budankovskip_if_no_z_record() {
10d94cfbabSArnaldo Carvalho de Melo	$perf_tool record -h 2>&1 | grep -q '\-z, \-\-compression\-level'
11bdc35cbcSAlexey Budankov}
12bdc35cbcSAlexey Budankov
13bdc35cbcSAlexey Budankovcollect_z_record() {
14bdc35cbcSAlexey Budankov	echo "Collecting compressed record file:"
15a9cdc1c5SJames Clark	[ "$(uname -m)" != s390x ] && gflag='-g'
16*3a4367c1SAthira Rajeev	$perf_tool record -o "$trace_file" $gflag -z -F 5000 -- \
17d93fc7acSJames Clark		dd count=500 if=/dev/urandom of=/dev/null
18bdc35cbcSAlexey Budankov}
19bdc35cbcSAlexey Budankov
20bdc35cbcSAlexey Budankovcheck_compressed_stats() {
21bdc35cbcSAlexey Budankov	echo "Checking compressed events stats:"
22*3a4367c1SAthira Rajeev	$perf_tool report -i "$trace_file" --header --stats | \
23d94cfbabSArnaldo Carvalho de Melo		grep -E "(# compressed : Zstd,)|(COMPRESSED events:)"
24bdc35cbcSAlexey Budankov}
25bdc35cbcSAlexey Budankov
26bdc35cbcSAlexey Budankovcheck_compressed_output() {
27*3a4367c1SAthira Rajeev	$perf_tool inject -i "$trace_file" -o "$trace_file.decomp" &&
28*3a4367c1SAthira Rajeev	$perf_tool report -i "$trace_file" --stdio -F comm,dso,sym | head -n -3 > "$trace_file.comp.output" &&
29*3a4367c1SAthira Rajeev	$perf_tool report -i "$trace_file.decomp" --stdio -F comm,dso,sym | head -n -3 > "$trace_file.decomp.output" &&
30*3a4367c1SAthira Rajeev	diff "$trace_file.comp.output" "$trace_file.decomp.output"
31bdc35cbcSAlexey Budankov}
32bdc35cbcSAlexey Budankov
33bdc35cbcSAlexey Budankovskip_if_no_z_record || exit 2
34bdc35cbcSAlexey Budankovcollect_z_record && check_compressed_stats && check_compressed_output
35bdc35cbcSAlexey Budankoverr=$?
36*3a4367c1SAthira Rajeevrm -f "$trace_file*"
37bdc35cbcSAlexey Budankovexit $err
38