linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Using perf_event_open() to sample multiple events of a process
@ 2021-11-05  5:57 Nadav Amit
  2021-11-06  0:45 ` Peter Zijlstra
  0 siblings, 1 reply; 6+ messages in thread
From: Nadav Amit @ 2021-11-05  5:57 UTC (permalink / raw)
  To: kan.liang, Peter Zijlstra; +Cc: LKML, linux-perf-users

Hello Ken, Peter,

I would appreciate some help regarding the use of perf_event_open()
to have multiple samples getting into the same mmap’d memory when they
are both attached to the same process.

I am doing so (using both PERF_FLAG_FD_NO_GROUP and PERF_FLAG_FD_OUTPUT),
but it results in -EINVAL. Debugging the code shows that
perf_event_set_output() fails due to the following check:

        /*
         * If its not a per-cpu rb, it must be the same task.
         */
        if (output_event->cpu == -1 && output_event->ctx != event->ctx)
                goto out;

However, it appears that at this point, event->ctx is not initialized
(it is null) so the test fails and the whole perf_event_open() syscall
fails.

Am I missing something? If not, I am unsure, unfortunately, what the
proper way to fix it is…

I include a small test that fails on my system. The second
perf_event_open fails due to the check in perf_event_set_output():




#define _GNU_SOURCE 1

#include <asm/unistd.h>
#include <linux/perf_event.h>
#include <linux/hw_breakpoint.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

long perf_event_open(struct perf_event_attr* event_attr, pid_t pid, int cpu,
		     int group_fd, unsigned long flags)
{
    return syscall(__NR_perf_event_open, event_attr, pid, cpu, group_fd, flags);
}

int main(void)
{
	pid_t pid = getpid();
	int group_fd, fd;
	void *p;
	struct perf_event_attr pe = {
		.type = 4,
		.size = sizeof(struct perf_event_attr),
		.config = 0x11d0,
		.sample_type = 0x8,
		.sample_period = 1000,
		.precise_ip = 2,
	};

	group_fd = perf_event_open(&pe, pid, -1, -1, PERF_FLAG_FD_CLOEXEC | 
						     PERF_FLAG_FD_NO_GROUP |
						     PERF_FLAG_FD_OUTPUT);

	if (group_fd < 0) {
		perror("first perf_event_open");
		exit(-1);
	}

	p = mmap(NULL, 3 * 4096, PROT_READ|PROT_WRITE, MAP_SHARED, group_fd, 0);

	if (p == MAP_FAILED) {
		perror("MAP_FAILED");
		exit(-1);
	}
	
	pe.config = 0x12d0;
	
	fd = perf_event_open(&pe, pid, -1, group_fd, PERF_FLAG_FD_CLOEXEC | 
						     PERF_FLAG_FD_NO_GROUP |
						     PERF_FLAG_FD_OUTPUT);

	if (fd < 0) {
		perror("second perf_event_open");
		exit(-1);
	}

	printf("success\n");
}


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-11-08 19:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-05  5:57 Using perf_event_open() to sample multiple events of a process Nadav Amit
2021-11-06  0:45 ` Peter Zijlstra
2021-11-06  0:57   ` Peter Zijlstra
2021-11-08 15:24     ` Peter Zijlstra
2021-11-08 19:51       ` Nadav Amit
2021-11-06  0:59   ` Nadav Amit

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).