perf/core improvements and fixes
Arnaldo Carvalho de Melo
maintainerHi Ingo,
Please consider pulling,
- Arnaldo
The following changes since commit a11c51acc52822754d66a11c15f6f6edd4d23c55:
Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux into perf/core (2015-07-21 07:58:06 +0200)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git tags/perf-core-for-mingo
for you to fetch changes up to 7c14898ba9386ee5c939bb418643ac6baff52840:
perf script: Add option --show-switch-events (2015-07-23 22:51:14 -0300)
----------------------------------------------------------------
perf/core improvements and fixes:
New features:
- Introduce PERF_RECORD_SWITCH(_CPU_WIDE) and use it in 'record' to ask for context switches, allowing non priviledged tasks to know when they are switched in and out, which wasn't possible with the other context switch tracepoint and software events, see the patch description for a comprehensive justification (Adrian Hunter)
- Stop collecting /proc/kallsyms in perf.data files, saving about 4.5MB on a typical x86-64 system, use the symbol resolution routines used in all the other tools (report, top, etc) now that we can ask libtraceevent to use perf's symbol resolution code. (Arnaldo Carvalho de Melo)
User visible fixes:
- Expose perf's symbol resolver to libtraceecent, so that its plugins can resolve tracepoint fields to kernel functions, like the 'function' field in the "timer:hrtimer_start tracepoint" (Arnaldo Carvalho de Melo)
Infrastructure:
- Map propagation of thread and cpu maps improvements, prep work for 'perf stat' new features (Jiri Olsa)
----------------------------------------------------------------
Adrian Hunter (5):
perf: Add PERF_RECORD_SWITCH to indicate context switches
perf tools: Add new PERF_RECORD_SWITCH event
perf record: Add option --switch-events to select PERF_RECORD_SWITCH events perf script: Don't assume evsel position of tracking events perf script: Add option --show-switch-events
Arnaldo Carvalho de Melo (8):
perf symbols: Add front end cache for DSO symbol lookup perf symbols: Introduce map__is_(kernel,kmodule)()
tools lib traceevent: Allow setting an alternative symbol resolver perf symbols: Provide libtraceevent callback to resolve kernel symbols perf trace: Provide libtracevent with a kernel symbol resolver perf script: Switch from perf.data's kallsyms to perf's symbol resolver perf tools: Stop reading the kallsyms data from perf.data perf tools: Stop copying kallsyms into the perf.data file header
Jiri Olsa (5):
perf test: Check for refcnt in thread_map test
perf evlist: Force perf_evlist__set_maps to propagate maps through events perf evlist: Use bool instead of target argument in propagate_maps() perf evlist: Tolerate NULL maps in propagate_maps
perf header: Use argv style storage for cmdline feature data
include/uapi/linux/perf_event.h | 31 +++++++++- kernel/events/core.c | 103 +++++++++++++++++++++++++++++++ tools/lib/traceevent/event-parse.c | 68 +++++++++++++++++++- tools/lib/traceevent/event-parse.h | 8 +++ tools/perf/Documentation/perf-record.txt | 4 ++ tools/perf/Documentation/perf-script.txt | 4 ++ tools/perf/builtin-inject.c | 1 + tools/perf/builtin-record.c | 7 +++ tools/perf/builtin-script.c | 48 ++++++++++++-- tools/perf/builtin-trace.c | 3 + tools/perf/perf.h | 1 + tools/perf/tests/thread-map.c | 4 ++ tools/perf/util/dso.h | 4 ++ tools/perf/util/event.c | 28 +++++++++ tools/perf/util/event.h | 12 ++++ tools/perf/util/evlist.c | 28 +++++++-- tools/perf/util/evlist.h | 12 ++-- tools/perf/util/evsel.c | 4 ++ tools/perf/util/header.c | 35 ++++++----- tools/perf/util/header.h | 1 + tools/perf/util/machine.c | 25 ++++++++ tools/perf/util/machine.h | 6 ++ tools/perf/util/map.c | 14 +++++ tools/perf/util/map.h | 7 +++ tools/perf/util/record.c | 10 +++ tools/perf/util/session.c | 21 +++++++ tools/perf/util/symbol.c | 7 ++- tools/perf/util/tool.h | 1 + tools/perf/util/trace-event-info.c | 22 +++---- tools/perf/util/trace-event-parse.c | 30 --------- tools/perf/util/trace-event-read.c | 28 ++++----- tools/perf/util/trace-event.c | 45 +++++++++----- tools/perf/util/trace-event.h | 1 + 33 files changed, 513 insertions(+), 110 deletions(-)
Arnaldo Carvalho de Melo
maintainerPATCH 1/18 perf test: Check for refcnt in thread_map test
From: Jiri Olsa <[email protected]>
Checking also for refcnt in thread_map test.
--- tools/perf/tests/thread-map.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/perf/tests/thread-map.c +4 -0
@@ -20,6 +20,8 @@ int test__thread_map(void)TEST_ASSERT_VAL ("wrong comm" ,thread_map__comm (map,0 ) && !strcmp (thread_map__comm (map,0 ),"perf" )); +TEST_ASSERT_VAL ("wrong refcnt" , +atomic_read (&map->refcnt) ==1 );thread_map__put (map);/* test dummy pid */
@@ -33,6 +35,8 @@ int test__thread_map(void)TEST_ASSERT_VAL ("wrong comm" ,thread_map__comm (map,0 ) && !strcmp (thread_map__comm (map,0 ),"dummy" )); +TEST_ASSERT_VAL ("wrong refcnt" , +atomic_read (&map->refcnt) ==1 );thread_map__put (map);return 0 ; }
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 2/18 perf evlist: Force perf_evlist__set_maps to propagate maps through events
In reply to Arnaldo Carvalho de MeloFrom: Jiri Olsa <[email protected]>
Forcing perf_evlist__set_maps to propagate maps through events, so cpu/thread maps get set within evlist.
--- tools/perf/util/evlist.c | 17 +++++++++++++++++ tools/perf/util/evlist.h | 11 +++-------- 2 files changed, 20 insertions(+), 8 deletions(-)
tools/perf/util/evlist.c +17 -0
@@ -1150,6 +1150,23 @@ out_delete_threads:return -1 ; } +int perf_evlist__set_maps (struct perf_evlist *evlist, +struct cpu_map *cpus, +struct thread_map *threads) +{ +if (evlist->cpus) +cpu_map__put (evlist->cpus); + + evlist->cpus = cpus; + +if (evlist->threads) +thread_map__put (evlist->threads); + + evlist->threads = threads; + +return perf_evlist__propagate_maps (evlist, false); +} +int perf_evlist__apply_filters (struct perf_evlist *evlist,struct perf_evsel **err_evsel) {struct perf_evsel *evsel;
tools/perf/util/evlist.h +3 -8
@@ -152,14 +152,9 @@ int perf_evlist__enable_event_idx(struct perf_evlist *evlist,void perf_evlist__set_selected (struct perf_evlist *evlist,struct perf_evsel *evsel); -static inline void perf_evlist__set_maps (struct perf_evlist *evlist, -struct cpu_map *cpus, -struct thread_map *threads) -{ - evlist->cpus = cpus; - evlist->threads = threads; -} - +int perf_evlist__set_maps (struct perf_evlist *evlist, +struct cpu_map *cpus, +struct thread_map *threads);int perf_evlist__create_maps (struct perf_evlist *evlist,struct target *target);int perf_evlist__apply_filters (struct perf_evlist *evlist,struct perf_evsel **err_evsel);
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 3/18 perf evlist: Use bool instead of target argument in propagate_maps()
In reply to Arnaldo Carvalho de MeloFrom: Jiri Olsa <[email protected]>
We need only bool info wether user defined her own set of cpus.
Switching target argument to bool so it could be used from places without target object defined in following patches.
--- tools/perf/util/evlist.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/util/evlist.c +4 -4
@@ -1102,7 +1102,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, }static int perf_evlist__propagate_maps (struct perf_evlist *evlist, -struct target *target) +bool has_user_cpus) {struct perf_evsel *evsel;
@@ -1111,10 +1111,10 @@ static int perf_evlist__propagate_maps(struct perf_evlist *evlist,* We already have cpus for evsel (via PMU sysfs) so * keep it, if there's no target cpu list defined. */ -if (evsel->cpus && target->cpu_list) +if (evsel->cpus && has_user_cpus)cpu_map__put (evsel->cpus); -if (!evsel->cpus || target->cpu_list) +if (!evsel->cpus || has_user_cpus) evsel->cpus =cpu_map__get (evlist->cpus); evsel->threads =thread_map__get (evlist->threads);
@@ -1142,7 +1142,7 @@ int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)if (evlist->cpus ==NULL ) goto out_delete_threads; -return perf_evlist__propagate_maps (evlist, target); +return perf_evlist__propagate_maps (evlist, !!target->cpu_list); out_delete_threads:thread_map__put (evlist->threads);
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 4/18 perf evlist: Tolerate NULL maps in propagate_maps
In reply to Arnaldo Carvalho de MeloFrom: Jiri Olsa <[email protected]>
Tolerating NULL maps in perf_evlist__propagate_maps, so we dont need to pass evlist with both cpus and threads maps defined.
--- tools/perf/util/evlist.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/evlist.c +2 -1
@@ -1119,7 +1119,8 @@ static int perf_evlist__propagate_maps(struct perf_evlist *evlist, evsel->threads =thread_map__get (evlist->threads); -if (!evsel->cpus || !evsel->threads) +if ((evlist->cpus && !evsel->cpus) || + (evlist->threads && !evsel->threads))return -ENOMEM ; }
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 5/18 perf header: Use argv style storage for cmdline feature data
In reply to Arnaldo Carvalho de MeloFrom: Jiri Olsa <[email protected]>
We will reuse argv style data in following change to display counters header showing monitored command line.
--- tools/perf/util/header.c | 35 ++++++++++++++++++++--------------- tools/perf/util/header.h | 1 + tools/perf/util/session.c | 1 + 3 files changed, 22 insertions(+), 15 deletions(-)
tools/perf/util/header.c +20 -15
@@ -923,17 +923,13 @@ static void print_cmdline(struct perf_header *ph, int fd __maybe_unused, FILE *fp) {int nr, i; -char *str; nr = ph->env.nr_cmdline; - str = ph->env.cmdline;fprintf (fp,"# cmdline : " ); -for (i =0 ; i < nr; i++) { -fprintf (fp,"%s " , str); - str +=strlen (str) +1 ; - } +for (i =0 ; i < nr; i++) +fprintf (fp,"%s " , ph->env.cmdline_argv[i]);fputc ('\n' , fp); }
@@ -1541,14 +1537,13 @@ process_event_desc(struct perf_file_section *section __maybe_unused,return 0 ; } -static int process_cmdline (struct perf_file_section *section__maybe_unused , +static int process_cmdline (struct perf_file_section *section,struct perf_header *ph,int fd,void *data__maybe_unused ) {ssize_t ret; -char *str; -u32 nr, i; -struct strbuf sb; +char *str, *cmdline =NULL , **argv =NULL ; +u32 nr, i, len =0 ; ret =readn (fd, &nr,sizeof (nr));if (ret !=sizeof (nr))
@@ -1558,22 +1553,32 @@ static int process_cmdline(struct perf_file_section *section __maybe_unused, nr =bswap_32 (nr); ph->env.nr_cmdline = nr; -strbuf_init (&sb,128 ); + + cmdline =zalloc (section->size + nr +1 ); +if (!cmdline) +return -1 ; + + argv =zalloc (sizeof (char *) * (nr +1 )); +if (!argv) + goto error;for (i =0 ; i < nr; i++) { str =do_read_string (fd, ph);if (!str) goto error; -/* include a NULL character at the end */ -strbuf_add (&sb, str,strlen (str) +1 ); + argv[i] = cmdline + len; +memcpy (argv[i], str,strlen (str) +1 ); + len +=strlen (str) +1 ;free (str); } - ph->env.cmdline =strbuf_detach (&sb,NULL ); + ph->env.cmdline = cmdline; + ph->env.cmdline_argv = (const char **) argv;return 0 ; error: -strbuf_release (&sb); +free (argv); +free (cmdline);return -1 ; }
tools/perf/util/header.h +1 -0
@@ -84,6 +84,7 @@ struct perf_session_env {int nr_pmu_mappings;int nr_groups;char *cmdline; +const char **cmdline_argv;char *sibling_cores;char *sibling_threads;char *numa_nodes;
tools/perf/util/session.c +1 -0
@@ -180,6 +180,7 @@ static void perf_session_env__delete(struct perf_session_env *env)zfree (&env->cpuid);zfree (&env->cmdline); +zfree (&env->cmdline_argv);zfree (&env->sibling_cores);zfree (&env->sibling_threads);zfree (&env->numa_nodes);
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 6/18 perf symbols: Add front end cache for DSO symbol lookup
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
--- tools/perf/util/dso.h | 4 ++++ tools/perf/util/symbol.c | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
tools/perf/util/dso.h +4 -0
@@ -137,6 +137,10 @@ struct dso {struct rb_node rb_node;/* rbtree node sorted by long name */ struct rb_root symbols[MAP__NR_TYPES ];struct rb_root symbol_names[MAP__NR_TYPES ]; +struct { +u64 addr; +struct symbol *symbol; + } last_find_result[MAP__NR_TYPES ];void *a2l;char *symsrc_filename;unsigned int a2l_fails;
tools/perf/util/symbol.c +6 -1
@@ -444,7 +444,12 @@ static struct symbol *symbols__find_by_name(struct rb_root *symbols,struct symbol *dso__find_symbol (struct dso *dso,enum map_type type,u64 addr) { -return symbols__find (&dso->symbols[type], addr); +if (dso->last_find_result[type].addr != addr) { + dso->last_find_result[type].addr = addr; + dso->last_find_result[type].symbol =symbols__find (&dso->symbols[type], addr); + } + +return dso->last_find_result[type].symbol; }struct symbol *dso__first_symbol (struct dso *dso,enum map_type type)
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 7/18 perf symbols: Introduce map__is_(kernel,kmodule)()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
To, with members we already have, check if a kernel level map is for the kernel proper or for a module.
--- tools/perf/util/map.c | 14 ++++++++++++++ tools/perf/util/map.h | 7 +++++++ 2 files changed, 21 insertions(+)
tools/perf/util/map.c +14 -0
@@ -224,6 +224,20 @@ struct map *map__new2(u64 start, struct dso *dso, enum map_type type)return map; } +/* +* Use this and __map__is_kmodule() for map instances that are in +* machine->kmaps, and thus have map->groups->machine all properly set, to +* disambiguate between the kernel and modules. +* +* When the need arises, introduce map__is_{kernel,kmodule)() that +* checks (map->groups != NULL && map->groups->machine != NULL && +* map->dso->kernel) before calling __map__is_{kernel,kmodule}()) +*/ +bool __map__is_kernel (const struct map *map) +{ +return map->groups->machine->vmlinux_maps[map->type] == map; +} +static void map__exit (struct map *map) {BUG_ON (!RB_EMPTY_NODE (&map->rb_node));
tools/perf/util/map.h +7 -0
@@ -256,4 +256,11 @@ int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,struct map *map_groups__find_by_name (struct map_groups *mg,enum map_type type,const char *name); +bool __map__is_kernel (const struct map *map); + +static inline bool __map__is_kmodule (const struct map *map) +{ +return !__map__is_kernel (map); +} +#endif /* __PERF_MAP_H */
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 8/18 tools lib traceevent: Allow setting an alternative symbol resolver
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
The perf tools have a symbol resolver that includes solving kernel symbols using either kallsyms or ELF symtabs, and it also is using libtraceevent to format the trace events fields, including via subsystem specific plugins, like the "timer" one.
To solve fields like "timer:hrtimer_start"'s "function", libtraceevent needs a way to map from its value to a function name and addr.
This patch provides a way for tools that already have symbol resolving facilities to ask libtraceevent to use it when needing to resolve kernel symbols.
--- tools/lib/traceevent/event-parse.c | 68 +++++++++++++++++++++++++++++++++++++- tools/lib/traceevent/event-parse.h | 8 +++++ 2 files changed, 75 insertions(+), 1 deletion(-)
tools/lib/traceevent/event-parse.c +67 -1
@@ -418,7 +418,7 @@ static int func_map_init(struct pevent *pevent) }static struct func_map * -find_func (struct pevent *pevent,unsigned long long addr) +__find_func (struct pevent *pevent,unsigned long long addr) {struct func_map *func;struct func_map key;
@@ -434,6 +434,71 @@ find_func(struct pevent *pevent, unsigned long long addr)return func; } +struct func_resolver { +pevent_func_resolver_t *func; +void *priv; +struct func_map map; +}; + +/** +* pevent_set_function_resolver - set an alternative function resolver +* @pevent: handle for the pevent +* @resolver: function to be used +* @priv: resolver function private state. +* +* Some tools may have already a way to resolve kernel functions, allow them to +* keep using it instead of duplicating all the entries inside +* pevent->funclist. +*/ +int pevent_set_function_resolver (struct pevent *pevent, +pevent_func_resolver_t *func,void *priv) +{ +struct func_resolver *resolver =malloc (sizeof (*resolver)); + +if (resolver ==NULL ) +return -1 ; + + resolver->func = func; + resolver->priv = priv; + +free (pevent->func_resolver); + pevent->func_resolver = resolver; + +return 0 ; +} + +/** +* pevent_reset_function_resolver - reset alternative function resolver +* @pevent: handle for the pevent +* +* Stop using whatever alternative resolver was set, use the default +* one instead. +*/ +void pevent_reset_function_resolver (struct pevent *pevent) +{ +free (pevent->func_resolver); + pevent->func_resolver =NULL ; +} + +static struct func_map * +find_func (struct pevent *pevent,unsigned long long addr) +{ +struct func_map *map; + +if (!pevent->func_resolver) +return __find_func (pevent, addr); + + map = &pevent->func_resolver->map; + map->mod =NULL ; + map->addr = addr; + map->func = pevent->func_resolver->func (pevent->func_resolver->priv, + &map->addr, &map->mod); +if (map->func ==NULL ) +return NULL ; + +return map; +} +/** * pevent_find_function - find a function by a given address * @pevent: handle for the pevent
@@ -6564,6 +6629,7 @@ void pevent_free(struct pevent *pevent)free (pevent->trace_clock);free (pevent->events);free (pevent->sort_events); +free (pevent->func_resolver);free (pevent); }
tools/lib/traceevent/event-parse.h +8 -0
@@ -453,6 +453,10 @@ struct cmdline_list;struct func_map ;struct func_list ;struct event_handler ; +struct func_resolver ; + +typedef char *(pevent_func_resolver_t )(void *priv, +unsigned long long *addrp,char **modp);struct pevent {int ref_count;
@@ -481,6 +485,7 @@ struct pevent {int cmdline_count;struct func_map *func_map; +struct func_resolver *func_resolver;struct func_list *funclist;unsigned int func_count;
@@ -611,6 +616,9 @@ enum trace_flag_type { TRACE_FLAG_SOFTIRQ = 0x10, }; +int pevent_set_function_resolver (struct pevent *pevent, +pevent_func_resolver_t *func,void *priv); +void pevent_reset_function_resolver (struct pevent *pevent);int pevent_register_comm (struct pevent *pevent,const char *comm,int pid);int pevent_register_trace_clock (struct pevent *pevent,const char *trace_clock); int pevent_register_function(struct pevent *pevent, char *name,
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 9/18 perf symbols: Provide libtraceevent callback to resolve kernel symbols
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
That provides the function signature expected by libtraceevent's pevent_set_function_resolver().
--- tools/perf/util/machine.c | 14 ++++++++++++++ tools/perf/util/machine.h | 4 ++++ tools/perf/util/trace-event.c | 45 ++++++++++++++++++++++++++++--------------- tools/perf/util/trace-event.h | 1 + 4 files changed, 49 insertions(+), 15 deletions(-)
tools/perf/util/machine.c +14 -0
@@ -1993,3 +1993,17 @@ struct dso *machine__findnew_dso(struct machine *machine, const char *filename) {return dsos__findnew (&machine->dsos, filename); } + +char *machine__resolve_kernel_addr (void *vmachine,unsigned long long *addrp,char **modp) +{ +struct machine *machine = vmachine; +struct map *map; +struct symbol *sym =map_groups__find_symbol (&machine->kmaps,MAP__FUNCTION , *addrp, &map,NULL ); + +if (sym ==NULL ) +return NULL ; + + *modp =__map__is_kmodule (map) ? (char *)map->dso->short_name :NULL ; + *addrp = map->unmap_ip (map, sym->start); +return sym->name; +}
tools/perf/util/machine.h +4 -0
@@ -237,5 +237,9 @@ int machine__synthesize_threads(struct machine *machine, struct target *target,pid_t machine__get_current_tid (struct machine *machine,int cpu);int machine__set_current_tid (struct machine *machine,int cpu,pid_t pid,pid_t tid); +/* +* For use with libtraceevent's pevent_set_function_resolver() +*/ +char *machine__resolve_kernel_addr (void *vmachine,unsigned long long *addrp,char **modp);#endif /* __PERF_MACHINE_H */
tools/perf/util/trace-event.c +30 -15
69 lines not on this page · raw message
tools/perf/util/trace-event.h +1 -0
@@ -18,6 +18,7 @@ struct trace_event {int trace_event__init (struct trace_event *t);void trace_event__cleanup (struct trace_event *t); +int trace_event__register_resolver (struct machine *machine);struct event_format *trace_event__tp_format (const char *sys,const char *name);
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 10/18 perf trace: Provide libtracevent with a kernel symbol resolver
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
So that beautifiers wanting to resolve kernel function addresses to names can do its work, now, for instance, the 'timer' tracepoints beautifiers works with 'perf trace', see the "function=tick..." part:
# perf trace --event timer:hrtimer_start <SNIP> 0.000 timer:hrtimer_start:hrtimer=0xffff88026f3101c0 function=tick_sched_timer/0x0 expires=52098339000000 softexpires=52098339000000) 0.003 timer:hrtimer_start:hrtimer=0xffff88026f3101c0 function=tick_sched_timer/0x0 expires=52098339000000 softexpires=52098339000000) <SNIP>
--- tools/perf/builtin-trace.c | 3 +++ 1 file changed, 3 insertions(+)
tools/perf/builtin-trace.c +3 -0
@@ -1489,6 +1489,9 @@ static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)if (trace->host ==NULL )return -ENOMEM ; +if (trace_event__register_resolver (trace->host) <0 ) +return -errno; + err =__machine__synthesize_threads (trace->host, &trace->tool, &trace->opts.target, evlist->threads, trace__tool_process, false, trace->opts.proc_map_timeout);
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 11/18 perf script: Switch from perf.data's kallsyms to perf's symbol resolver
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
We were storing a copy of kallsyms inside perf.data file so that we could resolve kernel addresses to function (start, name, mod) tuples, but that can be achieved using the symbol resolving routines we have in symbols.c, and that are used elsewhere in tools/perf.
So, do just like 'perf trace' did and ask libtraceevent to use perf's symbol resolution routines.
The next step is to just skip whatever kallsyms data is embedded in older perf.data files and finally to stop storing kallsyms in the perf data file, as the 20-bytes build-id stored in perf.data's header is enough to find out the right symtab (be it ELF, kcore, kallsyms, etc) to use.
--- tools/perf/builtin-script.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/perf/builtin-script.c +7 -0
@@ -1830,6 +1830,13 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)else symbol_conf.use_callchain = false; +if (pevent_set_function_resolver (session->tevent.pevent, + machine__resolve_kernel_addr, + &session->machines.host) <0 ) { +pr_err ("%s: failed to set libtraceevent function resolver\n" , __func__); +return -1 ; + } +if (generate_script_lang) {struct stat perf_stat;int input;
Signature · 1 line
2.1.0
Re: [PATCH 11/18] perf script: Switch from perf.data's kallsyms to perf's symbol resolver
Quoted 35 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
We were storing a copy of kallsyms inside perf.data file so that we could resolve kernel addresses to function (start, name, mod) tuples, but that can be achieved using the symbol resolving routines we have in
symbols.c, and that are used elsewhere intools/perf.So, do just like 'perf trace' did and ask libtraceevent to use perf's symbol resolution routines.
The next step is to just skip whatever kallsyms data is embedded in older perf.data files and finally to stop storing kallsyms in the perf data file, as the 20-bytes build-id stored in perf.data's header is enough to find out the right symtab (be it ELF, kcore, kallsyms, etc) to use.
Acked-by David Ahern <[email protected]>Cc Adrian Hunter <[email protected]>Cc Borislav Petkov <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Jiri Olsa <[email protected]>Cc Namhyung Kim <[email protected]>Cc Stephane Eranian <[email protected]>Cc Steven Rostedt <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/builtin-script.c | 7 +++++++ 1 file changed, 7 insertions(+)tools/perf/builtin-script.c +0
-0
tools/perf/builtin-script.c int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) · line 1839
Quoted 10 lines
tools/perf/builtin-script.c +7
-0@@ -1830,6 +1830,13 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)else symbol_conf.use_callchain = false; +if (pevent_set_function_resolver (session->tevent.pevent, + machine__resolve_kernel_addr, + &session->machines.host) <0 ) { +pr_err ("%s: failed to set libtraceevent function resolver\n" , __func__); +return -1 ; + } +
got segfault because of this:
[jolsa@krava perf]$ ./perf record ls
[ perf record: Woken up 1 times to write data ]
SNIP
/proc/kcore requires CAP_SYS_RAWIO capability to access. [ perf record: Captured and wrote 0.016 MB perf.data (7 samples) ] [jolsa@krava perf]$ ./perf script
Segmentation fault (core dumped)
[jolsa@krava perf]$ gdb ./perf
GNU gdb (GDB) Fedora 7.9.1-17.fc22
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"... Reading symbols from ./perf...done.
(gdb) r script
Starting program: /home/jolsa/kernel/linux-perf/tools/perf/perf script Missing separate debuginfos, use: dnf debuginfo-install glibc-2.21-7.fc22.x86_64 [Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1". Detaching after fork from child process 11230.
Program received signal SIGSEGV, Segmentation fault.
0x00000000004eeace in pevent_set_function_resolver (pevent=0x0, func=0x4a3197 <machine__resolve_kernel_addr>, priv=0x18e1f48) at event-parse.c:464
464 free(pevent->func_resolver);
Missing separate debuginfos, use: dnf debuginfo-install audit-libs-2.4.3-1.fc22.x86_64 bzip2-libs-1.0.6-14.fc22.x86_64 elfutils-libelf-0.163-1.fc22.x86_64 elfutils-libs-0.163-1.fc22.x86_64 libunwind-1.1-10.fc22.x86_64 nss-softokn-freebl-3.19.2-1.0.fc22.x86_64 numactl-libs-2.0.10-2.fc22.x86_64 perl-libs-5.20.2-326.fc22.x86_64 python-libs-2.7.10-4.fc22.x86_64 slang-2.3.0-2.fc22.x86_64 xz-libs-5.2.0-2.fc22.x86_64 zlib-1.2.8-7.fc22.x86_64 (gdb)
I think 'session->tevent.pevent' gets initialized only for tracepoint, right?
jirka
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 11/18] perf script: Switch from perf.data's kallsyms to perf's symbol resolver
Em Mon, Aug 03, 2015 at 07:41:13PM +0200, Jiri Olsa escreveu:
Quoted 5 lines
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.21-7.fc22.x86_64 [Thread debugging using
libthread_dbenabled]
Using hostlibthread_dblibrary "/lib64/libthread_db.so.1". Detaching after fork from child process 11230.
Quoted 7 lines
Program received signal SIGSEGV, Segmentation fault.
0x00000000004eeaceinpevent_set_function_resolver(pevent=0x0, func=0x4a3197 <machine__resolve_kernel_addr>, priv=0x18e1f48) at event-parse.c:464
464free(pevent->func_resolver);
Missing separate debuginfos, use: dnf debuginfo-install audit-libs-2.4.3-1.fc22.x86_64 bzip2-libs-1.0.6-14.fc22.x86_64 elfutils-libelf-0.163-1.fc22.x86_64 elfutils-libs-0.163-1.fc22.x86_64 libunwind-1.1-10.fc22.x86_64 nss-softokn-freebl-3.19.2-1.0.fc22.x86_64 numactl-libs-2.0.10-2.fc22.x86_64 perl-libs-5.20.2-326.fc22.x86_64 python-libs-2.7.10-4.fc22.x86_64 slang-2.3.0-2.fc22.x86_64 xz-libs-5.2.0-2.fc22.x86_64 zlib-1.2.8-7.fc22.x86_64 (gdb)I think '
session->tevent.pevent' gets initialized only for tracepoint, right?
Reproduced, checking.
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 11/18] perf script: Switch from perf.data's kallsyms to perf's symbol resolver
Em Mon, Aug 03, 2015 at 04:00:09PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 12 lines
Em Mon, Aug 03, 2015 at 07:41:13PM +0200, Jiri Olsa escreveu:
Quoted 10 lines
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.21-7.fc22.x86_64 [Thread debugging using
libthread_dbenabled]
Using hostlibthread_dblibrary "/lib64/libthread_db.so.1". Detaching after fork from child process 11230.Program received signal SIGSEGV, Segmentation fault.
0x00000000004eeaceinpevent_set_function_resolver(pevent=0x0, func=0x4a3197 <machine__resolve_kernel_addr>, priv=0x18e1f48) at event-parse.c:464
464free(pevent->func_resolver);
Missing separate debuginfos, use: dnf debuginfo-install audit-libs-2.4.3-1.fc22.x86_64 bzip2-libs-1.0.6-14.fc22.x86_64 elfutils-libelf-0.163-1.fc22.x86_64 elfutils-libs-0.163-1.fc22.x86_64 libunwind-1.1-10.fc22.x86_64 nss-softokn-freebl-3.19.2-1.0.fc22.x86_64 numactl-libs-2.0.10-2.fc22.x86_64 perl-libs-5.20.2-326.fc22.x86_64 python-libs-2.7.10-4.fc22.x86_64 slang-2.3.0-2.fc22.x86_64 xz-libs-5.2.0-2.fc22.x86_64 zlib-1.2.8-7.fc22.x86_64 (gdb)
Quoted 3 lines
Quoted 1 line
I think '
session->tevent.pevent' gets initialized only for tracepoint, right?Reproduced, checking.
Yeah, I think we only need to set libtraceevent's function resolver if there are tracepoints, i.e. if libtraceevent is being used. Following patch should be enough, right?
- Arnaldo
tools/perf/builtin-script.c +2 -1
@@ -1861,7 +1861,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)else symbol_conf.use_callchain = false; -if (pevent_set_function_resolver (session->tevent.pevent, +if (session->tevent.pevent && +pevent_set_function_resolver (session->tevent.pevent, machine__resolve_kernel_addr, &session->machines.host) <0 ) {pr_err ("%s: failed to set libtraceevent function resolver\n" , __func__);
Re: [PATCH 11/18] perf script: Switch from perf.data's kallsyms to perf's symbol resolver
Quoted 28 lines · Arnaldo Carvalho de Melo
Em Mon, Aug 03, 2015 at 04:00:09PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 12 lines
Em Mon, Aug 03, 2015 at 07:41:13PM +0200, Jiri Olsa escreveu:
Quoted 10 lines
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.21-7.fc22.x86_64 [Thread debugging using
libthread_dbenabled]
Using hostlibthread_dblibrary "/lib64/libthread_db.so.1". Detaching after fork from child process 11230.Program received signal SIGSEGV, Segmentation fault.
0x00000000004eeaceinpevent_set_function_resolver(pevent=0x0, func=0x4a3197 <machine__resolve_kernel_addr>, priv=0x18e1f48) at event-parse.c:464
464free(pevent->func_resolver);
Missing separate debuginfos, use: dnf debuginfo-install audit-libs-2.4.3-1.fc22.x86_64 bzip2-libs-1.0.6-14.fc22.x86_64 elfutils-libelf-0.163-1.fc22.x86_64 elfutils-libs-0.163-1.fc22.x86_64 libunwind-1.1-10.fc22.x86_64 nss-softokn-freebl-3.19.2-1.0.fc22.x86_64 numactl-libs-2.0.10-2.fc22.x86_64 perl-libs-5.20.2-326.fc22.x86_64 python-libs-2.7.10-4.fc22.x86_64 slang-2.3.0-2.fc22.x86_64 xz-libs-5.2.0-2.fc22.x86_64 zlib-1.2.8-7.fc22.x86_64 (gdb)Quoted 3 lines
Quoted 1 line
I think '
session->tevent.pevent' gets initialized only for tracepoint, right?Reproduced, checking.
Yeah, I think we only need to set libtraceevent's function resolver if there are tracepoints, i.e. if libtraceevent is being used. Following patch should be enough, right?
- Arnaldo
tools/perf/builtin-script.c +0
-0
tools/perf/builtin-script.c int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) · line 1868
Quoted 9 lines
tools/perf/builtin-script.c +2
-1@@ -1861,7 +1861,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)else symbol_conf.use_callchain = false; -if (pevent_set_function_resolver (session->tevent.pevent, +if (session->tevent.pevent && +pevent_set_function_resolver (session->tevent.pevent, machine__resolve_kernel_addr, &session->machines.host) <0 ) {pr_err ("%s: failed to set libtraceevent function resolver\n" , __func__);
yep, works nicely
thanks,
jirka
Arnaldo Carvalho de Melo
maintainerPATCH 12/18 perf tools: Stop reading the kallsyms data from perf.data
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
As it is not used anymore, since 'perf script' switched to asking libtraceevent to use tools/perf's symbol resolution routines.
--- tools/perf/util/trace-event-parse.c | 30 ------------------------------ tools/perf/util/trace-event-read.c | 28 +++++++++++++--------------- 2 files changed, 13 insertions(+), 45 deletions(-)
tools/perf/util/trace-event-parse.c +0 -30
@@ -135,36 +135,6 @@ void event_format__print(struct event_format *event,return event_format__fprintf (event, cpu, data, size, stdout); } -void parse_proc_kallsyms (struct pevent *pevent, -char *file,unsigned int size__maybe_unused ) -{ -unsigned long long addr; -char *func; -char *line; -char *next =NULL ; -char *addr_str; -char *mod; -char *fmt =NULL ; - - line =strtok_r (file,"\n" , &next); -while (line) { - mod =NULL ; - addr_str =strtok_r (line," " , &fmt); - addr =strtoull (addr_str,NULL ,16 ); -/* skip character */ -strtok_r (NULL ," " , &fmt); - func =strtok_r (NULL ,"\t" , &fmt); - mod =strtok_r (NULL ,"]" , &fmt); -/* truncate the extra '[' */ -if (mod) - mod = mod +1 ; - -pevent_register_function (pevent, func, addr, mod); - - line =strtok_r (NULL ,"\n" , &next); - } -} -void parse_ftrace_printk (struct pevent *pevent,char *file,unsigned int size__maybe_unused ) {
tools/perf/util/trace-event-read.c +13 -15
@@ -162,25 +162,23 @@ out:static int read_proc_kallsyms (struct pevent *pevent) {unsigned int size; -char *buf; size =read4 (pevent);if (!size)return 0 ; - - buf =malloc (size +1 ); -if (buf ==NULL ) -return -1 ; - -if (do_read (buf, size) <0 ) { -free (buf); -return -1 ; - } - buf[size] ='\0' ; - -parse_proc_kallsyms (pevent, buf, size); - -free (buf); +/* +* Just skip it, now that we configure libtraceevent to use the +* tools/perf/ symbol resolver. +* +* We need to skip it so that we can continue parsing old perf.data +* files, that contains this /proc/kallsyms payload. +* +* Newer perf.data files will have just the 4-bytes zeros "kallsyms +* payload", so that older tools can continue reading it and interpret +* it as "no kallsyms payload is present". +*/ +lseek (input_fd, size,SEEK_CUR ); + trace_data_size += size;return 0 ; }
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 13/18 perf tools: Stop copying kallsyms into the perf.data file header
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Since we now ask libtraceevent, the only user of this payload, to use perf's symbol resolution routines, there is no need to carry about ~4.5MB per perf.data when we can get it from one of the places the perf symbol resolution looks for that symtab (debuginfo, ~/.debug/, /proc/kallsyms, --symfs, etc), using the kernel and modules build-ids to make sure the right table is used.
--- tools/perf/util/trace-event-info.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-)
tools/perf/util/trace-event-info.c +8 -14
@@ -341,20 +341,14 @@ out:static int record_proc_kallsyms (void ) { -unsigned int size; -const char *path ="/proc/kallsyms" ; -struct stat st; -int ret, err =0 ; - - ret =stat (path, &st); -if (ret <0 ) { -/* not found */ - size =0 ; -if (write (output_fd, &size,4 ) !=4 ) - err = -EIO ; -return err; - } -return record_file (path,4 ); +unsigned long long size =0 ; +/* +* Just to keep older perf.data file parsers happy, record a zero +* sized kallsyms file, i.e. do the same thing that was done when +* /proc/kallsyms (or something specified via --kallsyms, in a +* different path) couldn't be read. +*/ +return write (output_fd, &size,4 ) !=4 ? -EIO :0 ; }static int record_ftrace_printk (void )
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 14/18 perf: Add PERF_RECORD_SWITCH to indicate context switches
In reply to Arnaldo Carvalho de MeloFrom: Adrian Hunter <[email protected]>
There are already two events for context switches, namely the tracepoint sched:sched_switch and the software event context_switches. Unfortunately neither are suitable for use by non-privileged users for the purpose of synchronizing hardware trace data (e.g. Intel PT) to the context switch.
Tracepoints are no good at all for non-privileged users because they need either CAP_SYS_ADMIN or /proc/sys/kernel/perf_event_paranoid <= -1.
On the other hand, kernel software events need either CAP_SYS_ADMIN or /proc/sys/kernel/perf_event_paranoid <= 1.
Now many distributions do default perf_event_paranoid to 1 making context_switches a contender, except it has another problem (which is also shared with sched:sched_switch) which is that it happens before perf schedules events out instead of after perf schedules events in. Whereas a privileged user can see all the events anyway, a non-privileged user only sees events for their own processes, in other words they see when their process was scheduled out not when it was scheduled in. That presents two problems to use the event:
1. the information comes too late, so tools have to look ahead in the event stream to find out what the current state is
2. if they are unlucky tracing might have stopped before the context-switches event is recorded.
This new PERF_RECORD_SWITCH event does not have those problems and it also has a couple of other small advantages.
It is easier to use because it is an auxiliary event (like mmap, comm and task events) which can be enabled by setting a single bit. It is smaller than sched:sched_switch and easier to parse.
To make the event useful for privileged users also, if the context is cpu-wide then the event record will be PERF_RECORD_SWITCH_CPU_WIDE which is the same as PERF_RECORD_SWITCH except it also provides the next or previous pid/tid.
--- include/uapi/linux/perf_event.h | 31 +++++++++++- kernel/events/core.c | 103 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-)
include/uapi/linux/perf_event.h +30 -1
@@ -330,7 +330,8 @@ struct perf_event_attr { mmap2 : 1, /* include mmap with inode data */ comm_exec : 1, /* flag comm events that are due to an exec */ use_clockid : 1, /* use @clockid for time fields */ - __reserved_1 : 38; + context_switch : 1, /* context switch data */ + __reserved_1 : 37;union {__u32 wakeup_events;/* wakeup every n events */
@@ -572,9 +573,11 @@ struct perf_event_mmap_page {/* * PERF_RECORD_MISC_MMAP_DATA and PERF_RECORD_MISC_COMM_EXEC are used on * different events so can reuse the same bit position. +* Ditto PERF_RECORD_MISC_SWITCH_OUT. */ #define PERF_RECORD_MISC_MMAP_DATA (1 << 13)#define PERF_RECORD_MISC_COMM_EXEC (1 << 13) +#define PERF_RECORD_MISC_SWITCH_OUT (1 << 13)/* * Indicates that the content of PERF_SAMPLE_IP points to * the actual instruction that triggered the event. See also
@@ -818,6 +821,32 @@ enum perf_event_type {*/ PERF_RECORD_LOST_SAMPLES =13 , +/* +* Records a context switch in or out (flagged by +* PERF_RECORD_MISC_SWITCH_OUT). See also +* PERF_RECORD_SWITCH_CPU_WIDE. +* +* struct { +* struct perf_event_header header; +* struct sample_id sample_id; +* }; +*/ +PERF_RECORD_SWITCH =14 , + +/* +* CPU-wide version of PERF_RECORD_SWITCH with next_prev_pid and +* next_prev_tid that are the next (switching out) or previous +* (switching in) pid/tid. +* +* struct { +* struct perf_event_header header; +* u32 next_prev_pid; +* u32 next_prev_tid; +* struct sample_id sample_id; +* }; +*/ +PERF_RECORD_SWITCH_CPU_WIDE =15 , + PERF_RECORD_MAX, /* non-ABI */ };
kernel/events/core.c +103 -0
145 lines not on this page · raw message
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 15/18 perf tools: Add new PERF_RECORD_SWITCH event
In reply to Arnaldo Carvalho de MeloFrom: Adrian Hunter <[email protected]>
Support processing of PERF_RECORD_SWITCH events and PERF_RECORD_SWITCH_CPU_WIDE events. There is a single tools callback for them both so that the tool must check the event type before using the extra members in PERF_RECORD_SWITCH_CPU_WIDE.
There is still no way to select the events, though. That is added in a subsequest patch.
--- tools/perf/builtin-inject.c | 1 + tools/perf/util/event.c | 28 ++++++++++++++++++++++++++++ tools/perf/util/event.h | 12 ++++++++++++ tools/perf/util/evsel.c | 1 + tools/perf/util/machine.c | 11 +++++++++++ tools/perf/util/machine.h | 2 ++ tools/perf/util/session.c | 20 ++++++++++++++++++++ tools/perf/util/tool.h | 1 + 8 files changed, 76 insertions(+)
tools/perf/builtin-inject.c +1 -0
@@ -561,6 +561,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused) .lost = perf_event__repipe, .aux = perf_event__repipe, .itrace_start = perf_event__repipe, + .context_switch = perf_event__repipe, .read = perf_event__repipe_sample, .throttle = perf_event__repipe, .unthrottle = perf_event__repipe,
tools/perf/util/event.c +28 -0
@@ -26,6 +26,8 @@ static const char *perf_event__names[] = { [PERF_RECORD_AUX] = "AUX", [PERF_RECORD_ITRACE_START] = "ITRACE_START", [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES", + [PERF_RECORD_SWITCH] = "SWITCH", + [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE", [PERF_RECORD_HEADER_ATTR] = "ATTR", [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
@@ -749,6 +751,14 @@ int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,return machine__process_lost_samples_event (machine, event, sample); } +int perf_event__process_switch (struct perf_tool *tool__maybe_unused , +union perf_event *event, +struct perf_sample *sample__maybe_unused , +struct machine *machine) +{ +return machine__process_switch_event (machine, event); +} +size_t perf_event__fprintf_mmap (union perf_event *event,FILE *fp) { return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
@@ -827,6 +837,20 @@ size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp) event->itrace_start.pid, event->itrace_start.tid); } +size_t perf_event__fprintf_switch (union perf_event *event,FILE *fp) +{ +bool out = event->header.misc &PERF_RECORD_MISC_SWITCH_OUT ; +const char *in_out = out ?"OUT" :"IN " ; + +if (event->header.type ==PERF_RECORD_SWITCH ) +return fprintf (fp," %s\n" , in_out); + +return fprintf (fp," %s %s pid/tid: %5u/%-5u\n" , + in_out, out ?"next" :"prev" , + event->context_switch.next_prev_pid, + event->context_switch.next_prev_tid); +} +size_t perf_event__fprintf (union perf_event *event,FILE *fp) { size_t ret = fprintf(fp, "PERF_RECORD_%s",
@@ -852,6 +876,10 @@ size_t perf_event__fprintf(union perf_event *event, FILE *fp)case PERF_RECORD_ITRACE_START : ret +=perf_event__fprintf_itrace_start (event, fp);break ; +case PERF_RECORD_SWITCH : +case PERF_RECORD_SWITCH_CPU_WIDE : + ret +=perf_event__fprintf_switch (event, fp); +break ;default : ret +=fprintf (fp,"\n" ); }
tools/perf/util/event.h +12 -0
@@ -348,6 +348,12 @@ struct itrace_start_event {u32 pid, tid; }; +struct context_switch_event { +struct perf_event_header header; +u32 next_prev_pid; +u32 next_prev_tid; +}; +union perf_event {struct perf_event_header header;struct mmap_event mmap;
@@ -369,6 +375,7 @@ union perf_event {struct auxtrace_error_event auxtrace_error;struct aux_event aux;struct itrace_start_event itrace_start; +struct context_switch_event context_switch; };void perf_event__print_totals (void );
@@ -418,6 +425,10 @@ int perf_event__process_itrace_start(struct perf_tool *tool,union perf_event *event, struct perf_sample *sample, struct machine *machine); +int perf_event__process_switch (struct perf_tool *tool, +union perf_event *event, +struct perf_sample *sample, +struct machine *machine); int perf_event__process_mmap(struct perf_tool *tool, union perf_event *event, struct perf_sample *sample,
@@ -480,6 +491,7 @@ size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);size_t perf_event__fprintf_task (union perf_event *event,FILE *fp);size_t perf_event__fprintf_aux (union perf_event *event,FILE *fp);size_t perf_event__fprintf_itrace_start (union perf_event *event,FILE *fp); +size_t perf_event__fprintf_switch (union perf_event *event,FILE *fp);size_t perf_event__fprintf (union perf_event *event,FILE *fp); u64 kallsyms__get_function_start(const char *kallsyms_filename,
tools/perf/util/evsel.c +1 -0
@@ -1127,6 +1127,7 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,PRINT_ATTRf (mmap2, p_unsigned);PRINT_ATTRf (comm_exec, p_unsigned);PRINT_ATTRf (use_clockid, p_unsigned); +PRINT_ATTRf (context_switch, p_unsigned);PRINT_ATTRn ("{ wakeup_events, wakeup_watermark }" , wakeup_events, p_unsigned);PRINT_ATTRf (bp_type, p_unsigned);
tools/perf/util/machine.c +11 -0
@@ -550,6 +550,14 @@ int machine__process_itrace_start_event(struct machine *machine __maybe_unused,return 0 ; } +int machine__process_switch_event (struct machine *machine__maybe_unused , +union perf_event *event) +{ +if (dump_trace) +perf_event__fprintf_switch (event, stdout); +return 0 ; +} +struct map *machine__findnew_module_map (struct machine *machine,u64 start,const char *filename) {
@@ -1451,6 +1459,9 @@ int machine__process_event(struct machine *machine, union perf_event *event, ret =machine__process_itrace_start_event (machine, event);break ;case PERF_RECORD_LOST_SAMPLES : ret =machine__process_lost_samples_event (machine, event, sample);break ; +case PERF_RECORD_SWITCH : +case PERF_RECORD_SWITCH_CPU_WIDE : + ret =machine__process_switch_event (machine, event);break ;default : ret =-1 ;break ;
tools/perf/util/machine.h +2 -0
@@ -87,6 +87,8 @@ int machine__process_aux_event(struct machine *machine, union perf_event *event);int machine__process_itrace_start_event (struct machine *machine,union perf_event *event); +int machine__process_switch_event (struct machine *machine__maybe_unused , +union perf_event *event);int machine__process_mmap_event (struct machine *machine,union perf_event *event,struct perf_sample *sample); int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
tools/perf/util/session.c +20 -0
@@ -333,6 +333,8 @@ void perf_tool__fill_defaults(struct perf_tool *tool) tool->aux = perf_event__process_aux;if (tool->itrace_start ==NULL ) tool->itrace_start = perf_event__process_itrace_start; +if (tool->context_switch ==NULL ) + tool->context_switch = perf_event__process_switch;if (tool->read ==NULL ) tool->read = process_event_sample_stub;if (tool->throttle ==NULL )
@@ -471,6 +473,19 @@ static void perf_event__itrace_start_swap(union perf_event *event,swap_sample_id_all (event, &event->itrace_start +1 ); } +static void perf_event__switch_swap (union perf_event *event,bool sample_id_all) +{ +if (event->header.type ==PERF_RECORD_SWITCH_CPU_WIDE ) { + event->context_switch.next_prev_pid = +bswap_32 (event->context_switch.next_prev_pid); + event->context_switch.next_prev_tid = +bswap_32 (event->context_switch.next_prev_tid); + } + +if (sample_id_all) +swap_sample_id_all (event, &event->context_switch +1 ); +} +static void perf_event__throttle_swap (union perf_event *event,bool sample_id_all) {
@@ -633,6 +648,8 @@ static perf_event__swap_op perf_event__swap_ops[] = { [PERF_RECORD_AUX] = perf_event__aux_swap, [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap, [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap, + [PERF_RECORD_SWITCH] = perf_event__switch_swap, + [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap, [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
@@ -1094,6 +1111,9 @@ static int machines__deliver_event(struct machines *machines,return tool->aux (tool, event, sample, machine);case PERF_RECORD_ITRACE_START :return tool->itrace_start (tool, event, sample, machine); +case PERF_RECORD_SWITCH : +case PERF_RECORD_SWITCH_CPU_WIDE : +return tool->context_switch (tool, event, sample, machine);default : ++evlist->stats.nr_unknown_events;return -1 ;
tools/perf/util/tool.h +1 -0
@@ -46,6 +46,7 @@ struct perf_tool { lost_samples, aux, itrace_start, + context_switch, throttle, unthrottle;event_attr_op attr;
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 16/18 perf record: Add option --switch-events to select PERF_RECORD_SWITCH events
In reply to Arnaldo Carvalho de MeloFrom: Adrian Hunter <[email protected]>
Add an option to select PERF_RECORD_SWITCH events.
--- tools/perf/Documentation/perf-record.txt | 4 ++++ tools/perf/builtin-record.c | 7 +++++++ tools/perf/perf.h | 1 + tools/perf/util/evlist.h | 1 + tools/perf/util/evsel.c | 3 +++ tools/perf/util/record.c | 10 ++++++++++ 6 files changed, 26 insertions(+)
tools/perf/Documentation/perf-record.txt +4 -0
@@ -293,6 +293,10 @@ When processing pre-existing threads /proc/XXX/mmap, it may take a long time, because the file may be huge. A time out is needed in such cases. This option sets the time out limit. The default value is 500 ms. +--switch-events:: +Record context switch events i.e. events of type PERF_RECORD_SWITCH or +PERF_RECORD_SWITCH_CPU_WIDE. + SEE ALSO -------- linkperf:perf-stat[1], linkperf:perf-list[1]
tools/perf/builtin-record.c +7 -0
@@ -1075,6 +1075,8 @@ struct option __record_options[] = { "opts", "AUX area tracing Snapshot Mode", ""),OPT_UINTEGER (0 ,"proc-map-timeout" , &record.opts.proc_map_timeout,"per thread proc mmap processing timeout in ms" ), +OPT_BOOLEAN (0 ,"switch-events" , &record.opts.record_switch_events, +"Record context switch events" ), OPT_END() };
@@ -1102,6 +1104,11 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused) " system-wide mode\n");usage_with_options (record_usage, record_options); } +if (rec->opts.record_switch_events && + !perf_can_record_switch_events ()) { +ui__error ("kernel does not support recording context switch events (--switch-events option)\n" ); +usage_with_options (record_usage, record_options); + }if (!rec->itr) { rec->itr =auxtrace_record__init (rec->evlist, &err);
tools/perf/perf.h +1 -0
@@ -57,6 +57,7 @@ struct record_opts {bool running_time;bool full_auxtrace;bool auxtrace_snapshot_mode; +bool record_switch_events;unsigned int freq;unsigned int mmap_pages;unsigned int auxtrace_mmap_pages;
tools/perf/util/evlist.h +1 -0
@@ -114,6 +114,7 @@ void perf_evlist__close(struct perf_evlist *evlist);void perf_evlist__set_id_pos (struct perf_evlist *evlist);bool perf_can_sample_identifier (void ); +bool perf_can_record_switch_events (void );void perf_evlist__config (struct perf_evlist *evlist,struct record_opts *opts);int record_opts__config (struct record_opts *opts);
tools/perf/util/evsel.c +3 -0
@@ -738,6 +738,9 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts) attr->mmap2 = track && !perf_missing_features.mmap2; attr->comm = track; +if (opts->record_switch_events) + attr->context_switch = track; +if (opts->sample_transaction)perf_evsel__set_sample_bit (evsel,TRANSACTION );
tools/perf/util/record.c +10 -0
@@ -85,6 +85,11 @@ static void perf_probe_comm_exec(struct perf_evsel *evsel) evsel->attr.comm_exec =1 ; } +static void perf_probe_context_switch (struct perf_evsel *evsel) +{ + evsel->attr.context_switch =1 ; +} +bool perf_can_sample_identifier (void ) {return perf_probe_api (perf_probe_sample_identifier);
@@ -95,6 +100,11 @@ static bool perf_can_comm_exec(void)return perf_probe_api (perf_probe_comm_exec); } +bool perf_can_record_switch_events (void ) +{ +return perf_probe_api (perf_probe_context_switch); +} +void perf_evlist__config (struct perf_evlist *evlist,struct record_opts *opts) {struct perf_evsel *evsel;
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 17/18 perf script: Don't assume evsel position of tracking events
In reply to Arnaldo Carvalho de MeloFrom: Adrian Hunter <[email protected]>
The tracking event does not have to be the first event so replace perf_evlist__first() with perf_evlist__id2evsel() which uses the event ID to find the correct evsel.
--- tools/perf/builtin-script.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/perf/builtin-script.c +5 -5
@@ -661,7 +661,7 @@ static int process_comm_event(struct perf_tool *tool,struct thread *thread; struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session; -struct perf_evsel *evsel =perf_evlist__first (session->evlist); +struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id);int ret =-1 ; thread =machine__findnew_thread (machine, event->comm.pid, event->comm.tid);
@@ -695,7 +695,7 @@ static int process_fork_event(struct perf_tool *tool,struct thread *thread; struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session; -struct perf_evsel *evsel =perf_evlist__first (session->evlist); +struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id);if (perf_event__process_fork (tool, event, sample, machine) <0 )return -1 ;
@@ -727,7 +727,7 @@ static int process_exit_event(struct perf_tool *tool,struct thread *thread; struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session; -struct perf_evsel *evsel =perf_evlist__first (session->evlist); +struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); thread =machine__findnew_thread (machine, event->fork.pid, event->fork.tid);if (thread ==NULL ) {
@@ -759,7 +759,7 @@ static int process_mmap_event(struct perf_tool *tool,struct thread *thread; struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session; -struct perf_evsel *evsel =perf_evlist__first (session->evlist); +struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id);if (perf_event__process_mmap (tool, event, sample, machine) <0 )return -1 ;
@@ -790,7 +790,7 @@ static int process_mmap2_event(struct perf_tool *tool,struct thread *thread; struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session; -struct perf_evsel *evsel =perf_evlist__first (session->evlist); +struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id);if (perf_event__process_mmap2 (tool, event, sample, machine) <0 )return -1 ;
Signature · 1 line
2.1.0
Arnaldo Carvalho de Melo
maintainerPATCH 18/18 perf script: Add option --show-switch-events
In reply to Arnaldo Carvalho de MeloFrom: Adrian Hunter <[email protected]>
Add option --show-switch-events to show switch events in a similar fashion to --show-task-events and --show-mmap-events.
--- tools/perf/Documentation/perf-script.txt | 4 ++++ tools/perf/builtin-script.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+)
tools/perf/Documentation/perf-script.txt +4 -0
@@ -222,6 +222,10 @@ OPTIONS --show-mmap-events Display mmap related events (e.g. MMAP, MMAP2). +--show-switch-events + Display context switch events i.e. events of type PERF_RECORD_SWITCH or + PERF_RECORD_SWITCH_CPU_WIDE. + --header Show perf.data header.
tools/perf/builtin-script.c +31 -0
@@ -623,6 +623,7 @@ struct perf_script {struct perf_session *session;bool show_task_events;bool show_mmap_events; +bool show_switch_events; }; static int process_attr(struct perf_tool *tool, union perf_event *event,
@@ -813,6 +814,32 @@ static int process_mmap2_event(struct perf_tool *tool,return 0 ; } +static int process_switch_event (struct perf_tool *tool, +union perf_event *event, +struct perf_sample *sample, +struct machine *machine) +{ +struct thread *thread; + struct perf_script *script = container_of(tool, struct perf_script, tool); +struct perf_session *session = script->session; +struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); + +if (perf_event__process_switch (tool, event, sample, machine) <0 ) +return -1 ; + + thread =machine__findnew_thread (machine, sample->pid, + sample->tid); +if (thread ==NULL ) { +pr_debug ("problem processing SWITCH event, skipping it.\n" ); +return -1 ; + } + +print_sample_start (sample, thread, evsel); +perf_event__fprintf (event, stdout); +thread__put (thread); +return 0 ; +} +static void sig_handler (int sig__maybe_unused ) { session_done =1 ;
@@ -834,6 +861,8 @@ static int __cmd_script(struct perf_script *script) script->tool.mmap = process_mmap_event; script->tool.mmap2 = process_mmap2_event; } +if (script->show_switch_events) + script->tool.context_switch = process_switch_event; ret =perf_session__process_events (script->session);
@@ -1618,6 +1647,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) "Show the fork/comm/exit events"),OPT_BOOLEAN ('\0' ,"show-mmap-events" , &script.show_mmap_events,"Show the mmap events" ), +OPT_BOOLEAN ('\0' ,"show-switch-events" , &script.show_switch_events, +"Show context switch events (if recorded)" ),OPT_BOOLEAN ('f' ,"force" , &file.force,"don't complain, do it" ), OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", "Instruction Tracing options",
Signature · 1 line
2.1.0
Quoted 99 lines · Arnaldo Carvalho de Melo
Hi Ingo,
Please consider pulling,
- Arnaldo
The following changes since commit
a11c51acc52822754d66a11c15f6f6edd4d23c55:Merge tag 'perf-core-for-mingo' of
git://git.kernel.org/pub/scm/linux/kernel/git/acme/linuxinto perf/core (2015-07-21 07:58:06 +0200)are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.gittags/perf-core-for-mingofor you to fetch changes up to
7c14898ba9386ee5c939bb418643ac6baff52840:perf script: Add option --show-switch-events (2015-07-23 22:51:14 -0300)----------------------------------------------------------------
perf/core improvements and fixes:New features:
- Introduce
PERF_RECORD_SWITCH(_CPU_WIDE)and use it in 'record' to ask for context switches, allowing non priviledged tasks to know when they are switched in and out, which wasn't possible with the other context switch tracepoint and software events, see the patch description for a comprehensive justification (Adrian Hunter)- Stop collecting
/proc/kallsymsin perf.data files, saving about 4.5MB on a typical x86-64 system, use the symbol resolution routines used in all the other tools (report, top, etc) now that we can ask libtraceevent to use perf's symbol resolution code. (Arnaldo Carvalho de Melo)User visible fixes:
- Expose perf's symbol resolver to libtraceecent, so that its plugins can resolve tracepoint fields to kernel functions, like the 'function' field in the "timer:hrtimer_start tracepoint" (Arnaldo Carvalho de Melo)
Infrastructure:
- Map propagation of thread and cpu maps improvements, prep work for 'perf stat' new features (Jiri Olsa)
Signed-off-by Arnaldo Carvalho de Melo <[email protected]>----------------------------------------------------------------
Adrian Hunter (5):
perf: AddPERF_RECORD_SWITCHto indicate context switches
perf tools: Add newPERF_RECORD_SWITCHevent
perf record: Add option --switch-events to selectPERF_RECORD_SWITCHevents perf script: Don't assume evsel position of tracking events perf script: Add option --show-switch-eventsArnaldo Carvalho de Melo (8):
perf symbols: Add front end cache for DSO symbol lookup perf symbols: Introduce map__is_(kernel,kmodule)()
tools lib traceevent: Allow setting an alternative symbol resolver perf symbols: Provide libtraceevent callback to resolve kernel symbols perf trace: Provide libtracevent with a kernel symbol resolver perf script: Switch from perf.data's kallsyms to perf's symbol resolver perf tools: Stop reading the kallsyms data from perf.data perf tools: Stop copying kallsyms into the perf.data file headerJiri Olsa (5):
perf test: Check for refcnt inthread_maptest
perf evlist: Forceperf_evlist__set_mapsto propagate maps through events perf evlist: Use bool instead of target argument inpropagate_maps()perf evlist: Tolerate NULL maps inpropagate_maps
perf header: Use argv style storage for cmdline feature datainclude/uapi/linux/perf_event.h | 31 +++++++++- kernel/events/core.c | 103 +++++++++++++++++++++++++++++++ tools/lib/traceevent/event-parse.c | 68 +++++++++++++++++++- tools/lib/traceevent/event-parse.h | 8 +++ tools/perf/Documentation/perf-record.txt | 4 ++ tools/perf/Documentation/perf-script.txt | 4 ++ tools/perf/builtin-inject.c | 1 + tools/perf/builtin-record.c | 7 +++ tools/perf/builtin-script.c | 48 ++++++++++++-- tools/perf/builtin-trace.c | 3 + tools/perf/perf.h | 1 + tools/perf/tests/thread-map.c | 4 ++ tools/perf/util/dso.h | 4 ++ tools/perf/util/event.c | 28 +++++++++ tools/perf/util/event.h | 12 ++++ tools/perf/util/evlist.c | 28 +++++++-- tools/perf/util/evlist.h | 12 ++-- tools/perf/util/evsel.c | 4 ++ tools/perf/util/header.c | 35 ++++++----- tools/perf/util/header.h | 1 + tools/perf/util/machine.c | 25 ++++++++ tools/perf/util/machine.h | 6 ++ tools/perf/util/map.c | 14 +++++ tools/perf/util/map.h | 7 +++ tools/perf/util/record.c | 10 +++ tools/perf/util/session.c | 21 +++++++
Quoted 8 lines
tools/perf/util/symbol.c | 7 ++- tools/perf/util/tool.h | 1 + tools/perf/util/trace-event-info.c | 22 +++---- tools/perf/util/trace-event-parse.c | 30 --------- tools/perf/util/trace-event-read.c | 28 ++++----- tools/perf/util/trace-event.c | 45 +++++++++----- tools/perf/util/trace-event.h | 1 + 33 files changed, 513 insertions(+), 110 deletions(-)
Pulled, thanks a lot Arnaldo!
Ingo