perf: Speed up thread map generation
PATCH v2 1/1 perf: Speed up thread map generation
When trying to capture perf data on a system running spejbb2013, perf hung for about 15 minutes. This is because it took that long to gather about 10,000 thread maps and process them.
I don't think a user wants to wait that long.
Instead, recognize that thread maps are roughly equivalent to pid maps and just quickly copy those instead.
To do this, I synthesize 'fork' events, this eventually calls thread__fork() and copies the maps over.
The overhead goes from 15 minutes down to about a few seconds.
--
V2: based on Jiri's comments, moved malloc up a level
and made sure the memory was freed
--- tools/perf/util/event.c | 59 ++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 51 insertions(+), 8 deletions(-)
tools/perf/util/event.c +51 -8
@@ -129,6 +129,28 @@ out:return tgid; } +static int perf_event__synthesize_fork (struct perf_tool *tool, +union perf_event *event,pid_t pid, +pid_t tgid,perf_event__handler_t process, +struct machine *machine) +{ +memset (&event->fork,0 ,sizeof (event->fork) + machine->id_hdr_size); + +/* this is really a clone event but we use fork to synthesize it */ + event->fork.ppid = tgid; + event->fork.ptid = tgid; + event->fork.pid = tgid; + event->fork.tid = pid; + event->fork.header.type =PERF_RECORD_FORK ; + + event->fork.header.size = (sizeof (event->fork) + machine->id_hdr_size); + +if (process (tool, event, &synth_sample, machine) !=0 ) +return -1 ; + +return 0 ; +} + int perf_event__synthesize_mmap_events(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid,
@@ -278,6 +300,7 @@ int perf_event__synthesize_modules(struct perf_tool *tool, static int __event__synthesize_thread(union perf_event *comm_event, union perf_event *mmap_event, + union perf_event *fork_event, pid_t pid, int full, perf_event__handler_t process, struct perf_tool *tool,
@@ -326,9 +349,15 @@ static int __event__synthesize_thread(union perf_event *comm_event,if (tgid ==-1 )return -1 ; -/* process the thread's maps too */ - rc =perf_event__synthesize_mmap_events (tool, mmap_event, _pid, tgid, - process, machine, mmap_data); +if (_pid == pid) { +/* process the parent's maps too */ + rc =perf_event__synthesize_mmap_events (tool, mmap_event, pid, tgid, + process, machine, mmap_data); + }else { +/* only fork the tid's map, to save time */ + rc =perf_event__synthesize_fork (tool, fork_event, _pid, tgid, + process, machine); + }if (rc)return rc;
@@ -344,7 +373,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool, struct machine *machine, bool mmap_data) { -union perf_event *comm_event, *mmap_event; +union perf_event *comm_event, *mmap_event, *fork_event;int err =-1 , thread, j; comm_event =malloc (sizeof (comm_event->comm) + machine->id_hdr_size);
@@ -355,9 +384,14 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,if (mmap_event ==NULL ) goto out_free_comm; + fork_event =malloc (sizeof (fork_event->fork) + machine->id_hdr_size); +if (fork_event ==NULL ) + goto out_free_mmap; + err =0 ;for (thread =0 ; thread < threads->nr; ++thread) {if (__event__synthesize_thread (comm_event, mmap_event, + fork_event, threads->map[thread],0 , process, tool, machine, mmap_data)) {
@@ -383,6 +417,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,/* if not, generate events for it */ if (need_leader &&__event__synthesize_thread (comm_event, mmap_event, + fork_event, comm_event->comm.pid,0 , process, tool, machine, mmap_data)) {
@@ -391,6 +426,8 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool, } } } +free (fork_event); +out_free_mmap:free (mmap_event); out_free_comm:free (comm_event);
@@ -405,7 +442,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,DIR *proc;char proc_path[PATH_MAX ];struct dirent dirent, *next; -union perf_event *comm_event, *mmap_event; +union perf_event *comm_event, *mmap_event, *fork_event;int err =-1 ; comm_event =malloc (sizeof (comm_event->comm) + machine->id_hdr_size);
@@ -416,6 +453,10 @@ int perf_event__synthesize_threads(struct perf_tool *tool,if (mmap_event ==NULL ) goto out_free_comm; + fork_event =malloc (sizeof (fork_event->fork) + machine->id_hdr_size); +if (fork_event ==NULL ) + goto out_free_mmap; +if (machine__is_default_guest (machine))return 0 ;
@@ -423,7 +464,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool, proc =opendir (proc_path);if (proc ==NULL ) - goto out_free_mmap; + goto out_free_fork;while (!readdir_r (proc, &dirent, &next) && next) {char *end;
@@ -435,12 +476,14 @@ int perf_event__synthesize_threads(struct perf_tool *tool,* We may race with exiting thread, so don't stop just because * one thread couldn't be synthesized. */ -__event__synthesize_thread (comm_event, mmap_event, pid,1 , - process, tool, machine, mmap_data); +__event__synthesize_thread (comm_event, mmap_event, fork_event, pid, +1 , process, tool, machine, mmap_data); } err =0 ;closedir (proc); +out_free_fork: +free (fork_event); out_free_mmap:free (mmap_event); out_free_comm:
Signature · 1 line
1.7.1
Quoted 12 lines · Don Zickus
When trying to capture perf data on a system running spejbb2013, perf hung for about 15 minutes. This is because it took that long to gather about 10,000 thread maps and process them.
I don't think a user wants to wait that long.
Instead, recognize that thread maps are roughly equivalent to pid maps and just quickly copy those instead.
To do this, I synthesize 'fork' events, this eventually calls
thread__fork()and copies the maps over.
Quoted 6 lines
The overhead goes from 15 minutes down to about a few seconds.
Signed-off-by Don Zickus <[email protected]>--
V2: based on Jiri's comments, moved malloc up a level
and made sure the memory was freed
thanks,
jirka
Namhyung Kim
maintainerHi Don,
Quoted 13 lines · Don Zickus
When trying to capture perf data on a system running spejbb2013, perf hung for about 15 minutes. This is because it took that long to gather about 10,000 thread maps and process them.
I don't think a user wants to wait that long.
Instead, recognize that thread maps are roughly equivalent to pid maps and just quickly copy those instead.
To do this, I synthesize 'fork' events, this eventually calls
thread__fork()and copies the maps over.The overhead goes from 15 minutes down to about a few seconds.
[SNIP]
Review int perf_event__synthesize_threads(struct perf_tool *tool, · line 461
Quoted 10 lines
(quoted diff) +4
-0@@ -416,6 +453,10 @@ int perf_event__synthesize_threads(struct perf_tool *tool, if (mmap_event == NULL) goto out_free_comm; + fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); + if (fork_event == NULL) + goto out_free_mmap; + if (machine__is_default_guest(machine)) return 0;
Not related to this patch, but this will causes a memory leak.. So I think below patch is needed on top (not sure it's also needed for perf_event__synthesize_thread_map() too).
Quoted 1 line
From
3c4b49b4f540c7d49ee78364b02faa0bfce59c67Mon Sep 17 00:00:00 2001
From: Namhyung Kim <[email protected]>
Date: Mon, 17 Mar 2014 10:23:06 +0900
Subject: [PATCH] perf tools: Fix a possible memory leak
Checking default guest machine should be done before allocating event structures otherwise it'll leak memory.
--- tools/perf/util/event.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/event.c +3 -3
@@ -445,6 +445,9 @@ int perf_event__synthesize_threads(struct perf_tool *tool,union perf_event *comm_event, *mmap_event, *fork_event;int err =-1 ; +if (machine__is_default_guest (machine)) +return 0 ; + comm_event =malloc (sizeof (comm_event->comm) + machine->id_hdr_size);if (comm_event ==NULL ) goto out;
@@ -457,9 +460,6 @@ int perf_event__synthesize_threads(struct perf_tool *tool,if (fork_event ==NULL ) goto out_free_mmap; -if (machine__is_default_guest (machine)) -return 0 ; -snprintf (proc_path,sizeof (proc_path),"%s/proc" , machine->root_dir); proc =opendir (proc_path);
Signature · 1 line
1.7.11.7
Em Mon, Mar 17, 2014 at 10:45:49AM +0900, Namhyung Kim escreveu:
Quoted 22 lines
Hi Don,
Quoted 13 lines · Don Zickus
When trying to capture perf data on a system running spejbb2013, perf hung for about 15 minutes. This is because it took that long to gather about 10,000 thread maps and process them.
I don't think a user wants to wait that long.
Instead, recognize that thread maps are roughly equivalent to pid maps and just quickly copy those instead.
To do this, I synthesize 'fork' events, this eventually calls
thread__fork()and copies the maps over.The overhead goes from 15 minutes down to about a few seconds.
[SNIP]
Quoted 4 lines
(quoted diff) +0
-0@@ -416,6 +453,10 @@ int perf_event__synthesize_threads(struct perf_tool *tool, if (mmap_event == NULL) goto out_free_comm;
Quoted 10 lines
Review int perf_event__synthesize_threads(struct perf_tool *tool, · line 461
Quoted 6 lines
(quoted diff) +4
-0+ fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); + if (fork_event == NULL) + goto out_free_mmap; + if (machine__is_default_guest(machine)) return 0;Not related to this patch, but this will causes a memory leak.. So I think below patch is needed on top (not sure it's also needed for
perf_event__synthesize_thread_map()too).
Good catch, applying it, please next time send it under separate cover, i.e. not inside another thread.
- Arnaldo
Quoted 42 lines
Quoted 2 lines
From
3c4b49b4f540c7d49ee78364b02faa0bfce59c67Mon Sep 17 00:00:00 2001From: Namhyung Kim <[email protected]>
Date: Mon, 17 Mar 2014 10:23:06 +0900
Subject: [PATCH] perf tools: Fix a possible memory leakChecking default guest machine should be done before allocating event structures otherwise it'll leak memory.
Cc Don Zickus <[email protected]>Cc Jiri Olsa <[email protected]>Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/event.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)tools/perf/util/event.c +3
-3@@ -445,6 +445,9 @@ int perf_event__synthesize_threads(struct perf_tool *tool,union perf_event *comm_event, *mmap_event, *fork_event;int err =-1 ; +if (machine__is_default_guest (machine)) +return 0 ; + comm_event =malloc (sizeof (comm_event->comm) + machine->id_hdr_size);if (comm_event ==NULL ) goto out;@@ -457,9 +460,6 @@ int perf_event__synthesize_threads(struct perf_tool *tool,if (fork_event ==NULL ) goto out_free_mmap; -if (machine__is_default_guest (machine)) -return 0 ; -snprintf (proc_path,sizeof (proc_path),"%s/proc" , machine->root_dir); proc =opendir (proc_path);Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerQuoted 34 lines · Arnaldo Carvalho de Melo
Em Mon, Mar 17, 2014 at 10:45:49AM +0900, Namhyung Kim escreveu:
Quoted 22 lines
Hi Don,
Quoted 13 lines · Don Zickus
When trying to capture perf data on a system running spejbb2013, perf hung for about 15 minutes. This is because it took that long to gather about 10,000 thread maps and process them.
I don't think a user wants to wait that long.
Instead, recognize that thread maps are roughly equivalent to pid maps and just quickly copy those instead.
To do this, I synthesize 'fork' events, this eventually calls
thread__fork()and copies the maps over.The overhead goes from 15 minutes down to about a few seconds.
[SNIP]
Quoted 4 lines
(quoted diff) +0
-0@@ -416,6 +453,10 @@ int perf_event__synthesize_threads(struct perf_tool *tool, if (mmap_event == NULL) goto out_free_comm;Quoted 10 lines
Review int perf_event__synthesize_threads(struct perf_tool *tool, · line 461
Quoted 6 lines
(quoted diff) +4
-0+ fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); + if (fork_event == NULL) + goto out_free_mmap; + if (machine__is_default_guest(machine)) return 0;Not related to this patch, but this will causes a memory leak.. So I think below patch is needed on top (not sure it's also needed for
perf_event__synthesize_thread_map()too).
Quoted 2 lines
Good catch, applying it, please next time send it under separate cover, i.e. not inside another thread.
Okay. I'll send this kind of patch in a separate thread next time.
Thanks,
Namhyung
tip-bot for Namhyung Kim
merge bot[tip:perf/core] perf tools: Fix memory leak when synthesizing thread records
In reply to Namhyung Kimperf tools: Fix memory leak when synthesizing thread records
Commit-ID: 574799bfdbc3a13e0b5e2f6af34b761bde743a9a Gitweb: http://git.kernel.org/tip/574799bfdbc3a13e0b5e2f6af34b761bde743a9a Author: Namhyung Kim <[email protected]> AuthorDate: Mon, 17 Mar 2014 10:45:49 +0900 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Tue, 18 Mar 2014 18:17:01 -0300
perf tools: Fix memory leak when synthesizing thread records
Checking default guest machine should be done before allocating event structures otherwise it'll leak memory.
--- tools/perf/util/event.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/event.c +3 -3
@@ -446,6 +446,9 @@ int perf_event__synthesize_threads(struct perf_tool *tool,union perf_event *comm_event, *mmap_event, *fork_event;int err =-1 ; +if (machine__is_default_guest (machine)) +return 0 ; + comm_event =malloc (sizeof (comm_event->comm) + machine->id_hdr_size);if (comm_event ==NULL ) goto out;
@@ -458,9 +461,6 @@ int perf_event__synthesize_threads(struct perf_tool *tool,if (fork_event ==NULL ) goto out_free_mmap; -if (machine__is_default_guest (machine)) -return 0 ; -snprintf (proc_path,sizeof (proc_path),"%s/proc" , machine->root_dir); proc =opendir (proc_path);
tip-bot for Don Zickus
merge bot[tip:perf/core] perf tools: Speed up thread map generation
In reply to Don Zickusperf tools: Speed up thread map generation
Commit-ID: 363b785f3805a2632eb09a8b430842461c21a640 Gitweb: http://git.kernel.org/tip/363b785f3805a2632eb09a8b430842461c21a640 Author: Don Zickus <[email protected]> AuthorDate: Fri, 14 Mar 2014 10:43:44 -0400 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 14 Mar 2014 18:08:41 -0300
perf tools: Speed up thread map generation
When trying to capture perf data on a system running spejbb2013, perf hung for about 15 minutes. This is because it took that long to gather about 10,000 thread maps and process them.
I don't think a user wants to wait that long.
Instead, recognize that thread maps are roughly equivalent to pid maps and just quickly copy those instead.
To do this, I synthesize 'fork' events, this eventually calls thread__fork() and copies the maps over.
The overhead goes from 15 minutes down to about a few seconds.
--
V2: based on Jiri's comments, moved malloc up a level
and made sure the memory was freed
--- tools/perf/util/event.c | 59 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 8 deletions(-)
tools/perf/util/event.c +51 -8
@@ -129,6 +129,28 @@ out:return tgid; } +static int perf_event__synthesize_fork (struct perf_tool *tool, +union perf_event *event,pid_t pid, +pid_t tgid,perf_event__handler_t process, +struct machine *machine) +{ +memset (&event->fork,0 ,sizeof (event->fork) + machine->id_hdr_size); + +/* this is really a clone event but we use fork to synthesize it */ + event->fork.ppid = tgid; + event->fork.ptid = tgid; + event->fork.pid = tgid; + event->fork.tid = pid; + event->fork.header.type =PERF_RECORD_FORK ; + + event->fork.header.size = (sizeof (event->fork) + machine->id_hdr_size); + +if (process (tool, event, &synth_sample, machine) !=0 ) +return -1 ; + +return 0 ; +} + int perf_event__synthesize_mmap_events(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid,
@@ -278,6 +300,7 @@ int perf_event__synthesize_modules(struct perf_tool *tool, static int __event__synthesize_thread(union perf_event *comm_event, union perf_event *mmap_event, + union perf_event *fork_event, pid_t pid, int full, perf_event__handler_t process, struct perf_tool *tool,
@@ -326,9 +349,15 @@ static int __event__synthesize_thread(union perf_event *comm_event,if (tgid ==-1 )return -1 ; -/* process the thread's maps too */ - rc =perf_event__synthesize_mmap_events (tool, mmap_event, _pid, tgid, - process, machine, mmap_data); +if (_pid == pid) { +/* process the parent's maps too */ + rc =perf_event__synthesize_mmap_events (tool, mmap_event, pid, tgid, + process, machine, mmap_data); + }else { +/* only fork the tid's map, to save time */ + rc =perf_event__synthesize_fork (tool, fork_event, _pid, tgid, + process, machine); + }if (rc)return rc;
@@ -344,7 +373,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool, struct machine *machine, bool mmap_data) { -union perf_event *comm_event, *mmap_event; +union perf_event *comm_event, *mmap_event, *fork_event;int err =-1 , thread, j; comm_event =malloc (sizeof (comm_event->comm) + machine->id_hdr_size);
@@ -355,9 +384,14 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,if (mmap_event ==NULL ) goto out_free_comm; + fork_event =malloc (sizeof (fork_event->fork) + machine->id_hdr_size); +if (fork_event ==NULL ) + goto out_free_mmap; + err =0 ;for (thread =0 ; thread < threads->nr; ++thread) {if (__event__synthesize_thread (comm_event, mmap_event, + fork_event, threads->map[thread],0 , process, tool, machine, mmap_data)) {
@@ -383,6 +417,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,/* if not, generate events for it */ if (need_leader &&__event__synthesize_thread (comm_event, mmap_event, + fork_event, comm_event->comm.pid,0 , process, tool, machine, mmap_data)) {
@@ -391,6 +426,8 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool, } } } +free (fork_event); +out_free_mmap:free (mmap_event); out_free_comm:free (comm_event);
@@ -405,7 +442,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,DIR *proc;char proc_path[PATH_MAX ];struct dirent dirent, *next; -union perf_event *comm_event, *mmap_event; +union perf_event *comm_event, *mmap_event, *fork_event;int err =-1 ; comm_event =malloc (sizeof (comm_event->comm) + machine->id_hdr_size);
@@ -416,6 +453,10 @@ int perf_event__synthesize_threads(struct perf_tool *tool,if (mmap_event ==NULL ) goto out_free_comm; + fork_event =malloc (sizeof (fork_event->fork) + machine->id_hdr_size); +if (fork_event ==NULL ) + goto out_free_mmap; +if (machine__is_default_guest (machine))return 0 ;
@@ -423,7 +464,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool, proc =opendir (proc_path);if (proc ==NULL ) - goto out_free_mmap; + goto out_free_fork;while (!readdir_r (proc, &dirent, &next) && next) {char *end;
@@ -435,12 +476,14 @@ int perf_event__synthesize_threads(struct perf_tool *tool,* We may race with exiting thread, so don't stop just because * one thread couldn't be synthesized. */ -__event__synthesize_thread (comm_event, mmap_event, pid,1 , - process, tool, machine, mmap_data); +__event__synthesize_thread (comm_event, mmap_event, fork_event, pid, +1 , process, tool, machine, mmap_data); } err =0 ;closedir (proc); +out_free_fork: +free (fork_event); out_free_mmap:free (mmap_event); out_free_comm: