2

Under sample mode, when I test on all cpus, pid = 1:

fd = perf_event_open(&attr, 1, -1, -1, 0);

I always see pid == tid:

**********************************
value of meta_page: 7fd262ce2000
value of meta_page->data_head: 9838
value of meta_page->data_tail: 9820
value of meta_page->data_size: 10000
The size of the data section is: 18
sample.addr: 0x5622ef76f948
sample.pid: 1
sample.tid: 1
the final current is: 36197080
the final upper bound is: 36197080
**********************************
**********************************
value of meta_page: 7fd262ce2000
value of meta_page->data_head: 9850
value of meta_page->data_tail: 9838
value of meta_page->data_size: 10000
The size of the data section is: 18
sample.addr: 0x7fffc51ef080
sample.pid: 1
sample.tid: 1
the final current is: 36197080
the final upper bound is: 36197080
**********************************
**********************************
value of meta_page: 7fd262ce2000
value of meta_page->data_head: 9850
value of meta_page->data_tail: 9850
value of meta_page->data_size: 10000
Full ring buffer
**********************************

I have done the following trials:

a) I bind mysqld to cpu 0 and 1, with taskset -cp 0,1 pidof(mysqld)

b) I test with fd = perf_event_open(&attr, -1, 0, -1, 0);

and I can see many results with pid == pidof(mysqld), which is 2211374 in the following example:

**********************************
value of meta_page: 7f6fa4a18000
value of meta_page->data_head: 15820
value of meta_page->data_tail: 157e8
value of meta_page->data_size: 10000
The size of the data section is: 38
the header type is: 6
sample.addr: 0x7f8dd802e058
sample.pid: 2211374
sample.tid: 3148307
the final current is: 12784408
the final upper bound is: 12784408
**********************************
**********************************
value of meta_page: 7f6fa4a18000
value of meta_page->data_head: 15858
value of meta_page->data_tail: 15820
value of meta_page->data_size: 10000
The size of the data section is: 38
the header type is: 5
sample.addr: 0x7f8e3ff75d90
sample.pid: 2211374
sample.tid: 3148308
the final current is: 12784408
the final upper bound is: 12784408
**********************************
**********************************
value of meta_page: 7f6fa4a18000
value of meta_page->data_head: 15890
value of meta_page->data_tail: 15858
value of meta_page->data_size: 10000
The size of the data section is: 38
the header type is: 6
sample.addr: 0x7f8da803b6e0
sample.pid: 2211374
sample.tid: 3148306
the final current is: 12784408
the final upper bound is: 12784408
**********************************

c) but I can get barely anything testing with fd = perf_event_open(&attr, pidof(mysqld), 0, -1, 0); and fd = perf_event_open(&attr, pidof(mysqld), -1, -1, 0);

d) Quick summary of my tests:

i) I can get desired results when listening on specific CPU for all processes, including mysqld

ii) I can barely get anything when listening for specific process[mysqld], whether listening on all CPU/specific CPU

iii) I can see pid == tid when listening for process with pid = 1

Therefore I think it will be natural to make the assumption that the pid here in perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) is actually tid

  • 1
    In Linux, each thread has its own PID, and the PID for the whole process is just the PID/TID of the initial thread. And yeah, the man page for `perf_event_open` says "process/thread" for most of those, so I think you'd need to count events for each thread of a process separately. I assume `mysqld` doesn't do much in its main thread, only in some threads it started later. – Peter Cordes Jan 09 '23 at 04:35
  • Thanks, Peter, and I agree that I may have to listen to all the threads of mysqld. It seems to be kind of heavy to do so, considering that threads are constantly being created/destroyed during mysql's life span. I am thinking of using gdb to backtrace perf to have a peek at how perf calls and uses perf_event_open() – Yuchen Tang Jan 09 '23 at 06:24
  • Yeah, I looked in the man page, and there's something about all processes of a `cgroup`, but I didn't see an explicit mention of all threads of a process. Hopefully there's an easier way to do that. I didn't google on that, but presumably other people have wondered the same thing. – Peter Cordes Jan 09 '23 at 06:34
  • 1
    OK, so I just find this attribute: perf_event_attr.inherit. After enabling this flag, I can collect info of mysqld on a single CPU, but not on all CPUs. If I try to monitor mysqld on all CPUs, mmap will return 0xffff ffff ffff ffff and exit the program, I will try to figure out way and share more. – Yuchen Tang Jan 09 '23 at 07:09

0 Answers0