linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Jin Yao <yao.jin@linux.intel.com>
Cc: jolsa@kernel.org, peterz@infradead.org, mingo@redhat.com,
	alexander.shishkin@linux.intel.com, Linux-kernel@vger.kernel.org,
	ak@linux.intel.com, kan.liang@intel.com, yao.jin@intel.com
Subject: Re: [PATCH v7 3/6] perf util: Create function to parse time percent
Date: Mon, 8 Jan 2018 11:31:49 -0300	[thread overview]
Message-ID: <20180108143149.GB25476@kernel.org> (raw)
In-Reply-To: <1512738826-2628-4-git-send-email-yao.jin@linux.intel.com>

Em Fri, Dec 08, 2017 at 09:13:43PM +0800, Jin Yao escreveu:
> Current perf report/script/... have a --time option to limit the time
> range of output. But right now it only supports absolute time.
> 
> For easy using, now it can support a percent of time usage.
> 
> For example:
> 
> 1. Select the second 10% time slice
>    perf report --time 10%/2

After applying this patch I'm not being able to get any of these
examples to work:

[root@jouet home]# perf report --header | grep "time of"
# time of first sample : 22947.909226
# time of last sample : 22948.910704
[root@jouet home]# 

Then, when I try the first example:

[root@jouet home]# perf report --stdio --time 1%-20%
Invalid time string
# To display the perf.data header info, please use --header/--header-only options.
#
[root@jouet home]#

What am I doing wrong?
 
> 2. Select from 0% to 10% time slice
>    perf report --time 0%-10%
> 
> It also support the multiple time ranges.
> 
> 3. Select the first and second 10% time slices
>    perf report --time 10%/1,10%/2
> 
> 4. Select from 0% to 10% and 30% to 40% slices
>    perf report --time 0%-10%,30%-40%
> 
> Change log:
> -----------
> v4: An issue is found. Following passes.
>     perf script --time 10%/10x12321xsdfdasfdsafdsafdsa
> 
>     Now it uses strtol to replace atoi.
> 
> Signed-off-by: Jin Yao <yao.jin@linux.intel.com>
> ---
>  tools/perf/util/time-utils.c | 205 ++++++++++++++++++++++++++++++++++++++++---
>  tools/perf/util/time-utils.h |   3 +
>  2 files changed, 196 insertions(+), 12 deletions(-)
> 
> diff --git a/tools/perf/util/time-utils.c b/tools/perf/util/time-utils.c
> index 81927d0..61c46022 100644
> --- a/tools/perf/util/time-utils.c
> +++ b/tools/perf/util/time-utils.c
> @@ -6,6 +6,7 @@
>  #include <time.h>
>  #include <errno.h>
>  #include <inttypes.h>
> +#include <math.h>
>  
>  #include "perf.h"
>  #include "debug.h"
> @@ -60,11 +61,10 @@ static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
>  	return 0;
>  }
>  
> -int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
> +static int split_start_end(char **start, char **end, const char *ostr, char ch)
>  {
>  	char *start_str, *end_str;
>  	char *d, *str;
> -	int rc = 0;
>  
>  	if (ostr == NULL || *ostr == '\0')
>  		return 0;
> @@ -74,25 +74,35 @@ int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
>  	if (str == NULL)
>  		return -ENOMEM;
>  
> -	ptime->start = 0;
> -	ptime->end = 0;
> -
> -	/* str has the format: <start>,<stop>
> -	 * variations: <start>,
> -	 *             ,<stop>
> -	 *             ,
> -	 */
>  	start_str = str;
> -	d = strchr(start_str, ',');
> +	d = strchr(start_str, ch);
>  	if (d) {
>  		*d = '\0';
>  		++d;
>  	}
>  	end_str = d;
>  
> +	*start = start_str;
> +	*end = end_str;
> +
> +	return 0;
> +}
> +
> +int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
> +{
> +	char *start_str = NULL, *end_str;
> +	int rc;
> +
> +	rc = split_start_end(&start_str, &end_str, ostr, ',');
> +	if (rc || !start_str)
> +		return rc;
> +
> +	ptime->start = 0;
> +	ptime->end = 0;
> +
>  	rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
>  
> -	free(str);
> +	free(start_str);
>  
>  	/* make sure end time is after start time if it was given */
>  	if (rc == 0 && ptime->end && ptime->end < ptime->start)
> @@ -104,6 +114,177 @@ int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
>  	return rc;
>  }
>  
> +static int parse_percent(double *pcnt, char *str)
> +{
> +	char *c;
> +
> +	c = strchr(str, '%');
> +	if (c)
> +		*c = '\0';
> +	else
> +		return -1;
> +
> +	*pcnt = atof(str) / 100.0;
> +
> +	return 0;
> +}
> +
> +static int percent_slash_split(char *str, struct perf_time_interval *ptime,
> +			       u64 start, u64 end)
> +{
> +	char *p, *end_str;
> +	double pcnt, start_pcnt, end_pcnt;
> +	u64 total = end - start;
> +	int i;
> +
> +	/*
> +	 * Example:
> +	 * 10%/2: select the second 10% slice and the third 10% slice
> +	 */
> +
> +	/* We can modify this string since the original one is copied */
> +	p = strchr(str, '/');
> +	if (!p)
> +		return -1;
> +
> +	*p = '\0';
> +	if (parse_percent(&pcnt, str) < 0)
> +		return -1;
> +
> +	p++;
> +	i = (int)strtol(p, &end_str, 10);
> +	if (*end_str)
> +		return -1;
> +
> +	if (pcnt <= 0.0)
> +		return -1;
> +
> +	start_pcnt = pcnt * (i - 1);
> +	end_pcnt = pcnt * i;
> +
> +	if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
> +	    end_pcnt < 0.0 || end_pcnt > 1.0) {
> +		return -1;
> +	}
> +
> +	ptime->start = start + round(start_pcnt * total);
> +	ptime->end = start + round(end_pcnt * total);
> +
> +	return 0;
> +}
> +
> +static int percent_dash_split(char *str, struct perf_time_interval *ptime,
> +			      u64 start, u64 end)
> +{
> +	char *start_str = NULL, *end_str;
> +	double start_pcnt, end_pcnt;
> +	u64 total = end - start;
> +	int ret;
> +
> +	/*
> +	 * Example: 0%-10%
> +	 */
> +
> +	ret = split_start_end(&start_str, &end_str, str, '-');
> +	if (ret || !start_str)
> +		return ret;
> +
> +	if ((parse_percent(&start_pcnt, start_str) != 0) ||
> +	    (parse_percent(&end_pcnt, end_str) != 0)) {
> +		free(start_str);
> +		return -1;
> +	}
> +
> +	free(start_str);
> +
> +	if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
> +	    end_pcnt < 0.0 || end_pcnt > 1.0 ||
> +	    start_pcnt > end_pcnt) {
> +		return -1;
> +	}
> +
> +	ptime->start = start + round(start_pcnt * total);
> +	ptime->end = start + round(end_pcnt * total);
> +
> +	return 0;
> +}
> +
> +typedef int (*time_pecent_split)(char *, struct perf_time_interval *,
> +				 u64 start, u64 end);
> +
> +static int percent_comma_split(struct perf_time_interval *ptime_buf, int num,
> +			       const char *ostr, u64 start, u64 end,
> +			       time_pecent_split func)
> +{
> +	char *str, *p1, *p2;
> +	int len, ret, i = 0;
> +
> +	str = strdup(ostr);
> +	if (str == NULL)
> +		return -ENOMEM;
> +
> +	len = strlen(str);
> +	p1 = str;
> +
> +	while (p1 < str + len) {
> +		if (i >= num) {
> +			free(str);
> +			return -1;
> +		}
> +
> +		p2 = strchr(p1, ',');
> +		if (p2)
> +			*p2 = '\0';
> +
> +		ret = (func)(p1, &ptime_buf[i], start, end);
> +		if (ret < 0) {
> +			free(str);
> +			return -1;
> +		}
> +
> +		pr_debug("start time %d: %" PRIu64 ", ", i, ptime_buf[i].start);
> +		pr_debug("end time %d: %" PRIu64 "\n", i, ptime_buf[i].end);
> +
> +		i++;
> +
> +		if (p2)
> +			p1 = p2 + 1;
> +		else
> +			break;
> +	}
> +
> +	free(str);
> +	return i;
> +}
> +
> +int perf_time__percent_parse_str(struct perf_time_interval *ptime_buf, int num,
> +				 const char *ostr, u64 start, u64 end)
> +{
> +	char *c;
> +
> +	/*
> +	 * ostr example:
> +	 * 10%/2,10%/3: select the second 10% slice and the third 10% slice
> +	 * 0%-10%,30%-40%: multiple time range
> +	 */
> +
> +	memset(ptime_buf, 0, sizeof(*ptime_buf) * num);
> +
> +	c = strchr(ostr, '/');
> +	if (c) {
> +		return percent_comma_split(ptime_buf, num, ostr, start,
> +					   end, percent_slash_split);
> +	}
> +
> +	c = strchr(ostr, '-');
> +	if (c) {
> +		return percent_comma_split(ptime_buf, num, ostr, start,
> +					   end, percent_dash_split);
> +	}
> +
> +	return -1;
> +}
> +
>  bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
>  {
>  	/* if time is not set don't drop sample */
> diff --git a/tools/perf/util/time-utils.h b/tools/perf/util/time-utils.h
> index 15b475c..2308723 100644
> --- a/tools/perf/util/time-utils.h
> +++ b/tools/perf/util/time-utils.h
> @@ -13,6 +13,9 @@ int parse_nsec_time(const char *str, u64 *ptime);
>  
>  int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr);
>  
> +int perf_time__percent_parse_str(struct perf_time_interval *ptime_buf, int num,
> +				 const char *ostr, u64 start, u64 end);
> +
>  bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp);
>  
>  int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
> -- 
> 2.7.4

  reply	other threads:[~2018-01-08 14:31 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-08 13:13 [PATCH v7 0/6] perf report/script: Support percent and multiple range in --time option Jin Yao
2017-12-08 13:13 ` [PATCH v7 1/6] perf header: Record first sample time and last sample time in perf file header Jin Yao
2018-01-11  6:21   ` [tip:perf/core] perf header: Add infrastructure to record first and last sample time tip-bot for Jin Yao
2017-12-08 13:13 ` [PATCH v7 2/6] perf record: Get the first sample time " Jin Yao
2018-01-04 19:09   ` Arnaldo Carvalho de Melo
2018-01-05  1:15     ` Jin, Yao
2018-01-05 12:53       ` Arnaldo Carvalho de Melo
2018-01-05 22:55         ` Jin, Yao
2018-01-11  6:22   ` [tip:perf/core] perf record: Record the first and last sample time in the header tip-bot for Jin Yao
2017-12-08 13:13 ` [PATCH v7 3/6] perf util: Create function to parse time percent Jin Yao
2018-01-08 14:31   ` Arnaldo Carvalho de Melo [this message]
2018-01-08 14:38     ` Arnaldo Carvalho de Melo
2018-01-09  1:18       ` Jin, Yao
2018-01-11  6:22   ` [tip:perf/core] perf tools: " tip-bot for Jin Yao
2017-12-08 13:13 ` [PATCH v7 4/6] perf util: Create function to perform multiple time range checking Jin Yao
2018-01-11  6:22   ` [tip:perf/core] perf tools: " tip-bot for Jin Yao
2017-12-08 13:13 ` [PATCH v7 5/6] perf report: support time percent and multiple time ranges Jin Yao
2018-01-08 14:45   ` Arnaldo Carvalho de Melo
2018-01-08 14:48     ` Arnaldo Carvalho de Melo
2018-01-08 14:53   ` Arnaldo Carvalho de Melo
2018-01-08 15:02     ` Arnaldo Carvalho de Melo
2018-01-08 15:04       ` Arnaldo Carvalho de Melo
2018-01-09  2:02         ` Jin, Yao
2018-01-11  6:23   ` [tip:perf/core] perf report: Support " tip-bot for Jin Yao
2017-12-08 13:13 ` [PATCH v7 6/6] perf script: support " Jin Yao
2018-01-11  6:23   ` [tip:perf/core] perf script: Support " tip-bot for Jin Yao
2017-12-13 14:28 ` [PATCH v7 0/6] perf report/script: Support percent and multiple range in --time option Arnaldo Carvalho de Melo
2017-12-14  0:48   ` Jin, Yao
2017-12-14 14:08 ` Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180108143149.GB25476@kernel.org \
    --to=acme@kernel.org \
    --cc=Linux-kernel@vger.kernel.org \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@intel.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=yao.jin@intel.com \
    --cc=yao.jin@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).