2010-06-15 09:00:51

by Gui, Jianfeng/归 剑峰

[permalink] [raw]
Subject: [PATCH] perf: excluding "." and ".." directories when calculating tids.

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

Signed-off-by: Gui Jianfeng <[email protected]>
---
tools/perf/util/thread.c | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 1f7ecd4..4f71d1c 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -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);
--
1.6.5.2


2010-06-15 09:35:36

by Gui, Jianfeng/归 剑峰

[permalink] [raw]
Subject: Re: [PATCH] perf: excluding "." and ".." directories when calculating tids.

Gui Jianfeng wrote:
> excluding "." and ".." directories when calculating tids.

Please ignore this one, will post an updated version.

Thanks,
Gui

>
> Signed-off-by: Gui Jianfeng <[email protected]>
> ---
> tools/perf/util/thread.c | 2 ++
> 1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> index 1f7ecd4..4f71d1c 100644
> --- a/tools/perf/util/thread.c
> +++ b/tools/perf/util/thread.c
> @@ -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);

2010-06-16 05:24:13

by Gui, Jianfeng/归 剑峰

[permalink] [raw]
Subject: [PATCH] perf: excluding "." and ".." directories when calculating tids.

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

Signed-off-by: Gui Jianfeng <[email protected]>
---
tools/perf/util/thread.c | 11 ++++++++++-
1 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 1f7ecd4..9a448b4 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -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);
--
1.6.5.2

2010-06-16 16:43:28

by Adam Schrotenboer

[permalink] [raw]
Subject: Re: [PATCH] perf: excluding "." and ".." directories when calculating tids.

On 06/15/2010 10:21 PM, Gui Jianfeng wrote:
> Introduce a filter function to skip "." and ".." directories when calculating
> tid number.
>
> Signed-off-by: Gui Jianfeng <[email protected]>
> ---
> tools/perf/util/thread.c | 11 ++++++++++-
> 1 files changed, 10 insertions(+), 1 deletions(-)
>
> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> index 1f7ecd4..9a448b4 100644
> --- a/tools/perf/util/thread.c
> +++ b/tools/perf/util/thread.c
> @@ -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;
> +}
> +
>

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.



Attachments:
signature.asc (261.00 B)
OpenPGP digital signature

2010-06-17 03:14:09

by Gui, Jianfeng/归 剑峰

[permalink] [raw]
Subject: Re: [PATCH] perf: excluding "." and ".." directories when calculating tids.

Adam Schrotenboer wrote:
> On 06/15/2010 10:21 PM, Gui Jianfeng wrote:
>> Introduce a filter function to skip "." and ".." directories when calculating
>> tid number.
>>
>> Signed-off-by: Gui Jianfeng <[email protected]>
>> ---
>> tools/perf/util/thread.c | 11 ++++++++++-
>> 1 files changed, 10 insertions(+), 1 deletions(-)
>>
>> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
>> index 1f7ecd4..9a448b4 100644
>> --- a/tools/perf/util/thread.c
>> +++ b/tools/perf/util/thread.c
>> @@ -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;
>> +}
>> +
>>
>
> 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.

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

>
>

2010-06-18 14:40:27

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH] perf: excluding "." and ".." directories when calculating tids.

Em Thu, Jun 17, 2010 at 11:11:35AM +0800, Gui Jianfeng escreveu:
> Adam Schrotenboer wrote:
> > On 06/15/2010 10:21 PM, Gui Jianfeng wrote:
> >> Introduce a filter function to skip "." and ".." directories when calculating
> >> tid number.
> >>
> >> +/* 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.
>
> 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

2010-07-03 13:58:16

by Gui, Jianfeng/归 剑峰

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

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]>
Signed-off-by: Gui Jianfeng <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/thread.c | 11 ++++++++++-
1 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 1f7ecd4..9a448b4 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -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);