2016-10-24 02:02:55

by Namhyung Kim

[permalink] [raw]
Subject: [PATCH 1/3] perf sched map: Apply cpu color when there's an activity

Applying cpu color always doesn't help readability IMHO. Instead it
might be better to applying the color when there's an activity on those
CPUs.

Cc: Jiri Olsa <[email protected]>
Signed-off-by: Namhyung Kim <[email protected]>
---
tools/perf/builtin-sched.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index f5503ca22e1c..78006e991d91 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1479,7 +1479,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
cpu_color = COLOR_CPUS;

if (cpu != this_cpu)
- color_fprintf(stdout, cpu_color, " ");
+ color_fprintf(stdout, color, " ");
else
color_fprintf(stdout, cpu_color, "*");

--
2.10.0


2016-10-24 02:02:59

by Namhyung Kim

[permalink] [raw]
Subject: [PATCH 2/3] perf sched map: Always show task comm with -v

I'd like to see the name of tasks with perf sched map, but it only shows
name of new tasks and then use short names after all. This is not good
for long running tasks since it's hard for users to track the short
names. This patch makes it show the names (except the idle task) when
-v option is used. Probably we may make it as default behavior.

Signed-off-by: Namhyung Kim <[email protected]>
---
tools/perf/builtin-sched.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 78006e991d91..97d6cbf486bb 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1493,7 +1493,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
goto out;

color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp / NSEC_PER_SEC);
- if (new_shortname) {
+ if (new_shortname || (verbose && sched_in->tid)) {
const char *pid_color = color;

if (thread__has_color(sched_in))
--
2.10.0

2016-10-24 02:03:10

by Namhyung Kim

[permalink] [raw]
Subject: [PATCH 3/3] perf tools: Introduce timestamp_in_usec()

Joonwoo reported that there's a mismatch between timestamps in script
and sched commands. This was because of difference in printing the
timestamp. Factor out the code and share it so that they can be in
sync. Also I found that sched map has similar problem, fix it too.

Reported-by: Joonwoo Park <[email protected]>
Signed-off-by: Namhyung Kim <[email protected]>
---
tools/perf/builtin-sched.c | 9 ++++++---
tools/perf/builtin-script.c | 9 ++++++---
tools/perf/util/util.c | 9 +++++++++
tools/perf/util/util.h | 3 +++
4 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 97d6cbf486bb..c88d64ae997e 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1191,6 +1191,7 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_
int i;
int ret;
u64 avg;
+ char buf[32];

if (!work_list->nb_atoms)
return;
@@ -1213,11 +1214,11 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_

avg = work_list->total_lat / work_list->nb_atoms;

- printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n",
+ printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n",
(double)work_list->total_runtime / NSEC_PER_MSEC,
work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
(double)work_list->max_lat / NSEC_PER_MSEC,
- (double)work_list->max_lat_at / NSEC_PER_SEC);
+ timestamp_in_usec(buf, sizeof(buf), work_list->max_lat));
}

static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
@@ -1402,6 +1403,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
int cpus_nr;
bool new_cpu = false;
const char *color = PERF_COLOR_NORMAL;
+ char buf[32];

BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);

@@ -1492,7 +1494,8 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
goto out;

- color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp / NSEC_PER_SEC);
+ color_fprintf(stdout, color, " %12s secs ",
+ timestamp_in_usec(buf, sizeof(buf), timestamp));
if (new_shortname || (verbose && sched_in->tid)) {
const char *pid_color = color;

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 7228d141a789..c848c74bdc90 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -437,7 +437,6 @@ static void print_sample_start(struct perf_sample *sample,
{
struct perf_event_attr *attr = &evsel->attr;
unsigned long secs;
- unsigned long usecs;
unsigned long long nsecs;

if (PRINT_FIELD(COMM)) {
@@ -464,14 +463,18 @@ static void print_sample_start(struct perf_sample *sample,
}

if (PRINT_FIELD(TIME)) {
+ char buf[32];
+ size_t sz = sizeof(buf);
+
nsecs = sample->time;
secs = nsecs / NSEC_PER_SEC;
nsecs -= secs * NSEC_PER_SEC;
- usecs = nsecs / NSEC_PER_USEC;
+
if (nanosecs)
printf("%5lu.%09llu: ", secs, nsecs);
else
- printf("%5lu.%06lu: ", secs, usecs);
+ printf("%12s: ", timestamp_in_usec(buf, sz,
+ sample->time));
}
}

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 85c56800f17a..aa3e778989ce 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -433,6 +433,15 @@ int parse_nsec_time(const char *str, u64 *ptime)
return 0;
}

+char *timestamp_in_usec(char *buf, size_t sz, u64 timestamp)
+{
+ u64 sec = timestamp / NSEC_PER_SEC;
+ u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
+
+ scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
+ return buf;
+}
+
unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
{
struct parse_tag *i = tags;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 71b6992f1d98..ece974f1c538 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -362,4 +362,7 @@ extern int sched_getcpu(void);
#endif

int is_printable_array(char *p, unsigned int len);
+
+char *timestamp_in_usec(char *buf, size_t sz, u64 timestamp);
+
#endif /* GIT_COMPAT_UTIL_H */
--
2.10.0

2016-10-24 16:46:25

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH 3/3] perf tools: Introduce timestamp_in_usec()

On Mon, Oct 24, 2016 at 11:02:45AM +0900, Namhyung Kim wrote:

SNIP

> +
> if (nanosecs)
> printf("%5lu.%09llu: ", secs, nsecs);
> else
> - printf("%5lu.%06lu: ", secs, usecs);
> + printf("%12s: ", timestamp_in_usec(buf, sz,
> + sample->time));
> }
> }
>
> diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
> index 85c56800f17a..aa3e778989ce 100644
> --- a/tools/perf/util/util.c
> +++ b/tools/perf/util/util.c
> @@ -433,6 +433,15 @@ int parse_nsec_time(const char *str, u64 *ptime)
> return 0;
> }
>
> +char *timestamp_in_usec(char *buf, size_t sz, u64 timestamp)
> +{

good idea.. 2 things:
- I've spot several other places by looking for NSEC_PER_USEC
but haven't checked deeply they could be also users for this function

- timestamp_usec__scnprint might fir better

thanks,
jirka

2016-10-24 16:46:34

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH 1/3] perf sched map: Apply cpu color when there's an activity

On Mon, Oct 24, 2016 at 11:02:43AM +0900, Namhyung Kim wrote:
> Applying cpu color always doesn't help readability IMHO. Instead it
> might be better to applying the color when there's an activity on those
> CPUs.

ok, I added it to easily spot cpu columns I was interested in,
but I think I can live with dotted line ;-)

Acked-by: Jiri Olsa <[email protected]>

thanks,
jirka

>
> Cc: Jiri Olsa <[email protected]>
> Signed-off-by: Namhyung Kim <[email protected]>
> ---
> tools/perf/builtin-sched.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index f5503ca22e1c..78006e991d91 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -1479,7 +1479,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
> cpu_color = COLOR_CPUS;
>
> if (cpu != this_cpu)
> - color_fprintf(stdout, cpu_color, " ");
> + color_fprintf(stdout, color, " ");
> else
> color_fprintf(stdout, cpu_color, "*");
>
> --
> 2.10.0
>

2016-10-24 16:52:24

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH 2/3] perf sched map: Always show task comm with -v

On Mon, Oct 24, 2016 at 11:02:44AM +0900, Namhyung Kim wrote:
> I'd like to see the name of tasks with perf sched map, but it only shows
> name of new tasks and then use short names after all. This is not good
> for long running tasks since it's hard for users to track the short
> names. This patch makes it show the names (except the idle task) when
> -v option is used. Probably we may make it as default behavior.
>
> Signed-off-by: Namhyung Kim <[email protected]>

Acked-by: Jiri Olsa <[email protected]>

thanks,
jirka

> ---
> tools/perf/builtin-sched.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index 78006e991d91..97d6cbf486bb 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -1493,7 +1493,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
> goto out;
>
> color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp / NSEC_PER_SEC);
> - if (new_shortname) {
> + if (new_shortname || (verbose && sched_in->tid)) {
> const char *pid_color = color;
>
> if (thread__has_color(sched_in))
> --
> 2.10.0
>

2016-10-24 20:56:11

by Joonwoo Park

[permalink] [raw]
Subject: Re: [PATCH 3/3] perf tools: Introduce timestamp_in_usec()



On 10/23/2016 07:02 PM, Namhyung Kim wrote:
> Joonwoo reported that there's a mismatch between timestamps in script
> and sched commands. This was because of difference in printing the
> timestamp. Factor out the code and share it so that they can be in
> sync. Also I found that sched map has similar problem, fix it too.
>
> Reported-by: Joonwoo Park <[email protected]>

Sorry I was busy with something else so didn't have chance to follow up
on my initial proposal and thanks for take caring of this.

> Signed-off-by: Namhyung Kim <[email protected]>
> ---
> tools/perf/builtin-sched.c | 9 ++++++---
> tools/perf/builtin-script.c | 9 ++++++---
> tools/perf/util/util.c | 9 +++++++++
> tools/perf/util/util.h | 3 +++
> 4 files changed, 24 insertions(+), 6 deletions(-)
>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index 97d6cbf486bb..c88d64ae997e 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -1191,6 +1191,7 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_
> int i;
> int ret;
> u64 avg;
> + char buf[32];
>
> if (!work_list->nb_atoms)
> return;
> @@ -1213,11 +1214,11 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_
>
> avg = work_list->total_lat / work_list->nb_atoms;
>
> - printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n",
> + printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n",
> (double)work_list->total_runtime / NSEC_PER_MSEC,
> work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
> (double)work_list->max_lat / NSEC_PER_MSEC,
> - (double)work_list->max_lat_at / NSEC_PER_SEC);
> + timestamp_in_usec(buf, sizeof(buf), work_list->max_lat));

This should be :
s/work_list->max_lat/work_list->max_lat_at/

> }
>
> static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
> @@ -1402,6 +1403,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
> int cpus_nr;
> bool new_cpu = false;
> const char *color = PERF_COLOR_NORMAL;
> + char buf[32];
>
> BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
>
> @@ -1492,7 +1494,8 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
> if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
> goto out;
>
> - color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp / NSEC_PER_SEC);
> + color_fprintf(stdout, color, " %12s secs ",
> + timestamp_in_usec(buf, sizeof(buf), timestamp));
> if (new_shortname || (verbose && sched_in->tid)) {
> const char *pid_color = color;
>
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index 7228d141a789..c848c74bdc90 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -437,7 +437,6 @@ static void print_sample_start(struct perf_sample *sample,
> {
> struct perf_event_attr *attr = &evsel->attr;
> unsigned long secs;
> - unsigned long usecs;
> unsigned long long nsecs;
>
> if (PRINT_FIELD(COMM)) {
> @@ -464,14 +463,18 @@ static void print_sample_start(struct perf_sample *sample,
> }
>
> if (PRINT_FIELD(TIME)) {
> + char buf[32];
> + size_t sz = sizeof(buf);
> +
> nsecs = sample->time;
> secs = nsecs / NSEC_PER_SEC;
> nsecs -= secs * NSEC_PER_SEC;
> - usecs = nsecs / NSEC_PER_USEC;
> +
> if (nanosecs)
> printf("%5lu.%09llu: ", secs, nsecs);
> else
> - printf("%5lu.%06lu: ", secs, usecs);
> + printf("%12s: ", timestamp_in_usec(buf, sz,
> + sample->time));
> }
> }
>
> diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
> index 85c56800f17a..aa3e778989ce 100644
> --- a/tools/perf/util/util.c
> +++ b/tools/perf/util/util.c
> @@ -433,6 +433,15 @@ int parse_nsec_time(const char *str, u64 *ptime)
> return 0;
> }
>
> +char *timestamp_in_usec(char *buf, size_t sz, u64 timestamp)

I agree with Jirka. timestamp_usec__scnprintf looks better.

Thanks,
Joonwoo

> +{
> + u64 sec = timestamp / NSEC_PER_SEC;
> + u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
> +
> + scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
> + return buf;
> +}
> +
> unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
> {
> struct parse_tag *i = tags;
> diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
> index 71b6992f1d98..ece974f1c538 100644
> --- a/tools/perf/util/util.h
> +++ b/tools/perf/util/util.h
> @@ -362,4 +362,7 @@ extern int sched_getcpu(void);
> #endif
>
> int is_printable_array(char *p, unsigned int len);
> +
> +char *timestamp_in_usec(char *buf, size_t sz, u64 timestamp);
> +
> #endif /* GIT_COMPAT_UTIL_H */
>

2016-10-25 18:07:56

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 1/3] perf sched map: Apply cpu color when there's an activity

Em Mon, Oct 24, 2016 at 11:02:43AM +0900, Namhyung Kim escreveu:
> Applying cpu color always doesn't help readability IMHO. Instead it
> might be better to applying the color when there's an activity on those
> CPUs.

thanks, applied the three patches.

- Arnaldo

> Cc: Jiri Olsa <[email protected]>
> Signed-off-by: Namhyung Kim <[email protected]>
> ---
> tools/perf/builtin-sched.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index f5503ca22e1c..78006e991d91 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -1479,7 +1479,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
> cpu_color = COLOR_CPUS;
>
> if (cpu != this_cpu)
> - color_fprintf(stdout, cpu_color, " ");
> + color_fprintf(stdout, color, " ");
> else
> color_fprintf(stdout, cpu_color, "*");
>
> --
> 2.10.0

2016-10-25 21:25:28

by Namhyung Kim

[permalink] [raw]
Subject: Re: [PATCH 1/3] perf sched map: Apply cpu color when there's an activity

Hi Arnaldo,

On Tue, Oct 25, 2016 at 03:07:46PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Mon, Oct 24, 2016 at 11:02:43AM +0900, Namhyung Kim escreveu:
> > Applying cpu color always doesn't help readability IMHO. Instead it
> > might be better to applying the color when there's an activity on those
> > CPUs.
>
> thanks, applied the three patches.

Thanks, but unfortunately the patch 3/3 has a bug. Could you please
fold the fix below into the patch?

Thanks,
Namhyung


diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index f0ab715b4923..a8e58d495a66 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1218,7 +1218,7 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_
(double)work_list->total_runtime / NSEC_PER_MSEC,
work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
(double)work_list->max_lat / NSEC_PER_MSEC,
- timestamp_in_usec(buf, sizeof(buf), work_list->max_lat));
+ timestamp_in_usec(buf, sizeof(buf), work_list->max_lat_at));
}

static int pid_cmp(struct work_atoms *l, struct work_atoms *r)

Subject: [tip:perf/core] perf sched map: Apply cpu color when there's an activity

Commit-ID: 1208bb274ba547012579d5b68c693e0b36682b74
Gitweb: http://git.kernel.org/tip/1208bb274ba547012579d5b68c693e0b36682b74
Author: Namhyung Kim <[email protected]>
AuthorDate: Mon, 24 Oct 2016 11:02:43 +0900
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 25 Oct 2016 15:04:48 -0300

perf sched map: Apply cpu color when there's an activity

Applying cpu color always doesn't help readability IMHO. Instead it
might be better to applying the color when there's an activity on those
CPUs.

Signed-off-by: Namhyung Kim <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-sched.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 8ca1b540..a8ad859 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1479,7 +1479,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
cpu_color = COLOR_CPUS;

if (cpu != this_cpu)
- color_fprintf(stdout, cpu_color, " ");
+ color_fprintf(stdout, color, " ");
else
color_fprintf(stdout, cpu_color, "*");


Subject: [tip:perf/core] perf sched map: Always show task comm with -v

Commit-ID: e107f129e2e0e75ddf1cd7995a9f5ffff2307766
Gitweb: http://git.kernel.org/tip/e107f129e2e0e75ddf1cd7995a9f5ffff2307766
Author: Namhyung Kim <[email protected]>
AuthorDate: Mon, 24 Oct 2016 11:02:44 +0900
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 25 Oct 2016 15:05:09 -0300

perf sched map: Always show task comm with -v

I'd like to see the name of tasks with perf sched map, but it only shows
name of new tasks and then use short names after all. This is not good
for long running tasks since it's hard for users to track the short
names. This patch makes it show the names (except the idle task) when
-v option is used. Probably we may make it as default behavior.

Signed-off-by: Namhyung Kim <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-sched.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index a8ad859..1f33d15 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1493,7 +1493,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
goto out;

color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp / NSEC_PER_SEC);
- if (new_shortname) {
+ if (new_shortname || (verbose && sched_in->tid)) {
const char *pid_color = color;

if (thread__has_color(sched_in))

Subject: [tip:perf/core] perf tools: Introduce timestamp__scnprintf_usec()

Commit-ID: 99620a5d0cc8e2dd9aedb629a6e81825f0db020e
Gitweb: http://git.kernel.org/tip/99620a5d0cc8e2dd9aedb629a6e81825f0db020e
Author: Namhyung Kim <[email protected]>
AuthorDate: Mon, 24 Oct 2016 11:02:45 +0900
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 28 Oct 2016 11:29:40 -0200

perf tools: Introduce timestamp__scnprintf_usec()

Joonwoo reported that there's a mismatch between timestamps in script
and sched commands. This was because of difference in printing the
timestamp. Factor out the code and share it so that they can be in
sync. Also I found that sched map has similar problem, fix it too.

Committer notes:

Fixed the max_lat_at bug introduced by Namhyung's original patch, as
pointed out by Joonwoo, and made it a function following the scnprintf()
model, i.e. returning the number of bytes formatted, and receiving as
the first parameter the object from where the data to the formatting is
obtained, renaming it from:

char *timestamp_in_usec(char *bf, size_t size, u64 timestamp)

to

int timestamp__scnprintf_usec(u64 timestamp, char *bf, size_t size)

Reported-by: Joonwoo Park <[email protected]>
Signed-off-by: Namhyung Kim <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-sched.c | 10 +++++++---
tools/perf/builtin-script.c | 10 ++++++----
tools/perf/util/util.c | 8 ++++++++
tools/perf/util/util.h | 3 +++
4 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 1f33d15..fb34412 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1191,6 +1191,7 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_
int i;
int ret;
u64 avg;
+ char max_lat_at[32];

if (!work_list->nb_atoms)
return;
@@ -1212,12 +1213,13 @@ static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_
printf(" ");

avg = work_list->total_lat / work_list->nb_atoms;
+ timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));

- printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n",
+ printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n",
(double)work_list->total_runtime / NSEC_PER_MSEC,
work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
(double)work_list->max_lat / NSEC_PER_MSEC,
- (double)work_list->max_lat_at / NSEC_PER_SEC);
+ max_lat_at);
}

static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
@@ -1402,6 +1404,7 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
int cpus_nr;
bool new_cpu = false;
const char *color = PERF_COLOR_NORMAL;
+ char stimestamp[32];

BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);

@@ -1492,7 +1495,8 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
goto out;

- color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp / NSEC_PER_SEC);
+ timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
+ color_fprintf(stdout, color, " %12s secs ", stimestamp);
if (new_shortname || (verbose && sched_in->tid)) {
const char *pid_color = color;

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 412fb6e..e1daff3 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -441,7 +441,6 @@ static void print_sample_start(struct perf_sample *sample,
{
struct perf_event_attr *attr = &evsel->attr;
unsigned long secs;
- unsigned long usecs;
unsigned long long nsecs;

if (PRINT_FIELD(COMM)) {
@@ -471,11 +470,14 @@ static void print_sample_start(struct perf_sample *sample,
nsecs = sample->time;
secs = nsecs / NSEC_PER_SEC;
nsecs -= secs * NSEC_PER_SEC;
- usecs = nsecs / NSEC_PER_USEC;
+
if (nanosecs)
printf("%5lu.%09llu: ", secs, nsecs);
- else
- printf("%5lu.%06lu: ", secs, usecs);
+ else {
+ char sample_time[32];
+ timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
+ printf("%12s: ", sample_time);
+ }
}
}

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 85c5680..5bbd1f6 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -433,6 +433,14 @@ int parse_nsec_time(const char *str, u64 *ptime)
return 0;
}

+int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
+{
+ u64 sec = timestamp / NSEC_PER_SEC;
+ u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
+
+ return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
+}
+
unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
{
struct parse_tag *i = tags;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 71b6992..79662d6 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -362,4 +362,7 @@ extern int sched_getcpu(void);
#endif

int is_printable_array(char *p, unsigned int len);
+
+int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
+
#endif /* GIT_COMPAT_UTIL_H */