Linux Perf
top.c
Go to the documentation of this file.
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 
18 #define SNPRINTF(buf, size, fmt, args...) \
19 ({ \
20  size_t r = snprintf(buf, size, fmt, ## args); \
21  r > size ? size : r; \
22 })
23 
24 size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
25 {
26  float samples_per_sec;
27  float ksamples_per_sec;
28  float esamples_percent;
29  struct record_opts *opts = &top->record_opts;
30  struct target *target = &opts->target;
31  size_t ret = 0;
32 
33  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 
41  if (!perf_guest) {
42  float ksamples_percent = 0.0;
43 
44  if (samples_per_sec)
45  ksamples_percent = (100.0 * ksamples_per_sec) /
46  samples_per_sec;
47  ret = SNPRINTF(bf, size,
48  " PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
49  " exact: %4.1f%% [", samples_per_sec,
50  ksamples_percent, esamples_percent);
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 
56  ret = SNPRINTF(bf, size,
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 
73  if (top->evlist->nr_entries == 1) {
74  struct perf_evsel *first = perf_evlist__first(top->evlist);
75  ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ",
76  (uint64_t)first->attr.sample_period,
77  opts->freq ? "Hz" : "");
78  }
79 
80  ret += SNPRINTF(bf + ret, size - ret, "%s", perf_evsel__name(top->sym_evsel));
81 
82  ret += SNPRINTF(bf + ret, size - ret, "], ");
83 
84  if (target->pid)
85  ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %s",
86  target->pid);
87  else if (target->tid)
88  ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s",
89  target->tid);
90  else if (target->uid_str != NULL)
91  ret += SNPRINTF(bf + ret, size - ret, " (uid: %s",
92  target->uid_str);
93  else
94  ret += SNPRINTF(bf + ret, size - ret, " (all");
95 
96  if (target->cpu_list)
97  ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
98  top->evlist->cpus->nr > 1 ? "s" : "",
99  target->cpu_list);
100  else {
101  if (target->tid)
102  ret += SNPRINTF(bf + ret, size - ret, ")");
103  else
104  ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
105  top->evlist->cpus->nr,
106  top->evlist->cpus->nr > 1 ? "s" : "");
107  }
108 
109  return ret;
110 }
111 
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 }
int nr
Definition: cpumap.h:14
#define SNPRINTF(buf, size, fmt, args...)
Definition: top.c:18
const char * uid_str
Definition: target.h:12
size_t size
Definition: evsel.c:60
u64 samples
Definition: top.h:25
struct perf_evsel * sym_evsel
Definition: top.h:36
u64 guest_us_samples
Definition: top.h:28
int delay_secs
Definition: top.h:29
u64 us_samples
Definition: top.h:26
int nr_entries
Definition: evlist.h:30
struct target target
Definition: perf.h:38
bool perf_guest
Definition: util.c:81
void perf_top__reset_sample_counters(struct perf_top *top)
Definition: top.c:112
struct record_opts record_opts
Definition: top.h:19
const char * perf_evsel__name(struct perf_evsel *evsel)
Definition: evsel.c:577
size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
Definition: top.c:24
static struct perf_evsel * perf_evlist__first(struct perf_evlist *evlist)
Definition: evlist.h:215
Definition: top.h:16
const char * tid
Definition: target.h:10
u64 kernel_samples
Definition: top.h:26
unsigned int freq
Definition: perf.h:66
const char * cpu_list
Definition: target.h:11
u64 exact_samples
Definition: top.h:27
const char * pid
Definition: target.h:9
struct cpu_map * cpus
Definition: evlist.h:48
u64 guest_kernel_samples
Definition: top.h:28
struct perf_evlist * evlist
Definition: top.h:18
struct perf_event_attr attr
Definition: evsel.h:93
Definition: target.h:8