blob: 27945eeb0cb589cb022dbedcc6630b4ec4cb227f [file] [log] [blame]
Thomas Gleixner91007042019-05-29 07:12:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02002/*
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 Melo8c3e10e2011-01-31 14:50:39 -02006 */
7
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02008#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 Meloc1a604d2019-08-29 15:20:59 -030014#include "../perf.h"
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020015#include <inttypes.h>
16
Jiri Olsab9a46bb2011-03-07 21:13:40 +010017#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 Melo8c3e10e2011-01-31 14:50:39 -020023size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
24{
Namhyung Kim6f290972013-05-14 11:09:00 +090025 float samples_per_sec;
26 float ksamples_per_sec;
27 float esamples_percent;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030028 struct record_opts *opts = &top->record_opts;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030029 struct target *target = &opts->target;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020030 size_t ret = 0;
31
Namhyung Kim6f290972013-05-14 11:09:00 +090032 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 Melo8c3e10e2011-01-31 14:50:39 -020040 if (!perf_guest) {
Namhyung Kim6f290972013-05-14 11:09:00 +090041 float ksamples_percent = 0.0;
42
43 if (samples_per_sec)
44 ksamples_percent = (100.0 * ksamples_per_sec) /
45 samples_per_sec;
Jiri Olsab9a46bb2011-03-07 21:13:40 +010046 ret = SNPRINTF(bf, size,
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020047 " PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
Jiri Olsa97f7e0b2018-11-11 20:02:46 +010048 " 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 Melo8c3e10e2011-01-31 14:50:39 -020051 } 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 Olsab9a46bb2011-03-07 21:13:40 +010056 ret = SNPRINTF(bf, size,
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020057 " 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 Olsa6484d2f2019-07-21 13:24:28 +020073 if (top->evlist->core.nr_entries == 1) {
Jiri Olsa515dbe42019-09-03 10:39:52 +020074 struct evsel *first = evlist__first(top->evlist);
Jiri Olsab9a46bb2011-03-07 21:13:40 +010075 ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ",
Jiri Olsa1fc632c2019-07-21 13:24:29 +020076 (uint64_t)first->core.attr.sample_period,
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030077 opts->freq ? "Hz" : "");
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020078 }
79
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -030080 ret += SNPRINTF(bf + ret, size - ret, "%s", evsel__name(top->sym_evsel));
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020081
Jiri Olsab9a46bb2011-03-07 21:13:40 +010082 ret += SNPRINTF(bf + ret, size - ret, "], ");
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020083
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030084 if (target->pid)
David Ahernb52956c2012-02-08 09:32:52 -070085 ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %s",
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030086 target->pid);
87 else if (target->tid)
David Ahernb52956c2012-02-08 09:32:52 -070088 ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s",
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030089 target->tid);
90 else if (target->uid_str != NULL)
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -020091 ret += SNPRINTF(bf + ret, size - ret, " (uid: %s",
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030092 target->uid_str);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020093 else
Jiri Olsab9a46bb2011-03-07 21:13:40 +010094 ret += SNPRINTF(bf + ret, size - ret, " (all");
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020095
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030096 if (target->cpu_list)
Jiri Olsab9a46bb2011-03-07 21:13:40 +010097 ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
Jiri Olsaf72f9012019-07-21 13:24:41 +020098 top->evlist->core.cpus->nr > 1 ? "s" : "",
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030099 target->cpu_list);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200100 else {
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -0300101 if (target->tid)
Jiri Olsab9a46bb2011-03-07 21:13:40 +0100102 ret += SNPRINTF(bf + ret, size - ret, ")");
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200103 else
Jiri Olsab9a46bb2011-03-07 21:13:40 +0100104 ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
Jiri Olsaf72f9012019-07-21 13:24:41 +0200105 top->evlist->core.cpus->nr,
106 top->evlist->core.cpus->nr > 1 ? "s" : "");
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200107 }
108
Jiri Olsa8aa5c8e2018-11-13 11:15:34 +0100109 perf_top__reset_sample_counters(top);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200110 return ret;
111}
112
113void 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 Olsa97f7e0b2018-11-11 20:02:46 +0100117 top->guest_us_samples = top->lost = top->drop = 0;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200118}