LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] perf-probe: rewrite find_lazy_match_lines() by using getline(3)
@ 2011-01-13 10:18 Franck Bui-Huu
  2011-01-13 10:25 ` Masami Hiramatsu
  2011-02-08 15:16 ` [tip:perf/core] perf probe: Rewrite " tip-bot for Franck Bui-Huu
  0 siblings, 2 replies; 5+ messages in thread
From: Franck Bui-Huu @ 2011-01-13 10:18 UTC (permalink / raw)
  To: Masami Hiramatsu; +Cc: Arnaldo Carvalho de Melo, lkml

From: Franck Bui-Huu <fbuihuu@gmail.com>

Signed-off-by: Franck Bui-Huu <fbuihuu@gmail.com>
---
 tools/perf/util/probe-finder.c |   70 +++++++++++++++------------------------
 1 files changed, 27 insertions(+), 43 deletions(-)

diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index ddf4d45..4221738 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -1092,51 +1092,38 @@ static int find_probe_point_by_line(struct probe_finder *pf)
 static int find_lazy_match_lines(struct list_head *head,
 				 const char *fname, const char *pat)
 {
-	char *fbuf, *p1, *p2;
-	int fd, line, nlines = -1;
-	struct stat st;
-
-	fd = open(fname, O_RDONLY);
-	if (fd < 0) {
-		pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
+	FILE *fp;
+	char *line = NULL;
+	size_t line_len;
+	ssize_t len;
+	int count = 0, linenum = 1;
+
+	fp = fopen(fname, "r");
+	if (!fp) {
+		pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
 		return -errno;
 	}
 
-	if (fstat(fd, &st) < 0) {
-		pr_warning("Failed to get the size of %s: %s\n",
-			   fname, strerror(errno));
-		nlines = -errno;
-		goto out_close;
-	}
-
-	nlines = -ENOMEM;
-	fbuf = malloc(st.st_size + 2);
-	if (fbuf == NULL)
-		goto out_close;
-	if (read(fd, fbuf, st.st_size) < 0) {
-		pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
-		nlines = -errno;
-		goto out_free_fbuf;
-	}
-	fbuf[st.st_size] = '\n';	/* Dummy line */
-	fbuf[st.st_size + 1] = '\0';
-	p1 = fbuf;
-	line = 1;
-	nlines = 0;
-	while ((p2 = strchr(p1, '\n')) != NULL) {
-		*p2 = '\0';
-		if (strlazymatch(p1, pat)) {
-			line_list__add_line(head, line);
-			nlines++;
+	while ((len = getline(&line, &line_len, fp)) > 0) {
+
+		if (line[len - 1] == '\n')
+			line[len - 1] = '\0';
+
+		if (strlazymatch(line, pat)) {
+			line_list__add_line(head, linenum);
+			count++;
 		}
-		line++;
-		p1 = p2 + 1;
-	}
-out_free_fbuf:
-	free(fbuf);
-out_close:
-	close(fd);
-	return nlines;
+		linenum++;
+	}
+
+	if (ferror(fp))
+		count = -errno;
+	free(line);
+	fclose(fp);
+
+	if (count == 0)
+		pr_debug("No matched lines found in %s.\n", fname);
+	return count;
 }
 
 /* Find probe points from lazy pattern  */
@@ -1154,10 +1141,7 @@ static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
 		/* Matching lazy line pattern */
 		ret = find_lazy_match_lines(&pf->lcache, pf->fname,
 					    pf->pev->point.lazy_line);
-		if (ret == 0) {
-			pr_debug("No matched lines found in %s.\n", pf->fname);
-			return 0;
-		} else if (ret < 0)
+		if (ret <= 0)
 			return ret;
 	}
 
-- 
1.7.3.2

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf-probe: rewrite find_lazy_match_lines() by using getline(3)
  2011-01-13 10:18 [PATCH] perf-probe: rewrite find_lazy_match_lines() by using getline(3) Franck Bui-Huu
@ 2011-01-13 10:25 ` Masami Hiramatsu
  2011-02-07  9:32   ` Masami Hiramatsu
  2011-02-08 15:16 ` [tip:perf/core] perf probe: Rewrite " tip-bot for Franck Bui-Huu
  1 sibling, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2011-01-13 10:25 UTC (permalink / raw)
  To: Franck Bui-Huu; +Cc: Arnaldo Carvalho de Melo, lkml, 2nddept-manager

(2011/01/13 19:18), Franck Bui-Huu wrote:
> From: Franck Bui-Huu <fbuihuu@gmail.com>
> 
> Signed-off-by: Franck Bui-Huu <fbuihuu@gmail.com>

Looks very nice! :)

Acked-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>

Thanks!

> ---
>  tools/perf/util/probe-finder.c |   70 +++++++++++++++------------------------
>  1 files changed, 27 insertions(+), 43 deletions(-)
> 
> diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
> index ddf4d45..4221738 100644
> --- a/tools/perf/util/probe-finder.c
> +++ b/tools/perf/util/probe-finder.c
> @@ -1092,51 +1092,38 @@ static int find_probe_point_by_line(struct probe_finder *pf)
>  static int find_lazy_match_lines(struct list_head *head,
>  				 const char *fname, const char *pat)
>  {
> -	char *fbuf, *p1, *p2;
> -	int fd, line, nlines = -1;
> -	struct stat st;
> -
> -	fd = open(fname, O_RDONLY);
> -	if (fd < 0) {
> -		pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
> +	FILE *fp;
> +	char *line = NULL;
> +	size_t line_len;
> +	ssize_t len;
> +	int count = 0, linenum = 1;
> +
> +	fp = fopen(fname, "r");
> +	if (!fp) {
> +		pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
>  		return -errno;
>  	}
>  
> -	if (fstat(fd, &st) < 0) {
> -		pr_warning("Failed to get the size of %s: %s\n",
> -			   fname, strerror(errno));
> -		nlines = -errno;
> -		goto out_close;
> -	}
> -
> -	nlines = -ENOMEM;
> -	fbuf = malloc(st.st_size + 2);
> -	if (fbuf == NULL)
> -		goto out_close;
> -	if (read(fd, fbuf, st.st_size) < 0) {
> -		pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
> -		nlines = -errno;
> -		goto out_free_fbuf;
> -	}
> -	fbuf[st.st_size] = '\n';	/* Dummy line */
> -	fbuf[st.st_size + 1] = '\0';
> -	p1 = fbuf;
> -	line = 1;
> -	nlines = 0;
> -	while ((p2 = strchr(p1, '\n')) != NULL) {
> -		*p2 = '\0';
> -		if (strlazymatch(p1, pat)) {
> -			line_list__add_line(head, line);
> -			nlines++;
> +	while ((len = getline(&line, &line_len, fp)) > 0) {
> +
> +		if (line[len - 1] == '\n')
> +			line[len - 1] = '\0';
> +
> +		if (strlazymatch(line, pat)) {
> +			line_list__add_line(head, linenum);
> +			count++;
>  		}
> -		line++;
> -		p1 = p2 + 1;
> -	}
> -out_free_fbuf:
> -	free(fbuf);
> -out_close:
> -	close(fd);
> -	return nlines;
> +		linenum++;
> +	}
> +
> +	if (ferror(fp))
> +		count = -errno;
> +	free(line);
> +	fclose(fp);
> +
> +	if (count == 0)
> +		pr_debug("No matched lines found in %s.\n", fname);
> +	return count;
>  }
>  
>  /* Find probe points from lazy pattern  */
> @@ -1154,10 +1141,7 @@ static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
>  		/* Matching lazy line pattern */
>  		ret = find_lazy_match_lines(&pf->lcache, pf->fname,
>  					    pf->pev->point.lazy_line);
> -		if (ret == 0) {
> -			pr_debug("No matched lines found in %s.\n", pf->fname);
> -			return 0;
> -		} else if (ret < 0)
> +		if (ret <= 0)
>  			return ret;
>  	}
>  


-- 
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf-probe: rewrite find_lazy_match_lines() by using getline(3)
  2011-01-13 10:25 ` Masami Hiramatsu
@ 2011-02-07  9:32   ` Masami Hiramatsu
  2011-02-07 12:31     ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2011-02-07  9:32 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: Franck Bui-Huu, lkml, 2nddept-manager

Hi Arnaldo,

Could you merge this patch into your perf/core tree?
This makes code very simple and easy to read :-)

Thank you,

(2011/01/13 19:25), Masami Hiramatsu wrote:
> (2011/01/13 19:18), Franck Bui-Huu wrote:
>> From: Franck Bui-Huu <fbuihuu@gmail.com>
>>
>> Signed-off-by: Franck Bui-Huu <fbuihuu@gmail.com>
> 
> Looks very nice! :)
> 
> Acked-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
> 
> Thanks!
> 
>> ---
>>  tools/perf/util/probe-finder.c |   70 +++++++++++++++------------------------
>>  1 files changed, 27 insertions(+), 43 deletions(-)
>>
>> diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
>> index ddf4d45..4221738 100644
>> --- a/tools/perf/util/probe-finder.c
>> +++ b/tools/perf/util/probe-finder.c
>> @@ -1092,51 +1092,38 @@ static int find_probe_point_by_line(struct probe_finder *pf)
>>  static int find_lazy_match_lines(struct list_head *head,
>>  				 const char *fname, const char *pat)
>>  {
>> -	char *fbuf, *p1, *p2;
>> -	int fd, line, nlines = -1;
>> -	struct stat st;
>> -
>> -	fd = open(fname, O_RDONLY);
>> -	if (fd < 0) {
>> -		pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
>> +	FILE *fp;
>> +	char *line = NULL;
>> +	size_t line_len;
>> +	ssize_t len;
>> +	int count = 0, linenum = 1;
>> +
>> +	fp = fopen(fname, "r");
>> +	if (!fp) {
>> +		pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
>>  		return -errno;
>>  	}
>>  
>> -	if (fstat(fd, &st) < 0) {
>> -		pr_warning("Failed to get the size of %s: %s\n",
>> -			   fname, strerror(errno));
>> -		nlines = -errno;
>> -		goto out_close;
>> -	}
>> -
>> -	nlines = -ENOMEM;
>> -	fbuf = malloc(st.st_size + 2);
>> -	if (fbuf == NULL)
>> -		goto out_close;
>> -	if (read(fd, fbuf, st.st_size) < 0) {
>> -		pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
>> -		nlines = -errno;
>> -		goto out_free_fbuf;
>> -	}
>> -	fbuf[st.st_size] = '\n';	/* Dummy line */
>> -	fbuf[st.st_size + 1] = '\0';
>> -	p1 = fbuf;
>> -	line = 1;
>> -	nlines = 0;
>> -	while ((p2 = strchr(p1, '\n')) != NULL) {
>> -		*p2 = '\0';
>> -		if (strlazymatch(p1, pat)) {
>> -			line_list__add_line(head, line);
>> -			nlines++;
>> +	while ((len = getline(&line, &line_len, fp)) > 0) {
>> +
>> +		if (line[len - 1] == '\n')
>> +			line[len - 1] = '\0';
>> +
>> +		if (strlazymatch(line, pat)) {
>> +			line_list__add_line(head, linenum);
>> +			count++;
>>  		}
>> -		line++;
>> -		p1 = p2 + 1;
>> -	}
>> -out_free_fbuf:
>> -	free(fbuf);
>> -out_close:
>> -	close(fd);
>> -	return nlines;
>> +		linenum++;
>> +	}
>> +
>> +	if (ferror(fp))
>> +		count = -errno;
>> +	free(line);
>> +	fclose(fp);
>> +
>> +	if (count == 0)
>> +		pr_debug("No matched lines found in %s.\n", fname);
>> +	return count;
>>  }
>>  
>>  /* Find probe points from lazy pattern  */
>> @@ -1154,10 +1141,7 @@ static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
>>  		/* Matching lazy line pattern */
>>  		ret = find_lazy_match_lines(&pf->lcache, pf->fname,
>>  					    pf->pev->point.lazy_line);
>> -		if (ret == 0) {
>> -			pr_debug("No matched lines found in %s.\n", pf->fname);
>> -			return 0;
>> -		} else if (ret < 0)
>> +		if (ret <= 0)
>>  			return ret;
>>  	}
>>  
> 
> 


-- 
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf-probe: rewrite find_lazy_match_lines() by using getline(3)
  2011-02-07  9:32   ` Masami Hiramatsu
@ 2011-02-07 12:31     ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 5+ messages in thread
From: Arnaldo Carvalho de Melo @ 2011-02-07 12:31 UTC (permalink / raw)
  To: Masami Hiramatsu; +Cc: Franck Bui-Huu, lkml, 2nddept-manager

Em Mon, Feb 07, 2011 at 06:32:27PM +0900, Masami Hiramatsu escreveu:
> Hi Arnaldo,
> 
> Could you merge this patch into your perf/core tree?
> This makes code very simple and easy to read :-)

Right, will process these and others you sent before returning to work
on the live TUI annotation in perf top :)

- Arnaldo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [tip:perf/core] perf probe: Rewrite find_lazy_match_lines() by using getline(3)
  2011-01-13 10:18 [PATCH] perf-probe: rewrite find_lazy_match_lines() by using getline(3) Franck Bui-Huu
  2011-01-13 10:25 ` Masami Hiramatsu
@ 2011-02-08 15:16 ` tip-bot for Franck Bui-Huu
  1 sibling, 0 replies; 5+ messages in thread
From: tip-bot for Franck Bui-Huu @ 2011-02-08 15:16 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: acme, linux-kernel, hpa, mingo, masami.hiramatsu.pt, fbuihuu, tglx

Commit-ID:  f50c2169bd054984e976e67e8651d28f3caf6ba3
Gitweb:     http://git.kernel.org/tip/f50c2169bd054984e976e67e8651d28f3caf6ba3
Author:     Franck Bui-Huu <fbuihuu@gmail.com>
AuthorDate: Thu, 13 Jan 2011 11:18:30 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Mon, 7 Feb 2011 09:12:42 -0200

perf probe: Rewrite find_lazy_match_lines() by using getline(3)

Acked-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: lkml <linux-kernel@vger.kernel.org>
LKML-Reference: <m3d3o185u1.fsf@gmail.com>
Signed-off-by: Franck Bui-Huu <fbuihuu@gmail.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/probe-finder.c |   70 +++++++++++++++------------------------
 1 files changed, 27 insertions(+), 43 deletions(-)

diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 69215bf..46addfb 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -1234,51 +1234,38 @@ static int find_probe_point_by_line(struct probe_finder *pf)
 static int find_lazy_match_lines(struct list_head *head,
 				 const char *fname, const char *pat)
 {
-	char *fbuf, *p1, *p2;
-	int fd, line, nlines = -1;
-	struct stat st;
-
-	fd = open(fname, O_RDONLY);
-	if (fd < 0) {
-		pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
+	FILE *fp;
+	char *line = NULL;
+	size_t line_len;
+	ssize_t len;
+	int count = 0, linenum = 1;
+
+	fp = fopen(fname, "r");
+	if (!fp) {
+		pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
 		return -errno;
 	}
 
-	if (fstat(fd, &st) < 0) {
-		pr_warning("Failed to get the size of %s: %s\n",
-			   fname, strerror(errno));
-		nlines = -errno;
-		goto out_close;
-	}
-
-	nlines = -ENOMEM;
-	fbuf = malloc(st.st_size + 2);
-	if (fbuf == NULL)
-		goto out_close;
-	if (read(fd, fbuf, st.st_size) < 0) {
-		pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
-		nlines = -errno;
-		goto out_free_fbuf;
-	}
-	fbuf[st.st_size] = '\n';	/* Dummy line */
-	fbuf[st.st_size + 1] = '\0';
-	p1 = fbuf;
-	line = 1;
-	nlines = 0;
-	while ((p2 = strchr(p1, '\n')) != NULL) {
-		*p2 = '\0';
-		if (strlazymatch(p1, pat)) {
-			line_list__add_line(head, line);
-			nlines++;
+	while ((len = getline(&line, &line_len, fp)) > 0) {
+
+		if (line[len - 1] == '\n')
+			line[len - 1] = '\0';
+
+		if (strlazymatch(line, pat)) {
+			line_list__add_line(head, linenum);
+			count++;
 		}
-		line++;
-		p1 = p2 + 1;
+		linenum++;
 	}
-out_free_fbuf:
-	free(fbuf);
-out_close:
-	close(fd);
-	return nlines;
+
+	if (ferror(fp))
+		count = -errno;
+	free(line);
+	fclose(fp);
+
+	if (count == 0)
+		pr_debug("No matched lines found in %s.\n", fname);
+	return count;
 }
 
 static int probe_point_lazy_walker(const char *fname, int lineno,
@@ -1312,10 +1299,7 @@ static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
 		/* Matching lazy line pattern */
 		ret = find_lazy_match_lines(&pf->lcache, pf->fname,
 					    pf->pev->point.lazy_line);
-		if (ret == 0) {
-			pr_debug("No matched lines found in %s.\n", pf->fname);
-			return 0;
-		} else if (ret < 0)
+		if (ret <= 0)
 			return ret;
 	}
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-02-08 15:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-13 10:18 [PATCH] perf-probe: rewrite find_lazy_match_lines() by using getline(3) Franck Bui-Huu
2011-01-13 10:25 ` Masami Hiramatsu
2011-02-07  9:32   ` Masami Hiramatsu
2011-02-07 12:31     ` Arnaldo Carvalho de Melo
2011-02-08 15:16 ` [tip:perf/core] perf probe: Rewrite " tip-bot for Franck Bui-Huu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).