| 1 | /* |
| 2 | * lttng-context-perf-counters.c |
| 3 | * |
| 4 | * LTTng UST performance monitoring counters (perf-counters) integration. |
| 5 | * |
| 6 | * Copyright (C) 2009-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <sys/syscall.h> |
| 30 | #include <linux/perf_event.h> |
| 31 | #include <lttng/ust-events.h> |
| 32 | #include <lttng/ust-tracer.h> |
| 33 | #include <lttng/ringbuffer-config.h> |
| 34 | #include <urcu/system.h> |
| 35 | #include <urcu/arch.h> |
| 36 | #include <urcu/rculist.h> |
| 37 | #include <helper.h> |
| 38 | #include <urcu/ref.h> |
| 39 | #include <usterr-signal-safe.h> |
| 40 | #include <signal.h> |
| 41 | #include "lttng-tracer-core.h" |
| 42 | |
| 43 | /* |
| 44 | * We use a global perf counter key and iterate on per-thread RCU lists |
| 45 | * of fields in the fast path, even though this is not strictly speaking |
| 46 | * what would provide the best fast-path complexity, to ensure teardown |
| 47 | * of sessions vs thread exit is handled racelessly. |
| 48 | * |
| 49 | * Updates and traversals of thread_list are protected by UST lock. |
| 50 | * Updates to rcu_field_list are protected by UST lock. |
| 51 | */ |
| 52 | |
| 53 | struct lttng_perf_counter_thread_field { |
| 54 | struct lttng_perf_counter_field *field; /* Back reference */ |
| 55 | struct perf_event_mmap_page *pc; |
| 56 | struct cds_list_head thread_field_node; /* Per-field list of thread fields (node) */ |
| 57 | struct cds_list_head rcu_field_node; /* RCU per-thread list of fields (node) */ |
| 58 | }; |
| 59 | |
| 60 | struct lttng_perf_counter_thread { |
| 61 | struct cds_list_head rcu_field_list; /* RCU per-thread list of fields */ |
| 62 | }; |
| 63 | |
| 64 | struct lttng_perf_counter_field { |
| 65 | struct perf_event_attr attr; |
| 66 | struct cds_list_head thread_field_list; /* Per-field list of thread fields */ |
| 67 | }; |
| 68 | |
| 69 | static pthread_key_t perf_counter_key; |
| 70 | |
| 71 | static |
| 72 | size_t perf_counter_get_size(size_t offset) |
| 73 | { |
| 74 | size_t size = 0; |
| 75 | |
| 76 | size += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 77 | size += sizeof(uint64_t); |
| 78 | return size; |
| 79 | } |
| 80 | |
| 81 | #if defined(__x86_64__) || defined(__i386__) |
| 82 | |
| 83 | static |
| 84 | uint64_t rdpmc(unsigned int counter) |
| 85 | { |
| 86 | unsigned int low, high; |
| 87 | |
| 88 | asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter)); |
| 89 | |
| 90 | return low | ((uint64_t) high) << 32; |
| 91 | } |
| 92 | |
| 93 | #else /* defined(__x86_64__) || defined(__i386__) */ |
| 94 | |
| 95 | #error "Perf event counters are only supported on x86 so far." |
| 96 | |
| 97 | #endif /* #else defined(__x86_64__) || defined(__i386__) */ |
| 98 | |
| 99 | static |
| 100 | uint64_t read_perf_counter(struct perf_event_mmap_page *pc) |
| 101 | { |
| 102 | uint32_t seq, idx; |
| 103 | uint64_t count; |
| 104 | |
| 105 | if (caa_unlikely(!pc)) |
| 106 | return 0; |
| 107 | |
| 108 | do { |
| 109 | seq = CMM_LOAD_SHARED(pc->lock); |
| 110 | cmm_barrier(); |
| 111 | |
| 112 | idx = pc->index; |
| 113 | if (idx) |
| 114 | count = pc->offset + rdpmc(idx - 1); |
| 115 | else |
| 116 | count = 0; |
| 117 | |
| 118 | cmm_barrier(); |
| 119 | } while (CMM_LOAD_SHARED(pc->lock) != seq); |
| 120 | |
| 121 | return count; |
| 122 | } |
| 123 | |
| 124 | static |
| 125 | int sys_perf_event_open(struct perf_event_attr *attr, |
| 126 | pid_t pid, int cpu, int group_fd, |
| 127 | unsigned long flags) |
| 128 | { |
| 129 | return syscall(SYS_perf_event_open, attr, pid, cpu, |
| 130 | group_fd, flags); |
| 131 | } |
| 132 | |
| 133 | static |
| 134 | struct perf_event_mmap_page *setup_perf(struct perf_event_attr *attr) |
| 135 | { |
| 136 | void *perf_addr; |
| 137 | int fd, ret; |
| 138 | |
| 139 | fd = sys_perf_event_open(attr, 0, -1, -1, 0); |
| 140 | if (fd < 0) |
| 141 | return NULL; |
| 142 | |
| 143 | perf_addr = mmap(NULL, sizeof(struct perf_event_mmap_page), |
| 144 | PROT_READ, MAP_SHARED, fd, 0); |
| 145 | if (perf_addr == MAP_FAILED) |
| 146 | return NULL; |
| 147 | ret = close(fd); |
| 148 | if (ret) { |
| 149 | perror("Error closing LTTng-UST perf memory mapping FD"); |
| 150 | } |
| 151 | return perf_addr; |
| 152 | } |
| 153 | |
| 154 | static |
| 155 | void unmap_perf_page(struct perf_event_mmap_page *pc) |
| 156 | { |
| 157 | int ret; |
| 158 | |
| 159 | if (!pc) |
| 160 | return; |
| 161 | ret = munmap(pc, sizeof(struct perf_event_mmap_page)); |
| 162 | if (ret < 0) { |
| 163 | PERROR("Error in munmap"); |
| 164 | abort(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static |
| 169 | struct lttng_perf_counter_thread *alloc_perf_counter_thread(void) |
| 170 | { |
| 171 | struct lttng_perf_counter_thread *perf_thread; |
| 172 | sigset_t newmask, oldmask; |
| 173 | int ret; |
| 174 | |
| 175 | ret = sigfillset(&newmask); |
| 176 | if (ret) |
| 177 | abort(); |
| 178 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
| 179 | if (ret) |
| 180 | abort(); |
| 181 | /* Check again with signals disabled */ |
| 182 | perf_thread = pthread_getspecific(perf_counter_key); |
| 183 | if (perf_thread) |
| 184 | goto skip; |
| 185 | perf_thread = zmalloc(sizeof(*perf_thread)); |
| 186 | if (!perf_thread) |
| 187 | abort(); |
| 188 | CDS_INIT_LIST_HEAD(&perf_thread->rcu_field_list); |
| 189 | ret = pthread_setspecific(perf_counter_key, perf_thread); |
| 190 | if (ret) |
| 191 | abort(); |
| 192 | skip: |
| 193 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
| 194 | if (ret) |
| 195 | abort(); |
| 196 | return perf_thread; |
| 197 | } |
| 198 | |
| 199 | static |
| 200 | struct lttng_perf_counter_thread_field * |
| 201 | add_thread_field(struct lttng_perf_counter_field *perf_field, |
| 202 | struct lttng_perf_counter_thread *perf_thread) |
| 203 | { |
| 204 | struct lttng_perf_counter_thread_field *thread_field; |
| 205 | sigset_t newmask, oldmask; |
| 206 | int ret; |
| 207 | |
| 208 | ret = sigfillset(&newmask); |
| 209 | if (ret) |
| 210 | abort(); |
| 211 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
| 212 | if (ret) |
| 213 | abort(); |
| 214 | /* Check again with signals disabled */ |
| 215 | cds_list_for_each_entry_rcu(thread_field, &perf_thread->rcu_field_list, |
| 216 | rcu_field_node) { |
| 217 | if (thread_field->field == perf_field) |
| 218 | goto skip; |
| 219 | } |
| 220 | thread_field = zmalloc(sizeof(*thread_field)); |
| 221 | if (!thread_field) |
| 222 | abort(); |
| 223 | thread_field->field = perf_field; |
| 224 | thread_field->pc = setup_perf(&perf_field->attr); |
| 225 | /* Note: thread_field->pc can be NULL if setup_perf() fails. */ |
| 226 | ust_lock_nocheck(); |
| 227 | cds_list_add_rcu(&thread_field->rcu_field_node, |
| 228 | &perf_thread->rcu_field_list); |
| 229 | cds_list_add(&thread_field->thread_field_node, |
| 230 | &perf_field->thread_field_list); |
| 231 | ust_unlock(); |
| 232 | skip: |
| 233 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
| 234 | if (ret) |
| 235 | abort(); |
| 236 | return thread_field; |
| 237 | } |
| 238 | |
| 239 | static |
| 240 | struct lttng_perf_counter_thread_field * |
| 241 | get_thread_field(struct lttng_perf_counter_field *field) |
| 242 | { |
| 243 | struct lttng_perf_counter_thread *perf_thread; |
| 244 | struct lttng_perf_counter_thread_field *thread_field; |
| 245 | |
| 246 | perf_thread = pthread_getspecific(perf_counter_key); |
| 247 | if (!perf_thread) |
| 248 | perf_thread = alloc_perf_counter_thread(); |
| 249 | cds_list_for_each_entry_rcu(thread_field, &perf_thread->rcu_field_list, |
| 250 | rcu_field_node) { |
| 251 | if (thread_field->field == field) |
| 252 | return thread_field; |
| 253 | } |
| 254 | /* perf_counter_thread_field not found, need to add one */ |
| 255 | return add_thread_field(field, perf_thread); |
| 256 | } |
| 257 | |
| 258 | static |
| 259 | uint64_t wrapper_perf_counter_read(struct lttng_ctx_field *field) |
| 260 | { |
| 261 | struct lttng_perf_counter_field *perf_field; |
| 262 | struct lttng_perf_counter_thread_field *perf_thread_field; |
| 263 | |
| 264 | perf_field = field->u.perf_counter; |
| 265 | perf_thread_field = get_thread_field(perf_field); |
| 266 | return read_perf_counter(perf_thread_field->pc); |
| 267 | } |
| 268 | |
| 269 | static |
| 270 | void perf_counter_record(struct lttng_ctx_field *field, |
| 271 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 272 | struct lttng_channel *chan) |
| 273 | { |
| 274 | uint64_t value; |
| 275 | |
| 276 | value = wrapper_perf_counter_read(field); |
| 277 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(value)); |
| 278 | chan->ops->event_write(ctx, &value, sizeof(value)); |
| 279 | } |
| 280 | |
| 281 | static |
| 282 | void perf_counter_get_value(struct lttng_ctx_field *field, |
| 283 | union lttng_ctx_value *value) |
| 284 | { |
| 285 | uint64_t v; |
| 286 | |
| 287 | v = wrapper_perf_counter_read(field); |
| 288 | value->s64 = v; |
| 289 | } |
| 290 | |
| 291 | /* Called with UST lock held */ |
| 292 | static |
| 293 | void lttng_destroy_perf_thread_field( |
| 294 | struct lttng_perf_counter_thread_field *thread_field) |
| 295 | { |
| 296 | unmap_perf_page(thread_field->pc); |
| 297 | cds_list_del_rcu(&thread_field->rcu_field_node); |
| 298 | cds_list_del(&thread_field->thread_field_node); |
| 299 | free(thread_field); |
| 300 | } |
| 301 | |
| 302 | static |
| 303 | void lttng_destroy_perf_thread_key(void *_key) |
| 304 | { |
| 305 | struct lttng_perf_counter_thread *perf_thread = _key; |
| 306 | struct lttng_perf_counter_thread_field *pos, *p; |
| 307 | |
| 308 | ust_lock_nocheck(); |
| 309 | cds_list_for_each_entry_safe(pos, p, &perf_thread->rcu_field_list, |
| 310 | rcu_field_node) |
| 311 | lttng_destroy_perf_thread_field(pos); |
| 312 | ust_unlock(); |
| 313 | free(perf_thread); |
| 314 | } |
| 315 | |
| 316 | /* Called with UST lock held */ |
| 317 | static |
| 318 | void lttng_destroy_perf_counter_field(struct lttng_ctx_field *field) |
| 319 | { |
| 320 | struct lttng_perf_counter_field *perf_field; |
| 321 | struct lttng_perf_counter_thread_field *pos, *p; |
| 322 | |
| 323 | free((char *) field->event_field.name); |
| 324 | perf_field = field->u.perf_counter; |
| 325 | /* |
| 326 | * This put is performed when no threads can concurrently |
| 327 | * perform a "get" concurrently, thanks to urcu-bp grace |
| 328 | * period. |
| 329 | */ |
| 330 | cds_list_for_each_entry_safe(pos, p, &perf_field->thread_field_list, |
| 331 | thread_field_node) |
| 332 | lttng_destroy_perf_thread_field(pos); |
| 333 | free(perf_field); |
| 334 | } |
| 335 | |
| 336 | /* Called with UST lock held */ |
| 337 | int lttng_add_perf_counter_to_ctx(uint32_t type, |
| 338 | uint64_t config, |
| 339 | const char *name, |
| 340 | struct lttng_ctx **ctx) |
| 341 | { |
| 342 | struct lttng_ctx_field *field; |
| 343 | struct lttng_perf_counter_field *perf_field; |
| 344 | struct perf_event_mmap_page *tmp_pc; |
| 345 | char *name_alloc; |
| 346 | int ret; |
| 347 | |
| 348 | name_alloc = strdup(name); |
| 349 | if (!name_alloc) { |
| 350 | ret = -ENOMEM; |
| 351 | goto name_alloc_error; |
| 352 | } |
| 353 | perf_field = zmalloc(sizeof(*perf_field)); |
| 354 | if (!perf_field) { |
| 355 | ret = -ENOMEM; |
| 356 | goto perf_field_alloc_error; |
| 357 | } |
| 358 | field = lttng_append_context(ctx); |
| 359 | if (!field) { |
| 360 | ret = -ENOMEM; |
| 361 | goto append_context_error; |
| 362 | } |
| 363 | if (lttng_find_context(*ctx, name_alloc)) { |
| 364 | ret = -EEXIST; |
| 365 | goto find_error; |
| 366 | } |
| 367 | |
| 368 | field->destroy = lttng_destroy_perf_counter_field; |
| 369 | |
| 370 | field->event_field.name = name_alloc; |
| 371 | field->event_field.type.atype = atype_integer; |
| 372 | field->event_field.type.u.basic.integer.size = |
| 373 | sizeof(uint64_t) * CHAR_BIT; |
| 374 | field->event_field.type.u.basic.integer.alignment = |
| 375 | lttng_alignof(uint64_t) * CHAR_BIT; |
| 376 | field->event_field.type.u.basic.integer.signedness = |
| 377 | lttng_is_signed_type(uint64_t); |
| 378 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; |
| 379 | field->event_field.type.u.basic.integer.base = 10; |
| 380 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; |
| 381 | field->get_size = perf_counter_get_size; |
| 382 | field->record = perf_counter_record; |
| 383 | field->get_value = perf_counter_get_value; |
| 384 | |
| 385 | perf_field->attr.type = type; |
| 386 | perf_field->attr.config = config; |
| 387 | perf_field->attr.exclude_kernel = 1; |
| 388 | CDS_INIT_LIST_HEAD(&perf_field->thread_field_list); |
| 389 | field->u.perf_counter = perf_field; |
| 390 | |
| 391 | /* Ensure that this perf counter can be used in this process. */ |
| 392 | tmp_pc = setup_perf(&perf_field->attr); |
| 393 | if (!tmp_pc) { |
| 394 | ret = -ENODEV; |
| 395 | goto setup_error; |
| 396 | } |
| 397 | unmap_perf_page(tmp_pc); |
| 398 | |
| 399 | /* |
| 400 | * Contexts can only be added before tracing is started, so we |
| 401 | * don't have to synchronize against concurrent threads using |
| 402 | * the field here. |
| 403 | */ |
| 404 | |
| 405 | return 0; |
| 406 | |
| 407 | setup_error: |
| 408 | find_error: |
| 409 | lttng_remove_context_field(ctx, field); |
| 410 | append_context_error: |
| 411 | free(perf_field); |
| 412 | perf_field_alloc_error: |
| 413 | free(name_alloc); |
| 414 | name_alloc_error: |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | int lttng_perf_counter_init(void) |
| 419 | { |
| 420 | int ret; |
| 421 | |
| 422 | ret = pthread_key_create(&perf_counter_key, |
| 423 | lttng_destroy_perf_thread_key); |
| 424 | if (ret) |
| 425 | ret = -ret; |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | void lttng_perf_counter_exit(void) |
| 430 | { |
| 431 | int ret; |
| 432 | |
| 433 | ret = pthread_key_delete(perf_counter_key); |
| 434 | if (ret) { |
| 435 | errno = ret; |
| 436 | PERROR("Error in pthread_key_delete"); |
| 437 | } |
| 438 | } |