blob: 2c340e7da458e4fa53b2c08914f1acd976fa90c5 [file] [log] [blame]
Thomas Gleixner6eda5832009-05-01 18:29:57 +02001#ifndef _PERF_PERF_H
2#define _PERF_PERF_H
3
Arnaldo Carvalho de Melo895f0ed2010-03-11 20:12:41 -03004struct winsize;
5
6void get_term_dimensions(struct winsize *ws);
7
David Howellsd2709c72012-11-19 22:21:03 +00008#include <asm/unistd.h>
9
Vince Weaver11d15782009-07-08 17:46:14 -040010#if defined(__i386__)
Vince Weaver11d15782009-07-08 17:46:14 -040011#define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
12#define cpu_relax() asm volatile("rep; nop" ::: "memory");
Stephane Eranianfbe96f22011-09-30 15:40:40 +020013#define CPUINFO_PROC "model name"
Ingo Molnareae7a752012-03-14 12:42:34 -030014#ifndef __NR_perf_event_open
15# define __NR_perf_event_open 336
16#endif
Vince Weaver11d15782009-07-08 17:46:14 -040017#endif
18
19#if defined(__x86_64__)
Peter Zijlstra1a482f32009-05-23 18:28:58 +020020#define rmb() asm volatile("lfence" ::: "memory")
21#define cpu_relax() asm volatile("rep; nop" ::: "memory");
Stephane Eranianfbe96f22011-09-30 15:40:40 +020022#define CPUINFO_PROC "model name"
Ingo Molnareae7a752012-03-14 12:42:34 -030023#ifndef __NR_perf_event_open
24# define __NR_perf_event_open 298
25#endif
Peter Zijlstra1a482f32009-05-23 18:28:58 +020026#endif
27
28#ifdef __powerpc__
Sukadev Bhattiprolu1483c2a2012-10-31 11:21:28 -070029#include "../../arch/powerpc/include/uapi/asm/unistd.h"
Peter Zijlstra1a482f32009-05-23 18:28:58 +020030#define rmb() asm volatile ("sync" ::: "memory")
31#define cpu_relax() asm volatile ("" ::: "memory");
Stephane Eranianfbe96f22011-09-30 15:40:40 +020032#define CPUINFO_PROC "cpu"
Peter Zijlstra1a482f32009-05-23 18:28:58 +020033#endif
34
Martin Schwidefsky12310e92009-06-22 12:08:22 +020035#ifdef __s390__
Martin Schwidefsky12310e92009-06-22 12:08:22 +020036#define rmb() asm volatile("bcr 15,0" ::: "memory")
37#define cpu_relax() asm volatile("" ::: "memory");
38#endif
39
Paul Mundtfebe8342009-06-25 14:41:57 +090040#ifdef __sh__
Paul Mundtfebe8342009-06-25 14:41:57 +090041#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 Eranianfbe96f22011-09-30 15:40:40 +020047#define CPUINFO_PROC "cpu type"
Paul Mundtfebe8342009-06-25 14:41:57 +090048#endif
49
Kyle McMartin2d4618d2009-06-23 21:38:49 -040050#ifdef __hppa__
Kyle McMartin2d4618d2009-06-23 21:38:49 -040051#define rmb() asm volatile("" ::: "memory")
52#define cpu_relax() asm volatile("" ::: "memory");
Stephane Eranianfbe96f22011-09-30 15:40:40 +020053#define CPUINFO_PROC "cpu"
Kyle McMartin2d4618d2009-06-23 21:38:49 -040054#endif
55
Jens Axboe825c9fb2009-09-04 02:56:22 -070056#ifdef __sparc__
Jens Axboe825c9fb2009-09-04 02:56:22 -070057#define rmb() asm volatile("":::"memory")
58#define cpu_relax() asm volatile("":::"memory")
Stephane Eranianfbe96f22011-09-30 15:40:40 +020059#define CPUINFO_PROC "cpu"
Jens Axboe825c9fb2009-09-04 02:56:22 -070060#endif
61
Michael Creefcd14b32009-10-26 21:32:06 +130062#ifdef __alpha__
Michael Creefcd14b32009-10-26 21:32:06 +130063#define rmb() asm volatile("mb" ::: "memory")
64#define cpu_relax() asm volatile("" ::: "memory")
Stephane Eranianfbe96f22011-09-30 15:40:40 +020065#define CPUINFO_PROC "cpu model"
Michael Creefcd14b32009-10-26 21:32:06 +130066#endif
67
Luck, Tony11ada262009-11-17 09:05:56 -080068#ifdef __ia64__
Luck, Tony11ada262009-11-17 09:05:56 -080069#define rmb() asm volatile ("mf" ::: "memory")
70#define cpu_relax() asm volatile ("hint @pause" ::: "memory")
Stephane Eranianfbe96f22011-09-30 15:40:40 +020071#define CPUINFO_PROC "model name"
Luck, Tony11ada262009-11-17 09:05:56 -080072#endif
73
Jamie Iles58e9f942009-12-11 12:20:09 +000074#ifdef __arm__
Jamie Iles58e9f942009-12-11 12:20:09 +000075/*
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 Deaconda7196e2010-03-03 11:47:58 +000079#define rmb() ((void(*)(void))0xffff0fa0)()
Jamie Iles58e9f942009-12-11 12:20:09 +000080#define cpu_relax() asm volatile("":::"memory")
Stephane Eranianfbe96f22011-09-30 15:40:40 +020081#define CPUINFO_PROC "Processor"
Jamie Iles58e9f942009-12-11 12:20:09 +000082#endif
83
Will Deacon03089682012-03-05 11:49:32 +000084#ifdef __aarch64__
Will Deacon03089682012-03-05 11:49:32 +000085#define rmb() asm volatile("dmb ld" ::: "memory")
86#define cpu_relax() asm volatile("yield" ::: "memory")
87#endif
88
Deng-Cheng Zhuc1e028e2010-10-12 19:33:33 +080089#ifdef __mips__
Deng-Cheng Zhuc1e028e2010-10-12 19:33:33 +080090#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 Eranianfbe96f22011-09-30 15:40:40 +020098#define CPUINFO_PROC "cpu model"
Deng-Cheng Zhuc1e028e2010-10-12 19:33:33 +080099#endif
100
Peter Zijlstra1a482f32009-05-23 18:28:58 +0200101#include <time.h>
102#include <unistd.h>
103#include <sys/types.h>
104#include <sys/syscall.h>
105
David Howellsd2709c72012-11-19 22:21:03 +0000106#include <linux/perf_event.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200107#include "util/types.h"
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300108#include <stdbool.h>
Peter Zijlstra1a482f32009-05-23 18:28:58 +0200109
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200110struct perf_mmap {
111 void *base;
112 int mask;
113 unsigned int prev;
114};
115
116static 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 Melo115d2d82011-01-12 17:11:53 -0200124static 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 Gleixner6eda5832009-05-01 18:29:57 +0200136/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200137 * prctl(PR_TASK_PERF_EVENTS_DISABLE) will (cheaply) disable all
Thomas Gleixner6eda5832009-05-01 18:29:57 +0200138 * counters in the current task.
139 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200140#define PR_TASK_PERF_EVENTS_DISABLE 31
141#define PR_TASK_PERF_EVENTS_ENABLE 32
Thomas Gleixner6eda5832009-05-01 18:29:57 +0200142
Thomas Gleixnera92e702372009-05-01 18:39:47 +0200143#ifndef NSEC_PER_SEC
144# define NSEC_PER_SEC 1000000000ULL
145#endif
146
147static 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 Gleixner6eda5832009-05-01 18:29:57 +0200154
155/*
156 * Pick up some kernel type conventions:
157 */
158#define __user
159#define asmlinkage
160
Thomas Gleixner6eda5832009-05-01 18:29:57 +0200161#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 Olsa52502bf2012-10-31 15:52:47 +0100168extern bool test_attr__enabled;
169void test_attr__init(void);
170void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
171 int fd, int group_fd, unsigned long flags);
172
Thomas Gleixner6eda5832009-05-01 18:29:57 +0200173static inline int
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200174sys_perf_event_open(struct perf_event_attr *attr,
Thomas Gleixner6eda5832009-05-01 18:29:57 +0200175 pid_t pid, int cpu, int group_fd,
176 unsigned long flags)
177{
Jiri Olsa52502bf2012-10-31 15:52:47 +0100178 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 Gleixner6eda5832009-05-01 18:29:57 +0200187}
188
Ingo Molnar85a9f922009-05-25 09:59:50 +0200189#define MAX_COUNTERS 256
190#define MAX_NR_CPUS 256
Thomas Gleixner6eda5832009-05-01 18:29:57 +0200191
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200192struct ip_callchain {
193 u64 nr;
194 u64 ips[0];
Peter Zijlstraf5970552009-06-18 23:22:55 +0200195};
196
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100197struct branch_flags {
198 u64 mispred:1;
199 u64 predicted:1;
200 u64 reserved:62;
201};
202
203struct branch_entry {
204 u64 from;
205 u64 to;
206 struct branch_flags flags;
207};
208
209struct branch_stack {
210 u64 nr;
211 struct branch_entry entries[0];
212};
213
Feng Tang70cb4e92012-10-30 11:56:02 +0800214extern const char *input_name;
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300215extern bool perf_host, perf_guest;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200216extern const char perf_version_string[];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800217
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300218void pthread__unblock_sigwinch(void);
219
Namhyung Kim12864b32012-04-26 14:15:22 +0900220#include "util/target.h"
Namhyung Kimbea03402012-04-26 14:15:15 +0900221
Jiri Olsa26d33022012-08-07 15:20:47 +0200222enum perf_call_graph_mode {
223 CALLCHAIN_NONE,
224 CALLCHAIN_FP,
225 CALLCHAIN_DWARF
226};
227
Namhyung Kimbea03402012-04-26 14:15:15 +0900228struct perf_record_opts {
229 struct perf_target target;
Jiri Olsa26d33022012-08-07 15:20:47 +0200230 int call_graph;
Arnaldo Carvalho de Meloed80f582011-11-11 15:12:56 -0200231 bool group;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200232 bool inherit_stat;
233 bool no_delay;
234 bool no_inherit;
235 bool no_samples;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200236 bool pipe_output;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200237 bool raw_samples;
238 bool sample_address;
239 bool sample_time;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200240 bool sample_id_all_missing;
Arnaldo Carvalho de Melobc76efe2012-02-14 14:05:30 -0200241 bool exclude_guest_missing;
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300242 bool period;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200243 unsigned int freq;
Arnaldo Carvalho de Melo01c2d992011-11-09 09:16:26 -0200244 unsigned int mmap_pages;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200245 unsigned int user_freq;
Stephane Eraniana00dc312012-05-25 23:13:44 +0200246 u64 branch_stack;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200247 u64 default_interval;
248 u64 user_interval;
Jiri Olsa26d33022012-08-07 15:20:47 +0200249 u16 stack_dump_size;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200250};
251
Thomas Gleixner6eda5832009-05-01 18:29:57 +0200252#endif