mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: stephane eranian <eranian@googlemail.com>
To: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>,
	LKML <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Robert Richter <robert.richter@amd.com>,
	Paul Mackerras <paulus@samba.org>,
	Andi Kleen <andi@firstfloor.org>,
	Maynard Johnson <mpjohn@us.ibm.com>, Carl Love <cel@us.ibm.com>,
	Corey J Ashford <cjashfor@us.ibm.com>,
	Philip Mucci <mucci@eecs.utk.edu>,
	Dan Terpstra <terpstra@eecs.utk.edu>,
	perfmon2-devel <perfmon2-devel@lists.sourceforge.net>
Subject: Re: perf_counters issue remapped count
Date: Thu, 13 Aug 2009 13:14:59 +0200	[thread overview]
Message-ID: <7c86c4470908130414xafa4441v5e58fad38e24863e@mail.gmail.com> (raw)
In-Reply-To: <20090813095640.GB28446@elte.hu>

On Thu, Aug 13, 2009 at 11:56 AM, Ingo Molnar<mingo@elte.hu> wrote:
>
> * stephane eranian <eranian@googlemail.com> wrote:
>
>> Hi,
>>
>> I am trying to use the remapped counter feature
>> on Intel Core. It does not work as expected.
>
> it would be highly useful if you posted the .c files you used to
> test it.
>
Here it is. It assumes you have enabled PCE in CR4 on all your CPUs.
I used BRANCH to ensure it would not use a fixed counter given the
hdr->index is wrong for those.

#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/mman.h>
#include <err.h>

#include <perf_counter.h>

#ifdef __x86_64__
#define DECLARE_ARGS(val, low, high)    unsigned low, high
#define EAX_EDX_VAL(val, low, high)     ((low) | ((uint64_t )(high) << 32))
#define EAX_EDX_ARGS(val, low, high)    "a" (low), "d" (high)
#define EAX_EDX_RET(val, low, high)     "=a" (low), "=d" (high)
#else
#define DECLARE_ARGS(val, low, high)    unsigned long long val
#define EAX_EDX_VAL(val, low, high)     (val)
#define EAX_EDX_ARGS(val, low, high)    "A" (val)
#define EAX_EDX_RET(val, low, high)     "=A" (val)
#endif

#define barrier() __asm__ __volatile__("": : :"memory")

static inline unsigned long long rdpmc(int counter)
{
       unsigned low, high;

       asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
       return EAX_EDX_VAL(val, low, high);
}

static volatile int quit;
void sig_handler(int n)
{
       quit = 1;
}

/*
 * our test code (function cannot be made static otherwise it is optimized away)
 */
unsigned long
fib(unsigned long n)
{
       if (n == 0)
               return 0;
       if (n == 1)
               return 2;
       return fib(n-1)+fib(n-2);
}

int
main(int argc, char **argv)
{
       struct perf_counter_mmap_page *hdr;
       struct perf_counter_attr hw;
       char *name;
       uint64_t values[3];
       int ret, fd, src;
       unsigned int seq;

       memset(&hw, 0, sizeof(hw));

       /* make sure we do not use a fixed counter event */
       name = "PERF_COUNT_HW_BRANCH_INSTRUCTIONS";
       hw.type = PERF_TYPE_HARDWARE;
       hw.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
       hw.read_format =
PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING;
       hw.disabled = 0;

       fd = perf_counter_open(&hw, 0, -1, -1, 0);
       if (fd == -1)
               err(1, "cannot open event");

       hdr = mmap(NULL, getpagesize(), PROT_READ, MAP_SHARED, fd, 0);
       if (hdr == MAP_FAILED)
               err(1, "cannot mmap page");

       signal(SIGALRM, sig_handler);

       ioctl(fd, PERF_COUNTER_IOC_ENABLE, 0);

       alarm(10);

       for(;quit == 0;) {
               uint64_t offset = 0;
               unsigned long loop;

               /* generate branches */
               fib(10);

               loop = 0;
               do {
                       loop++;
                       seq = hdr->lock;
                       barrier();
                       if (hdr->index > 0) {
                               values[0] = rdpmc(hdr->index-1);
                               offset = hdr->offset;
                               values[0] += offset;
                               values[1] = hdr->time_enabled;
                               values[2] = hdr->time_running;
                               src = 0;
                       } else {
                               ret = read(fd, values, sizeof(values));
                               src =1;
                               break;
                       }
                       barrier();
               } while (hdr->lock != seq);

               if (values[2])
                       values[0]= (uint64_t)((double)values[0] *
values[1]/values[2]);

               printf("%20"PRIx64" %s %s offset=0x%"PRIx64" src=%s loop=%lu\n",
                       values[0],
                       name,
                       values[1] != values[2] ? "(scaled)" : "",
                       offset, src ? "read" : "rdpmc",
                       loop);
       }
       ioctl(fd, PERF_COUNTER_IOC_DISABLE, 0);
       close(fd);
       return 0;
}

      reply	other threads:[~2009-08-13 11:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-11 22:04 stephane eranian
2009-08-13  9:56 ` Ingo Molnar
2009-08-13 11:14   ` stephane eranian [this message]

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=7c86c4470908130414xafa4441v5e58fad38e24863e@mail.gmail.com \
    --to=eranian@googlemail.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=cel@us.ibm.com \
    --cc=cjashfor@us.ibm.com \
    --cc=eranian@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=mpjohn@us.ibm.com \
    --cc=mucci@eecs.utk.edu \
    --cc=paulus@samba.org \
    --cc=perfmon2-devel@lists.sourceforge.net \
    --cc=robert.richter@amd.com \
    --cc=terpstra@eecs.utk.edu \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®