perf_counter: add tracepoint support
cover perf_counter: add tracepoint support
hi,
The following two patches build on Peter's initial tracepoint perf counter work. The first patch, adds 'perf list' and 'perf stat' support to the perf tool. Thus, we can specify tracepoint events as: '-e subsystem:event_name'. The second patch looks for the debugfs filesystem in /proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DIR_DEBUGFS'
Below, I show the output of 'perf list', and 'perf stat' for a tbench run.
thanks,
-Jason
#perf list
List of pre-defined events (to be used in -e):
cpu-cycles OR cycles [Hardware event] instructions [Hardware event] cache-references [Hardware event] cache-misses [Hardware event] branch-instructions OR branches [Hardware event] branch-misses [Hardware event] bus-cycles [Hardware event]
cpu-clock [Software event] task-clock [Software event] page-faults OR faults [Software event] minor-faults [Software event] major-faults [Software event] context-switches OR cs [Software event] cpu-migrations OR migrations [Software event]
L1-dcache-loads [Hardware cache event] L1-dcache-load-misses [Hardware cache event] L1-dcache-stores [Hardware cache event] L1-dcache-store-misses [Hardware cache event] L1-dcache-prefetches [Hardware cache event] L1-dcache-prefetch-misses [Hardware cache event] L1-icache-loads [Hardware cache event] L1-icache-load-misses [Hardware cache event] L1-icache-prefetches [Hardware cache event] L1-icache-prefetch-misses [Hardware cache event] LLC-loads [Hardware cache event] LLC-load-misses [Hardware cache event] LLC-stores [Hardware cache event] LLC-store-misses [Hardware cache event] LLC-prefetches [Hardware cache event] LLC-prefetch-misses [Hardware cache event] dTLB-loads [Hardware cache event] dTLB-load-misses [Hardware cache event] dTLB-stores [Hardware cache event] dTLB-store-misses [Hardware cache event] dTLB-prefetches [Hardware cache event] dTLB-prefetch-misses [Hardware cache event] iTLB-loads [Hardware cache event] iTLB-load-misses [Hardware cache event] branch-loads [Hardware cache event] branch-load-misses [Hardware cache event]
rNNN [raw hardware event descriptor]
skb:kfree_skb [Tracepoint event] block:block_rq_abort [Tracepoint event] block:block_rq_insert [Tracepoint event] block:block_rq_issue [Tracepoint event] block:block_rq_requeue [Tracepoint event] block:block_rq_complete [Tracepoint event] block:block_bio_bounce [Tracepoint event] block:block_bio_complete [Tracepoint event] block:block_bio_backmerge [Tracepoint event] block:block_bio_frontmerge [Tracepoint event] block:block_bio_queue [Tracepoint event] block:block_getrq [Tracepoint event] block:block_sleeprq [Tracepoint event] block:block_plug [Tracepoint event] block:block_unplug_timer [Tracepoint event] block:block_unplug_io [Tracepoint event] block:block_split [Tracepoint event] block:block_remap [Tracepoint event] kmem:kmalloc [Tracepoint event] kmem:kmem_cache_alloc [Tracepoint event] kmem:kmalloc_node [Tracepoint event] kmem:kmem_cache_alloc_node [Tracepoint event] kmem:kfree [Tracepoint event] kmem:kmem_cache_free [Tracepoint event] ftrace:kmem_free [Tracepoint event] ftrace:kmem_alloc [Tracepoint event] ftrace:power [Tracepoint event] ftrace:hw_branch [Tracepoint event] ftrace:branch [Tracepoint event] ftrace:print [Tracepoint event] ftrace:bprint [Tracepoint event] ftrace:user_stack [Tracepoint event] ftrace:kernel_stack [Tracepoint event] ftrace:special [Tracepoint event] ftrace:context_switch [Tracepoint event] ftrace:wakeup [Tracepoint event] ftrace:funcgraph_exit [Tracepoint event] ftrace:funcgraph_entry [Tracepoint event] ftrace:function [Tracepoint event] workqueue:workqueue_insertion [Tracepoint event] workqueue:workqueue_execution [Tracepoint event] workqueue:workqueue_creation [Tracepoint event] workqueue:workqueue_destruction [Tracepoint event] irq:irq_handler_entry [Tracepoint event] irq:irq_handler_exit [Tracepoint event] irq:softirq_entry [Tracepoint event] irq:softirq_exit [Tracepoint event] sched:sched_kthread_stop [Tracepoint event] sched:sched_kthread_stop_ret [Tracepoint event] sched:sched_wait_task [Tracepoint event] sched:sched_wakeup [Tracepoint event] sched:sched_wakeup_new [Tracepoint event] sched:sched_switch [Tracepoint event] sched:sched_migrate_task [Tracepoint event] sched:sched_process_free [Tracepoint event] sched:sched_process_exit [Tracepoint event] sched:sched_process_wait [Tracepoint event] sched:sched_process_fork [Tracepoint event] sched:sched_signal_send [Tracepoint event]
# perf stat -a -e sched:sched_switch -e sched:sched_wakeup -e irq:irq_handler_entry tbench 4
Performance counter stats for 'tbench 4':
194658693 sched:sched_switch
97479295 sched:sched_wakeup
9122 irq:irq_handler_entry720.171439729 seconds time elapsed
-add support for tracepoints to the perf tool
-add detection for debugfs mount directory
tools/perf/perf.c | 58 +++++++++++++- tools/perf/util/cache.h | 1 + tools/perf/util/parse-events.c | 176 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/parse-events.h | 3 + tools/perf/util/string.h | 3 + tools/perf/util/util.h | 2 + 6 files changed, 241 insertions(+), 2 deletions(-)
PATCH 1/2 perf_counter: add tracepoint support to perf list, perf stat
Add support to 'perf list' and 'perf stat' for kernel tracepoints. The implementation creates a 'for_each_subsystem' and 'for_each_event' for easy iteration over the tracepoints.
--- tools/perf/util/parse-events.c | 175 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/util.h | 2 + 2 files changed, 176 insertions(+), 1 deletions(-)
tools/perf/util/parse-events.c +174 -1
@@ -12,6 +12,8 @@ int nr_counters;struct perf_counter_attr attrs[MAX_COUNTERS ]; +static char default_debugfs_path[] ="/sys/kernel/debug/tracing/events" ; +struct event_symbol {u8 type;u64 config;
@@ -110,6 +112,88 @@ static unsigned long hw_cache_stat[C(MAX)] = { [C(BPU)] = (CACHE_READ), }; +#define for_each_subsystem (sys_dir, sys_dirent, sys_next, file, st) \ + while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ + if (snprintf(file, MAXPATHLEN, "%s/%s", default_debugfs_path, \ + sys_dirent.d_name) && \ + (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ + (strcmp(sys_dirent.d_name, ".")) && \ + (strcmp(sys_dirent.d_name, ".."))) + +#define for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \ + while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ + if (snprintf(file, MAXPATHLEN, "%s/%s/%s", default_debugfs_path, \ + sys_dirent.d_name, evt_dirent.d_name) && \ + (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ + (strcmp(evt_dirent.d_name, ".")) && \ + (strcmp(evt_dirent.d_name, ".."))) + +#define MAX_EVENT_LENGTH 30 + +static int valid_debugfs_mount (void ) +{ +struct statfs st_fs; + +if (statfs (default_debugfs_path, &st_fs) <0 ) +return -ENOENT ; +else if (st_fs.f_type != (long )DEBUGFS_MAGIC ) +return -ENOENT ; +return 0 ; +} + +static char *tracepoint_id_to_name (u64 config) +{ +static char tracepoint_name[2 *MAX_EVENT_LENGTH ]; +DIR *sys_dir, *evt_dir; +struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; +struct stat st; +char id_buf[4 ]; +int fd; +u64 id; +char evt_path[MAXPATHLEN ]; + +if (valid_debugfs_mount ()) +return "unkown" ; + + sys_dir =opendir (default_debugfs_path); +if (!sys_dir) + goto cleanup; + +for_each_subsystem (sys_dir, sys_dirent, sys_next, evt_path, st) { + evt_dir =opendir (evt_path); +if (!evt_dir) + goto cleanup; +for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, + evt_path, st) { +snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , + default_debugfs_path, sys_dirent.d_name, + evt_dirent.d_name); + fd =open (evt_path,O_RDONLY ); +if (fd <0 ) +continue ; +if (read (fd, id_buf,sizeof (id_buf)) <0 ) { +close (fd); +continue ; + } +close (fd); + id =atoll (id_buf); +if (id == config) { +closedir (evt_dir); +closedir (sys_dir); +snprintf (tracepoint_name,2 *MAX_EVENT_LENGTH , +"%s:%s" , sys_dirent.d_name, + evt_dirent.d_name); +return tracepoint_name; + } + } +closedir (evt_dir); + } + +cleanup: +closedir (sys_dir); +return "unkown" ; +} +static int is_cache_op_valid (u8 cache_type,u8 cache_op) {if (hw_cache_stat[cache_type] &COP (cache_op))
@@ -177,6 +261,9 @@ char *event_name(int counter)return sw_event_names[config];return "unknown-software" ; +case PERF_TYPE_TRACEPOINT : +return tracepoint_id_to_name (config); +default :break ; }
@@ -265,6 +352,53 @@ parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)return 1 ; } +static int parse_tracepoint_event (const char **strp, +struct perf_counter_attr *attr) +{ +const char *evt_name; +char sys_name[MAX_EVENT_LENGTH ]; +char id_buf[4 ]; +int fd; +unsigned int sys_length, evt_length; +u64 id; +char evt_path[MAXPATHLEN ]; + +if (valid_debugfs_mount ()) +return 0 ; + + evt_name =strchr (*strp,':' ); +if (!evt_name) +return 0 ; + + sys_length = evt_name - *strp; +if (sys_length >=MAX_EVENT_LENGTH ) +return 0 ; + +strncpy (sys_name, *strp, sys_length); + sys_name[sys_length] ='\0' ; + evt_name = evt_name +1 ; + evt_length =strlen (evt_name); +if (evt_length >=MAX_EVENT_LENGTH ) +return 0 ; + +snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , default_debugfs_path, + sys_name, evt_name); + fd =open (evt_path,O_RDONLY ); +if (fd <0 ) +return 0 ; + +if (read (fd, id_buf,sizeof (id_buf)) <0 ) { +close (fd); +return 0 ; + } +close (fd); + id =atoll (id_buf); + attr->config = id; + attr->type =PERF_TYPE_TRACEPOINT ; + *strp = evt_name + evt_length; +return 1 ; +} +static int check_events (const char *str,unsigned int i) {int n;
@@ -374,7 +508,8 @@ parse_event_modifier(const char **strp, struct perf_counter_attr *attr)*/ static int parse_event_symbols (const char **str,struct perf_counter_attr *attr) { -if (!(parse_raw_event (str, attr) || +if (!(parse_tracepoint_event (str, attr) || +parse_raw_event (str, attr) ||parse_numeric_event (str, attr) ||parse_symbolic_event (str, attr) ||parse_generic_hw_event (str, attr)))
@@ -423,6 +558,42 @@ static const char * const event_type_descriptors[] = { };/* +* Print the events from <debugfs_mount_point>/tracing/events +*/ + +static void print_tracepoint_events (void ) +{ +DIR *sys_dir, *evt_dir; +struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; +struct stat st; +char evt_path[MAXPATHLEN ]; + +if (valid_debugfs_mount ()) +return ; + + sys_dir =opendir (default_debugfs_path); +if (!sys_dir) + goto cleanup; + +for_each_subsystem (sys_dir, sys_dirent, sys_next, evt_path, st) { + evt_dir =opendir (evt_path); +if (!evt_dir) + goto cleanup; +for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, + evt_path, st) { +snprintf (evt_path,MAXPATHLEN ,"%s:%s" , + sys_dirent.d_name, evt_dirent.d_name); +fprintf (stderr," %-40s [%s]\n" , evt_path, + event_type_descriptors[PERF_TYPE_TRACEPOINT +1 ]); + } +closedir (evt_dir); + } + +cleanup: +closedir (sys_dir); +} + +/* * Print the help text for the event symbols: */ void print_events (void )
@@ -472,5 +643,7 @@ void print_events(void) "rNNN");fprintf (stderr,"\n" ); +print_tracepoint_events (); +exit (129 ); }
tools/perf/util/util.h +2 -0
@@ -50,6 +50,7 @@#include <unistd.h> #include <stdio.h> #include <sys/stat.h> +#include <sys/statfs.h> #include <fcntl.h> #include <stddef.h> #include <stdlib.h>
@@ -80,6 +81,7 @@#include <netdb.h> #include <pwd.h> #include <inttypes.h> +#include "../../../include/linux/magic.h" #ifndef NO_ICONV #include <iconv.h>
Signature · 1 line
1.6.0.6
PATCH 2/2 perf_counter: detect debugfs location
In reply to Jason BaronSearch for the debugfs filesystem in /proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DIR_DEBUGFS'
--- tools/perf/perf.c | 58 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/cache.h | 1 + tools/perf/util/parse-events.c | 25 +++++++++-------- tools/perf/util/parse-events.h | 3 ++ tools/perf/util/string.h | 3 ++ 5 files changed, 77 insertions(+), 13 deletions(-)
tools/perf/perf.c +57 -1
@@ -12,6 +12,8 @@#include "util/cache.h" #include "util/quote.h" #include "util/run-command.h" +#include "util/parse-events.h" +#include "util/string.h" const char perf_usage_string[] ="perf [--version] [--help] COMMAND [ARGS]" ;
@@ -25,6 +27,8 @@ struct pager_config {int val; }; +static char debugfs_mntpt[MAXPATHLEN ]; +static int pager_command_config (const char *var,const char *value,void *data) {struct pager_config *c = data;
@@ -56,6 +60,15 @@ static void commit_pager_choice(void) { } } +static void set_debugfs_path (void ) +{ +char *path; + + path =getenv (PERF_DEBUGFS_ENVIRONMENT ); +snprintf (debugfs_path,MAXPATHLEN ,"%s/%s" , path ?: debugfs_mntpt, +"tracing/events" ); +} +static int handle_options (const char *** argv,int * argc,int * envchanged) {int handled =0 ;
@@ -122,6 +135,22 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)setenv (PERF_WORK_TREE_ENVIRONMENT , cmd +12 ,1 );if (envchanged) *envchanged =1 ; + }else if (!strcmp (cmd,"--debugfs-dir" )) { +if (*argc <2 ) { +fprintf (stderr,"No directory given for --debugfs-dir.\n" ); +usage (perf_usage_string); + } +strncpy (debugfs_mntpt, (*argv)[1 ],MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; +if (envchanged) + *envchanged =1 ; + (*argv)++; + (*argc)--; + }else if (!prefixcmp (cmd,"--debugfs-dir=" )) { +strncpy (debugfs_mntpt, cmd +14 ,MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; +if (envchanged) + *envchanged =1 ; }else {fprintf (stderr,"Unknown option: %s\n" , cmd);usage (perf_usage_string);
@@ -228,6 +257,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)if (use_pager ==-1 && p->option &USE_PAGER ) use_pager =1 ;commit_pager_choice (); +set_debugfs_path (); status = p->fn (argc, argv, prefix);if (status)
@@ -346,6 +376,30 @@ static int run_argv(int *argcp, const char ***argv)return done_alias; } +/* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */ +static void get_debugfs_mntpt (void ) +{ +FILE *file; +char fs_type[100 ]; +char debugfs[MAXPATHLEN ]; + + file =fopen ("/proc/mounts" ,"r" ); +if (file ==NULL ) +return ; + + while (fscanf(file, "%*s %" + STR(MAXPATHLEN) +"s %99s %*s %*d %*d\n" , + debugfs, fs_type) ==2 ) { +if (strcmp (fs_type,"debugfs" ) ==0 ) +break ; + } +fclose (file); +if (strcmp (fs_type,"debugfs" ) ==0 ) { +strncpy (debugfs_mntpt, debugfs,MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; + } +}int main (int argc,const char **argv) {
@@ -354,7 +408,8 @@ int main(int argc, const char **argv) cmd =perf_extract_argv0_path (argv[0 ]);if (!cmd) cmd ="perf-help" ; - +/* get debugfs mount point from /proc/mounts */ +get_debugfs_mntpt ();/* * "perf-xxxx" is the same as "perf xxxx", but we obviously: *
@@ -377,6 +432,7 @@ int main(int argc, const char **argv) argc--;handle_options (&argv, &argc,NULL );commit_pager_choice (); +set_debugfs_path ();if (argc >0 ) {if (!prefixcmp (argv[0 ],"--" )) argv[0 ] +=2 ;
tools/perf/util/cache.h +1 -0
@@ -18,6 +18,7 @@#define PERFATTRIBUTES_FILE ".perfattributes"#define INFOATTRIBUTES_FILE "info/attributes"#define ATTRIBUTE_MACRO_PREFIX "[attr]" +#define PERF_DEBUGFS_ENVIRONMENT "PERF_DIR_DEBUGFS"typedef int (*config_fn_t )(const char *,const char *,void *);extern int perf_default_config (const char *,const char *,void *);
tools/perf/util/parse-events.c +13 -12
@@ -5,6 +5,7 @@#include "parse-events.h" #include "exec_cmd.h" #include "string.h" +#include "cache.h" extern char *strcasestr (const char *haystack,const char *needle);
@@ -12,8 +13,6 @@ int nr_counters;struct perf_counter_attr attrs[MAX_COUNTERS ]; -static char default_debugfs_path[] ="/sys/kernel/debug/tracing/events" ; -struct event_symbol {u8 type;u64 config;
@@ -21,6 +20,8 @@ struct event_symbol {char *alias; }; +char debugfs_path[MAXPATHLEN ]; +#define CHW (x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x#define CSW (x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
@@ -114,16 +115,16 @@ static unsigned long hw_cache_stat[C(MAX)] = {#define for_each_subsystem (sys_dir, sys_dirent, sys_next, file, st) \ while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ - if (snprintf(file, MAXPATHLEN, "%s/%s", default_debugfs_path, \ - sys_dirent.d_name) && \ + if (snprintf(file, MAXPATHLEN, "%s/%s", debugfs_path, \ + sys_dirent.d_name) && \ (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ (strcmp(sys_dirent.d_name, ".")) && \ (strcmp(sys_dirent.d_name, "..")))#define for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \ while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ - if (snprintf(file, MAXPATHLEN, "%s/%s/%s", default_debugfs_path, \ - sys_dirent.d_name, evt_dirent.d_name) && \ + if (snprintf(file, MAXPATHLEN, "%s/%s/%s", debugfs_path, \ + sys_dirent.d_name, evt_dirent.d_name) && \ (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ (strcmp(evt_dirent.d_name, ".")) && \ (strcmp(evt_dirent.d_name, "..")))
@@ -134,7 +135,7 @@ static int valid_debugfs_mount(void) {struct statfs st_fs; -if (statfs (default_debugfs_path, &st_fs) <0 ) +if (statfs (debugfs_path, &st_fs) <0 )return -ENOENT ;else if (st_fs.f_type != (long )DEBUGFS_MAGIC )return -ENOENT ;
@@ -155,7 +156,7 @@ static char *tracepoint_id_to_name(u64 config)if (valid_debugfs_mount ())return "unkown" ; - sys_dir =opendir (default_debugfs_path); + sys_dir =opendir (debugfs_path);if (!sys_dir) goto cleanup;
@@ -166,7 +167,7 @@ static char *tracepoint_id_to_name(u64 config)for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, evt_path, st) {snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , - default_debugfs_path, sys_dirent.d_name, + debugfs_path, sys_dirent.d_name, evt_dirent.d_name); fd =open (evt_path,O_RDONLY );if (fd <0 )
@@ -381,8 +382,8 @@ static int parse_tracepoint_event(const char **strp,if (evt_length >=MAX_EVENT_LENGTH )return 0 ; -snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , default_debugfs_path, - sys_name, evt_name); +snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , debugfs_path, + sys_name, evt_name); fd =open (evt_path,O_RDONLY );if (fd <0 )return 0 ;
@@ -571,7 +572,7 @@ static void print_tracepoint_events(void)if (valid_debugfs_mount ())return ; - sys_dir =opendir (default_debugfs_path); + sys_dir =opendir (debugfs_path);if (!sys_dir) goto cleanup;
tools/perf/util/parse-events.h +3 -0
@@ -3,6 +3,8 @@* Parse symbolic events/counts passed in as options: */ +struct option ; +extern int nr_counters;extern struct perf_counter_attr attrs[MAX_COUNTERS ];
@@ -15,3 +17,4 @@ extern int parse_events(const struct option *opt, const char *str, int unset);extern void print_events (void ); +extern char debugfs_path[];
tools/perf/util/string.h +3 -0
@@ -5,4 +5,7 @@int hex2u64 (const char *ptr,u64 *val); +#define _STR (x) #x +#define STR (x) _STR(x) +#endif
Signature · 1 line
1.6.0.6
Re: [PATCH 2/2] perf_counter: detect debugfs location
Quoted 5 lines · Jason Baron
Search for the debugfs filesystem in
/proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DIR_DEBUGFS'-static char default_debugfs_path[] = "
/sys/kernel/debug/tracing/events";
There are server systems out there that run with thousands of mounts. It would be kinder to the users of those systems (ie, faster perf startup) if you only went rooting around /proc/mounts if opening the default location failed.
Re: [PATCH 2/2] perf_counter: detect debugfs location
Quoted 11 lines · Ray Lee
Quoted 5 lines · Jason Baron
Search for the debugfs filesystem in
/proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DIR_DEBUGFS'-static char default_debugfs_path[] = "
/sys/kernel/debug/tracing/events";There are server systems out there that run with thousands of mounts. It would be kinder to the users of those systems (ie, faster perf startup) if you only went rooting around
/proc/mountsif opening the default location failed.
Well, those that have thousand of mounts could probably get away with setting PERF_DIR_DEBUGFS in /etc/profile.
But, a simple stat("/sys/kernel/debug/tracing") should be easy enough.
-- Steve
Re: [PATCH 2/2] perf_counter: detect debugfs location
Quoted 10 lines · Steven Rostedt
Quoted 7 lines · Ray Lee
Quoted 5 lines · Jason Baron
Search for the debugfs filesystem in
/proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DIR_DEBUGFS'-static char default_debugfs_path[] = "
/sys/kernel/debug/tracing/events";
Quoted 7 lines
Quoted 4 lines
There are server systems out there that run with thousands of mounts. It would be kinder to the users of those systems (ie, faster perf startup) if you only went rooting around
/proc/mountsif opening the default location failed.Well, those that have thousand of mounts could probably get away with setting
PERF_DIR_DEBUGFSin/etc/profile.
The intent of the patch is obviously to try to Do The Right Thing by default. I think that's great. I just think it should DT(fast and)RT instead.
Quoted 1 line
But, a simple
stat("/sys/kernel/debug/tracing")should be easy enough.
<nod>
Re: [PATCH 2/2] perf_counter: detect debugfs location
Quoted 19 lines · Ray Lee
Quoted 10 lines · Steven Rostedt
Quoted 7 lines · Ray Lee
Quoted 5 lines · Jason Baron
Search for the debugfs filesystem in
/proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DIR_DEBUGFS'-static char default_debugfs_path[] = "
/sys/kernel/debug/tracing/events";Quoted 7 lines
Quoted 4 lines
There are server systems out there that run with thousands of mounts. It would be kinder to the users of those systems (ie, faster perf startup) if you only went rooting around
/proc/mountsif opening the default location failed.Well, those that have thousand of mounts could probably get away with setting
PERF_DIR_DEBUGFSin/etc/profile.
Quoted 7 lines
The intent of the patch is obviously to try to Do The Right Thing by default. I think that's great. I just think it should DT(fast and)RT instead.
Quoted 1 line
But, a simple
stat("/sys/kernel/debug/tracing")should be easy enough.<nod>
agreed, that would be better. I'm also renaming the environment variable PERF_DIR_DEBUGFS -> PERF_DEBUGFS_DIR, as Zach Brown pointed out. Re-spun path below.
thanks,
-Jason
If "/sys/kernel/debug" is not a debugfs mount point, search for the debugfs filesystem in /proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DEBUGFS_DIR'
---
tools/perf/perf.c | 63 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/cache.h | 1 + tools/perf/util/parse-events.c | 33 +++++++++++---------- tools/perf/util/parse-events.h | 5 +++ tools/perf/util/string.h | 3 ++ 5 files changed, 88 insertions(+), 17 deletions(-)
tools/perf/perf.c +62 -1
@@ -12,6 +12,8 @@#include "util/cache.h" #include "util/quote.h" #include "util/run-command.h" +#include "util/parse-events.h" +#include "util/string.h" const char perf_usage_string[] ="perf [--version] [--help] COMMAND [ARGS]" ;
@@ -25,6 +27,8 @@ struct pager_config {int val; }; +static char debugfs_mntpt[MAXPATHLEN ]; +static int pager_command_config (const char *var,const char *value,void *data) {struct pager_config *c = data;
@@ -56,6 +60,15 @@ static void commit_pager_choice(void) { } } +static void set_debugfs_path (void ) +{ +char *path; + + path =getenv (PERF_DEBUGFS_ENVIRONMENT ); +snprintf (debugfs_path,MAXPATHLEN ,"%s/%s" , path ?: debugfs_mntpt, +"tracing/events" ); +} +static int handle_options (const char *** argv,int * argc,int * envchanged) {int handled =0 ;
@@ -122,6 +135,22 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)setenv (PERF_WORK_TREE_ENVIRONMENT , cmd +12 ,1 );if (envchanged) *envchanged =1 ; + }else if (!strcmp (cmd,"--debugfs-dir" )) { +if (*argc <2 ) { +fprintf (stderr,"No directory given for --debugfs-dir.\n" ); +usage (perf_usage_string); + } +strncpy (debugfs_mntpt, (*argv)[1 ],MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; +if (envchanged) + *envchanged =1 ; + (*argv)++; + (*argc)--; + }else if (!prefixcmp (cmd,"--debugfs-dir=" )) { +strncpy (debugfs_mntpt, cmd +14 ,MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; +if (envchanged) + *envchanged =1 ; }else {fprintf (stderr,"Unknown option: %s\n" , cmd);usage (perf_usage_string);
@@ -228,6 +257,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)if (use_pager ==-1 && p->option &USE_PAGER ) use_pager =1 ;commit_pager_choice (); +set_debugfs_path (); status = p->fn (argc, argv, prefix);if (status)
@@ -346,6 +376,35 @@ static int run_argv(int *argcp, const char ***argv)return done_alias; } +/* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */ +static void get_debugfs_mntpt (void ) +{ +FILE *file; +char fs_type[100 ]; +char debugfs[MAXPATHLEN ]; + +if (valid_debugfs_mount ("/sys/kernel/debug/" ) ==0 ) { +strcpy (debugfs_mntpt,"/sys/kernel/debug/" ); +return ; + } + + file =fopen ("/proc/mounts" ,"r" ); +if (file ==NULL ) +return ; + + while (fscanf(file, "%*s %" + STR(MAXPATHLEN) +"s %99s %*s %*d %*d\n" , + debugfs, fs_type) ==2 ) { +if (strcmp (fs_type,"debugfs" ) ==0 ) +break ; + } +fclose (file); +if (strcmp (fs_type,"debugfs" ) ==0 ) { +strncpy (debugfs_mntpt, debugfs,MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; + } +}int main (int argc,const char **argv) {
@@ -354,7 +413,8 @@ int main(int argc, const char **argv) cmd =perf_extract_argv0_path (argv[0 ]);if (!cmd) cmd ="perf-help" ; - +/* get debugfs mount point from /proc/mounts */ +get_debugfs_mntpt ();/* * "perf-xxxx" is the same as "perf xxxx", but we obviously: *
@@ -377,6 +437,7 @@ int main(int argc, const char **argv) argc--;handle_options (&argv, &argc,NULL );commit_pager_choice (); +set_debugfs_path ();if (argc >0 ) {if (!prefixcmp (argv[0 ],"--" )) argv[0 ] +=2 ;
tools/perf/util/cache.h +1 -0
@@ -18,6 +18,7 @@#define PERFATTRIBUTES_FILE ".perfattributes"#define INFOATTRIBUTES_FILE "info/attributes"#define ATTRIBUTE_MACRO_PREFIX "[attr]" +#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"typedef int (*config_fn_t )(const char *,const char *,void *);extern int perf_default_config (const char *,const char *,void *);
tools/perf/util/parse-events.c +17 -16
@@ -5,6 +5,7 @@#include "parse-events.h" #include "exec_cmd.h" #include "string.h" +#include "cache.h" extern char *strcasestr (const char *haystack,const char *needle);
@@ -12,8 +13,6 @@ int nr_counters;struct perf_counter_attr attrs[MAX_COUNTERS ]; -static char default_debugfs_path[] ="/sys/kernel/debug/tracing/events" ; -struct event_symbol {u8 type;u64 config;
@@ -21,6 +20,8 @@ struct event_symbol {char *alias; }; +char debugfs_path[MAXPATHLEN ]; +#define CHW (x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x#define CSW (x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
@@ -114,27 +115,27 @@ static unsigned long hw_cache_stat[C(MAX)] = {#define for_each_subsystem (sys_dir, sys_dirent, sys_next, file, st) \ while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ - if (snprintf(file, MAXPATHLEN, "%s/%s", default_debugfs_path, \ - sys_dirent.d_name) && \ + if (snprintf(file, MAXPATHLEN, "%s/%s", debugfs_path, \ + sys_dirent.d_name) && \ (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ (strcmp(sys_dirent.d_name, ".")) && \ (strcmp(sys_dirent.d_name, "..")))#define for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \ while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ - if (snprintf(file, MAXPATHLEN, "%s/%s/%s", default_debugfs_path, \ - sys_dirent.d_name, evt_dirent.d_name) && \ + if (snprintf(file, MAXPATHLEN, "%s/%s/%s", debugfs_path, \ + sys_dirent.d_name, evt_dirent.d_name) && \ (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ (strcmp(evt_dirent.d_name, ".")) && \ (strcmp(evt_dirent.d_name, "..")))#define MAX_EVENT_LENGTH 30 -static int valid_debugfs_mount (void ) +int valid_debugfs_mount (const char *debugfs) {struct statfs st_fs; -if (statfs (default_debugfs_path, &st_fs) <0 ) +if (statfs (debugfs, &st_fs) <0 )return -ENOENT ;else if (st_fs.f_type != (long )DEBUGFS_MAGIC )return -ENOENT ;
@@ -152,10 +153,10 @@ static char *tracepoint_id_to_name(u64 config)u64 id;char evt_path[MAXPATHLEN ]; -if (valid_debugfs_mount ()) +if (valid_debugfs_mount (debugfs_path))return "unkown" ; - sys_dir =opendir (default_debugfs_path); + sys_dir =opendir (debugfs_path);if (!sys_dir) goto cleanup;
@@ -166,7 +167,7 @@ static char *tracepoint_id_to_name(u64 config)for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, evt_path, st) {snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , - default_debugfs_path, sys_dirent.d_name, + debugfs_path, sys_dirent.d_name, evt_dirent.d_name); fd =open (evt_path,O_RDONLY );if (fd <0 )
@@ -363,7 +364,7 @@ static int parse_tracepoint_event(const char **strp,u64 id;char evt_path[MAXPATHLEN ]; -if (valid_debugfs_mount ()) +if (valid_debugfs_mount (debugfs_path))return 0 ; evt_name =strchr (*strp,':' );
@@ -381,8 +382,8 @@ static int parse_tracepoint_event(const char **strp,if (evt_length >=MAX_EVENT_LENGTH )return 0 ; -snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , default_debugfs_path, - sys_name, evt_name); +snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , debugfs_path, + sys_name, evt_name); fd =open (evt_path,O_RDONLY );if (fd <0 )return 0 ;
@@ -568,10 +569,10 @@ static void print_tracepoint_events(void)struct stat st;char evt_path[MAXPATHLEN ]; -if (valid_debugfs_mount ()) +if (valid_debugfs_mount (debugfs_path))return ; - sys_dir =opendir (default_debugfs_path); + sys_dir =opendir (debugfs_path);if (!sys_dir) goto cleanup;
tools/perf/util/parse-events.h +5 -0
@@ -3,6 +3,8 @@* Parse symbolic events/counts passed in as options: */ +struct option ; +extern int nr_counters;extern struct perf_counter_attr attrs[MAX_COUNTERS ];
@@ -15,3 +17,6 @@ extern int parse_events(const struct option *opt, const char *str, int unset);extern void print_events (void ); +extern char debugfs_path[]; +extern int valid_debugfs_mount (const char *debugfs); +
tools/perf/util/string.h +3 -0
@@ -5,4 +5,7 @@int hex2u64 (const char *ptr,u64 *val); +#define _STR (x) #x +#define STR (x) _STR(x) +#endif
Re: [PATCH 2/2] perf_counter: detect debugfs location
I queued the below.
Thanks Jason!
---
Subject: perf_counter: Detect debugfs location
From: Jason Baron <[email protected]>
Date: Tue, 21 Jul 2009 14:16:29 -0400
If "/sys/kernel/debug" is not a debugfs mount point, search for the debugfs filesystem in /proc/mounts, but also allows the user to specify '--debugfs-dir=blah' or set the environment variable: 'PERF_DEBUGFS_DIR'
LKML-Reference: <[email protected]>
--- tools/perf/perf.c | 77 ++++++++++++++++++++++++++++++++++++++++- tools/perf/util/cache.h | 1 tools/perf/util/parse-events.c | 33 +++++++++-------- tools/perf/util/parse-events.h | 5 ++ tools/perf/util/string.h | 3 + 5 files changed, 102 insertions(+), 17 deletions(-)
tools/perf/perf.c +76 -1
@@ -12,6 +12,8 @@#include "util/cache.h" #include "util/quote.h" #include "util/run-command.h" +#include "util/parse-events.h" +#include "util/string.h" const char perf_usage_string[] ="perf [--version] [--help] COMMAND [ARGS]" ;
@@ -25,6 +27,8 @@ struct pager_config {int val; }; +static char debugfs_mntpt[MAXPATHLEN ]; +static int pager_command_config (const char *var,const char *value,void *data) {struct pager_config *c = data;
@@ -56,6 +60,15 @@ static void commit_pager_choice(void) { } } +static void set_debugfs_path (void ) +{ +char *path; + + path =getenv (PERF_DEBUGFS_ENVIRONMENT ); +snprintf (debugfs_path,MAXPATHLEN ,"%s/%s" , path ?: debugfs_mntpt, +"tracing/events" ); +} +static int handle_options (const char *** argv,int * argc,int * envchanged) {int handled =0 ;
@@ -122,6 +135,22 @@ static int handle_options(const char***setenv (PERF_WORK_TREE_ENVIRONMENT , cmd +12 ,1 );if (envchanged) *envchanged =1 ; + }else if (!strcmp (cmd,"--debugfs-dir" )) { +if (*argc <2 ) { +fprintf (stderr,"No directory given for --debugfs-dir.\n" ); +usage (perf_usage_string); + } +strncpy (debugfs_mntpt, (*argv)[1 ],MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; +if (envchanged) + *envchanged =1 ; + (*argv)++; + (*argc)--; + }else if (!prefixcmp (cmd,"--debugfs-dir=" )) { +strncpy (debugfs_mntpt, cmd +14 ,MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; +if (envchanged) + *envchanged =1 ; }else {fprintf (stderr,"Unknown option: %s\n" , cmd);usage (perf_usage_string);
@@ -228,6 +257,7 @@ static int run_builtin(struct cmd_structif (use_pager ==-1 && p->option &USE_PAGER ) use_pager =1 ;commit_pager_choice (); +set_debugfs_path (); status = p->fn (argc, argv, prefix);if (status)
@@ -346,6 +376,49 @@ static int run_argv(int *argcp, const chreturn done_alias; } +/* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */ +static void get_debugfs_mntpt (void ) +{ +FILE *file; +char fs_type[100 ]; +char debugfs[MAXPATHLEN ]; + +/* +* try the standard location +*/ +if (valid_debugfs_mount ("/sys/kernel/debug/" ) ==0 ) { +strcpy (debugfs_mntpt,"/sys/kernel/debug/" ); +return ; + } + +/* +* try the sane location +*/ +if (valid_debugfs_mount ("/debug/" ) ==0 ) { +strcpy (debugfs_mntpt,"/debug/" ); +return ; + } + +/* +* give up and parse /proc/mounts +*/ + file =fopen ("/proc/mounts" ,"r" ); +if (file ==NULL ) +return ; + + while (fscanf(file, "%*s %" + STR(MAXPATHLEN) +"s %99s %*s %*d %*d\n" , + debugfs, fs_type) ==2 ) { +if (strcmp (fs_type,"debugfs" ) ==0 ) +break ; + } +fclose (file); +if (strcmp (fs_type,"debugfs" ) ==0 ) { +strncpy (debugfs_mntpt, debugfs,MAXPATHLEN ); + debugfs_mntpt[MAXPATHLEN -1 ] ='\0' ; + } +}int main (int argc,const char **argv) {
@@ -354,7 +427,8 @@ int main(int argc, const char **argv) cmd =perf_extract_argv0_path (argv[0 ]);if (!cmd) cmd ="perf-help" ; - +/* get debugfs mount point from /proc/mounts */ +get_debugfs_mntpt ();/* * "perf-xxxx" is the same as "perf xxxx", but we obviously: *
@@ -377,6 +451,7 @@ int main(int argc, const char **argv) argc--;handle_options (&argv, &argc,NULL );commit_pager_choice (); +set_debugfs_path ();if (argc >0 ) {if (!prefixcmp (argv[0 ],"--" )) argv[0 ] +=2 ;
tools/perf/util/cache.h +1 -0
@@ -18,6 +18,7 @@#define PERFATTRIBUTES_FILE ".perfattributes"#define INFOATTRIBUTES_FILE "info/attributes"#define ATTRIBUTE_MACRO_PREFIX "[attr]" +#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"typedef int (*config_fn_t )(const char *,const char *,void *);extern int perf_default_config (const char *,const char *,void *);
tools/perf/util/parse-events.c +17 -16
@@ -5,6 +5,7 @@#include "parse-events.h" #include "exec_cmd.h" #include "string.h" +#include "cache.h" extern char *strcasestr (const char *haystack,const char *needle);
@@ -12,8 +13,6 @@ int nr_counters;struct perf_counter_attr attrs[MAX_COUNTERS ]; -static char default_debugfs_path[] ="/sys/kernel/debug/tracing/events" ; -struct event_symbol {u8 type;u64 config;
@@ -21,6 +20,8 @@ struct event_symbol {char *alias; }; +char debugfs_path[MAXPATHLEN ]; +#define CHW (x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x#define CSW (x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
@@ -114,27 +115,27 @@ static unsigned long hw_cache_stat[C(MAX#define for_each_subsystem (sys_dir, sys_dirent, sys_next, file, st) \ while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ - if (snprintf(file, MAXPATHLEN, "%s/%s", default_debugfs_path, \ - sys_dirent.d_name) && \ + if (snprintf(file, MAXPATHLEN, "%s/%s", debugfs_path, \ + sys_dirent.d_name) && \ (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ (strcmp(sys_dirent.d_name, ".")) && \ (strcmp(sys_dirent.d_name, "..")))#define for_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \ while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ - if (snprintf(file, MAXPATHLEN, "%s/%s/%s", default_debugfs_path, \ - sys_dirent.d_name, evt_dirent.d_name) && \ + if (snprintf(file, MAXPATHLEN, "%s/%s/%s", debugfs_path, \ + sys_dirent.d_name, evt_dirent.d_name) && \ (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ (strcmp(evt_dirent.d_name, ".")) && \ (strcmp(evt_dirent.d_name, "..")))#define MAX_EVENT_LENGTH 30 -static int valid_debugfs_mount (void ) +int valid_debugfs_mount (const char *debugfs) {struct statfs st_fs; -if (statfs (default_debugfs_path, &st_fs) <0 ) +if (statfs (debugfs, &st_fs) <0 )return -ENOENT ;else if (st_fs.f_type != (long )DEBUGFS_MAGIC )return -ENOENT ;
@@ -152,10 +153,10 @@ static char *tracepoint_id_to_name(u64 cu64 id;char evt_path[MAXPATHLEN ]; -if (valid_debugfs_mount ()) +if (valid_debugfs_mount (debugfs_path))return "unkown" ; - sys_dir =opendir (default_debugfs_path); + sys_dir =opendir (debugfs_path);if (!sys_dir) goto cleanup;
@@ -166,7 +167,7 @@ static char *tracepoint_id_to_name(u64 cfor_each_event (sys_dirent, evt_dir, evt_dirent, evt_next, evt_path, st) {snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , - default_debugfs_path, sys_dirent.d_name, + debugfs_path, sys_dirent.d_name, evt_dirent.d_name); fd =open (evt_path,O_RDONLY );if (fd <0 )
@@ -363,7 +364,7 @@ static int parse_tracepoint_event(constu64 id;char evt_path[MAXPATHLEN ]; -if (valid_debugfs_mount ()) +if (valid_debugfs_mount (debugfs_path))return 0 ; evt_name =strchr (*strp,':' );
@@ -381,8 +382,8 @@ static int parse_tracepoint_event(constif (evt_length >=MAX_EVENT_LENGTH )return 0 ; -snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , default_debugfs_path, - sys_name, evt_name); +snprintf (evt_path,MAXPATHLEN ,"%s/%s/%s/id" , debugfs_path, + sys_name, evt_name); fd =open (evt_path,O_RDONLY );if (fd <0 )return 0 ;
@@ -568,10 +569,10 @@ static void print_tracepoint_events(voidstruct stat st;char evt_path[MAXPATHLEN ]; -if (valid_debugfs_mount ()) +if (valid_debugfs_mount (debugfs_path))return ; - sys_dir =opendir (default_debugfs_path); + sys_dir =opendir (debugfs_path);if (!sys_dir) goto cleanup;
tools/perf/util/parse-events.h +5 -0
@@ -3,6 +3,8 @@* Parse symbolic events/counts passed in as options: */ +struct option ; +extern int nr_counters;extern struct perf_counter_attr attrs[MAX_COUNTERS ];
@@ -15,3 +17,6 @@ extern int parse_events(const struct optextern void print_events (void ); +extern char debugfs_path[]; +extern int valid_debugfs_mount (const char *debugfs); +
tools/perf/util/string.h +3 -0
@@ -5,4 +5,7 @@int hex2u64 (const char *ptr,u64 *val); +#define _STR (x) #x +#define STR (x) _STR(x) +#endif