public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
To: mingo@elte.hu
Cc: William Cohen <wcohen@redhat.com>,
	linux-kernel@vger.kernel.org,
	Stephane Eranian <eranian@google.com>,
	Arun Sharma <asharma@fb.com>, Vince Weaver <vince@deater.net>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [RFC][PATCH 6/6] perf, tools: X86 RDPMC, RDTSC test
Date: Mon, 21 Nov 2011 15:51:20 +0100	[thread overview]
Message-ID: <20111121145337.912104693@chello.nl> (raw)
In-Reply-To: 20111121145114.049265181@chello.nl

[-- Attachment #1: perf-tools-test-rdpmc.patch --]
[-- Type: text/plain, Size: 3633 bytes --]

Implement a simple test for the self-monitoring data from the
mmap-control page.

 6: x86 rdpmc test: Ok
0: 6042 2481
1: 60042 23669
2: 605410 245588
3: 6060818 2396231

XXX: come up with logic to automagically turn these numbers into OK/FAIL.

Cc: Stephane Eranian <eranian@google.com>
Cc: Arun Sharma <asharma@fb.com>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
 tools/perf/builtin-test.c |  129 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
Index: linux-2.6/tools/perf/builtin-test.c
===================================================================
--- linux-2.6.orig/tools/perf/builtin-test.c
+++ linux-2.6/tools/perf/builtin-test.c
@@ -14,6 +14,8 @@
 #include "util/thread_map.h"
 #include "../../include/linux/hw_breakpoint.h"
 
+#include <sys/mman.h>
+
 static long page_size;
 
 static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
@@ -841,6 +843,127 @@ static int test__parse_events(void)
 
 	return ret;
 }
+
+#if defined(__x86_64__) || defined(__i386__)
+
+#define barrier() asm volatile("" ::: "memory")
+
+static u64 rdpmc(unsigned int counter)
+{
+	unsigned int low, high;
+
+	asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
+
+	return low | ((u64)high) << 32;
+}
+
+static u64 rdtsc(void)
+{
+	unsigned int low, high;
+
+	asm volatile("rdtsc" : "=a" (low), "=d" (high));
+
+	return low | ((u64)high) << 32;
+}
+
+static u64 mmap_read_self(void *addr, u64 *enabled, u64 *running)
+{
+	struct perf_event_mmap_page *pc = addr;
+	u32 seq;
+	u64 count, delta;
+
+	do {
+again:
+		seq = pc->lock;
+		barrier();
+		if (seq & 1)
+			goto again;
+
+		if ((enabled || running) && pc->time_mult) {
+			u64 cyc = rdtsc();
+			u64 rem, quot;
+
+			quot = (cyc >> pc->time_shift);
+			rem = cyc & ((1 << pc->time_shift) - 1);
+			delta = pc->time_offset +
+				quot * pc->time_mult +
+				((rem * pc->time_mult) >> pc->time_shift);
+		}
+
+		if (enabled)
+			*enabled = pc->time_enabled + delta;
+
+		if (running)
+			*running = pc->time_running + delta;
+
+		if (pc->index) {
+			count = rdpmc(pc->index - 1);
+			count += pc->offset;
+		} else
+			goto fail;
+
+		barrier();
+	} while (pc->lock != seq);
+
+	return count;
+
+fail:
+	/*
+	 * XXX do read() here
+	 */
+	printf("FAIL FAIL FAIL\n");
+	return 0;
+}
+
+static int test__rdpmc(void)
+{
+	volatile int tmp = 0;
+	int loops = 1000;
+	int n, i;
+	int fd;
+	void *addr;
+	struct perf_event_attr attr = {
+		.type = PERF_TYPE_HARDWARE,
+		.config = PERF_COUNT_HW_INSTRUCTIONS,
+	};
+
+
+	fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
+	if (fd < 0) {
+		die("Error: sys_perf_event_open() syscall returned "
+		    "with %d (%s)\n", fd, strerror(errno));
+	}
+
+	addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
+	if (addr == (void *)(-1)) {
+		die("Error: mmap() syscall returned "
+		    "with (%s)\n", strerror(errno));
+	}
+
+	for (n = 0; n < 4; n++) {
+
+		u64 stamp, now;
+		u64 stamp2, now2;
+
+		stamp = mmap_read_self(addr, NULL, &stamp2);
+
+		for (i = 0; i < loops; i++)
+			tmp++;
+
+		now = mmap_read_self(addr, NULL, &now2);
+		loops *= 10;
+
+		printf("%d: %Lu %Lu\n", n, (unsigned long long)(now - stamp),
+					   (unsigned long long)(now2 - stamp2));
+	}
+
+	munmap(addr, page_size);
+	close(fd);
+
+	return 0;
+}
+#endif
+
 static struct test {
 	const char *desc;
 	int (*func)(void);
@@ -865,6 +988,12 @@ static struct test {
 		.desc = "parse events tests",
 		.func = test__parse_events,
 	},
+#if defined(__x86_64__) || defined(__i386__)
+	{
+		.desc = "x86 rdpmc test",
+		.func = test__rdpmc,
+	},
+#endif
 	{
 		.func = NULL,
 	},



  parent reply	other threads:[~2011-11-21 14:56 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-21 14:51 [RFC][PATCH 0/6] perf: x86 RDPMC and RDTSC support Peter Zijlstra
2011-11-21 14:51 ` [RFC][PATCH 1/6] perf: Update the mmap control page on mmap() Peter Zijlstra
2011-11-21 14:51 ` [RFC][PATCH 2/6] perf, arch: Rework perf_event_index() Peter Zijlstra
2011-11-21 16:22   ` Eric B Munson
2011-11-21 17:23   ` Will Deacon
2011-11-21 19:18     ` Peter Zijlstra
2011-11-21 20:31       ` Will Deacon
2011-11-21 20:35         ` Peter Zijlstra
2011-11-21 22:43           ` Will Deacon
2011-11-22 11:26             ` Peter Zijlstra
2011-11-22 11:47               ` Will Deacon
2011-11-22 11:49                 ` Oleg Strikov
2011-11-22 11:52                   ` Will Deacon
2011-11-22 11:56                     ` Oleg Strikov
2011-11-22 12:00                     ` Oleg Strikov
2011-11-22 12:14                       ` Will Deacon
2011-11-22 12:25                         ` Oleg Strikov
2011-11-22 11:51                 ` Peter Zijlstra
2011-11-22 11:54                   ` Will Deacon
2011-11-22 11:48               ` Oleg Strikov
2011-11-21 14:51 ` [RFC][PATCH 3/6] perf, x86: Implement userspace RDPMC Peter Zijlstra
2011-11-21 14:51 ` [RFC][PATCH 4/6] perf, x86: Provide means of disabling " Peter Zijlstra
2011-11-21 14:51 ` [RFC][PATCH 5/6] perf: Extend the mmap control page with time (TSC) fields Peter Zijlstra
2011-12-28 17:55   ` Stephane Eranian
2011-11-21 14:51 ` Peter Zijlstra [this message]
2011-11-21 15:29   ` [RFC][PATCH 6/6] perf, tools: X86 RDPMC, RDTSC test Stephane Eranian
2011-11-21 15:37     ` Peter Zijlstra
2011-11-21 16:59       ` Peter Zijlstra
2011-11-21 17:42         ` Stephane Eranian
2011-11-21 15:02 ` [RFC][PATCH 0/6] perf: x86 RDPMC and RDTSC support Vince Weaver
2011-11-21 16:05   ` William Cohen
2011-11-21 16:08   ` William Cohen
2011-12-02 19:26 ` Arun Sharma
2011-12-02 22:22   ` Stephane Eranian
2011-12-05 20:16     ` Arun Sharma
2011-12-05 23:17       ` Arun Sharma
2011-12-06  1:38         ` Stephane Eranian
2011-12-06  9:42         ` Peter Zijlstra
2011-12-06 21:53           ` Arun Sharma
2011-12-16 22:36 ` Vince Weaver
2011-12-21 12:58   ` Peter Zijlstra
2011-12-21 13:15     ` Ingo Molnar
2011-12-23 20:12       ` Vince Weaver
2011-12-21 15:04     ` Vince Weaver
2011-12-21 21:32       ` Vince Weaver
2011-12-21 21:41         ` Peter Zijlstra
2011-12-21 22:19           ` Vince Weaver
2011-12-21 22:32             ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111121145337.912104693@chello.nl \
    --to=a.p.zijlstra@chello.nl \
    --cc=asharma@fb.com \
    --cc=eranian@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=vince@deater.net \
    --cc=wcohen@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox