Thomas Gleixner | 9100704 | 2019-05-29 07:12:25 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 4 | * |
| 5 | * Refactored from builtin-top.c, see that files for further copyright notes. |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 6 | */ |
| 7 | |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 8 | #include "event.h" |
| 9 | #include "evlist.h" |
| 10 | #include "evsel.h" |
| 11 | #include "parse-events.h" |
| 12 | #include "symbol.h" |
| 13 | #include "top.h" |
Arnaldo Carvalho de Melo | c1a604d | 2019-08-29 15:20:59 -0300 | [diff] [blame] | 14 | #include "../perf.h" |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 15 | #include <inttypes.h> |
| 16 | |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 17 | #define SNPRINTF(buf, size, fmt, args...) \ |
| 18 | ({ \ |
| 19 | size_t r = snprintf(buf, size, fmt, ## args); \ |
| 20 | r > size ? size : r; \ |
| 21 | }) |
| 22 | |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 23 | size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) |
| 24 | { |
Namhyung Kim | 6f29097 | 2013-05-14 11:09:00 +0900 | [diff] [blame] | 25 | float samples_per_sec; |
| 26 | float ksamples_per_sec; |
| 27 | float esamples_percent; |
Arnaldo Carvalho de Melo | b400679 | 2013-12-19 14:43:45 -0300 | [diff] [blame] | 28 | struct record_opts *opts = &top->record_opts; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 29 | struct target *target = &opts->target; |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 30 | size_t ret = 0; |
| 31 | |
Namhyung Kim | 6f29097 | 2013-05-14 11:09:00 +0900 | [diff] [blame] | 32 | if (top->samples) { |
| 33 | samples_per_sec = top->samples / top->delay_secs; |
| 34 | ksamples_per_sec = top->kernel_samples / top->delay_secs; |
| 35 | esamples_percent = (100.0 * top->exact_samples) / top->samples; |
| 36 | } else { |
| 37 | samples_per_sec = ksamples_per_sec = esamples_percent = 0.0; |
| 38 | } |
| 39 | |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 40 | if (!perf_guest) { |
Namhyung Kim | 6f29097 | 2013-05-14 11:09:00 +0900 | [diff] [blame] | 41 | float ksamples_percent = 0.0; |
| 42 | |
| 43 | if (samples_per_sec) |
| 44 | ksamples_percent = (100.0 * ksamples_per_sec) / |
| 45 | samples_per_sec; |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 46 | ret = SNPRINTF(bf, size, |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 47 | " PerfTop:%8.0f irqs/sec kernel:%4.1f%%" |
Jiri Olsa | 97f7e0b | 2018-11-11 20:02:46 +0100 | [diff] [blame] | 48 | " exact: %4.1f%% lost: %" PRIu64 "/%" PRIu64 " drop: %" PRIu64 "/%" PRIu64 " [", |
| 49 | samples_per_sec, ksamples_percent, esamples_percent, |
| 50 | top->lost, top->lost_total, top->drop, top->drop_total); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 51 | } else { |
| 52 | float us_samples_per_sec = top->us_samples / top->delay_secs; |
| 53 | float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs; |
| 54 | float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs; |
| 55 | |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 56 | ret = SNPRINTF(bf, size, |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 57 | " PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%" |
| 58 | " guest kernel:%4.1f%% guest us:%4.1f%%" |
| 59 | " exact: %4.1f%% [", samples_per_sec, |
| 60 | 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) / |
| 61 | samples_per_sec)), |
| 62 | 100.0 - (100.0 * ((samples_per_sec - us_samples_per_sec) / |
| 63 | samples_per_sec)), |
| 64 | 100.0 - (100.0 * ((samples_per_sec - |
| 65 | guest_kernel_samples_per_sec) / |
| 66 | samples_per_sec)), |
| 67 | 100.0 - (100.0 * ((samples_per_sec - |
| 68 | guest_us_samples_per_sec) / |
| 69 | samples_per_sec)), |
| 70 | esamples_percent); |
| 71 | } |
| 72 | |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 73 | if (top->evlist->core.nr_entries == 1) { |
Jiri Olsa | 515dbe4 | 2019-09-03 10:39:52 +0200 | [diff] [blame] | 74 | struct evsel *first = evlist__first(top->evlist); |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 75 | ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ", |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 76 | (uint64_t)first->core.attr.sample_period, |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 77 | opts->freq ? "Hz" : ""); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 78 | } |
| 79 | |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 80 | ret += SNPRINTF(bf + ret, size - ret, "%s", evsel__name(top->sym_evsel)); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 81 | |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 82 | ret += SNPRINTF(bf + ret, size - ret, "], "); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 83 | |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 84 | if (target->pid) |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 85 | ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %s", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 86 | target->pid); |
| 87 | else if (target->tid) |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 88 | ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 89 | target->tid); |
| 90 | else if (target->uid_str != NULL) |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 91 | ret += SNPRINTF(bf + ret, size - ret, " (uid: %s", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 92 | target->uid_str); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 93 | else |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 94 | ret += SNPRINTF(bf + ret, size - ret, " (all"); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 95 | |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 96 | if (target->cpu_list) |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 97 | ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)", |
Ian Rogers | 4402869 | 2022-01-21 20:58:10 -0800 | [diff] [blame] | 98 | perf_cpu_map__nr(top->evlist->core.cpus) > 1 ? "s" : "", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 99 | target->cpu_list); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 100 | else { |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame] | 101 | if (target->tid) |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 102 | ret += SNPRINTF(bf + ret, size - ret, ")"); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 103 | else |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 104 | ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)", |
Ian Rogers | 4402869 | 2022-01-21 20:58:10 -0800 | [diff] [blame] | 105 | perf_cpu_map__nr(top->evlist->core.cpus), |
| 106 | perf_cpu_map__nr(top->evlist->core.cpus) > 1 ? "s" : ""); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 107 | } |
| 108 | |
Jiri Olsa | 8aa5c8e | 2018-11-13 11:15:34 +0100 | [diff] [blame] | 109 | perf_top__reset_sample_counters(top); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | void perf_top__reset_sample_counters(struct perf_top *top) |
| 114 | { |
| 115 | top->samples = top->us_samples = top->kernel_samples = |
| 116 | top->exact_samples = top->guest_kernel_samples = |
Jiri Olsa | 97f7e0b | 2018-11-11 20:02:46 +0100 | [diff] [blame] | 117 | top->guest_us_samples = top->lost = top->drop = 0; |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 118 | } |