Commit 78075d94 authored by Ravi Bangoria's avatar Ravi Bangoria Committed by Peter Zijlstra
Browse files

perf test: Add selftest to test IBS invocation via core pmu events



IBS pmu can be invoked via fixed set of core pmu events with 'precise_ip'
set to 1. Add a simple event open test for all these events.

Without kernel fix:
  $ sudo ./perf test -vv 76
   76: AMD IBS via core pmu                                      :
  --- start ---
  test child forked, pid 6553
  Using CPUID AuthenticAMD-25-1-1
  type: 0x0, config: 0x0, fd: 3  -  Pass
  type: 0x0, config: 0x1, fd: -1  -  Pass
  type: 0x4, config: 0x76, fd: -1  -  Fail
  type: 0x4, config: 0xc1, fd: -1  -  Fail
  type: 0x4, config: 0x12, fd: -1  -  Pass
  test child finished with -1
  ---- end ----
  AMD IBS via core pmu: FAILED!

With kernel fix:
  $ sudo ./perf test -vv 76
   76: AMD IBS via core pmu                                      :
  --- start ---
  test child forked, pid 7526
  Using CPUID AuthenticAMD-25-1-1
  type: 0x0, config: 0x0, fd: 3  -  Pass
  type: 0x0, config: 0x1, fd: -1  -  Pass
  type: 0x4, config: 0x76, fd: 3  -  Pass
  type: 0x4, config: 0xc1, fd: 3  -  Pass
  type: 0x4, config: 0x12, fd: -1  -  Pass
  test child finished with 0
  ---- end ----
  AMD IBS via core pmu: Ok

Signed-off-by: default avatarRavi Bangoria <ravi.bangoria@amd.com>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230504110003.2548-5-ravi.bangoria@amd.com
parent 9551fbb6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ int test__intel_pt_pkt_decoder(struct test_suite *test, int subtest);
int test__intel_pt_hybrid_compat(struct test_suite *test, int subtest);
int test__bp_modify(struct test_suite *test, int subtest);
int test__x86_sample_parsing(struct test_suite *test, int subtest);
int test__amd_ibs_via_core_pmu(struct test_suite *test, int subtest);

extern struct test_suite *arch_tests[];

+1 −0
Original line number Diff line number Diff line
@@ -5,3 +5,4 @@ perf-y += arch-tests.o
perf-y += sample-parsing.o
perf-$(CONFIG_AUXTRACE) += insn-x86.o intel-pt-test.o
perf-$(CONFIG_X86_64) += bp-modify.o
perf-y += amd-ibs-via-core-pmu.o
+71 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
#include "arch-tests.h"
#include "linux/perf_event.h"
#include "tests/tests.h"
#include "pmu.h"
#include "pmus.h"
#include "../perf-sys.h"
#include "debug.h"

#define NR_SUB_TESTS 5

static struct sub_tests {
	int type;
	unsigned long config;
	bool valid;
} sub_tests[NR_SUB_TESTS] = {
	{ PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, true },
	{ PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS, false },
	{ PERF_TYPE_RAW, 0x076, true },
	{ PERF_TYPE_RAW, 0x0C1, true },
	{ PERF_TYPE_RAW, 0x012, false },
};

static int event_open(int type, unsigned long config)
{
	struct perf_event_attr attr;

	memset(&attr, 0, sizeof(struct perf_event_attr));
	attr.type = type;
	attr.size = sizeof(struct perf_event_attr);
	attr.config = config;
	attr.disabled = 1;
	attr.precise_ip = 1;
	attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
	attr.sample_period = 100000;

	return sys_perf_event_open(&attr, -1, 0, -1, 0);
}

int test__amd_ibs_via_core_pmu(struct test_suite *test __maybe_unused,
			       int subtest __maybe_unused)
{
	struct perf_pmu *ibs_pmu;
	int ret = TEST_OK;
	int fd, i;

	if (list_empty(&pmus))
		perf_pmu__scan(NULL);

	ibs_pmu = perf_pmu__find("ibs_op");
	if (!ibs_pmu)
		return TEST_SKIP;

	for (i = 0; i < NR_SUB_TESTS; i++) {
		fd = event_open(sub_tests[i].type, sub_tests[i].config);
		pr_debug("type: 0x%x, config: 0x%lx, fd: %d  -  ", sub_tests[i].type,
			 sub_tests[i].config, fd);
		if ((sub_tests[i].valid && fd == -1) ||
		    (!sub_tests[i].valid && fd > 0)) {
			pr_debug("Fail\n");
			ret = TEST_FAIL;
		} else {
			pr_debug("Pass\n");
		}

		if (fd > 0)
			close(fd);
	}

	return ret;
}
+2 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ struct test_suite suite__intel_pt = {
DEFINE_SUITE("x86 bp modify", bp_modify);
#endif
DEFINE_SUITE("x86 Sample parsing", x86_sample_parsing);
DEFINE_SUITE("AMD IBS via core pmu", amd_ibs_via_core_pmu);

struct test_suite *arch_tests[] = {
#ifdef HAVE_DWARF_UNWIND_SUPPORT
@@ -35,5 +36,6 @@ struct test_suite *arch_tests[] = {
	&suite__bp_modify,
#endif
	&suite__x86_sample_parsing,
	&suite__amd_ibs_via_core_pmu,
	NULL,
};