Subject: [PATCH 0/2] debugfs utility routines for perf
Ingo,
Here are the cleaned-up debugfs routines.
Clark
Clark Williams (2):
debugfs utility routines for perf
modify perf routines to use new debugfs routines
tools/perf/perf.c | 45 +------ tools/perf/util/debugfs.c | 247 ++++++++++++++++++++++++++++++++++++++++ tools/perf/util/debugfs.h | 22 ++++ tools/perf/util/parse-events.c | 17 +-- 4 files changed, 280 insertions(+), 51 deletions(-) create mode 100644 tools/perf/util/debugfs.c create mode 100644 tools/perf/util/debugfs.h
PATCH 1/2 debugfs utility routines for perf
Add routines to locate the debugfs mount point and to manage the mounting and unmountin of the debugfs.
Signed-off-by Clark Williams <[email protected]>
CC Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
--- tools/perf/util/debugfs.c | 247 +++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/debugfs.h | 22 ++++ 2 files changed, 269 insertions(+), 0 deletions(-) create mode 100644 tools/perf/util/debugfs.c create mode 100644 tools/perf/util/debugfs.h
tools/perf/util/debugfs.c new file +247 -0
@@ -0,0 +1,247 @@ +#include "util.h" +#include "debugfs.h" +#include "cache.h" + +static int debugfs_premounted; +static char debugfs_mountpoint[MAX_PATH +1 ]; + +static const char *debugfs_known_mountpoints[] = { +"/sys/kernel/debug/" , +"/debug/" , +0 , +}; + +/* use this to force a umount */ +void debugfs_force_cleanup (void ) +{ +debugfs_find_mountpoint (); + debugfs_premounted =0 ; +debugfs_umount (); +} + +/* construct a full path to a debugfs element */ +int debugfs_make_path (const char *element,char *buffer,int size) +{ +int len; + +if (strlen (debugfs_mountpoint) ==0 ) { + buffer[0 ] ='\0' ; +return -1 ; + } + + len =strlen (debugfs_mountpoint) +strlen (element) +1 ; +if (len >= size) +return len+1 ; + +snprintf (buffer, size-1 ,"%s/%s" , debugfs_mountpoint, element); +return 0 ; +} + +/* find the path to the mounted debugfs */ +const char * +debugfs_find_mountpoint (void ) +{ +static int debugfs_found; +char type[100 ]; +FILE *fp; +const char **ptr; + +if (debugfs_found) +return (const char *) debugfs_mountpoint; + + ptr = debugfs_known_mountpoints; +while (*ptr) { +if (debugfs_valid_mountpoint (*ptr) ==0 ) { + debugfs_found =1 ; +strcpy (debugfs_mountpoint, *ptr); +return debugfs_mountpoint; + } + ptr++; + } + +/* give up and parse /proc/mounts */ + fp =fopen ("/proc/mounts" ,"r" ); +if (fp ==NULL ) +die ("Can't open /proc/mounts for read" ); + + while (fscanf(fp, "%*s %" + STR(MAX_PATH) +"s %99s %*s %*d %*d\n" , + debugfs_mountpoint, type) ==2 ) { +if (strcmp (type,"debugfs" ) ==0 ) +break ; + } +fclose (fp); + +if (strcmp (type,"debugfs" ) !=0 ) +return NULL ; + + debugfs_found =1 ; + +return debugfs_mountpoint; +} + +/* verify that a mountpoint is actually a debugfs instance */ + +int +debugfs_valid_mountpoint (const char *debugfs) +{ +struct statfs st_fs; + +if (statfs (debugfs, &st_fs) <0 ) +return -ENOENT ; +else if (st_fs.f_type != (long )DEBUGFS_MAGIC ) +return -ENOENT ; +return 0 ; +} + + +int +debugfs_valid_entry (const char *path) +{ +struct stat st; + +if (stat (path, &st)) +return -errno; +return 0 ; +} + +/* mount the debugfs somewhere */ + +int +debugfs_mount (const char *mountpoint) +{ +char mountcmd[128 ]; + +/* see if it's already mounted */ +if (debugfs_find_mountpoint ()) { + debugfs_premounted =1 ; +return 0 ; + } + +/* if not mounted and no argument */ +if (mountpoint ==NULL ) { +/* see if environment variable set */ + mountpoint =getenv (PERF_DEBUGFS_ENVIRONMENT ); +/* if no environment variable, use default */ +if (mountpoint ==NULL ) + mountpoint ="/sys/kernel/debug" ; + } + +/* save the mountpoint */ +strncpy (debugfs_mountpoint, mountpoint,sizeof (debugfs_mountpoint)); + +/* mount it */ +snprintf (mountcmd,sizeof (mountcmd), +"/bin/mount -t debugfs debugfs %s" , mountpoint); +return system (mountcmd); +} + +/* umount the debugfs */ + +int +debugfs_umount (void ) +{ +int ret; +char umountcmd[128 ]; + +/* if it was already mounted, leave it */ +if (debugfs_premounted) +return 0 ; + +/* make sure it's a valid mount point */ + ret =debugfs_valid_mountpoint (debugfs_mountpoint); +if (ret) +return ret; + +snprintf (umountcmd,sizeof (umountcmd), +"/bin/umount %s" , debugfs_mountpoint); +return system (umountcmd); +} + + +int +debugfs_write (const char *entry,const char *value) +{ +int ret, count; +int fd; +char path[MAX_PATH +1 ]; + +/* construct the path */ +snprintf (path,sizeof (path),"%s/%s" , debugfs_mountpoint, entry); + +/* verify that it exists */ + ret =debugfs_valid_entry (path); +if (ret) +return ret; + +/* get how many chars we're going to write */ + count =strlen (value); + +/* open the debugfs entry */ + fd =open (path,O_RDWR ); +if (fd <0 ) +return -errno; + +while (count >0 ) { +/* write it */ + ret =write (fd, value, count); +if (ret <=0 ) { +if (ret ==EAGAIN ) +continue ; +close (fd); +return -errno; + } + count -= ret; + } + +/* close it */ +close (fd); + +/* return success */ +return 0 ; +} + +/* +* read a debugfs entry +* returns the number of chars read or a negative errno +*/ + +int +debugfs_read (const char *entry,char *buffer,size_t size) +{ +int ret; +int fd; +char path[MAX_PATH +1 ]; + +/* construct the path */ +snprintf (path,sizeof (path),"%s/%s" , debugfs_mountpoint, entry); + +/* verify that it exists */ + ret =debugfs_valid_entry (path); +if (ret) +return ret; + +/* open the debugfs entry */ + fd =open (path,O_RDONLY ); +if (fd <0 ) +return -errno; + +do { +/* read it */ + ret =read (fd, buffer, size); +if (ret ==0 ) { +close (fd); +return EOF ; + } + }while (ret <0 && errno ==EAGAIN ); + +/* close it */ +close (fd); + +/* make *sure* there's a null character at the end */ + buffer[ret] ='\0' ; + +/* return the number of chars read */ +return ret; +}
tools/perf/util/debugfs.h new file +22 -0
@@ -0,1 +1,22 @@ +#ifndef __DEBUGFS_H__ +#define __DEBUGFS_H__ +#include <sys/mount.h> +#ifndef MAX_PATH +#define MAX_PATH 256 +#endif + +#ifndef STR +#define _STR (x) #x +#define STR (x) _STR(x) +#endif + +extern const char *debugfs_find_mountpoint (void ); +extern int debugfs_valid_mountpoint (const char *debugfs); +extern int debugfs_valid_entry (const char *path); +extern int debugfs_mount (const char *mountpoint); +extern int debugfs_umount (void ); +extern int debugfs_write (const char *entry,const char *value); +extern int debugfs_read (const char *entry,char *buffer,size_t size); +extern void debugfs_force_cleanup (void ); +extern int debugfs_make_path (const char *element,char *buffer,int size); +#endif
Signature · 1 line
1.6.2.5
PATCH 2/2 modify perf routines to use new debugfs routines
In reply to Clark Williamsmodified perf.c get_debugfs_mntpnt() to use the util/debugfs.c debugfs_find_mountpoint()
modified util/parse-events.c to use debugfs_valid_mountpoint().
Signed-off-by Clark Williams <[email protected]>
CC Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
--- tools/perf/perf.c | 45 ++++++--------------------------------- tools/perf/util/parse-events.c | 17 +++----------- 2 files changed, 11 insertions(+), 51 deletions(-)
tools/perf/perf.c +7 -38
@@ -14,6 +14,7 @@#include "util/run-command.h" #include "util/parse-events.h" #include "util/string.h" +#include "util/debugfs.h" const char perf_usage_string[] ="perf [--version] [--help] COMMAND [ARGS]" ;
@@ -296,6 +297,7 @@ static void handle_internal_command(int argc, const char **argv) { "trace", cmd_trace, 0 }, { "sched", cmd_sched, 0 }, { "probe", cmd_probe, 0 }, + { "latency", cmd_latency, 0}, };unsigned int i;static const char ext[] =STRIP_EXTENSION ;
@@ -383,45 +385,12 @@ static int run_argv(int *argcp, const char ***argv)/* 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 ]; +const char *path =debugfs_find_mountpoint (); -/* -* 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' ; - } +if (path) +strncpy (debugfs_mntpt, path,sizeof (debugfs_mntpt)); +else + debugfs_mntpt[0 ] ='\0' ; }int main (int argc,const char **argv)
tools/perf/util/parse-events.c +4 -13
@@ -7,6 +7,7 @@#include "string.h" #include "cache.h" #include "header.h" +#include "debugfs.h" int nr_counters;
@@ -149,16 +150,6 @@ static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)#define MAX_EVENT_LENGTH 512 -int valid_debugfs_mount (const char *debugfs) -{ -struct statfs st_fs; - -if (statfs (debugfs, &st_fs) <0 ) -return -ENOENT ; -else if (st_fs.f_type != (long )DEBUGFS_MAGIC ) -return -ENOENT ; -return 0 ; -}struct tracepoint_path *tracepoint_id_to_path (u64 config) {
@@ -171,7 +162,7 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)char evt_path[MAXPATHLEN ];char dir_path[MAXPATHLEN ]; -if (valid_debugfs_mount (debugfs_path)) +if (debugfs_valid_mountpoint (debugfs_path))return NULL ; sys_dir =opendir (debugfs_path);
@@ -510,7 +501,7 @@ static enum event_result parse_tracepoint_event(const char **strp,char sys_name[MAX_EVENT_LENGTH ];unsigned int sys_length, evt_length; -if (valid_debugfs_mount (debugfs_path)) +if (debugfs_valid_mountpoint (debugfs_path))return 0 ; evt_name =strchr (*strp,':' );
@@ -788,7 +779,7 @@ static void print_tracepoint_events(void)char evt_path[MAXPATHLEN ];char dir_path[MAXPATHLEN ]; -if (valid_debugfs_mount (debugfs_path)) +if (debugfs_valid_mountpoint (debugfs_path))return ; sys_dir =opendir (debugfs_path);
Signature · 1 line
1.6.2.5