1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2022, Athira Rajeev, IBM Corp.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8
9#include "../event.h"
10#include "misc.h"
11#include "utils.h"
12
13/*
14 * A perf sampling test for making sure
15 * enabling branch stack doesn't crash in any
16 * environment, say:
17 * - With generic compat PMU
18 * - without any PMU registered
19 * - With platform specific PMU
20 * A fix for bhrb sampling crash was added in kernel
21 * via commit: b460b512417a ("powerpc/perf: Fix crashes
22 * with generic_compat_pmu & BHRB")
23 *
24 * This testcase exercises this code by doing branch
25 * stack enable for software event. s/w event is used
26 * since software event will work even in platform
27 * without PMU.
28 */
29static int bhrb_no_crash_wo_pmu_test(void)
30{
31 struct event event;
32
33 /*
34 * Init the event for the sampling test.
35 * This uses software event which works on
36 * any platform.
37 */
38 event_init_opts(e: &event, config: 0, type: PERF_TYPE_SOFTWARE, name: "cycles");
39
40 event.attr.sample_period = 1000;
41 event.attr.sample_type = PERF_SAMPLE_BRANCH_STACK;
42 event.attr.disabled = 1;
43
44 /*
45 * Return code of event_open is not
46 * considered since test just expects no crash from
47 * using PERF_SAMPLE_BRANCH_STACK. Also for environment
48 * like generic compat PMU, branch stack is unsupported.
49 */
50 event_open(e: &event);
51
52 event_close(e: &event);
53 return 0;
54}
55
56int main(void)
57{
58 return test_harness(bhrb_no_crash_wo_pmu_test, "bhrb_no_crash_wo_pmu_test");
59}
60

source code of linux/tools/testing/selftests/powerpc/pmu/sampling_tests/bhrb_no_crash_wo_pmu_test.c