| 1 |
/* -*- linux-c -*- |
| 2 |
* Perf Functions |
| 3 |
* Copyright (C) 2006-2014 Red Hat Inc. |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef _PERF_C_ |
| 12 |
#define _PERF_C_ |
| 13 |
|
| 14 |
#include <linux/perf_event.h> |
| 15 |
#include <linux/workqueue.h> |
| 16 |
|
| 17 |
#include "perf.h" |
| 18 |
|
| 19 |
#ifndef INIT_WORK_ONSTACK |
| 20 |
#define INIT_WORK_ONSTACK(_work, _func) INIT_WORK((_work), (_func)) |
| 21 |
#define destroy_work_on_stack(_work) do { (void)(_work); } while (0) |
| 22 |
#endif |
| 23 |
|
| 24 |
/** @file perf.c |
| 25 |
* @brief Implements performance monitoring hardware support |
| 26 |
*/ |
| 27 |
|
| 28 |
/** Initialize performance sampling |
| 29 |
* Call this during probe initialization to set up performance event sampling |
| 30 |
* for all online cpus. Returns non-zero on error. |
| 31 |
* |
| 32 |
* @param stp Handle for the event to be registered. |
| 33 |
*/ |
| 34 |
static long _stp_perf_init (struct stap_perf_probe *stp, struct task_struct* task) |
| 35 |
{ |
| 36 |
int cpu; |
| 37 |
|
| 38 |
if (!stp->system_wide) { |
| 39 |
if (task == 0) /* need to setup later when we know the task */ |
| 40 |
return 0; |
| 41 |
else { |
| 42 |
if (stp->e.t.per_thread_event != 0) /* already setup */ |
| 43 |
return 0; |
| 44 |
stp->e.t.per_thread_event = perf_event_create_kernel_counter(&stp->attr, |
| 45 |
-1, |
| 46 |
#if defined(STAPCONF_PERF_STRUCTPID) || defined (STAPCONF_PERF_COUNTER_CONTEXT) |
| 47 |
task, |
| 48 |
#else |
| 49 |
task->pid, |
| 50 |
#endif |
| 51 |
stp->callback |
| 52 |
#ifdef STAPCONF_PERF_COUNTER_CONTEXT |
| 53 |
, NULL |
| 54 |
#endif |
| 55 |
); |
| 56 |
if (IS_ERR(stp->e.t.per_thread_event)) { |
| 57 |
long rc = PTR_ERR(stp->e.t.per_thread_event); |
| 58 |
stp->e.t.per_thread_event = NULL; |
| 59 |
|
| 60 |
/* |
| 61 |
* PPC returns ENXIO for HW counters until 2.6.37 |
| 62 |
* (behavior changed with commit b0a873e). |
| 63 |
*/ |
| 64 |
if (rc == -EINVAL || rc == -ENOSYS || rc == -ENOENT |
| 65 |
|| rc == -EOPNOTSUPP || rc == -ENXIO) { |
| 66 |
_stp_warn("perf probe '%s' is not supported by this kernel (%ld).", |
| 67 |
#ifdef STP_NEED_PROBE_NAME |
| 68 |
stp->probe->pn, |
| 69 |
#else |
| 70 |
stp->probe->pp, |
| 71 |
#endif |
| 72 |
rc); |
| 73 |
/* Lie and return 0. This way the more generic |
| 74 |
* task_finder warning won't be printed. */ |
| 75 |
rc = 0; |
| 76 |
} |
| 77 |
return rc; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
else { |
| 82 |
/* allocate space for the event descriptor for each cpu */ |
| 83 |
stp->e.events = _stp_alloc_percpu (sizeof(struct perf_event*)); |
| 84 |
if (stp->e.events == NULL) { |
| 85 |
return -ENOMEM; |
| 86 |
} |
| 87 |
|
| 88 |
/* initialize event on each processor */ |
| 89 |
for_each_possible_cpu(cpu) { |
| 90 |
struct perf_event **event = per_cpu_ptr (stp->e.events, cpu); |
| 91 |
if (cpu_is_offline(cpu)) { |
| 92 |
*event = NULL; |
| 93 |
continue; |
| 94 |
} |
| 95 |
*event = perf_event_create_kernel_counter(&stp->attr, |
| 96 |
cpu, |
| 97 |
#if defined(STAPCONF_PERF_STRUCTPID) || defined (STAPCONF_PERF_COUNTER_CONTEXT) |
| 98 |
NULL, |
| 99 |
#else |
| 100 |
-1, |
| 101 |
#endif |
| 102 |
stp->callback |
| 103 |
#ifdef STAPCONF_PERF_COUNTER_CONTEXT |
| 104 |
, NULL |
| 105 |
#endif |
| 106 |
); |
| 107 |
|
| 108 |
if (IS_ERR(*event)) { |
| 109 |
long rc = PTR_ERR(*event); |
| 110 |
*event = NULL; |
| 111 |
_stp_perf_del(stp); |
| 112 |
return rc; |
| 113 |
} |
| 114 |
} |
| 115 |
} /* (stp->system_wide) */ |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
/** Delete performance event. |
| 120 |
* Call this to shutdown one performance event sampling |
| 121 |
* |
| 122 |
* @param stp Handle for the event to be unregistered. |
| 123 |
*/ |
| 124 |
static void _stp_perf_del (struct stap_perf_probe *stp) |
| 125 |
{ |
| 126 |
int cpu; |
| 127 |
if (! stp || !stp->e.events) |
| 128 |
return; |
| 129 |
|
| 130 |
/* shut down performance event sampling */ |
| 131 |
if (stp->system_wide) { |
| 132 |
for_each_possible_cpu(cpu) { |
| 133 |
struct perf_event **event = per_cpu_ptr (stp->e.events, cpu); |
| 134 |
if (*event) { |
| 135 |
perf_event_release_kernel(*event); |
| 136 |
} |
| 137 |
} |
| 138 |
_stp_free_percpu (stp->e.events); |
| 139 |
stp->e.events = NULL; |
| 140 |
} |
| 141 |
else { |
| 142 |
if (stp->e.t.per_thread_event) { |
| 143 |
perf_event_release_kernel(stp->e.t.per_thread_event); |
| 144 |
} |
| 145 |
stp->e.t.per_thread_event = NULL; |
| 146 |
|
| 147 |
#ifdef STP_PERF_USE_TASK_FINDER |
| 148 |
if (stp->task_finder) { |
| 149 |
struct stap_task_finder_target *tgt = &stp->e.t.tgt; |
| 150 |
stap_cleanup_task_finder_target(tgt); |
| 151 |
} |
| 152 |
#endif |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** Delete many performance events in reverse order. |
| 157 |
* Call this to shutdown all performance event sampling |
| 158 |
* |
| 159 |
* @param probes A pointer array for the events to be unregistered. |
| 160 |
* @param n The number of events in the array. |
| 161 |
*/ |
| 162 |
static void _stp_perf_del_n (struct stap_perf_probe *probes, size_t n) |
| 163 |
{ |
| 164 |
while (n--) |
| 165 |
_stp_perf_del(&probes[n]); |
| 166 |
} |
| 167 |
|
| 168 |
struct _stp_perf_work { |
| 169 |
struct work_struct work; |
| 170 |
struct stap_perf_probe *probes; |
| 171 |
size_t nprobes; |
| 172 |
const char* probe_point; |
| 173 |
int rc; |
| 174 |
}; |
| 175 |
|
| 176 |
/** Initialize many performance events from a workqueue |
| 177 |
* Even though we're using the kernel interface, perf checks CAP_SYS_ADMIN, |
| 178 |
* which our mere @stapdev user may not have. By running via a workqueue, |
| 179 |
* we'll be in an events/X kernel thread with sufficient privileges. |
| 180 |
* |
| 181 |
* @param work The _stp_perf_work encapsulating _stp_perf_init_n parameters. |
| 182 |
*/ |
| 183 |
static void _stp_perf_init_work (struct work_struct *work) |
| 184 |
{ |
| 185 |
size_t i; |
| 186 |
struct _stp_perf_work *pwork = |
| 187 |
container_of(work, struct _stp_perf_work, work); |
| 188 |
|
| 189 |
for (i = 0; i < pwork->nprobes; ++i) { |
| 190 |
struct stap_perf_probe* stp = &pwork->probes[i]; |
| 191 |
|
| 192 |
if (stp->system_wide) |
| 193 |
pwork->rc = _stp_perf_init(stp, NULL); |
| 194 |
|
| 195 |
if (pwork->rc) { |
| 196 |
pwork->probe_point = stp->probe->pp; |
| 197 |
_stp_perf_del_n(pwork->probes, i); |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** Initialize many performance events |
| 204 |
* Call this to start all performance event sampling |
| 205 |
* |
| 206 |
* @param probes A pointer array for the events to be registered. |
| 207 |
* @param n The number of events in the array. |
| 208 |
* @param ppfail A pointer to return the probe_point on failure. |
| 209 |
*/ |
| 210 |
static int _stp_perf_init_n (struct stap_perf_probe *probes, size_t n, |
| 211 |
const char **ppfail) |
| 212 |
{ |
| 213 |
struct _stp_perf_work pwork = { .probes = probes, .nprobes = n }; |
| 214 |
int i; |
| 215 |
bool found_system_wide = false; |
| 216 |
|
| 217 |
for (i = 0; i < n; i++) { |
| 218 |
int rc = 0; |
| 219 |
struct stap_perf_probe* stp = &probes[i]; |
| 220 |
|
| 221 |
if (stp->system_wide) { |
| 222 |
/* postponed to work queue since the perf event registration |
| 223 |
* needs SYS_CAP_ADMIN which is missing in stapdev. */ |
| 224 |
found_system_wide = true; |
| 225 |
continue; |
| 226 |
} |
| 227 |
|
| 228 |
if (stp->task_finder) { |
| 229 |
#ifdef STP_PERF_USE_TASK_FINDER |
| 230 |
rc = stap_register_task_finder_target(&stp->e.t.tgt); |
| 231 |
#else |
| 232 |
rc = EINVAL; |
| 233 |
#endif |
| 234 |
} |
| 235 |
|
| 236 |
if (unlikely(rc != 0)) { |
| 237 |
*ppfail = stp->probe->pp; |
| 238 |
_stp_perf_del_n(probes, i); |
| 239 |
return rc; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if (found_system_wide) { |
| 244 |
INIT_WORK_ONSTACK(&pwork.work, _stp_perf_init_work); |
| 245 |
schedule_work(&pwork.work); |
| 246 |
flush_work(&pwork.work); |
| 247 |
if (unlikely(pwork.rc)) |
| 248 |
*ppfail = pwork.probe_point; |
| 249 |
destroy_work_on_stack(&pwork.work); |
| 250 |
return pwork.rc; |
| 251 |
} |
| 252 |
|
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/* |
| 258 |
The first call to _stp_perf_init, via systemtap_module_init at runtime, is for |
| 259 |
setting up aggregate counters. Per thread counters need to be setup when the |
| 260 |
thread is known. This is done by calling _stp_perf_init later when the thread |
| 261 |
is known. A per thread perf counter is defined by a counter("var") suffix on |
| 262 |
the perf probe. It is defined by perf_builder. This counter is read on demand |
| 263 |
via the "@perf("var")" builtin which is treated as an expression right hand side |
| 264 |
which reads the perf counter associated with the previously defined perf |
| 265 |
counter. It is expanded by dwarf_var_expanding_visitor |
| 266 |
*/ |
| 267 |
|
| 268 |
static int _stp_perf_read_init (unsigned i, struct task_struct* task) |
| 269 |
{ |
| 270 |
/* Choose the stap_perf_probes entry */ |
| 271 |
struct stap_perf_probe* stp = & stap_perf_probes[i]; |
| 272 |
|
| 273 |
return _stp_perf_init (stp, task); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
long _stp_perf_read (int ncpu, unsigned i) |
| 278 |
{ |
| 279 |
/* Choose the stap_perf_probes entry */ |
| 280 |
struct stap_perf_probe* stp; |
| 281 |
u64 enabled, running; |
| 282 |
|
| 283 |
if (i > sizeof(stap_perf_probes)/sizeof(struct stap_perf_probe)) |
| 284 |
{ |
| 285 |
_stp_error ("_stp_perf_read - out of range"); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
stp = & stap_perf_probes[i]; |
| 289 |
|
| 290 |
if (stp == NULL || stp->system_wide || stp->e.t.per_thread_event == NULL) |
| 291 |
{ |
| 292 |
_stp_error ("_stp_perf_read - probe '%s' is not supported by this kernel", |
| 293 |
#ifdef STP_NEED_PROBE_NAME |
| 294 |
(stp ? stp->probe->pn : "unknown") |
| 295 |
#else |
| 296 |
(stp ? stp->probe->pp : "unknown") |
| 297 |
#endif |
| 298 |
); |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
might_sleep(); |
| 303 |
return perf_event_read_value (stp->e.t.per_thread_event, &enabled, &running); |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
#endif /* _PERF_C_ */ |