Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * auxtrace.c: AUX area trace support |
| 3 | * Copyright (c) 2013-2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include <stdbool.h> |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/perf_event.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/bitops.h> |
| 24 | #include <linux/log2.h> |
| 25 | |
| 26 | #include "../perf.h" |
| 27 | #include "util.h" |
| 28 | #include "evlist.h" |
| 29 | #include "cpumap.h" |
| 30 | #include "thread_map.h" |
| 31 | #include "asm/bug.h" |
| 32 | #include "auxtrace.h" |
| 33 | |
| 34 | int auxtrace_mmap__mmap(struct auxtrace_mmap *mm, |
| 35 | struct auxtrace_mmap_params *mp, |
| 36 | void *userpg, int fd) |
| 37 | { |
| 38 | struct perf_event_mmap_page *pc = userpg; |
| 39 | |
| 40 | #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT) |
| 41 | pr_err("Cannot use AUX area tracing mmaps\n"); |
| 42 | return -1; |
| 43 | #endif |
| 44 | |
| 45 | WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n"); |
| 46 | |
| 47 | mm->userpg = userpg; |
| 48 | mm->mask = mp->mask; |
| 49 | mm->len = mp->len; |
| 50 | mm->prev = 0; |
| 51 | mm->idx = mp->idx; |
| 52 | mm->tid = mp->tid; |
| 53 | mm->cpu = mp->cpu; |
| 54 | |
| 55 | if (!mp->len) { |
| 56 | mm->base = NULL; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | pc->aux_offset = mp->offset; |
| 61 | pc->aux_size = mp->len; |
| 62 | |
| 63 | mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset); |
| 64 | if (mm->base == MAP_FAILED) { |
| 65 | pr_debug2("failed to mmap AUX area\n"); |
| 66 | mm->base = NULL; |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | void auxtrace_mmap__munmap(struct auxtrace_mmap *mm) |
| 74 | { |
| 75 | if (mm->base) { |
| 76 | munmap(mm->base, mm->len); |
| 77 | mm->base = NULL; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp, |
| 82 | off_t auxtrace_offset, |
| 83 | unsigned int auxtrace_pages, |
| 84 | bool auxtrace_overwrite) |
| 85 | { |
| 86 | if (auxtrace_pages) { |
| 87 | mp->offset = auxtrace_offset; |
| 88 | mp->len = auxtrace_pages * (size_t)page_size; |
| 89 | mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0; |
| 90 | mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE); |
| 91 | pr_debug2("AUX area mmap length %zu\n", mp->len); |
| 92 | } else { |
| 93 | mp->len = 0; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, |
| 98 | struct perf_evlist *evlist, int idx, |
| 99 | bool per_cpu) |
| 100 | { |
| 101 | mp->idx = idx; |
| 102 | |
| 103 | if (per_cpu) { |
| 104 | mp->cpu = evlist->cpus->map[idx]; |
| 105 | if (evlist->threads) |
| 106 | mp->tid = evlist->threads->map[0]; |
| 107 | else |
| 108 | mp->tid = -1; |
| 109 | } else { |
| 110 | mp->cpu = -1; |
| 111 | mp->tid = evlist->threads->map[idx]; |
| 112 | } |
| 113 | } |