perf top optimization
cover perf top optimization
From: Kan Liang <[email protected]>
The patch series intends to fix the severe performance issue in Knights Landing/Mill, when monitoring in heavy load system. perf top costs a few minutes to show the result, which is unacceptable.
With the patch series applied, the latency will reduces to several seconds.
machine__synthesize_threads and perf_top__mmap_read costs most of the perf top time (> 99%).
Patch 1-9 do the optimization for machine__synthesize_threads. Patch 10 does the optimization for perf_top__mmap_read.
Optimization for machine__synthesize_threads
- Multithreading the whole process.
- The threads number is set to the max online CPU# by default. User can change the threads number through the new option.
- Introduces hashtable for machine threads to reduce the lock contention.
- The optimization can also benefit other platforms and other perf tools, like perf record. But this patch series doesn't do the optimization for other tools. It can be done later separately.
- With this optimization applied, there is a 1.56x speedup in Knights Mill with heavy workload.
Optimization for perf_top__mmap_read
- switch back to overwrite mode
For non overwrite mode, it tries to read everything in the ring buffer and does not check the messup. Once there are lots of samples delivered shortly, the processing time could be very long. Considering the real time requirement for perf top, it should switch back to overwrite mode.
- With this optimization applied, there is a huge 53.8x speedup in Knights Mill with heavy workload.
- With this optimization applied, the latency of perf_top__mmap_read is less than the default perf top fresh time (2s) in Knights Mill with heavy workload.
Here are the perf top latency test result on Knights Mill and Skylake server
The heavy workload is to compile Linux kernel as below "sudo nice make -j$(grep -c '^processor' /proc/cpuinfo)" Then, "sudo perf top"
The latency period is the time between perf top launched and the first profiling result shown.
- Latency on Knights Mill (272 CPUs)
Original(s) With patch(s) Speedup 272.68 16.48 16.54x
- Latency on Skylake server (192 CPUs)
Original(s) With patch(s) Speedup 12.28 2.96 4.15x
Kan Liang (10):
perf tools: hashtable for machine threads
perf tools: using scandir to replace readdir
petf tools: using comm_str to replace comm in hist_entry petf tools: introduce a new function to set namespaces id perf tools: lock to protect thread list
perf tools: lock to protect comm_str rb tree
perf tools: change machine comm_exec type to atomic
perf top: implement multithreading for perf_event__synthesize_threads perf top: add option to set the number of thread for event synthesize perf top: switch back to overwrite mode
tools/perf/builtin-kvm.c | 3 +- tools/perf/builtin-record.c | 2 +- tools/perf/builtin-top.c | 9 +- tools/perf/builtin-trace.c | 21 +++-- tools/perf/tests/mmap-thread-lookup.c | 2 +- tools/perf/ui/browsers/hists.c | 2 +- tools/perf/util/comm.c | 18 +++- tools/perf/util/event.c | 142 ++++++++++++++++++++++++------ tools/perf/util/event.h | 14 ++- tools/perf/util/evlist.c | 5 +- tools/perf/util/hist.c | 10 +-- tools/perf/util/machine.c | 158 +++++++++++++++++++++------------- tools/perf/util/machine.h | 34 ++++++-- tools/perf/util/rb_resort.h | 5 +- tools/perf/util/sort.c | 8 +- tools/perf/util/sort.h | 2 +- tools/perf/util/thread.c | 68 ++++++++++++--- tools/perf/util/thread.h | 6 +- tools/perf/util/top.h | 1 + 19 files changed, 368 insertions(+), 142 deletions(-)
Signature · 1 line
2.5.5
RFC PATCH 1/10 perf tools: hashtable for machine threads
From: Kan Liang <[email protected]>
To process any events, it needs to find the thread in the machine first. The machine maintains a rb tree to store all threads. The rb tree is protected by a rw lock.
It is not a problem for current perf which serially processing events. However, it will have scalability performance issue to process events in parallel, especially on a heave load system which have many threads.
Introduce a hashtable to divide the big rb tree into many samll rb tree for threads. The index is thread id % hashtable size. It can reduce the lock contention.
--- tools/perf/builtin-trace.c | 19 +++--- tools/perf/util/machine.c | 139 ++++++++++++++++++++++++++++---------------- tools/perf/util/machine.h | 23 ++++++-- tools/perf/util/rb_resort.h | 5 +- 4 files changed, 120 insertions(+), 66 deletions(-)
tools/perf/builtin-trace.c +11 -8
@@ -2730,20 +2730,23 @@ DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_eventstatic size_t trace__fprintf_thread_summary (struct trace *trace,FILE *fp) { -DECLARE_RESORT_RB_MACHINE_THREADS (threads, trace->host);size_t printed =trace__fprintf_threads_header (fp);struct rb_node *nd; +int i; -if (threads ==NULL ) { -fprintf (fp,"%s" ,"Error sorting output by nr_events!\n" ); -return 0 ; - } +for (i =0 ; i <MACHINE_TH_TABLE_SIZE ; i++) { +DECLARE_RESORT_RB_MACHINE_THREADS (threads, trace->host, i); -resort_rb__for_each_entry (nd, threads) - printed +=trace__fprintf_thread (fp, threads_entry->thread, trace); +if (threads ==NULL ) { +fprintf (fp,"%s" ,"Error sorting output by nr_events!\n" ); +return 0 ; + } -resort_rb__delete (threads); +resort_rb__for_each_entry (nd, threads) + printed +=trace__fprintf_thread (fp, threads_entry->thread, trace); +resort_rb__delete (threads); + }return printed; }
tools/perf/util/machine.c +88 -51
269 lines not on this page · raw message
tools/perf/util/machine.h +18 -5
@@ -23,6 +23,17 @@ extern const char *ref_reloc_sym_names[];struct vdso_info ; +#define MACHINE_TH_TABLE_BITS 8 +#define MACHINE_TH_TABLE_SIZE (1 << MACHINE_TH_TABLE_BITS) + +struct machine_th { +struct rb_root threads; +pthread_rwlock_t threads_lock; +unsigned int nr_threads; +struct list_head dead_threads; +struct thread *last_match; +}; +struct machine {struct rb_node rb_node;pid_t pid;
@@ -30,11 +41,7 @@ struct machine {bool comm_exec;bool kptr_restrict_warned;char *root_dir; -struct rb_root threads; -pthread_rwlock_t threads_lock; -unsigned int nr_threads; -struct list_head dead_threads; -struct thread *last_match; +struct machine_th threads[MACHINE_TH_TABLE_SIZE ];struct vdso_info *vdso_info;struct perf_env *env;struct dsos dsos;
@@ -49,6 +56,12 @@ struct machine { };static inline +struct machine_th *machine_thread (struct machine *machine,pid_t tid) +{ +return &machine->threads[tid %MACHINE_TH_TABLE_SIZE ]; +} + +static inline struct map *__machine__kernel_map (struct machine *machine,enum map_type type) {return machine->vmlinux_maps[type];
tools/perf/util/rb_resort.h +3 -2
@@ -143,7 +143,8 @@ struct __name##_sorted *__name = __name##_sorted__new __ilist->rblist.nr_entries)/* For 'struct machine->threads' */ -#define DECLARE_RESORT_RB_MACHINE_THREADS (__name, __machine) \ - DECLARE_RESORT_RB(__name)(&__machine->threads, __machine->nr_threads) +#define DECLARE_RESORT_RB_MACHINE_THREADS (__name, __machine, tid) \ + DECLARE_RESORT_RB(__name)(&__machine->threads[tid].threads, \ + __machine->threads[tid].nr_threads)#endif /* _PERF_RESORT_RB_H_ */
Signature · 1 line
2.5.5
Arnaldo Carvalho de Melo
maintainerRe: [PATCH RFC 01/10] perf tools: hashtable for machine threads
Em Thu, Sep 07, 2017 at 10:55:45AM -0700, [email protected] escreveu:
Quoted 12 lines
From: Kan Liang <[email protected]>
To process any events, it needs to find the thread in the machine first. The machine maintains a rb tree to store all threads. The rb tree is protected by a rw lock.
It is not a problem for current perf which serially processing events. However, it will have scalability performance issue to process events in parallel, especially on a heave load system which have many threads.Introduce a hashtable to divide the big rb tree into many samll rb tree for threads. The index is thread id % hashtable size. It can reduce the lock contention.
<SNIP>
Quoted 6 lines
+++
b/tools/perf/util/machine.htools/perf/util/machine.h +1
-0@@ -23,6 +23,17 @@ extern const char *ref_reloc_sym_names[];struct vdso_info ; +#define MACHINE_TH_TABLE_BITS 8
tools/perf/util/machine.h extern const char *ref_reloc_sym_names[]; · line 36
Quoted 10 lines
tools/perf/util/machine.h +10
-0+#define MACHINE_TH_TABLE_SIZE (1 << MACHINE_TH_TABLE_BITS) + +struct machine_th { +struct rb_root threads; +pthread_rwlock_t threads_lock; +unsigned int nr_threads; +struct list_head dead_threads; +struct thread *last_match; +}; +
Call it just "threads", no need to call it then threads->threads, but threads->entries, also no threads->threads_lock, but threads->lock, threads->deads, threads->nr.
MACHINE_TH_TABLE_SIZE -> THREADS__TABLE_SIZE, etc.
Quoted 17 lines
tools/perf/util/machine.h +1
-5struct machine {struct rb_node rb_node;pid_t pid;@@ -30,11 +41,7 @@ struct machine {bool comm_exec;bool kptr_restrict_warned;char *root_dir; -struct rb_root threads; -pthread_rwlock_t threads_lock; -unsigned int nr_threads; -struct list_head dead_threads; -struct thread *last_match; +struct machine_th threads[MACHINE_TH_TABLE_SIZE ];struct vdso_info *vdso_info;struct perf_env *env;struct dsos dsos;
tools/perf/util/machine.h struct machine { · line 59
Quoted 4 lines
tools/perf/util/machine.h +1
-0@@ -49,6 +56,12 @@ struct machine { };static inline +struct machine_th *machine_thread (struct machine *machine,pid_t tid)
We separate the class name (machine) from the method name (thread) using double underscores, i.e. the above becomes:
static inline threads *machine__threads(struct machine *machine, pid_t tid)
Quoted 25 lines
tools/perf/util/machine.h +5
-0+{ +return &machine->threads[tid %MACHINE_TH_TABLE_SIZE ]; +} + +static inline struct map *__machine__kernel_map (struct machine *machine,enum map_type type) {return machine->vmlinux_maps[type];tools/perf/util/rb_resort.h +3
-2@@ -143,7 +143,8 @@ struct __name##_sorted *__name = __name##_sorted__new __ilist->rblist.nr_entries)/* For 'struct machine->threads' */ -#define DECLARE_RESORT_RB_MACHINE_THREADS (__name, __machine) \ - DECLARE_RESORT_RB(__name)(&__machine->threads, __machine->nr_threads) +#define DECLARE_RESORT_RB_MACHINE_THREADS (__name, __machine, tid) \ + DECLARE_RESORT_RB(__name)(&__machine->threads[tid].threads, \ + __machine->threads[tid].nr_threads)#endif /* _PERF_RESORT_RB_H_ */ Signature · 1 line
2.5.5
RFC PATCH 2/10 perf tools: using scandir to replace readdir
In reply to [email protected]From: Kan Liang <[email protected]>
For perf_event__synthesize_threads, perf goes through all proc files serially by readdir.
scandir did a snapshoot of proc, which is multithreading friendly.
It's possible that some threads which are added during event synthesize. But the number of lost threads should be small.
They should not impact the final analysis.
--- tools/perf/util/event.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-)
tools/perf/util/event.c +25 -20
@@ -683,12 +683,14 @@ int perf_event__synthesize_threads(struct perf_tool *tool,bool mmap_data, unsigned int proc_map_timeout) { - DIR *proc; -char proc_path[PATH_MAX ]; -struct dirent *dirent; union perf_event *comm_event, *mmap_event, *fork_event;union perf_event *namespaces_event; +char proc_path[PATH_MAX ]; +struct dirent **dirent;int err =-1 ; +char *end; +pid_t pid; +int n, i;if (machine__is_default_guest (machine))return 0 ;
@@ -712,29 +714,32 @@ int perf_event__synthesize_threads(struct perf_tool *tool, goto out_free_fork;snprintf (proc_path,sizeof (proc_path),"%s/proc" , machine->root_dir); - proc =opendir (proc_path); + n =scandir (proc_path, &dirent,0 , alphasort); -if (proc ==NULL ) +if (n <0 ) goto out_free_namespaces; -while ((dirent =readdir (proc)) !=NULL ) { -char *end; -pid_t pid =strtol (dirent->d_name, &end,10 ); - -if (*end)/* only interested in proper numerical dirents */ +for (i =0 ; i < n; i++) { +if (!isdigit (dirent[i]->d_name[0 ]))continue ; -/* -* 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, fork_event, - namespaces_event, pid,1 , process, - tool, machine, mmap_data, - proc_map_timeout); - } + pid = (pid_t )strtol (dirent[i]->d_name, &end,10 ); +/* only interested in proper numerical dirents */ +if (!*end) { +/* +* 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, fork_event, + namespaces_event, pid,1 , process, + tool, machine, mmap_data, + proc_map_timeout); + } +free (dirent[i]); + } +free (dirent); err =0 ; -closedir (proc); + out_free_namespaces:free (namespaces_event); out_free_fork:
Signature · 1 line
2.5.5
Arnaldo Carvalho de Melo
maintainerRe: [PATCH RFC 02/10] perf tools: using scandir to replace readdir
Em Thu, Sep 07, 2017 at 10:55:46AM -0700, [email protected] escreveu:
Quoted 9 lines
From: Kan Liang <[email protected]>
For
perf_event__synthesize_threads, perf goes through all proc files serially by readdir.
scandir did a snapshoot of proc, which is multithreading friendly.It's possible that some threads which are added during event synthesize. But the number of lost threads should be small.
They should not impact the final analysis.
Looks ok, applied locally, will see how it builds in my containers tests.
- Arnaldo
Quoted 79 lines
Signed-off-by Kan Liang <[email protected]>--- tools/perf/util/event.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-)tools/perf/util/event.c +25
-20@@ -683,12 +683,14 @@ int perf_event__synthesize_threads(struct perf_tool *tool,bool mmap_data, unsigned int proc_map_timeout) { - DIR *proc; -char proc_path[PATH_MAX ]; -struct dirent *dirent; union perf_event *comm_event, *mmap_event, *fork_event;union perf_event *namespaces_event; +char proc_path[PATH_MAX ]; +struct dirent **dirent;int err =-1 ; +char *end; +pid_t pid; +int n, i;if (machine__is_default_guest (machine))return 0 ;@@ -712,29 +714,32 @@ int perf_event__synthesize_threads(struct perf_tool *tool, goto out_free_fork;snprintf (proc_path,sizeof (proc_path),"%s/proc" , machine->root_dir); - proc =opendir (proc_path); + n =scandir (proc_path, &dirent,0 , alphasort); -if (proc ==NULL ) +if (n <0 ) goto out_free_namespaces; -while ((dirent =readdir (proc)) !=NULL ) { -char *end; -pid_t pid =strtol (dirent->d_name, &end,10 ); - -if (*end)/* only interested in proper numerical dirents */ +for (i =0 ; i < n; i++) { +if (!isdigit (dirent[i]->d_name[0 ]))continue ; -/* -* 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, fork_event, - namespaces_event, pid,1 , process, - tool, machine, mmap_data, - proc_map_timeout); - } + pid = (pid_t )strtol (dirent[i]->d_name, &end,10 ); +/* only interested in proper numerical dirents */ +if (!*end) { +/* +* 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, fork_event, + namespaces_event, pid,1 , process, + tool, machine, mmap_data, + proc_map_timeout); + } +free (dirent[i]); + } +free (dirent); err =0 ; -closedir (proc); + out_free_namespaces:free (namespaces_event); out_free_fork:Signature · 1 line
2.5.5
tip-bot for Kan Liang
merge bot[tip:perf/core] perf tools: Use scandir() to replace readdir()
In reply to [email protected]perf tools: Use scandir() to replace readdir()
Commit-ID: ecdad24d7a4480c9af0ff6dbe00ac8bbae720d19 Gitweb: http://git.kernel.org/tip/ecdad24d7a4480c9af0ff6dbe00ac8bbae720d19 Author: Kan Liang <[email protected]> AuthorDate: Thu, 7 Sep 2017 10:55:46 -0700 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 13 Sep 2017 09:49:15 -0300
perf tools: Use scandir() to replace readdir()
In perf_event__synthesize_threads() perf goes through all proc files serially by readdir.
scandir() does a snapshoot of /proc, which is multithreading friendly.
It's possible that some threads which are added during event synthesize. But the number of lost threads should be small. They should not impact the final analysis.
--- tools/perf/util/event.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-)
tools/perf/util/event.c +25 -20
@@ -683,12 +683,14 @@ int perf_event__synthesize_threads(struct perf_tool *tool,bool mmap_data, unsigned int proc_map_timeout) { - DIR *proc; -char proc_path[PATH_MAX ]; -struct dirent *dirent; union perf_event *comm_event, *mmap_event, *fork_event;union perf_event *namespaces_event; +char proc_path[PATH_MAX ]; +struct dirent **dirent;int err =-1 ; +char *end; +pid_t pid; +int n, i;if (machine__is_default_guest (machine))return 0 ;
@@ -712,29 +714,32 @@ int perf_event__synthesize_threads(struct perf_tool *tool, goto out_free_fork;snprintf (proc_path,sizeof (proc_path),"%s/proc" , machine->root_dir); - proc =opendir (proc_path); + n =scandir (proc_path, &dirent,0 , alphasort); -if (proc ==NULL ) +if (n <0 ) goto out_free_namespaces; -while ((dirent =readdir (proc)) !=NULL ) { -char *end; -pid_t pid =strtol (dirent->d_name, &end,10 ); - -if (*end)/* only interested in proper numerical dirents */ +for (i =0 ; i < n; i++) { +if (!isdigit (dirent[i]->d_name[0 ]))continue ; -/* -* 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, fork_event, - namespaces_event, pid,1 , process, - tool, machine, mmap_data, - proc_map_timeout); - } + pid = (pid_t )strtol (dirent[i]->d_name, &end,10 ); +/* only interested in proper numerical dirents */ +if (!*end) { +/* +* 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, fork_event, + namespaces_event, pid,1 , process, + tool, machine, mmap_data, + proc_map_timeout); + } +free (dirent[i]); + } +free (dirent); err =0 ; -closedir (proc); + out_free_namespaces:free (namespaces_event); out_free_fork:
RFC PATCH 3/10 petf tools: using comm_str to replace comm in hist_entry
In reply to [email protected]From: Kan Liang <[email protected]>
For hist_entry, it only needs comm_str for cmp.
--- tools/perf/util/hist.c | 4 ++-- tools/perf/util/sort.c | 8 ++++---- tools/perf/util/sort.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
tools/perf/util/hist.c +2 -2
@@ -587,7 +587,7 @@ __hists__add_entry(struct hists *hists,struct namespaces *ns =thread__namespaces (al->thread);struct hist_entry entry = { .thread = al->thread, - .comm =thread__comm (al->thread), + .comm_str =thread__comm_str (al->thread), .cgroup_id = { .dev = ns ? ns->link_info[CGROUP_NS_INDEX ].dev :0 , .ino = ns ? ns->link_info[CGROUP_NS_INDEX ].ino :0 ,
@@ -944,7 +944,7 @@ iter_add_next_cumulative_entry(struct hist_entry_iter *iter, .hists = evsel__hists(evsel), .cpu = al->cpu, .thread = al->thread, - .comm = thread__comm(al->thread), + .comm_str = thread__comm_str(al->thread), .ip = al->addr, .ms = { .map = al->map,
tools/perf/util/sort.c +4 -4
@@ -114,26 +114,26 @@ static int64_t sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) {/* Compare the addr that should be unique among comm */ -return strcmp (comm__str (right->comm),comm__str (left->comm)); +return strcmp (right->comm_str, left->comm_str); }static int64_t sort__comm_collapse (struct hist_entry *left,struct hist_entry *right) {/* Compare the addr that should be unique among comm */ -return strcmp (comm__str (right->comm),comm__str (left->comm)); +return strcmp (right->comm_str, left->comm_str); }static int64_t sort__comm_sort (struct hist_entry *left,struct hist_entry *right) { -return strcmp (comm__str (right->comm),comm__str (left->comm)); +return strcmp (right->comm_str, left->comm_str); }static int hist_entry__comm_snprintf (struct hist_entry *he,char *bf,size_t size,unsigned int width) { -return repsep_snprintf (bf, size,"%-*.*s" , width, width,comm__str (he->comm)); +return repsep_snprintf (bf, size,"%-*.*s" , width, width, he->comm_str); }struct sort_entry sort_comm = {
tools/perf/util/sort.h +1 -1
@@ -96,7 +96,7 @@ struct hist_entry {struct he_stat *stat_acc;struct map_symbol ms;struct thread *thread; -struct comm *comm; +const char *comm_str;struct namespace_id cgroup_id;u64 ip;u64 transaction;
Signature · 1 line
2.5.5
RFC PATCH 4/10 petf tools: introduce a new function to set namespaces id
In reply to [email protected]From: Kan Liang <[email protected]>
Finish the namespaces id setting job in thread.c.
--- tools/perf/util/hist.c | 6 +----- tools/perf/util/thread.c | 10 ++++++++++ tools/perf/util/thread.h | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-)
tools/perf/util/hist.c +1 -5
@@ -584,14 +584,9 @@ __hists__add_entry(struct hists *hists, bool sample_self, struct hist_entry_ops *ops) { - struct namespaces *ns = thread__namespaces(al->thread); struct hist_entry entry = { .thread = al->thread, .comm_str = thread__comm_str(al->thread), - .cgroup_id = { - .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0, - .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0, - }, .ms = { .map = al->map, .sym = al->sym,
@@ -617,6 +612,7 @@ __hists__add_entry(struct hists *hists, .ops = ops, }; +thread__namespaces_id (al->thread, &entry.cgroup_id.dev, &entry.cgroup_id.ino);return hists__findnew_entry (hists, &entry, al, sample_self); }
tools/perf/util/thread.c +10 -0
@@ -149,6 +149,16 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,return 0 ; } +void thread__namespaces_id (const struct thread *thread, +u64 *dev,u64 *ino) +{ +struct namespaces *ns; + + ns =thread__namespaces (thread); + *dev = ns ? ns->link_info[CGROUP_NS_INDEX ].dev :0 ; + *ino = ns ? ns->link_info[CGROUP_NS_INDEX ].ino :0 ; +} +struct comm *thread__comm (const struct thread *thread) {if (list_empty (&thread->comm_list))
tools/perf/util/thread.h +2 -0
@@ -68,6 +68,8 @@ static inline void thread__exited(struct thread *thread)struct namespaces *thread__namespaces (const struct thread *thread);int thread__set_namespaces (struct thread *thread,u64 timestamp,struct namespaces_event *event); +void thread__namespaces_id (const struct thread *thread, +u64 *dev,u64 *ino);int __thread__set_comm (struct thread *thread,const char *comm,u64 timestamp,bool exec);
Signature · 1 line
2.5.5
RFC PATCH 5/10 perf tools: lock to protect thread list
In reply to [email protected]From: Kan Liang <[email protected]>
Add two locks to protect namespaces_list and comm_list.
The comm which is used in db-export are not protected. Because the multithread code will not touch it. It can be added later if required.
--- tools/perf/ui/browsers/hists.c | 2 +- tools/perf/util/thread.c | 60 +++++++++++++++++++++++++++++++++--------- tools/perf/util/thread.h | 6 +++-- 3 files changed, 52 insertions(+), 16 deletions(-)
tools/perf/ui/browsers/hists.c +1 -1
@@ -2370,7 +2370,7 @@ static int perf_evsel_browser_title(struct hist_browser *browser,char unit;int printed;const struct dso *dso = hists->dso_filter; -const struct thread *thread = hists->thread_filter; +struct thread *thread = hists->thread_filter;int socket_id = hists->socket_filter;unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE ];u64 nr_events = hists->stats.total_period;
tools/perf/util/thread.c +47 -13
134 lines not on this page · raw message
tools/perf/util/thread.h +4 -2
@@ -29,7 +29,9 @@ struct thread {int comm_len;bool dead;/* if set thread has exited */ struct list_head namespaces_list; +pthread_mutex_t namespaces_lock;struct list_head comm_list; +pthread_mutex_t comm_lock;u64 db_id;void *priv;
@@ -68,7 +70,7 @@ static inline void thread__exited(struct thread *thread)struct namespaces *thread__namespaces (const struct thread *thread);int thread__set_namespaces (struct thread *thread,u64 timestamp,struct namespaces_event *event); -void thread__namespaces_id (const struct thread *thread, +void thread__namespaces_id (struct thread *thread,u64 *dev,u64 *ino); int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
@@ -84,7 +86,7 @@ int thread__set_comm_from_proc(struct thread *thread);int thread__comm_len (struct thread *thread);struct comm *thread__comm (const struct thread *thread);struct comm *thread__exec_comm (const struct thread *thread); -const char *thread__comm_str (const struct thread *thread); +const char *thread__comm_str (struct thread *thread);int thread__insert_map (struct thread *thread,struct map *map);int thread__fork (struct thread *thread,struct thread *parent,u64 timestamp);size_t thread__fprintf (struct thread *thread,FILE *fp);
Signature · 1 line
2.5.5
RFC PATCH 6/10 perf tools: lock to protect comm_str rb tree
In reply to [email protected]From: Kan Liang <[email protected]>
Add comm_str_lock to protect comm_str rb tree.
--- tools/perf/util/comm.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
tools/perf/util/comm.c +14 -4
@@ -5,6 +5,7 @@#include <stdio.h> #include <string.h> #include <linux/refcount.h> +#include <pthread.h> struct comm_str {char *str;
@@ -14,6 +15,7 @@ struct comm_str {/* Should perhaps be moved to struct machine */ static struct rb_root comm_str_root; +static pthread_mutex_t comm_str_lock =PTHREAD_MUTEX_INITIALIZER ;static struct comm_str *comm_str__get (struct comm_str *cs) {
@@ -24,11 +26,13 @@ static struct comm_str *comm_str__get(struct comm_str *cs)static void comm_str__put (struct comm_str *cs) { +pthread_mutex_lock (&comm_str_lock);if (cs &&refcount_dec_and_test (&cs->refcnt)) {rb_erase (&cs->rb_node, &comm_str_root);zfree (&cs->str);free (cs); } +pthread_mutex_unlock (&comm_str_lock); }static struct comm_str *comm_str__alloc (const char *str)
@@ -52,18 +56,22 @@ static struct comm_str *comm_str__alloc(const char *str)static struct comm_str *comm_str__findnew (const char *str,struct rb_root *root) { -struct rb_node **p = &root->rb_node;struct rb_node *parent =NULL ;struct comm_str *iter, *new; +struct rb_node **p;int cmp; +pthread_mutex_lock (&comm_str_lock); + p = &root->rb_node;while (*p !=NULL ) { parent = *p; iter = rb_entry(parent, struct comm_str, rb_node); cmp =strcmp (str, iter->str); -if (!cmp) -return comm_str__get (iter); +if (!cmp) { + new =comm_str__get (iter); + goto unlock; + }if (cmp <0 ) p = &(*p)->rb_left;
@@ -73,11 +81,13 @@ static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root) new =comm_str__alloc (str);if (!new) -return NULL ; + goto unlock;rb_link_node (&new->rb_node, parent, p);rb_insert_color (&new->rb_node, root); +unlock: +pthread_mutex_unlock (&comm_str_lock);return new; }
Signature · 1 line
2.5.5
Arnaldo Carvalho de Melo
maintainerRe: [PATCH RFC 06/10] perf tools: lock to protect comm_str rb tree
Em Thu, Sep 07, 2017 at 10:55:50AM -0700, [email protected] escreveu:
Quoted 31 lines
From: Kan Liang <[email protected]>
Add
comm_str_lockto protectcomm_strrb tree.Signed-off-by Kan Liang <[email protected]>--- tools/perf/util/comm.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)tools/perf/util/comm.c +2
-0@@ -5,6 +5,7 @@#include <stdio.h> #include <string.h> #include <linux/refcount.h> +#include <pthread.h> struct comm_str {char *str;@@ -14,6 +15,7 @@ struct comm_str {/* Should perhaps be moved to struct machine */ static struct rb_root comm_str_root; +static pthread_mutex_t comm_str_lock =PTHREAD_MUTEX_INITIALIZER ;static struct comm_str *comm_str__get (struct comm_str *cs) {@@ -24,11 +26,13 @@ static struct comm_str *comm_str__get(struct comm_str *cs)
tools/perf/util/comm.c static struct comm_str *comm_str__get(struct comm_str *cs) · line 36
Quoted 10 lines
tools/perf/util/comm.c +2
-0static void comm_str__put (struct comm_str *cs) { +pthread_mutex_lock (&comm_str_lock);if (cs &&refcount_dec_and_test (&cs->refcnt)) {rb_erase (&cs->rb_node, &comm_str_root);zfree (&cs->str);free (cs); } +pthread_mutex_unlock (&comm_str_lock); }
The above should use a smaller locked section, i.e.:
static void comm_str__put(struct comm_str *cs) {
if (cs && refcount_dec_and_test(&cs->refcnt)) {
+ pthread_mutex_lock(&comm_str_lock);
rb_erase(&cs->rb_node, &comm_str_root);
+ pthread_mutex_unlock(&comm_str_lock);
zfree(&cs->str);
free(cs);
}
}
tools/perf/util/comm.c static struct comm_str *comm_str__alloc(const char *str) · line 57
Quoted 4 lines
tools/perf/util/comm.c +0
-0static struct comm_str *comm_str__alloc (const char *str)@@ -52,18 +56,22 @@ static struct comm_str *comm_str__alloc(const char *str)static struct comm_str *comm_str__findnew (const char *str,struct rb_root *root)
The usual way is to just rename the above to __comm_str__findnew(), leaving it unlocked, and then add a locked wrapper:
static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
{
struct comm_str *cs;
pthread_mutex_lock(&comm_str_lock);
cs = __comm_str__findnew(str, root);
pthread_mutex_unlock(&comm_str_lock);
return cs;
}Quoted 40 lines
tools/perf/util/comm.c +10
-4{ -struct rb_node **p = &root->rb_node;struct rb_node *parent =NULL ;struct comm_str *iter, *new; +struct rb_node **p;int cmp; +pthread_mutex_lock (&comm_str_lock); + p = &root->rb_node;while (*p !=NULL ) { parent = *p; iter = rb_entry(parent, struct comm_str, rb_node); cmp =strcmp (str, iter->str); -if (!cmp) -return comm_str__get (iter); +if (!cmp) { + new =comm_str__get (iter); + goto unlock; + }if (cmp <0 ) p = &(*p)->rb_left;@@ -73,11 +81,13 @@ static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root) new =comm_str__alloc (str);if (!new) -return NULL ; + goto unlock;rb_link_node (&new->rb_node, parent, p);rb_insert_color (&new->rb_node, root); +unlock: +pthread_mutex_unlock (&comm_str_lock);return new; }Signature · 1 line
2.5.5
RFC PATCH 7/10 perf tools: change machine comm_exec type to atomic
In reply to [email protected]From: Kan Liang <[email protected]>
In case there are two or more threads want to change it.
--- tools/perf/util/machine.c | 11 ++++++----- tools/perf/util/machine.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-)
tools/perf/util/machine.c +6 -5
@@ -64,8 +64,8 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) machine->id_hdr_size =0 ; machine->kptr_restrict_warned = false; - machine->comm_exec = false; machine->kernel_start =0 ; +atomic_set (&machine->comm_exec,0 );memset (machine->vmlinux_maps,0 ,sizeof (machine->vmlinux_maps));
@@ -238,14 +238,15 @@ struct machine *machines__add(struct machines *machines, pid_t pid,void machines__set_comm_exec (struct machines *machines,bool comm_exec) { +int exec = comm_exec ?1 :0 ;struct rb_node *nd; - machines->host.comm_exec = comm_exec; +atomic_set (&machines->host.comm_exec, exec);for (nd =rb_first (&machines->guests); nd; nd =rb_next (nd)) { struct machine *machine = rb_entry(nd, struct machine, rb_node); - machine->comm_exec = comm_exec; +atomic_set (&machine->comm_exec, exec); } }
@@ -505,7 +506,7 @@ struct thread *machine__find_thread(struct machine *machine, pid_t pid,struct comm *machine__thread_exec_comm (struct machine *machine,struct thread *thread) { -if (machine->comm_exec) +if (atomic_read (&machine->comm_exec))return thread__exec_comm (thread);else return thread__comm (thread);
@@ -521,7 +522,7 @@ int machine__process_comm_event(struct machine *machine, union perf_event *eventint err =0 ;if (exec) - machine->comm_exec = true; +atomic_set (&machine->comm_exec,1 );if (dump_trace)perf_event__fprintf_comm (event, stdout);
tools/perf/util/machine.h +1 -1
@@ -38,7 +38,7 @@ struct machine {struct rb_node rb_node;pid_t pid;u16 id_hdr_size; -bool comm_exec; +atomic_t comm_exec;bool kptr_restrict_warned;char *root_dir;struct machine_th threads[MACHINE_TH_TABLE_SIZE ];
Signature · 1 line
2.5.5
RFC PATCH 8/10 perf top: implement multithreading for perf_event__synthesize_threads
In reply to [email protected]From: Kan Liang <[email protected]>
The proc files which is sorted with alphabetical order are evenly assigned to several synthesize threads to be processed in parallel.
For perf top, the threads number hard code to online CPU number. The following patch will introduce an option to set it.
For other perf tools, the thread number is 1. Because the process function is not ready for multithreading, e.g. process_synthesized_event.
This patch series only support event synthesize multithreading for perf top. For other tools, it can be done separately later.
With multithread applied, the total processing time can get up to 1.56x speedup on Knights Mill for perf top.
For specific single event processing, the processing time could increase because of the lock contention. So proc_map_timeout may need to be increased. Otherwise some proc maps will be truncated.
Based on my test, increasing the proc_map_timeout has small impact on the total processing time. The total processing time still get 1.49x speedup on Knights Mill after increasing the proc_map_timeout. The patch itself doesn't increase the proc_map_timeout.
Doesn't need to implement multithreading for perf_event__synthesize_thread_map. It doesn't have performance issue.
--- tools/perf/builtin-kvm.c | 3 +- tools/perf/builtin-record.c | 2 +- tools/perf/builtin-top.c | 4 +- tools/perf/builtin-trace.c | 2 +- tools/perf/tests/mmap-thread-lookup.c | 2 +- tools/perf/util/event.c | 144 ++++++++++++++++++++++++++-------- tools/perf/util/event.h | 14 +++- tools/perf/util/machine.c | 8 +- tools/perf/util/machine.h | 9 ++- 9 files changed, 145 insertions(+), 43 deletions(-)
tools/perf/builtin-kvm.c +2 -1
@@ -1442,7 +1442,8 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,perf_session__set_id_hdr_size (kvm->session);ordered_events__set_copy_on_queue (&kvm->session->ordered_events, true);machine__synthesize_threads (&kvm->session->machines.host, &kvm->opts.target, - kvm->evlist->threads, false, kvm->opts.proc_map_timeout); + kvm->evlist->threads, false, + kvm->opts.proc_map_timeout,1 ); err =kvm_live_open_events (kvm);if (err) goto out;
tools/perf/builtin-record.c +1 -1
@@ -863,7 +863,7 @@ static int record__synthesize(struct record *rec, bool tail) err =__machine__synthesize_threads (machine, tool, &opts->target, rec->evlist->threads, process_synthesized_event, opts->sample_address, - opts->proc_map_timeout); + opts->proc_map_timeout,1 ); out:return err; }
tools/perf/builtin-top.c +3 -1
@@ -959,7 +959,9 @@ static int __cmd_top(struct perf_top *top) goto out_delete;machine__synthesize_threads (&top->session->machines.host, &opts->target, - top->evlist->threads, false, opts->proc_map_timeout); + top->evlist->threads, false, + opts->proc_map_timeout, + (unsigned int )sysconf (_SC_NPROCESSORS_ONLN));if (perf_hpp_list.socket) { ret =perf_env__read_cpu_topology_map (&perf_env);
tools/perf/builtin-trace.c +1 -1
@@ -1131,7 +1131,7 @@ static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) err =__machine__synthesize_threads (trace->host, &trace->tool, &trace->opts.target, evlist->threads, trace__tool_process, false, - trace->opts.proc_map_timeout); + trace->opts.proc_map_timeout,1 );if (err)symbol__exit ();
tools/perf/tests/mmap-thread-lookup.c +1 -1
@@ -131,7 +131,7 @@ static int synth_all(struct machine *machine) {return perf_event__synthesize_threads (NULL , perf_event__process, - machine,0 ,500 ); + machine,0 ,500 ,1 ); }static int synth_process (struct machine *machine)
tools/perf/util/event.c +112 -32
175 lines not on this page · raw message
tools/perf/util/event.h +13 -1
@@ -664,6 +664,17 @@ typedef int (*perf_event__handler_t)(struct perf_tool *tool,struct perf_sample *sample, struct machine *machine); +struct synthesize_threads_arg { +struct perf_tool *tool; +perf_event__handler_t process; +struct machine *machine; +bool mmap_data; +unsigned int proc_map_timeout; +struct dirent **dirent; +int num; +int start; +}; + int perf_event__synthesize_thread_map(struct perf_tool *tool, struct thread_map *threads, perf_event__handler_t process,
@@ -680,7 +691,8 @@ int perf_event__synthesize_cpu_map(struct perf_tool *tool,int perf_event__synthesize_threads (struct perf_tool *tool,perf_event__handler_t process,struct machine *machine,bool mmap_data, -unsigned int proc_map_timeout); +unsigned int proc_map_timeout, +unsigned int nr_threads_synthesize);int perf_event__synthesize_kernel_mmap (struct perf_tool *tool,perf_event__handler_t process,struct machine *machine);
tools/perf/util/machine.c +6 -2
@@ -2221,12 +2221,16 @@ int machines__for_each_thread(struct machines *machines,int __machine__synthesize_threads (struct machine *machine,struct perf_tool *tool,struct target *target,struct thread_map *threads,perf_event__handler_t process,bool data_mmap, -unsigned int proc_map_timeout) +unsigned int proc_map_timeout, +unsigned int nr_threads_synthesize) {if (target__has_task (target))return perf_event__synthesize_thread_map (tool, threads, process, machine, data_mmap, proc_map_timeout);else if (target__has_cpu (target)) -return perf_event__synthesize_threads (tool, process, machine, data_mmap, proc_map_timeout); +return perf_event__synthesize_threads (tool, process, + machine, data_mmap, + proc_map_timeout, + nr_threads_synthesize);/* command specified */ return 0 ; }
tools/perf/util/machine.h +6 -3
@@ -256,15 +256,18 @@ int machines__for_each_thread(struct machines *machines,int __machine__synthesize_threads (struct machine *machine,struct perf_tool *tool,struct target *target,struct thread_map *threads,perf_event__handler_t process,bool data_mmap, -unsigned int proc_map_timeout); +unsigned int proc_map_timeout, +unsigned int nr_threads_synthesize);static inline int machine__synthesize_threads (struct machine *machine,struct target *target,struct thread_map *threads,bool data_mmap, -unsigned int proc_map_timeout) +unsigned int proc_map_timeout, +unsigned int nr_threads_synthesize) {return __machine__synthesize_threads (machine,NULL , target, threads, perf_event__process, data_mmap, - proc_map_timeout); + proc_map_timeout, + nr_threads_synthesize); }pid_t machine__get_current_tid (struct machine *machine,int cpu);
Signature · 1 line
2.5.5
RFC PATCH 9/10 perf top: add option to set the number of thread for event synthesize
In reply to [email protected]From: Kan Liang <[email protected]>
Using UINT_MAX to indicate the default thread#, which is the max number of online CPU.
--- tools/perf/builtin-top.c | 5 ++++- tools/perf/util/event.c | 5 ++++- tools/perf/util/top.h | 1 + 3 files changed, 9 insertions(+), 2 deletions(-)
tools/perf/builtin-top.c +4 -1
@@ -961,7 +961,7 @@ static int __cmd_top(struct perf_top *top)machine__synthesize_threads (&top->session->machines.host, &opts->target, top->evlist->threads, false, opts->proc_map_timeout, - (unsigned int )sysconf (_SC_NPROCESSORS_ONLN)); + top->nr_threads_synthesize);if (perf_hpp_list.socket) { ret =perf_env__read_cpu_topology_map (&perf_env);
@@ -1114,6 +1114,7 @@ int cmd_top(int argc, const char **argv) }, .max_stack = sysctl_perf_event_max_stack, .sym_pcnt_filter = 5, + .nr_threads_synthesize = UINT_MAX, };struct record_opts *opts = &top.record_opts;struct target *target = &opts->target;
@@ -1223,6 +1224,8 @@ int cmd_top(int argc, const char **argv)OPT_BOOLEAN (0 ,"hierarchy" , &symbol_conf.report_hierarchy,"Show entries in a hierarchy" ),OPT_BOOLEAN (0 ,"force" , &symbol_conf.force,"don't complain, do it" ), +OPT_UINTEGER (0 ,"num-thread-synthesize" , &top.nr_threads_synthesize, +"number of thread to run event synthesize" ),OPT_END () }; const char * const top_usage[] = {
tools/perf/util/event.c +4 -1
@@ -777,7 +777,10 @@ int perf_event__synthesize_threads(struct perf_tool *tool,if (n <0 )return err; - thread_nr = nr_threads_synthesize; +if (nr_threads_synthesize ==UINT_MAX ) + thread_nr =sysconf (_SC_NPROCESSORS_ONLN); +else + thread_nr = nr_threads_synthesize;if (thread_nr <=0 ) thread_nr =1 ;if (thread_nr > n)
tools/perf/util/top.h +1 -0
@@ -37,6 +37,7 @@ struct perf_top {int sym_pcnt_filter;const char *sym_filter;float min_percent; +unsigned int nr_threads_synthesize; };#define CONSOLE_CLEAR "[H[2J"
Signature · 1 line
2.5.5
RFC PATCH 10/10 perf top: switch back to overwrite mode
In reply to [email protected]From: Kan Liang <[email protected]>
perf_top__mmap_read has severe performance issue in Knights Landing/Mill, when monitoring in heavy load system. It costs several minutes to finish, which is unacceptable.
perf top was overwrite mode. But it is changed to non overwrite mode since commit 93fc64f14472 ("perf top: Switch to non overwrite mode"). For non overwrite mode, it tries to read everything in the ring buffer and does not check the messup. Once there are lots of samples delivered shortly, the processing time could be very long.
Knights Landing/Mill as a manycore processor contains a large number of small cores. Because of the huge core number, it will generated lots of samples in a heavy load system. Also, since the huge sample#, the mmap writer probably bite the tail and mess up the samples.
Switching to overwrite mode, which dropping the unsure mmap entries, significantly speeds up the whole progress.
Considering the real time requirement for perf top, it should switch back to overwrite mode.
Only warning once if the messup is detected.
Providing some hints to users.
--- tools/perf/builtin-top.c | 2 +- tools/perf/util/evlist.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-)
tools/perf/builtin-top.c +1 -1
@@ -902,7 +902,7 @@ static int perf_top__start_counters(struct perf_top *top) } } -if (perf_evlist__mmap (evlist, opts->mmap_pages, false) <0 ) { +if (perf_evlist__mmap (evlist, opts->mmap_pages, true) <0 ) {ui__error ("Failed to mmap with %d (%s)\n" , errno,str_error_r (errno, msg,sizeof (msg))); goto out_err;
tools/perf/util/evlist.c +4 -1
@@ -723,7 +723,10 @@ perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start,* In either case, truncate and restart at 'end'. */ if (diff > md->mask /2 || diff <0 ) { -fprintf (stderr,"WARNING: failed to keep up with mmap data.\n" ); +WARN_ONCE (1 ,"WARNING: failed to keep up with mmap data.\n" +"Please try increasing the period (-c) or\n" +"decreasing the freq (-F) or\n" +"limiting the number of CPUs" );/* * 'end' points to a known good entry, start there.
Signature · 1 line
2.5.5