Skip to contentSkip to conversation

perf: excluding "." and ".." directories when calculating tids.

PATCH In review Gui Jianfeng 3 participants 7 messages Jun 15, 2010 – Jul 3, 2010 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: excluding "." and ".." directories when calculating tids.

excluding "." and ".." directories when calculating tids.

---
 tools/perf/util/thread.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
tools/perf/util/thread.c +2 -0
index 1f7ecd4..4f71d1c 100644
@@ -17,6 +17,8 @@ int find_all_tid(int pid, pid_t ** all_tid)
 
 	sprintf(name, "/proc/%d/task", pid);
 	items = scandir(name, &namelist, NULL, NULL);
+	/* Excluding "." and ".." directories! */
+	items -= 2;
 	if (items <= 0)
                 return -ENOENT;
 	*all_tid = malloc(sizeof(pid_t) * items);
Signature · 1 line
1.6.5.2
#1 lore raw
Quoted 1 line · Gui Jianfeng

excluding "." and ".." directories when calculating tids.

Please ignore this one, will post an updated version.

Thanks,
Gui

Quoted 19 lines
---
 tools/perf/util/thread.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
tools/perf/util/thread.c +2 -0
index 1f7ecd4..4f71d1c 100644
@@ -17,6 +17,8 @@ int find_all_tid(int pid, pid_t ** all_tid)
 
 	sprintf(name, "/proc/%d/task", pid);
 	items = scandir(name, &namelist, NULL, NULL);
+	/* Excluding "." and ".." directories! */
+	items -= 2;
 	if (items <= 0)
                 return -ENOENT;
 	*all_tid = malloc(sizeof(pid_t) * items);
#2 lore raw

PATCH 1/1 perf: excluding "." and ".." directories when calculating tids.

Introduce a filter function to skip "." and ".." directories when calculating tid number.

---
 tools/perf/util/thread.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
tools/perf/util/thread.c +10 -1
index 1f7ecd4..9a448b4 100644
@@ -7,6 +7,15 @@
 #include "util.h"
 #include "debug.h"
 
+/* Skip "." and ".." directories */
+static int filter(const struct dirent *dir)
+{
+	if (dir->d_name[0] == '.')
+		return 0;
+	else
+		return 1;
+}
+
 int find_all_tid(int pid, pid_t ** all_tid)
 {
 	char name[256];
@@ -16,7 +25,7 @@ int find_all_tid(int pid, pid_t ** all_tid)
 	int i;
 
 	sprintf(name, "/proc/%d/task", pid);
-	items = scandir(name, &namelist, NULL, NULL);
+	items = scandir(name, &namelist, filter, NULL);
 	if (items <= 0)
                 return -ENOENT;
 	*all_tid = malloc(sizeof(pid_t) * items);
Signature · 1 line
1.6.5.2
#3 lore raw
Quoted 16 lines · Gui Jianfeng

Introduce a filter function to skip "." and ".." directories when calculating tid number.

---
 tools/perf/util/thread.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
tools/perf/util/thread.c +0 -0
index 1f7ecd4..9a448b4 100644
@@ -7,6 +7,15 @@
 #include "util.h"
 #include "debug.h"
 

tools/perf/util/thread.c line 19

Quoted 10 lines
tools/perf/util/thread.c +9 -0
+/* Skip "." and ".." directories */
+static int filter(const struct dirent *dir)
+{
+	if (dir->d_name[0] == '.')
+		return 0;
+	else
+		return 1;
+}
+
 

Is this safe? Can you _never_ have a d_name with a leading dot, like ' .hidden' ??
Maybe should
if(dir->d_name[0] == '.' && (dir->d_name[1] == '\0' || (dir->d_name[1] == '.' && dir->d_name[2] == '\0')))

Admittedly I don't think it happens in the current procfs, but I'd want to be careful regardless.

#4 lore raw
Quoted 34 lines · Adam Schrotenboer
Quoted 16 lines · Gui Jianfeng

Introduce a filter function to skip "." and ".." directories when calculating tid number.

---
 tools/perf/util/thread.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
tools/perf/util/thread.c +0 -0
index 1f7ecd4..9a448b4 100644
@@ -7,6 +7,15 @@
 #include "util.h"
 #include "debug.h"
 

tools/perf/util/thread.c line 19

Quoted 10 lines
tools/perf/util/thread.c +9 -0
+/* Skip "." and ".." directories */
+static int filter(const struct dirent *dir)
+{
+	if (dir->d_name[0] == '.')
+		return 0;
+	else
+		return 1;
+}
+
 

Is this safe? Can you _never_ have a d_name with a leading dot, like ' .hidden' ??
Maybe should
if(dir->d_name[0] == '.' && (dir->d_name[1] == '\0' || (dir->d_name[1] == '.' && dir->d_name[2] == '\0')))

Quoted 2 lines

Admittedly I don't think it happens in the current procfs, but I'd want to be careful regardless.

Actually, we only care the numeral directories. So, even if there's a ".hidden", it's fine to filter out this directory. Just keep things simple here.

Thanks,
Gui

Quoted 2 lines
#5 lore raw

Em Thu, Jun 17, 2010 at 11:11:35AM +0800, Gui Jianfeng escreveu:

Quoted 18 lines
Quoted 4 lines · Adam Schrotenboer
Quoted 3 lines · Gui Jianfeng

Introduce a filter function to skip "." and ".." directories when calculating tid number.

Quoted 12 lines

tools/perf/util/thread.c line 16

Quoted 7 lines
tools/perf/util/thread.c +7 -0
+/* Skip "." and ".." directories */
+static int filter(const struct dirent *dir)
+{
+	if (dir->d_name[0] == '.')
+		return 0;
+	else
+		return 1;

Is this safe? Can you _never_ have a d_name with a leading dot, like ' .hidden' ??
Admittedly I don't think it happens in the current procfs, but I'd want to be careful regardless.

Quoted 2 lines

Actually, we only care the numeral directories. So, even if there's a ".hidden", it's fine to filter out this directory. Just keep things simple here.

Agreed, applying to perf/core, thanks,

- Arnaldo

#6 lore raw

[tip:perf/urgent] perf tools: Fix find tids routine by excluding "." and ".."

In reply to Gui Jianfeng
Commit-ID:  c214909b36efec632432acdcbfacdd46a6e11370
Gitweb:     http://git.kernel.org/tip/c214909b36efec632432acdcbfacdd46a6e11370
Author:     Gui Jianfeng <[email protected]>
AuthorDate: Wed, 16 Jun 2010 13:21:44 +0800
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 1 Jul 2010 14:02:38 -0300

perf tools: Fix find tids routine by excluding "." and ".."

Introduce a filter function to skip "." and ".." directories when calculating tid number, otherwise tid 0 will be included in the all_tid result array.

Cc: Ingo Molnar <[email protected]>
LKML-Reference: <[email protected]>

---
 tools/perf/util/thread.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
tools/perf/util/thread.c +10 -1
index 1f7ecd4..9a448b4 100644
@@ -7,6 +7,15 @@
 #include "util.h"
 #include "debug.h"
 
+/* Skip "." and ".." directories */
+static int filter(const struct dirent *dir)
+{
+	if (dir->d_name[0] == '.')
+		return 0;
+	else
+		return 1;
+}
+
 int find_all_tid(int pid, pid_t ** all_tid)
 {
 	char name[256];
@@ -16,7 +25,7 @@ int find_all_tid(int pid, pid_t ** all_tid)
 	int i;
 
 	sprintf(name, "/proc/%d/task", pid);
-	items = scandir(name, &namelist, NULL, NULL);
+	items = scandir(name, &namelist, filter, NULL);
 	if (items <= 0)
                 return -ENOENT;
 	*all_tid = malloc(sizeof(pid_t) * items);