Re: [RFC][PATCH 6/6] perf, tools: X86 RDPMC, RDTSC test

From: Stephane Eranian
Date: Mon Nov 21 2011 - 10:29:47 EST


Peter,

I don't see how this test and infrastructure handles the case where the event
is multiplexed. I know there is time_enabled and time_running. But those are
not sync'd to the moment of the rdpmc(). I think there needs to be some other
timestamp in the mmap struct so the user can compute a delta to then add to
time_enabled and time_running.

Unless, we assume the two time metrics are there ONLY to compute a scaling
ratio. In which case, I think, we don't need the delta because if we
can do rdpmc()
it means the event is currently scheduled and thus time_enabled and time_running
are both ticking which means the scaling ratio does not change since the moment
the event was scheduled in.

Am I understanding the reasoning right?


On Mon, Nov 21, 2011 at 3:51 PM, Peter Zijlstra <a.p.zijlstra@xxxxxxxxx> wrote:
> 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@xxxxxxxxxx>
> Cc: Arun Sharma <asharma@xxxxxx>
> Signed-off-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
> ---
> Â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,
> Â Â Â Â},
>
>
>
N‹§²æìr¸›yúèšØb²X¬¶ÇvØ^–)Þ{.nÇ+‰·¥Š{±‘êçzX§¶›¡Ü}©ž²ÆzÚ&j:+v‰¨¾«‘êçzZ+€Ê+zf£¢·hšˆ§~†­†Ûiÿûàz¹®w¥¢¸?™¨è­Ú&¢)ßf”ù^jÇy§m…á@A«a¶Úÿ 0¶ìh®å’i