diff options
author | Ulrich Drepper <drepper@redhat.com> | 2009-12-19 16:40:28 -0500 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2009-12-28 04:18:37 -0500 |
commit | 659d8cfbb225f1fa5a4f8671a847ef3ab5a89660 (patch) | |
tree | 5eb4d89c29110f8d82edbb86b6bf3a6aee431e4a /tools/perf | |
parent | fd2a50a0240f5f5b59070474eabd83a85720a406 (diff) |
perf tools: Do a few more directory handling optimizations
A few more optimizations for perf when dealing with directories.
Some of them significantly cut down the work which has to be
done. d_type should always be set; otherwise fix the kernel
code. And there are functions available to parse fstab-like
files, so use them.
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Cc: a.p.zijlstra@chello.nl
Cc: acme@redhat.com
Cc: eranian@google.com
Cc: fweisbec@gmail.com
Cc: lizf@cn.fujitsu.com
Cc: paulus@samba.org
Cc: xiaoguangrong@cn.fujitsu.com
LKML-Reference: <200912192140.nBJLeSfA028905@hs20-bc2-1.build.redhat.com>
[ v2: two small stylistic fixlets ]
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'tools/perf')
-rw-r--r-- | tools/perf/builtin-kmem.c | 17 | ||||
-rw-r--r-- | tools/perf/util/trace-event-info.c | 50 |
2 files changed, 29 insertions, 38 deletions
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index 4c06828fe39d..05dc5a735039 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c | |||
@@ -92,23 +92,18 @@ static void setup_cpunode_map(void) | |||
92 | if (!dir1) | 92 | if (!dir1) |
93 | return; | 93 | return; |
94 | 94 | ||
95 | while (true) { | 95 | while ((dent1 = readdir(dir1)) != NULL) { |
96 | dent1 = readdir(dir1); | 96 | if (dent1->d_type != DT_DIR || |
97 | if (!dent1) | 97 | sscanf(dent1->d_name, "node%u", &mem) < 1) |
98 | break; | ||
99 | |||
100 | if (sscanf(dent1->d_name, "node%u", &mem) < 1) | ||
101 | continue; | 98 | continue; |
102 | 99 | ||
103 | snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name); | 100 | snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name); |
104 | dir2 = opendir(buf); | 101 | dir2 = opendir(buf); |
105 | if (!dir2) | 102 | if (!dir2) |
106 | continue; | 103 | continue; |
107 | while (true) { | 104 | while ((dent2 = readdir(dir2)) != NULL) { |
108 | dent2 = readdir(dir2); | 105 | if (dent2->d_type != DT_LNK || |
109 | if (!dent2) | 106 | sscanf(dent2->d_name, "cpu%u", &cpu) < 1) |
110 | break; | ||
111 | if (sscanf(dent2->d_name, "cpu%u", &cpu) < 1) | ||
112 | continue; | 107 | continue; |
113 | cpunode_map[cpu] = mem; | 108 | cpunode_map[cpu] = mem; |
114 | } | 109 | } |
diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c index cace35595530..dfef238ce158 100644 --- a/tools/perf/util/trace-event-info.c +++ b/tools/perf/util/trace-event-info.c | |||
@@ -20,6 +20,7 @@ | |||
20 | */ | 20 | */ |
21 | #define _GNU_SOURCE | 21 | #define _GNU_SOURCE |
22 | #include <dirent.h> | 22 | #include <dirent.h> |
23 | #include <mntent.h> | ||
23 | #include <stdio.h> | 24 | #include <stdio.h> |
24 | #include <stdlib.h> | 25 | #include <stdlib.h> |
25 | #include <string.h> | 26 | #include <string.h> |
@@ -103,28 +104,28 @@ static const char *find_debugfs(void) | |||
103 | { | 104 | { |
104 | static char debugfs[MAX_PATH+1]; | 105 | static char debugfs[MAX_PATH+1]; |
105 | static int debugfs_found; | 106 | static int debugfs_found; |
106 | char type[100]; | ||
107 | FILE *fp; | 107 | FILE *fp; |
108 | struct mntent *m; | ||
108 | 109 | ||
109 | if (debugfs_found) | 110 | if (debugfs_found) |
110 | return debugfs; | 111 | return debugfs; |
111 | 112 | ||
112 | if ((fp = fopen("/proc/mounts","r")) == NULL) | 113 | fp = setmntent("/proc/mounts", "r"); |
114 | if (!fp) | ||
113 | die("Can't open /proc/mounts for read"); | 115 | die("Can't open /proc/mounts for read"); |
114 | 116 | ||
115 | while (fscanf(fp, "%*s %" | 117 | while ((m = getmntent(fp)) != NULL) { |
116 | STR(MAX_PATH) | 118 | if (strcmp(m->mnt_type, "debugfs") == 0) { |
117 | "s %99s %*s %*d %*d\n", | 119 | strcpy(debugfs, m->mnt_dir); |
118 | debugfs, type) == 2) { | 120 | debugfs_found = 1; |
119 | if (strcmp(type, "debugfs") == 0) | ||
120 | break; | 121 | break; |
122 | } | ||
121 | } | 123 | } |
122 | fclose(fp); | ||
123 | 124 | ||
124 | if (strcmp(type, "debugfs") != 0) | 125 | endmntent(fp); |
125 | die("debugfs not mounted, please mount"); | ||
126 | 126 | ||
127 | debugfs_found = 1; | 127 | if (!debugfs_found) |
128 | die("debugfs not mounted, please mount"); | ||
128 | 129 | ||
129 | return debugfs; | 130 | return debugfs; |
130 | } | 131 | } |
@@ -317,7 +318,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps) | |||
317 | die("can't read directory '%s'", sys); | 318 | die("can't read directory '%s'", sys); |
318 | 319 | ||
319 | while ((dent = readdir(dir))) { | 320 | while ((dent = readdir(dir))) { |
320 | if (strcmp(dent->d_name, ".") == 0 || | 321 | if (dent->d_type != DT_DIR || |
322 | strcmp(dent->d_name, ".") == 0 || | ||
321 | strcmp(dent->d_name, "..") == 0 || | 323 | strcmp(dent->d_name, "..") == 0 || |
322 | !name_in_tp_list(dent->d_name, tps)) | 324 | !name_in_tp_list(dent->d_name, tps)) |
323 | continue; | 325 | continue; |
@@ -334,7 +336,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps) | |||
334 | 336 | ||
335 | rewinddir(dir); | 337 | rewinddir(dir); |
336 | while ((dent = readdir(dir))) { | 338 | while ((dent = readdir(dir))) { |
337 | if (strcmp(dent->d_name, ".") == 0 || | 339 | if (dent->d_type != DT_DIR || |
340 | strcmp(dent->d_name, ".") == 0 || | ||
338 | strcmp(dent->d_name, "..") == 0 || | 341 | strcmp(dent->d_name, "..") == 0 || |
339 | !name_in_tp_list(dent->d_name, tps)) | 342 | !name_in_tp_list(dent->d_name, tps)) |
340 | continue; | 343 | continue; |
@@ -394,26 +397,21 @@ static void read_event_files(struct tracepoint_path *tps) | |||
394 | die("can't read directory '%s'", path); | 397 | die("can't read directory '%s'", path); |
395 | 398 | ||
396 | while ((dent = readdir(dir))) { | 399 | while ((dent = readdir(dir))) { |
397 | if (strcmp(dent->d_name, ".") == 0 || | 400 | if (dent->d_type != DT_DIR || |
401 | strcmp(dent->d_name, ".") == 0 || | ||
398 | strcmp(dent->d_name, "..") == 0 || | 402 | strcmp(dent->d_name, "..") == 0 || |
399 | strcmp(dent->d_name, "ftrace") == 0 || | 403 | strcmp(dent->d_name, "ftrace") == 0 || |
400 | !system_in_tp_list(dent->d_name, tps)) | 404 | !system_in_tp_list(dent->d_name, tps)) |
401 | continue; | 405 | continue; |
402 | sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2); | 406 | count++; |
403 | sprintf(sys, "%s/%s", path, dent->d_name); | ||
404 | ret = stat(sys, &st); | ||
405 | free(sys); | ||
406 | if (ret < 0) | ||
407 | continue; | ||
408 | if (S_ISDIR(st.st_mode)) | ||
409 | count++; | ||
410 | } | 407 | } |
411 | 408 | ||
412 | write_or_die(&count, 4); | 409 | write_or_die(&count, 4); |
413 | 410 | ||
414 | rewinddir(dir); | 411 | rewinddir(dir); |
415 | while ((dent = readdir(dir))) { | 412 | while ((dent = readdir(dir))) { |
416 | if (strcmp(dent->d_name, ".") == 0 || | 413 | if (dent->d_type != DT_DIR || |
414 | strcmp(dent->d_name, ".") == 0 || | ||
417 | strcmp(dent->d_name, "..") == 0 || | 415 | strcmp(dent->d_name, "..") == 0 || |
418 | strcmp(dent->d_name, "ftrace") == 0 || | 416 | strcmp(dent->d_name, "ftrace") == 0 || |
419 | !system_in_tp_list(dent->d_name, tps)) | 417 | !system_in_tp_list(dent->d_name, tps)) |
@@ -422,10 +420,8 @@ static void read_event_files(struct tracepoint_path *tps) | |||
422 | sprintf(sys, "%s/%s", path, dent->d_name); | 420 | sprintf(sys, "%s/%s", path, dent->d_name); |
423 | ret = stat(sys, &st); | 421 | ret = stat(sys, &st); |
424 | if (ret >= 0) { | 422 | if (ret >= 0) { |
425 | if (S_ISDIR(st.st_mode)) { | 423 | write_or_die(dent->d_name, strlen(dent->d_name) + 1); |
426 | write_or_die(dent->d_name, strlen(dent->d_name) + 1); | 424 | copy_event_system(sys, tps); |
427 | copy_event_system(sys, tps); | ||
428 | } | ||
429 | } | 425 | } |
430 | free(sys); | 426 | free(sys); |
431 | } | 427 | } |