Skip to contentSkip to conversation

Add Intel PT full trace mode basic test case

PATCH Superseded Ammy Yi 2 participants 2 messages Sep 11, 2018

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ammy Yi

author
#0 lore raw

PATCH 1/1 Add Intel PT full trace mode basic test case

---
 runtest/tracing                             |   1 +
 testcases/kernel/tracing/pt_test/.gitignore |   1 +
 testcases/kernel/tracing/pt_test/Makefile   |  21 +++
 testcases/kernel/tracing/pt_test/pt_test.c  | 207 ++++++++++++++++++++++++++++
 4 files changed, 230 insertions(+)
 create mode 100644 testcases/kernel/tracing/pt_test/.gitignore
 create mode 100644 testcases/kernel/tracing/pt_test/Makefile
 create mode 100644 testcases/kernel/tracing/pt_test/pt_test.c
runtest/tracing +1 -0
index 8285eeb43..504132d70 100644
@@ -3,3 +3,4 @@ ftrace_regression01	ftrace_regression01.sh
 ftrace_regression02	ftrace_regression02.sh
 ftrace-stress-test	ftrace_stress_test.sh 90
 dynamic_debug01		dynamic_debug01.sh
+pt_full_trace_basic pt_test
testcases/kernel/tracing/pt_test/.gitignore new file +1 -0
new file mode 100644
index 000000000..96efc142d
@@ -0,0 +1 @@
+/pt_test
testcases/kernel/tracing/pt_test/Makefile new file +21 -0
new file mode 100644
index 000000000..81fc8620f
@@ -0,0 +1,21 @@
+# Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write the Free Software Foundation,
+# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/testcases/kernel/mem/include/libmem.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file
testcases/kernel/tracing/pt_test/pt_test.c new file +207 -0

207 lines not on this page · raw message

Signature · 1 line
2.14.1
#1 lore raw

PATCH 1/1 Add Intel PT full trace mode basic test case

Hi!

testcases/kernel/tracing/pt_test/Makefile line 1

Quoted 3 lines

+++ b/testcases/kernel/tracing/pt_test/Makefile

testcases/kernel/tracing/pt_test/Makefile new file +1 -0
@@ -0,0 +1,21 @@
+# Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.

^
This looks like obvious copy&paste mistake

Quoted 8 lines
testcases/kernel/tracing/pt_test/Makefile new file +8 -0
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

testcases/kernel/tracing/pt_test/Makefile line 20

Quoted 10 lines
testcases/kernel/tracing/pt_test/Makefile new file +10 -0
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write the Free Software Foundation,
+# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/testcases/kernel/mem/include/libmem.mk

^
I doubt that this is needed here

testcases/kernel/tracing/pt_test/Makefile line 21

Quoted 2 lines
testcases/kernel/tracing/pt_test/Makefile new file +1 -0
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file

^
Missing newline at the end of file

Quoted 46 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +40 -0
new file mode 100644
index 000000000..0e85652e5
@@ -0,0 +1,207 @@
+/*
+ * Copyright (C) 2018 Intel Corporation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * author: Ammy Yi ([email protected])
+ * date:   8/20/2018
+ */
+
+/*
+ * This test will check if Intel PT(Intel Processer Trace) full trace mode is
+ * working. It is only applied on X86 platforms.
+ */
+
+#include "tst_test.h"
+#include <linux/perf_event.h>
+#include <sys/syscall.h>
+#include <sched.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define PAGESIZE 4096
+#define INTEL_PT_MEMSIZE 17*PAGESIZE
+
+#define BIT(nr)                 (1UL << (nr))
+
+#define INTEL_PT_PMU_TYPE "/sys/devices/intel_pt/type"
+#define INTEL_PT_FORMAT_TSC "/sys/devices/intel_pt/format/tsc"
+#define INTEL_PT_FORMAT_NRT "/sys/devices/intel_pt/format/noretcomp"

testcases/kernel/tracing/pt_test/pt_test.c line 50

Quoted 10 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +10 -0
+
+//Intel PT event handle
+int FDE = -1;
+//map head and size
+uint64_t ** BUFM = NULL;
+long BUFSZ = 0;
+
+/*
+ * mapping management
+ */

Please avoid commenting the obvious.

testcases/kernel/tracing/pt_test/pt_test.c line 61

Quoted 11 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +11 -0
+uint64_t ** creaMap(int fde,long bufsize)
+{
+	int PROTo;
+	long pbufSz;
+	uint64_t **bufEv;
+	struct perf_event_mmap_page *pc;
+
+	bufEv = malloc(2*sizeof(uint64_t *));
+	if (!bufEv) {
+		return NULL;
+	}

You should use SAFE_MALLOC() instead.

testcases/kernel/tracing/pt_test/pt_test.c line 67

Quoted 5 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +5 -0
+	PROTo = PROT_READ | PROT_WRITE;
+
+	pbufSz = INTEL_PT_MEMSIZE;
+
+	bufEv[0] = mmap(NULL, pbufSz, PROT_READ | PROT_WRITE, MAP_SHARED, fde, 0);

SAFE_MMAP() here.

testcases/kernel/tracing/pt_test/pt_test.c line 74

Quoted 5 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +5 -0
+	if (bufEv[0]!=MAP_FAILED) {
+		pc=(struct perf_event_mmap_page *)bufEv[0];
+		pc->aux_offset=pbufSz;
+		pc->aux_size=bufsize;
+		bufEv[1] = mmap(NULL, bufsize, PROTo, MAP_SHARED, fde, pbufSz);

And here.

testcases/kernel/tracing/pt_test/pt_test.c line 83

Quoted 9 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +9 -0
+
+	}
+	return bufEv;
+}
+
+
+/*
+ * get sysfs value
+ */

Here as well, do not comment the obvious.

Quoted 16 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +16 -0
+int Intel_PT_pmu_value(char *dir)
+{
+	char *value;
+	int temp = 0, val = 0;
+	char delims[] = ":";
+	FILE *f = fopen(dir, "r");
+
+	if (f) {
+		temp = fscanf(f, "%m[^\n]", &value);
+		if (temp != 1){
+			tst_res(TINFO, "Read value fail!");
+        }
+		else {
+			if (strstr(value, delims) == NULL){
+				val = atoi(value);
+			}

testcases/kernel/tracing/pt_test/pt_test.c line 109

Quoted 10 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +10 -0
+			else {
+				strsep(&value, delims);
+				val = atoi(value);
+			}
+		}
+		fclose(f);
+	}
+
+	return val;
+}

We do have SAFE_FILE_SCANF() that does exactly this.

Quoted 5 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +5 -0
+void delMap(uint64_t **bufEv,long bufsize)
+{
+	long pbufSz;
+
+	pbufSz = INTEL_PT_MEMSIZE;

testcases/kernel/tracing/pt_test/pt_test.c line 125

Quoted 10 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +10 -0
+
+	munmap(bufEv[0],pbufSz);
+	munmap(bufEv[1],bufsize);
+
+	free(bufEv);
+}
+
+/*
+ * Will test Intel PT Full Trace mode working normal
+ */

And here as well.

Quoted 8 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +8 -0
+static void INTEL_PT_FULL_TRACE_CHECK(void)
+{
+	struct perf_event_attr attr = {};
+	struct perf_event_mmap_page *pmp;
+	uint64_t aux_head = 0;
+	BUFSZ = 2 * PAGESIZE;
+	//set attr for Intel PT trace
+	attr.type			= Intel_PT_pmu_value(INTEL_PT_PMU_TYPE);

testcases/kernel/tracing/pt_test/pt_test.c line 143

Quoted 10 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +10 -0
+	attr.read_format		= PERF_FORMAT_ID | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_TOTAL_TIME_ENABLED;
+	attr.disabled			= 1;
+	attr.config			= BIT(Intel_PT_pmu_value(INTEL_PT_FORMAT_TSC)) | BIT(Intel_PT_pmu_value(INTEL_PT_FORMAT_NRT));
+	attr.size			= sizeof(struct perf_event_attr);
+	attr.exclude_kernel		= 0;
+	attr.exclude_user		= 0;
+	attr.mmap			= 1;
+
+	//only get trace for own pid
+	FDE = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);

You have to use tst_syscall() here and include lapi/syscalls.h instead of sys/syscall.h.

Quoted 14 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +14 -0
+	if (FDE < 0) {
+		tst_res(TINFO,"Open Intel PT event failed!");
+		tst_res(TFAIL, "perf trace full mode is failed!");
+		return;
+	}
+
+	/* allocate memory for trace */
+	BUFM =  creaMap(FDE,BUFSZ);
+
+	if (!BUFM || (BUFM)[0]==MAP_FAILED || (BUFM)[1]==MAP_FAILED) {
+		tst_res(TINFO,"creaMap failed!");
+		tst_res(TFAIL, "perf trace full mode is failed!");
+		return;
+	}

testcases/kernel/tracing/pt_test/pt_test.c line 167

Quoted 10 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +10 -0
+
+	/* enable tracing */
+	if(ioctl(FDE, PERF_EVENT_IOC_RESET) != 0) {
+		tst_res(TFAIL, "ioctl with PERF_EVENT_IOC_RESET is failed!");
+		return;
+	}
+	if(ioctl(FDE, PERF_EVENT_IOC_ENABLE) != 0) {
+		tst_res(TFAIL, "ioctl with PERF_EVENT_IOC_ENABLE is failed!");
+		return;
+	}

We do have SAFE_IOCTL() as well.

Quoted 11 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +11 -0
+	/* stop tracing */
+	if(ioctl(FDE, PERF_EVENT_IOC_DISABLE) != 0) {
+		tst_res(TFAIL, "ioctl with PERF_EVENT_IOC_DISABLE is failed!");
+		return;
+	}
+
+	// check if there is some trace generated
+	pmp = (struct perf_event_mmap_page *)BUFM[0];
+	aux_head = *(volatile uint64_t *)&pmp->aux_head;
+	if(aux_head == 0) {
+		tst_res(TFAIL, "There is no trace, so failed!");

testcases/kernel/tracing/pt_test/pt_test.c line 189

Quoted 10 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +10 -0
+		return;
+	}
+
+	tst_res(TPASS, "perf trace full mode is passed!");
+
+}
+
+/*
+* clean up for Intel PT test.
+*/

And here as well.

testcases/kernel/tracing/pt_test/pt_test.c line 192

Quoted 3 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +3 -0
+void Cleanup(void) {
+
+	//close device id

This is even more obvious comment, please avoid adding comments like that.

testcases/kernel/tracing/pt_test/pt_test.c line 196

Quoted 4 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +4 -0
+	if(FDE != -1){
+		close(FDE);
+	}
+	// free memory

Here as well.

testcases/kernel/tracing/pt_test/pt_test.c line 207

Quoted 11 lines
testcases/kernel/tracing/pt_test/pt_test.c new file +11 -0
+	if (!BUFM || (BUFM)[0]==MAP_FAILED || (BUFM)[1]==MAP_FAILED){
+		delMap(BUFM,BUFSZ);
+	}
+}
+
+static struct tst_test test = {
+	.test_all = INTEL_PT_FULL_TRACE_CHECK,
+	.min_kver = "4.1",
+	.cleanup = Cleanup,
+	.needs_root = 1,
+};

There are various coding-style violations in the code that should be fixed, you can check for these with the chechpatch.pl script that is shipped with linux kernel sources.

Signature · 2 lines
Cyril Hrubis
[email protected]