Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <joonwoop@codeaurora.org>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/20161024020246.14928-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
99620a5d e107f129

+24 -7
+7 -3
tools/perf/builtin-sched.c
··· 1191 1191 int i; 1192 1192 int ret; 1193 1193 u64 avg; 1194 + char max_lat_at[32]; 1194 1195 1195 1196 if (!work_list->nb_atoms) 1196 1197 return; ··· 1213 1212 printf(" "); 1214 1213 1215 1214 avg = work_list->total_lat / work_list->nb_atoms; 1215 + timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at)); 1216 1216 1217 - printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n", 1217 + printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n", 1218 1218 (double)work_list->total_runtime / NSEC_PER_MSEC, 1219 1219 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC, 1220 1220 (double)work_list->max_lat / NSEC_PER_MSEC, 1221 - (double)work_list->max_lat_at / NSEC_PER_SEC); 1221 + max_lat_at); 1222 1222 } 1223 1223 1224 1224 static int pid_cmp(struct work_atoms *l, struct work_atoms *r) ··· 1404 1402 int cpus_nr; 1405 1403 bool new_cpu = false; 1406 1404 const char *color = PERF_COLOR_NORMAL; 1405 + char stimestamp[32]; 1407 1406 1408 1407 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0); 1409 1408 ··· 1495 1492 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu)) 1496 1493 goto out; 1497 1494 1498 - color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp / NSEC_PER_SEC); 1495 + timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp)); 1496 + color_fprintf(stdout, color, " %12s secs ", stimestamp); 1499 1497 if (new_shortname || (verbose && sched_in->tid)) { 1500 1498 const char *pid_color = color; 1501 1499
+6 -4
tools/perf/builtin-script.c
··· 441 441 { 442 442 struct perf_event_attr *attr = &evsel->attr; 443 443 unsigned long secs; 444 - unsigned long usecs; 445 444 unsigned long long nsecs; 446 445 447 446 if (PRINT_FIELD(COMM)) { ··· 470 471 nsecs = sample->time; 471 472 secs = nsecs / NSEC_PER_SEC; 472 473 nsecs -= secs * NSEC_PER_SEC; 473 - usecs = nsecs / NSEC_PER_USEC; 474 + 474 475 if (nanosecs) 475 476 printf("%5lu.%09llu: ", secs, nsecs); 476 - else 477 - printf("%5lu.%06lu: ", secs, usecs); 477 + else { 478 + char sample_time[32]; 479 + timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time)); 480 + printf("%12s: ", sample_time); 481 + } 478 482 } 479 483 } 480 484
+8
tools/perf/util/util.c
··· 433 433 return 0; 434 434 } 435 435 436 + int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz) 437 + { 438 + u64 sec = timestamp / NSEC_PER_SEC; 439 + u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC; 440 + 441 + return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec); 442 + } 443 + 436 444 unsigned long parse_tag_value(const char *str, struct parse_tag *tags) 437 445 { 438 446 struct parse_tag *i = tags;
+3
tools/perf/util/util.h
··· 362 362 #endif 363 363 364 364 int is_printable_array(char *p, unsigned int len); 365 + 366 + int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz); 367 + 365 368 #endif /* GIT_COMPAT_UTIL_H */