perf: Make the 'perf top -p $pid' can perceive the new forked threads.
v2 cover perf: Make the 'perf top -p $pid' can perceive the new forked threads.
From: [email protected]
This patch set add a function that make the 'perf top -p $pid' is able to perceive the new threads that is forked by target processes. 'perf top{record} -p $pid' can perceive the threads are forked before we execute perf, but it cannot perceive the new threads are forked after we started perf. This is perf's important defect, because the applications who will fork new threads on-the-fly are very much.
For performance reasons, the event inherit mechanism is forbidden while we use per-task counters. Some internal data structures, such as, thread_map, evlist->mmap, evsel->fd, evsel->id, evsel->sample_id are implemented as arrays at the initialization phase. Their size is fixed, and they cannot be extended or shrinked easily while we want to adjust them for new forked threads and exit threads.
So, we have done the following work:
1) Transformed xyarray to linked list.
Implementd the interfaces to extand and shrink a exist xyarray.
The xyarray is a 2-dimensional structure. The row is still a array (because the number of CPU is fixed forever), the columns are linked list.
2) Transformed evlist->mmap, evsel->fd, evsel->id and evsel->sample_id to list with the new xyarray.
Implemented interfaces to expand and shrink these structures.
The nodes in these structures can be referenced by some predefined macros, such as FD(cpu, thread), MMAP(cpu, thread), ID(cpu, thread), etc.
3) Transformed thread_map to linked list.
Implemented the interfaces to extand and shrink a exist thread_map.
4) Added 2 callback functions to top->perf_tool, they are called while the PERF_RECORD_FORK & PERF_RECORD_EXIT events are got.
While a PERF_RECORD_FORK event is got, all related data structures are expanded, a new fd and mmap are opened.
While a PERF_RECORD_EXIT event is got, all nodes in the related data structures are removed, the fd and mmap are closed.
The linked list is flexible, list_add & list_del can be used easily. Additional, performance penalty (especially the CPU utilization) is low.
This function has been already implemented for 'perf top -p $pid' in the patch [4/4] of this patch set. Next step, the 'perf record -p $pid' should be modified with the same method.
Thanks for David Ahern's suggestion.
chenggang (4):
Transform xyarray to linked list.
Transform thread_map to linked list.
Transform mmap and other related structures to list with new xyarray. Add fork and exit callback functions into top->perf_tool.
tools/perf/builtin-record.c | 6 +- tools/perf/builtin-stat.c | 2 +- tools/perf/builtin-top.c | 100 ++++++++++++- tools/perf/tests/open-syscall-tp-fields.c | 2 +- tools/perf/util/event.c | 10 +- tools/perf/util/evlist.c | 171 +++++++++++++++++++--- tools/perf/util/evlist.h | 6 +- tools/perf/util/evsel.c | 98 +++++++++++-- tools/perf/util/evsel.h | 8 +- tools/perf/util/header.c | 31 ++-- tools/perf/util/header.h | 3 +- tools/perf/util/python.c | 2 +- tools/perf/util/thread_map.c | 223 +++++++++++++++++++---------- tools/perf/util/thread_map.h | 16 ++- tools/perf/util/xyarray.c | 85 ++++++++++- tools/perf/util/xyarray.h | 25 +++- 16 files changed, 641 insertions(+), 147 deletions(-)
Signature · 1 line
1.7.9.5
PATCH v2 1/4 Transform xyarray to linked list
From: chenggang <[email protected]>
The 2-dimensional array cannot expand and shrink easily while we want to response the thread's fork and exit events on-the-fly.
We transform xyarray to a 2-demesional linked list. The row is still a array, but column is implemented as a list. The number of nodes in every row are same. The interface to append and shrink a exist xyarray is provided.
1) xyarray__append()
append a column for all rows.
2) xyarray__remove()
remove a column for all rows.
--- tools/perf/util/xyarray.c | 85 +++++++++++++++++++++++++++++++++++++++++---- tools/perf/util/xyarray.h | 25 +++++++++++-- 2 files changed, 101 insertions(+), 9 deletions(-)
tools/perf/util/xyarray.c +79 -6
@@ -1,20 +1,93 @@#include "xyarray.h" #include "util.h" -struct xyarray *xyarray__new (int xlen,int ylen,size_t entry_size) +/* +* Add a column for all rows; +*/ +int xyarray__append (struct xyarray *xy) { -size_t row_size = ylen * entry_size; -struct xyarray *xy =zalloc (sizeof (*xy) + xlen * row_size); +struct xyentry *new_entry; +unsigned int x; + +for (x =0 ; x < xy->row_count; x++) { + new_entry =zalloc (sizeof (*new_entry)); +if (new_entry ==NULL ) +return -1 ; + + new_entry->contents =zalloc (xy->entry_size); +if (new_entry->contents ==NULL ) +return -1 ; -if (xy !=NULL ) { - xy->entry_size = entry_size; - xy->row_size = row_size; +list_add_tail (&new_entry->next, &xy->rows[x].head); } +return 0 ; +} + +struct xyarray *xyarray__new (int xlen,int ylen,size_t entry_size) +{ +struct xyarray *xy =zalloc (sizeof (*xy) + xlen *sizeof (struct row )); +int i; + +if (xy ==NULL ) +return NULL ; + + xy->row_count = xlen; + xy->entry_size = entry_size; + +for (i =0 ; i < xlen; i++) +INIT_LIST_HEAD (&xy->rows[i].head); + +for (i =0 ; i < ylen; i++) +if (xyarray__append (xy) <0 ) { +xyarray__delete (xy); +return NULL ; + } +return xy; } +/* +* remove a column for all rows; +*/ +int xyarray__remove (struct xyarray *xy,int y) +{ +struct xyentry *entry; +unsigned int x; +int count; + +if (!xy) +return 0 ; + +for (x =0 ; x < xy->row_count; x++) { + count =0 ; +list_for_each_entry (entry, &xy->rows[x].head, next) +if (count++ == y) { +list_del (&entry->next); +free (entry); +return 0 ; + } + } + +return -1 ; +} + +/* +* All nodes in every rows should be deleted before delete @xy. +*/ void xyarray__delete (struct xyarray *xy) { +unsigned int i; +struct xyentry *entry; + +if (!xy) +return ; + +for (i =0 ; i < xy->row_count; i++) +list_for_each_entry (entry, &xy->rows[i].head, next) { +list_del (&entry->next); +free (entry); + } +free (xy); }
tools/perf/util/xyarray.h +22 -3
@@ -2,19 +2,38 @@#define _PERF_XYARRAY_H_ 1#include <sys/types.h> +#include <linux/list.h> + +struct row { +struct list_head head; +}; + +struct xyentry { +struct list_head next; +char *contents; +};struct xyarray { -size_t row_size; +size_t row_count;size_t entry_size; -char contents[]; +struct row rows[]; };struct xyarray *xyarray__new (int xlen,int ylen,size_t entry_size);void xyarray__delete (struct xyarray *xy); +int xyarray__append (struct xyarray *xy); +int xyarray__remove (struct xyarray *xy,int y);static inline void *xyarray__entry (struct xyarray *xy,int x,int y) { -return &xy->contents[x * xy->row_size + y * xy->entry_size]; +struct xyentry *entry; +int columns =0 ; + +list_for_each_entry (entry, &xy->rows[x].head, next) +if (columns++ == y) +return entry->contents; + +return NULL ; }#endif /* _PERF_XYARRAY_H_ */
Signature · 1 line
1.7.9.5
Re: [PATCH v2 1/4] Transform xyarray to linked list
Quoted 4 lines · chenggang
From: chenggang <[email protected]>
The 2-dimensional array cannot expand and shrink easily while we want to response the thread's fork and exit events on-the-fly.
perhaps a better explanation of why -- file descriptors for perf events are kept in an xyarray where the x dimension is cpus and the y dimension is threads of interest. You want to be able to grow and shrink the y dimension as monitored processes spawn and terminate threads.
Quoted 42 lines
We transform xyarray to a 2-demesional linked list. The row is still a array, but column is implemented as a list. The number of nodes in every row are same. The interface to append and shrink a exist xyarray is provided.
1)xyarray__append()
append a column for all rows.
2)xyarray__remove()
remove a column for all rows.Cc David Ahern <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Paul Mackerras <[email protected]>Cc Ingo Molnar <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Arjan van de Ven <[email protected]>Cc Namhyung Kim <[email protected]>Cc Yanmin Zhang <[email protected]>Cc Wu Fengguang <[email protected]>Cc Mike Galbraith <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Chenggang Qin <[email protected]>--- tools/perf/util/xyarray.c | 85 +++++++++++++++++++++++++++++++++++++++++---- tools/perf/util/xyarray.h | 25 +++++++++++-- 2 files changed, 101 insertions(+), 9 deletions(-)tools/perf/util/xyarray.c +4
-3@@ -1,20 +1,93 @@#include "xyarray.h" #include "util.h" -struct xyarray *xyarray__new (int xlen,int ylen,size_t entry_size) +/* +* Add a column for all rows; +*/ +int xyarray__append (struct xyarray *xy) { -size_t row_size = ylen * entry_size; -struct xyarray *xy =zalloc (sizeof (*xy) + xlen * row_size);
tools/perf/util/xyarray.c line 18
Quoted 10 lines
tools/perf/util/xyarray.c +10
-0+struct xyentry *new_entry; +unsigned int x; + +for (x =0 ; x < xy->row_count; x++) { + new_entry =zalloc (sizeof (*new_entry)); +if (new_entry ==NULL ) +return -1 ; + + new_entry->contents =zalloc (xy->entry_size); +if (new_entry->contents ==NULL )
free new_entry on this error path
Quoted 44 lines
tools/perf/util/xyarray.c +35
-3+return -1 ; -if (xy !=NULL ) { - xy->entry_size = entry_size; - xy->row_size = row_size; +list_add_tail (&new_entry->next, &xy->rows[x].head); } +return 0 ; +} + +struct xyarray *xyarray__new (int xlen,int ylen,size_t entry_size) +{ +struct xyarray *xy =zalloc (sizeof (*xy) + xlen *sizeof (struct row )); +int i; + +if (xy ==NULL ) +return NULL ; + + xy->row_count = xlen; + xy->entry_size = entry_size; + +for (i =0 ; i < xlen; i++) +INIT_LIST_HEAD (&xy->rows[i].head); + +for (i =0 ; i < ylen; i++) +if (xyarray__append (xy) <0 ) { +xyarray__delete (xy); +return NULL ; + } +return xy; } +/* +* remove a column for all rows; +*/ +int xyarray__remove (struct xyarray *xy,int y) +{ +struct xyentry *entry; +unsigned int x; +int count; + +if (!xy)
tools/perf/util/xyarray.c line 69
Quoted 10 lines
tools/perf/util/xyarray.c +10
-0+return 0 ; + +for (x =0 ; x < xy->row_count; x++) { + count =0 ; +list_for_each_entry (entry, &xy->rows[x].head, next) +if (count++ == y) { +list_del (&entry->next); +free (entry); +return 0 ; + }
braces are needed around the list_for_each block.
Quoted 55 lines
tools/perf/util/xyarray.c +20
-0+ } + +return -1 ; +} + +/* +* All nodes in every rows should be deleted before delete @xy. +*/ void xyarray__delete (struct xyarray *xy) { +unsigned int i; +struct xyentry *entry; + +if (!xy) +return ; + +for (i =0 ; i < xy->row_count; i++) +list_for_each_entry (entry, &xy->rows[i].head, next) { +list_del (&entry->next); +free (entry); + } +free (xy); }tools/perf/util/xyarray.h +14
-2@@ -2,19 +2,38 @@#define _PERF_XYARRAY_H_ 1#include <sys/types.h> +#include <linux/list.h> + +struct row { +struct list_head head; +}; + +struct xyentry { +struct list_head next; +char *contents; +};struct xyarray { -size_t row_size; +size_t row_count;size_t entry_size; -char contents[]; +struct row rows[]; };struct xyarray *xyarray__new (int xlen,int ylen,size_t entry_size);void xyarray__delete (struct xyarray *xy); +int xyarray__append (struct xyarray *xy); +int xyarray__remove (struct xyarray *xy,int y);
tools/perf/util/xyarray.h line 34
Quoted 10 lines
tools/perf/util/xyarray.h +6
-1static inline void *xyarray__entry (struct xyarray *xy,int x,int y) { -return &xy->contents[x * xy->row_size + y * xy->entry_size]; +struct xyentry *entry; +int columns =0 ; + +list_for_each_entry (entry, &xy->rows[x].head, next) +if (columns++ == y) +return entry->contents;
braces needed around multi-statement block
I'll review next patches as I get time.
David
Quoted 6 lines
tools/perf/util/xyarray.h +2
-0+ + return NULL; }#endif /* _PERF_XYARRAY_H_ */
PATCH v2 2/4 Transform thread_map to linked list
In reply to chenggangFrom: chenggang <[email protected]>
The size of thread_map is fixed at initialized phase according to the files in /proc/{$pid}. It cannot be expanded and shrinked easily while we want to response the thread fork and exit events.
We transform the thread_map structure to a linked list, and implement some interfaces to expend and shrink it. In order to improve compatibility with the existing code, we can get a thread by its index in the thread_map also.
1) thread_map__append()
Append a new thread into thread_map according to new thread's pid.
2) thread_map__remove()
Remove a exist thread from thread_map according to the index of the thread in thread_map.
3) thread_map__init()
Allocate a thread_map, and initialize it. But the thread_map is empty after we called this function. We should call thread_map__append() to insert threads.
4) thread_map__delete()
Delete a exist thread_map.
5) thread_map__get_pid()
Got a thread's pid by its index in the thread_map.
6) thread_map__get_idx_by_pid()
Got a thread's index in the thread_map according to its pid.
While we got a PERF_RECORD_EXIT event, we only know the pid of the exited thread.
7) thread_map__empty_thread_map()
Return a empty thread_map, there is only a dumb thread in it.
This function is used to instead of the global varible empty_thread_map.
--- tools/perf/builtin-stat.c | 2 +- tools/perf/tests/open-syscall-tp-fields.c | 2 +- tools/perf/util/event.c | 10 +- tools/perf/util/evlist.c | 2 +- tools/perf/util/evsel.c | 16 +-- tools/perf/util/python.c | 2 +- tools/perf/util/thread_map.c | 210 +++++++++++++++++++---------- tools/perf/util/thread_map.h | 19 ++- tools/perf/util/xyarray.c | 4 +- 9 files changed, 171 insertions(+), 96 deletions(-)
tools/perf/builtin-stat.c +1 -1
@@ -401,7 +401,7 @@ static int __run_perf_stat(int argc __maybe_unused, const char **argv) }if (perf_target__none (&target)) - evsel_list->threads->map[0 ] = child_pid; +thread_map__append (evsel_list->threads, child_pid);/* * Wait for the child to be ready to exec.
tools/perf/tests/open-syscall-tp-fields.c +1 -1
@@ -43,7 +43,7 @@ int test__syscall_open_tp_fields(void)perf_evsel__config (evsel, &opts); - evlist->threads->map[0 ] =getpid (); +thread_map__append (evlist->threads,getpid ()); err =perf_evlist__open (evlist);if (err <0 ) {
tools/perf/util/event.c +3 -2
@@ -327,8 +327,8 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool, err =0 ;for (thread =0 ; thread < threads->nr; ++thread) {if (__event__synthesize_thread (comm_event, mmap_event, - threads->map[thread],0 , - process, tool, machine)) { +thread_map__get_pid (threads, + thread),0 , process, tool, + machine)) { err =-1 ;break ;
}
@@ -337,12 +337,14 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
* comm.pid is set to thread group id by
* perf_event__synthesize_comm
*/
- if ((int) comm_event->comm.pid != threads->map[thread]) {
+ if ((int) comm_event->comm.pid
+ != thread_map__get_pid(threads, thread)) {
bool need_leader = true;
/* is thread group leader in thread_map? */ for (j = 0; j < threads->nr; ++j) {
- if ((int) comm_event->comm.pid == threads->map[j]) {
+ if ((int) comm_event->comm.pid
+ == thread_map__get_pid(threads, thread)) {
need_leader = false; break;
}
tools/perf/util/evlist.c +1 -1
@@ -793,7 +793,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, }if (perf_target__none (&opts->target)) - evlist->threads->map[0 ] = evlist->workload.pid; +thread_map__append (evlist->threads, evlist->workload.pid);close (child_ready_pipe[1 ]);close (go_pipe[0 ]);
tools/perf/util/evsel.c +5 -11
@@ -835,7 +835,7 @@ retry_sample_id:int group_fd;if (!evsel->cgrp) - pid = threads->map[thread]; + pid =thread_map__get_pid (threads, thread); group_fd =get_group_fd (evsel, cpu, thread);
@@ -894,14 +894,6 @@ static struct { .cpus = { -1, }, }; -static struct { -struct thread_map map; -int threads[1 ]; -} empty_thread_map = { - .map.nr =1 , - .threads = {-1 , }, -}; -int perf_evsel__open (struct perf_evsel *evsel,struct cpu_map *cpus,struct thread_map *threads) {
@@ -911,7 +903,7 @@ int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, }if (threads ==NULL ) - threads = &empty_thread_map.map; + threads =thread_map__empty_thread_map ();return __perf_evsel__open (evsel, cpus, threads); }
@@ -919,7 +911,9 @@ int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,int perf_evsel__open_per_cpu (struct perf_evsel *evsel,struct cpu_map *cpus) { -return __perf_evsel__open (evsel, cpus, &empty_thread_map.map); +struct thread_map *empty_thread_map =thread_map__empty_thread_map (); + +return __perf_evsel__open (evsel, cpus, empty_thread_map); } int perf_evsel__open_per_thread(struct perf_evsel *evsel,
tools/perf/util/python.c +1 -1
@@ -458,7 +458,7 @@ static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)if (i >= pthreads->threads->nr)return NULL ; -return Py_BuildValue ("i" , pthreads->threads->map[i]); +return Py_BuildValue ("i" ,thread_map__get_pid (pthreads->threads, i)); }static PySequenceMethods pyrf_thread_map__sequence_methods = {
tools/perf/util/thread_map.c +136 -74
344 lines not on this page · raw message
tools/perf/util/thread_map.h +15 -1
@@ -3,20 +3,37 @@#include <sys/types.h> #include <stdio.h> +#include <linux/list.h> +#include "xyarray.h" + +struct thread_pid { +struct list_head next; +pid_t pid; +};struct thread_map {int nr; -pid_t map[]; +struct list_head head; }; +struct thread_map *thread_map__init (void );struct thread_map *thread_map__new_by_pid (pid_t pid);struct thread_map *thread_map__new_by_tid (pid_t tid);struct thread_map *thread_map__new_by_uid (uid_t uid);struct thread_map *thread_map__new (pid_t pid,pid_t tid,uid_t uid); +struct thread_map *thread_map__empty_thread_map (void );struct thread_map *thread_map__new_str (const char *pid,const char *tid,uid_t uid); +int thread_map__append (struct thread_map *threads,pid_t pid); +int thread_map__remove (struct thread_map *threads,int idx); +int thread_map__get_pid (struct thread_map *threads,int index); +int thread_map__get_idx_by_pid (struct thread_map *threads,pid_t pid); +void thread_map__delete (struct thread_map *threads);size_t thread_map__fprintf (struct thread_map *threads,FILE *fp);
tools/perf/util/xyarray.c +2 -2
@@ -78,13 +78,13 @@ int xyarray__remove(struct xyarray *xy, int y)void xyarray__delete (struct xyarray *xy) {unsigned int i; -struct xyentry *entry; +struct xyentry *entry, *tmp;if (!xy)return ;for (i =0 ; i < xy->row_count; i++) -list_for_each_entry (entry, &xy->rows[i].head, next) { +list_for_each_entry_safe (entry, tmp, &xy->rows[i].head, next) {list_del (&entry->next);free (entry); }
Signature · 1 line
1.7.9.5
PATCH v2 3/4 Transform mmap and other related structures to list with new xyarray
In reply to chenggangFrom: chenggang <[email protected]>
evlist->mmap, evsel->id, evsel->sample_id are arrays. They cannot be expended or shrinked easily for the forked and exited threads while we get the fork and exit events.
We transfromed them to linked list with the new xyarray.
xyarray is a 2-dimensional structure. The row is a array still, and a row represents a cpu. The column is a linked list, and a column represents a thread.
Some functions are implemented to expand and shrink the mmap, id and sample_id too.
1) perf_evsel__append_id_thread()
Append a id for a evsel while a new thread is perceived.
2) perf_evsel__append_fd_thread()
Append a fd for a evsel while a new thread is perceived.
3) perf_evlist__append_mmap_thread()
Append a new node into evlist->mmap while a new thread is perceived.
3) perf_evsel__open_thread()
Open the fd for the new thread with sys_perf_event_open.
4) perf_evsel__close_thread()
Close the fd while a thread exit.
5) perf_evlist__mmap_thread()
mmap a new thread's fd.
6) perf_evlist__munmap_thread()
unmmap a exit thread's fd.
The following macros can be used to reference a special fd, id, mmap, sample_id etc.
1) FD(cpu, thread)
2) SID(cpu, thread)
3) ID(cpu, thread)
4) MMAP(cpu, thread)
evlist->pollfd is the parameter of syscall poll(), it must be a array. But we implement a function (perf_evlist__append_pollfd_thread) to expand and shrink it.
--- tools/perf/builtin-record.c | 6 +- tools/perf/util/evlist.c | 169 ++++++++++++++++++++++++++++++++++++++----- tools/perf/util/evlist.h | 6 +- tools/perf/util/evsel.c | 83 ++++++++++++++++++++- tools/perf/util/evsel.h | 8 +- tools/perf/util/header.c | 31 ++++---- tools/perf/util/header.h | 3 +- 7 files changed, 263 insertions(+), 43 deletions(-)
tools/perf/builtin-record.c +4 -2
@@ -31,6 +31,8 @@#include <sched.h> #include <sys/mman.h> +#define MMAP (e, y) (*(struct perf_mmap *)xyarray__entry(e->mmap, 0, y)) +#ifndef HAVE_ON_EXIT #ifndef ATEXIT_MAX #define ATEXIT_MAX 32
@@ -367,8 +369,8 @@ static int perf_record__mmap_read_all(struct perf_record *rec)int rc =0 ;for (i =0 ; i < rec->evlist->nr_mmaps; i++) { -if (rec->evlist->mmap[i].base) { -if (perf_record__mmap_read (rec, &rec->evlist->mmap[i]) !=0 ) { +if (MMAP (rec->evlist, i).base) { +if (perf_record__mmap_read (rec, &MMAP (rec->evlist, i)) !=0 ) { rc =-1 ; goto out; }
tools/perf/util/evlist.c +149 -20
248 lines not on this page · raw message
tools/perf/util/evlist.h +5 -1
@@ -7,6 +7,7 @@#include "event.h" #include "evsel.h" #include "util.h" +#include "xyarray.h" #include <unistd.h> struct pollfd ;
@@ -37,7 +38,7 @@ struct perf_evlist {pid_t pid; } workload;bool overwrite; -struct perf_mmap *mmap; +struct xyarray *mmap;struct pollfd *pollfd;struct thread_map *threads;struct cpu_map *cpus;
@@ -131,6 +132,9 @@ void perf_evlist__splice_list_tail(struct perf_evlist *evlist,struct list_head *list, int nr_entries); +int perf_evlist__mmap_thread (struct perf_evlist *evlist,bool overwrite,int tidx); +void perf_evlist__munmap_thread (struct perf_evlist *evlist,int tidx); +static inline struct perf_evsel *perf_evlist__first (struct perf_evlist *evlist) { return list_entry(evlist->entries.next, struct perf_evsel, node);
tools/perf/util/evsel.c +80 -3
119 lines not on this page · raw message
tools/perf/util/evsel.h +7 -1
@@ -51,7 +51,7 @@ struct perf_evsel {char *filter;struct xyarray *fd;struct xyarray *sample_id; -u64 *id; +struct xyarray *id;struct perf_counts *counts;struct perf_counts *prev_raw_counts;int idx;
@@ -271,6 +271,12 @@ static inline int perf_evsel__group_idx(struct perf_evsel *evsel)return evsel->idx - evsel->leader->idx; } +int perf_evsel__append_id_thread (struct perf_evsel *evsel,int tidx); +int perf_evsel__append_fd_thread (struct perf_evsel *evsel,int tidx); +void perf_evsel__close_thread (struct perf_evsel *evsel,int cpu_nr,int tidx); +int perf_evsel__open_thread (struct perf_evsel *evsel,struct cpu_map *cpus, +struct thread_map *threads,int tidx); +#define for_each_group_member (_evsel, _leader) \ for ((_evsel) = list_entry((_leader)->node.next, struct perf_evsel, node); \ (_evsel) && (_evsel)->leader == (_leader); \
tools/perf/util/header.c +16 -15
@@ -25,6 +25,8 @@#include "strbuf.h" #include "build-id.h" +#define ID (e, y) (*(u64 *)xyarray__entry(e->id, 0, y)) +static bool no_buildid_cache = false;static int trace_event_count;
@@ -1260,7 +1262,6 @@ static struct perf_evsel * read_event_desc(struct perf_header *ph, int fd) {struct perf_evsel *evsel, *events =NULL ; -u64 *id;void *buf =NULL ;u32 nre, sz, nr, i, j;ssize_t ret;
@@ -1325,19 +1326,17 @@ read_event_desc(struct perf_header *ph, int fd)if (!nr)continue ; - id =calloc (nr,sizeof (*id)); -if (!id) - goto error; evsel->ids = nr; - evsel->id = id; + evsel->id =xyarray__new (1 , nr,sizeof (u64)); +if (!evsel->id) + goto error;for (j =0 ; j < nr; j++) { - ret =readn (fd, id,sizeof (*id)); -if (ret != (ssize_t )sizeof (*id)) + ret =readn (fd, &ID (evsel, j),sizeof (u64)); +if (ret != (ssize_t )sizeof (u64)) goto error;if (ph->needs_swap) - *id =bswap_64 (*id); - id++; +ID (evsel, j) =bswap_64 (ID (evsel, j)); } } out:
@@ -1355,7 +1354,6 @@ static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) {struct perf_evsel *evsel, *events =read_event_desc (ph, fd);u32 j; -u64 *id;if (!events) {fprintf (fp,"# event desc: not available or unable to read\n" );
@@ -1384,10 +1382,10 @@ static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)if (evsel->ids) {fprintf (fp,", id = {" ); -for (j =0 , id = evsel->id; j < evsel->ids; j++, id++) { +for (j =0 ; j < evsel->ids; j++) {if (j)fputc (',' , fp); -fprintf (fp," %" PRIu64, *id); +fprintf (fp," %" PRIu64,ID (evsel, j)); }fprintf (fp," }" ); }
@@ -2880,12 +2878,14 @@ out_delete_evlist: }int perf_event__synthesize_attr (struct perf_tool *tool, -struct perf_event_attr *attr,u32 ids,u64 *id, +struct perf_event_attr *attr,u32 ids, +struct perf_evsel *evsel,perf_event__handler_t process) {union perf_event *ev;size_t size;int err; +u32 i; size =sizeof (struct perf_event_attr ); size =PERF_ALIGN (size,sizeof (u64));
@@ -2898,7 +2898,8 @@ int perf_event__synthesize_attr(struct perf_tool *tool,return -ENOMEM ; ev->attr.attr = *attr; -memcpy (ev->attr.id, id, ids *sizeof (u64)); +for (i =0 ; i < ids; i++) + ev->attr.id[i] =ID (evsel, i); ev->attr.header.type =PERF_RECORD_HEADER_ATTR ; ev->attr.header.size = (u16 )size;
@@ -2922,7 +2923,7 @@ int perf_event__synthesize_attrs(struct perf_tool *tool,list_for_each_entry (evsel, &session->evlist->entries, node) { err =perf_event__synthesize_attr (tool, &evsel->attr, evsel->ids, - evsel->id, process); + evsel, process);if (err) {pr_debug ("failed to create perf header attribute\n" );return err;
tools/perf/util/header.h +2 -1
@@ -126,7 +126,8 @@ int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,int build_id_cache__remove_s (const char *sbuild_id,const char *debugdir);int perf_event__synthesize_attr (struct perf_tool *tool, -struct perf_event_attr *attr,u32 ids,u64 *id, +struct perf_event_attr *attr,u32 ids, +struct perf_evsel *evsel,perf_event__handler_t process); int perf_event__synthesize_attrs(struct perf_tool *tool, struct perf_session *session,
Signature · 1 line
1.7.9.5
PATCH v2 4/4 Add fork and exit callback functions into top->perf_tool
In reply to chenggangFrom: chenggang <[email protected]>
Many applications will fork threads on-the-fly, these threads could exit before the main thread exit. The perf top tool should perceive the new forked threads while we profile a special application.
If the target process fork a thread or a thread exit, we will get a PERF_RECORD_FORK
or PERF_RECORD_EXIT events. The following callback functions can process these events.
1) perf_top__process_event_fork()
Open a new fd for the new forked, and expend the related data structures.
2) perf_top__process_event_exit()
Close the fd of exit threadsd, and destroy the nodes in the related data structures.
--- tools/perf/builtin-top.c | 100 +++++++++++++++++++++++++++++++++++++++++- tools/perf/util/evlist.c | 30 ++++++------- tools/perf/util/evsel.c | 13 +++--- tools/perf/util/thread_map.c | 13 ++++++ tools/perf/util/thread_map.h | 3 -- 5 files changed, 133 insertions(+), 26 deletions(-)
tools/perf/builtin-top.c +99 -1
@@ -806,7 +806,7 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx)struct perf_evsel *evsel;struct perf_session *session = top->session;union perf_event *event; -struct machine *machine; +struct machine *machine =NULL ;u8 origin;int ret;
@@ -825,6 +825,20 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx)if (event->header.type ==PERF_RECORD_SAMPLE ) ++top->samples; +if (cpu_map__all (top->evlist->cpus) && + event->header.type ==PERF_RECORD_FORK ) + (&top->tool)->fork (&top->tool, event, &sample, machine); + +if (cpu_map__all (top->evlist->cpus) && + event->header.type ==PERF_RECORD_EXIT ) { +int tidx; + + tidx = (&top->tool)->exit (&top->tool, event, + &sample, machine); +if (tidx == idx) +break ; + } +switch (origin) {case PERF_RECORD_MISC_USER : ++top->us_samples;
@@ -1024,11 +1038,95 @@ parse_callchain_opt(const struct option *opt, const char *arg, int unset)return record_parse_callchain_opt (opt, arg, unset); } +static int perf_top__append_thread (struct perf_top *top,int tidx) +{ +struct perf_evsel *counter; +struct perf_evlist *evlist = top->evlist; +struct cpu_map *cpus = evlist->cpus; + +list_for_each_entry (counter, &evlist->entries, node) +if (perf_evsel__open_thread (counter, cpus, evlist->threads, tidx) <0 ) { +printf ("errno: %d\n" , errno); +return -1 ; + } + +if (perf_evlist__mmap_thread (evlist, false, tidx) <0 ) +return -1 ; + +return 0 ; +} + +static int perf_top__process_event_fork (struct perf_tool *tool__maybe_unused , +union perf_event *event__maybe_unused , +struct perf_sample *sample__maybe_unused , +struct machine *machine__maybe_unused ) +{ +pid_t tid = event->fork.tid; +pid_t ptid = event->fork.ptid; + struct perf_top *top = container_of(tool, struct perf_top, tool); +struct thread_map *threads = top->evlist->threads; +struct perf_evsel *evsel; +int i, ret; + +if (!cpu_map__all (top->evlist->cpus)) +return -1 ; + + ret =thread_map__append (threads, tid); +if (ret ==1 ) +return ret; +if (ret ==-1 ) +return ret; + +for (i =0 ; i < threads->nr; i++) { +if (ptid ==thread_map__get_pid (threads, i)) { +if (perf_top__append_thread (top, threads->nr -1 ) <0 ) + goto free_new_thread; +break ; + } + } + +return 0 ; + +free_new_thread: +list_for_each_entry (evsel, &top->evlist->entries, node) +perf_evsel__close_thread (evsel, top->evlist->cpus->nr, threads->nr -1 ); +thread_map__remove (threads, threads->nr -1 ); +return -1 ; +} + +static int perf_top__process_event_exit (struct perf_tool *tool__maybe_unused , +union perf_event *event__maybe_unused , +struct perf_sample *sample__maybe_unused , +struct machine *machine__maybe_unused ) +{ +pid_t tid = event->fork.tid; + struct perf_top *top = container_of(tool, struct perf_top, tool); +struct perf_evsel *evsel; +struct thread_map *threads = top->evlist->threads; +int tidx =thread_map__get_idx_by_pid (threads, tid); + +if (!cpu_map__all (top->evlist->cpus) || tidx <0 )//ignore +return -1 ; + +perf_evlist__munmap_thread (top->evlist, tidx); + +list_for_each_entry (evsel, &top->evlist->entries, node) +perf_evsel__close_thread (evsel, top->evlist->cpus->nr, tidx); + +thread_map__remove (threads, tidx); + +return tidx; +} +int cmd_top (int argc,const char **argv,const char *prefix__maybe_unused ) {int status;char errbuf[BUFSIZ ];struct perf_top top = { + .tool = { + .fork = perf_top__process_event_fork, + .exit = perf_top__process_event_exit, + }, .count_filter =5 , .delay_secs =2 , .record_opts = {
tools/perf/util/evlist.c +15 -15
@@ -264,24 +264,24 @@ void perf_evlist__enable(struct perf_evlist *evlist)*/ static int perf_evlist__append_pollfd_thread (struct perf_evlist *evlist) { -int new_nfds; +int new_nfds; -if (cpu_map__all (evlist->cpus)) { -struct pollfd *pfd; +if (cpu_map__all (evlist->cpus)) { +struct pollfd *pfd; - new_nfds = evlist->threads->nr * evlist->nr_entries; - pfd =zalloc (sizeof (struct pollfd ) * new_nfds);//FIXME + new_nfds = evlist->threads->nr * evlist->nr_entries; + pfd =zalloc (sizeof (struct pollfd ) * new_nfds); -if (!pfd) -return -1 ; +if (!pfd) +return -1 ; -memcpy (pfd, evlist->pollfd, (evlist->threads->nr -1 ) * evlist->nr_entries); +memcpy (pfd, evlist->pollfd, (evlist->threads->nr -1 ) * evlist->nr_entries); - evlist->pollfd = pfd; -return 0 ; - } + evlist->pollfd = pfd; +return 0 ; + } -return 1 ; +return 1 ; }static int perf_evlist__alloc_pollfd (struct perf_evlist *evlist)
@@ -448,7 +448,7 @@ static int perf_evlist__append_mmap_thread(struct perf_evlist *evlist)return -1 ; evlist->nr_mmaps++; -return 1 ; +return 0 ; }static int perf_evlist__alloc_mmap (struct perf_evlist *evlist)
@@ -573,8 +573,7 @@ int perf_evlist__mmap_thread(struct perf_evlist *evlist, bool overwrite, int tid goto free_append_mmap;list_for_each_entry (evsel, &evlist->entries, node) -if ((evsel->attr.read_format &PERF_FORMAT_ID ) && - evsel->sample_id ==NULL ) +if (evsel->attr.read_format &PERF_FORMAT_ID )if (perf_evsel__append_id_thread (evsel, tidx) <0 ) goto free_append_pollfd;
@@ -633,6 +632,7 @@ void perf_evlist__munmap_thread(struct perf_evlist *evlist, int tidx)list_for_each_entry (evsel, &evlist->entries, node) {xyarray__remove (evsel->id, tidx); + evsel->ids--;xyarray__remove (evsel->sample_id, tidx); }
tools/perf/util/evsel.c +6 -7
@@ -851,10 +851,9 @@ int perf_evsel__open_thread(struct perf_evsel *evsel, struct cpu_map *cpus,int cpu;int pid =-1 ;unsigned long flags =0 ; -int err;if (perf_evsel__append_fd (evsel, tidx) <0 ) -return 1 ; +return -1 ;if (evsel->cgrp) { flags =PERF_FLAG_PID_CGROUP ;
@@ -868,15 +867,15 @@ int perf_evsel__open_thread(struct perf_evsel *evsel, struct cpu_map *cpus, pid = tid; group_fd =get_group_fd (evsel, cpu, tidx); + evsel->attr.disabled =0 ;FD (evsel, cpu, tidx) =sys_perf_event_open (&evsel->attr, pid, cpus->map[cpu], group_fd, flags); -if (FD (evsel, cpu, tidx) <0 ) { -printf ("error: cannot open counter for: %d\n" , tid); - err = -errno; -printf ("errno: %d\n" , errno); -return err; +if (FD (evsel, cpu, tidx) <0 ) { +pr_warning ("error: cannot open counter for: %d\n" , tid); +pr_warning ("errno: %d\n" , errno); +return -errno; } }
tools/perf/util/thread_map.c +13 -0
@@ -322,6 +322,19 @@ struct thread_map *thread_map__new_str(const char *pid, const char *tid,return thread_map__new_by_tid_str (tid); } +int thread_map__get_idx_by_pid (struct thread_map *threads,pid_t pid) +{ +struct thread_pid *tp; +int count =0 ; + +list_for_each_entry (tp, &threads->head, next) { +if (tp->pid == pid) +return count; + count++; + } +return -1 ; +} +struct thread_map *thread_map__empty_thread_map (void ) {struct thread_map *empty_thread_map =NULL ;
tools/perf/util/thread_map.h +0 -3
@@ -27,10 +27,7 @@ struct thread_map *thread_map__new_str(const char *pid, const char *tid, uid_t uid);int thread_map__append (struct thread_map *threads,pid_t pid); -int thread_map__remove_by_pid (struct thread_map *threads,pid_t pid);int thread_map__remove (struct thread_map *threads,int idx); -int thread_map__set_xy_pid (struct xyarray *xy,struct thread_map *threads); -int thread_map__set_pid (struct thread_map *threads,int index,pid_t pid);int thread_map__get_pid (struct thread_map *threads,int index);int thread_map__get_idx_by_pid (struct thread_map *threads,pid_t pid);
Signature · 1 line
1.7.9.5