Linux Perf
arm-spe.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Arm Statistical Profiling Extensions (SPE) support
4  * Copyright (c) 2017-2018, Arm Ltd.
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/bitops.h>
10 #include <linux/log2.h>
11 #include <time.h>
12 
13 #include "../../util/cpumap.h"
14 #include "../../util/evsel.h"
15 #include "../../util/evlist.h"
16 #include "../../util/session.h"
17 #include "../../util/util.h"
18 #include "../../util/pmu.h"
19 #include "../../util/debug.h"
20 #include "../../util/auxtrace.h"
21 #include "../../util/arm-spe.h"
22 
23 #define KiB(x) ((x) * 1024)
24 #define MiB(x) ((x) * 1024 * 1024)
25 
30 };
31 
32 static size_t
34  struct perf_evlist *evlist __maybe_unused)
35 {
37 }
38 
40  struct perf_session *session,
41  struct auxtrace_info_event *auxtrace_info,
42  size_t priv_size)
43 {
44  struct arm_spe_recording *sper =
45  container_of(itr, struct arm_spe_recording, itr);
46  struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
47 
48  if (priv_size != ARM_SPE_AUXTRACE_PRIV_SIZE)
49  return -EINVAL;
50 
51  if (!session->evlist->nr_mmaps)
52  return -EINVAL;
53 
54  auxtrace_info->type = PERF_AUXTRACE_ARM_SPE;
55  auxtrace_info->priv[ARM_SPE_PMU_TYPE] = arm_spe_pmu->type;
56 
57  return 0;
58 }
59 
61  struct perf_evlist *evlist,
62  struct record_opts *opts)
63 {
64  struct arm_spe_recording *sper =
65  container_of(itr, struct arm_spe_recording, itr);
66  struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
67  struct perf_evsel *evsel, *arm_spe_evsel = NULL;
68  bool privileged = geteuid() == 0 || perf_event_paranoid() < 0;
69  struct perf_evsel *tracking_evsel;
70  int err;
71 
72  sper->evlist = evlist;
73 
74  evlist__for_each_entry(evlist, evsel) {
75  if (evsel->attr.type == arm_spe_pmu->type) {
76  if (arm_spe_evsel) {
77  pr_err("There may be only one " ARM_SPE_PMU_NAME "x event\n");
78  return -EINVAL;
79  }
80  evsel->attr.freq = 0;
81  evsel->attr.sample_period = 1;
82  arm_spe_evsel = evsel;
83  opts->full_auxtrace = true;
84  }
85  }
86 
87  if (!opts->full_auxtrace)
88  return 0;
89 
90  /* We are in full trace mode but '-m,xyz' wasn't specified */
91  if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
92  if (privileged) {
93  opts->auxtrace_mmap_pages = MiB(4) / page_size;
94  } else {
95  opts->auxtrace_mmap_pages = KiB(128) / page_size;
96  if (opts->mmap_pages == UINT_MAX)
97  opts->mmap_pages = KiB(256) / page_size;
98  }
99  }
100 
101  /* Validate auxtrace_mmap_pages */
102  if (opts->auxtrace_mmap_pages) {
103  size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
104  size_t min_sz = KiB(8);
105 
106  if (sz < min_sz || !is_power_of_2(sz)) {
107  pr_err("Invalid mmap size for ARM SPE: must be at least %zuKiB and a power of 2\n",
108  min_sz / 1024);
109  return -EINVAL;
110  }
111  }
112 
113 
114  /*
115  * To obtain the auxtrace buffer file descriptor, the auxtrace event
116  * must come first.
117  */
118  perf_evlist__to_front(evlist, arm_spe_evsel);
119 
120  perf_evsel__set_sample_bit(arm_spe_evsel, CPU);
121  perf_evsel__set_sample_bit(arm_spe_evsel, TIME);
122  perf_evsel__set_sample_bit(arm_spe_evsel, TID);
123 
124  /* Add dummy event to keep tracking */
125  err = parse_events(evlist, "dummy:u", NULL);
126  if (err)
127  return err;
128 
129  tracking_evsel = perf_evlist__last(evlist);
130  perf_evlist__set_tracking_event(evlist, tracking_evsel);
131 
132  tracking_evsel->attr.freq = 0;
133  tracking_evsel->attr.sample_period = 1;
134  perf_evsel__set_sample_bit(tracking_evsel, TIME);
135  perf_evsel__set_sample_bit(tracking_evsel, CPU);
136  perf_evsel__reset_sample_bit(tracking_evsel, BRANCH_STACK);
137 
138  return 0;
139 }
140 
141 static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)
142 {
143  struct timespec ts;
144 
145  clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
146 
147  return ts.tv_sec ^ ts.tv_nsec;
148 }
149 
151 {
152  struct arm_spe_recording *sper =
153  container_of(itr, struct arm_spe_recording, itr);
154 
155  free(sper);
156 }
157 
158 static int arm_spe_read_finish(struct auxtrace_record *itr, int idx)
159 {
160  struct arm_spe_recording *sper =
161  container_of(itr, struct arm_spe_recording, itr);
162  struct perf_evsel *evsel;
163 
164  evlist__for_each_entry(sper->evlist, evsel) {
165  if (evsel->attr.type == sper->arm_spe_pmu->type)
167  evsel, idx);
168  }
169  return -EINVAL;
170 }
171 
173  struct perf_pmu *arm_spe_pmu)
174 {
175  struct arm_spe_recording *sper;
176 
177  if (!arm_spe_pmu) {
178  *err = -ENODEV;
179  return NULL;
180  }
181 
182  sper = zalloc(sizeof(struct arm_spe_recording));
183  if (!sper) {
184  *err = -ENOMEM;
185  return NULL;
186  }
187 
188  sper->arm_spe_pmu = arm_spe_pmu;
195  sper->itr.alignment = 0;
196 
197  return &sper->itr;
198 }
199 
200 struct perf_event_attr
202 {
203  struct perf_event_attr *attr;
204 
205  attr = zalloc(sizeof(struct perf_event_attr));
206  if (!attr) {
207  pr_err("arm_spe default config cannot allocate a perf_event_attr\n");
208  return NULL;
209  }
210 
211  /*
212  * If kernel driver doesn't advertise a minimum,
213  * use max allowable by PMSIDR_EL1.INTERVAL
214  */
215  if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu",
216  &attr->sample_period) != 1) {
217  pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n");
218  attr->sample_period = 4096;
219  }
220 
221  arm_spe_pmu->selectable = true;
222  arm_spe_pmu->is_uncore = false;
223 
224  return attr;
225 }
struct perf_evlist * evlist
Definition: session.h:25
static int arm_spe_read_finish(struct auxtrace_record *itr, int idx)
Definition: arm-spe.c:158
#define MiB(x)
Definition: arm-spe.c:24
static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)
Definition: arm-spe.c:141
unsigned int page_size
Definition: util.c:40
bool full_auxtrace
Definition: perf.h:55
int int err
Definition: 5sec.c:44
static void arm_spe_recording_free(struct auxtrace_record *itr)
Definition: arm-spe.c:150
int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,...)
Definition: pmu.c:1431
size_t(* info_priv_size)(struct auxtrace_record *itr, struct perf_evlist *evlist)
Definition: auxtrace.h:311
#define perf_evsel__set_sample_bit(evsel, bit)
Definition: evsel.h:260
struct auxtrace_record * arm_spe_recording_init(int *err, struct perf_pmu *arm_spe_pmu)
Definition: arm-spe.c:172
Definition: pmu.h:22
#define pr_err(fmt,...)
Definition: json.h:21
static int arm_spe_info_fill(struct auxtrace_record *itr, struct perf_session *session, struct auxtrace_info_event *auxtrace_info, size_t priv_size)
Definition: arm-spe.c:39
#define KiB(x)
Definition: arm-spe.c:23
int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *err)
void perf_evlist__set_tracking_event(struct perf_evlist *evlist, struct perf_evsel *tracking_evsel)
Definition: evlist.c:1697
struct auxtrace_record itr
Definition: arm-spe.c:27
int(* read_finish)(struct auxtrace_record *itr, int idx)
Definition: auxtrace.h:327
unsigned int alignment
Definition: auxtrace.h:328
u64(* reference)(struct auxtrace_record *itr)
Definition: auxtrace.h:326
#define pr_debug(fmt,...)
Definition: json.h:27
static struct perf_session * session
Definition: builtin-lock.c:34
#define evlist__for_each_entry(evlist, evsel)
Definition: evlist.h:247
struct perf_evlist * evlist
Definition: arm-spe.c:29
void(* free)(struct auxtrace_record *itr)
Definition: auxtrace.h:317
struct perf_event_attr * arm_spe_pmu_default_config(struct perf_pmu *arm_spe_pmu)
Definition: arm-spe.c:201
int(* recording_options)(struct auxtrace_record *itr, struct perf_evlist *evlist, struct record_opts *opts)
Definition: auxtrace.h:308
unsigned int mmap_pages
Definition: perf.h:67
#define CPU(he)
#define CLOCK_MONOTONIC_RAW
void perf_evlist__to_front(struct perf_evlist *evlist, struct perf_evsel *move_evsel)
Definition: evlist.c:1680
bool is_uncore
Definition: pmu.h:26
static int arm_spe_recording_options(struct auxtrace_record *itr, struct perf_evlist *evlist, struct record_opts *opts)
Definition: arm-spe.c:60
struct perf_pmu * arm_spe_pmu
Definition: arm-spe.c:28
int perf_event_paranoid(void)
Definition: util.c:388
int nr_mmaps
Definition: evlist.h:32
#define ARM_SPE_PMU_NAME
Definition: arm-spe.h:10
#define ARM_SPE_AUXTRACE_PRIV_SIZE
Definition: arm-spe.h:18
bool selectable
Definition: pmu.h:25
static size_t arm_spe_info_priv_size(struct auxtrace_record *itr __maybe_unused, struct perf_evlist *evlist __maybe_unused)
Definition: arm-spe.c:33
void free(void *)
#define perf_evsel__reset_sample_bit(evsel, bit)
Definition: evsel.h:263
Definition: attr.py:1
__u32 type
Definition: pmu.h:24
static struct perf_evsel * perf_evlist__last(struct perf_evlist *evlist)
Definition: evlist.h:220
unsigned int auxtrace_mmap_pages
Definition: perf.h:68
int(* info_fill)(struct auxtrace_record *itr, struct perf_session *session, struct auxtrace_info_event *auxtrace_info, size_t priv_size)
Definition: auxtrace.h:313
struct perf_event_attr attr
Definition: evsel.h:93
int perf_evlist__enable_event_idx(struct perf_evlist *evlist, struct perf_evsel *evsel, int idx)
Definition: evlist.c:422
void static void * zalloc(size_t size)
Definition: util.h:20