From patchwork Tue Sep 11 02:33:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammy Yi X-Patchwork-Id: 968484 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=intel.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 428g3p068Tz9s4s for ; Tue, 11 Sep 2018 19:44:21 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 42AFB3E61E5 for ; Tue, 11 Sep 2018 11:44:19 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [217.194.8.3]) by picard.linux.it (Postfix) with ESMTP id E89E83E627F for ; Tue, 11 Sep 2018 04:51:51 +0200 (CEST) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by in-3.smtp.seeweb.it (Postfix) with ESMTPS id CCA941A0098D for ; Tue, 11 Sep 2018 04:51:48 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Sep 2018 19:51:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,358,1531810800"; d="scan'208";a="90601599" Received: from yifei-server.sh.intel.com ([10.239.13.94]) by orsmga002.jf.intel.com with ESMTP; 10 Sep 2018 19:51:44 -0700 From: Ammy Yi To: ltp@lists.linux.it Date: Tue, 11 Sep 2018 10:33:29 +0800 Message-Id: <20180911023329.9376-1-ammy.yi@intel.com> X-Mailer: git-send-email 2.14.1 X-Virus-Scanned: clamav-milter 0.99.2 at in-3.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-3.smtp.seeweb.it X-Mailman-Approved-At: Tue, 11 Sep 2018 11:44:17 +0200 Cc: Ammy Yi Subject: [LTP] [PATCH ltp] Add Intel PT full trace mode basic test case X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Ammy Yi --- 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 diff --git a/runtest/tracing b/runtest/tracing index 8285eeb43..504132d70 100644 --- a/runtest/tracing +++ b/runtest/tracing @@ -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 diff --git a/testcases/kernel/tracing/pt_test/.gitignore b/testcases/kernel/tracing/pt_test/.gitignore new file mode 100644 index 000000000..96efc142d --- /dev/null +++ b/testcases/kernel/tracing/pt_test/.gitignore @@ -0,0 +1 @@ +/pt_test diff --git a/testcases/kernel/tracing/pt_test/Makefile b/testcases/kernel/tracing/pt_test/Makefile new file mode 100644 index 000000000..81fc8620f --- /dev/null +++ b/testcases/kernel/tracing/pt_test/Makefile @@ -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 diff --git a/testcases/kernel/tracing/pt_test/pt_test.c b/testcases/kernel/tracing/pt_test/pt_test.c new file mode 100644 index 000000000..0e85652e5 --- /dev/null +++ b/testcases/kernel/tracing/pt_test/pt_test.c @@ -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 . + * + * author: Ammy Yi (ammy.yi@intel.com) + * 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 +#include +#include +#include +#include + +#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" + +//Intel PT event handle +int FDE = -1; +//map head and size +uint64_t ** BUFM = NULL; +long BUFSZ = 0; + +/* + * mapping management + */ +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; + } + + PROTo = PROT_READ | PROT_WRITE; + + pbufSz = INTEL_PT_MEMSIZE; + + bufEv[0] = mmap(NULL, pbufSz, PROT_READ | PROT_WRITE, MAP_SHARED, fde, 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); + + } + return bufEv; +} + + +/* + * get sysfs value + */ +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); + } + else { + strsep(&value, delims); + val = atoi(value); + } + } + fclose(f); + } + + return val; +} + +void delMap(uint64_t **bufEv,long bufsize) +{ + long pbufSz; + + pbufSz = INTEL_PT_MEMSIZE; + + munmap(bufEv[0],pbufSz); + munmap(bufEv[1],bufsize); + + free(bufEv); +} + +/* + * Will test Intel PT Full Trace mode working normal + */ +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); + 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); + 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; + } + + /* 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; + } + + /* 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!"); + return; + } + + tst_res(TPASS, "perf trace full mode is passed!"); + +} + +/* +* clean up for Intel PT test. +*/ +void Cleanup(void) { + + //close device id + if(FDE != -1){ + close(FDE); + } + // free memory + 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, +};