Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 1 | #ifndef _PERF_PERF_H |
| 2 | #define _PERF_PERF_H |
| 3 | |
Arnaldo Carvalho de Melo | 895f0ed | 2010-03-11 20:12:41 -0300 | [diff] [blame] | 4 | struct winsize; |
| 5 | |
| 6 | void get_term_dimensions(struct winsize *ws); |
| 7 | |
David Howells | d2709c7 | 2012-11-19 22:21:03 +0000 | [diff] [blame] | 8 | #include <asm/unistd.h> |
| 9 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 10 | #if defined(__i386__) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 11 | #define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory") |
| 12 | #define cpu_relax() asm volatile("rep; nop" ::: "memory"); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 13 | #define CPUINFO_PROC "model name" |
Ingo Molnar | eae7a75 | 2012-03-14 12:42:34 -0300 | [diff] [blame] | 14 | #ifndef __NR_perf_event_open |
| 15 | # define __NR_perf_event_open 336 |
| 16 | #endif |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 17 | #endif |
| 18 | |
| 19 | #if defined(__x86_64__) |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 20 | #define rmb() asm volatile("lfence" ::: "memory") |
| 21 | #define cpu_relax() asm volatile("rep; nop" ::: "memory"); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 22 | #define CPUINFO_PROC "model name" |
Ingo Molnar | eae7a75 | 2012-03-14 12:42:34 -0300 | [diff] [blame] | 23 | #ifndef __NR_perf_event_open |
| 24 | # define __NR_perf_event_open 298 |
| 25 | #endif |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #ifdef __powerpc__ |
Sukadev Bhattiprolu | 1483c2a | 2012-10-31 11:21:28 -0700 | [diff] [blame] | 29 | #include "../../arch/powerpc/include/uapi/asm/unistd.h" |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 30 | #define rmb() asm volatile ("sync" ::: "memory") |
| 31 | #define cpu_relax() asm volatile ("" ::: "memory"); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 32 | #define CPUINFO_PROC "cpu" |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 33 | #endif |
| 34 | |
Martin Schwidefsky | 12310e9 | 2009-06-22 12:08:22 +0200 | [diff] [blame] | 35 | #ifdef __s390__ |
Martin Schwidefsky | 12310e9 | 2009-06-22 12:08:22 +0200 | [diff] [blame] | 36 | #define rmb() asm volatile("bcr 15,0" ::: "memory") |
| 37 | #define cpu_relax() asm volatile("" ::: "memory"); |
| 38 | #endif |
| 39 | |
Paul Mundt | febe834 | 2009-06-25 14:41:57 +0900 | [diff] [blame] | 40 | #ifdef __sh__ |
Paul Mundt | febe834 | 2009-06-25 14:41:57 +0900 | [diff] [blame] | 41 | #if defined(__SH4A__) || defined(__SH5__) |
| 42 | # define rmb() asm volatile("synco" ::: "memory") |
| 43 | #else |
| 44 | # define rmb() asm volatile("" ::: "memory") |
| 45 | #endif |
| 46 | #define cpu_relax() asm volatile("" ::: "memory") |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 47 | #define CPUINFO_PROC "cpu type" |
Paul Mundt | febe834 | 2009-06-25 14:41:57 +0900 | [diff] [blame] | 48 | #endif |
| 49 | |
Kyle McMartin | 2d4618d | 2009-06-23 21:38:49 -0400 | [diff] [blame] | 50 | #ifdef __hppa__ |
Kyle McMartin | 2d4618d | 2009-06-23 21:38:49 -0400 | [diff] [blame] | 51 | #define rmb() asm volatile("" ::: "memory") |
| 52 | #define cpu_relax() asm volatile("" ::: "memory"); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 53 | #define CPUINFO_PROC "cpu" |
Kyle McMartin | 2d4618d | 2009-06-23 21:38:49 -0400 | [diff] [blame] | 54 | #endif |
| 55 | |
Jens Axboe | 825c9fb | 2009-09-04 02:56:22 -0700 | [diff] [blame] | 56 | #ifdef __sparc__ |
Jens Axboe | 825c9fb | 2009-09-04 02:56:22 -0700 | [diff] [blame] | 57 | #define rmb() asm volatile("":::"memory") |
| 58 | #define cpu_relax() asm volatile("":::"memory") |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 59 | #define CPUINFO_PROC "cpu" |
Jens Axboe | 825c9fb | 2009-09-04 02:56:22 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
Michael Cree | fcd14b3 | 2009-10-26 21:32:06 +1300 | [diff] [blame] | 62 | #ifdef __alpha__ |
Michael Cree | fcd14b3 | 2009-10-26 21:32:06 +1300 | [diff] [blame] | 63 | #define rmb() asm volatile("mb" ::: "memory") |
| 64 | #define cpu_relax() asm volatile("" ::: "memory") |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 65 | #define CPUINFO_PROC "cpu model" |
Michael Cree | fcd14b3 | 2009-10-26 21:32:06 +1300 | [diff] [blame] | 66 | #endif |
| 67 | |
Luck, Tony | 11ada26 | 2009-11-17 09:05:56 -0800 | [diff] [blame] | 68 | #ifdef __ia64__ |
Luck, Tony | 11ada26 | 2009-11-17 09:05:56 -0800 | [diff] [blame] | 69 | #define rmb() asm volatile ("mf" ::: "memory") |
| 70 | #define cpu_relax() asm volatile ("hint @pause" ::: "memory") |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 71 | #define CPUINFO_PROC "model name" |
Luck, Tony | 11ada26 | 2009-11-17 09:05:56 -0800 | [diff] [blame] | 72 | #endif |
| 73 | |
Jamie Iles | 58e9f94 | 2009-12-11 12:20:09 +0000 | [diff] [blame] | 74 | #ifdef __arm__ |
Jamie Iles | 58e9f94 | 2009-12-11 12:20:09 +0000 | [diff] [blame] | 75 | /* |
| 76 | * Use the __kuser_memory_barrier helper in the CPU helper page. See |
| 77 | * arch/arm/kernel/entry-armv.S in the kernel source for details. |
| 78 | */ |
Will Deacon | da7196e | 2010-03-03 11:47:58 +0000 | [diff] [blame] | 79 | #define rmb() ((void(*)(void))0xffff0fa0)() |
Jamie Iles | 58e9f94 | 2009-12-11 12:20:09 +0000 | [diff] [blame] | 80 | #define cpu_relax() asm volatile("":::"memory") |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 81 | #define CPUINFO_PROC "Processor" |
Jamie Iles | 58e9f94 | 2009-12-11 12:20:09 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
Will Deacon | 0308968 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 84 | #ifdef __aarch64__ |
Will Deacon | 0308968 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 85 | #define rmb() asm volatile("dmb ld" ::: "memory") |
| 86 | #define cpu_relax() asm volatile("yield" ::: "memory") |
| 87 | #endif |
| 88 | |
Deng-Cheng Zhu | c1e028e | 2010-10-12 19:33:33 +0800 | [diff] [blame] | 89 | #ifdef __mips__ |
Deng-Cheng Zhu | c1e028e | 2010-10-12 19:33:33 +0800 | [diff] [blame] | 90 | #define rmb() asm volatile( \ |
| 91 | ".set mips2\n\t" \ |
| 92 | "sync\n\t" \ |
| 93 | ".set mips0" \ |
| 94 | : /* no output */ \ |
| 95 | : /* no input */ \ |
| 96 | : "memory") |
| 97 | #define cpu_relax() asm volatile("" ::: "memory") |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 98 | #define CPUINFO_PROC "cpu model" |
Deng-Cheng Zhu | c1e028e | 2010-10-12 19:33:33 +0800 | [diff] [blame] | 99 | #endif |
| 100 | |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 101 | #include <time.h> |
| 102 | #include <unistd.h> |
| 103 | #include <sys/types.h> |
| 104 | #include <sys/syscall.h> |
| 105 | |
David Howells | d2709c7 | 2012-11-19 22:21:03 +0000 | [diff] [blame] | 106 | #include <linux/perf_event.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 107 | #include "util/types.h" |
Arnaldo Carvalho de Melo | 8035458 | 2010-05-17 15:51:10 -0300 | [diff] [blame] | 108 | #include <stdbool.h> |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 109 | |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 110 | struct perf_mmap { |
| 111 | void *base; |
| 112 | int mask; |
| 113 | unsigned int prev; |
| 114 | }; |
| 115 | |
| 116 | static inline unsigned int perf_mmap__read_head(struct perf_mmap *mm) |
| 117 | { |
| 118 | struct perf_event_mmap_page *pc = mm->base; |
| 119 | int head = pc->data_head; |
| 120 | rmb(); |
| 121 | return head; |
| 122 | } |
| 123 | |
Arnaldo Carvalho de Melo | 115d2d8 | 2011-01-12 17:11:53 -0200 | [diff] [blame] | 124 | static inline void perf_mmap__write_tail(struct perf_mmap *md, |
| 125 | unsigned long tail) |
| 126 | { |
| 127 | struct perf_event_mmap_page *pc = md->base; |
| 128 | |
| 129 | /* |
| 130 | * ensure all reads are done before we write the tail out. |
| 131 | */ |
| 132 | /* mb(); */ |
| 133 | pc->data_tail = tail; |
| 134 | } |
| 135 | |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 136 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 137 | * prctl(PR_TASK_PERF_EVENTS_DISABLE) will (cheaply) disable all |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 138 | * counters in the current task. |
| 139 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 140 | #define PR_TASK_PERF_EVENTS_DISABLE 31 |
| 141 | #define PR_TASK_PERF_EVENTS_ENABLE 32 |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 142 | |
Thomas Gleixner | a92e70237 | 2009-05-01 18:39:47 +0200 | [diff] [blame] | 143 | #ifndef NSEC_PER_SEC |
| 144 | # define NSEC_PER_SEC 1000000000ULL |
| 145 | #endif |
| 146 | |
| 147 | static inline unsigned long long rdclock(void) |
| 148 | { |
| 149 | struct timespec ts; |
| 150 | |
| 151 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 152 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 153 | } |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * Pick up some kernel type conventions: |
| 157 | */ |
| 158 | #define __user |
| 159 | #define asmlinkage |
| 160 | |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 161 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 162 | #define min(x, y) ({ \ |
| 163 | typeof(x) _min1 = (x); \ |
| 164 | typeof(y) _min2 = (y); \ |
| 165 | (void) (&_min1 == &_min2); \ |
| 166 | _min1 < _min2 ? _min1 : _min2; }) |
| 167 | |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 168 | extern bool test_attr__enabled; |
| 169 | void test_attr__init(void); |
| 170 | void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu, |
| 171 | int fd, int group_fd, unsigned long flags); |
| 172 | |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 173 | static inline int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 174 | sys_perf_event_open(struct perf_event_attr *attr, |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 175 | pid_t pid, int cpu, int group_fd, |
| 176 | unsigned long flags) |
| 177 | { |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 178 | int fd; |
| 179 | |
| 180 | fd = syscall(__NR_perf_event_open, attr, pid, cpu, |
| 181 | group_fd, flags); |
| 182 | |
| 183 | if (unlikely(test_attr__enabled)) |
| 184 | test_attr__open(attr, pid, cpu, fd, group_fd, flags); |
| 185 | |
| 186 | return fd; |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Ingo Molnar | 85a9f92 | 2009-05-25 09:59:50 +0200 | [diff] [blame] | 189 | #define MAX_COUNTERS 256 |
| 190 | #define MAX_NR_CPUS 256 |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 191 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 192 | struct ip_callchain { |
| 193 | u64 nr; |
| 194 | u64 ips[0]; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 195 | }; |
| 196 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 197 | struct branch_flags { |
| 198 | u64 mispred:1; |
| 199 | u64 predicted:1; |
| 200 | u64 reserved:62; |
| 201 | }; |
| 202 | |
| 203 | struct branch_entry { |
| 204 | u64 from; |
| 205 | u64 to; |
| 206 | struct branch_flags flags; |
| 207 | }; |
| 208 | |
| 209 | struct branch_stack { |
| 210 | u64 nr; |
| 211 | struct branch_entry entries[0]; |
| 212 | }; |
| 213 | |
Feng Tang | 70cb4e9 | 2012-10-30 11:56:02 +0800 | [diff] [blame] | 214 | extern const char *input_name; |
Arnaldo Carvalho de Melo | 8035458 | 2010-05-17 15:51:10 -0300 | [diff] [blame] | 215 | extern bool perf_host, perf_guest; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 216 | extern const char perf_version_string[]; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 217 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 218 | void pthread__unblock_sigwinch(void); |
| 219 | |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 220 | #include "util/target.h" |
Namhyung Kim | bea0340 | 2012-04-26 14:15:15 +0900 | [diff] [blame] | 221 | |
Jiri Olsa | 26d3302 | 2012-08-07 15:20:47 +0200 | [diff] [blame] | 222 | enum perf_call_graph_mode { |
| 223 | CALLCHAIN_NONE, |
| 224 | CALLCHAIN_FP, |
| 225 | CALLCHAIN_DWARF |
| 226 | }; |
| 227 | |
Namhyung Kim | bea0340 | 2012-04-26 14:15:15 +0900 | [diff] [blame] | 228 | struct perf_record_opts { |
| 229 | struct perf_target target; |
Jiri Olsa | 26d3302 | 2012-08-07 15:20:47 +0200 | [diff] [blame] | 230 | int call_graph; |
Arnaldo Carvalho de Melo | ed80f58 | 2011-11-11 15:12:56 -0200 | [diff] [blame] | 231 | bool group; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 232 | bool inherit_stat; |
| 233 | bool no_delay; |
| 234 | bool no_inherit; |
| 235 | bool no_samples; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 236 | bool pipe_output; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 237 | bool raw_samples; |
| 238 | bool sample_address; |
| 239 | bool sample_time; |
Arnaldo Carvalho de Melo | 808e122 | 2012-02-14 14:18:57 -0200 | [diff] [blame] | 240 | bool sample_id_all_missing; |
Arnaldo Carvalho de Melo | bc76efe | 2012-02-14 14:05:30 -0200 | [diff] [blame] | 241 | bool exclude_guest_missing; |
Andrew Vagin | 3e76ac7 | 2011-12-20 17:32:45 +0300 | [diff] [blame] | 242 | bool period; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 243 | unsigned int freq; |
Arnaldo Carvalho de Melo | 01c2d99 | 2011-11-09 09:16:26 -0200 | [diff] [blame] | 244 | unsigned int mmap_pages; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 245 | unsigned int user_freq; |
Stephane Eranian | a00dc31 | 2012-05-25 23:13:44 +0200 | [diff] [blame] | 246 | u64 branch_stack; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 247 | u64 default_interval; |
| 248 | u64 user_interval; |
Jiri Olsa | 26d3302 | 2012-08-07 15:20:47 +0200 | [diff] [blame] | 249 | u16 stack_dump_size; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 250 | }; |
| 251 | |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 252 | #endif |