Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1380128imw; Tue, 5 Jul 2022 08:27:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tI9EpR8guT09dSv2MJFyh7u9LM3IyF+e4mkbTkQXtfh6tH7Dr/BMAZdvBXwM+/zCnRfUwn X-Received: by 2002:a17:902:aa05:b0:16a:5113:229d with SMTP id be5-20020a170902aa0500b0016a5113229dmr41668866plb.111.1657034819841; Tue, 05 Jul 2022 08:26:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657034819; cv=none; d=google.com; s=arc-20160816; b=LVurI9l5Q/O+k0yz0pwkCScfQTYwucWp110S6Xp0yDXkKotgM677t35hIVXL5iUQpp OM3JKPJU83Gy4C3cepV/QnCwn2TsfJPCxGYTxgLmf37Hj20p/tefuTkhnuvqEhVrZ8kY gSLTBWKY2mFg2OMJeALbZS1O/js0iQiUAjCmkGKADQAOoioTziqtg3sXO4GgJukfBYAO cnBvYnFkqzeK/gx3/XNU80rF40L/SplWo0H6qG91fqXAJ8jjPdczhVnkIMKGYWnOBdty THjx2zqnR9dVeD5RYUm7t2MIJn3OLIbuOp/tcsAUvojmE8gzPzcVZOpEZu7bpkJrVzVz Xk4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=1Vbofzdblq+1SkxQ1IA30pKSbs5nW6UjiI/ki6Wx4fo=; b=dxrS5cXRERGRHuG3OUPUr92SVwP6bpoKeBBi3rEVM76jdwlP4Hyy+mKru9xZxPUnly eDd53796VWTaUbfIfOq65cPmtL5rGIfgHEdegqM6sLZFJTs0HzsLxw7W3goPfb0mun0J AKH2rQ/UhwIRIp/wYKb4k+ERUgi7H07u8zg3+pJVkkilL4vyoKifvCq3RvOD6ESWokxi HI1r7NdtXVNiuRYLw+7o7tFwND8baZUtHaoEujYHHVeIJ8oMn/UZObmcSSyraa4NhbDL aXWSWd0JTMC2AkSICreteXKrfJ4Uo4UKaitQ8CIW0u7IWMgjciLeh1VnvCnrmp5ZKGl1 ncJw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y21-20020a63fa15000000b0040c99b2c660si22133275pgh.177.2022.07.05.08.26.48; Tue, 05 Jul 2022 08:26:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232635AbiGEPI5 (ORCPT + 99 others); Tue, 5 Jul 2022 11:08:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231927AbiGEPIf (ORCPT ); Tue, 5 Jul 2022 11:08:35 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DA69622BDF; Tue, 5 Jul 2022 08:05:25 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DF8631570; Tue, 5 Jul 2022 08:05:25 -0700 (PDT) Received: from e127744.cambridge.arm.com (e127744.cambridge.arm.com [10.1.26.123]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 1A17F3F66F; Tue, 5 Jul 2022 08:05:22 -0700 (PDT) From: German Gomez To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org, irogers@google.com Cc: German Gomez , Anshuman Khandual , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim Subject: [PATCH v2 1/1] perf test: Add test for branch stack sampling Date: Tue, 5 Jul 2022 16:05:11 +0100 Message-Id: <20220705150511.473919-2-german.gomez@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220705150511.473919-1-german.gomez@arm.com> References: <20220705150511.473919-1-german.gomez@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a self test for branch stack sampling, to check that we get the expected branch types, and filters behave as expected. Suggested-by: Anshuman Khandual Signed-off-by: German Gomez --- tools/perf/tests/shell/test_brstack.sh | 114 +++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100755 tools/perf/tests/shell/test_brstack.sh diff --git a/tools/perf/tests/shell/test_brstack.sh b/tools/perf/tests/shell/test_brstack.sh new file mode 100755 index 000000000..5f6f40a77 --- /dev/null +++ b/tools/perf/tests/shell/test_brstack.sh @@ -0,0 +1,114 @@ +#!/bin/sh +# Check branch stack sampling + +# SPDX-License-Identifier: GPL-2.0 +# German Gomez , 2022 + +# we need a C compiler to build the test programs +# so bail if none is found +if ! [ -x "$(command -v cc)" ]; then + echo "failed: no compiler, install gcc" + exit 2 +fi + +# skip the test if the hardware doesn't support branch stack sampling +perf record -b -o- -e dummy -B true > /dev/null 2>&1 || exit 2 + +TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX) + +cleanup() { + rm -rf $TMPDIR +} + +trap cleanup exit term int + +gen_test_program() { + # generate test program + cat << EOF > $1 +#define BENCH_RUNS 999999 +int cnt; +void bar(void) { +} /* return */ +void foo(void) { + bar(); /* call */ +} /* return */ +void bench(void) { + void (*foo_ind)(void) = foo; + if ((cnt++) % 3) /* branch (cond) */ + foo(); /* call */ + bar(); /* call */ + foo_ind(); /* call (ind) */ +} +int main(void) +{ + int cnt = 0; + while (1) { + if ((cnt++) > BENCH_RUNS) + break; + bench(); /* call */ + } /* branch (uncond) */ + return 0; +} +EOF +} + +test_user_branches() { + echo "Testing user branch stack sampling" + + gen_test_program "$TEMPDIR/program.c" + cc -fno-inline -g "$TEMPDIR/program.c" -o $TMPDIR/a.out + + perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u -- $TMPDIR/a.out > /dev/null 2>&1 + perf script -i $TMPDIR/perf.data --fields brstacksym | xargs -n1 > $TMPDIR/perf.script + + # example of branch entries: + # foo+0x14/bar+0x40/P/-/-/0/CALL + + set -x + egrep -m1 "^bench\+[^ ]*/foo\+[^ ]*/IND_CALL$" $TMPDIR/perf.script + egrep -m1 "^foo\+[^ ]*/bar\+[^ ]*/CALL$" $TMPDIR/perf.script + egrep -m1 "^bench\+[^ ]*/foo\+[^ ]*/CALL$" $TMPDIR/perf.script + egrep -m1 "^bench\+[^ ]*/bar\+[^ ]*/CALL$" $TMPDIR/perf.script + egrep -m1 "^bar\+[^ ]*/foo\+[^ ]*/RET$" $TMPDIR/perf.script + egrep -m1 "^foo\+[^ ]*/bench\+[^ ]*/RET$" $TMPDIR/perf.script + egrep -m1 "^bench\+[^ ]*/bench\+[^ ]*/COND$" $TMPDIR/perf.script + egrep -m1 "^main\+[^ ]*/main\+[^ ]*/UNCOND$" $TMPDIR/perf.script + set +x + + # some branch types are still not being tested: + # IND COND_CALL COND_RET SYSCALL SYSRET IRQ SERROR NO_TX +} + +# first argument is the argument passed to "--branch-stack ,save_type,u" +# second argument are the expected branch types for the given filter +test_filter() { + local filter=$1 + local expect=$2 + + echo "Testing branch stack filtering permutation ($filter,$expect)" + + gen_test_program "$TEMPDIR/program.c" + cc -fno-inline -g "$TEMPDIR/program.c" -o $TMPDIR/a.out + + perf record -o $TMPDIR/perf.data --branch-filter $filter,save_type,u -- $TMPDIR/a.out > /dev/null 2>&1 + perf script -i $TMPDIR/perf.data --fields brstack | xargs -n1 > $TMPDIR/perf.script + + # fail if we find any branch type that doesn't match any of the expected ones + # also consider UNKNOWN branch types (-) + if egrep -vm1 "^[^ ]*/($expect|-|( *))$" $TMPDIR/perf.script; then + return 1 + fi +} + +set -e + +test_user_branches + +test_filter "any_call" "CALL|IND_CALL|COND_CALL|SYSCALL|IRQ" +test_filter "call" "CALL|SYSCALL" +test_filter "cond" "COND" +test_filter "any_ret" "RET|COND_RET|SYSRET|ERET" + +test_filter "call,cond" "CALL|SYSCALL|COND" +test_filter "any_call,cond" "CALL|IND_CALL|COND_CALL|IRQ|SYSCALL|COND" +test_filter "cond,any_call,any_ret" "COND|CALL|IND_CALL|COND_CALL|SYSCALL|IRQ|RET|COND_RET|SYSRET|ERET" -- 2.25.1