diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2019-08-09 16:53:39 +0200 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2019-08-09 16:53:39 +0200 |
commit | 0e1c438c44dd9cde56effb44c5f1cfeda72e108d (patch) | |
tree | fa3492d4d7d8b7444e5d8ebe6c78210826333e4b /tools/perf/util | |
parent | c096397c78f766db972f923433031f2dec01cae0 (diff) | |
parent | cdb2d3ee0436d74fa9092f2df46aaa6f9e03c969 (diff) |
Merge tag 'kvmarm-fixes-for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into HEAD
KVM/arm fixes for 5.3
- A bunch of switch/case fall-through annotation, fixing one actual bug
- Fix PMU reset bug
- Add missing exception class debug strings
Diffstat (limited to 'tools/perf/util')
90 files changed, 567 insertions, 330 deletions
diff --git a/tools/perf/util/Build b/tools/perf/util/Build index d3408a463060..14f812bb07a7 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -20,11 +20,13 @@ perf-y += parse-events.o perf-y += perf_regs.o perf-y += path.o perf-y += print_binary.o +perf-y += rlimit.o perf-y += argv_split.o perf-y += rbtree.o perf-y += libstring.o perf-y += bitmap.o perf-y += hweight.o +perf-y += zalloc.o perf-y += smt.o perf-y += strbuf.o perf-y += string.o @@ -241,3 +243,7 @@ $(OUTPUT)util/hweight.o: ../lib/hweight.c FORCE $(OUTPUT)util/vsprintf.o: ../lib/vsprintf.c FORCE $(call rule_mkdir) $(call if_changed_dep,cc_o_c) + +$(OUTPUT)util/zalloc.o: ../lib/zalloc.c FORCE + $(call rule_mkdir) + $(call if_changed_dep,cc_o_c) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index ec7aaf31c2b2..ac9ad2330f93 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1119,16 +1119,14 @@ static int disasm_line__parse(char *line, const char **namep, char **rawp) *namep = strdup(name); if (*namep == NULL) - goto out_free_name; + goto out; (*rawp)[0] = tmp; *rawp = skip_spaces(*rawp); return 0; -out_free_name: - free((void *)namep); - *namep = NULL; +out: return -1; } @@ -1237,8 +1235,7 @@ void disasm_line__free(struct disasm_line *dl) dl->ins.ops->free(&dl->ops); else ins__delete(&dl->ops); - free((void *)dl->ins.name); - dl->ins.name = NULL; + zfree(&dl->ins.name); annotation_line__delete(&dl->al); } @@ -1589,7 +1586,7 @@ static void delete_last_nop(struct symbol *sym) return; } - list_del(&dl->al.node); + list_del_init(&dl->al.node); disasm_line__free(dl); } } @@ -2466,7 +2463,7 @@ void annotated_source__purge(struct annotated_source *as) struct annotation_line *al, *n; list_for_each_entry_safe(al, n, &as->source, node) { - list_del(&al->node); + list_del_init(&al->node); disasm_line__free(disasm_line(al)); } } diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c index 6067267cc76c..a314e5b26e9d 100644 --- a/tools/perf/util/arm-spe.c +++ b/tools/perf/util/arm-spe.c @@ -12,6 +12,7 @@ #include <linux/types.h> #include <linux/bitops.h> #include <linux/log2.h> +#include <linux/zalloc.h> #include "cpumap.h" #include "color.h" @@ -19,7 +20,6 @@ #include "evlist.h" #include "machine.h" #include "session.h" -#include "util.h" #include "thread.h" #include "debug.h" #include "auxtrace.h" diff --git a/tools/perf/util/auxtrace.c b/tools/perf/util/auxtrace.c index bc215fe0b4b4..ec0af36697c4 100644 --- a/tools/perf/util/auxtrace.c +++ b/tools/perf/util/auxtrace.c @@ -24,9 +24,9 @@ #include <stdlib.h> #include <stdio.h> #include <linux/list.h> +#include <linux/zalloc.h> #include "../perf.h" -#include "util.h" #include "evlist.h" #include "dso.h" #include "map.h" @@ -408,7 +408,7 @@ void auxtrace_queues__free(struct auxtrace_queues *queues) buffer = list_entry(queues->queue_array[i].head.next, struct auxtrace_buffer, list); - list_del(&buffer->list); + list_del_init(&buffer->list); auxtrace_buffer__free(buffer); } } @@ -612,7 +612,7 @@ void auxtrace_index__free(struct list_head *head) struct auxtrace_index *auxtrace_index, *n; list_for_each_entry_safe(auxtrace_index, n, head, list) { - list_del(&auxtrace_index->list); + list_del_init(&auxtrace_index->list); free(auxtrace_index); } } @@ -1413,7 +1413,7 @@ void auxtrace_cache__free(struct auxtrace_cache *c) return; auxtrace_cache__drop(c); - free(c->hashtable); + zfree(&c->hashtable); free(c); } @@ -1459,12 +1459,11 @@ void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key) static void addr_filter__free_str(struct addr_filter *filt) { - free(filt->str); + zfree(&filt->str); filt->action = NULL; filt->sym_from = NULL; filt->sym_to = NULL; filt->filename = NULL; - filt->str = NULL; } static struct addr_filter *addr_filter__new(void) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 251d9ea6252f..c61974a50aa5 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -12,6 +12,7 @@ #include <linux/err.h> #include <linux/kernel.h> #include <linux/string.h> +#include <linux/zalloc.h> #include <errno.h> #include "perf.h" #include "debug.h" @@ -828,7 +829,7 @@ static void bpf_map_op__delete(struct bpf_map_op *op) { if (!list_empty(&op->list)) - list_del(&op->list); + list_del_init(&op->list); if (op->key_type == BPF_MAP_KEY_RANGES) parse_events__clear_array(&op->k.array); free(op); diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index 89c6913dfc25..f1abfab7aa8c 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -30,6 +30,7 @@ #include "strlist.h" #include <linux/ctype.h> +#include <linux/zalloc.h> static bool no_buildid_cache; diff --git a/tools/perf/util/call-path.c b/tools/perf/util/call-path.c index c5b90300304d..5c60b8be1cf6 100644 --- a/tools/perf/util/call-path.c +++ b/tools/perf/util/call-path.c @@ -6,8 +6,9 @@ #include <linux/rbtree.h> #include <linux/list.h> +#include <linux/zalloc.h> +#include <stdlib.h> -#include "util.h" #include "call-path.h" static void call_path__init(struct call_path *cp, struct call_path *parent, @@ -39,7 +40,7 @@ void call_path_root__free(struct call_path_root *cpr) struct call_path_block *pos, *n; list_for_each_entry_safe(pos, n, &cpr->blocks, node) { - list_del(&pos->node); + list_del_init(&pos->node); free(pos); } free(cpr); diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index abb608b09269..8d7d8f62fcca 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -16,11 +16,11 @@ #include <stdbool.h> #include <errno.h> #include <math.h> +#include <linux/zalloc.h> #include "asm/bug.h" #include "hist.h" -#include "util.h" #include "sort.h" #include "machine.h" #include "map.h" @@ -636,7 +636,7 @@ add_child(struct callchain_node *parent, struct callchain_list *call, *tmp; list_for_each_entry_safe(call, tmp, &new->val, list) { - list_del(&call->list); + list_del_init(&call->list); map__zput(call->ms.map); free(call); } @@ -1002,7 +1002,7 @@ merge_chain_branch(struct callchain_cursor *cursor, callchain_cursor_append(cursor, list->ip, list->ms.map, list->ms.sym, false, NULL, 0, 0, 0, list->srcline); - list_del(&list->list); + list_del_init(&list->list); map__zput(list->ms.map); free(list); } @@ -1453,13 +1453,13 @@ static void free_callchain_node(struct callchain_node *node) struct rb_node *n; list_for_each_entry_safe(list, tmp, &node->parent_val, list) { - list_del(&list->list); + list_del_init(&list->list); map__zput(list->ms.map); free(list); } list_for_each_entry_safe(list, tmp, &node->val, list) { - list_del(&list->list); + list_del_init(&list->list); map__zput(list->ms.map); free(list); } @@ -1544,7 +1544,7 @@ int callchain_node__make_parent_list(struct callchain_node *node) out: list_for_each_entry_safe(chain, new, &head, list) { - list_del(&chain->list); + list_del_init(&chain->list); map__zput(chain->ms.map); free(chain); } diff --git a/tools/perf/util/cgroup.c b/tools/perf/util/cgroup.c index ccd02634a616..484c29830a81 100644 --- a/tools/perf/util/cgroup.c +++ b/tools/perf/util/cgroup.c @@ -1,11 +1,11 @@ // SPDX-License-Identifier: GPL-2.0 -#include "util.h" #include "../perf.h" #include <subcmd/parse-options.h> #include "evsel.h" #include "cgroup.h" #include "evlist.h" #include <linux/stringify.h> +#include <linux/zalloc.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> @@ -124,7 +124,7 @@ static struct cgroup *cgroup__new(const char *name) return cgroup; out_free_name: - free(cgroup->name); + zfree(&cgroup->name); out_err: free(cgroup); return NULL; diff --git a/tools/perf/util/comm.c b/tools/perf/util/comm.c index 1066de92af12..afb8d4fd2644 100644 --- a/tools/perf/util/comm.c +++ b/tools/perf/util/comm.c @@ -1,12 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 #include "comm.h" -#include "util.h" #include <errno.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <linux/refcount.h> #include <linux/rbtree.h> +#include <linux/zalloc.h> #include "rwsem.h" struct comm_str { diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c index 752cce853e51..042ffbc8c53f 100644 --- a/tools/perf/util/config.c +++ b/tools/perf/util/config.c @@ -11,7 +11,6 @@ */ #include <errno.h> #include <sys/param.h> -#include "util.h" #include "cache.h" #include "callchain.h" #include <subcmd/exec-cmd.h> @@ -23,7 +22,7 @@ #include <sys/stat.h> #include <unistd.h> #include <linux/string.h> - +#include <linux/zalloc.h> #include <linux/ctype.h> #define MAXNAME (256) diff --git a/tools/perf/util/counts.c b/tools/perf/util/counts.c index 03032b410c29..88be9c4365e0 100644 --- a/tools/perf/util/counts.c +++ b/tools/perf/util/counts.c @@ -3,7 +3,7 @@ #include <stdlib.h> #include "evsel.h" #include "counts.h" -#include "util.h" +#include <linux/zalloc.h> struct perf_counts *perf_counts__new(int ncpus, int nthreads) { diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index 0d8fbedf7bd5..3acfbe34ebaf 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c @@ -1,5 +1,4 @@ // SPDX-License-Identifier: GPL-2.0 -#include "util.h" #include <api/fs/fs.h> #include "../perf.h" #include "cpumap.h" @@ -11,6 +10,7 @@ #include "asm/bug.h" #include <linux/ctype.h> +#include <linux/zalloc.h> static int max_cpu_num; static int max_present_cpu_num; diff --git a/tools/perf/util/cputopo.c b/tools/perf/util/cputopo.c index 26e73a4bd4fe..64336a280967 100644 --- a/tools/perf/util/cputopo.c +++ b/tools/perf/util/cputopo.c @@ -2,11 +2,12 @@ #include <sys/param.h> #include <sys/utsname.h> #include <inttypes.h> +#include <stdlib.h> #include <api/fs/fs.h> +#include <linux/zalloc.h> #include "cputopo.h" #include "cpumap.h" -#include "util.h" #include "env.h" #define CORE_SIB_FMT \ @@ -343,7 +344,7 @@ void numa_topology__delete(struct numa_topology *tp) u32 i; for (i = 0; i < tp->nr; i++) - free(tp->nodes[i].cpus); + zfree(&tp->nodes[i].cpus); free(tp); } diff --git a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c index bb45e23018ee..37d7c492b155 100644 --- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c @@ -8,6 +8,7 @@ #include <linux/err.h> #include <linux/list.h> +#include <linux/zalloc.h> #include <stdlib.h> #include <opencsd/c_api/opencsd_c_api.h> #include <opencsd/etmv4/trc_pkt_types_etmv4.h> diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index 0c7776b51045..3d1c34fc4d68 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -11,6 +11,7 @@ #include <linux/kernel.h> #include <linux/log2.h> #include <linux/types.h> +#include <linux/zalloc.h> #include <opencsd/ocsd_if_types.h> #include <stdlib.h> @@ -554,8 +555,7 @@ static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq) etmq->traceid_queues_list = NULL; /* finally free the traceid_queues array */ - free(etmq->traceid_queues); - etmq->traceid_queues = NULL; + zfree(&etmq->traceid_queues); } static void cs_etm__free_queue(void *priv) @@ -2460,7 +2460,7 @@ int cs_etm__process_auxtrace_info(union perf_event *event, /* Something went wrong, no need to continue */ if (!inode) { - err = PTR_ERR(inode); + err = -ENOMEM; goto err_free_metadata; } @@ -2517,8 +2517,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event, session->auxtrace = &etm->auxtrace; etm->unknown_thread = thread__new(999999999, 999999999); - if (!etm->unknown_thread) + if (!etm->unknown_thread) { + err = -ENOMEM; goto err_free_queues; + } /* * Initialize list node so that at thread__zput() we can avoid @@ -2530,15 +2532,17 @@ int cs_etm__process_auxtrace_info(union perf_event *event, if (err) goto err_delete_thread; - if (thread__init_map_groups(etm->unknown_thread, etm->machine)) + if (thread__init_map_groups(etm->unknown_thread, etm->machine)) { + err = -ENOMEM; goto err_delete_thread; + } if (dump_trace) { cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu); return 0; } - if (session->itrace_synth_opts && session->itrace_synth_opts->set) { + if (session->itrace_synth_opts->set) { etm->synth_opts = *session->itrace_synth_opts; } else { itrace_synth_opts__set_default(&etm->synth_opts, @@ -2568,12 +2572,12 @@ err_free_etm: err_free_metadata: /* No need to check @metadata[j], free(NULL) is supported */ for (j = 0; j < num_cpu; j++) - free(metadata[j]); + zfree(&metadata[j]); zfree(&metadata); err_free_traceid_list: intlist__delete(traceid_list); err_free_hdr: zfree(&hdr); - return -EINVAL; + return err; } diff --git a/tools/perf/util/data-convert-bt.c b/tools/perf/util/data-convert-bt.c index 7b06e7373b9e..ddbcd59f2d9b 100644 --- a/tools/perf/util/data-convert-bt.c +++ b/tools/perf/util/data-convert-bt.c @@ -10,6 +10,7 @@ #include <inttypes.h> #include <linux/compiler.h> #include <linux/kernel.h> +#include <linux/zalloc.h> #include <babeltrace/ctf-writer/writer.h> #include <babeltrace/ctf-writer/clock.h> #include <babeltrace/ctf-writer/stream.h> @@ -22,7 +23,6 @@ #include "asm/bug.h" #include "data-convert-bt.h" #include "session.h" -#include "util.h" #include "debug.h" #include "tool.h" #include "evlist.h" @@ -1353,7 +1353,7 @@ static void free_streams(struct ctf_writer *cw) for (cpu = 0; cpu < cw->stream_cnt; cpu++) ctf_stream__delete(cw->stream[cpu]); - free(cw->stream); + zfree(&cw->stream); } static int ctf_writer__setup_env(struct ctf_writer *cw, diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c index 6a64f713710d..1d1b97a92c3f 100644 --- a/tools/perf/util/data.c +++ b/tools/perf/util/data.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include <linux/compiler.h> #include <linux/kernel.h> +#include <linux/zalloc.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h> @@ -20,7 +21,7 @@ static void close_dir(struct perf_data_file *files, int nr) { while (--nr >= 1) { close(files[nr].fd); - free(files[nr].path); + zfree(&files[nr].path); } free(files); } diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c index 2182f552aac6..ffbb3e7d3288 100644 --- a/tools/perf/util/db-export.c +++ b/tools/perf/util/db-export.c @@ -5,6 +5,7 @@ */ #include <errno.h> +#include <stdlib.h> #include "evsel.h" #include "machine.h" @@ -13,76 +14,20 @@ #include "symbol.h" #include "map.h" #include "event.h" -#include "util.h" #include "thread-stack.h" #include "callchain.h" #include "call-path.h" #include "db-export.h" - -struct deferred_export { - struct list_head node; - struct comm *comm; -}; - -static int db_export__deferred(struct db_export *dbe) -{ - struct deferred_export *de; - int err; - - while (!list_empty(&dbe->deferred)) { - de = list_entry(dbe->deferred.next, struct deferred_export, - node); - err = dbe->export_comm(dbe, de->comm); - list_del(&de->node); - free(de); - if (err) - return err; - } - - return 0; -} - -static void db_export__free_deferred(struct db_export *dbe) -{ - struct deferred_export *de; - - while (!list_empty(&dbe->deferred)) { - de = list_entry(dbe->deferred.next, struct deferred_export, - node); - list_del(&de->node); - free(de); - } -} - -static int db_export__defer_comm(struct db_export *dbe, struct comm *comm) -{ - struct deferred_export *de; - - de = zalloc(sizeof(struct deferred_export)); - if (!de) - return -ENOMEM; - - de->comm = comm; - list_add_tail(&de->node, &dbe->deferred); - - return 0; -} +#include <linux/zalloc.h> int db_export__init(struct db_export *dbe) { memset(dbe, 0, sizeof(struct db_export)); - INIT_LIST_HEAD(&dbe->deferred); return 0; } -int db_export__flush(struct db_export *dbe) -{ - return db_export__deferred(dbe); -} - void db_export__exit(struct db_export *dbe) { - db_export__free_deferred(dbe); call_return_processor__free(dbe->crp); dbe->crp = NULL; } @@ -114,71 +59,73 @@ int db_export__machine(struct db_export *dbe, struct machine *machine) } int db_export__thread(struct db_export *dbe, struct thread *thread, - struct machine *machine, struct comm *comm) + struct machine *machine, struct thread *main_thread) { - struct thread *main_thread; u64 main_thread_db_id = 0; - int err; if (thread->db_id) return 0; thread->db_id = ++dbe->thread_last_db_id; - if (thread->pid_ != -1) { - if (thread->pid_ == thread->tid) { - main_thread = thread; - } else { - main_thread = machine__findnew_thread(machine, - thread->pid_, - thread->pid_); - if (!main_thread) - return -ENOMEM; - err = db_export__thread(dbe, main_thread, machine, - comm); - if (err) - goto out_put; - if (comm) { - err = db_export__comm_thread(dbe, comm, thread); - if (err) - goto out_put; - } - } + if (main_thread) main_thread_db_id = main_thread->db_id; - if (main_thread != thread) - thread__put(main_thread); - } if (dbe->export_thread) return dbe->export_thread(dbe, thread, main_thread_db_id, machine); return 0; +} -out_put: - thread__put(main_thread); - return err; +static int __db_export__comm(struct db_export *dbe, struct comm *comm, + struct thread *thread) +{ + comm->db_id = ++dbe->comm_last_db_id; + + if (dbe->export_comm) + return dbe->export_comm(dbe, comm, thread); + + return 0; } int db_export__comm(struct db_export *dbe, struct comm *comm, - struct thread *main_thread) + struct thread *thread) +{ + if (comm->db_id) + return 0; + + return __db_export__comm(dbe, comm, thread); +} + +/* + * Export the "exec" comm. The "exec" comm is the program / application command + * name at the time it first executes. It is used to group threads for the same + * program. Note that the main thread pid (or thread group id tgid) cannot be + * used because it does not change when a new program is exec'ed. + */ +int db_export__exec_comm(struct db_export *dbe, struct comm *comm, + struct thread *main_thread) { int err; if (comm->db_id) return 0; - comm->db_id = ++dbe->comm_last_db_id; - - if (dbe->export_comm) { - if (main_thread->comm_set) - err = dbe->export_comm(dbe, comm); - else - err = db_export__defer_comm(dbe, comm); - if (err) - return err; - } + err = __db_export__comm(dbe, comm, main_thread); + if (err) + return err; + /* + * Record the main thread for this comm. Note that the main thread can + * have many "exec" comms because there will be a new one every time it + * exec's. An "exec" comm however will only ever have 1 main thread. + * That is different to any other threads for that same program because + * exec() will effectively kill them, so the relationship between the + * "exec" comm and non-main threads is 1-to-1. That is why + * db_export__comm_thread() is called here for the main thread, but it + * is called for non-main threads when they are exported. + */ return db_export__comm_thread(dbe, comm, main_thread); } @@ -339,11 +286,65 @@ int db_export__branch_type(struct db_export *dbe, u32 branch_type, return 0; } +static int db_export__threads(struct db_export *dbe, struct thread *thread, + struct thread *main_thread, + struct machine *machine, struct comm **comm_ptr) +{ + struct comm *comm = NULL; + struct comm *curr_comm; + int err; + + if (main_thread) { + /* + * A thread has a reference to the main thread, so export the + * main thread first. + */ + err = db_export__thread(dbe, main_thread, machine, main_thread); + if (err) + return err; + /* + * Export comm before exporting the non-main thread because + * db_export__comm_thread() can be called further below. + */ + comm = machine__thread_exec_comm(machine, main_thread); + if (comm) { + err = db_export__exec_comm(dbe, comm, main_thread); + if (err) + return err; + *comm_ptr = comm; + } + } + + if (thread != main_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; + + err = db_export__thread(dbe, thread, machine, main_thread); + if (err) + return err; + + if (export_comm_thread) { + err = db_export__comm_thread(dbe, comm, thread); + if (err) + return err; + } + } + + curr_comm = thread__comm(thread); + if (curr_comm) + return db_export__comm(dbe, curr_comm, thread); + + return 0; +} + int db_export__sample(struct db_export *dbe, union perf_event *event, struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al) { - struct thread* thread = al->thread; + struct thread *thread = al->thread; struct export_sample es = { .event = event, .sample = sample, @@ -363,19 +364,13 @@ int db_export__sample(struct db_export *dbe, union perf_event *event, return err; main_thread = thread__main_thread(al->machine, thread); - if (main_thread) - comm = machine__thread_exec_comm(al->machine, main_thread); - err = db_export__thread(dbe, thread, al->machine, comm); + err = db_export__threads(dbe, thread, main_thread, al->machine, &comm); if (err) goto out_put; - if (comm) { - err = db_export__comm(dbe, comm, main_thread); - if (err) - goto out_put; + if (comm) es.comm_db_id = comm->db_id; - } es.db_id = ++dbe->sample_last_db_id; @@ -524,3 +519,92 @@ int db_export__call_return(struct db_export *dbe, struct call_return *cr, return 0; } + +static int db_export__pid_tid(struct db_export *dbe, struct machine *machine, + pid_t pid, pid_t tid, u64 *db_id, + struct comm **comm_ptr, bool *is_idle) +{ + struct thread *thread = machine__find_thread(machine, pid, tid); + struct thread *main_thread; + int err = 0; + + if (!thread || !thread->comm_set) + goto out_put; + + *is_idle = !thread->pid_ && !thread->tid; + + main_thread = thread__main_thread(machine, thread); + + err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr); + + *db_id = thread->db_id; + + thread__put(main_thread); +out_put: + thread__put(thread); + + return err; +} + +int db_export__switch(struct db_export *dbe, union perf_event *event, + struct perf_sample *sample, struct machine *machine) +{ + bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; + bool out_preempt = out && + (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT); + int flags = out | (out_preempt << 1); + bool is_idle_a = false, is_idle_b = false; + u64 th_a_id = 0, th_b_id = 0; + u64 comm_out_id, comm_in_id; + struct comm *comm_a = NULL; + struct comm *comm_b = NULL; + u64 th_out_id, th_in_id; + u64 db_id; + int err; + + err = db_export__machine(dbe, machine); + if (err) + return err; + + err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid, + &th_a_id, &comm_a, &is_idle_a); + if (err) + return err; + + if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) { + pid_t pid = event->context_switch.next_prev_pid; + pid_t tid = event->context_switch.next_prev_tid; + + err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id, + &comm_b, &is_idle_b); + if (err) + return err; + } + + /* + * Do not export if both threads are unknown (i.e. not being traced), + * or one is unknown and the other is the idle task. + */ + if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b)) + return 0; + + db_id = ++dbe->context_switch_last_db_id; + + if (out) { + th_out_id = th_a_id; + th_in_id = th_b_id; + comm_out_id = comm_a ? comm_a->db_id : 0; + comm_in_id = comm_b ? comm_b->db_id : 0; + } else { + th_out_id = th_b_id; + th_in_id = th_a_id; + comm_out_id = comm_b ? comm_b->db_id : 0; + comm_in_id = comm_a ? comm_a->db_id : 0; + } + + if (dbe->export_context_switch) + return dbe->export_context_switch(dbe, db_id, machine, sample, + th_out_id, comm_out_id, + th_in_id, comm_in_id, flags); + return 0; +} diff --git a/tools/perf/util/db-export.h b/tools/perf/util/db-export.h index e8a64028a386..ba1f62a5fe10 100644 --- a/tools/perf/util/db-export.h +++ b/tools/perf/util/db-export.h @@ -43,7 +43,8 @@ struct db_export { int (*export_machine)(struct db_export *dbe, struct machine *machine); int (*export_thread)(struct db_export *dbe, struct thread *thread, u64 main_thread_db_id, struct machine *machine); - int (*export_comm)(struct db_export *dbe, struct comm *comm); + int (*export_comm)(struct db_export *dbe, struct comm *comm, + struct thread *thread); int (*export_comm_thread)(struct db_export *dbe, u64 db_id, struct comm *comm, struct thread *thread); int (*export_dso)(struct db_export *dbe, struct dso *dso, @@ -56,6 +57,11 @@ struct db_export { int (*export_call_path)(struct db_export *dbe, struct call_path *cp); int (*export_call_return)(struct db_export *dbe, struct call_return *cr); + int (*export_context_switch)(struct db_export *dbe, u64 db_id, + struct machine *machine, + struct perf_sample *sample, + u64 th_out_id, u64 comm_out_id, + u64 th_in_id, u64 comm_in_id, int flags); struct call_return_processor *crp; struct call_path_root *cpr; u64 evsel_last_db_id; @@ -68,18 +74,19 @@ struct db_export { u64 sample_last_db_id; u64 call_path_last_db_id; u64 call_return_last_db_id; - struct list_head deferred; + u64 context_switch_last_db_id; }; int db_export__init(struct db_export *dbe); -int db_export__flush(struct db_export *dbe); void db_export__exit(struct db_export *dbe); int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel); int db_export__machine(struct db_export *dbe, struct machine *machine); int db_export__thread(struct db_export *dbe, struct thread *thread, - struct machine *machine, struct comm *comm); + struct machine *machine, struct thread *main_thread); int db_export__comm(struct db_export *dbe, struct comm *comm, - struct thread *main_thread); + struct thread *thread); +int db_export__exec_comm(struct db_export *dbe, struct comm *comm, + struct thread *main_thread); int db_export__comm_thread(struct db_export *dbe, struct comm *comm, struct thread *thread); int db_export__dso(struct db_export *dbe, struct dso *dso, @@ -97,5 +104,7 @@ int db_export__branch_types(struct db_export *dbe); int db_export__call_path(struct db_export *dbe, struct call_path *cp); int db_export__call_return(struct db_export *dbe, struct call_return *cr, u64 *parent_db_id); +int db_export__switch(struct db_export *dbe, union perf_event *event, + struct perf_sample *sample, struct machine *machine); #endif diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c index 3cc578343f48..3780fe42453b 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c @@ -7,6 +7,7 @@ #include <string.h> #include <stdarg.h> #include <stdio.h> +#include <stdlib.h> #include <sys/wait.h> #include <api/debug.h> #include <linux/time64.h> diff --git a/tools/perf/util/demangle-java.c b/tools/perf/util/demangle-java.c index 5b4900d67c80..763328c151e9 100644 --- a/tools/perf/util/demangle-java.c +++ b/tools/perf/util/demangle-java.c @@ -1,14 +1,15 @@ // SPDX-License-Identifier: GPL-2.0 #include <sys/types.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> -#include "util.h" #include "debug.h" #include "symbol.h" #include "demangle-java.h" #include <linux/ctype.h> +#include <linux/kernel.h> enum { MODE_PREFIX = 0, diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index c7fde04400f7..ebc9d46c15a7 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -2,6 +2,7 @@ #include <asm/bug.h> #include <linux/kernel.h> #include <linux/string.h> +#include <linux/zalloc.h> #include <sys/time.h> #include <sys/resource.h> #include <sys/types.h> @@ -21,7 +22,7 @@ #include "dso.h" #include "machine.h" #include "auxtrace.h" -#include "util.h" +#include "util.h" /* O_CLOEXEC for older systems */ #include "debug.h" #include "string2.h" #include "vdso.h" @@ -433,7 +434,7 @@ static void dso__list_add(struct dso *dso) static void dso__list_del(struct dso *dso) { - list_del(&dso->data.open_entry); + list_del_init(&dso->data.open_entry); WARN_ONCE(dso__data_open_cnt <= 0, "DSO data fd counter out of bounds."); dso__data_open_cnt--; diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 218bfea8f8a8..03b2de1f5a35 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -6,7 +6,7 @@ #include <errno.h> #include <inttypes.h> #include <stdbool.h> -#include "util.h" +#include <stdlib.h> #include "debug.h" #include "dwarf-aux.h" #include "string2.h" diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 22eee8942527..9909ec40c6d2 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -2,11 +2,12 @@ #include "cpumap.h" #include "env.h" #include <linux/ctype.h> -#include "util.h" +#include <linux/zalloc.h> #include "bpf-event.h" #include <errno.h> #include <sys/utsname.h> #include <bpf/libbpf.h> +#include <stdlib.h> struct perf_env perf_env; @@ -186,7 +187,7 @@ void perf_env__exit(struct perf_env *env) zfree(&env->caches); for (i = 0; i < env->nr_memory_nodes; i++) - free(env->memory_nodes[i].set); + zfree(&env->memory_nodes[i].set); zfree(&env->memory_nodes); } @@ -286,9 +287,9 @@ int perf_env__nr_cpus_avail(struct perf_env *env) void cpu_cache_level__free(struct cpu_cache_level *cache) { - free(cache->type); - free(cache->map); - free(cache->size); + zfree(&cache->type); + zfree(&cache->map); + zfree(&cache->size); } /* diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index e1d0c5ba1f92..f1f4848947ce 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -11,6 +11,7 @@ #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ #include <api/fs/fs.h> #include <linux/perf_event.h> +#include <linux/zalloc.h> #include "event.h" #include "debug.h" #include "hist.h" @@ -855,7 +856,7 @@ free_threads: free(synthesize_threads); free_dirent: for (i = 0; i < n; i++) - free(dirent[i]); + zfree(&dirent[i]); free(dirent); return err; diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index a474ede17cd6..b0364d923f76 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -5,7 +5,6 @@ * Parts came from builtin-{top,stat,record}.c, see those files for further * copyright notes. */ -#include "util.h" #include <api/fs/fs.h> #include <errno.h> #include <inttypes.h> @@ -33,6 +32,7 @@ #include <linux/hash.h> #include <linux/log2.h> #include <linux/err.h> +#include <linux/zalloc.h> #ifdef LACKS_SIGQUEUE_PROTOTYPE int sigqueue(pid_t pid, int sig, const union sigval value); diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 7fb4ae82f34c..ebb46da4dfe5 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -17,6 +17,7 @@ #include <linux/perf_event.h> #include <linux/compiler.h> #include <linux/err.h> +#include <linux/zalloc.h> #include <sys/ioctl.h> #include <sys/resource.h> #include <sys/types.h> @@ -27,7 +28,6 @@ #include "event.h" #include "evsel.h" #include "evlist.h" -#include "util.h" #include "cpumap.h" #include "thread_map.h" #include "target.h" @@ -1298,7 +1298,7 @@ static void perf_evsel__free_config_terms(struct perf_evsel *evsel) struct perf_evsel_config_term *term, *h; list_for_each_entry_safe(term, h, &evsel->config_terms, list) { - list_del(&term->list); + list_del_init(&term->list); free(term); } } diff --git a/tools/perf/util/get_current_dir_name.c b/tools/perf/util/get_current_dir_name.c index 267aa609a582..01f32f26552d 100644 --- a/tools/perf/util/get_current_dir_name.c +++ b/tools/perf/util/get_current_dir_name.c @@ -1,8 +1,8 @@ -// SPDX-License-Identifier: GPL-2.0 -// Copyright (C) 2018, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> +// SPDX-License-Identifier: LGPL-2.1 +// Copyright (C) 2018, 2019 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> // #ifndef HAVE_GET_CURRENT_DIR_NAME -#include "util.h" +#include "get_current_dir_name.h" #include <unistd.h> #include <stdlib.h> #include <stdlib.h> diff --git a/tools/perf/util/get_current_dir_name.h b/tools/perf/util/get_current_dir_name.h new file mode 100644 index 000000000000..69f7d5537d32 --- /dev/null +++ b/tools/perf/util/get_current_dir_name.h @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: LGPL-2.1 +// Copyright (C) 2018, 2019 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> +// +#ifndef __PERF_GET_CURRENT_DIR_NAME_H +#ifndef HAVE_GET_CURRENT_DIR_NAME +char *get_current_dir_name(void); +#endif // HAVE_GET_CURRENT_DIR_NAME +#endif // __PERF_GET_CURRENT_DIR_NAME_H diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 6a93ff5d8db5..c24db7f4909c 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -1,7 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include <errno.h> #include <inttypes.h> -#include "util.h" #include "string2.h" #include <sys/param.h> #include <sys/types.h> @@ -15,6 +14,7 @@ #include <linux/bitops.h> #include <linux/string.h> #include <linux/stringify.h> +#include <linux/zalloc.h> #include <sys/stat.h> #include <sys/utsname.h> #include <linux/time64.h> @@ -1052,7 +1052,7 @@ static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 lev scnprintf(file, PATH_MAX, "%s/size", path); if (sysfs__read_str(file, &cache->size, &len)) { - free(cache->type); + zfree(&cache->type); return -1; } @@ -1061,8 +1061,8 @@ static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 lev scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path); if (sysfs__read_str(file, &cache->map, &len)) { - free(cache->map); - free(cache->type); + zfree(&cache->map); + zfree(&cache->type); return -1; } diff --git a/tools/perf/util/help-unknown-cmd.c b/tools/perf/util/help-unknown-cmd.c index 4f07a5ba5030..ab9e16123626 100644 --- a/tools/perf/util/help-unknown-cmd.c +++ b/tools/perf/util/help-unknown-cmd.c @@ -3,9 +3,11 @@ #include "config.h" #include <poll.h> #include <stdio.h> +#include <stdlib.h> #include <subcmd/help.h> #include "../builtin.h" #include "levenshtein.h" +#include <linux/zalloc.h> static int autocorrect; diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 27cecb59f866..f24fd1954f6c 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -1,6 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 #include "callchain.h" -#include "util.h" #include "build-id.h" #include "hist.h" #include "map.h" @@ -20,6 +19,7 @@ #include <inttypes.h> #include <sys/param.h> #include <linux/time64.h> +#include <linux/zalloc.h> static bool hists__filter_entry_by_dso(struct hists *hists, struct hist_entry *he); @@ -472,16 +472,16 @@ static int hist_entry__init(struct hist_entry *he, return 0; err_srcline: - free(he->srcline); + zfree(&he->srcline); err_rawdata: - free(he->raw_data); + zfree(&he->raw_data); err_infos: if (he->branch_info) { map__put(he->branch_info->from.map); map__put(he->branch_info->to.map); - free(he->branch_info); + zfree(&he->branch_info); } if (he->mem_info) { map__put(he->mem_info->iaddr.map); @@ -489,7 +489,7 @@ err_infos: } err: map__zput(he->ms.map); - free(he->stat_acc); + zfree(&he->stat_acc); return -ENOMEM; } @@ -1254,10 +1254,10 @@ void hist_entry__delete(struct hist_entry *he) zfree(&he->stat_acc); free_srcline(he->srcline); if (he->srcfile && he->srcfile[0]) - free(he->srcfile); + zfree(&he->srcfile); free_callchain(he->callchain); - free(he->trace_output); - free(he->raw_data); + zfree(&he->trace_output); + zfree(&he->raw_data); ops->free(he); } @@ -2741,10 +2741,10 @@ static void hists_evsel__exit(struct perf_evsel *evsel) list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) { perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) { - list_del(&fmt->list); + list_del_init(&fmt->list); free(fmt); } - list_del(&node->list); + list_del_init(&node->list); free(node); } } diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c index e32dbffebb2f..5560e95afdda 100644 --- a/tools/perf/util/intel-bts.c +++ b/tools/perf/util/intel-bts.c @@ -12,6 +12,7 @@ #include <linux/types.h> #include <linux/bitops.h> #include <linux/log2.h> +#include <linux/zalloc.h> #include "cpumap.h" #include "color.h" @@ -21,7 +22,6 @@ #include "map.h" #include "symbol.h" #include "session.h" -#include "util.h" #include "thread.h" #include "thread-stack.h" #include "debug.h" @@ -891,13 +891,12 @@ int intel_bts_process_auxtrace_info(union perf_event *event, if (dump_trace) return 0; - if (session->itrace_synth_opts && session->itrace_synth_opts->set) { + if (session->itrace_synth_opts->set) { bts->synth_opts = *session->itrace_synth_opts; } else { itrace_synth_opts__set_default(&bts->synth_opts, session->itrace_synth_opts->default_no_sample); - if (session->itrace_synth_opts) - bts->synth_opts.thread_stack = + bts->synth_opts.thread_stack = session->itrace_synth_opts->thread_stack; } diff --git a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c index 4d14e78c5927..3bfdf2b7a96a 100644 --- a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c +++ b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c @@ -14,9 +14,9 @@ #include <stdint.h> #include <inttypes.h> #include <linux/compiler.h> +#include <linux/zalloc.h> #include "../cache.h" -#include "../util.h" #include "../auxtrace.h" #include "intel-pt-insn-decoder.h" diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index 470aaae9d930..df061599fef4 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -10,6 +10,7 @@ #include <errno.h> #include <linux/kernel.h> #include <linux/types.h> +#include <linux/zalloc.h> #include "../perf.h" #include "session.h" @@ -22,7 +23,6 @@ #include "evsel.h" #include "map.h" #include "color.h" -#include "util.h" #include "thread.h" #include "thread-stack.h" #include "symbol.h" @@ -3210,7 +3210,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event, goto err_delete_thread; } - if (session->itrace_synth_opts && session->itrace_synth_opts->set) { + if (session->itrace_synth_opts->set) { pt->synth_opts = *session->itrace_synth_opts; } else { itrace_synth_opts__set_default(&pt->synth_opts, @@ -3220,8 +3220,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event, pt->synth_opts.branches = false; pt->synth_opts.callchain = true; } - if (session->itrace_synth_opts) - pt->synth_opts.thread_stack = + pt->synth_opts.thread_stack = session->itrace_synth_opts->thread_stack; } @@ -3241,11 +3240,9 @@ int intel_pt_process_auxtrace_info(union perf_event *event, pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000; } - if (session->itrace_synth_opts) { - err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts); - if (err) - goto err_delete_thread; - } + err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts); + if (err) + goto err_delete_thread; if (pt->synth_opts.calls) pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c index 28908afedec4..18c34f0c1966 100644 --- a/tools/perf/util/jitdump.c +++ b/tools/perf/util/jitdump.c @@ -29,6 +29,7 @@ #include "../builtin.h" #include <linux/ctype.h> +#include <linux/zalloc.h> struct jit_buf_desc { struct perf_data *output; @@ -431,14 +432,12 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr) jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size); if (jd->debug_data && jd->nr_debug_entries) { - free(jd->debug_data); - jd->debug_data = NULL; + zfree(&jd->debug_data); jd->nr_debug_entries = 0; } if (jd->unwinding_data && jd->eh_frame_hdr_size) { - free(jd->unwinding_data); - jd->unwinding_data = NULL; + zfree(&jd->unwinding_data); jd->eh_frame_hdr_size = 0; jd->unwinding_mapped_size = 0; jd->unwinding_size = 0; diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 5b0b60f00275..9f0470ecbca9 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -9,6 +9,7 @@ #include <stdio.h> #include <stdlib.h> #include <linux/err.h> +#include <linux/zalloc.h> #include "debug.h" #include "llvm-utils.h" #include "config.h" @@ -352,8 +353,7 @@ void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) " \toption in [llvm] to \"\" to suppress this detection.\n\n", *kbuild_dir); - free(*kbuild_dir); - *kbuild_dir = NULL; + zfree(kbuild_dir); goto errout; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 147ed85ea2bc..cf826eca3aaf 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -15,7 +15,6 @@ #include "strlist.h" #include "thread.h" #include "vdso.h" -#include "util.h" #include <stdbool.h> #include <sys/types.h> #include <sys/stat.h> @@ -28,6 +27,7 @@ #include <linux/ctype.h> #include <symbol/kallsyms.h> #include <linux/mman.h> +#include <linux/zalloc.h> static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock); @@ -810,7 +810,7 @@ struct map *machine__findnew_module_map(struct machine *machine, u64 start, out: /* put the dso here, corresponding to machine__findnew_module_dso */ dso__put(dso); - free(m.name); + zfree(&m.name); return map; } @@ -1350,7 +1350,7 @@ static int map_groups__set_modules_path_dir(struct map_groups *mg, if (m.kmod) ret = map_groups__set_module_path(mg, path, &m); - free(m.name); + zfree(&m.name); if (ret) goto out; diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 6fce983c6115..668410b1d426 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -12,10 +12,10 @@ #include "thread.h" #include "vdso.h" #include "build-id.h" -#include "util.h" #include "debug.h" #include "machine.h" #include <linux/string.h> +#include <linux/zalloc.h> #include "srcline.h" #include "namespaces.h" #include "unwind.h" @@ -476,8 +476,11 @@ int map__fprintf_srccode(struct map *map, u64 addr, goto out_free_line; ret = fprintf(fp, "|%-8d %.*s", line, len, srccode); - state->srcfile = srcfile; - state->line = line; + + if (state) { + state->srcfile = srcfile; + state->line = line; + } return ret; out_free_line: diff --git a/tools/perf/util/mem2node.c b/tools/perf/util/mem2node.c index c6fd81c02586..cacc2fc4dcbd 100644 --- a/tools/perf/util/mem2node.c +++ b/tools/perf/util/mem2node.c @@ -1,8 +1,8 @@ #include <errno.h> #include <inttypes.h> #include <linux/bitmap.h> +#include <linux/zalloc.h> #include "mem2node.h" -#include "util.h" struct phys_entry { struct rb_node rb_node; diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index d8164574cb16..416a9015405e 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -18,6 +18,7 @@ #include "strlist.h" #include <assert.h> #include <linux/ctype.h> +#include <linux/zalloc.h> struct metric_event *metricgroup__lookup(struct rblist *metric_events, struct perf_evsel *evsel, @@ -235,7 +236,7 @@ static struct rb_node *mep_new(struct rblist *rl __maybe_unused, goto out_name; return &me->nd; out_name: - free((char *)me->name); + zfree(&me->name); out_me: free(me); return NULL; @@ -263,7 +264,7 @@ static void mep_delete(struct rblist *rl __maybe_unused, struct mep *me = container_of(nd, struct mep, nd); strlist__delete(me->metrics); - free((void *)me->name); + zfree(&me->name); free(me); } @@ -489,8 +490,9 @@ static void metricgroup__free_egroups(struct list_head *group_list) list_for_each_entry_safe (eg, egtmp, group_list, nd) { for (i = 0; i < eg->idnum; i++) - free((char *)eg->ids[i]); - free(eg->ids); + zfree(&eg->ids[i]); + zfree(&eg->ids); + list_del_init(&eg->nd); free(eg); } } diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c index 768c632b0d82..9f0b6391af33 100644 --- a/tools/perf/util/mmap.c +++ b/tools/perf/util/mmap.c @@ -9,6 +9,7 @@ #include <sys/mman.h> #include <inttypes.h> #include <asm/bug.h> +#include <linux/zalloc.h> #ifdef HAVE_LIBNUMA_SUPPORT #include <numaif.h> #endif diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c index 023c4efd788d..46d3a7754897 100644 --- a/tools/perf/util/namespaces.c +++ b/tools/perf/util/namespaces.c @@ -5,8 +5,8 @@ */ #include "namespaces.h" -#include "util.h" #include "event.h" +#include "get_current_dir_name.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> @@ -17,6 +17,7 @@ #include <string.h> #include <unistd.h> #include <asm/bug.h> +#include <linux/zalloc.h> struct namespaces *namespaces__new(struct namespaces_event *event) { diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h index 15a5a276c478..004430c0de93 100644 --- a/tools/perf/util/namespaces.h +++ b/tools/perf/util/namespaces.h @@ -13,6 +13,10 @@ #include <linux/refcount.h> #include <linux/types.h> +#ifndef HAVE_SETNS_SUPPORT +int setns(int fd, int nstype); +#endif + struct namespaces_event; struct namespaces { diff --git a/tools/perf/util/ordered-events.c b/tools/perf/util/ordered-events.c index 989fed6f43b5..bb5f34b7ab44 100644 --- a/tools/perf/util/ordered-events.c +++ b/tools/perf/util/ordered-events.c @@ -138,7 +138,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe, if (!list_empty(cache)) { new = list_entry(cache->next, struct ordered_event, list); - list_del(&new->list); + list_del_init(&new->list); } else if (oe->buffer) { new = &oe->buffer->event[oe->buffer_idx]; if (++oe->buffer_idx == MAX_SAMPLE_BUFFER) @@ -394,13 +394,13 @@ void ordered_events__free(struct ordered_events *oe) * yet, we need to free only allocated ones ... */ if (oe->buffer) { - list_del(&oe->buffer->list); + list_del_init(&oe->buffer->list); ordered_events_buffer__free(oe->buffer, oe->buffer_idx, oe); } /* ... and continue with the rest */ list_for_each_entry_safe(buffer, tmp, &oe->to_free, list) { - list_del(&buffer->list); + list_del_init(&buffer->list); ordered_events_buffer__free(buffer, MAX_SAMPLE_BUFFER, oe); } } diff --git a/tools/perf/util/parse-branch-options.c b/tools/perf/util/parse-branch-options.c index bd779d9f4d1e..726e8d9e8c54 100644 --- a/tools/perf/util/parse-branch-options.c +++ b/tools/perf/util/parse-branch-options.c @@ -1,9 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 #include "perf.h" -#include "util/util.h" #include "util/debug.h" #include <subcmd/parse-options.h> #include "util/parse-branch-options.h" +#include <stdlib.h> #define BRANCH_OPT(n, m) \ { .name = n, .mode = (m) } diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index cf0b9b81c5aa..371ff3aee769 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include <linux/hw_breakpoint.h> #include <linux/err.h> +#include <linux/zalloc.h> #include <dirent.h> #include <errno.h> #include <sys/ioctl.h> @@ -651,7 +652,7 @@ static int add_bpf_event(const char *group, const char *event, int fd, pr_debug("Failed to add BPF event %s:%s\n", group, event); list_for_each_entry_safe(evsel, tmp, &new_evsels, node) { - list_del(&evsel->node); + list_del_init(&evsel->node); perf_evsel__delete(evsel); } return err; diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y index 6ad8d4914969..f1c36ed1cf36 100644 --- a/tools/perf/util/parse-events.y +++ b/tools/perf/util/parse-events.y @@ -480,7 +480,6 @@ event_bpf_file: PE_BPF_OBJECT opt_event_config { struct parse_events_state *parse_state = _parse_state; - struct parse_events_error *error = parse_state->error; struct list_head *list; ALLOC_LIST(list); @@ -626,7 +625,6 @@ PE_TERM PE_NAME array '=' PE_NAME { struct parse_events_term *term; - int i; ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER, $1, $4, &@1, &@4)); diff --git a/tools/perf/util/parse-regs-options.c b/tools/perf/util/parse-regs-options.c index 08581e276225..ef46c2848808 100644 --- a/tools/perf/util/parse-regs-options.c +++ b/tools/perf/util/parse-regs-options.c @@ -1,8 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 -#include "perf.h" -#include "util/util.h" +#include <stdbool.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <stdio.h> #include "util/debug.h" #include <subcmd/parse-options.h> +#include "util/perf_regs.h" #include "util/parse-regs-options.h" static int diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 55f4de6442e3..f32b710347db 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -2,6 +2,7 @@ #include <linux/list.h> #include <linux/compiler.h> #include <linux/string.h> +#include <linux/zalloc.h> #include <sys/types.h> #include <errno.h> #include <fcntl.h> @@ -14,7 +15,6 @@ #include <api/fs/fs.h> #include <locale.h> #include <regex.h> -#include "util.h" #include "pmu.h" #include "parse-events.h" #include "cpumap.h" @@ -1245,7 +1245,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, info->metric_expr = alias->metric_expr; info->metric_name = alias->metric_name; - list_del(&term->list); + list_del_init(&term->list); free(term); } diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 6f24eaf6e504..cd1eb73cfe83 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -19,7 +19,6 @@ #include <limits.h> #include <elf.h> -#include "util.h" #include "event.h" #include "namespaces.h" #include "strlist.h" @@ -40,6 +39,7 @@ #include "string2.h" #include <linux/ctype.h> +#include <linux/zalloc.h> #define PERFPROBE_GROUP "probe" @@ -214,9 +214,9 @@ out: static void clear_perf_probe_point(struct perf_probe_point *pp) { - free(pp->file); - free(pp->function); - free(pp->lazy_line); + zfree(&pp->file); + zfree(&pp->function); + zfree(&pp->lazy_line); } static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) @@ -1175,12 +1175,11 @@ int show_available_vars(struct perf_probe_event *pevs __maybe_unused, void line_range__clear(struct line_range *lr) { - free(lr->function); - free(lr->file); - free(lr->path); - free(lr->comp_dir); + zfree(&lr->function); + zfree(&lr->file); + zfree(&lr->path); + zfree(&lr->comp_dir); intlist__delete(lr->line_list); - memset(lr, 0, sizeof(*lr)); } int line_range__init(struct line_range *lr) @@ -1563,6 +1562,17 @@ static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) str = tmp + 1; } + tmp = strchr(str, '@'); + if (tmp && tmp != str && strcmp(tmp + 1, "user")) { /* user attr */ + if (!user_access_is_supported()) { + semantic_error("ftrace does not support user access\n"); + return -EINVAL; + } + *tmp = '\0'; + arg->user_access = true; + pr_debug("user_access "); + } + tmp = strchr(str, ':'); if (tmp) { /* Type setting */ *tmp = '\0'; @@ -2203,15 +2213,15 @@ void clear_perf_probe_event(struct perf_probe_event *pev) struct perf_probe_arg_field *field, *next; int i; - free(pev->event); - free(pev->group); - free(pev->target); + zfree(&pev->event); + zfree(&pev->group); + zfree(&pev->target); clear_perf_probe_point(&pev->point); for (i = 0; i < pev->nargs; i++) { - free(pev->args[i].name); - free(pev->args[i].var); - free(pev->args[i].type); + zfree(&pev->args[i].name); + zfree(&pev->args[i].var); + zfree(&pev->args[i].type); field = pev->args[i].field; while (field) { next = field->next; @@ -2220,8 +2230,7 @@ void clear_perf_probe_event(struct perf_probe_event *pev) field = next; } } - free(pev->args); - memset(pev, 0, sizeof(*pev)); + zfree(&pev->args); } #define strdup_or_goto(str, label) \ @@ -2302,15 +2311,15 @@ void clear_probe_trace_event(struct probe_trace_event *tev) struct probe_trace_arg_ref *ref, *next; int i; - free(tev->event); - free(tev->group); - free(tev->point.symbol); - free(tev->point.realname); - free(tev->point.module); + zfree(&tev->event); + zfree(&tev->group); + zfree(&tev->point.symbol); + zfree(&tev->point.realname); + zfree(&tev->point.module); for (i = 0; i < tev->nargs; i++) { - free(tev->args[i].name); - free(tev->args[i].value); - free(tev->args[i].type); + zfree(&tev->args[i].name); + zfree(&tev->args[i].value); + zfree(&tev->args[i].type); ref = tev->args[i].ref; while (ref) { next = ref->next; @@ -2318,8 +2327,7 @@ void clear_probe_trace_event(struct probe_trace_event *tev) ref = next; } } - free(tev->args); - memset(tev, 0, sizeof(*tev)); + zfree(&tev->args); } struct kprobe_blacklist_node { @@ -2336,8 +2344,8 @@ static void kprobe_blacklist__delete(struct list_head *blacklist) while (!list_empty(blacklist)) { node = list_first_entry(blacklist, struct kprobe_blacklist_node, list); - list_del(&node->list); - free(node->symbol); + list_del_init(&node->list); + zfree(&node->symbol); free(node); } } diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h index 05c8d571a901..96a319cd2378 100644 --- a/tools/perf/util/probe-event.h +++ b/tools/perf/util/probe-event.h @@ -37,6 +37,7 @@ struct probe_trace_point { struct probe_trace_arg_ref { struct probe_trace_arg_ref *next; /* Next reference */ long offset; /* Offset value */ + bool user_access; /* User-memory access */ }; /* kprobe-tracer and uprobe-tracer tracing argument */ @@ -82,6 +83,7 @@ struct perf_probe_arg { char *var; /* Variable name */ char *type; /* Type name */ struct perf_probe_arg_field *field; /* Structure fields */ + bool user_access; /* User-memory access */ }; /* Perf probe probing event (point + arg) */ diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c index 0ed1900454eb..5b4d49382932 100644 --- a/tools/perf/util/probe-file.c +++ b/tools/perf/util/probe-file.c @@ -10,8 +10,8 @@ #include <sys/types.h> #include <sys/uio.h> #include <unistd.h> +#include <linux/zalloc.h> #include "namespaces.h" -#include "util.h" #include "event.h" #include "strlist.h" #include "strfilter.h" @@ -1005,6 +1005,7 @@ enum ftrace_readme { FTRACE_README_PROBE_TYPE_X = 0, FTRACE_README_KRETPROBE_OFFSET, FTRACE_README_UPROBE_REF_CTR, + FTRACE_README_USER_ACCESS, FTRACE_README_END, }; @@ -1017,6 +1018,7 @@ static struct { DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"), DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"), DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"), + DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*[u]<offset>*"), }; static bool scan_ftrace_readme(enum ftrace_readme type) @@ -1077,3 +1079,8 @@ bool uprobe_ref_ctr_is_supported(void) { return scan_ftrace_readme(FTRACE_README_UPROBE_REF_CTR); } + +bool user_access_is_supported(void) +{ + return scan_ftrace_readme(FTRACE_README_USER_ACCESS); +} diff --git a/tools/perf/util/probe-file.h b/tools/perf/util/probe-file.h index 2a249182f2a6..986c1c94f64f 100644 --- a/tools/perf/util/probe-file.h +++ b/tools/perf/util/probe-file.h @@ -70,6 +70,7 @@ int probe_cache__show_all_caches(struct strfilter *filter); bool probe_type_is_available(enum probe_type type); bool kretprobe_offset_is_supported(void); bool uprobe_ref_ctr_is_supported(void); +bool user_access_is_supported(void); #else /* ! HAVE_LIBELF_SUPPORT */ static inline struct probe_cache *probe_cache__new(const char *tgt __maybe_unused, struct nsinfo *nsi __maybe_unused) { diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c index 6b40cc691a2d..025fc4491993 100644 --- a/tools/perf/util/probe-finder.c +++ b/tools/perf/util/probe-finder.c @@ -19,11 +19,11 @@ #include <dwarf-regs.h> #include <linux/bitops.h> +#include <linux/zalloc.h> #include "event.h" #include "dso.h" #include "debug.h" #include "intlist.h" -#include "util.h" #include "strlist.h" #include "symbol.h" #include "probe-finder.h" @@ -280,7 +280,7 @@ static_var: static int convert_variable_type(Dwarf_Die *vr_die, struct probe_trace_arg *tvar, - const char *cast) + const char *cast, bool user_access) { struct probe_trace_arg_ref **ref_ptr = &tvar->ref; Dwarf_Die type; @@ -320,7 +320,8 @@ static int convert_variable_type(Dwarf_Die *vr_die, pr_debug("%s type is %s.\n", dwarf_diename(vr_die), dwarf_diename(&type)); - if (cast && strcmp(cast, "string") == 0) { /* String type */ + if (cast && (!strcmp(cast, "string") || !strcmp(cast, "ustring"))) { + /* String type */ ret = dwarf_tag(&type); if (ret != DW_TAG_pointer_type && ret != DW_TAG_array_type) { @@ -343,6 +344,7 @@ static int convert_variable_type(Dwarf_Die *vr_die, pr_warning("Out of memory error\n"); return -ENOMEM; } + (*ref_ptr)->user_access = user_access; } if (!die_compare_name(&type, "char") && !die_compare_name(&type, "unsigned char")) { @@ -397,7 +399,7 @@ formatted: static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, struct perf_probe_arg_field *field, struct probe_trace_arg_ref **ref_ptr, - Dwarf_Die *die_mem) + Dwarf_Die *die_mem, bool user_access) { struct probe_trace_arg_ref *ref = *ref_ptr; Dwarf_Die type; @@ -434,6 +436,7 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, *ref_ptr = ref; } ref->offset += dwarf_bytesize(&type) * field->index; + ref->user_access = user_access; goto next; } else if (tag == DW_TAG_pointer_type) { /* Check the pointer and dereference */ @@ -505,17 +508,18 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, } } ref->offset += (long)offs; + ref->user_access = user_access; /* If this member is unnamed, we need to reuse this field */ if (!dwarf_diename(die_mem)) return convert_variable_fields(die_mem, varname, field, - &ref, die_mem); + &ref, die_mem, user_access); next: /* Converting next field */ if (field->next) return convert_variable_fields(die_mem, field->name, - field->next, &ref, die_mem); + field->next, &ref, die_mem, user_access); else return 0; } @@ -541,11 +545,12 @@ static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf) else if (ret == 0 && pf->pvar->field) { ret = convert_variable_fields(vr_die, pf->pvar->var, pf->pvar->field, &pf->tvar->ref, - &die_mem); + &die_mem, pf->pvar->user_access); vr_die = &die_mem; } if (ret == 0) - ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type); + ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type, + pf->pvar->user_access); /* *expr will be cached in libdw. Don't free it. */ return ret; } diff --git a/tools/perf/util/pstack.c b/tools/perf/util/pstack.c index 797fe1ae2d2e..28de8a4c2ce8 100644 --- a/tools/perf/util/pstack.c +++ b/tools/perf/util/pstack.c @@ -5,10 +5,10 @@ * (c) 2010 Arnaldo Carvalho de Melo <acme@redhat.com> */ -#include "util.h" #include "pstack.h" #include "debug.h" #include <linux/kernel.h> +#include <linux/zalloc.h> #include <stdlib.h> struct pstack { diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources index 2237bac9fadb..ceb8afdf9a89 100644 --- a/tools/perf/util/python-ext-sources +++ b/tools/perf/util/python-ext-sources @@ -18,6 +18,7 @@ util/namespaces.c ../lib/hweight.c ../lib/string.c ../lib/vsprintf.c +../lib/zalloc.c util/thread_map.c util/util.c util/xyarray.c diff --git a/tools/perf/util/rlimit.c b/tools/perf/util/rlimit.c new file mode 100644 index 000000000000..13521d392a22 --- /dev/null +++ b/tools/perf/util/rlimit.c @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: LGPL-2.1 */ + +#include "util/debug.h" +#include "util/rlimit.h" +#include <sys/time.h> +#include <sys/resource.h> + +/* + * Bump the memlock so that we can get bpf maps of a reasonable size, + * like the ones used with 'perf trace' and with 'perf test bpf', + * improve this to some specific request if needed. + */ +void rlimit__bump_memlock(void) +{ + struct rlimit rlim; + + if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) { + rlim.rlim_cur *= 4; + rlim.rlim_max *= 4; + + if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) { + rlim.rlim_cur /= 2; + rlim.rlim_max /= 2; + + if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) + pr_debug("Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc\n"); + } + } +} diff --git a/tools/perf/util/rlimit.h b/tools/perf/util/rlimit.h new file mode 100644 index 000000000000..9f59d8e710a3 --- /dev/null +++ b/tools/perf/util/rlimit.h @@ -0,0 +1,6 @@ +#ifndef __PERF_RLIMIT_H_ +#define __PERF_RLIMIT_H_ +/* SPDX-License-Identifier: LGPL-2.1 */ + +void rlimit__bump_memlock(void); +#endif // __PERF_RLIMIT_H_ diff --git a/tools/perf/util/s390-cpumsf.c b/tools/perf/util/s390-cpumsf.c index 10d36d9b7909..83d2e149ef19 100644 --- a/tools/perf/util/s390-cpumsf.c +++ b/tools/perf/util/s390-cpumsf.c @@ -146,6 +146,7 @@ #include <linux/types.h> #include <linux/bitops.h> #include <linux/log2.h> +#include <linux/zalloc.h> #include <sys/stat.h> #include <sys/types.h> @@ -156,7 +157,6 @@ #include "evlist.h" #include "machine.h" #include "session.h" -#include "util.h" #include "thread.h" #include "debug.h" #include "auxtrace.h" @@ -756,7 +756,7 @@ static int s390_cpumsf_run_decoder(struct s390_cpumsf_queue *sfq, */ if (err) { sfq->buffer = NULL; - list_del(&buffer->list); + list_del_init(&buffer->list); auxtrace_buffer__free(buffer); if (err > 0) /* Buffer done, no error */ err = 0; @@ -1044,7 +1044,7 @@ static void s390_cpumsf_free(struct perf_session *session) auxtrace_heap__free(&sf->heap); s390_cpumsf_free_queues(session); session->auxtrace = NULL; - free(sf->logdir); + zfree(&sf->logdir); free(sf); } @@ -1101,8 +1101,7 @@ static int s390_cpumsf__config(const char *var, const char *value, void *cb) if (rc == -1 || !S_ISDIR(stbuf.st_mode)) { pr_err("Missing auxtrace log directory %s," " continue with current directory...\n", value); - free(sf->logdir); - sf->logdir = NULL; + zfree(&sf->logdir); } return 1; } @@ -1162,7 +1161,7 @@ err_free_queues: auxtrace_queues__free(&sf->queues); session->auxtrace = NULL; err_free: - free(sf->logdir); + zfree(&sf->logdir); free(sf); return err; } diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index 112bed65232f..25dc1d765553 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -113,6 +113,7 @@ struct tables { PyObject *call_path_handler; PyObject *call_return_handler; PyObject *synth_handler; + PyObject *context_switch_handler; bool db_export_mode; }; @@ -1011,15 +1012,19 @@ static int python_export_thread(struct db_export *dbe, struct thread *thread, return 0; } -static int python_export_comm(struct db_export *dbe, struct comm *comm) +static int python_export_comm(struct db_export *dbe, struct comm *comm, + struct thread *thread) { struct tables *tables = container_of(dbe, struct tables, dbe); PyObject *t; - t = tuple_new(2); + t = tuple_new(5); tuple_set_u64(t, 0, comm->db_id); tuple_set_string(t, 1, comm__str(comm)); + tuple_set_u64(t, 2, thread->db_id); + tuple_set_u64(t, 3, comm->start); + tuple_set_s32(t, 4, comm->exec); call_object(tables->comm_handler, t, "comm_table"); @@ -1233,6 +1238,34 @@ static int python_export_call_return(struct db_export *dbe, return 0; } +static int python_export_context_switch(struct db_export *dbe, u64 db_id, + struct machine *machine, + struct perf_sample *sample, + u64 th_out_id, u64 comm_out_id, + u64 th_in_id, u64 comm_in_id, int flags) +{ + struct tables *tables = container_of(dbe, struct tables, dbe); + PyObject *t; + + t = tuple_new(9); + + tuple_set_u64(t, 0, db_id); + tuple_set_u64(t, 1, machine->db_id); + tuple_set_u64(t, 2, sample->time); + tuple_set_s32(t, 3, sample->cpu); + tuple_set_u64(t, 4, th_out_id); + tuple_set_u64(t, 5, comm_out_id); + tuple_set_u64(t, 6, th_in_id); + tuple_set_u64(t, 7, comm_in_id); + tuple_set_s32(t, 8, flags); + + call_object(tables->context_switch_handler, t, "context_switch"); + + Py_DECREF(t); + + return 0; +} + static int python_process_call_return(struct call_return *cr, u64 *parent_db_id, void *data) { @@ -1296,6 +1329,16 @@ static void python_process_event(union perf_event *event, } } +static void python_process_switch(union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct tables *tables = &tables_global; + + if (tables->db_export_mode) + db_export__switch(&tables->dbe, event, sample, machine); +} + static void get_handler_name(char *str, size_t size, struct perf_evsel *evsel) { @@ -1511,6 +1554,7 @@ static void set_table_handlers(struct tables *tables) SET_TABLE_HANDLER(sample); SET_TABLE_HANDLER(call_path); SET_TABLE_HANDLER(call_return); + SET_TABLE_HANDLER(context_switch); /* * Synthesized events are samples but with architecture-specific data @@ -1620,9 +1664,7 @@ error: static int python_flush_script(void) { - struct tables *tables = &tables_global; - - return db_export__flush(&tables->dbe); + return 0; } /* @@ -1831,6 +1873,7 @@ struct scripting_ops python_scripting_ops = { .flush_script = python_flush_script, .stop_script = python_stop_script, .process_event = python_process_event, + .process_switch = python_process_switch, .process_stat = python_process_stat, .process_stat_interval = python_process_stat_interval, .generate_script = python_generate_script, diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 54cf163347f7..d0fd6c614e68 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -2,6 +2,7 @@ #include <errno.h> #include <inttypes.h> #include <linux/kernel.h> +#include <linux/zalloc.h> #include <traceevent/event-parse.h> #include <api/fs/fs.h> @@ -18,7 +19,6 @@ #include "session.h" #include "tool.h" #include "sort.h" -#include "util.h" #include "cpumap.h" #include "perf_regs.h" #include "asm/bug.h" @@ -1246,9 +1246,12 @@ static void dump_read(struct perf_evsel *evsel, union perf_event *event) return; printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid, - evsel ? perf_evsel__name(evsel) : "FAIL", + perf_evsel__name(evsel), event->read.value); + if (!evsel) + return; + read_format = evsel->attr.read_format; if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) diff --git a/tools/perf/util/setns.c b/tools/perf/util/setns.c index ce8fc290fce8..48f9c0af63b2 100644 --- a/tools/perf/util/setns.c +++ b/tools/perf/util/setns.c @@ -1,4 +1,6 @@ -#include "util.h" +// SPDX-License-Identifier: LGPL-2.1 + +#include "namespaces.h" #include <unistd.h> #include <sys/syscall.h> diff --git a/tools/perf/util/srccode.c b/tools/perf/util/srccode.c index 684b155c222a..adfcf1ff464c 100644 --- a/tools/perf/util/srccode.c +++ b/tools/perf/util/srccode.c @@ -4,7 +4,8 @@ * Copyright (c) 2017, Intel Corporation. * Author: Andi Kleen */ -#include "linux/list.h" +#include <linux/list.h> +#include <linux/zalloc.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/stat.h> @@ -82,12 +83,12 @@ static void fill_lines(char **lines, int maxline, char *map, int maplen) static void free_srcfile(struct srcfile *sf) { - list_del(&sf->nd); + list_del_init(&sf->nd); hlist_del(&sf->hash_nd); map_total_sz -= sf->maplen; munmap(sf->map, sf->maplen); - free(sf->lines); - free(sf->fn); + zfree(&sf->lines); + zfree(&sf->fn); free(sf); num_srcfiles--; } @@ -153,7 +154,7 @@ static struct srcfile *find_srcfile(char *fn) out_map: munmap(h->map, sz); out_fn: - free(h->fn); + zfree(&h->fn); out_h: free(h); return NULL; diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index dcad75daf5e4..6ccf6f6d09df 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -6,9 +6,9 @@ #include <linux/kernel.h> #include <linux/string.h> +#include <linux/zalloc.h> #include "util/dso.h" -#include "util/util.h" #include "util/debug.h" #include "util/callchain.h" #include "util/symbol_conf.h" diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c index cb891e5c2969..656065af4971 100644 --- a/tools/perf/util/stat-shadow.c +++ b/tools/perf/util/stat-shadow.c @@ -8,6 +8,7 @@ #include "evlist.h" #include "expr.h" #include "metricgroup.h" +#include <linux/zalloc.h> /* * AGGR_GLOBAL: Use CPU 0 @@ -775,7 +776,7 @@ static void generic_metric(struct perf_stat_config *config, print_metric(config, ctxp, NULL, NULL, "", 0); for (i = 1; i < pctx.num_ids; i++) - free((void *)pctx.ids[i].name); + zfree(&pctx.ids[i].name); } void perf_stat__print_shadow_stats(struct perf_stat_config *config, diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index d91fe754b6d2..db8a6cf336be 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -6,6 +6,7 @@ #include "evlist.h" #include "evsel.h" #include "thread_map.h" +#include <linux/zalloc.h> void update_stats(struct stats *stats, u64 val) { @@ -132,7 +133,7 @@ static void perf_evsel__free_stat_priv(struct perf_evsel *evsel) struct perf_stat_evsel *ps = evsel->stats; if (ps) - free(ps->group_data); + zfree(&ps->group_data); zfree(&evsel->stats); } diff --git a/tools/perf/util/strbuf.c b/tools/perf/util/strbuf.c index 23092fd6451d..2ce0dc887364 100644 --- a/tools/perf/util/strbuf.c +++ b/tools/perf/util/strbuf.c @@ -1,8 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 #include "debug.h" -#include "util.h" #include <linux/kernel.h> +#include <linux/zalloc.h> #include <errno.h> +#include <stdlib.h> /* * Used as the default ->buf value, so that people can always assume diff --git a/tools/perf/util/strfilter.c b/tools/perf/util/strfilter.c index 90ea2b209cbb..78aa4c3b990d 100644 --- a/tools/perf/util/strfilter.c +++ b/tools/perf/util/strfilter.c @@ -1,11 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 -#include "util.h" #include "string2.h" #include "strfilter.h" #include <errno.h> +#include <stdlib.h> #include <linux/ctype.h> #include <linux/string.h> +#include <linux/zalloc.h> /* Operators */ static const char *OP_and = "&"; /* Logical AND */ diff --git a/tools/perf/util/strlist.c b/tools/perf/util/strlist.c index af45c6fd97db..8a868cbeffae 100644 --- a/tools/perf/util/strlist.c +++ b/tools/perf/util/strlist.c @@ -4,12 +4,12 @@ */ #include "strlist.h" -#include "util.h" #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <linux/zalloc.h> static struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry) diff --git a/tools/perf/util/svghelper.c b/tools/perf/util/svghelper.c index fab8a048d31b..76cc54000483 100644 --- a/tools/perf/util/svghelper.c +++ b/tools/perf/util/svghelper.c @@ -15,10 +15,10 @@ #include <string.h> #include <linux/bitmap.h> #include <linux/time64.h> +#include <linux/zalloc.h> #include "perf.h" #include "svghelper.h" -#include "util.h" #include "cpumap.h" static u64 first_time, last_time; diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 62008756d8cc..7d504dc22108 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -2,6 +2,7 @@ #include <fcntl.h> #include <stdio.h> #include <errno.h> +#include <stdlib.h> #include <string.h> #include <unistd.h> #include <inttypes.h> @@ -16,6 +17,7 @@ #include "debug.h" #include "util.h" #include <linux/ctype.h> +#include <linux/zalloc.h> #include <symbol/kallsyms.h> #ifndef EM_AARCH64 @@ -1476,7 +1478,7 @@ static void kcore_copy__free_phdrs(struct kcore_copy_info *kci) struct phdr_data *p, *tmp; list_for_each_entry_safe(p, tmp, &kci->phdrs, node) { - list_del(&p->node); + list_del_init(&p->node); free(p); } } @@ -1499,7 +1501,7 @@ static void kcore_copy__free_syms(struct kcore_copy_info *kci) struct sym_data *s, *tmp; list_for_each_entry_safe(s, tmp, &kci->syms, node) { - list_del(&s->node); + list_del_init(&s->node); free(s); } } @@ -2131,11 +2133,11 @@ static int populate_sdt_note(Elf **elf, const char *data, size_t len, return 0; out_free_args: - free(tmp->args); + zfree(&tmp->args); out_free_name: - free(tmp->name); + zfree(&tmp->name); out_free_prov: - free(tmp->provider); + zfree(&tmp->provider); out_free_note: free(tmp); out_err: @@ -2250,9 +2252,9 @@ int cleanup_sdt_note_list(struct list_head *sdt_notes) int nr_free = 0; list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) { - list_del(&pos->note_list); - free(pos->name); - free(pos->provider); + list_del_init(&pos->note_list); + zfree(&pos->name); + zfree(&pos->provider); free(pos); nr_free++; } diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c index 17edbd4f6f85..3bc8b7e3300e 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c @@ -7,9 +7,10 @@ #include <stdio.h> #include <fcntl.h> #include <string.h> +#include <stdlib.h> #include <byteswap.h> #include <sys/stat.h> - +#include <linux/zalloc.h> static bool check_need_swap(int file_endian) { diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index ae2ce255e848..173f3378aaa0 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -26,6 +26,7 @@ #include "header.h" #include "path.h" #include <linux/ctype.h> +#include <linux/zalloc.h> #include <elf.h> #include <limits.h> diff --git a/tools/perf/util/syscalltbl.c b/tools/perf/util/syscalltbl.c index c2037ac533f3..022a9c670338 100644 --- a/tools/perf/util/syscalltbl.c +++ b/tools/perf/util/syscalltbl.c @@ -10,9 +10,9 @@ #include <linux/compiler.h> #ifdef HAVE_SYSCALL_TABLE_SUPPORT +#include <linux/zalloc.h> #include <string.h> #include "string2.h" -#include "util.h" #if defined(__x86_64__) #include <asm/syscalls_64.c> diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c index 3852d07c49bd..3adc65480349 100644 --- a/tools/perf/util/target.c +++ b/tools/perf/util/target.c @@ -10,9 +10,9 @@ #include "debug.h" #include <pwd.h> +#include <stdlib.h> #include <string.h> - enum target_errno target__validate(struct target *target) { enum target_errno ret = TARGET_ERRNO__SUCCESS; diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c index 6ff1ff4d4ce7..15134ac9b8f1 100644 --- a/tools/perf/util/thread-stack.c +++ b/tools/perf/util/thread-stack.c @@ -7,12 +7,13 @@ #include <linux/rbtree.h> #include <linux/list.h> #include <linux/log2.h> +#include <linux/zalloc.h> #include <errno.h> +#include <stdlib.h> #include "thread.h" #include "event.h" #include "machine.h" #include "env.h" -#include "util.h" #include "debug.h" #include "symbol.h" #include "comm.h" diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 3e29a4e8b5e6..873ab505ca80 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -5,10 +5,10 @@ #include <stdio.h> #include <string.h> #include <linux/kernel.h> +#include <linux/zalloc.h> #include "session.h" #include "thread.h" #include "thread-stack.h" -#include "util.h" #include "debug.h" #include "namespaces.h" #include "comm.h" @@ -93,14 +93,14 @@ void thread__delete(struct thread *thread) down_write(&thread->namespaces_lock); list_for_each_entry_safe(namespaces, tmp_namespaces, &thread->namespaces_list, list) { - list_del(&namespaces->list); + list_del_init(&namespaces->list); namespaces__free(namespaces); } up_write(&thread->namespaces_lock); down_write(&thread->comm_lock); list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) { - list_del(&comm->list); + list_del_init(&comm->list); comm__free(comm); } up_write(&thread->comm_lock); diff --git a/tools/perf/util/thread_map.c b/tools/perf/util/thread_map.c index 281bf06f10f2..5b3511f2b6b1 100644 --- a/tools/perf/util/thread_map.c +++ b/tools/perf/util/thread_map.c @@ -13,9 +13,9 @@ #include <string.h> #include <api/fs/fs.h> #include <linux/string.h> +#include <linux/zalloc.h> #include "asm/bug.h" #include "thread_map.h" -#include "util.h" #include "debug.h" #include "event.h" @@ -480,7 +480,7 @@ int thread_map__remove(struct thread_map *threads, int idx) /* * Free the 'idx' item and shift the rest up. */ - free(threads->map[idx].comm); + zfree(&threads->map[idx].comm); for (i = idx; i < threads->nr - 1; i++) threads->map[i] = threads->map[i + 1]; diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c index 806a11b334d3..4550015b9d5d 100644 --- a/tools/perf/util/trace-event-info.c +++ b/tools/perf/util/trace-event-info.c @@ -18,6 +18,7 @@ #include <stdbool.h> #include <linux/list.h> #include <linux/kernel.h> +#include <linux/zalloc.h> #include "../perf.h" #include "trace-event.h" diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c index b023db136ef3..ba58f69777a1 100644 --- a/tools/perf/util/trace-event-scripting.c +++ b/tools/perf/util/trace-event-scripting.c @@ -12,8 +12,8 @@ #include "../perf.h" #include "debug.h" -#include "util.h" #include "trace-event.h" +#include <linux/zalloc.h> struct scripting_context *scripting_context; diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h index d9b0a942090a..c7002fe11673 100644 --- a/tools/perf/util/trace-event.h +++ b/tools/perf/util/trace-event.h @@ -81,6 +81,9 @@ struct scripting_ops { struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al); + void (*process_switch)(union perf_event *event, + struct perf_sample *sample, + struct machine *machine); void (*process_stat)(struct perf_stat_config *config, struct perf_evsel *evsel, u64 tstamp); void (*process_stat_interval)(u64 tstamp); diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index 407d0167b942..28f71ca6ce1c 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -12,6 +12,7 @@ #include "symbol.h" #include "thread.h" #include <linux/types.h> +#include <linux/zalloc.h> #include "event.h" #include "perf_regs.h" #include "callchain.h" diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c index 25e1406b1f8b..71a788921b62 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -25,6 +25,7 @@ #include <unistd.h> #include <sys/mman.h> #include <linux/list.h> +#include <linux/zalloc.h> #ifndef REMOTE_UNWIND_LIBUNWIND #include <libunwind.h> #include <libunwind-ptrace.h> @@ -345,7 +346,7 @@ static int read_unwind_spec_debug_frame(struct dso *dso, __func__, dso->symsrc_filename, debuglink); - free(dso->symsrc_filename); + zfree(&dso->symsrc_filename); } dso->symsrc_filename = debuglink; } else { diff --git a/tools/perf/util/usage.c b/tools/perf/util/usage.c index 070d25ceea6a..3949a60b00ae 100644 --- a/tools/perf/util/usage.c +++ b/tools/perf/util/usage.c @@ -9,6 +9,9 @@ */ #include "util.h" #include "debug.h" +#include <stdio.h> +#include <stdlib.h> +#include <linux/compiler.h> static __noreturn void usage_builtin(const char *err) { diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 125e215dd3d8..dc7a469921e9 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -9,8 +9,6 @@ #include <fcntl.h> #include <stdbool.h> #include <stddef.h> -#include <stdlib.h> -#include <stdarg.h> #include <linux/compiler.h> #include <sys/types.h> @@ -18,13 +16,6 @@ void usage(const char *err) __noreturn; void die(const char *err, ...) __noreturn __printf(1, 2); -static inline void *zalloc(size_t size) -{ - return calloc(1, size); -} - -#define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) - struct dirent; struct nsinfo; struct strlist; @@ -59,18 +50,10 @@ int fetch_kernel_version(unsigned int *puint, const char *perf_tip(const char *dirpath); -#ifndef HAVE_GET_CURRENT_DIR_NAME -char *get_current_dir_name(void); -#endif - #ifndef HAVE_SCHED_GETCPU_SUPPORT int sched_getcpu(void); #endif -#ifndef HAVE_SETNS_SUPPORT -int setns(int fd, int nstype); -#endif - extern bool perf_singlethreaded; void perf_set_singlethreaded(void); diff --git a/tools/perf/util/values.c b/tools/perf/util/values.c index 4b7a303e4ba8..c59154e2d124 100644 --- a/tools/perf/util/values.c +++ b/tools/perf/util/values.c @@ -3,8 +3,8 @@ #include <stdio.h> #include <stdlib.h> #include <errno.h> +#include <linux/zalloc.h> -#include "util.h" #include "values.h" #include "debug.h" diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index 5031b7b22bbd..7f427bab6c12 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -16,6 +16,7 @@ #include "machine.h" #include "thread.h" #include "linux/string.h" +#include <linux/zalloc.h> #include "debug.h" /* diff --git a/tools/perf/util/xyarray.c b/tools/perf/util/xyarray.c index dc95154f5646..86889ebc3514 100644 --- a/tools/perf/util/xyarray.c +++ b/tools/perf/util/xyarray.c @@ -1,8 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 #include "xyarray.h" -#include "util.h" #include <stdlib.h> #include <string.h> +#include <linux/zalloc.h> struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size) { |