Skip to contentSkip to conversation

perf util: Add parse_nsec_time() function

PATCH Superseded Namhyung Kim 3 participants 11 messages Jun 3, 2013 – Aug 12, 2013 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 1/4 perf util: Add parse_nsec_time() function

From: Namhyung Kim <[email protected]>

The parse_nsec_time() function is for parsing a string of time into 64-bit nsec value. It's a preparation of time filtering in some of perf commands.

---
 tools/perf/util/util.c | 30 ++++++++++++++++++++++++++++++
 tools/perf/util/util.h |  2 ++
 2 files changed, 32 insertions(+)
tools/perf/util/util.c +30 -0
index 59d868add275..4a207c40d7bc 100644
@@ -269,3 +269,33 @@ void perf_debugfs_set_path(const char *mntpt)
 	snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
 	set_tracing_events_path(mntpt);
 }
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+	u64 time_sec, time_nsec;
+	char *end;
+
+	time_sec = strtoul(str, &end, 10);
+	if (*end != '.' && *end != '\0')
+		return -1;
+
+	if (*end == '.') {
+		int i;
+		char nsec_buf[10];
+
+		strncpy(nsec_buf, end + 1, 9);
+		nsec_buf[9] = '\0';
+
+		/* make it nsec precision */
+		for (i = strlen(nsec_buf); i < 9; i++)
+			nsec_buf[i] = '0';
+
+		time_nsec = strtoul(nsec_buf, &end, 10);
+		if (*end != '\0')
+			return -1;
+	} else
+		time_nsec = 0;
+
+	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
+	return 0;
+}
tools/perf/util/util.h +2 -0
index 7a484c97e500..6c8baf312c8a 100644
@@ -204,6 +204,8 @@ static inline int has_extension(const char *filename, const char *ext)
 #define NSEC_PER_MSEC	1000000L
 #endif
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
 extern unsigned char sane_ctype[256];
 #define GIT_SPACE		0x01
 #define GIT_DIGIT		0x02
Signature · 1 line
1.7.11.7
author maintainer
#1 lore raw

PATCH v2 2/4 perf script: Add --time-filter option

From: Namhyung Kim <[email protected]>

The --time-filter option is for limiting samples within a range of time. A time range looks like <time1>-<time2> and at most one of them can be omitted. For instance:

  $ perf script --time-filter -2178446.12
  ...
         xchat  1772 [002] 2178446.070330: sched:sched_switch: prev_comm=xchat prev_pid=177
       swapper     0 [002] 2178446.070338: power:cpu_idle: state=4 cpu_id=2
       swapper     0 [001] 2178446.091952: sched:sched_wakeup: comm=synergys pid=1488 prio=
       swapper     0 [001] 2178446.091958: power:cpu_idle: state=4294967295 cpu_id=1
       swapper     0 [001] 2178446.091970: sched:sched_switch: prev_comm=swapper/1 prev_pid
      synergys  1488 [001] 2178446.091995: sched:sched_switch: prev_comm=synergys prev_pid=
       swapper     0 [001] 2178446.092003: power:cpu_idle: state=4 cpu_id=1
       swapper     0 [001] 2178446.116997: sched:sched_wakeup: comm=synergys pid=1488 prio=
       swapper     0 [001] 2178446.117004: power:cpu_idle: state=4294967295 cpu_id=1
       swapper     0 [001] 2178446.117016: sched:sched_switch: prev_comm=swapper/1 prev_pid
      synergys  1488 [001] 2178446.117040: sched:sched_switch: prev_comm=synergys prev_pid=
       swapper     0 [001] 2178446.117048: power:cpu_idle: state=4 cpu_id=1

Above example only displays samples which have a timestamp before 2178446.120000.

Cc Joonsoo Kim <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/Documentation/perf-script.txt |  7 +++++++
 tools/perf/builtin-script.c              | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
tools/perf/Documentation/perf-script.txt +7 -0
index e9cbfcddfa3f..c4994c5f27ff 100644
@@ -203,6 +203,13 @@ OPTIONS
 --show-kernel-path::
 	Try to resolve the path of [kernel.kallsyms]
 
+-X::
+--time-filter::
+	Display samples within a range of time only. A time range can be given
+	like 'time1-time2' and treated as a start time and end time
+        respectively. The time format is like "<sec>.<usec>". Either of time1
+	or time2 can be omitted.
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-script-perl[1],
tools/perf/builtin-script.c +32 -0
index 92d4658f56fb..d598765e59cb 100644
@@ -28,6 +28,13 @@ static bool			system_wide;
 static const char		*cpu_list;
 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
 
+struct time_range {
+	u64 start;
+	u64 end;
+};
+
+static struct time_range trange;
+
 enum perf_output_field {
 	PERF_OUTPUT_COMM            = 1U << 0,
 	PERF_OUTPUT_TID             = 1U << 1,
@@ -510,6 +517,12 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
 	if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
 		return 0;
 
+	if (trange.start && trange.start > sample->time)
+		return 0;
+
+	if (trange.end && trange.end < sample->time)
+		return 0;
+
 	scripting_ops->process_event(event, sample, evsel, machine, &al);
 
 	evsel->hists.stats.total_period += sample->period;
@@ -1236,6 +1249,23 @@ static int have_cmd(int argc, const char **argv)
 	return 0;
 }
 
+static int
+parse_time_filter(const struct option *opt, const char *str,
+		  int unset __maybe_unused)
+{
+	struct time_range *time_range = opt->value;
+	char *sep = strchr(str, '-');
+
+	if (sep == NULL)
+		return parse_nsec_time(str, &time_range->start);
+	else if (sep == str)
+		return parse_nsec_time(++str, &time_range->end);
+
+	*sep++ = '\0';
+	return parse_nsec_time(str, &time_range->start) ||
+		parse_nsec_time(sep, &time_range->end);
+}
+
 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	bool show_full_info = false;
@@ -1286,6 +1316,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 		    "display extended information from perf.data file"),
 	OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
 		    "Show the path of [kernel.kallsyms]"),
+	OPT_CALLBACK('X', "time-filter", &trange, "time[-time]",
+		     "Only display entries in the time range", parse_time_filter),
 	OPT_END()
 	};
 	const char * const script_usage[] = {
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [PATCH v2 2/4] perf script: Add --time-filter option

Quoted 21 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

The --time-filter option is for limiting samples within a range of time. A time range looks like <time1>-<time2> and at most one of them can be omitted. For instance:

   $ perf script --time-filter -2178446.12
   ...
          xchat  1772 [002] 2178446.070330: sched:sched_switch: prev_comm=xchat prev_pid=177
        swapper     0 [002] 2178446.070338: power:cpu_idle: state=4 cpu_id=2
        swapper     0 [001] 2178446.091952: sched:sched_wakeup: comm=synergys pid=1488 prio=
        swapper     0 [001] 2178446.091958: power:cpu_idle: state=4294967295 cpu_id=1
        swapper     0 [001] 2178446.091970: sched:sched_switch: prev_comm=swapper/1 prev_pid
       synergys  1488 [001] 2178446.091995: sched:sched_switch: prev_comm=synergys prev_pid=
        swapper     0 [001] 2178446.092003: power:cpu_idle: state=4 cpu_id=1
        swapper     0 [001] 2178446.116997: sched:sched_wakeup: comm=synergys pid=1488 prio=
        swapper     0 [001] 2178446.117004: power:cpu_idle: state=4294967295 cpu_id=1
        swapper     0 [001] 2178446.117016: sched:sched_switch: prev_comm=swapper/1 prev_pid
       synergys  1488 [001] 2178446.117040: sched:sched_switch: prev_comm=synergys prev_pid=
        swapper     0 [001] 2178446.117048: power:cpu_idle: state=4 cpu_id=1
Quoted 6 lines

Above example only displays samples which have a timestamp before 2178446.120000.

Cc Joonsoo Kim <[email protected]>
Cc David Ahern <[email protected]>
Quoted 88 lines
---
  tools/perf/Documentation/perf-script.txt |  7 +++++++
  tools/perf/builtin-script.c              | 32 ++++++++++++++++++++++++++++++++
  2 files changed, 39 insertions(+)
tools/perf/Documentation/perf-script.txt +7 -0
index e9cbfcddfa3f..c4994c5f27ff 100644
@@ -203,6 +203,13 @@ OPTIONS
  --show-kernel-path::
  	Try to resolve the path of [kernel.kallsyms]
 
+-X::
+--time-filter::
+	Display samples within a range of time only. A time range can be given
+	like 'time1-time2' and treated as a start time and end time
+        respectively. The time format is like "<sec>.<usec>". Either of time1
+	or time2 can be omitted.
+
  SEE ALSO
  --------
  linkperf:perf-record[1], linkperf:perf-script-perl[1],
tools/perf/builtin-script.c +32 -0
index 92d4658f56fb..d598765e59cb 100644
@@ -28,6 +28,13 @@ static bool			system_wide;
  static const char		*cpu_list;
  static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
 
+struct time_range {
+	u64 start;
+	u64 end;
+};
+
+static struct time_range trange;
+
  enum perf_output_field {
  	PERF_OUTPUT_COMM            = 1U << 0,
  	PERF_OUTPUT_TID             = 1U << 1,
@@ -510,6 +517,12 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
  	if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  		return 0;
 
+	if (trange.start && trange.start > sample->time)
+		return 0;
+
+	if (trange.end && trange.end < sample->time)
+		return 0;
+
  	scripting_ops->process_event(event, sample, evsel, machine, &al);
 
  	evsel->hists.stats.total_period += sample->period;
@@ -1236,6 +1249,23 @@ static int have_cmd(int argc, const char **argv)
  	return 0;
  }
 
+static int
+parse_time_filter(const struct option *opt, const char *str,
+		  int unset __maybe_unused)
+{
+	struct time_range *time_range = opt->value;
+	char *sep = strchr(str, '-');
+
+	if (sep == NULL)
+		return parse_nsec_time(str, &time_range->start);
+	else if (sep == str)
+		return parse_nsec_time(++str, &time_range->end);
+
+	*sep++ = '\0';
+	return parse_nsec_time(str, &time_range->start) ||
+		parse_nsec_time(sep, &time_range->end);
+}
+
  int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  {
  	bool show_full_info = false;
@@ -1286,6 +1316,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  		    "display extended information from perf.data file"),
  	OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  		    "Show the path of [kernel.kallsyms]"),
+	OPT_CALLBACK('X', "time-filter", &trange, "time[-time]",
+		     "Only display entries in the time range", parse_time_filter),
  	OPT_END()
  	};
  	const char * const script_usage[] = {
author maintainer
#3 lore raw

PATCH v2 3/4 perf report: Add --time-filter option

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

The --time-filter option is for limiting samples within a range of time. A time range looks like <time1>-<time2> and at most one of them can be omitted. This can be useful when analyzing a part of a huge data only.

Cc Joonsoo Kim <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/Documentation/perf-report.txt |  7 +++++++
 tools/perf/builtin-report.c              | 27 +++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
tools/perf/Documentation/perf-report.txt +7 -0
index 66dab7410c1d..04a96f657ca7 100644
@@ -214,6 +214,13 @@ OPTIONS
 	Do not show entries which have an overhead under that percent.
 	(Default: 0).
 
+-X::
+--time-filter::
+	Report samples within a range of time only. A time range can be given
+	like 'time1-time2' and treated as a start time and end time
+	respectively. The time format is like "<sec>.<usec>". Either of time1
+	or time2 can be omitted.
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-annotate[1]
tools/perf/builtin-report.c +27 -0
index ca98d34cd58b..e09e1bdb1401 100644
@@ -53,6 +53,7 @@ struct perf_report {
 	const char		*cpu_list;
 	const char		*symbol_filter_str;
 	float			min_percent;
+	u64			time_start, time_end;
 	DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
 };
 
@@ -318,6 +319,12 @@ static int process_sample_event(struct perf_tool *tool,
 	if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
 		return 0;
 
+	if (rep->time_start && rep->time_start > sample->time)
+		return 0;
+
+	if (rep->time_end && rep->time_end < sample->time)
+		return 0;
+
 	if (sort__mode == SORT_MODE__BRANCH) {
 		ret = perf_report__add_branch_hist_entry(tool, &al, sample,
 							 evsel, machine);
@@ -714,6 +721,24 @@ parse_percent_limit(const struct option *opt, const char *str,
 	return 0;
 }
 
+static int
+parse_time_filter(const struct option *opt, const char *str,
+		  int unset __maybe_unused)
+{
+	struct perf_report *rep = opt->value;
+	char *sep;
+
+	sep = strchr(str, '-');
+	if (sep == NULL)
+		return parse_nsec_time(str, &rep->time_start);
+	else if (sep == str)
+		return parse_nsec_time(++str, &rep->time_end);
+
+	*sep++ = '\0';
+	return parse_nsec_time(str, &rep->time_start) ||
+		parse_nsec_time(sep , &rep->time_end);
+}
+
 int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	struct perf_session *session;
@@ -825,6 +850,8 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 	OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
 	OPT_CALLBACK(0, "percent-limit", &report, "percent",
 		     "Don't show entries under that percent", parse_percent_limit),
+	OPT_CALLBACK('X', "time-filter", &report, "time[-time]",
+		     "Only display entries in the time range", parse_time_filter),
 	OPT_END()
 	};
 
Signature · 1 line
1.7.11.7
#4 lore raw

Re: [PATCH v2 3/4] perf report: Add --time-filter option

Quoted 10 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

The --time-filter option is for limiting samples within a range of time. A time range looks like <time1>-<time2> and at most one of them can be omitted. This can be useful when analyzing a part of a huge data only.

Cc Joonsoo Kim <[email protected]>
Cc David Ahern <[email protected]>
Quoted 83 lines
---
  tools/perf/Documentation/perf-report.txt |  7 +++++++
  tools/perf/builtin-report.c              | 27 +++++++++++++++++++++++++++
  2 files changed, 34 insertions(+)
tools/perf/Documentation/perf-report.txt +7 -0
index 66dab7410c1d..04a96f657ca7 100644
@@ -214,6 +214,13 @@ OPTIONS
  	Do not show entries which have an overhead under that percent.
  	(Default: 0).
 
+-X::
+--time-filter::
+	Report samples within a range of time only. A time range can be given
+	like 'time1-time2' and treated as a start time and end time
+	respectively. The time format is like "<sec>.<usec>". Either of time1
+	or time2 can be omitted.
+
  SEE ALSO
  --------
  linkperf:perf-stat[1], linkperf:perf-annotate[1]
tools/perf/builtin-report.c +27 -0
index ca98d34cd58b..e09e1bdb1401 100644
@@ -53,6 +53,7 @@ struct perf_report {
  	const char		*cpu_list;
  	const char		*symbol_filter_str;
  	float			min_percent;
+	u64			time_start, time_end;
  	DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  };
 
@@ -318,6 +319,12 @@ static int process_sample_event(struct perf_tool *tool,
  	if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
  		return 0;
 
+	if (rep->time_start && rep->time_start > sample->time)
+		return 0;
+
+	if (rep->time_end && rep->time_end < sample->time)
+		return 0;
+
  	if (sort__mode == SORT_MODE__BRANCH) {
  		ret = perf_report__add_branch_hist_entry(tool, &al, sample,
  							 evsel, machine);
@@ -714,6 +721,24 @@ parse_percent_limit(const struct option *opt, const char *str,
  	return 0;
  }
 
+static int
+parse_time_filter(const struct option *opt, const char *str,
+		  int unset __maybe_unused)
+{
+	struct perf_report *rep = opt->value;
+	char *sep;
+
+	sep = strchr(str, '-');
+	if (sep == NULL)
+		return parse_nsec_time(str, &rep->time_start);
+	else if (sep == str)
+		return parse_nsec_time(++str, &rep->time_end);
+
+	*sep++ = '\0';
+	return parse_nsec_time(str, &rep->time_start) ||
+		parse_nsec_time(sep , &rep->time_end);
+}
+
  int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  {
  	struct perf_session *session;
@@ -825,6 +850,8 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  	OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  	OPT_CALLBACK(0, "percent-limit", &report, "percent",
  		     "Don't show entries under that percent", parse_percent_limit),
+	OPT_CALLBACK('X', "time-filter", &report, "time[-time]",
+		     "Only display entries in the time range", parse_time_filter),
  	OPT_END()
  	};
 
author maintainer
#5 lore raw

PATCH v2 4/4 perf inject: Add --time-filter option

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

The --time-filter option is for limiting samples within a range of time. A time range looks like <time1>-<time2> and at most one of them can be omitted. This can be useful when analyzing a part of a huge data only.

It's unclear how to specify a time range when used with a pipe. It seems that we need to support relative time too.

Cc David Ahern <[email protected]>
Cc Andi Kleen <[email protected]>
---
 tools/perf/Documentation/perf-inject.txt |  7 +++++++
 tools/perf/builtin-inject.c              | 35 ++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
tools/perf/Documentation/perf-inject.txt +7 -0
index a00a34276c54..5bc474369dfb 100644
@@ -41,6 +41,13 @@ OPTIONS
 	tasks slept. sched_switch contains a callchain where a task slept and
 	sched_stat contains a timeslice how long a task slept.
 
+-X::
+--time-filter::
+	Display samples within a range of time only. A time range can be given
+	like 'time1-time2' and treated as a start time and an end time
+	respectively. The time format is like "<sec>.<usec>". Either of time1
+	or time2 can be omitted.
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-report[1], linkperf:perf-archive[1]
tools/perf/builtin-inject.c +35 -0
index 84ad6abe4258..4fcd75508b24 100644
@@ -28,6 +28,7 @@ struct perf_inject {
 	int		 pipe_output,
 			 output;
 	u64		 bytes_written;
+	u64		 time_start, time_end;
 	struct list_head samples;
 };
 
@@ -112,6 +113,14 @@ static int perf_event__repipe_sample(struct perf_tool *tool,
 				     struct perf_evsel *evsel,
 				     struct machine *machine)
 {
+	struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
+
+	if (inject->time_start && inject->time_start > sample->time)
+		return 0;
+
+	if (inject->time_end && inject->time_end < sample->time)
+		return 0;
+
 	if (evsel->handler.func) {
 		inject_handler f = evsel->handler.func;
 		return f(tool, event, sample, evsel, machine);
@@ -206,6 +215,13 @@ static int perf_event__inject_buildid(struct perf_tool *tool,
 	struct addr_location al;
 	struct thread *thread;
 	u8 cpumode;
+	struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
+
+	if (inject->time_start && inject->time_start > sample->time)
+		return 0;
+
+	if (inject->time_end && inject->time_end < sample->time)
+		return 0;
 
 	cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
 
@@ -393,6 +409,23 @@ static int __cmd_inject(struct perf_inject *inject)
 	return ret;
 }
 
+static int
+parse_time_filter(const struct option *opt, const char *str,
+		  int unset __maybe_unused)
+{
+	struct perf_inject *inject = opt->value;
+	char *sep = strchr(str, '-');
+
+	if (sep == NULL)
+		return parse_nsec_time(str, &inject->time_start);
+	else if (sep == str)
+		return parse_nsec_time(++str, &inject->time_end);
+
+	*sep++ = '\0';
+	return parse_nsec_time(str, &inject->time_start) ||
+		parse_nsec_time(sep, &inject->time_end);
+}
+
 int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	struct perf_inject inject = {
@@ -427,6 +460,8 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 			    "where and how long tasks slept"),
 		OPT_INCR('v', "verbose", &verbose,
 			 "be more verbose (show build ids, etc)"),
+		OPT_CALLBACK('X', "time-filter", &inject, "time[-time]",
+			     "Only display entries in the time range", parse_time_filter),
 		OPT_END()
 	};
 	const char * const inject_usage[] = {
Signature · 1 line
1.7.11.7
#6 lore raw
In reply to Namhyung Kim
Quoted 25 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

The parse_nsec_time() function is for parsing a string of time into 64-bit nsec value. It's a preparation of time filtering in some of perf commands.

---
  tools/perf/util/util.c | 30 ++++++++++++++++++++++++++++++
  tools/perf/util/util.h |  2 ++
  2 files changed, 32 insertions(+)
tools/perf/util/util.c +4 -0
index 59d868add275..4a207c40d7bc 100644
@@ -269,3 +269,33 @@ void perf_debugfs_set_path(const char *mntpt)
  	snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
  	set_tracing_events_path(mntpt);
  }
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+	u64 time_sec, time_nsec;

tools/perf/util/util.c void perf_debugfs_set_path(const char *mntpt) · line 285

Quoted 10 lines
tools/perf/util/util.c +10 -0
+	char *end;
+
+	time_sec = strtoul(str, &end, 10);
+	if (*end != '.' && *end != '\0')
+		return -1;
+
+	if (*end == '.') {
+		int i;
+		char nsec_buf[10];
+

catch garbage nsec times too:

if strlen(end+1) > 9)

	return -1;

David

Quoted 30 lines
tools/perf/util/util.c +16 -0
+		strncpy(nsec_buf, end + 1, 9);
+		nsec_buf[9] = '\0';
+
+		/* make it nsec precision */
+		for (i = strlen(nsec_buf); i < 9; i++)
+			nsec_buf[i] = '0';
+
+		time_nsec = strtoul(nsec_buf, &end, 10);
+		if (*end != '\0')
+			return -1;
+	} else
+		time_nsec = 0;
+
+	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
+	return 0;
+}
tools/perf/util/util.h +2 -0
index 7a484c97e500..6c8baf312c8a 100644
@@ -204,6 +204,8 @@ static inline int has_extension(const char *filename, const char *ext)
  #define NSEC_PER_MSEC	1000000L
  #endif
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
  extern unsigned char sane_ctype[256];
  #define GIT_SPACE		0x01
  #define GIT_DIGIT		0x02
author maintainer
#7 lore raw

PATCH v3 1/4 perf util: Add parse_nsec_time() function

From: Namhyung Kim <[email protected]>

The parse_nsec_time() function is for parsing a string of time into 64-bit nsec value. It's a preparation of time filtering in some of perf commands.

---
 tools/perf/util/util.c | 33 +++++++++++++++++++++++++++++++++
 tools/perf/util/util.h |  2 ++
 2 files changed, 35 insertions(+)
tools/perf/util/util.c +33 -0
index 59d868add275..205972f6addb 100644
@@ -269,3 +269,36 @@ void perf_debugfs_set_path(const char *mntpt)
 	snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
 	set_tracing_events_path(mntpt);
 }
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+	u64 time_sec, time_nsec;
+	char *end;
+
+	time_sec = strtoul(str, &end, 10);
+	if (*end != '.' && *end != '\0')
+		return -1;
+
+	if (*end == '.') {
+		int i;
+		char nsec_buf[10];
+
+		if (strlen(++end) > 9)
+			return -1;
+
+		strncpy(nsec_buf, end, 9);
+		nsec_buf[9] = '\0';
+
+		/* make it nsec precision */
+		for (i = strlen(nsec_buf); i < 9; i++)
+			nsec_buf[i] = '0';
+
+		time_nsec = strtoul(nsec_buf, &end, 10);
+		if (*end != '\0')
+			return -1;
+	} else
+		time_nsec = 0;
+
+	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
+	return 0;
+}
tools/perf/util/util.h +2 -0
index 7a484c97e500..6c8baf312c8a 100644
@@ -204,6 +204,8 @@ static inline int has_extension(const char *filename, const char *ext)
 #define NSEC_PER_MSEC	1000000L
 #endif
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
 extern unsigned char sane_ctype[256];
 #define GIT_SPACE		0x01
 #define GIT_DIGIT		0x02
Signature · 1 line
1.7.11.7
#8 lore raw
Quoted 8 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

The parse_nsec_time() function is for parsing a string of time into 64-bit nsec value. It's a preparation of time filtering in some of perf commands.

Quoted 60 lines
---
  tools/perf/util/util.c | 33 +++++++++++++++++++++++++++++++++
  tools/perf/util/util.h |  2 ++
  2 files changed, 35 insertions(+)
tools/perf/util/util.c +33 -0
index 59d868add275..205972f6addb 100644
@@ -269,3 +269,36 @@ void perf_debugfs_set_path(const char *mntpt)
  	snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
  	set_tracing_events_path(mntpt);
  }
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+	u64 time_sec, time_nsec;
+	char *end;
+
+	time_sec = strtoul(str, &end, 10);
+	if (*end != '.' && *end != '\0')
+		return -1;
+
+	if (*end == '.') {
+		int i;
+		char nsec_buf[10];
+
+		if (strlen(++end) > 9)
+			return -1;
+
+		strncpy(nsec_buf, end, 9);
+		nsec_buf[9] = '\0';
+
+		/* make it nsec precision */
+		for (i = strlen(nsec_buf); i < 9; i++)
+			nsec_buf[i] = '0';
+
+		time_nsec = strtoul(nsec_buf, &end, 10);
+		if (*end != '\0')
+			return -1;
+	} else
+		time_nsec = 0;
+
+	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
+	return 0;
+}
tools/perf/util/util.h +2 -0
index 7a484c97e500..6c8baf312c8a 100644
@@ -204,6 +204,8 @@ static inline int has_extension(const char *filename, const char *ext)
  #define NSEC_PER_MSEC	1000000L
  #endif
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
  extern unsigned char sane_ctype[256];
  #define GIT_SPACE		0x01
  #define GIT_DIGIT		0x02
#9 lore raw
In reply to Namhyung Kim
perf util: Add parse_nsec_time() function
Commit-ID:  3b47abe1b5f5446ab41a3a139b6075f46f77f21f
Gitweb:     http://git.kernel.org/tip/3b47abe1b5f5446ab41a3a139b6075f46f77f21f
Author:     Namhyung Kim <[email protected]>
AuthorDate: Tue, 4 Jun 2013 10:50:29 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 7 Aug 2013 17:35:26 -0300

perf util: Add parse_nsec_time() function

The parse_nsec_time() function is for parsing a string of time into 64-bit nsec value. It's a preparation of time filtering in some of perf commands.

---
 tools/perf/util/util.c | 33 +++++++++++++++++++++++++++++++++
 tools/perf/util/util.h |  2 ++
 2 files changed, 35 insertions(+)
tools/perf/util/util.c +33 -0
index 9a06584..6d17b18 100644
@@ -328,3 +328,36 @@ void put_tracing_file(char *file)
 {
 	free(file);
 }
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+	u64 time_sec, time_nsec;
+	char *end;
+
+	time_sec = strtoul(str, &end, 10);
+	if (*end != '.' && *end != '\0')
+		return -1;
+
+	if (*end == '.') {
+		int i;
+		char nsec_buf[10];
+
+		if (strlen(++end) > 9)
+			return -1;
+
+		strncpy(nsec_buf, end, 9);
+		nsec_buf[9] = '\0';
+
+		/* make it nsec precision */
+		for (i = strlen(nsec_buf); i < 9; i++)
+			nsec_buf[i] = '0';
+
+		time_nsec = strtoul(nsec_buf, &end, 10);
+		if (*end != '\0')
+			return -1;
+	} else
+		time_nsec = 0;
+
+	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
+	return 0;
+}
tools/perf/util/util.h +2 -0
index cc1574e..a535359 100644
@@ -208,6 +208,8 @@ static inline int has_extension(const char *filename, const char *ext)
 #define NSEC_PER_MSEC	1000000L
 #endif
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
 extern unsigned char sane_ctype[256];
 #define GIT_SPACE		0x01
 #define GIT_DIGIT		0x02
author maintainer
#10 lore raw
In reply to Namhyung Kim

Hi Arnaldo,

Could you pick these up or give a comment?

Thanks,
Namhyung

Quoted 64 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

The parse_nsec_time() function is for parsing a string of time into 64-bit nsec value. It's a preparation of time filtering in some of perf commands.

---
 tools/perf/util/util.c | 30 ++++++++++++++++++++++++++++++
 tools/perf/util/util.h |  2 ++
 2 files changed, 32 insertions(+)
tools/perf/util/util.c +30 -0
index 59d868add275..4a207c40d7bc 100644
@@ -269,3 +269,33 @@ void perf_debugfs_set_path(const char *mntpt)
 	snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
 	set_tracing_events_path(mntpt);
 }
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+	u64 time_sec, time_nsec;
+	char *end;
+
+	time_sec = strtoul(str, &end, 10);
+	if (*end != '.' && *end != '\0')
+		return -1;
+
+	if (*end == '.') {
+		int i;
+		char nsec_buf[10];
+
+		strncpy(nsec_buf, end + 1, 9);
+		nsec_buf[9] = '\0';
+
+		/* make it nsec precision */
+		for (i = strlen(nsec_buf); i < 9; i++)
+			nsec_buf[i] = '0';
+
+		time_nsec = strtoul(nsec_buf, &end, 10);
+		if (*end != '\0')
+			return -1;
+	} else
+		time_nsec = 0;
+
+	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
+	return 0;
+}
tools/perf/util/util.h +2 -0
index 7a484c97e500..6c8baf312c8a 100644
@@ -204,6 +204,8 @@ static inline int has_extension(const char *filename, const char *ext)
 #define NSEC_PER_MSEC	1000000L
 #endif
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
 extern unsigned char sane_ctype[256];
 #define GIT_SPACE		0x01
 #define GIT_DIGIT		0x02