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
--
To unsubscribe from this list: send the line "unsubscribe linux-perf-users" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to