Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Factor sysfs code into generic fs object

Moving sysfs code into generic fs object and preparing it to carry
procfs support.

This should be merged with tools/lib/lk/debugfs.c at some point in the
future.

Signed-off-by: Jiri Olsa <jolsa@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1383660887-1734-2-git-send-email-jolsa@redhat.com
[ Added fs__ namespace qualifier to some more functions ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
4299a549 44d742e0

+119 -72
+2 -2
tools/perf/Makefile.perf
··· 242 242 LIB_H += util/callchain.h 243 243 LIB_H += util/build-id.h 244 244 LIB_H += util/debug.h 245 - LIB_H += util/sysfs.h 245 + LIB_H += util/fs.h 246 246 LIB_H += util/pmu.h 247 247 LIB_H += util/event.h 248 248 LIB_H += util/evsel.h ··· 304 304 LIB_OBJS += $(OUTPUT)util/build-id.o 305 305 LIB_OBJS += $(OUTPUT)util/config.o 306 306 LIB_OBJS += $(OUTPUT)util/ctype.o 307 - LIB_OBJS += $(OUTPUT)util/sysfs.o 307 + LIB_OBJS += $(OUTPUT)util/fs.o 308 308 LIB_OBJS += $(OUTPUT)util/pmu.o 309 309 LIB_OBJS += $(OUTPUT)util/environment.o 310 310 LIB_OBJS += $(OUTPUT)util/event.o
+1 -1
tools/perf/tests/parse-events.c
··· 2 2 #include "parse-events.h" 3 3 #include "evsel.h" 4 4 #include "evlist.h" 5 - #include "sysfs.h" 5 + #include "fs.h" 6 6 #include <lk/debugfs.h> 7 7 #include "tests.h" 8 8 #include <linux/hw_breakpoint.h>
+1 -1
tools/perf/util/cpumap.c
··· 1 1 #include "util.h" 2 - #include "sysfs.h" 2 + #include "fs.h" 3 3 #include "../perf.h" 4 4 #include "cpumap.h" 5 5 #include <assert.h>
+107
tools/perf/util/fs.c
··· 1 + 2 + /* TODO merge/factor into tools/lib/lk/debugfs.c */ 3 + 4 + #include "util.h" 5 + #include "util/fs.h" 6 + 7 + static const char * const sysfs__fs_known_mountpoints[] = { 8 + "/sys", 9 + 0, 10 + }; 11 + 12 + struct fs { 13 + const char *name; 14 + const char * const *mounts; 15 + char path[PATH_MAX + 1]; 16 + bool found; 17 + long magic; 18 + }; 19 + 20 + enum { 21 + FS__SYSFS = 0, 22 + }; 23 + 24 + static struct fs fs__entries[] = { 25 + [FS__SYSFS] = { 26 + .name = "sysfs", 27 + .mounts = sysfs__fs_known_mountpoints, 28 + .magic = SYSFS_MAGIC, 29 + }, 30 + }; 31 + 32 + static bool fs__read_mounts(struct fs *fs) 33 + { 34 + bool found = false; 35 + char type[100]; 36 + FILE *fp; 37 + 38 + fp = fopen("/proc/mounts", "r"); 39 + if (fp == NULL) 40 + return NULL; 41 + 42 + while (!found && 43 + fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", 44 + fs->path, type) == 2) { 45 + 46 + if (strcmp(type, fs->name) == 0) 47 + found = true; 48 + } 49 + 50 + fclose(fp); 51 + return fs->found = found; 52 + } 53 + 54 + static int fs__valid_mount(const char *fs, long magic) 55 + { 56 + struct statfs st_fs; 57 + 58 + if (statfs(fs, &st_fs) < 0) 59 + return -ENOENT; 60 + else if (st_fs.f_type != magic) 61 + return -ENOENT; 62 + 63 + return 0; 64 + } 65 + 66 + static bool fs__check_mounts(struct fs *fs) 67 + { 68 + const char * const *ptr; 69 + 70 + ptr = fs->mounts; 71 + while (*ptr) { 72 + if (fs__valid_mount(*ptr, fs->magic) == 0) { 73 + fs->found = true; 74 + strcpy(fs->path, *ptr); 75 + return true; 76 + } 77 + ptr++; 78 + } 79 + 80 + return false; 81 + } 82 + 83 + static const char *fs__get_mountpoint(struct fs *fs) 84 + { 85 + if (fs__check_mounts(fs)) 86 + return fs->path; 87 + 88 + return fs__read_mounts(fs) ? fs->path : NULL; 89 + } 90 + 91 + static const char *fs__find_mountpoint(int idx) 92 + { 93 + struct fs *fs = &fs__entries[idx]; 94 + 95 + if (fs->found) 96 + return (const char *)fs->path; 97 + 98 + return fs__get_mountpoint(fs); 99 + } 100 + 101 + #define FIND_MOUNTPOINT(name, idx) \ 102 + const char *name##_find_mountpoint(void) \ 103 + { \ 104 + return fs__find_mountpoint(idx); \ 105 + } 106 + 107 + FIND_MOUNTPOINT(sysfs, FS__SYSFS);
+6
tools/perf/util/fs.h
··· 1 + #ifndef __PERF_FS 2 + #define __PERF_FS 3 + 4 + const char *sysfs_find_mountpoint(void); 5 + 6 + #endif /* __PERF_FS */
+1 -1
tools/perf/util/pmu.c
··· 4 4 #include <unistd.h> 5 5 #include <stdio.h> 6 6 #include <dirent.h> 7 - #include "sysfs.h" 7 + #include "fs.h" 8 8 #include "util.h" 9 9 #include "pmu.h" 10 10 #include "parse-events.h"
+1 -1
tools/perf/util/python-ext-sources
··· 17 17 util/cgroup.c 18 18 util/rblist.c 19 19 util/strlist.c 20 - util/sysfs.c 20 + util/fs.c 21 21 ../../lib/rbtree.c
-60
tools/perf/util/sysfs.c
··· 1 - 2 - #include "util.h" 3 - #include "sysfs.h" 4 - 5 - static const char * const sysfs_known_mountpoints[] = { 6 - "/sys", 7 - 0, 8 - }; 9 - 10 - static int sysfs_found; 11 - char sysfs_mountpoint[PATH_MAX + 1]; 12 - 13 - static int sysfs_valid_mountpoint(const char *sysfs) 14 - { 15 - struct statfs st_fs; 16 - 17 - if (statfs(sysfs, &st_fs) < 0) 18 - return -ENOENT; 19 - else if (st_fs.f_type != (long) SYSFS_MAGIC) 20 - return -ENOENT; 21 - 22 - return 0; 23 - } 24 - 25 - const char *sysfs_find_mountpoint(void) 26 - { 27 - const char * const *ptr; 28 - char type[100]; 29 - FILE *fp; 30 - 31 - if (sysfs_found) 32 - return (const char *) sysfs_mountpoint; 33 - 34 - ptr = sysfs_known_mountpoints; 35 - while (*ptr) { 36 - if (sysfs_valid_mountpoint(*ptr) == 0) { 37 - sysfs_found = 1; 38 - strcpy(sysfs_mountpoint, *ptr); 39 - return sysfs_mountpoint; 40 - } 41 - ptr++; 42 - } 43 - 44 - /* give up and parse /proc/mounts */ 45 - fp = fopen("/proc/mounts", "r"); 46 - if (fp == NULL) 47 - return NULL; 48 - 49 - while (!sysfs_found && 50 - fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", 51 - sysfs_mountpoint, type) == 2) { 52 - 53 - if (strcmp(type, "sysfs") == 0) 54 - sysfs_found = 1; 55 - } 56 - 57 - fclose(fp); 58 - 59 - return sysfs_found ? sysfs_mountpoint : NULL; 60 - }
-6
tools/perf/util/sysfs.h
··· 1 - #ifndef __SYSFS_H__ 2 - #define __SYSFS_H__ 3 - 4 - const char *sysfs_find_mountpoint(void); 5 - 6 - #endif /* __DEBUGFS_H__ */