Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 3 | * |
| 4 | * Refactored from builtin-top.c, see that files for further copyright notes. |
| 5 | * |
| 6 | * Released under the GPL v2. (and only v2, not any later version) |
| 7 | */ |
| 8 | |
| 9 | #include "cpumap.h" |
| 10 | #include "event.h" |
| 11 | #include "evlist.h" |
| 12 | #include "evsel.h" |
| 13 | #include "parse-events.h" |
| 14 | #include "symbol.h" |
| 15 | #include "top.h" |
| 16 | #include <inttypes.h> |
| 17 | |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 18 | #define SNPRINTF(buf, size, fmt, args...) \ |
| 19 | ({ \ |
| 20 | size_t r = snprintf(buf, size, fmt, ## args); \ |
| 21 | r > size ? size : r; \ |
| 22 | }) |
| 23 | |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 24 | size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) |
| 25 | { |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 26 | float samples_per_sec = top->samples / top->delay_secs; |
| 27 | float ksamples_per_sec = top->kernel_samples / top->delay_secs; |
| 28 | float esamples_percent = (100.0 * top->exact_samples) / top->samples; |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 29 | struct perf_record_opts *opts = &top->record_opts; |
| 30 | struct perf_target *target = &opts->target; |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 31 | size_t ret = 0; |
| 32 | |
| 33 | if (!perf_guest) { |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 34 | ret = SNPRINTF(bf, size, |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 35 | " PerfTop:%8.0f irqs/sec kernel:%4.1f%%" |
| 36 | " exact: %4.1f%% [", samples_per_sec, |
| 37 | 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) / |
| 38 | samples_per_sec)), |
| 39 | esamples_percent); |
| 40 | } else { |
| 41 | float us_samples_per_sec = top->us_samples / top->delay_secs; |
| 42 | float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs; |
| 43 | float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs; |
| 44 | |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 45 | ret = SNPRINTF(bf, size, |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 46 | " PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%" |
| 47 | " guest kernel:%4.1f%% guest us:%4.1f%%" |
| 48 | " exact: %4.1f%% [", samples_per_sec, |
| 49 | 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) / |
| 50 | samples_per_sec)), |
| 51 | 100.0 - (100.0 * ((samples_per_sec - us_samples_per_sec) / |
| 52 | samples_per_sec)), |
| 53 | 100.0 - (100.0 * ((samples_per_sec - |
| 54 | guest_kernel_samples_per_sec) / |
| 55 | samples_per_sec)), |
| 56 | 100.0 - (100.0 * ((samples_per_sec - |
| 57 | guest_us_samples_per_sec) / |
| 58 | samples_per_sec)), |
| 59 | esamples_percent); |
| 60 | } |
| 61 | |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 62 | if (top->evlist->nr_entries == 1) { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 63 | struct perf_evsel *first = perf_evlist__first(top->evlist); |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 64 | ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ", |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 65 | (uint64_t)first->attr.sample_period, |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 66 | opts->freq ? "Hz" : ""); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 67 | } |
| 68 | |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 69 | ret += SNPRINTF(bf + ret, size - ret, "%s", perf_evsel__name(top->sym_evsel)); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 70 | |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 71 | ret += SNPRINTF(bf + ret, size - ret, "], "); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 72 | |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 73 | if (target->pid) |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 74 | ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %s", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 75 | target->pid); |
| 76 | else if (target->tid) |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 77 | ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 78 | target->tid); |
| 79 | else if (target->uid_str != NULL) |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 80 | ret += SNPRINTF(bf + ret, size - ret, " (uid: %s", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 81 | target->uid_str); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 82 | else |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 83 | ret += SNPRINTF(bf + ret, size - ret, " (all"); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 84 | |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 85 | if (target->cpu_list) |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 86 | ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)", |
Namhyung Kim | fe9d18a | 2012-04-26 14:15:17 +0900 | [diff] [blame] | 87 | top->evlist->cpus->nr > 1 ? "s" : "", |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 88 | target->cpu_list); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 89 | else { |
Arnaldo Carvalho de Melo | 2376c67 | 2012-12-11 16:48:41 -0300 | [diff] [blame^] | 90 | if (target->tid) |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 91 | ret += SNPRINTF(bf + ret, size - ret, ")"); |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 92 | else |
Jiri Olsa | b9a46bb | 2011-03-07 21:13:40 +0100 | [diff] [blame] | 93 | ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)", |
Arnaldo Carvalho de Melo | 8c3e10e | 2011-01-31 14:50:39 -0200 | [diff] [blame] | 94 | top->evlist->cpus->nr, |
| 95 | top->evlist->cpus->nr > 1 ? "s" : ""); |
| 96 | } |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | void perf_top__reset_sample_counters(struct perf_top *top) |
| 102 | { |
| 103 | top->samples = top->us_samples = top->kernel_samples = |
| 104 | top->exact_samples = top->guest_kernel_samples = |
| 105 | top->guest_us_samples = 0; |
| 106 | } |