linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Moody <benjamin.moody@gmail.com>
To: David Ahern <dsahern@gmail.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: Bizarre results from perf event API
Date: Sat, 21 Feb 2015 21:44:02 +0000	[thread overview]
Message-ID: <CAAk6P0X5N3pvEjaEy-v6mHy7+fg3pdLyeRYR2VGm2wuqPYeOZw@mail.gmail.com> (raw)
In-Reply-To: <54E7D3B3.7020108@gmail.com>

Hi,

> Trying using a program with established results. e.g., This is from
> PeterZ years ago. On intel 'perf stat -e instructions a.out' should show
> ~1 billion (a wee bit more, but clearly in the 1b range).

That program itself gave the expected results, but it's a good
suggestion.  When I tried changing that program to also fork a bunch
of child processes, I started getting similarly weird results to what
I had seen before.

Looking more closely at the behavior of perf, I think the essential
difference is that perf uses the *child* PID as argument to
perf_event_open.  When I tried changing my program to do the same, it
seems to have fixed the problem.  That is to say, where I used
something like

   fd = perf_event_open(..., getpid(), ...);
   child = fork();
   if (child == 0) {
       execvp(...);
   }

I needed to instead use

   pipe(pipefd);
   child = fork();
   if (child == 0) {
       read(pipefd[0], &x, 1);
       execvp(...);
   }
   fd = perf_event_open(..., child, ...);
   write(pipefd[1], &x, 1);

This seems like a bug, especially since it only seems to come up when
there are grandchild processes involved.  (And I can't help thinking
the first version is a lot more elegant!)  Does the kernel somehow get
confused because enable_on_exec is set and the original process hasn't
actually exec'ed anything?

The problem doesn't occur *every* time there are grandchildren - I
didn't see any problems when running some basic experiments with shell
scripts.  But here's a simple C program that does exhibit the problem:

int main()
{
  int i, j;
  for (i = 0; i < 50; i++) {
    if (!fork()) {
      for (i = 0; i < 100000000; i++) {
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
      }
      exit(0);
    }
  }
  for (i = 0; i < 50; i++)
    wait(&j);
  return 0;
}

At any rate, thanks for your advice!

Benjamin

  reply	other threads:[~2015-02-21 21:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-20 23:26 Bizarre results from perf event API Benjamin Moody
2015-02-21  0:39 ` David Ahern
2015-02-21 21:44   ` Benjamin Moody [this message]
2015-02-21 22:59     ` Vince Weaver
2015-02-22 20:12       ` Benjamin Moody
2015-02-22 20:37         ` Vince Weaver
2015-02-24  0:46           ` Benjamin Moody
2015-02-24  1:21     ` David Ahern
2015-02-27 21:41       ` Benjamin Moody
2015-02-23 20:44 ` Andi Kleen
2015-02-23 23:13   ` Benjamin Moody

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=CAAk6P0X5N3pvEjaEy-v6mHy7+fg3pdLyeRYR2VGm2wuqPYeOZw@mail.gmail.com \
    --to=benjamin.moody@gmail.com \
    --cc=dsahern@gmail.com \
    --cc=linux-perf-users@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).