Wu Fengguang | cea92ce | 2009-03-20 10:08:02 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Ioctls that can be done on a perf counter fd: |
| 3 | */ |
| 4 | #define PERF_COUNTER_IOC_ENABLE _IO('$', 0) |
| 5 | #define PERF_COUNTER_IOC_DISABLE _IO('$', 1) |
| 6 | |
| 7 | /* |
| 8 | * prctl(PR_TASK_PERF_COUNTERS_DISABLE) will (cheaply) disable all |
| 9 | * counters in the current task. |
| 10 | */ |
| 11 | #define PR_TASK_PERF_COUNTERS_DISABLE 31 |
| 12 | #define PR_TASK_PERF_COUNTERS_ENABLE 32 |
| 13 | |
| 14 | #define MAX_COUNTERS 64 |
| 15 | #define MAX_NR_CPUS 256 |
| 16 | |
| 17 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 18 | |
| 19 | /* |
| 20 | * Pick up some kernel type conventions: |
| 21 | */ |
| 22 | #define __user |
| 23 | #define asmlinkage |
| 24 | |
| 25 | typedef unsigned int __u32; |
| 26 | typedef unsigned long long __u64; |
| 27 | typedef long long __s64; |
| 28 | |
| 29 | /* |
| 30 | * User-space ABI bits: |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Generalized performance counter event types, used by the hw_event.type |
| 35 | * parameter of the sys_perf_counter_open() syscall: |
| 36 | */ |
| 37 | enum hw_event_types { |
| 38 | /* |
| 39 | * Common hardware events, generalized by the kernel: |
| 40 | */ |
| 41 | PERF_COUNT_CPU_CYCLES = 0, |
| 42 | PERF_COUNT_INSTRUCTIONS = 1, |
| 43 | PERF_COUNT_CACHE_REFERENCES = 2, |
| 44 | PERF_COUNT_CACHE_MISSES = 3, |
| 45 | PERF_COUNT_BRANCH_INSTRUCTIONS = 4, |
| 46 | PERF_COUNT_BRANCH_MISSES = 5, |
| 47 | PERF_COUNT_BUS_CYCLES = 6, |
| 48 | |
| 49 | PERF_HW_EVENTS_MAX = 7, |
| 50 | |
| 51 | /* |
| 52 | * Special "software" counters provided by the kernel, even if |
| 53 | * the hardware does not support performance counters. These |
| 54 | * counters measure various physical and sw events of the |
| 55 | * kernel (and allow the profiling of them as well): |
| 56 | */ |
| 57 | PERF_COUNT_CPU_CLOCK = -1, |
| 58 | PERF_COUNT_TASK_CLOCK = -2, |
| 59 | PERF_COUNT_PAGE_FAULTS = -3, |
| 60 | PERF_COUNT_CONTEXT_SWITCHES = -4, |
| 61 | PERF_COUNT_CPU_MIGRATIONS = -5, |
| 62 | |
| 63 | PERF_SW_EVENTS_MIN = -6, |
| 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * IRQ-notification data record type: |
| 68 | */ |
| 69 | enum perf_counter_record_type { |
| 70 | PERF_RECORD_SIMPLE = 0, |
| 71 | PERF_RECORD_IRQ = 1, |
| 72 | PERF_RECORD_GROUP = 2, |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * Hardware event to monitor via a performance monitoring counter: |
| 77 | */ |
| 78 | struct perf_counter_hw_event { |
| 79 | __s64 type; |
| 80 | |
| 81 | __u64 irq_period; |
| 82 | __u64 record_type; |
| 83 | __u64 read_format; |
| 84 | |
| 85 | __u64 disabled : 1, /* off by default */ |
| 86 | nmi : 1, /* NMI sampling */ |
| 87 | raw : 1, /* raw event type */ |
| 88 | inherit : 1, /* children inherit it */ |
| 89 | pinned : 1, /* must always be on PMU */ |
| 90 | exclusive : 1, /* only group on PMU */ |
| 91 | exclude_user : 1, /* don't count user */ |
| 92 | exclude_kernel : 1, /* ditto kernel */ |
| 93 | exclude_hv : 1, /* ditto hypervisor */ |
| 94 | exclude_idle : 1, /* don't count when idle */ |
| 95 | |
| 96 | __reserved_1 : 54; |
| 97 | |
| 98 | __u32 extra_config_len; |
| 99 | __u32 __reserved_4; |
| 100 | |
| 101 | __u64 __reserved_2; |
| 102 | __u64 __reserved_3; |
| 103 | }; |
| 104 | |
| 105 | #ifdef __x86_64__ |
| 106 | # define __NR_perf_counter_open 295 |
| 107 | #endif |
| 108 | |
| 109 | #ifdef __i386__ |
| 110 | # define __NR_perf_counter_open 333 |
| 111 | #endif |
| 112 | |
| 113 | #ifdef __powerpc__ |
| 114 | #define __NR_perf_counter_open 319 |
| 115 | #endif |
| 116 | |
| 117 | asmlinkage int sys_perf_counter_open( |
| 118 | |
| 119 | struct perf_counter_hw_event *hw_event_uptr __user, |
| 120 | pid_t pid, |
| 121 | int cpu, |
| 122 | int group_fd, |
| 123 | unsigned long flags) |
| 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | ret = syscall( |
| 128 | __NR_perf_counter_open, hw_event_uptr, pid, cpu, group_fd, flags); |
| 129 | #if defined(__x86_64__) || defined(__i386__) |
| 130 | if (ret < 0 && ret > -4096) { |
| 131 | errno = -ret; |
| 132 | ret = -1; |
| 133 | } |
| 134 | #endif |
| 135 | return ret; |
| 136 | } |
| 137 | |