blob: f857b51b6bde81248ae747f13694acb52a153907 [file] [log] [blame]
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001/*
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 Olsab9a46bb2011-03-07 21:13:40 +010018#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 Melo8c3e10e2011-01-31 14:50:39 -020024size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
25{
Namhyung Kim6f290972013-05-14 11:09:00 +090026 float samples_per_sec;
27 float ksamples_per_sec;
28 float esamples_percent;
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -030029 struct perf_record_opts *opts = &top->record_opts;
30 struct perf_target *target = &opts->target;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020031 size_t ret = 0;
32
Namhyung Kim6f290972013-05-14 11:09:00 +090033 if (top->samples) {
34 samples_per_sec = top->samples / top->delay_secs;
35 ksamples_per_sec = top->kernel_samples / top->delay_secs;
36 esamples_percent = (100.0 * top->exact_samples) / top->samples;
37 } else {
38 samples_per_sec = ksamples_per_sec = esamples_percent = 0.0;
39 }
40
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020041 if (!perf_guest) {
Namhyung Kim6f290972013-05-14 11:09:00 +090042 float ksamples_percent = 0.0;
43
44 if (samples_per_sec)
45 ksamples_percent = (100.0 * ksamples_per_sec) /
46 samples_per_sec;
Jiri Olsab9a46bb2011-03-07 21:13:40 +010047 ret = SNPRINTF(bf, size,
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020048 " PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
49 " exact: %4.1f%% [", samples_per_sec,
Namhyung Kim6f290972013-05-14 11:09:00 +090050 ksamples_percent, esamples_percent);
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
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -030073 if (top->evlist->nr_entries == 1) {
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -030074 struct perf_evsel *first = perf_evlist__first(top->evlist);
Jiri Olsab9a46bb2011-03-07 21:13:40 +010075 ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020076 (uint64_t)first->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 Melo7289f832012-06-12 12:34:58 -030080 ret += SNPRINTF(bf + ret, size - ret, "%s", perf_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)",
Namhyung Kimfe9d18a2012-04-26 14:15:17 +090098 top->evlist->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)",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200105 top->evlist->cpus->nr,
106 top->evlist->cpus->nr > 1 ? "s" : "");
107 }
108
109 return ret;
110}
111
112void perf_top__reset_sample_counters(struct perf_top *top)
113{
114 top->samples = top->us_samples = top->kernel_samples =
115 top->exact_samples = top->guest_kernel_samples =
116 top->guest_us_samples = 0;
117}