diff options
author | Jiri Olsa <jolsa@redhat.com> | 2013-11-05 09:14:45 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2013-11-05 12:44:26 -0500 |
commit | 4299a549979783668d787959d61ba22b6b200877 (patch) | |
tree | c55824db21d990ee0d0a65318b8362c0c081911a | |
parent | 44d742e01e6d3dd544ee1873b660a3c8bc1413bb (diff) |
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>
-rw-r--r-- | tools/perf/Makefile.perf | 4 | ||||
-rw-r--r-- | tools/perf/tests/parse-events.c | 2 | ||||
-rw-r--r-- | tools/perf/util/cpumap.c | 2 | ||||
-rw-r--r-- | tools/perf/util/fs.c | 107 | ||||
-rw-r--r-- | tools/perf/util/fs.h | 6 | ||||
-rw-r--r-- | tools/perf/util/pmu.c | 2 | ||||
-rw-r--r-- | tools/perf/util/python-ext-sources | 2 | ||||
-rw-r--r-- | tools/perf/util/sysfs.c | 60 | ||||
-rw-r--r-- | tools/perf/util/sysfs.h | 6 |
9 files changed, 119 insertions, 72 deletions
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index 5b8639025aae..7fc8f179cae7 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf | |||
@@ -242,7 +242,7 @@ LIB_H += util/cache.h | |||
242 | LIB_H += util/callchain.h | 242 | LIB_H += util/callchain.h |
243 | LIB_H += util/build-id.h | 243 | LIB_H += util/build-id.h |
244 | LIB_H += util/debug.h | 244 | LIB_H += util/debug.h |
245 | LIB_H += util/sysfs.h | 245 | LIB_H += util/fs.h |
246 | LIB_H += util/pmu.h | 246 | LIB_H += util/pmu.h |
247 | LIB_H += util/event.h | 247 | LIB_H += util/event.h |
248 | LIB_H += util/evsel.h | 248 | LIB_H += util/evsel.h |
@@ -304,7 +304,7 @@ LIB_OBJS += $(OUTPUT)util/annotate.o | |||
304 | LIB_OBJS += $(OUTPUT)util/build-id.o | 304 | LIB_OBJS += $(OUTPUT)util/build-id.o |
305 | LIB_OBJS += $(OUTPUT)util/config.o | 305 | LIB_OBJS += $(OUTPUT)util/config.o |
306 | LIB_OBJS += $(OUTPUT)util/ctype.o | 306 | LIB_OBJS += $(OUTPUT)util/ctype.o |
307 | LIB_OBJS += $(OUTPUT)util/sysfs.o | 307 | LIB_OBJS += $(OUTPUT)util/fs.o |
308 | LIB_OBJS += $(OUTPUT)util/pmu.o | 308 | LIB_OBJS += $(OUTPUT)util/pmu.o |
309 | LIB_OBJS += $(OUTPUT)util/environment.o | 309 | LIB_OBJS += $(OUTPUT)util/environment.o |
310 | LIB_OBJS += $(OUTPUT)util/event.o | 310 | LIB_OBJS += $(OUTPUT)util/event.o |
diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-events.c index 48114d164e9f..f47bf459c3f5 100644 --- a/tools/perf/tests/parse-events.c +++ b/tools/perf/tests/parse-events.c | |||
@@ -2,7 +2,7 @@ | |||
2 | #include "parse-events.h" | 2 | #include "parse-events.h" |
3 | #include "evsel.h" | 3 | #include "evsel.h" |
4 | #include "evlist.h" | 4 | #include "evlist.h" |
5 | #include "sysfs.h" | 5 | #include "fs.h" |
6 | #include <lk/debugfs.h> | 6 | #include <lk/debugfs.h> |
7 | #include "tests.h" | 7 | #include "tests.h" |
8 | #include <linux/hw_breakpoint.h> | 8 | #include <linux/hw_breakpoint.h> |
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index beb8cf9f9976..4af5a23b2423 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c | |||
@@ -1,5 +1,5 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | #include "sysfs.h" | 2 | #include "fs.h" |
3 | #include "../perf.h" | 3 | #include "../perf.h" |
4 | #include "cpumap.h" | 4 | #include "cpumap.h" |
5 | #include <assert.h> | 5 | #include <assert.h> |
diff --git a/tools/perf/util/fs.c b/tools/perf/util/fs.c new file mode 100644 index 000000000000..a2413e842a02 --- /dev/null +++ b/tools/perf/util/fs.c | |||
@@ -0,0 +1,107 @@ | |||
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); | ||
diff --git a/tools/perf/util/fs.h b/tools/perf/util/fs.h new file mode 100644 index 000000000000..082edbd2159b --- /dev/null +++ b/tools/perf/util/fs.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #ifndef __PERF_FS | ||
2 | #define __PERF_FS | ||
3 | |||
4 | const char *sysfs_find_mountpoint(void); | ||
5 | |||
6 | #endif /* __PERF_FS */ | ||
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 64362fe45b71..45b42dffcd70 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c | |||
@@ -4,7 +4,7 @@ | |||
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | #include <dirent.h> | 6 | #include <dirent.h> |
7 | #include "sysfs.h" | 7 | #include "fs.h" |
8 | #include "util.h" | 8 | #include "util.h" |
9 | #include "pmu.h" | 9 | #include "pmu.h" |
10 | #include "parse-events.h" | 10 | #include "parse-events.h" |
diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources index f75ae1b9900c..239036fb2b2c 100644 --- a/tools/perf/util/python-ext-sources +++ b/tools/perf/util/python-ext-sources | |||
@@ -17,5 +17,5 @@ util/xyarray.c | |||
17 | util/cgroup.c | 17 | util/cgroup.c |
18 | util/rblist.c | 18 | util/rblist.c |
19 | util/strlist.c | 19 | util/strlist.c |
20 | util/sysfs.c | 20 | util/fs.c |
21 | ../../lib/rbtree.c | 21 | ../../lib/rbtree.c |
diff --git a/tools/perf/util/sysfs.c b/tools/perf/util/sysfs.c deleted file mode 100644 index f71e9eafe15a..000000000000 --- a/tools/perf/util/sysfs.c +++ /dev/null | |||
@@ -1,60 +0,0 @@ | |||
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 | } | ||
diff --git a/tools/perf/util/sysfs.h b/tools/perf/util/sysfs.h deleted file mode 100644 index a813b7203938..000000000000 --- a/tools/perf/util/sysfs.h +++ /dev/null | |||
@@ -1,6 +0,0 @@ | |||
1 | #ifndef __SYSFS_H__ | ||
2 | #define __SYSFS_H__ | ||
3 | |||
4 | const char *sysfs_find_mountpoint(void); | ||
5 | |||
6 | #endif /* __DEBUGFS_H__ */ | ||