Reference count checking for thread
Ian Rogers
reviewercover Reference count checking for thread
Add reference count checking to thread after first refactoring bits of the code, such as making the thread red-black tree non-invasive (so the thread it references is easier to reference count, rather than having 3 potential references). Part of this refactoring also removes the dead thread list because if we held a reference here the threads would never die and anything else has questionable correctness. addr_location is made into its own C/header file to capture the init, exit and copy code.
Fix additional outstanding memory leak and reference count issues to the point that "perf test" compiled with address sanitizer but without libtraceevent passes all but one test - libtraceevent reports leaks within its own code, most likely as it isn't compiled with sanitizers. The remaining failing test is "68: Test dwarf unwind" and that has address sanitizer issues as it uses memcpy to access the stack within the process - we likely want to skip parts of the test with sanitizers enabled.
Ian Rogers (20):
perf thread: Remove notion of dead threads perf thread: Make threads rbtree non-invasive perf thread: Add accessor functions for thread perf maps: Make delete static, always use put perf addr_location: Move to its own header perf addr_location: Add init/exit/copy functions perf thread: Add reference count checking
perf machine: Make delete_threads part of machine__exit perf report: Avoid thread leak
perf header: Ensure bitmaps are freed
perf stat: Avoid evlist leak
perf intel-pt: Fix missed put and leak
perf evlist: Free stats in all evlist destruction perf python: Avoid 2 leak sanitizer issues perf jit: Fix two thread leaks
perf symbol-elf: Correct holding a reference perf maps: Fix overlapping memory leak
perf machine: Fix leak of kernel dso
perf machine: Don't leak module maps
perf map/maps/thread: Changes to reference counting
tools/perf/arch/arm/tests/dwarf-unwind.c | 2 +- tools/perf/arch/arm64/tests/dwarf-unwind.c | 2 +- tools/perf/arch/powerpc/tests/dwarf-unwind.c | 2 +- tools/perf/arch/x86/tests/dwarf-unwind.c | 2 +- tools/perf/builtin-annotate.c | 28 +- tools/perf/builtin-c2c.c | 18 +- tools/perf/builtin-diff.c | 16 +- tools/perf/builtin-inject.c | 4 +- tools/perf/builtin-kmem.c | 13 +- tools/perf/builtin-kwork.c | 15 +- tools/perf/builtin-mem.c | 4 +- tools/perf/builtin-report.c | 21 +- tools/perf/builtin-sched.c | 80 +++-- tools/perf/builtin-script.c | 97 +++--- tools/perf/builtin-stat.c | 1 + tools/perf/builtin-timechart.c | 11 +- tools/perf/builtin-top.c | 8 +- tools/perf/builtin-trace.c | 38 ++- .../scripts/python/Perf-Trace-Util/Context.c | 4 +- tools/perf/tests/code-reading.c | 6 +- tools/perf/tests/dwarf-unwind.c | 1 - tools/perf/tests/hists_common.c | 2 +- tools/perf/tests/hists_cumulate.c | 18 +- tools/perf/tests/hists_filter.c | 11 +- tools/perf/tests/hists_link.c | 20 +- tools/perf/tests/hists_output.c | 12 +- tools/perf/tests/maps.c | 2 +- tools/perf/tests/mmap-thread-lookup.c | 5 +- tools/perf/tests/perf-targz-src-pkg | 5 +- tools/perf/tests/symbols.c | 1 - tools/perf/tests/thread-maps-share.c | 13 +- tools/perf/trace/beauty/pid.c | 4 +- tools/perf/ui/browsers/hists.c | 19 +- tools/perf/ui/hist.c | 5 +- tools/perf/ui/stdio/hist.c | 2 +- tools/perf/util/Build | 1 + tools/perf/util/addr_location.c | 44 +++ tools/perf/util/addr_location.h | 31 ++ tools/perf/util/arm-spe.c | 4 +- tools/perf/util/build-id.c | 2 + tools/perf/util/callchain.c | 7 +- tools/perf/util/cs-etm.c | 28 +- tools/perf/util/data-convert-json.c | 16 +- tools/perf/util/db-export.c | 20 +- tools/perf/util/dlfilter.c | 17 +- tools/perf/util/event.c | 37 +-- tools/perf/util/evlist.c | 2 + tools/perf/util/evsel_fprintf.c | 8 +- tools/perf/util/header.c | 12 +- tools/perf/util/hist.c | 22 +- tools/perf/util/intel-bts.c | 2 +- tools/perf/util/intel-pt.c | 88 +++--- tools/perf/util/jitdump.c | 12 +- tools/perf/util/machine.c | 277 +++++++++--------- tools/perf/util/map.c | 2 +- tools/perf/util/maps.c | 5 +- tools/perf/util/maps.h | 9 +- tools/perf/util/python.c | 4 + .../scripting-engines/trace-event-python.c | 28 +- tools/perf/util/session.c | 8 +- tools/perf/util/sort.c | 12 +- tools/perf/util/symbol-elf.c | 4 +- tools/perf/util/symbol.h | 17 +- tools/perf/util/thread-stack.c | 25 +- tools/perf/util/thread.c | 218 +++++++------- tools/perf/util/thread.h | 210 +++++++++++-- tools/perf/util/unwind-libdw.c | 27 +- tools/perf/util/unwind-libunwind-local.c | 19 +- tools/perf/util/unwind-libunwind.c | 2 +- tools/perf/util/vdso.c | 2 +- 70 files changed, 1059 insertions(+), 655 deletions(-) create mode 100644 tools/perf/util/addr_location.c create mode 100644 tools/perf/util/addr_location.h
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 1/20 perf thread: Remove notion of dead threads
The dead thread list is best effort. Threads live on it until the reference count hits zero and they are removed. With correct reference counting this should never happen. It is, however, part of the 'perf sched' output that is now removed. If this is an issue we should implement tracking of dead threads in a robust not best-effort way.
--- tools/perf/builtin-sched.c | 23 +---------------------- tools/perf/util/cs-etm.c | 6 ------ tools/perf/util/intel-pt.c | 8 -------- tools/perf/util/machine.c | 32 +------------------------------- tools/perf/util/thread.c | 25 +------------------------ tools/perf/util/thread.h | 11 +---------- 6 files changed, 4 insertions(+), 101 deletions(-)
tools/perf/builtin-sched.c +1 -22
@@ -2760,7 +2760,7 @@ struct total_run_stats {u64 total_run_time; }; -static int __show_thread_runtime (struct thread *t,void *priv) +static int show_thread_runtime (struct thread *t,void *priv) {struct total_run_stats *stats = priv;struct thread_runtime *r;
@@ -2783,22 +2783,6 @@ static int __show_thread_runtime(struct thread *t, void *priv)return 0 ; } -static int show_thread_runtime (struct thread *t,void *priv) -{ -if (t->dead) -return 0 ; - -return __show_thread_runtime (t, priv); -} - -static int show_deadthread_runtime (struct thread *t,void *priv) -{ -if (!t->dead) -return 0 ; - -return __show_thread_runtime (t, priv); -} -static size_t callchain__fprintf_folded (FILE *fp,struct callchain_node *node) {const char *sep =" <- " ;
@@ -2890,11 +2874,6 @@ static void timehist_print_summary(struct perf_sched *sched,if (!task_count)printf ("<no still running tasks>\n" ); -printf ("\nTerminated tasks:\n" ); -machine__for_each_thread (m, show_deadthread_runtime, &totals); -if (task_count == totals.task_count) -printf ("<no terminated tasks>\n" ); -/* CPU idle stats not tracked when samples were skipped */ if (sched->skipped_samples && !sched->idle_hist)return ;
tools/perf/util/cs-etm.c +0 -6
@@ -3292,12 +3292,6 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, goto err_free_queues; } -/* -* Initialize list node so that at thread__zput() we can avoid -* segmentation fault at list_del_init(). -*/ -INIT_LIST_HEAD (&etm->unknown_thread->node); - err =thread__set_comm (etm->unknown_thread,"unknown" ,0 );if (err) goto err_delete_thread;
tools/perf/util/intel-pt.c +0 -8
@@ -4311,14 +4311,6 @@ int intel_pt_process_auxtrace_info(union perf_event *event, goto err_free_queues; } -/* -* Since this thread will not be kept in any rbtree not in a -* list, initialize its list node so that at thread__put() the -* current thread lifetime assumption is kept and we don't segfault -* at list_del_init(). -*/ -INIT_LIST_HEAD (&pt->unknown_thread->node); - err =thread__set_comm (pt->unknown_thread,"unknown" ,0 );if (err) goto err_delete_thread;
tools/perf/util/machine.c +1 -31
@@ -241,17 +241,6 @@ void machine__exit(struct machine *machine)for (i =0 ; i <THREADS__TABLE_SIZE ; i++) {struct threads *threads = &machine->threads[i]; -struct thread *thread, *n; -/* -* Forget about the dead, at this point whatever threads were -* left in the dead lists better have a reference count taken -* by who is using them, and then, when they drop those references -* and it finally hits zero, thread__put() will check and see that -* its not in the dead threads list and will not try to remove it -* from there, just calling thread__delete() straight away. -*/ -list_for_each_entry_safe (thread, n, &threads->dead, node) -list_del_init (&thread->node);exit_rwsem (&threads->lock); }
@@ -2046,18 +2035,7 @@ static void __machine__remove_thread(struct machine *machine, struct thread *th,rb_erase_cached (&th->rb_node, &threads->entries);RB_CLEAR_NODE (&th->rb_node); --threads->nr; -/* -* Move it first to the dead_threads list, then drop the reference, -* if this is the last reference, then the thread__delete destructor -* will be called and we will remove it from the dead_threads list. -*/ -list_add_tail (&th->node, &threads->dead); -/* -* We need to do the put here because if this is the last refcount, -* then we will be touching the threads->dead head when removing the -* thread. -*/ thread__put (th);if (lock)
@@ -2145,10 +2123,8 @@ int machine__process_exit_event(struct machine *machine, union perf_event *eventif (dump_trace)perf_event__fprintf_task (event, stdout); -if (thread !=NULL ) { -thread__exited (thread); +if (thread !=NULL )thread__put (thread); - }return 0 ; }
@@ -3204,12 +3180,6 @@ int machine__for_each_thread(struct machine *machine,if (rc !=0 )return rc; } - -list_for_each_entry (thread, &threads->dead, node) { - rc =fn (thread, priv); -if (rc !=0 ) -return rc; - } }return rc; }
tools/perf/util/thread.c +1 -24
@@ -125,31 +125,8 @@ struct thread *thread__get(struct thread *thread)void thread__put (struct thread *thread) { -if (thread &&refcount_dec_and_test (&thread->refcnt)) { -/* -* Remove it from the dead threads list, as last reference is -* gone, if it is in a dead threads list. -* -* We may not be there anymore if say, the machine where it was -* stored was already deleted, so we already removed it from -* the dead threads and some other piece of code still keeps a -* reference. -* -* This is what 'perf sched' does and finally drops it in -* perf_sched__lat(), where it calls perf_sched__read_events(), -* that processes the events by creating a session and deleting -* it, which ends up destroying the list heads for the dead -* threads, but before it does that it removes all threads from -* it using list_del_init(). -* -* So we need to check here if it is in a dead threads list and -* if so, remove it before finally deleting the thread, to avoid -* an use after free situation. -*/ -if (!list_empty (&thread->node)) -list_del_init (&thread->node); +if (thread &&refcount_dec_and_test (&thread->refcnt))thread__delete (thread); - } }static struct namespaces *__thread__namespaces (const struct thread *thread)
tools/perf/util/thread.h +1 -10
@@ -30,10 +30,7 @@ struct lbr_stitch { };struct thread { -union { -struct rb_node rb_node; -struct list_head node; - }; +struct rb_node rb_node;struct maps *maps;pid_t pid_;/* Not all tools update this */ pid_t tid;
@@ -43,7 +40,6 @@ struct thread {refcount_t refcnt;bool comm_set;int comm_len; -bool dead;/* if set thread has exited */ struct list_head namespaces_list;struct rw_semaphore namespaces_lock;struct list_head comm_list;
@@ -81,11 +77,6 @@ static inline void __thread__zput(struct thread **thread)#define thread__zput (thread) __thread__zput(&thread) -static inline void thread__exited (struct thread *thread) -{ - thread->dead = true; -} -struct namespaces *thread__namespaces (struct thread *thread);int thread__set_namespaces (struct thread *thread,u64 timestamp,struct perf_record_namespaces *event);
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 2/20 perf thread: Make threads rbtree non-invasive
In reply to Ian RogersSeparate the rbtree out of thread and into a new struct thread_rb_node. The refcnt is in thread and the rbtree is responsible for a single count.
--- tools/perf/builtin-report.c | 2 +- tools/perf/builtin-trace.c | 2 +- tools/perf/util/machine.c | 101 +++++++++++++++++++++++------------- tools/perf/util/thread.c | 3 -- tools/perf/util/thread.h | 6 ++- 5 files changed, 73 insertions(+), 41 deletions(-)
tools/perf/builtin-report.c +1 -1
@@ -911,7 +911,7 @@ static int tasks_print(struct report *rep, FILE *fp) nd = rb_next(nd)) { task = tasks + itask++; - task->thread = rb_entry(nd, struct thread, rb_node); + task->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread;INIT_LIST_HEAD (&task->children);INIT_LIST_HEAD (&task->list);thread__set_priv (task->thread, task);
tools/perf/builtin-trace.c +1 -1
@@ -4348,7 +4348,7 @@ DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_eventstruct thread *thread; ) { - entry->thread = rb_entry(nd, struct thread, rb_node); + entry->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread; }static size_t trace__fprintf_thread_summary (struct trace *trace,FILE *fp)
tools/perf/util/machine.c +66 -35
182 lines not on this page · raw message
tools/perf/util/thread.c +0 -3
@@ -66,7 +66,6 @@ struct thread *thread__new(pid_t pid, pid_t tid)list_add (&comm->list, &thread->comm_list);refcount_set (&thread->refcnt,1 ); -RB_CLEAR_NODE (&thread->rb_node);/* Thread holds first ref to nsdata. */ thread->nsinfo =nsinfo__new (pid);srccode_state_init (&thread->srccode_state);
@@ -84,8 +83,6 @@ void thread__delete(struct thread *thread)struct namespaces *namespaces, *tmp_namespaces;struct comm *comm, *tmp_comm; -BUG_ON (!RB_EMPTY_NODE (&thread->rb_node)); -thread_stack__free (thread);if (thread->maps) {
tools/perf/util/thread.h +5 -1
@@ -29,8 +29,12 @@ struct lbr_stitch {struct callchain_cursor_node *prev_lbr_cursor; }; +struct thread_rb_node { +struct rb_node rb_node; +struct thread *thread; +}; +struct thread { -struct rb_node rb_node;struct maps *maps;pid_t pid_;/* Not all tools update this */ pid_t tid;
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 3/20 perf thread: Add accessor functions for thread
In reply to Ian RogersUsing accessors will make it easier to add reference count checking in later patches.
--- tools/perf/arch/arm/tests/dwarf-unwind.c | 2 +- tools/perf/arch/arm64/tests/dwarf-unwind.c | 2 +- tools/perf/arch/powerpc/tests/dwarf-unwind.c | 2 +- tools/perf/arch/x86/tests/dwarf-unwind.c | 2 +- tools/perf/builtin-c2c.c | 6 +- tools/perf/builtin-inject.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-report.c | 12 +- tools/perf/builtin-sched.c | 51 +++-- tools/perf/builtin-script.c | 20 +- tools/perf/builtin-top.c | 2 +- tools/perf/builtin-trace.c | 26 ++- .../scripts/python/Perf-Trace-Util/Context.c | 4 +- tools/perf/tests/code-reading.c | 2 +- tools/perf/tests/hists_common.c | 2 +- tools/perf/tests/hists_cumulate.c | 1 - tools/perf/tests/hists_output.c | 2 +- tools/perf/tests/perf-targz-src-pkg | 5 +- tools/perf/tests/thread-maps-share.c | 13 +- tools/perf/trace/beauty/pid.c | 4 +- tools/perf/ui/browsers/hists.c | 19 +- tools/perf/ui/stdio/hist.c | 2 +- tools/perf/util/arm-spe.c | 4 +- tools/perf/util/cs-etm.c | 2 +- tools/perf/util/data-convert-json.c | 8 +- tools/perf/util/db-export.c | 16 +- tools/perf/util/dlfilter.c | 4 +- tools/perf/util/event.c | 6 +- tools/perf/util/hist.c | 6 +- tools/perf/util/intel-bts.c | 2 +- tools/perf/util/intel-pt.c | 12 +- tools/perf/util/jitdump.c | 10 +- tools/perf/util/machine.c | 91 +++++---- tools/perf/util/map.c | 2 +- tools/perf/util/maps.c | 2 +- .../scripting-engines/trace-event-python.c | 14 +- tools/perf/util/session.c | 2 +- tools/perf/util/sort.c | 10 +- tools/perf/util/thread-stack.c | 25 +-- tools/perf/util/thread.c | 161 +++++++-------- tools/perf/util/thread.h | 188 +++++++++++++++++- tools/perf/util/unwind-libdw.c | 6 +- tools/perf/util/unwind-libunwind-local.c | 6 +- tools/perf/util/unwind-libunwind.c | 2 +- tools/perf/util/vdso.c | 2 +- 45 files changed, 485 insertions(+), 279 deletions(-)
tools/perf/arch/arm/tests/dwarf-unwind.c +1 -1
@@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, sp = (unsigned long ) regs[PERF_REG_ARM_SP ]; - map =maps__find (thread->maps, (u64 )sp); + map =maps__find (thread__maps (thread), (u64 )sp);if (!map) {pr_debug ("failed to get stack map\n" );free (buf);
tools/perf/arch/arm64/tests/dwarf-unwind.c +1 -1
@@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, sp = (unsigned long ) regs[PERF_REG_ARM64_SP ]; - map =maps__find (thread->maps, (u64 )sp); + map =maps__find (thread__maps (thread), (u64 )sp);if (!map) {pr_debug ("failed to get stack map\n" );free (buf);
tools/perf/arch/powerpc/tests/dwarf-unwind.c +1 -1
@@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, sp = (unsigned long ) regs[PERF_REG_POWERPC_R1 ]; - map =maps__find (thread->maps, (u64 )sp); + map =maps__find (thread__maps (thread), (u64 )sp);if (!map) {pr_debug ("failed to get stack map\n" );free (buf);
tools/perf/arch/x86/tests/dwarf-unwind.c +1 -1
@@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, sp = (unsigned long ) regs[PERF_REG_X86_SP ]; - map =maps__find (thread->maps, (u64 )sp); + map =maps__find (thread__maps (thread), (u64 )sp);if (!map) {pr_debug ("failed to get stack map\n" );free (buf);
tools/perf/builtin-c2c.c +3 -3
@@ -293,7 +293,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused, }if (c2c.stitch_lbr) - al.thread->lbr_stitch_enable = true; +thread__set_lbr_stitch_enable (al.thread, true); ret =sample__resolve_callchain (sample, &callchain_cursor,NULL , evsel, &al, sysctl_perf_event_max_stack);
@@ -1149,14 +1149,14 @@ pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, {int width =c2c_width (fmt, hpp, he->hists); -return scnprintf (hpp->buf, hpp->size,"%*d" , width, he->thread->pid_); +return scnprintf (hpp->buf, hpp->size,"%*d" , width,thread__pid (he->thread)); }static int64_t pid_cmp (struct perf_hpp_fmt *fmt__maybe_unused ,struct hist_entry *left,struct hist_entry *right) { -return left->thread->pid_ - right->thread->pid_; +return thread__pid (left->thread) -thread__pid (right->thread); } static int64_t
tools/perf/builtin-inject.c +1 -1
@@ -417,7 +417,7 @@ static struct dso *findnew_dso(int pid, int tid, const char *filename, } vdso =is_vdso_map (filename); - nsi =nsinfo__get (thread->nsinfo); + nsi =nsinfo__get (thread__nsinfo (thread));if (vdso) {/* The vdso maps are always on the host and not the
tools/perf/builtin-kmem.c +1 -1
@@ -964,7 +964,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,if (perf_kmem__skip_sample (sample))return 0 ; -dump_printf (" ... thread: %s:%d\n" ,thread__comm_str (thread), thread->tid); +dump_printf (" ... thread: %s:%d\n" ,thread__comm_str (thread),thread__tid (thread));if (evsel->handler !=NULL ) {tracepoint_handler f = evsel->handler;
tools/perf/builtin-report.c +6 -6
@@ -292,7 +292,7 @@ static int process_sample_event(struct perf_tool *tool, }if (rep->stitch_lbr) - al.thread->lbr_stitch_enable = true; +thread__set_lbr_stitch_enable (al.thread, true);if (symbol_conf.hide_unresolved && al.sym ==NULL ) goto out_put;
@@ -829,10 +829,10 @@ static struct task *tasks_list(struct task *task, struct machine *machine)return NULL ;/* Last one in the chain. */ -if (thread->ppid ==-1 ) +if (thread__ppid (thread) ==-1 )return task; - parent_thread =machine__find_thread (machine,-1 , thread->ppid); + parent_thread =machine__find_thread (machine,-1 ,thread__ppid (thread));if (!parent_thread)return ERR_PTR (-ENOENT );
@@ -869,12 +869,12 @@ static void task__print_level(struct task *task, FILE *fp, int level)struct thread *thread = task->thread;struct task *child;int comm_indent =fprintf (fp," %8d %8d %8d |%*s" , - thread->pid_, thread->tid, thread->ppid, - level,"" ); +thread__pid (thread),thread__tid (thread), +thread__ppid (thread), level,"" );fprintf (fp,"%s\n" ,thread__comm_str (thread)); -maps__fprintf_task (thread->maps, comm_indent, fp); +maps__fprintf_task (thread__maps (thread), comm_indent, fp);if (!list_empty (&task->children)) {list_for_each_entry (child, &task->children, list)
tools/perf/builtin-sched.c +29 -22
140 lines not on this page · raw message
tools/perf/builtin-script.c +10 -10
@@ -1142,7 +1142,7 @@ static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)if (!al.map)return 0 ; ret =map__fprintf_srccode (al.map, al.addr, stdout, - &thread->srccode_state); +thread__srccode_state (thread));if (ret) ret +=printf ("\n" );return ret;
@@ -1439,7 +1439,7 @@ static int perf_sample__fprintf_callindent(struct perf_sample *sample,* The 'return' has already been popped off the stack so the depth has * to be adjusted to match the 'call'. */ -if (thread->ts && sample->flags &PERF_IP_FLAG_RETURN ) +if (thread__ts (thread) && sample->flags &PERF_IP_FLAG_RETURN ) depth +=1 ; name =resolve_branch_sym (sample, evsel, thread, al, addr_al, &ip);
@@ -1577,7 +1577,7 @@ static int perf_sample__fprintf_bts(struct perf_sample *sample, printed +=fprintf (fp,"\n" );if (PRINT_FIELD (SRCCODE )) {int ret =map__fprintf_srccode (al->map, al->addr, stdout, - &thread->srccode_state); +thread__srccode_state (thread));if (ret) { printed += ret; printed +=printf ("\n" );
@@ -2086,9 +2086,9 @@ static bool show_event(struct perf_sample *sample,if (!symbol_conf.graph_function)return true; -if (thread->filter) { -if (depth <= thread->filter_entry_depth) { - thread->filter = false; +if (thread__filter (thread)) { +if (depth <=thread__filter_entry_depth (thread)) { +thread__set_filter (thread, false);return false; }return true;
@@ -2105,8 +2105,8 @@ static bool show_event(struct perf_sample *sample,while (*s) {unsigned len =strcspn (s,"," );if (nlen == len && !strncmp (name, s, len)) { - thread->filter = true; - thread->filter_entry_depth = depth; +thread__set_filter (thread, true); +thread__set_filter_entry_depth (thread, depth);return true; } s += len;
@@ -2186,7 +2186,7 @@ static void process_event(struct perf_script *script,struct callchain_cursor *cursor =NULL ;if (script->stitch_lbr) - al->thread->lbr_stitch_enable = true; +thread__set_lbr_stitch_enable (al->thread, true);if (symbol_conf.use_callchain && sample->callchain && thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
@@ -2241,7 +2241,7 @@ static void process_event(struct perf_script *script,if (PRINT_FIELD (SRCCODE )) {if (map__fprintf_srccode (al->map, al->addr, stdout, - &thread->srccode_state)) +thread__srccode_state (thread)))printf ("\n" ); }
tools/perf/builtin-top.c +1 -1
@@ -777,7 +777,7 @@ static void perf_event__process_sample(struct perf_tool *tool,return ;if (top->stitch_lbr) - al.thread->lbr_stitch_enable = true; +thread__set_lbr_stitch_enable (al.thread, true); if (!machine->kptr_restrict_warned && symbol_conf.kptr_restrict &&
tools/perf/builtin-trace.c +16 -10
@@ -1386,12 +1386,13 @@ static int thread__read_fd_path(struct thread *thread, int fd)struct stat st;int ret; -if (thread->pid_ == thread->tid) { +if (thread__pid (thread) ==thread__tid (thread)) {scnprintf (linkname,sizeof (linkname), -"/proc/%d/fd/%d" , thread->pid_, fd); +"/proc/%d/fd/%d" ,thread__pid (thread), fd); }else {scnprintf (linkname,sizeof (linkname), -"/proc/%d/task/%d/fd/%d" , thread->pid_, thread->tid, fd); +"/proc/%d/task/%d/fd/%d" , +thread__pid (thread),thread__tid (thread), fd); }if (lstat (linkname, &st) <0 || st.st_size +1 > (off_t )sizeof (pathname))
@@ -1559,7 +1560,7 @@ static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *threadif (trace->multiple_threads) {if (trace->show_comm) printed +=fprintf (fp,"%.14s/" ,thread__comm_str (thread)); - printed +=fprintf (fp,"%d " , thread->tid); + printed +=fprintf (fp,"%d " ,thread__tid (thread)); }return printed;
@@ -2205,7 +2206,8 @@ static void thread__update_stats(struct thread *thread, struct thread_trace *ttrmemset (new_errnos + stats->max_errno,0 , (err - stats->max_errno) *sizeof (u32)); }else {pr_debug ("Not enough memory for errno stats for thread \"%s\"(%d/%d), results will be incomplete\n" , -thread__comm_str (thread), thread->pid_, thread->tid); +thread__comm_str (thread),thread__pid (thread), +thread__tid (thread));return ; }
@@ -2550,7 +2552,7 @@ errno_print: {if (child !=NULL ) {fprintf (trace->output,"%ld" , ret); -if (child->comm_set) +if (thread__comm_set (child))fprintf (trace->output," (%s)" ,thread__comm_str (child));thread__put (child); }
@@ -3616,14 +3618,16 @@ static int trace__set_filter_loop_pids(struct trace *trace)struct thread *thread =machine__find_thread (trace->host, pids[0 ], pids[0 ]);while (thread && nr <ARRAY_SIZE (pids)) { -struct thread *parent =machine__find_thread (trace->host, thread->ppid, thread->ppid); +struct thread *parent =machine__find_thread (trace->host, +thread__ppid (thread), +thread__ppid (thread));if (parent ==NULL )break ;if (!strcmp (thread__comm_str (parent),"sshd" ) ||strstarts (thread__comm_str (parent),"gnome-terminal" )) { - pids[nr++] = parent->tid; + pids[nr++] =thread__tid (parent);break ; } thread = parent;
@@ -4322,7 +4326,7 @@ static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trac ratio = (double )ttrace->nr_events / trace->nr_events *100.0 ; - printed +=fprintf (fp," %s (%d), " ,thread__comm_str (thread), thread->tid); + printed +=fprintf (fp," %s (%d), " ,thread__comm_str (thread),thread__tid (thread)); printed +=fprintf (fp,"%lu events, " , ttrace->nr_events); printed +=fprintf (fp,"%.1f%%" , ratio);if (ttrace->pfmaj)
@@ -4344,7 +4348,9 @@ static unsigned long thread__nr_events(struct thread_trace *ttrace)return ttrace ? ttrace->nr_events :0 ; } -DEFINE_RESORT_RB (threads, (thread__nr_events (a->thread->priv) <thread__nr_events (b->thread->priv)), +DEFINE_RESORT_RB (threads, + (thread__nr_events (thread__priv (a->thread)) < +thread__nr_events (thread__priv (b->thread))), struct thread *thread; ) {
tools/perf/scripts/python/Perf-Trace-Util/Context.c +2 -2
@@ -100,8 +100,8 @@ static PyObject *perf_sample_insn(PyObject *obj, PyObject *args)if (!c)return NULL ; -if (c->sample->ip && !c->sample->insn_len && c->al->thread->maps) { -struct machine *machine =maps__machine (c->al->thread->maps); +if (c->sample->ip && !c->sample->insn_len &&thread__maps (c->al->thread)) { +struct machine *machine =maps__machine (thread__maps (c->al->thread));script_fetch_insn (c->sample, c->al->thread, machine); }
tools/perf/tests/code-reading.c +1 -1
@@ -269,7 +269,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode, len =map__end (al.map) - addr;/* Read the object code using perf */ - ret_len =dso__data_read_offset (dso,maps__machine (thread->maps), + ret_len =dso__data_read_offset (dso,maps__machine (thread__maps (thread)), al.addr, buf1, len);if (ret_len != len) {pr_debug ("dso__data_read_offset failed\n" );
tools/perf/tests/hists_common.c +1 -1
@@ -211,7 +211,7 @@ void print_hists_out(struct hists *hists)struct dso *dso =map__dso (he->ms.map);pr_info ("%2d: entry: %8s:%5d [%-8s] %20s: period = %" PRIu64"/%" PRIu64"\n" , - i,thread__comm_str (he->thread), he->thread->tid, + i,thread__comm_str (he->thread),thread__tid (he->thread), dso->short_name, he->ms.sym->name, he->stat.period, he->stat_acc ? he->stat_acc->period :0 );
tools/perf/tests/hists_cumulate.c +0 -1
@@ -162,7 +162,6 @@ typedef int (*test_fn_t)(struct evsel *, struct machine *);#define DSO (he) (map__dso(he->ms.map)->short_name)#define SYM (he) (he->ms.sym->name)#define CPU (he) (he->cpu) -#define PID (he) (he->thread->tid)#define DEPTH (he) (he->callchain->max_depth)#define CDSO (cl) (map__dso(cl->ms.map)->short_name)#define CSYM (cl) (cl->ms.sym->name)
tools/perf/tests/hists_output.c +1 -1
@@ -128,7 +128,7 @@ typedef int (*test_fn_t)(struct evsel *, struct machine *);#define DSO (he) (map__dso(he->ms.map)->short_name)#define SYM (he) (he->ms.sym->name)#define CPU (he) (he->cpu) -#define PID (he) (he->thread->tid) +#define PID (he) (thread__tid(he->thread))/* default sort keys (no field) */ static int test1 (struct evsel *evsel,struct machine *machine)
tools/perf/tests/perf-targz-src-pkg +3 -2
@@ -7,16 +7,17 @@ # be in such tarball, which sometimes gets broken when we move files around, # like when we made some files that were in tools/perf/ available to other tools/ # codebases by moving it to tools/include/, etc. +set -e PERF=$1 cd ${PERF}/../.. -make perf-targz-src-pkg > /dev/null +make perf-targz-src-pkg TARBALL=$(ls -rt perf-*.tar.gz) TMP_DEST=$(mktemp -d) tar xf ${TARBALL} -C $TMP_DEST rm -f ${TARBALL} cd - > /dev/null -make -C $TMP_DEST/perf*/tools/perf > /dev/null +make -C $TMP_DEST/perf*/tools/perf RC=$? rm -rf ${TMP_DEST} exit $RC
tools/perf/tests/thread-maps-share.c +7 -6
@@ -42,13 +42,13 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int sTEST_ASSERT_VAL ("failed to create threads" , leader && t1 && t2 && t3 && other); - maps = leader->maps; + maps =thread__maps (leader);TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (maps__refcnt (maps)),4 );/* test the maps pointer is shared */ -TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (t1->maps)); -TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (t2->maps)); -TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (t3->maps)); +TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (thread__maps (t1))); +TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (thread__maps (t2))); +TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (thread__maps (t3)));/* * Verify the other leader was created by previous call.
@@ -70,10 +70,11 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int smachine__remove_thread (machine, other);machine__remove_thread (machine, other_leader); - other_maps = other->maps; + other_maps =thread__maps (other);TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (maps__refcnt (other_maps)),2 ); -TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (other_maps) ==RC_CHK_ACCESS (other_leader->maps)); +TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (other_maps) == +RC_CHK_ACCESS (thread__maps (other_leader)));/* release thread group */ thread__put (t3);
tools/perf/trace/beauty/pid.c +2 -2
@@ -8,10 +8,10 @@ size_t syscall_arg__scnprintf_pid(char *bf, size_t size, struct syscall_arg *argstruct thread *thread =machine__findnew_thread (trace->host, pid, pid);if (thread !=NULL ) { -if (!thread->comm_set) +if (!thread__comm_set (thread))thread__set_comm_from_proc (thread); -if (thread->comm_set) +if (thread__comm_set (thread)) printed +=scnprintf (bf + printed, size - printed," (%s)" ,thread__comm_str (thread));thread__put (thread);
tools/perf/ui/browsers/hists.c +10 -9
@@ -2533,13 +2533,15 @@ do_zoom_thread(struct hist_browser *browser, struct popup_action *act)thread__zput (browser->hists->thread_filter);ui_helpline__pop (); }else { +const char *comm_set_str = +thread__comm_set (thread) ?thread__comm_str (thread) :"" ; +if (hists__has (browser->hists, thread)) {ui_helpline__fpush ("To zoom out press ESC or ENTER + \"Zoom out of %s(%d) thread\"" , - thread->comm_set ?thread__comm_str (thread) :"" , - thread->tid); + comm_set_str,thread__tid (thread)); }else {ui_helpline__fpush ("To zoom out press ESC or ENTER + \"Zoom out of %s thread\"" , - thread->comm_set ?thread__comm_str (thread) :"" ); + comm_set_str); } browser->hists->thread_filter =thread__get (thread);
@@ -2557,20 +2559,19 @@ add_thread_opt(struct hist_browser *browser, struct popup_action *act, char **optstr, struct thread *thread) { int ret; +const char *comm_set_str, *in_out;if ((!hists__has (browser->hists, thread) && !hists__has (browser->hists, comm)) || thread ==NULL )return 0 ; + in_out = browser->hists->thread_filter ?"out of" :"into" ; + comm_set_str =thread__comm_set (thread) ?thread__comm_str (thread) :"" ;if (hists__has (browser->hists, thread)) { ret =asprintf (optstr,"Zoom %s %s(%d) thread" , - browser->hists->thread_filter ?"out of" :"into" , - thread->comm_set ?thread__comm_str (thread) :"" , - thread->tid); + in_out, comm_set_str,thread__tid (thread)); }else { - ret =asprintf (optstr,"Zoom %s %s thread" , - browser->hists->thread_filter ?"out of" :"into" , - thread->comm_set ?thread__comm_str (thread) :"" ); + ret =asprintf (optstr,"Zoom %s %s thread" , in_out, comm_set_str); }if (ret <0 )return 0 ;
tools/perf/ui/stdio/hist.c +1 -1
@@ -885,7 +885,7 @@ size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows, }if (h->ms.map ==NULL && verbose >1 ) { -maps__fprintf (h->thread->maps, fp); +maps__fprintf (thread__maps (h->thread), fp);fprintf (fp,"%.10s end\n" , graph_dotted_line); } }
tools/perf/util/arm-spe.c +2 -2
@@ -254,9 +254,9 @@ static void arm_spe_set_pid_tid_cpu(struct arm_spe *spe, }if (speq->thread) { - speq->pid = speq->thread->pid_; + speq->pid =thread__pid (speq->thread);if (queue->cpu ==-1 ) - speq->cpu = speq->thread->cpu; + speq->cpu =thread__cpu (speq->thread); } }
tools/perf/util/cs-etm.c +1 -1
@@ -1311,7 +1311,7 @@ static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm, tidq->tid);if (tidq->thread) - tidq->pid = tidq->thread->pid_; + tidq->pid =thread__pid (tidq->thread); } int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
tools/perf/util/data-convert-json.c +4 -4
@@ -172,13 +172,13 @@ static int process_sample_event(struct perf_tool *tool,output_json_format (out, false,2 ,"{" );output_json_key_format (out, false,3 ,"timestamp" ,"%" PRIi64, sample->time); -output_json_key_format (out, true,3 ,"pid" ,"%i" , al.thread->pid_); -output_json_key_format (out, true,3 ,"tid" ,"%i" , al.thread->tid); +output_json_key_format (out, true,3 ,"pid" ,"%i" ,thread__pid (al.thread)); +output_json_key_format (out, true,3 ,"tid" ,"%i" ,thread__tid (al.thread));if ((sample_type &PERF_SAMPLE_CPU ))output_json_key_format (out, true,3 ,"cpu" ,"%i" , sample->cpu); -else if (al.thread->cpu >=0 ) -output_json_key_format (out, true,3 ,"cpu" ,"%i" , al.thread->cpu); +else if (thread__cpu (al.thread) >=0 ) +output_json_key_format (out, true,3 ,"cpu" ,"%i" ,thread__cpu (al.thread));output_json_key_string (out, true,3 ,"comm" ,thread__comm_str (al.thread));
tools/perf/util/db-export.c +8 -8
@@ -64,13 +64,13 @@ int db_export__thread(struct db_export *dbe, struct thread *thread, {u64 main_thread_db_id =0 ; -if (thread->db_id) +if (thread__db_id (thread))return 0 ; - thread->db_id = ++dbe->thread_last_db_id; +thread__set_db_id (thread, ++dbe->thread_last_db_id);if (main_thread) - main_thread_db_id = main_thread->db_id; + main_thread_db_id =thread__db_id (main_thread);if (dbe->export_thread) return dbe->export_thread(dbe, thread, main_thread_db_id,
@@ -251,7 +251,7 @@ static struct call_path *call_path_from_sample(struct db_export *dbe,*/ al.sym = node->ms.sym; al.map = node->ms.map; - al.maps = thread->maps; + al.maps =thread__maps (thread); al.addr = node->ip;if (al.map && !al.sym)
@@ -321,7 +321,7 @@ static int db_export__threads(struct db_export *dbe, struct thread *thread,* For a non-main thread, db_export__comm_thread() must be * called only if thread has not previously been exported. */ -bool export_comm_thread = comm && !thread->db_id; +bool export_comm_thread = comm && !thread__db_id (thread); err =db_export__thread (dbe, thread, machine, main_thread);if (err)
@@ -529,16 +529,16 @@ static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,struct thread *main_thread;int err =0 ; -if (!thread || !thread->comm_set) +if (!thread || !thread__comm_set (thread)) goto out_put; - *is_idle = !thread->pid_ && !thread->tid; + *is_idle = !thread__pid (thread) && !thread__tid (thread); main_thread =thread__main_thread (machine, thread); err =db_export__threads (dbe, thread, main_thread, machine, comm_ptr); - *db_id = thread->db_id; + *db_id =thread__db_id (thread);thread__put (main_thread); out_put:
tools/perf/util/dlfilter.c +2 -2
@@ -197,8 +197,8 @@ static const __u8 *dlfilter__insn(void *ctx, __u32 *len)if (!al->thread &&machine__resolve (d->machine, al, d->sample) <0 )return NULL ; -if (al->thread->maps) { -struct machine *machine =maps__machine (al->thread->maps); +if (thread__maps (al->thread)) { +struct machine *machine =maps__machine (thread__maps (al->thread));if (machine)script_fetch_insn (d->sample, al->thread, machine);
tools/perf/util/event.c +3 -3
@@ -573,7 +573,7 @@ int perf_event__process(struct perf_tool *tool __maybe_unused,struct map *thread__find_map (struct thread *thread,u8 cpumode,u64 addr,struct addr_location *al) { -struct maps *maps = thread->maps; +struct maps *maps =thread__maps (thread);struct machine *machine =maps__machine (maps);bool load_map = false;
@@ -639,7 +639,7 @@ struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr, struct addr_location *al) {struct map *map =thread__find_map (thread, cpumode, addr, al); -struct machine *machine =maps__machine (thread->maps); +struct machine *machine =maps__machine (thread__maps (thread));u8 addr_cpumode =machine__addr_cpumode (machine, cpumode, addr);if (map || addr_cpumode == cpumode)
@@ -696,7 +696,7 @@ int machine__resolve(struct machine *machine, struct addr_location *al,if (thread ==NULL )return -1 ; -dump_printf (" ... thread: %s:%d\n" ,thread__comm_str (thread), thread->tid); +dump_printf (" ... thread: %s:%d\n" ,thread__comm_str (thread),thread__tid (thread));thread__find_map (thread, sample->cpumode, sample->ip, al); dso = al->map ?map__dso (al->map) :NULL ; dump_printf(" ...... dso: %s\n",
tools/perf/util/hist.c +3 -3
@@ -2778,12 +2778,12 @@ int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool shif (hists__has (hists, thread)) { printed +=scnprintf (bf + printed, size - printed,", Thread: %s(%d)" , - (thread->comm_set ?thread__comm_str (thread) :"" ), - thread->tid); + (thread__comm_set (thread) ?thread__comm_str (thread) :"" ), +thread__tid (thread)); }else { printed +=scnprintf (bf + printed, size - printed,", Thread: %s" , - (thread->comm_set ?thread__comm_str (thread) :"" )); + (thread__comm_set (thread) ?thread__comm_str (thread) :"" )); } }if (dso)
tools/perf/util/intel-bts.c +1 -1
@@ -456,7 +456,7 @@ static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp) thread =machine__find_thread (btsq->bts->machine,-1 , btsq->tid);if (thread) - btsq->pid = thread->pid_; + btsq->pid =thread__pid (thread); }else { thread =machine__findnew_thread (btsq->bts->machine, btsq->pid, btsq->tid);
tools/perf/util/intel-pt.c +6 -6
@@ -1428,13 +1428,13 @@ static int intel_pt_get_guest_from_sideband(struct intel_pt_queue *ptq) ptq->guest_machine = machine; } - vcpu = ptq->thread ? ptq->thread->guest_cpu :-1 ; + vcpu = ptq->thread ?thread__guest_cpu (ptq->thread) :-1 ;if (vcpu <0 )return -1 ; tid =machine__get_current_tid (machine, vcpu); -if (ptq->guest_thread && ptq->guest_thread->tid != tid) +if (ptq->guest_thread &&thread__tid (ptq->guest_thread) != tid)thread__zput (ptq->guest_thread);if (!ptq->guest_thread) {
@@ -1444,7 +1444,7 @@ static int intel_pt_get_guest_from_sideband(struct intel_pt_queue *ptq) } ptq->guest_machine_pid = machine_pid; - ptq->guest_pid = ptq->guest_thread->pid_; + ptq->guest_pid =thread__pid (ptq->guest_thread); ptq->guest_tid = tid; ptq->vcpu = vcpu;
@@ -1467,9 +1467,9 @@ static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt, ptq->thread =machine__find_thread (pt->machine,-1 , ptq->tid);if (ptq->thread) { - ptq->pid = ptq->thread->pid_; + ptq->pid =thread__pid (ptq->thread);if (queue->cpu ==-1 ) - ptq->cpu = ptq->thread->cpu; + ptq->cpu =thread__cpu (ptq->thread); }if (pt->have_guest_sideband &&intel_pt_get_guest_from_sideband (ptq)) {
@@ -3074,7 +3074,7 @@ static void intel_pt_sample_set_pid_tid_cpu(struct intel_pt_queue *ptq,if (ptq->pid ==-1 ) { ptq->thread =machine__find_thread (m,-1 , ptq->tid);if (ptq->thread) - ptq->pid = ptq->thread->pid_; + ptq->pid =thread__pid (ptq->thread);return ; }
tools/perf/util/jitdump.c +6 -4
@@ -799,17 +799,19 @@ static void jit_add_pid(struct machine *machine, pid_t pid)return ; } - thread->priv = (void *)1 ; +thread__set_priv (thread, (void *)true); }static bool jit_has_pid (struct machine *machine,pid_t pid) {struct thread *thread =machine__find_thread (machine, pid, pid); +void *priv;if (!thread) -return 0 ; +return false; -return (bool )thread->priv; + priv =thread__priv (thread); +return (bool )priv; }int
@@ -833,7 +835,7 @@ jit_process(struct perf_session *session,return 0 ; } - nsi =nsinfo__get (thread->nsinfo); + nsi =nsinfo__get (thread__nsinfo (thread));thread__put (thread);/*
tools/perf/util/machine.c +47 -44
254 lines not on this page · raw message
tools/perf/util/map.c +1 -1
@@ -137,7 +137,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len, no_dso =is_no_dso_memory (filename); map->prot = prot; map->flags = flags; - nsi =nsinfo__get (thread->nsinfo); + nsi =nsinfo__get (thread__nsinfo (thread));if ((anon || no_dso) && nsi && (prot &PROT_EXEC )) { snprintf(newfilename, sizeof(newfilename),
tools/perf/util/maps.c +1 -1
@@ -384,7 +384,7 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)*/ int maps__clone (struct thread *thread,struct maps *parent) { -struct maps *maps = thread->maps; +struct maps *maps =thread__maps (thread);int err;struct map_rb_node *rb_node;
tools/perf/util/scripting-engines/trace-event-python.c +7 -7
@@ -1163,11 +1163,11 @@ static int python_export_thread(struct db_export *dbe, struct thread *thread, t =tuple_new (5 ); -tuple_set_d64 (t,0 , thread->db_id); +tuple_set_d64 (t,0 ,thread__db_id (thread));tuple_set_d64 (t,1 , machine->db_id);tuple_set_d64 (t,2 , main_thread_db_id); -tuple_set_s32 (t,3 , thread->pid_); -tuple_set_s32 (t,4 , thread->tid); +tuple_set_s32 (t,3 ,thread__pid (thread)); +tuple_set_s32 (t,4 ,thread__tid (thread));call_object (tables->thread_handler, t,"thread_table" );
@@ -1186,7 +1186,7 @@ static int python_export_comm(struct db_export *dbe, struct comm *comm,tuple_set_d64 (t,0 , comm->db_id);tuple_set_string (t,1 ,comm__str (comm)); -tuple_set_d64 (t,2 , thread->db_id); +tuple_set_d64 (t,2 ,thread__db_id (thread));tuple_set_d64 (t,3 , comm->start);tuple_set_s32 (t,4 , comm->exec);
@@ -1207,7 +1207,7 @@ static int python_export_comm_thread(struct db_export *dbe, u64 db_id,tuple_set_d64 (t,0 , db_id);tuple_set_d64 (t,1 , comm->db_id); -tuple_set_d64 (t,2 , thread->db_id); +tuple_set_d64 (t,2 ,thread__db_id (thread));call_object (tables->comm_thread_handler, t,"comm_thread_table" );
@@ -1292,7 +1292,7 @@ static void python_export_sample_table(struct db_export *dbe,tuple_set_d64 (t,0 , es->db_id);tuple_set_d64 (t,1 , es->evsel->db_id);tuple_set_d64 (t,2 ,maps__machine (es->al->maps)->db_id); -tuple_set_d64 (t,3 , es->al->thread->db_id); +tuple_set_d64 (t,3 ,thread__db_id (es->al->thread));tuple_set_d64 (t,4 , es->comm_db_id);tuple_set_d64 (t,5 , es->dso_db_id);tuple_set_d64 (t,6 , es->sym_db_id);
@@ -1382,7 +1382,7 @@ static int python_export_call_return(struct db_export *dbe, t =tuple_new (14 );tuple_set_d64 (t,0 , cr->db_id); -tuple_set_d64 (t,1 , cr->thread->db_id); +tuple_set_d64 (t,1 ,thread__db_id (cr->thread));tuple_set_d64 (t,2 , comm_db_id);tuple_set_d64 (t,3 , cr->cp->db_id);tuple_set_d64 (t,4 , cr->call_time);
tools/perf/util/session.c +1 -1
@@ -2807,7 +2807,7 @@ static int perf_session__set_guest_cpu(struct perf_session *session, pid_t pid,if (!thread)return -ENOMEM ; - thread->guest_cpu = guest_cpu; +thread__set_guest_cpu (thread, guest_cpu);thread__put (thread);return 0 ;
tools/perf/util/sort.c +6 -4
@@ -108,7 +108,7 @@ static int64_t cmp_null(const void *l, const void *r)static int64_t sort__thread_cmp (struct hist_entry *left,struct hist_entry *right) { -return right->thread->tid - left->thread->tid; +return thread__tid (right->thread) -thread__tid (left->thread); } static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
@@ -117,7 +117,7 @@ static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,const char *comm =thread__comm_str (he->thread); width =max (7U , width) -8 ; -return repsep_snprintf (bf, size,"%7d:%-*.*s" , he->thread->tid, +return repsep_snprintf (bf, size,"%7d:%-*.*s" ,thread__tid (he->thread), width, width, comm ?:"" ); }
@@ -1543,8 +1543,10 @@ sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right) !l_dso->id.ino && !l_dso->id.ino_generation) {/* userspace anonymous */ -if (left->thread->pid_ > right->thread->pid_)return -1 ; -if (left->thread->pid_ < right->thread->pid_)return 1 ; +if (thread__pid (left->thread) >thread__pid (right->thread)) +return -1 ; +if (thread__pid (left->thread) <thread__pid (right->thread)) +return 1 ; } addr:
tools/perf/util/thread-stack.c +13 -12
@@ -112,7 +112,7 @@ struct thread_stack {*/ static inline bool thread_stack__per_cpu (struct thread *thread) { -return !(thread->tid || thread->pid_); +return !(thread__tid (thread) ||thread__pid (thread)); }static int thread_stack__grow (struct thread_stack *ts)
@@ -155,8 +155,8 @@ static int thread_stack__init(struct thread_stack *ts, struct thread *thread, ts->br_stack_sz = br_stack_sz; } -if (thread->maps &&maps__machine (thread->maps)) { -struct machine *machine =maps__machine (thread->maps); +if (thread__maps (thread) &&maps__machine (thread__maps (thread))) { +struct machine *machine =maps__machine (thread__maps (thread));const char *arch =perf_env__arch (machine->env); ts->kernel_start =machine__kernel_start (machine);
@@ -175,7 +175,7 @@ static struct thread_stack *thread_stack__new(struct thread *thread, int cpu,bool callstack, unsigned int br_stack_sz) { - struct thread_stack *ts = thread->ts, *new_ts; + struct thread_stack *ts = thread__ts(thread), *new_ts;unsigned int old_sz = ts ? ts->arr_sz :0 ;unsigned int new_sz =1 ;
@@ -189,8 +189,8 @@ static struct thread_stack *thread_stack__new(struct thread *thread, int cpu,if (ts)memcpy (new_ts, ts, old_sz *sizeof (*ts)); new_ts->arr_sz = new_sz; -zfree (&thread->ts); - thread->ts = new_ts; +free (thread__ts (thread)); +thread__set_ts (thread, new_ts); ts = new_ts; }
@@ -207,7 +207,7 @@ static struct thread_stack *thread_stack__new(struct thread *thread, int cpu,static struct thread_stack *thread__cpu_stack (struct thread *thread,int cpu) { -struct thread_stack *ts = thread->ts; +struct thread_stack *ts =thread__ts (thread);if (cpu <0 ) cpu =0 ;
@@ -232,7 +232,7 @@ static inline struct thread_stack *thread__stack(struct thread *thread,if (thread_stack__per_cpu (thread))return thread__cpu_stack (thread, cpu); -return thread->ts; +return thread__ts (thread); } static int thread_stack__push(struct thread_stack *ts, u64 ret_addr,
@@ -363,7 +363,7 @@ static int __thread_stack__flush(struct thread *thread, struct thread_stack *ts)int thread_stack__flush (struct thread *thread) { -struct thread_stack *ts = thread->ts; +struct thread_stack *ts =thread__ts (thread);unsigned int pos;int err =0 ;
@@ -502,13 +502,14 @@ static void thread_stack__reset(struct thread *thread, struct thread_stack *ts)void thread_stack__free (struct thread *thread) { -struct thread_stack *ts = thread->ts; +struct thread_stack *ts =thread__ts (thread);unsigned int pos;if (ts) {for (pos =0 ; pos < ts->arr_sz; pos++)__thread_stack__free (thread, ts + pos); -zfree (&thread->ts); +free (thread__ts (thread)); +thread__set_ts (thread,NULL ); } }
@@ -1127,7 +1128,7 @@ int thread_stack__process(struct thread *thread, struct comm *comm, ts->rstate =X86_RETPOLINE_POSSIBLE ;/* Flush stack on exec */ -if (ts->comm != comm && thread->pid_ == thread->tid) { +if (ts->comm != comm &&thread__pid (thread) ==thread__tid (thread)) { err =__thread_stack__flush (thread, ts);if (err)return err;
tools/perf/util/thread.c +82 -79
389 lines not on this page · raw message
tools/perf/util/thread.h +184 -4
216 lines not on this page · raw message
tools/perf/util/unwind-libdw.c +3 -3
@@ -230,7 +230,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,struct unwind_info *ui, ui_buf = { .sample = data, .thread = thread, - .machine =RC_CHK_ACCESS (thread->maps)->machine, + .machine =RC_CHK_ACCESS (thread__maps (thread))->machine, .cb = cb, .arg = arg, .max_stack = max_stack,
@@ -260,11 +260,11 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,if (err) goto out; - err = !dwfl_attach_state (ui->dwfl,EM_NONE , thread->tid, &callbacks, ui); + err = !dwfl_attach_state (ui->dwfl,EM_NONE ,thread__tid (thread), &callbacks, ui);if (err) goto out; - err =dwfl_getthread_frames (ui->dwfl, thread->tid, frame_callback, ui); + err =dwfl_getthread_frames (ui->dwfl,thread__tid (thread), frame_callback, ui);if (err && ui->max_stack != max_stack) err =0 ;
tools/perf/util/unwind-libunwind-local.c +3 -3
@@ -325,7 +325,7 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui,return -EINVAL ; } -maps__for_each_entry (ui->thread->maps, map_node) { +maps__for_each_entry (thread__maps (ui->thread), map_node) {struct map *map = map_node->map;u64 start =map__start (map);
@@ -719,7 +719,7 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,*/ if (max_stack -1 >0 ) {WARN_ONCE (!ui->thread,"WARNING: ui->thread is NULL" ); - addr_space =maps__addr_space (ui->thread->maps); + addr_space =maps__addr_space (thread__maps (ui->thread));if (addr_space ==NULL )return -1 ;
@@ -769,7 +769,7 @@ static int _unwind__get_entries(unwind_entry_cb_t cb, void *arg,struct unwind_info ui = { .sample = data, .thread = thread, - .machine =maps__machine (thread->maps), + .machine =maps__machine (thread__maps (thread)), .best_effort = best_effort };
tools/perf/util/unwind-libunwind.c +1 -1
@@ -89,7 +89,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg, struct perf_sample *data, int max_stack, bool best_effort) { - const struct unwind_libunwind_ops *ops = maps__unwind_libunwind_ops(thread->maps); + const struct unwind_libunwind_ops *ops = maps__unwind_libunwind_ops(thread__maps(thread));if (ops)return ops->get_entries (cb, arg, thread, data, max_stack, best_effort);
tools/perf/util/vdso.c +1 -1
@@ -146,7 +146,7 @@ static enum dso_type machine__thread_dso_type(struct machine *machine,enum dso_type dso_type =DSO__TYPE_UNKNOWN ;struct map_rb_node *rb_node; -maps__for_each_entry (thread->maps, rb_node) { +maps__for_each_entry (thread__maps (thread), rb_node) {struct dso *dso =map__dso (rb_node->map);if (!dso || dso->long_name[0 ] !='/' )
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 4/20 perf maps: Make delete static, always use put
In reply to Ian RogersAddress/leak sanitizer with reference count checking can identify the location of leaks, so use put rather than delete to avoid free-ing memory when the reference count is >1. Add maps__zput to ensure the variable is cleared.
--- tools/perf/tests/maps.c | 2 +- tools/perf/util/machine.c | 2 +- tools/perf/util/maps.c | 2 +- tools/perf/util/maps.h | 9 ++++++++- 4 files changed, 11 insertions(+), 4 deletions(-)
tools/perf/tests/maps.c +1 -1
@@ -140,7 +140,7 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest ret =check_maps (merged3,ARRAY_SIZE (merged3), maps);TEST_ASSERT_VAL ("merge check failed" , !ret); -maps__delete (maps); +maps__zput (maps);return TEST_OK ; }
tools/perf/util/machine.c +1 -1
@@ -248,7 +248,7 @@ void machine__exit(struct machine *machine)return ;machine__destroy_kernel_maps (machine); -maps__delete (machine->kmaps); +maps__zput (machine->kmaps);dsos__exit (&machine->dsos);machine__exit_vdso (machine);zfree (&machine->root_dir);
tools/perf/util/maps.c +1 -1
@@ -171,7 +171,7 @@ struct maps *maps__new(struct machine *machine)return result; } -void maps__delete (struct maps *maps) +static void maps__delete (struct maps *maps) {maps__exit (maps);unwind__finish_access (maps);
tools/perf/util/maps.h +8 -1
@@ -57,13 +57,20 @@ struct kmap { };struct maps *maps__new (struct machine *machine); -void maps__delete (struct maps *maps);bool maps__empty (struct maps *maps);int maps__clone (struct thread *thread,struct maps *parent);struct maps *maps__get (struct maps *maps);void maps__put (struct maps *maps); +static inline void __maps__zput (struct maps **map) +{ +maps__put (*map); + *map =NULL ; +} + +#define maps__zput (map) __maps__zput(&map) +static inline struct rb_root *maps__entries (struct maps *maps) {return &RC_CHK_ACCESS (maps)->entries;
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 5/20 perf addr_location: Move to its own header
In reply to Ian Rogersaddr_location is a common abstraction, move it into its own header and source file in preparation for wider clean up.
--- tools/perf/util/Build | 1 + tools/perf/util/addr_location.c | 16 ++++++++++++++++ tools/perf/util/addr_location.h | 28 ++++++++++++++++++++++++++++ tools/perf/util/event.c | 12 ------------ tools/perf/util/symbol.h | 17 +---------------- 5 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 tools/perf/util/addr_location.c create mode 100644 tools/perf/util/addr_location.h
tools/perf/util/Build +1 -0
@@ -1,4 +1,5 @@ perf-y += arm64-frame-pointer-unwind-support.o +perf-y += addr_location.o perf-y += annotate.o perf-y += block-info.o perf-y += block-range.o
tools/perf/util/addr_location.c new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "addr_location.h" +#include "map.h" +#include "thread.h" + +/* +* The preprocess_sample method will return with reference counts for the +* in it, when done using (and perhaps getting ref counts if needing to +* keep a pointer to one of those entries) it must be paired with +* addr_location__put(), so that the refcounts can be decremented. +*/ +void addr_location__put (struct addr_location *al) +{ +map__zput (al->map); +thread__zput (al->thread); +}
tools/perf/util/addr_location.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_ADDR_LOCATION +#define __PERF_ADDR_LOCATION 1 + +#include <linux/types.h> + +struct thread ; +struct maps ; +struct map ; +struct symbol ; + +struct addr_location { +struct thread *thread; +struct maps *maps; +struct map *map; +struct symbol *sym; +const char *srcline; +u64 addr; +char level; +u8 filtered; +u8 cpumode; +s32 cpu; +s32 socket; +}; + +void addr_location__put (struct addr_location *al); + +#endif /* __PERF_ADDR_LOCATION */
tools/perf/util/event.c +0 -12
@@ -767,18 +767,6 @@ int machine__resolve(struct machine *machine, struct addr_location *al,return 0 ; } -/* -* The preprocess_sample method will return with reference counts for the -* in it, when done using (and perhaps getting ref counts if needing to -* keep a pointer to one of those entries) it must be paired with -* addr_location__put(), so that the refcounts can be decremented. -*/ -void addr_location__put (struct addr_location *al) -{ -map__zput (al->map); -thread__zput (al->thread); -} -bool is_bts_event (struct perf_event_attr *attr) { return attr->type == PERF_TYPE_HARDWARE &&
tools/perf/util/symbol.h +1 -16
@@ -9,6 +9,7 @@#include <linux/list.h> #include <linux/rbtree.h> #include <stdio.h> +#include "addr_location.h" #include "path.h" #include "symbol_conf.h" #include "spark.h"
@@ -120,22 +121,6 @@ struct ref_reloc_sym {u64 unrelocated_addr; }; -struct addr_location { -struct thread *thread; -struct maps *maps; -struct map *map; -struct symbol *sym; -const char *srcline; -u64 addr; -char level; -u8 filtered; -u8 cpumode; -s32 cpu; -s32 socket; -}; - -void addr_location__put (struct addr_location *al); -int dso__load (struct dso *dso,struct map *map);int dso__load_vmlinux (struct dso *dso,struct map *map,const char *vmlinux,bool vmlinux_allocated);
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 6/20 perf addr_location: Add init/exit/copy functions
In reply to Ian Rogersstruct addr_location holds references to multiple reference counted objects. Add init/exit functions to make maintenance of those more consistent with the rest of the code and to try to avoid leaks. Modification of thread reference counts isn't included in this change.
--- tools/perf/builtin-annotate.c | 28 ++++--- tools/perf/builtin-c2c.c | 12 ++- tools/perf/builtin-diff.c | 16 ++-- tools/perf/builtin-inject.c | 2 + tools/perf/builtin-kmem.c | 11 ++- tools/perf/builtin-kwork.c | 15 +++- tools/perf/builtin-mem.c | 4 +- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-sched.c | 2 + tools/perf/builtin-script.c | 77 +++++++++++-------- tools/perf/builtin-timechart.c | 11 ++- tools/perf/builtin-top.c | 6 +- tools/perf/builtin-trace.c | 10 ++- tools/perf/tests/code-reading.c | 3 +- tools/perf/tests/hists_cumulate.c | 17 ++-- tools/perf/tests/hists_filter.c | 11 ++- tools/perf/tests/hists_link.c | 18 +++-- tools/perf/tests/hists_output.c | 10 ++- tools/perf/tests/mmap-thread-lookup.c | 4 +- tools/perf/util/addr_location.c | 30 +++++++- tools/perf/util/addr_location.h | 5 +- tools/perf/util/build-id.c | 2 + tools/perf/util/cs-etm.c | 20 +++-- tools/perf/util/data-convert-json.c | 8 +- tools/perf/util/db-export.c | 4 +- tools/perf/util/dlfilter.c | 13 +++- tools/perf/util/event.c | 16 ++-- tools/perf/util/evsel_fprintf.c | 8 +- tools/perf/util/hist.c | 8 +- tools/perf/util/intel-pt.c | 66 +++++++++++----- tools/perf/util/machine.c | 35 +++++---- .../scripting-engines/trace-event-python.c | 8 +- tools/perf/util/thread.c | 9 ++- tools/perf/util/unwind-libdw.c | 21 ++++- tools/perf/util/unwind-libunwind-local.c | 13 +++- 35 files changed, 365 insertions(+), 164 deletions(-)
tools/perf/builtin-annotate.c +19 -9
@@ -184,7 +184,7 @@ static int hist_iter__branch_callback(struct hist_entry_iter *iter,static int process_branch_callback (struct evsel *evsel,struct perf_sample *sample, -struct addr_location *al__maybe_unused , +struct addr_location *al,struct perf_annotate *ann,struct machine *machine) {
@@ -195,21 +195,29 @@ static int process_branch_callback(struct evsel *evsel, .hide_unresolved = symbol_conf.hide_unresolved, .ops = &hist_iter_branch, }; -struct addr_location a; +int ret; -if (machine__resolve (machine, &a, sample) <0 ) -return -1 ; +addr_location__init (&a); +if (machine__resolve (machine, &a, sample) <0 ) { + ret =-1 ; + goto out; + } -if (a.sym ==NULL ) -return 0 ; +if (a.sym ==NULL ) { + ret =0 ; + goto out; + }if (a.map !=NULL )map__dso (a.map)->hit =1 ;hist__account_cycles (sample->branch_stack, al, sample, false,NULL ); -return hist_entry_iter__add (&iter, &a,PERF_MAX_STACK_DEPTH , ann); + ret =hist_entry_iter__add (&iter, &a,PERF_MAX_STACK_DEPTH , ann); +out: +addr_location__exit (&a); +return ret; }static bool has_annotation (struct perf_annotate *ann)
@@ -272,10 +280,12 @@ static int process_sample_event(struct perf_tool *tool,struct addr_location al;int ret =0 ; +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {pr_warning ("problem processing %d event, skipping it.\n" , event->header.type); -return -1 ; + ret =-1 ; + goto out_put; }if (ann->cpu_list && !test_bit (sample->cpu, ann->cpu_bitmap))
@@ -288,7 +298,7 @@ static int process_sample_event(struct perf_tool *tool, ret =-1 ; } out_put: -addr_location__put (&al); +addr_location__exit (&al);return ret; }
tools/perf/builtin-c2c.c +8 -4
@@ -286,10 +286,12 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,struct mem_info *mi, *mi_dup;int ret; +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {pr_debug ("problem processing %d event, skipping it.\n" , event->header.type); -return -1 ; + ret =-1 ; + goto out; }if (c2c.stitch_lbr)
@@ -301,8 +303,10 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused, goto out; mi =sample__resolve_mem (sample, &al); -if (mi ==NULL ) -return -ENOMEM ; +if (mi ==NULL ) { + ret = -ENOMEM ; + goto out; + }/* * The mi object is released in hists__add_entry_ops,
@@ -368,7 +372,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused, } out: -addr_location__put (&al); +addr_location__exit (&al);return ret; free_mi:
tools/perf/builtin-diff.c +9 -7
@@ -409,15 +409,17 @@ static int diff__process_sample_event(struct perf_tool *tool,return 0 ; } +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {pr_warning ("problem processing %d event, skipping it.\n" , event->header.type); -return -1 ; + ret =-1 ; + goto out; }if (cpu_list && !test_bit (sample->cpu, cpu_bitmap)) { ret =0 ; - goto out_put; + goto out; }switch (compute) {
@@ -426,7 +428,7 @@ static int diff__process_sample_event(struct perf_tool *tool, NULL, NULL, NULL, sample, true)) {pr_warning ("problem incrementing symbol period, " "skipping event\n" ); - goto out_put; + goto out; } hist__account_cycles(sample->branch_stack, &al, sample, false,
@@ -437,7 +439,7 @@ static int diff__process_sample_event(struct perf_tool *tool,if (hist_entry_iter__add (&iter, &al,PERF_MAX_STACK_DEPTH ,NULL )) {pr_debug ("problem adding hist entry, skipping event\n" ); - goto out_put; + goto out; }break ;
@@ -446,7 +448,7 @@ static int diff__process_sample_event(struct perf_tool *tool, true)) {pr_warning ("problem incrementing symbol period, " "skipping event\n" ); - goto out_put; + goto out; } }
@@ -460,8 +462,8 @@ static int diff__process_sample_event(struct perf_tool *tool,if (!al.filtered) hists->stats.total_non_filtered_period += sample->period; ret =0 ; -out_put: -addr_location__put (&al); +out: +addr_location__exit (&al);return ret; }
tools/perf/builtin-inject.c +2 -0
@@ -743,6 +743,7 @@ int perf_event__inject_buildid(struct perf_tool *tool, union perf_event *event,struct addr_location al;struct thread *thread; +addr_location__init (&al); thread =machine__findnew_thread (machine, sample->pid, sample->tid);if (thread ==NULL ) { pr_err("problem processing %d event, skipping it.\n",
@@ -763,6 +764,7 @@ int perf_event__inject_buildid(struct perf_tool *tool, union perf_event *event,thread__put (thread); repipe:perf_event__repipe (tool, event, sample, machine); +addr_location__exit (&al);return 0 ; }
tools/perf/builtin-kmem.c +8 -3
@@ -399,7 +399,9 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)struct addr_location al;struct machine *machine = &kmem_session->machines.host;struct callchain_cursor_node *node; +u64 result; +addr_location__init (&al);if (alloc_func_list ==NULL ) {if (build_alloc_func_list () <0 ) goto out;
@@ -427,16 +429,19 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)else addr = node->ip; -return addr; + result = addr; + goto out; }else pr_debug3 ("skipping alloc function: %s\n" , caller->name);callchain_cursor_advance (&callchain_cursor); } -out:pr_debug2 ("unknown callsite: %" PRIx64"\n" , sample->ip); -return sample->ip; + result = sample->ip; +out: +addr_location__exit (&al); +return result; } struct sort_dimension {
tools/perf/builtin-kwork.c +11 -4
@@ -739,17 +739,22 @@ static int timehist_exit_event(struct perf_kwork *kwork,struct kwork_atom *atom =NULL ;struct kwork_work *work =NULL ;struct addr_location al; +int ret =0 ; +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {pr_debug ("Problem processing event, skipping it\n" ); -return -1 ; + ret =-1 ; + goto out; } atom =work_pop_atom (kwork, class,KWORK_TRACE_EXIT ,KWORK_TRACE_ENTRY , evsel, sample, machine, &work); -if (work ==NULL ) -return -1 ; +if (work ==NULL ) { + ret =-1 ; + goto out; + }if (atom !=NULL ) { work->nr_atoms++;
@@ -757,7 +762,9 @@ static int timehist_exit_event(struct perf_kwork *kwork,atom_del (atom); } -return 0 ; +out: +addr_location__exit (&al); +return ret; }static struct kwork_class kwork_irq;
tools/perf/builtin-mem.c +3 -1
@@ -199,9 +199,11 @@ dump_raw_samples(struct perf_tool *tool,char str[PAGE_SIZE_NAME_LEN ];struct dso *dso =NULL ; +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {fprintf (stderr,"problem processing %d event, skipping it.\n" , event->header.type); +addr_location__exit (&al);return -1 ; }
@@ -256,7 +258,7 @@ dump_raw_samples(struct perf_tool *tool, dso ? dso->long_name :"???" , al.sym ? al.sym->name : "???"); out_put: -addr_location__put (&al); +addr_location__exit (&al);return 0 ; }
tools/perf/builtin-report.c +4 -2
@@ -285,10 +285,12 @@ static int process_sample_event(struct perf_tool *tool,if (evswitch__discard (&rep->evswitch, evsel))return 0 ; +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {pr_debug ("problem processing %d event, skipping it.\n" , event->header.type); -return -1 ; + ret =-1 ; + goto out_put; }if (rep->stitch_lbr)
@@ -331,7 +333,7 @@ static int process_sample_event(struct perf_tool *tool,if (ret <0 )pr_debug ("problem adding hist entry, skipping event\n" ); out_put: -addr_location__put (&al); +addr_location__exit (&al);return ret; }
tools/perf/builtin-sched.c +2 -0
@@ -2584,6 +2584,7 @@ static int timehist_sched_change_event(struct perf_tool *tool,int rc =0 ;int state =evsel__intval (evsel, sample,"prev_state" ); +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {pr_err ("problem processing %d event. skipping it\n" , event->header.type);
@@ -2692,6 +2693,7 @@ static int timehist_sched_change_event(struct perf_tool *tool,evsel__save_time (evsel, sample->time, sample->cpu); +addr_location__exit (&al);return rc; }
tools/perf/builtin-script.c +46 -31
220 lines not on this page · raw message
tools/perf/builtin-timechart.c +7 -4
@@ -498,7 +498,6 @@ static const char *cat_backtrace(union perf_event *event,char *p =NULL ;size_t p_len;u8 cpumode =PERF_RECORD_MISC_USER ; -struct addr_location tal;struct ip_callchain *chain = sample->callchain;FILE *f =open_memstream (&p, &p_len);
@@ -507,6 +506,7 @@ static const char *cat_backtrace(union perf_event *event,return NULL ; } +addr_location__init (&al);if (!chain) goto exit;
@@ -518,6 +518,7 @@ static const char *cat_backtrace(union perf_event *event,for (i =0 ; i < chain->nr; i++) {u64 ip; +struct addr_location tal;if (callchain_param.order ==ORDER_CALLEE ) ip = chain->ips[i];
@@ -544,20 +545,22 @@ static const char *cat_backtrace(union perf_event *event,* Discard all. */ zfree (&p); - goto exit_put; + goto exit; }continue ; } +addr_location__init (&tal); tal.filtered =0 ;if (thread__find_symbol (al.thread, cpumode, ip, &tal))fprintf (f,"..... %016" PRIx64" %s\n" , ip, tal.sym->name);else fprintf (f,"..... %016" PRIx64"\n" , ip); + +addr_location__exit (&tal); } -exit_put: -addr_location__put (&al); exit: +addr_location__exit (&al);fclose (f);return p;
tools/perf/builtin-top.c +4 -2
@@ -773,8 +773,9 @@ static void perf_event__process_sample(struct perf_tool *tool,if (event->header.misc &PERF_RECORD_MISC_EXACT_IP ) top->exact_samples++; +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) -return ; + goto out;if (top->stitch_lbr)thread__set_lbr_stitch_enable (al.thread, true);
@@ -848,7 +849,8 @@ static void perf_event__process_sample(struct perf_tool *tool,mutex_unlock (&hists->lock); } -addr_location__put (&al); +out: +addr_location__exit (&al); } static void
tools/perf/builtin-trace.c +7 -3
@@ -2418,13 +2418,15 @@ static int trace__resolve_callchain(struct trace *trace, struct evsel *evsel,int max_stack = evsel->core.attr.sample_max_stack ? evsel->core.attr.sample_max_stack : trace->max_stack; -int err; +int err =-1 ; +addr_location__init (&al);if (machine__resolve (trace->host, &al, sample) <0 ) -return -1 ; + goto out; err =thread__resolve_callchain (al.thread, cursor, evsel, sample,NULL ,NULL , max_stack); -addr_location__put (&al); +out: +addr_location__exit (&al);return err; }
@@ -2893,6 +2895,7 @@ static int trace__pgfault(struct trace *trace,int err =-1 ;int callchain_ret =0 ; +addr_location__init (&al); thread =machine__findnew_thread (trace->host, sample->pid, sample->tid);if (sample->callchain) {
@@ -2953,6 +2956,7 @@ static int trace__pgfault(struct trace *trace, err =0 ; out_put:thread__put (thread); +addr_location__exit (&al);return err; }
tools/perf/tests/code-reading.c +2 -1
@@ -241,6 +241,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,pr_debug ("Reading object code for memory address: %#" PRIx64"\n" , addr); +addr_location__init (&al);if (!thread__find_map (thread, cpumode, addr, &al) || !map__dso (al.map)) {if (cpumode ==PERF_RECORD_MISC_HYPERVISOR ) {pr_debug ("Hypervisor address can not be resolved - skipping\n" );
@@ -366,7 +367,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode, }pr_debug ("Bytes read match those read by objdump\n" ); out: -map__put (al.map); +addr_location__exit (&al);return err; }
tools/perf/tests/hists_cumulate.c +11 -6
@@ -8,8 +8,8 @@#include "util/evsel.h" #include "util/evlist.h" #include "util/machine.h" -#include "util/thread.h" #include "util/parse-events.h" +#include "util/thread.h" #include "tests/tests.h" #include "tests/hists_common.h" #include <linux/kernel.h>
@@ -84,6 +84,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)struct perf_sample sample = { .period =1000 , };size_t i; +addr_location__init (&al);for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) {struct hist_entry_iter iter = { .evsel = evsel,
@@ -107,20 +108,22 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)if (hist_entry_iter__add (&iter, &al, sysctl_perf_event_max_stack,NULL ) <0 ) { -addr_location__put (&al); goto out; } - fake_samples[i].thread = al.thread; +thread__put (fake_samples[i].thread); + fake_samples[i].thread =thread__get (al.thread);map__put (fake_samples[i].map); - fake_samples[i].map = al.map; + fake_samples[i].map =map__get (al.map); fake_samples[i].sym = al.sym; } +addr_location__exit (&al);return TEST_OK ; out:pr_debug ("Not enough memory for adding a hist entry\n" ); +addr_location__exit (&al);return TEST_FAIL ; }
@@ -152,8 +155,10 @@ static void put_fake_samples(void) {size_t i; -for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) -map__put (fake_samples[i].map); +for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) { +map__zput (fake_samples[i].map); +thread__zput (fake_samples[i].thread); + } }typedef int (*test_fn_t )(struct evsel *,struct machine *);
tools/perf/tests/hists_filter.c +7 -4
@@ -8,6 +8,7 @@#include "util/evlist.h" #include "util/machine.h" #include "util/parse-events.h" +#include "util/thread.h" #include "tests/tests.h" #include "tests/hists_common.h" #include <linux/kernel.h>
@@ -53,6 +54,7 @@ static int add_hist_entries(struct evlist *evlist,struct perf_sample sample = { .period =100 , };size_t i; +addr_location__init (&al);/* * each evsel will have 10 samples but the 4th sample * (perf [perf] main) will be collapsed to an existing entry
@@ -84,21 +86,22 @@ static int add_hist_entries(struct evlist *evlist, al.socket = fake_samples[i].socket;if (hist_entry_iter__add (&iter, &al, sysctl_perf_event_max_stack,NULL ) <0 ) { -addr_location__put (&al); goto out; } - fake_samples[i].thread = al.thread; +thread__put (fake_samples[i].thread); + fake_samples[i].thread =thread__get (al.thread);map__put (fake_samples[i].map); - fake_samples[i].map = al.map; + fake_samples[i].map =map__get (al.map); fake_samples[i].sym = al.sym; } } - +addr_location__exit (&al);return 0 ; out:pr_debug ("Not enough memory for adding a hist entry\n" ); +addr_location__exit (&al);return TEST_FAIL ; }
tools/perf/tests/hists_link.c +11 -7
@@ -8,6 +8,7 @@#include "machine.h" #include "map.h" #include "parse-events.h" +#include "thread.h" #include "hists_common.h" #include "util/mmap.h" #include <errno.h>
@@ -70,6 +71,7 @@ static int add_hist_entries(struct evlist *evlist, struct machine *machine)struct perf_sample sample = { .period =1 , .weight =1 , };size_t i =0 , k; +addr_location__init (&al);/* * each evsel will have 10 samples - 5 common and 5 distinct. * However the second evsel also has a collapsed entry for
@@ -90,13 +92,13 @@ static int add_hist_entries(struct evlist *evlist, struct machine *machine) he =hists__add_entry (hists, &al,NULL ,NULL ,NULL ,NULL , &sample, true);if (he ==NULL ) { -addr_location__put (&al); goto out; } - fake_common_samples[k].thread = al.thread; +thread__put (fake_common_samples[k].thread); + fake_common_samples[k].thread =thread__get (al.thread);map__put (fake_common_samples[k].map); - fake_common_samples[k].map = al.map; + fake_common_samples[k].map =map__get (al.map); fake_common_samples[k].sym = al.sym; }
@@ -110,20 +112,22 @@ static int add_hist_entries(struct evlist *evlist, struct machine *machine) he =hists__add_entry (hists, &al,NULL ,NULL ,NULL ,NULL , &sample, true);if (he ==NULL ) { -addr_location__put (&al); goto out; } - fake_samples[i][k].thread = al.thread; - fake_samples[i][k].map = al.map; +thread__put (fake_samples[i][k].thread); + fake_samples[i][k].thread =thread__get (al.thread); +map__put (fake_samples[i][k].map); + fake_samples[i][k].map =map__get (al.map); fake_samples[i][k].sym = al.sym; } i++; } +addr_location__exit (&al);return 0 ; - out: +addr_location__exit (&al);pr_debug ("Not enough memory for adding a hist entry\n" );return -1 ; }
tools/perf/tests/hists_output.c +7 -3
@@ -54,6 +54,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)struct perf_sample sample = { .period =100 , };size_t i; +addr_location__init (&al);for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) {struct hist_entry_iter iter = { .evsel = evsel,
@@ -73,20 +74,21 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)if (hist_entry_iter__add (&iter, &al, sysctl_perf_event_max_stack,NULL ) <0 ) { -addr_location__put (&al); goto out; } fake_samples[i].thread = al.thread;map__put (fake_samples[i].map); - fake_samples[i].map = al.map; + fake_samples[i].map =map__get (al.map); fake_samples[i].sym = al.sym; } +addr_location__exit (&al);return TEST_OK ; out:pr_debug ("Not enough memory for adding a hist entry\n" ); +addr_location__exit (&al);return TEST_FAIL ; }
@@ -118,8 +120,10 @@ static void put_fake_samples(void) {size_t i; -for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) +for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) {map__put (fake_samples[i].map); + fake_samples[i].map =NULL ; + } }typedef int (*test_fn_t )(struct evsel *,struct machine *);
tools/perf/tests/mmap-thread-lookup.c +3 -1
@@ -187,6 +187,7 @@ static int mmap_events(synth_cb synth)struct addr_location al;struct thread *thread; +addr_location__init (&al); thread =machine__findnew_thread (machine,getpid (), td->tid);pr_debug ("looking for map %p\n" , td->map);
@@ -199,11 +200,12 @@ static int mmap_events(synth_cb synth)if (!al.map) {pr_debug ("failed, couldn't find map\n" ); err =-1 ; +addr_location__exit (&al);break ; }pr_debug ("map %p, addr %" PRIx64"\n" , al.map,map__start (al.map)); -map__put (al.map); +addr_location__exit (&al); }machine__delete_threads (machine);
tools/perf/util/addr_location.c +29 -1
@@ -1,16 +1,44 @@// SPDX-License-Identifier: GPL-2.0 #include "addr_location.h" #include "map.h" +#include "maps.h" #include "thread.h" +void addr_location__init (struct addr_location *al) +{ + al->thread =NULL ; + al->maps =NULL ; + al->map =NULL ; + al->sym =NULL ; + al->srcline =NULL ; + al->addr =0 ; + al->level =0 ; + al->filtered =0 ; + al->cpumode =0 ; + al->cpu =0 ; + al->socket =0 ; +} +/* * The preprocess_sample method will return with reference counts for the * in it, when done using (and perhaps getting ref counts if needing to * keep a pointer to one of those entries) it must be paired with * addr_location__put(), so that the refcounts can be decremented. */ -void addr_location__put (struct addr_location *al) +void addr_location__exit (struct addr_location *al) {map__zput (al->map);thread__zput (al->thread); +maps__zput (al->maps); +} + +void addr_location__copy (struct addr_location *dst,struct addr_location *src) +{ +thread__put (dst->thread); +maps__put (dst->maps); +map__put (dst->map); + *dst = *src; + dst->thread =thread__get (src->thread); + dst->maps =maps__get (src->maps); + dst->map =map__get (src->map); }
tools/perf/util/addr_location.h +4 -1
@@ -23,6 +23,9 @@ struct addr_location { s32 socket; }; -void addr_location__put (struct addr_location *al); +void addr_location__init (struct addr_location *al); +void addr_location__exit (struct addr_location *al); + +void addr_location__copy (struct addr_location *dst,struct addr_location *src);#endif /* __PERF_ADDR_LOCATION */
tools/perf/util/build-id.c +2 -0
@@ -58,9 +58,11 @@ int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,return -1 ; } +addr_location__init (&al);if (thread__find_map (thread, sample->cpumode, sample->ip, &al))map__dso (al.map)->hit =1 ; +addr_location__exit (&al);thread__put (thread);return 0 ; }
tools/perf/util/cs-etm.c +12 -8
@@ -910,33 +910,35 @@ static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,struct addr_location al;struct dso *dso;struct cs_etm_traceid_queue *tidq; +int ret =0 ;if (!etmq)return 0 ; +addr_location__init (&al); machine = etmq->etm->machine; cpumode =cs_etm__cpu_mode (etmq, address); tidq =cs_etm__etmq_get_traceid_queue (etmq, trace_chan_id);if (!tidq) -return 0 ; + goto out; thread = tidq->thread;if (!thread) {if (cpumode !=PERF_RECORD_MISC_KERNEL ) -return 0 ; + goto out; thread = etmq->etm->unknown_thread; }if (!thread__find_map (thread, cpumode, address, &al)) -return 0 ; + goto out; dso =map__dso (al.map);if (!dso) -return 0 ; + goto out;if (dso->data.status ==DSO_DATA_STATUS_ERROR &&dso__data_status_seen (dso,DSO_DATA_STATUS_SEEN_ITRACE )) -return 0 ; + goto out; offset =map__map_ip (al.map, address);
@@ -953,10 +955,12 @@ static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id, dso->long_name ? dso->long_name : "Unknown"); dso->auxtrace_warned = true; } -return 0 ; + goto out; } - -return len; + ret = len; +out: +addr_location__exit (&al); +return ret; } static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
tools/perf/util/data-convert-json.c +7 -1
@@ -154,12 +154,14 @@ static int process_sample_event(struct perf_tool *tool, { struct convert_json *c = container_of(tool, struct convert_json, tool);FILE *out = c->out; -struct addr_location al, tal; +struct addr_location al;u64 sample_type =__evlist__combined_sample_type (evsel->evlist);u8 cpumode =PERF_RECORD_MISC_USER ; +addr_location__init (&al);if (machine__resolve (machine, &al, sample) <0 ) {pr_err ("Sample resolution failed!\n" ); +addr_location__exit (&al);return -1 ; }
@@ -190,6 +192,7 @@ static int process_sample_event(struct perf_tool *tool,for (i =0 ; i < sample->callchain->nr; ++i) {u64 ip = sample->callchain->ips[i]; +struct addr_location tal;if (ip >=PERF_CONTEXT_MAX ) {switch (ip) {
@@ -215,8 +218,10 @@ static int process_sample_event(struct perf_tool *tool,else fputc (',' , out); +addr_location__init (&tal); ok =thread__find_symbol (al.thread, cpumode, ip, &tal);output_sample_callchain_entry (tool, ip, ok ? &tal :NULL ); +addr_location__exit (&tal); } }else {output_sample_callchain_entry (tool, sample->ip, &al);
@@ -245,6 +250,7 @@ static int process_sample_event(struct perf_tool *tool, }#endif output_json_format (out, false,2 ,"}" ); +addr_location__exit (&al);return 0 ; }
tools/perf/util/db-export.c +3 -1
@@ -239,16 +239,17 @@ static struct call_path *call_path_from_sample(struct db_export *dbe,struct addr_location al;u64 dso_db_id =0 , sym_db_id =0 , offset =0 ; -memset (&al,0 ,sizeof (al)); node =callchain_cursor_current (&callchain_cursor);if (!node)break ; +/* * Handle export of symbol and dso for this node by * constructing an addr_location struct and then passing it to * db_ids_from_al() to perform the export. */ +addr_location__init (&al); al.sym = node->ms.sym; al.map = node->ms.map; al.maps =thread__maps (thread);
@@ -265,6 +266,7 @@ static struct call_path *call_path_from_sample(struct db_export *dbe, kernel_start);callchain_cursor_advance (&callchain_cursor); +addr_location__exit (&al); }/* Reset the callchain order to its prior value. */
tools/perf/util/dlfilter.c +10 -3
@@ -258,6 +258,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)struct addr_location a;struct map *map;u64 offset; +__s32 ret;if (!d->ctx_valid)return -1 ;
@@ -272,16 +273,22 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len) machine__kernel_ip(d->machine, ip) == machine__kernel_ip(d->machine, d->sample->ip)) goto have_map; +addr_location__init (&a);thread__find_map_fb (al->thread, d->sample->cpumode, ip, &a); -if (!a.map) -return -1 ; +if (!a.map) { + ret =-1 ; + goto out; + } map = a.map; have_map: offset =map__map_ip (map, ip);if (ip + len >=map__end (map)) len =map__end (map) - ip; -return dso__data_read_offset (map__dso (map), d->machine, offset, buf, len); + ret =dso__data_read_offset (map__dso (map), d->machine, offset, buf, len); +out: +addr_location__exit (&a); +return ret; }static const struct perf_dlfilter_fns perf_dlfilter_fns = {
tools/perf/util/event.c +10 -6
@@ -486,6 +486,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *maif (machine) {struct addr_location al; +addr_location__init (&al); al.map =map__get (maps__find (machine__kernel_maps (machine), tp->addr));if (al.map &&map__load (al.map) >=0 ) { al.addr =map__map_ip (al.map, tp->addr);
@@ -493,7 +494,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *maif (al.sym) ret +=symbol__fprintf_symname_offs (al.sym, &al, fp); } -map__put (al.map); +addr_location__exit (&al); } ret +=fprintf (fp," old len %u new len %u\n" , tp->old_len, tp->new_len); old = true;
@@ -577,8 +578,10 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,struct machine *machine =maps__machine (maps);bool load_map = false; - al->maps = maps; - al->thread = thread; +maps__zput (al->maps); +map__zput (al->map); +thread__zput (al->thread); + al->addr = addr; al->cpumode = cpumode; al->filtered =0 ;
@@ -590,13 +593,13 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,if (cpumode ==PERF_RECORD_MISC_KERNEL && perf_host) { al->level ='k' ; - al->maps = maps =machine__kernel_maps (machine); + maps =machine__kernel_maps (machine); load_map = true; }else if (cpumode ==PERF_RECORD_MISC_USER && perf_host) { al->level ='.' ; }else if (cpumode ==PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { al->level ='g' ; - al->maps = maps =machine__kernel_maps (machine); + maps =machine__kernel_maps (machine); load_map = true; }else if (cpumode ==PERF_RECORD_MISC_GUEST_USER && perf_guest) { al->level ='u' ;
@@ -615,7 +618,8 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,return NULL ; } - + al->maps =maps__get (maps); + al->thread =thread__get (thread); al->map =map__get (maps__find (maps, al->addr));if (al->map !=NULL ) {/*
tools/perf/util/evsel_fprintf.c +5 -3
@@ -128,8 +128,6 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,bool first = true;if (sample->callchain) { -struct addr_location node_al; -callchain_cursor_commit (cursor);while (1 ) {
@@ -159,9 +157,12 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, printed +=fprintf (fp,"%c%16" PRIx64, s, node->ip);if (print_sym) { +struct addr_location node_al; + +addr_location__init (&node_al); printed +=fprintf (fp," " ); node_al.addr = addr; - node_al.map = map; + node_al.map =map__get (map);if (print_symoffset) { printed += __symbol__fprintf_symname_offs(sym, &node_al,
@@ -171,6 +172,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, printed +=__symbol__fprintf_symname (sym, &node_al, print_unknown_as_addr, fp); } +addr_location__exit (&node_al); }if (print_dso && (!sym || !sym->inlined))
tools/perf/util/hist.c +5 -3
@@ -588,7 +588,7 @@ static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)static struct hist_entry *hists__findnew_entry (struct hists *hists,struct hist_entry *entry, -struct addr_location *al, +const struct addr_location *al,bool sample_self) {struct rb_node **p;
@@ -927,8 +927,10 @@ iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)if (iter->curr >= iter->total)return 0 ; - al->maps = bi[i].to.ms.maps; - al->map = bi[i].to.ms.map; +maps__put (al->maps); + al->maps =maps__get (bi[i].to.ms.maps); +map__put (al->map); + al->map =map__get (bi[i].to.ms.map); al->sym = bi[i].to.ms.sym; al->addr = bi[i].to.addr;return 1 ;
tools/perf/util/intel-pt.c +45 -21
185 lines not on this page · raw message
tools/perf/util/machine.c +20 -15
@@ -2221,7 +2221,7 @@ static void ip__resolve_ams(struct thread *thread, {struct addr_location al; -memset (&al,0 ,sizeof (al)); +addr_location__init (&al);/* * We cannot use the header.misc hint to determine whether a * branch stack address is user, kernel, guest, hypervisor.
@@ -2234,11 +2234,12 @@ static void ip__resolve_ams(struct thread *thread, ams->addr = ip; ams->al_addr = al.addr; ams->al_level = al.level; - ams->ms.maps = al.maps; + ams->ms.maps =maps__get (al.maps); ams->ms.sym = al.sym; - ams->ms.map = al.map; + ams->ms.map =map__get (al.map); ams->phys_addr =0 ; ams->data_page_size =0 ; +addr_location__exit (&al); } static void ip__resolve_data(struct thread *thread,
@@ -2247,18 +2248,19 @@ static void ip__resolve_data(struct thread *thread, {struct addr_location al; -memset (&al,0 ,sizeof (al)); +addr_location__init (&al);thread__find_symbol (thread, m, addr, &al); ams->addr = addr; ams->al_addr = al.addr; ams->al_level = al.level; - ams->ms.maps = al.maps; + ams->ms.maps =maps__get (al.maps); ams->ms.sym = al.sym; - ams->ms.map = al.map; + ams->ms.map =map__get (al.map); ams->phys_addr = phys_addr; ams->data_page_size = daddr_page_size; +addr_location__exit (&al); } struct mem_info *sample__resolve_mem(struct perf_sample *sample,
@@ -2319,10 +2321,11 @@ static int add_callchain_ip(struct thread *thread, {struct map_symbol ms;struct addr_location al; -int nr_loop_iter =0 , err; +int nr_loop_iter =0 , err =0 ;u64 iter_cycles =0 ;const char *srcline =NULL ; +addr_location__init (&al); al.filtered =0 ; al.sym =NULL ; al.srcline =NULL ;
@@ -2348,9 +2351,10 @@ static int add_callchain_ip(struct thread *thread,* Discard all. */ callchain_cursor_reset (cursor); -return 1 ; + err =1 ; + goto out; } -return 0 ; + goto out; }thread__find_symbol (thread, *cpumode, ip, &al); }
@@ -2363,31 +2367,32 @@ static int add_callchain_ip(struct thread *thread, symbol__match_regex(al.sym, &ignore_callees_regex)) {/* Treat this symbol as the root, forgetting its callees. */ - *root_al = al; +addr_location__copy (root_al, &al);callchain_cursor_reset (cursor); } }if (symbol_conf.hide_unresolved && al.sym ==NULL ) -return 0 ; + goto out;if (iter) { nr_loop_iter = iter->nr_loop_iter; iter_cycles = iter->cycles; } - ms.maps = al.maps; - ms.map = al.map; + ms.maps =maps__get (al.maps); + ms.map =map__get (al.map); ms.sym = al.sym;if (!branch &&append_inlines (cursor, &ms, ip) ==0 ) -return 0 ; + goto out; srcline =callchain_srcline (&ms, al.addr); err =callchain_cursor_append (cursor, ip, &ms, branch, flags, nr_loop_iter, iter_cycles, branch_from, srcline); -map__put (al.map); +out: +addr_location__exit (&al);return err; }
tools/perf/util/scripting-engines/trace-event-python.c +7 -1
@@ -469,9 +469,11 @@ static PyObject *python_process_callchain(struct perf_sample *sample,struct addr_location node_al;unsigned long offset; +addr_location__init (&node_al); node_al.addr =map__map_ip (map, node->ip); node_al.map = map; offset =get_offset (node->ms.sym, &node_al); +addr_location__exit (&node_al); pydict_set_item_string_decref( pyelem, "sym_off",
@@ -539,6 +541,7 @@ static PyObject *python_process_brstack(struct perf_sample *sample,pydict_set_item_string_decref (pyelem,"cycles" ,PyLong_FromUnsignedLongLong (entries[i].flags.cycles)); +addr_location__init (&al);thread__find_map_fb (thread, sample->cpumode, entries[i].from, &al); dsoname =get_dsoname (al.map);
@@ -551,6 +554,7 @@ static PyObject *python_process_brstack(struct perf_sample *sample,pydict_set_item_string_decref (pyelem,"to_dsoname" ,_PyUnicode_FromString (dsoname)); +addr_location__exit (&al);PyList_Append (pylist, pyelem);Py_DECREF (pyelem); }
@@ -594,7 +598,6 @@ static PyObject *python_process_brstacksym(struct perf_sample *sample,PyObject *pylist;u64 i;char bf[512 ]; -struct addr_location al; pylist =PyList_New (0 );if (!pylist)
@@ -605,7 +608,9 @@ static PyObject *python_process_brstacksym(struct perf_sample *sample,for (i =0 ; i < br->nr; i++) {PyObject *pyelem; +struct addr_location al; +addr_location__init (&al); pyelem =PyDict_New ();if (!pyelem)Py_FatalError ("couldn't create Python dictionary" );
@@ -644,6 +649,7 @@ static PyObject *python_process_brstacksym(struct perf_sample *sample,PyList_Append (pylist, pyelem);Py_DECREF (pyelem); +addr_location__exit (&al); } exit:
tools/perf/util/thread.c +7 -2
@@ -432,18 +432,23 @@ int thread__memcpy(struct thread *thread, struct machine *machine,if (machine__kernel_ip (machine, ip)) cpumode =PERF_RECORD_MISC_KERNEL ; +addr_location__init (&al);if (!thread__find_map (thread, cpumode, ip, &al)) -return -1 ; +return -1 ; dso =map__dso (al.map); -if ( !dso || dso->data.status ==DSO_DATA_STATUS_ERROR ||map__load (al.map) <0 ) +if (!dso || dso->data.status ==DSO_DATA_STATUS_ERROR ||map__load (al.map) <0 ) { +addr_location__exit (&al);return -1 ; + } offset =map__map_ip (al.map, ip);if (is64bit) *is64bit = dso->is_64_bit; +addr_location__exit (&al); +return dso__data_read_offset (dso, machine, offset, buf, len); }
tools/perf/util/unwind-libdw.c +17 -4
@@ -90,8 +90,12 @@ static int __report_module(struct addr_location *al, u64 ip,static int report_module (u64 ip,struct unwind_info *ui) {struct addr_location al; +int res; -return __report_module (&al, ip, ui); +addr_location__init (&al); + res =__report_module (&al, ip, ui); +addr_location__exit (&al); +return res; }/*
@@ -104,8 +108,11 @@ static int entry(u64 ip, struct unwind_info *ui)struct unwind_entry *e = &ui->entries[ui->idx++];struct addr_location al; -if (__report_module (&al, ip, ui)) +addr_location__init (&al); +if (__report_module (&al, ip, ui)) { +addr_location__exit (&al);return -1 ; + } e->ip = ip; e->ms.maps = al.maps;
@@ -116,6 +123,7 @@ static int entry(u64 ip, struct unwind_info *ui) al.sym ? al.sym->name :"''" , ip, al.map ? map__map_ip(al.map, ip) : (u64) 0); +addr_location__exit (&al);return 0 ; }
@@ -136,17 +144,22 @@ static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,ssize_t size;struct dso *dso; +addr_location__init (&al);if (!thread__find_map (ui->thread,PERF_RECORD_MISC_USER , addr, &al)) {pr_debug ("unwind: no map for %lx\n" , (unsigned long )addr); -return -1 ; + goto out_fail; } dso =map__dso (al.map);if (!dso) -return -1 ; + goto out_fail; size =dso__data_read_addr (dso, al.map, ui->machine, addr, (u8 *) data,sizeof (*data)); +addr_location__exit (&al);return !(size ==sizeof (*data)); +out_fail: +addr_location__exit (&al); +return -1 ; } static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
tools/perf/util/unwind-libunwind-local.c +11 -2
@@ -416,7 +416,12 @@ static int read_unwind_spec_debug_frame(struct dso *dso,static struct map *find_map (unw_word_t ip,struct unwind_info *ui) {struct addr_location al; -return thread__find_map (ui->thread,PERF_RECORD_MISC_USER , ip, &al); +struct map *ret; + +addr_location__init (&al); + ret =thread__find_map (ui->thread,PERF_RECORD_MISC_USER , ip, &al); +addr_location__exit (&al); +return ret; } static int
@@ -631,7 +636,9 @@ static int entry(u64 ip, struct thread *thread, {struct unwind_entry e;struct addr_location al; +int ret; +addr_location__init (&al); e.ms.sym =thread__find_symbol (thread,PERF_RECORD_MISC_USER , ip, &al); e.ip = ip; e.ms.map = al.map;
@@ -642,7 +649,9 @@ static int entry(u64 ip, struct thread *thread, ip, al.map ? map__map_ip(al.map, ip) : (u64) 0); -return cb (&e, arg); + ret =cb (&e, arg); +addr_location__exit (&al); +return ret; }static void display_error (int err)
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 7/20 perf thread: Add reference count checking
In reply to Ian RogersModify struct declaration and accessor functions for the reference count checkers additional layer of indirection. Make sure pid_cmp in builtin-sched.c uses the underlying/original struct in pointer arithmetic, and not the temporary get/put indirection.
--- tools/perf/builtin-sched.c | 4 +- tools/perf/tests/hists_link.c | 2 +- tools/perf/ui/hist.c | 5 ++- tools/perf/util/hist.c | 2 +- tools/perf/util/machine.c | 2 +- tools/perf/util/sort.c | 2 +- tools/perf/util/thread.c | 20 +++++---- tools/perf/util/thread.h | 79 ++++++++++++++++++----------------- 8 files changed, 63 insertions(+), 53 deletions(-)
tools/perf/builtin-sched.c +2 -2
@@ -1385,7 +1385,7 @@ static int pid_cmp(struct work_atoms *l, struct work_atoms *r) {pid_t l_tid, r_tid; -if (l->thread == r->thread) +if (RC_CHK_ACCESS (l->thread) ==RC_CHK_ACCESS (r->thread))return 0 ; l_tid =thread__tid (l->thread); r_tid =thread__tid (r->thread);
@@ -1393,7 +1393,7 @@ static int pid_cmp(struct work_atoms *l, struct work_atoms *r)return -1 ;if (l_tid > r_tid)return 1 ; -return (int )(l->thread - r->thread); +return (int )(RC_CHK_ACCESS (l->thread) -RC_CHK_ACCESS (r->thread)); }static int avg_cmp (struct work_atoms *l,struct work_atoms *r)
tools/perf/tests/hists_link.c +1 -1
@@ -148,7 +148,7 @@ static int find_sample(struct sample *samples, size_t nr_samples, struct thread *t, struct map *m, struct symbol *s) { while (nr_samples--) { - if (samples->thread == t && + if (RC_CHK_ACCESS(samples->thread) == RC_CHK_ACCESS(t) && RC_CHK_ACCESS(samples->map) == RC_CHK_ACCESS(m) && samples->sym == s) return 1;
tools/perf/ui/hist.c +4 -1
@@ -11,6 +11,7 @@#include "../util/sort.h" #include "../util/evsel.h" #include "../util/evlist.h" +#include "../util/thread.h" #include "../util/util.h" /* hist period print (hpp) functions */
@@ -274,7 +275,9 @@ static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,if (ret)return ret; -if (a->thread != b->thread || !hist_entry__has_callchains (a) || !symbol_conf.use_callchain) +if ((a->thread ==NULL ?NULL :RC_CHK_ACCESS (a->thread)) != + (b->thread ==NULL ?NULL :RC_CHK_ACCESS (b->thread)) || + !hist_entry__has_callchains (a) || !symbol_conf.use_callchain)return 0 ; ret = b->callchain->max_depth - a->callchain->max_depth;
tools/perf/util/hist.c +1 -1
@@ -2124,7 +2124,7 @@ static bool hists__filter_entry_by_thread(struct hists *hists, struct hist_entry *he) {if (hists->thread_filter !=NULL && - he->thread != hists->thread_filter) { +RC_CHK_ACCESS (he->thread) !=RC_CHK_ACCESS (hists->thread_filter)) { he->filtered |= (1 <<HIST_FILTER__THREAD );return true; }
tools/perf/util/machine.c +1 -1
@@ -2055,7 +2055,7 @@ static void __machine__remove_thread(struct machine *machine, struct thread_rb_nif (!nd) nd =thread_rb_node__find (th, &threads->entries.rb_root); -if (threads->last_match == th) +if (threads->last_match &&RC_CHK_ACCESS (threads->last_match) ==RC_CHK_ACCESS (th))threads__set_last_match (threads,NULL );if (lock)
tools/perf/util/sort.c +1 -1
@@ -128,7 +128,7 @@ static int hist_entry__thread_filter(struct hist_entry *he, int type, const voidif (type !=HIST_FILTER__THREAD )return -1 ; -return th && he->thread != th; +return th &&RC_CHK_ACCESS (he->thread) !=RC_CHK_ACCESS (th); }struct sort_entry sort_thread = {
tools/perf/util/thread.c +13 -7
@@ -41,9 +41,10 @@ struct thread *thread__new(pid_t pid, pid_t tid) {char *comm_str;struct comm *comm; -struct thread *thread =zalloc (sizeof (*thread)); +RC_STRUCT (thread) *_thread =zalloc (sizeof (*_thread)); +struct thread *thread; -if (thread !=NULL ) { +if (ADD_RC_CHK (thread, _thread) !=NULL ) {thread__set_pid (thread, pid);thread__set_tid (thread, tid);thread__set_ppid (thread,-1 );
@@ -68,7 +69,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)list_add (&comm->list,thread__comm_list (thread));refcount_set (thread__refcnt (thread),1 );/* Thread holds first ref to nsdata. */ - thread->nsinfo =nsinfo__new (pid); +RC_CHK_ACCESS (thread)->nsinfo =nsinfo__new (pid);srccode_state_init (thread__srccode_state (thread)); }
@@ -105,26 +106,31 @@ void thread__delete(struct thread *thread) }up_write (thread__comm_lock (thread)); -nsinfo__zput (thread->nsinfo); +nsinfo__zput (RC_CHK_ACCESS (thread)->nsinfo);srccode_state_free (thread__srccode_state (thread));exit_rwsem (thread__namespaces_lock (thread));exit_rwsem (thread__comm_lock (thread));thread__free_stitch_list (thread); -free (thread); +RC_CHK_FREE (thread); }struct thread *thread__get (struct thread *thread) { -if (thread) +struct thread *result; + +if (RC_CHK_GET (result, thread))refcount_inc (thread__refcnt (thread)); -return thread; + +return result; }void thread__put (struct thread *thread) {if (thread &&refcount_dec_and_test (thread__refcnt (thread)))thread__delete (thread); +else +RC_CHK_PUT (thread); }static struct namespaces *__thread__namespaces (struct thread *thread)
tools/perf/util/thread.h +40 -39
245 lines not on this page · raw message
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 8/20 perf machine: Make delete_threads part of machine__exit
In reply to Ian RogersThe code required threads to be deleted before machine__exit was called or the threads would be leaked. This was error prone so move the delete_threads into machine__exit.
--- tools/perf/tests/code-reading.c | 1 - tools/perf/tests/dwarf-unwind.c | 1 - tools/perf/tests/mmap-thread-lookup.c | 1 - tools/perf/tests/symbols.c | 1 - tools/perf/util/machine.c | 1 + tools/perf/util/session.c | 6 ------ 6 files changed, 1 insertion(+), 10 deletions(-)
tools/perf/tests/code-reading.c +0 -1
@@ -721,7 +721,6 @@ static int do_test_code_reading(bool try_kcore)evlist__delete (evlist);perf_cpu_map__put (cpus);perf_thread_map__put (threads); -machine__delete_threads (machine);machine__delete (machine);return err;
tools/perf/tests/dwarf-unwind.c +0 -1
@@ -235,7 +235,6 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,thread__put (thread); out: -machine__delete_threads (machine);machine__delete (machine);return err; }
tools/perf/tests/mmap-thread-lookup.c +0 -1
@@ -208,7 +208,6 @@ static int mmap_events(synth_cb synth)addr_location__exit (&al); } -machine__delete_threads (machine);machine__delete (machine);return err; }
tools/perf/tests/symbols.c +0 -1
@@ -38,7 +38,6 @@ static int init_test_info(struct test_info *ti)static void exit_test_info (struct test_info *ti) {thread__put (ti->thread); -machine__delete_threads (ti->machine);machine__delete (ti->machine); }
tools/perf/util/machine.c +1 -0
@@ -256,6 +256,7 @@ void machine__exit(struct machine *machine)zfree (&machine->current_tid);zfree (&machine->kallsyms_filename); +machine__delete_threads (machine);for (i =0 ; i <THREADS__TABLE_SIZE ; i++) {struct threads *threads = &machine->threads[i];
tools/perf/util/session.c +0 -6
@@ -278,11 +278,6 @@ struct perf_session *__perf_session__new(struct perf_data *data,return ERR_PTR (ret); } -static void perf_session__delete_threads (struct perf_session *session) -{ -machine__delete_threads (&session->machines.host); -} -static void perf_decomp__release_events (struct decomp *next) {struct decomp *decomp;
@@ -305,7 +300,6 @@ void perf_session__delete(struct perf_session *session)auxtrace__free (session);auxtrace_index__free (&session->auxtrace_index);perf_session__destroy_kernel_maps (session); -perf_session__delete_threads (session);perf_decomp__release_events (session->decomp_data.decomp);perf_env__exit (&session->header.env);machines__exit (&session->machines);
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 9/20 perf report: Avoid thread leak
In reply to Ian RogersCaught with address sanitizer and reference count checking.
--- tools/perf/builtin-report.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/builtin-report.c +1 -0
@@ -839,6 +839,7 @@ static struct task *tasks_list(struct task *task, struct machine *machine)return ERR_PTR (-ENOENT ); parent_task =thread__priv (parent_thread); +thread__put (parent_thread);list_add_tail (&task->list, &parent_task->children);return tasks_list (parent_task, machine); }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 10/20 perf header: Ensure bitmaps are freed
In reply to Ian Rogersmemory_node bitmaps need a bitmap_free to avoid memory leaks. Caught by leak sanitizer.
--- tools/perf/util/header.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/header.c +10 -2
@@ -1389,6 +1389,14 @@ static int memory_node__read(struct memory_node *n, unsigned long idx)return 0 ; } +static void memory_node__delete_nodes (struct memory_node *nodesp,u64 cnt) +{ +for (u64 i =0 ; i < cnt; i++) +bitmap_free (nodesp[i].set); + +free (nodesp); +} +static int memory_node__sort (const void *a,const void *b) {const struct memory_node *na = a;
@@ -1449,7 +1457,7 @@ static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) *nodesp = nodes;qsort (nodes, cnt,sizeof (nodes[0 ]), memory_node__sort); }else -free (nodes); +memory_node__delete_nodes (nodes, cnt);return ret; }
@@ -1516,7 +1524,7 @@ static int write_mem_topology(struct feat_fd *ff __maybe_unused, } out: -free (nodes); +memory_node__delete_nodes (nodes, nr);return ret; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 11/20 perf stat: Avoid evlist leak
In reply to Ian RogersFree evlist before overwriting in "perf stat report" mode. Detected using leak sanitizer.
--- tools/perf/builtin-stat.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/builtin-stat.c +1 -0
@@ -2427,6 +2427,7 @@ static int __cmd_report(int argc, const char **argv) perf_stat.session = session; stat_config.output = stderr; +evlist__delete (evsel_list); evsel_list = session->evlist; ret =perf_session__process_events (session);
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 12/20 perf intel-pt: Fix missed put and leak
In reply to Ian RogersAdd missing put and free, detected with leak sanitizer.
--- tools/perf/util/intel-pt.c | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/util/intel-pt.c +2 -0
@@ -1280,6 +1280,7 @@ static void intel_pt_add_br_stack(struct intel_pt *pt, pt->kernel_start); sample->branch_stack = pt->br_stack; +thread__put (thread); }/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
@@ -3580,6 +3581,7 @@ static void intel_pt_free(struct perf_session *session)zfree (&pt->chain);zfree (&pt->filter);zfree (&pt->time_ranges); +zfree (&pt->br_stack);free (pt); }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 13/20 perf evlist: Free stats in all evlist destruction
In reply to Ian RogersThere is no evsel free stats, freeing in the evlist__delete ensures memory leaks are avoided. Issues detected with "perf stat report" and leak sanitizer, perf stat uses perf_session__delete to free the evlist. Add dummy symbol for python build.
--- tools/perf/util/evlist.c | 2 ++ tools/perf/util/python.c | 4 ++++ 2 files changed, 6 insertions(+)
tools/perf/util/evlist.c +2 -0
@@ -31,6 +31,7 @@#include "util/pmu.h" #include "util/sample.h" #include "util/bpf-filter.h" +#include "util/stat.h" #include "util/util.h" #include <signal.h> #include <unistd.h>
@@ -171,6 +172,7 @@ void evlist__delete(struct evlist *evlist)if (evlist ==NULL )return ; +evlist__free_stats (evlist);evlist__munmap (evlist);evlist__close (evlist);evlist__purge (evlist);
tools/perf/util/python.c +4 -0
@@ -1494,3 +1494,7 @@ void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cp int fd, int group_fd, unsigned long flags) { } + +void evlist__free_stats(struct evlist *evlist) +{ +}
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 14/20 perf python: Avoid 2 leak sanitizer issues
In reply to Ian RogersLeak sanitizer complains about the variable size bf allocation and store to bf if sized 0.
--- tools/perf/util/scripting-engines/trace-event-python.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/perf/util/scripting-engines/trace-event-python.c +5 -1
@@ -735,6 +735,9 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, chunsigned int i =0 , r;int printed =0 ; +if (size <=0 ) +return ; + bf[0 ] =0 ;if (!regs || !regs->regs)
@@ -764,7 +767,7 @@ static void set_regs_in_dict(PyObject *dict,* 10 chars is for register name. */ int size =__sw_hweight64 (attr->sample_regs_intr) *28 ; -char bf[size]; +char *bf =malloc (size);regs_map (&sample->intr_regs, attr->sample_regs_intr, arch, bf,sizeof (bf));
@@ -775,6 +778,7 @@ static void set_regs_in_dict(PyObject *dict,pydict_set_item_string_decref (dict,"uregs" ,_PyUnicode_FromString (bf)); +free (bf); } static void set_sym_in_dict(PyObject *dict, struct addr_location *al,
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 15/20 perf jit: Fix two thread leaks
In reply to Ian RogersAs reported by leak sanitizer with reference count checking.
--- tools/perf/util/jitdump.c | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/util/jitdump.c +2 -0
@@ -800,6 +800,7 @@ static void jit_add_pid(struct machine *machine, pid_t pid) }thread__set_priv (thread, (void *)true); +thread__put (thread); }static bool jit_has_pid (struct machine *machine,pid_t pid)
@@ -811,6 +812,7 @@ static bool jit_has_pid(struct machine *machine, pid_t pid)return false; priv =thread__priv (thread); +thread__put (thread);return (bool )priv; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 16/20 perf symbol-elf: Correct holding a reference
In reply to Ian RogersIf a reference is held, don't put it as this will confuse reference count checking.
--- tools/perf/util/symbol-elf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/symbol-elf.c +2 -2
@@ -1389,11 +1389,11 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map,/* Ensure maps are correctly ordered */ if (kmaps) {int err; +struct map *tmp =map__get (map); -map__get (map);maps__remove (kmaps, map); err =maps__insert (kmaps, map); -map__put (map); +map__put (tmp);if (err)return err; }
Signature · 7 lines
2.41.0.rc0.172.g3f132b7071-goog _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ian Rogers
reviewerPATCH 17/20 perf maps: Fix overlapping memory leak
In reply to Ian RogersAdd a missed free detected by leak sanitizer.
--- tools/perf/util/maps.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/util/maps.c +1 -0
@@ -374,6 +374,7 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) } put_map:map__put (pos->map); +free (pos); }up_write (maps__lock (maps));return err;
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 18/20 perf machine: Fix leak of kernel dso
In reply to Ian RogersThe kernel dso may be found by searching dsos or allocating if not found. The allocation returns with a reference count of 2, once for the dsos list and once for the returned value. The list search has a reference count of 1, once for the dsos list. To make the reference counts consistent, increase the dsos list search reference count to 2 with a dso__get, and do a put when the scope ends for either the allocated or found dso.
This issue was found with leak sanitizer and reference count checking.
--- tools/perf/util/machine.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/machine.c +2 -1
@@ -1868,7 +1868,7 @@ static int machine__process_kernel_mmap_event(struct machine *machine,continue ; - kernel = dso; + kernel =dso__get (dso);break ; }
@@ -1913,6 +1913,7 @@ static int machine__process_kernel_mmap_event(struct machine *machine,*/ dso__load (kernel,machine__kernel_map (machine)); } +dso__put (kernel); }else if (perf_event__is_extra_kernel_mmap (machine, xm)) {return machine__process_extra_kernel_map (machine, xm); }
Signature · 7 lines
2.41.0.rc0.172.g3f132b7071-goog _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ian Rogers
reviewerPATCH 19/20 perf machine: Don't leak module maps
In reply to Ian Rogersmachine__addnew_module_map requires a put on its result. Add this and narrow the scope of map to make the correctness more obvious. This leak was caught with leak sanitizer and the reference count checker.
--- tools/perf/util/machine.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/machine.c +3 -3
@@ -1797,7 +1797,6 @@ static int machine__process_kernel_mmap_event(struct machine *machine,struct extra_kernel_map *xm, struct build_id *bid) { - struct map *map; enum dso_space_type dso_space;bool is_kernel_mmap;const char *mmap_name = machine->mmap_name;
@@ -1823,8 +1822,8 @@ static int machine__process_kernel_mmap_event(struct machine *machine, }if (xm->name[0 ] =='/' || (!is_kernel_mmap && xm->name[0 ] =='[' )) { - map =machine__addnew_module_map (machine, xm->start, - xm->name); +struct map *map =machine__addnew_module_map (machine, xm->start, xm->name); +if (map ==NULL ) goto out_problem;
@@ -1833,6 +1832,7 @@ static int machine__process_kernel_mmap_event(struct machine *machine,if (build_id__is_defined (bid))dso__set_build_id (map__dso (map), bid); +map__put (map); }else if (is_kernel_mmap) {const char *symbol_name = xm->name +strlen (mmap_name);/*
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Ian Rogers
reviewerPATCH 20/20 perf map/maps/thread: Changes to reference counting
In reply to Ian RogersFix missed reference count gets and puts as detected with leak sanitizer and reference count checking.
--- tools/perf/util/callchain.c | 7 +++++-- tools/perf/util/event.c | 3 +++ tools/perf/util/hist.c | 6 ++++-- tools/perf/util/machine.c | 14 ++++++++------ 4 files changed, 20 insertions(+), 10 deletions(-)
tools/perf/util/callchain.c +5 -2
@@ -1065,8 +1065,10 @@ int callchain_cursor_append(struct callchain_cursor *cursor, } node->ip = ip; +maps__zput (node->ms.maps);map__zput (node->ms.map); node->ms = *ms; + node->ms.maps =maps__get (node->ms.maps); node->ms.map =map__get (node->ms.map); node->branch = branch; node->nr_loop_iter = nr_loop_iter;
@@ -1114,7 +1116,8 @@ int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node * {struct machine *machine =maps__machine (node->ms.maps); - al->maps = node->ms.maps; +maps__put (al->maps); + al->maps =maps__get (node->ms.maps);map__put (al->map); al->map =map__get (node->ms.map); al->sym = node->ms.sym;
@@ -1127,7 +1130,7 @@ int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *if (al->map ==NULL ) goto out; } -if (al->maps ==machine__kernel_maps (machine)) { +if (RC_CHK_ACCESS (al->maps) ==RC_CHK_ACCESS (machine__kernel_maps (machine))) {if (machine__is_host (machine)) { al->cpumode =PERF_RECORD_MISC_KERNEL ; al->level ='k' ;
tools/perf/util/event.c +3 -0
@@ -711,6 +711,9 @@ int machine__resolve(struct machine *machine, struct addr_location *al,if (thread__is_filtered (thread)) al->filtered |= (1 <<HIST_FILTER__THREAD ); +thread__put (thread); + thread =NULL ; + al->sym =NULL ; al->cpu = sample->cpu; al->socket =-1 ;
tools/perf/util/hist.c +4 -2
@@ -450,6 +450,7 @@ static int hist_entry__init(struct hist_entry *he,memset (&he->stat,0 ,sizeof (he->stat)); } + he->ms.maps =maps__get (he->ms.maps); he->ms.map =map__get (he->ms.map);if (he->branch_info) {
@@ -497,7 +498,7 @@ static int hist_entry__init(struct hist_entry *he, }INIT_LIST_HEAD (&he->pairs.node); -thread__get (he->thread); + he->thread =thread__get (he->thread); he->hroot_in =RB_ROOT_CACHED ; he->hroot_out =RB_ROOT_CACHED ;
@@ -523,6 +524,7 @@ static int hist_entry__init(struct hist_entry *he,map__put (he->mem_info->daddr.ms.map); } err: +maps__zput (he->ms.maps);map__zput (he->ms.map);zfree (&he->stat_acc);return -ENOMEM ;
@@ -611,7 +613,6 @@ static struct hist_entry *hists__findnew_entry(struct hists *hists,* keys were used. */ cmp =hist_entry__cmp (he, entry); -if (!cmp) {if (sample_self) {he_stat__add_period (&he->stat, period);
@@ -1309,6 +1310,7 @@ void hist_entry__delete(struct hist_entry *he)struct hist_entry_ops *ops = he->ops;thread__zput (he->thread); +maps__zput (he->ms.maps);map__zput (he->ms.map);if (he->branch_info) {
tools/perf/util/machine.c +8 -6
@@ -539,7 +539,7 @@ static void machine__update_thread_pid(struct machine *machine, goto out_err;if (thread__maps (th) ==thread__maps (leader)) -return ; + goto out_put;if (thread__maps (th)) {/*
@@ -579,7 +579,7 @@ __threads__get_last_match(struct threads *threads, struct machine *machine,machine__update_thread_pid (machine, th, pid);return thread__get (th); } - +thread__put (threads->last_match); threads->last_match =NULL ; }
@@ -601,7 +601,8 @@ threads__get_last_match(struct threads *threads, struct machine *machine,static void __threads__set_last_match (struct threads *threads,struct thread *th) { - threads->last_match = th; +thread__put (threads->last_match); + threads->last_match =thread__get (th); } static void
@@ -664,7 +665,6 @@ static struct thread *____machine__findnew_thread(struct machine *machine,rb_link_node (&nd->rb_node, parent, p);rb_insert_color_cached (&nd->rb_node, &threads->entries, leftmost); -/* * We have to initialize maps separately after rb tree is updated. *
@@ -673,6 +673,7 @@ static struct thread *____machine__findnew_thread(struct machine *machine,* the rb tree. */ if (thread__init_maps (th, machine)) { +pr_err ("Thread init failed thread %d\n" , pid);rb_erase_cached (&nd->rb_node, &threads->entries);RB_CLEAR_NODE (&nd->rb_node);free (nd);
@@ -682,11 +683,10 @@ static struct thread *____machine__findnew_thread(struct machine *machine,/* * It is now in the rbtree, get a ref */ -thread__get (th);threads__set_last_match (threads, th); ++threads->nr; -return th; +return thread__get (th); }struct thread *__machine__findnew_thread (struct machine *machine,pid_t pid,pid_t tid)
@@ -2395,6 +2395,8 @@ static int add_callchain_ip(struct thread *thread, iter_cycles, branch_from, srcline); out:addr_location__exit (&al); +maps__put (ms.maps); +map__put (ms.map);return err; }
Signature · 7 lines
2.41.0.rc0.172.g3f132b7071-goog _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ian Rogers
reviewerQuoted 10 lines · Ian Rogers
Add reference count checking to thread after first refactoring bits of the code, such as making the thread red-black tree non-invasive (so the thread it references is easier to reference count, rather than having 3 potential references). Part of this refactoring also removes the dead thread list because if we held a reference here the threads would never die and anything else has questionable correctness.
addr_locationis made into its own C/header file to capture the init, exit and copy code.
Quoted 8 lines
Fix additional outstanding memory leak and reference count issues to the point that "perf test" compiled with address sanitizer but without libtraceevent passes all but one test - libtraceevent reports leaks within its own code, most likely as it isn't compiled with sanitizers. The remaining failing test is "68: Test dwarf unwind" and that has address sanitizer issues as it uses memcpy to access the stack within the process - we likely want to skip parts of the test with sanitizers enabled.
So I tried a bit harder to break things and ran into crashes/leaks around callchains fixed by the patch below - I used 'perf top -g' and I'll merge the patch into a v2 for the series. The largest remaining leak is caused by map/maps retaining dsos retaining symbols. The map and maps are being retained by the TLS call chain cursors [1] allocated in callchain_cursor_append. For perf report there are similar retention issues via "struct hists", but as this maybe merged with evsels it is messy to cleanup.
Thanks,
Ian
```
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 437325d19ad3..909f62b3b266 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -590,6 +590,7 @@ fill_node(struct callchain_node *node, struct
callchain_cursor *cursor)
call->ip = cursor_node->ip;
call->ms = cursor_node->ms;
call->ms.map = map__get(call->ms.map);
+ call->ms.maps = maps__get(call->ms.maps);
call->srcline = cursor_node->srcline;
if (cursor_node->branch) {
@@ -649,6 +650,7 @@ add_child(struct callchain_node *parent,
list_for_each_entry_safe(call, tmp, &new->val, list) {
list_del_init(&call->list);
map__zput(call->ms.map);
+ maps__zput(call->ms.maps);
free(call);
}
free(new);
@@ -1010,10 +1012,16 @@ merge_chain_branch(struct callchain_cursor *cursor,
int err = 0;
list_for_each_entry_safe(list, next_list, &src->val, list) {
- callchain_cursor_append(cursor, list->ip, &list->ms,
- false, NULL, 0, 0, 0, list->srcline);
+ struct map_symbol ms = {
+ .maps = maps__get(list->ms.maps),
+ .map = map__get(list->ms.map),
+ };
+ callchain_cursor_append(cursor, list->ip, &ms, false,
NULL, 0, 0, 0, list->srcline);
list_del_init(&list->list);
+ map__zput(ms.map);
+ maps__zput(ms.maps);
map__zput(list->ms.map);
+ maps__zput(list->ms.maps);
free(list);
}
@@ -1068,8 +1076,8 @@ int callchain_cursor_append(struct
callchain_cursor *cursor,
maps__zput(node->ms.maps);
map__zput(node->ms.map);
node->ms = *ms;
- node->ms.maps = maps__get(node->ms.maps);
- node->ms.map = map__get(node->ms.map);
+ node->ms.maps = maps__get(ms->maps);
+ node->ms.map = map__get(ms->map);
node->branch = branch;
node->nr_loop_iter = nr_loop_iter;
node->iter_cycles = iter_cycles;
@@ -1463,12 +1471,14 @@ static void free_callchain_node(struct
callchain_node *node)
list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
list_del_init(&list->list);
map__zput(list->ms.map);
+ maps__zput(list->ms.maps);
free(list);
}
list_for_each_entry_safe(list, tmp, &node->val, list) {
list_del_init(&list->list);
map__zput(list->ms.map);
+ maps__zput(list->ms.maps);
free(list);
}
@@ -1554,6 +1564,7 @@ int callchain_node__make_parent_list(struct
callchain_node *node)
list_for_each_entry_safe(chain, new, &head, list) {
list_del_init(&chain->list);
map__zput(chain->ms.map);
+ maps__zput(chain->ms.maps);
free(chain);
}
return -ENOMEM;
@@ -1599,8 +1610,10 @@ void callchain_cursor_reset(struct
callchain_cursor *cursor)
cursor->nr = 0;
cursor->last = &cursor->first;
- for (node = cursor->first; node != NULL; node = node->next)
+ for (node = cursor->first; node != NULL; node = node->next) {
map__zput(node->ms.map);
+ maps__zput(node->ms.maps);
+ }
}
void callchain_param_setup(u64 sample_type, const char *arch)
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 16dc49876e87..bdad4b8bf77d 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -2321,7 +2321,7 @@ static int add_callchain_ip(struct thread *thread,
struct iterations *iter,
u64 branch_from)
{
- struct map_symbol ms;
+ struct map_symbol ms = {};
struct addr_location al;
int nr_loop_iter = 0, err = 0;
u64 iter_cycles = 0;
@@ -3091,6 +3091,7 @@ static int append_inlines(struct
callchain_cursor *cursor, struct map_symbol *
ms
struct dso *dso;
u64 addr;
int ret = 1;
+ struct map_symbol ilist_ms;
if (!symbol_conf.inline_name || !map || !sym)
return ret;
@@ -3107,18 +3108,20 @@ static int append_inlines(struct
callchain_cursor *cursor, struct map_symbol
*ms
inlines__tree_insert(&dso->inlined_nodes, inline_node);
}
+ ilist_ms = (struct map_symbol) {
+ .maps = maps__get(ms->maps),
+ .map = map__get(map),
+ };
list_for_each_entry(ilist, &inline_node->val, list) {
- struct map_symbol ilist_ms = {
- .maps = ms->maps,
- .map = map,
- .sym = ilist->symbol,
- };
+ ilist_ms.sym = ilist->symbol;
ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
NULL, 0, 0, 0, ilist->srcline);
if (ret != 0)
return ret;
}
+ map__put(ilist_ms.map);
+ maps__put(ilist_ms.maps);
return ret;
}
```[1] http://lkml.kernel.org/r/[email protected]
Quoted 99 lines
Ian Rogers (20):
perf thread: Remove notion of dead threads perf thread: Make threads rbtree non-invasive perf thread: Add accessor functions for thread perf maps: Make delete static, always use put perfaddr_location: Move to its own header perfaddr_location: Addinit/exit/copyfunctions perf thread: Add reference count checking
perf machine: Makedelete_threadspart ofmachine__exitperf report: Avoid thread leak
perf header: Ensure bitmaps are freed
perf stat: Avoid evlist leak
perf intel-pt: Fix missed put and leak
perf evlist: Free stats in all evlist destruction perf python: Avoid 2 leak sanitizer issues perf jit: Fix two thread leaks
perf symbol-elf: Correct holding a reference perf maps: Fix overlapping memory leak
perf machine: Fix leak of kernel dso
perf machine: Don't leak module maps
perf map/maps/thread: Changes to reference countingtools/perf/arch/arm/tests/dwarf-unwind.c | 2 +- tools/perf/arch/arm64/tests/dwarf-unwind.c | 2 +- tools/perf/arch/powerpc/tests/dwarf-unwind.c | 2 +- tools/perf/arch/x86/tests/dwarf-unwind.c | 2 +- tools/perf/builtin-annotate.c | 28 +- tools/perf/builtin-c2c.c | 18 +- tools/perf/builtin-diff.c | 16 +- tools/perf/builtin-inject.c | 4 +- tools/perf/builtin-kmem.c | 13 +- tools/perf/builtin-kwork.c | 15 +- tools/perf/builtin-mem.c | 4 +- tools/perf/builtin-report.c | 21 +- tools/perf/builtin-sched.c | 80 +++-- tools/perf/builtin-script.c | 97 +++--- tools/perf/builtin-stat.c | 1 + tools/perf/builtin-timechart.c | 11 +- tools/perf/builtin-top.c | 8 +- tools/perf/builtin-trace.c | 38 ++- .../scripts/python/Perf-Trace-Util/Context.c | 4 +- tools/perf/tests/code-reading.c | 6 +- tools/perf/tests/dwarf-unwind.c | 1 - tools/perf/tests/hists_common.c | 2 +- tools/perf/tests/hists_cumulate.c | 18 +- tools/perf/tests/hists_filter.c | 11 +- tools/perf/tests/hists_link.c | 20 +- tools/perf/tests/hists_output.c | 12 +- tools/perf/tests/maps.c | 2 +- tools/perf/tests/mmap-thread-lookup.c | 5 +- tools/perf/tests/perf-targz-src-pkg | 5 +- tools/perf/tests/symbols.c | 1 - tools/perf/tests/thread-maps-share.c | 13 +- tools/perf/trace/beauty/pid.c | 4 +- tools/perf/ui/browsers/hists.c | 19 +- tools/perf/ui/hist.c | 5 +- tools/perf/ui/stdio/hist.c | 2 +- tools/perf/util/Build | 1 + tools/perf/util/addr_location.c | 44 +++ tools/perf/util/addr_location.h | 31 ++ tools/perf/util/arm-spe.c | 4 +- tools/perf/util/build-id.c | 2 + tools/perf/util/callchain.c | 7 +- tools/perf/util/cs-etm.c | 28 +- tools/perf/util/data-convert-json.c | 16 +- tools/perf/util/db-export.c | 20 +- tools/perf/util/dlfilter.c | 17 +- tools/perf/util/event.c | 37 +-- tools/perf/util/evlist.c | 2 + tools/perf/util/evsel_fprintf.c | 8 +- tools/perf/util/header.c | 12 +- tools/perf/util/hist.c | 22 +- tools/perf/util/intel-bts.c | 2 +- tools/perf/util/intel-pt.c | 88 +++--- tools/perf/util/jitdump.c | 12 +- tools/perf/util/machine.c | 277 +++++++++--------- tools/perf/util/map.c | 2 +- tools/perf/util/maps.c | 5 +- tools/perf/util/maps.h | 9 +- tools/perf/util/python.c | 4 + .../scripting-engines/trace-event-python.c | 28 +- tools/perf/util/session.c | 8 +- tools/perf/util/sort.c | 12 +- tools/perf/util/symbol-elf.c | 4 +- tools/perf/util/symbol.h | 17 +- tools/perf/util/thread-stack.c | 25 +- tools/perf/util/thread.c | 218 +++++++------- tools/perf/util/thread.h | 210 +++++++++++-- tools/perf/util/unwind-libdw.c | 27 +- tools/perf/util/unwind-libunwind-local.c | 19 +- tools/perf/util/unwind-libunwind.c | 2 +- tools/perf/util/vdso.c | 2 +- 70 files changed, 1059 insertions(+), 655 deletions(-) create mode 100644 tools/perf/util/addr_location.c create mode 100644 tools/perf/util/addr_location.h--
2.41.0.rc0.172.g3f132b7071-goog