Skip to content

Commit 659d8cf

Browse files
Ulrich DrepperIngo Molnar
authored andcommitted
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>
1 parent fd2a50a commit 659d8cf

2 files changed

Lines changed: 29 additions & 38 deletions

File tree

‎tools/perf/builtin-kmem.c‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,18 @@ static void setup_cpunode_map(void)
9292
if (!dir1)
9393
return;
9494

95-
while (true) {
96-
dent1 = readdir(dir1);
97-
if (!dent1)
98-
break;
99-
100-
if (sscanf(dent1->d_name, "node%u", &mem) < 1)
95+
while ((dent1 = readdir(dir1)) != NULL) {
96+
if (dent1->d_type != DT_DIR ||
97+
sscanf(dent1->d_name, "node%u", &mem) < 1)
10198
continue;
10299

103100
snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
104101
dir2 = opendir(buf);
105102
if (!dir2)
106103
continue;
107-
while (true) {
108-
dent2 = readdir(dir2);
109-
if (!dent2)
110-
break;
111-
if (sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
104+
while ((dent2 = readdir(dir2)) != NULL) {
105+
if (dent2->d_type != DT_LNK ||
106+
sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
112107
continue;
113108
cpunode_map[cpu] = mem;
114109
}

‎tools/perf/util/trace-event-info.c‎

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
#define _GNU_SOURCE
2222
#include <dirent.h>
23+
#include <mntent.h>
2324
#include <stdio.h>
2425
#include <stdlib.h>
2526
#include <string.h>
@@ -103,28 +104,28 @@ static const char *find_debugfs(void)
103104
{
104105
static char debugfs[MAX_PATH+1];
105106
static int debugfs_found;
106-
char type[100];
107107
FILE *fp;
108+
struct mntent *m;
108109

109110
if (debugfs_found)
110111
return debugfs;
111112

112-
if ((fp = fopen("/proc/mounts","r")) == NULL)
113+
fp = setmntent("/proc/mounts", "r");
114+
if (!fp)
113115
die("Can't open /proc/mounts for read");
114116

115-
while (fscanf(fp, "%*s %"
116-
STR(MAX_PATH)
117-
"s %99s %*s %*d %*d\n",
118-
debugfs, type) == 2) {
119-
if (strcmp(type, "debugfs") == 0)
117+
while ((m = getmntent(fp)) != NULL) {
118+
if (strcmp(m->mnt_type, "debugfs") == 0) {
119+
strcpy(debugfs, m->mnt_dir);
120+
debugfs_found = 1;
120121
break;
122+
}
121123
}
122-
fclose(fp);
123124

124-
if (strcmp(type, "debugfs") != 0)
125-
die("debugfs not mounted, please mount");
125+
endmntent(fp);
126126

127-
debugfs_found = 1;
127+
if (!debugfs_found)
128+
die("debugfs not mounted, please mount");
128129

129130
return debugfs;
130131
}
@@ -317,7 +318,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps)
317318
die("can't read directory '%s'", sys);
318319

319320
while ((dent = readdir(dir))) {
320-
if (strcmp(dent->d_name, ".") == 0 ||
321+
if (dent->d_type != DT_DIR ||
322+
strcmp(dent->d_name, ".") == 0 ||
321323
strcmp(dent->d_name, "..") == 0 ||
322324
!name_in_tp_list(dent->d_name, tps))
323325
continue;
@@ -334,7 +336,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps)
334336

335337
rewinddir(dir);
336338
while ((dent = readdir(dir))) {
337-
if (strcmp(dent->d_name, ".") == 0 ||
339+
if (dent->d_type != DT_DIR ||
340+
strcmp(dent->d_name, ".") == 0 ||
338341
strcmp(dent->d_name, "..") == 0 ||
339342
!name_in_tp_list(dent->d_name, tps))
340343
continue;
@@ -394,26 +397,21 @@ static void read_event_files(struct tracepoint_path *tps)
394397
die("can't read directory '%s'", path);
395398

396399
while ((dent = readdir(dir))) {
397-
if (strcmp(dent->d_name, ".") == 0 ||
400+
if (dent->d_type != DT_DIR ||
401+
strcmp(dent->d_name, ".") == 0 ||
398402
strcmp(dent->d_name, "..") == 0 ||
399403
strcmp(dent->d_name, "ftrace") == 0 ||
400404
!system_in_tp_list(dent->d_name, tps))
401405
continue;
402-
sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
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++;
406+
count++;
410407
}
411408

412409
write_or_die(&count, 4);
413410

414411
rewinddir(dir);
415412
while ((dent = readdir(dir))) {
416-
if (strcmp(dent->d_name, ".") == 0 ||
413+
if (dent->d_type != DT_DIR ||
414+
strcmp(dent->d_name, ".") == 0 ||
417415
strcmp(dent->d_name, "..") == 0 ||
418416
strcmp(dent->d_name, "ftrace") == 0 ||
419417
!system_in_tp_list(dent->d_name, tps))
@@ -422,10 +420,8 @@ static void read_event_files(struct tracepoint_path *tps)
422420
sprintf(sys, "%s/%s", path, dent->d_name);
423421
ret = stat(sys, &st);
424422
if (ret >= 0) {
425-
if (S_ISDIR(st.st_mode)) {
426-
write_or_die(dent->d_name, strlen(dent->d_name) + 1);
427-
copy_event_system(sys, tps);
428-
}
423+
write_or_die(dent->d_name, strlen(dent->d_name) + 1);
424+
copy_event_system(sys, tps);
429425
}
430426
free(sys);
431427
}

0 commit comments

Comments
 (0)