Commit 659d8cfb authored by Ulrich Drepper's avatar Ulrich Drepper Committed by Ingo Molnar

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: default avatarUlrich Drepper <drepper@redhat.com>
Acked-by: default avatarPekka 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: default avatarIngo Molnar <mingo@elte.hu>
parent fd2a50a0
...@@ -92,23 +92,18 @@ static void setup_cpunode_map(void) ...@@ -92,23 +92,18 @@ static void setup_cpunode_map(void)
if (!dir1) if (!dir1)
return; return;
while (true) { while ((dent1 = readdir(dir1)) != NULL) {
dent1 = readdir(dir1); if (dent1->d_type != DT_DIR ||
if (!dent1) sscanf(dent1->d_name, "node%u", &mem) < 1)
break;
if (sscanf(dent1->d_name, "node%u", &mem) < 1)
continue; continue;
snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name); snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
dir2 = opendir(buf); dir2 = opendir(buf);
if (!dir2) if (!dir2)
continue; continue;
while (true) { while ((dent2 = readdir(dir2)) != NULL) {
dent2 = readdir(dir2); if (dent2->d_type != DT_LNK ||
if (!dent2) sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
break;
if (sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
continue; continue;
cpunode_map[cpu] = mem; cpunode_map[cpu] = mem;
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <dirent.h> #include <dirent.h>
#include <mntent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -103,28 +104,28 @@ static const char *find_debugfs(void) ...@@ -103,28 +104,28 @@ static const char *find_debugfs(void)
{ {
static char debugfs[MAX_PATH+1]; static char debugfs[MAX_PATH+1];
static int debugfs_found; static int debugfs_found;
char type[100];
FILE *fp; FILE *fp;
struct mntent *m;
if (debugfs_found) if (debugfs_found)
return debugfs; return debugfs;
if ((fp = fopen("/proc/mounts","r")) == NULL) fp = setmntent("/proc/mounts", "r");
if (!fp)
die("Can't open /proc/mounts for read"); die("Can't open /proc/mounts for read");
while (fscanf(fp, "%*s %" while ((m = getmntent(fp)) != NULL) {
STR(MAX_PATH) if (strcmp(m->mnt_type, "debugfs") == 0) {
"s %99s %*s %*d %*d\n", strcpy(debugfs, m->mnt_dir);
debugfs, type) == 2) { debugfs_found = 1;
if (strcmp(type, "debugfs") == 0)
break; break;
}
} }
fclose(fp);
if (strcmp(type, "debugfs") != 0) endmntent(fp);
die("debugfs not mounted, please mount");
debugfs_found = 1; if (!debugfs_found)
die("debugfs not mounted, please mount");
return debugfs; return debugfs;
} }
...@@ -317,7 +318,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps) ...@@ -317,7 +318,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps)
die("can't read directory '%s'", sys); die("can't read directory '%s'", sys);
while ((dent = readdir(dir))) { while ((dent = readdir(dir))) {
if (strcmp(dent->d_name, ".") == 0 || if (dent->d_type != DT_DIR ||
strcmp(dent->d_name, ".") == 0 ||
strcmp(dent->d_name, "..") == 0 || strcmp(dent->d_name, "..") == 0 ||
!name_in_tp_list(dent->d_name, tps)) !name_in_tp_list(dent->d_name, tps))
continue; continue;
...@@ -334,7 +336,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps) ...@@ -334,7 +336,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps)
rewinddir(dir); rewinddir(dir);
while ((dent = readdir(dir))) { while ((dent = readdir(dir))) {
if (strcmp(dent->d_name, ".") == 0 || if (dent->d_type != DT_DIR ||
strcmp(dent->d_name, ".") == 0 ||
strcmp(dent->d_name, "..") == 0 || strcmp(dent->d_name, "..") == 0 ||
!name_in_tp_list(dent->d_name, tps)) !name_in_tp_list(dent->d_name, tps))
continue; continue;
...@@ -394,26 +397,21 @@ static void read_event_files(struct tracepoint_path *tps) ...@@ -394,26 +397,21 @@ static void read_event_files(struct tracepoint_path *tps)
die("can't read directory '%s'", path); die("can't read directory '%s'", path);
while ((dent = readdir(dir))) { while ((dent = readdir(dir))) {
if (strcmp(dent->d_name, ".") == 0 || if (dent->d_type != DT_DIR ||
strcmp(dent->d_name, ".") == 0 ||
strcmp(dent->d_name, "..") == 0 || strcmp(dent->d_name, "..") == 0 ||
strcmp(dent->d_name, "ftrace") == 0 || strcmp(dent->d_name, "ftrace") == 0 ||
!system_in_tp_list(dent->d_name, tps)) !system_in_tp_list(dent->d_name, tps))
continue; continue;
sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2); count++;
sprintf(sys, "%s/%s", path, dent->d_name);
ret = stat(sys, &st);
free(sys);
if (ret < 0)
continue;
if (S_ISDIR(st.st_mode))
count++;
} }
write_or_die(&count, 4); write_or_die(&count, 4);
rewinddir(dir); rewinddir(dir);
while ((dent = readdir(dir))) { while ((dent = readdir(dir))) {
if (strcmp(dent->d_name, ".") == 0 || if (dent->d_type != DT_DIR ||
strcmp(dent->d_name, ".") == 0 ||
strcmp(dent->d_name, "..") == 0 || strcmp(dent->d_name, "..") == 0 ||
strcmp(dent->d_name, "ftrace") == 0 || strcmp(dent->d_name, "ftrace") == 0 ||
!system_in_tp_list(dent->d_name, tps)) !system_in_tp_list(dent->d_name, tps))
...@@ -422,10 +420,8 @@ static void read_event_files(struct tracepoint_path *tps) ...@@ -422,10 +420,8 @@ static void read_event_files(struct tracepoint_path *tps)
sprintf(sys, "%s/%s", path, dent->d_name); sprintf(sys, "%s/%s", path, dent->d_name);
ret = stat(sys, &st); ret = stat(sys, &st);
if (ret >= 0) { if (ret >= 0) {
if (S_ISDIR(st.st_mode)) { write_or_die(dent->d_name, strlen(dent->d_name) + 1);
write_or_die(dent->d_name, strlen(dent->d_name) + 1); copy_event_system(sys, tps);
copy_event_system(sys, tps);
}
} }
free(sys); free(sys);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment