The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Binary implementation of the original opcontrol script due to missing tools |
| 19 | * like awk, test, etc. |
| 20 | */ |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #include <getopt.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Jean-Baptiste Queru | d850f37 | 2010-05-03 15:16:06 -0700 | [diff] [blame] | 26 | #include <string.h> |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <signal.h> |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 30 | #include <dirent.h> |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 31 | #include <sys/stat.h> |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 34 | |
| 35 | #include "op_config.h" |
| 36 | |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 37 | #define verbose(fmt...) if (verbose_print) printf(fmt) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 38 | |
| 39 | /* Experiments found that using a small interval may hang the device, and the |
| 40 | * more events tracked simultaneously, the longer the interval has to be. |
| 41 | */ |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 42 | |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 43 | #if defined(__i386__) || defined(__x86_64__) |
| 44 | #define MAX_EVENTS 2 |
| 45 | int min_count[MAX_EVENTS] = {60000, 100000}; |
| 46 | #elif !defined(WITH_ARM_V7_A) |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 47 | #define MAX_EVENTS 3 |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 48 | int min_count[MAX_EVENTS] = {150000, 200000, 250000}; |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 49 | #else |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 50 | #define MAX_EVENTS 5 |
Ben Cheng | afec5b9 | 2010-02-22 15:33:08 -0800 | [diff] [blame] | 51 | int min_count[MAX_EVENTS] = {150000, 20000, 25000, 30000, 35000}; |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 52 | #endif |
| 53 | |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 54 | int verbose_print; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 55 | int list_events; |
| 56 | int show_usage; |
| 57 | int setup; |
| 58 | int quick; |
Ben Cheng | a9404b8 | 2010-09-03 16:46:45 -0700 | [diff] [blame] | 59 | int timer; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 60 | int num_events; |
| 61 | int start; |
| 62 | int stop; |
| 63 | int reset; |
| 64 | |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 65 | int selected_events[MAX_EVENTS]; |
| 66 | int selected_counts[MAX_EVENTS]; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 67 | |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 68 | char callgraph[8]; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 69 | char kernel_range[512]; |
| 70 | char vmlinux[512]; |
| 71 | |
| 72 | struct option long_options[] = { |
| 73 | {"help", 0, &show_usage, 1}, |
| 74 | {"list-events", 0, &list_events, 1}, |
| 75 | {"reset", 0, &reset, 1}, |
| 76 | {"setup", 0, &setup, 1}, |
| 77 | {"quick", 0, &quick, 1}, |
Ben Cheng | a9404b8 | 2010-09-03 16:46:45 -0700 | [diff] [blame] | 78 | {"timer", 0, &timer, 1}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 79 | {"callgraph", 1, 0, 'c'}, |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 80 | {"event", 1, 0, 'e'}, |
| 81 | {"vmlinux", 1, 0, 'v'}, |
| 82 | {"kernel-range", 1, 0, 'r'}, |
| 83 | {"start", 0, &start, 1}, |
| 84 | {"stop", 0, &stop, 1}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 85 | {"dump", 0, 0, 'd'}, |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 86 | {"shutdown", 0, 0, 'h'}, |
| 87 | {"status", 0, 0, 't'}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 88 | {"verbose", 0, 0, 'V'}, |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 89 | {"verbose-log", 1, 0, 'l'}, |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 90 | {0, 0, 0, 0}, |
| 91 | }; |
| 92 | |
| 93 | struct event_info { |
| 94 | int id; |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 95 | int um; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 96 | const char *name; |
| 97 | const char *explanation; |
| 98 | } event_info[] = { |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 99 | #if defined(__i386__) || defined(__x86_64__) |
| 100 | /* INTEL_ARCH_PERFMON events */ |
| 101 | |
| 102 | /* 0x3c counters:cpuid um:zero minimum:6000 filter:0 name:CPU_CLK_UNHALTED : |
| 103 | * Clock cycles when not halted |
| 104 | */ |
| 105 | {0x3c, 0, "CPU_CLK_UNHALTED", |
| 106 | "Clock cycles when not halted" }, |
| 107 | |
| 108 | /* event:0x3c counters:cpuid um:one minimum:6000 filter:2 name:UNHALTED_REFERENCE_CYCLES : |
| 109 | * Unhalted reference cycles |
| 110 | */ |
| 111 | {0x3c, 1, "UNHALTED_REFERENCE_CYCLES", |
| 112 | "Unhalted reference cycles" }, |
| 113 | |
| 114 | /* event:0xc0 counters:cpuid um:zero minimum:6000 filter:1 name:INST_RETIRED : |
| 115 | * number of instructions retired |
| 116 | */ |
| 117 | {0xc0, 0, "INST_RETIRED", |
| 118 | "number of instructions retired"}, |
| 119 | |
| 120 | /* event:0x2e counters:cpuid um:x41 minimum:6000 filter:5 name:LLC_MISSES : |
| 121 | * Last level cache demand requests from this core that missed the LLC |
| 122 | */ |
| 123 | {0x2e, 0x41, "LLC_MISSES", |
| 124 | "Last level cache demand requests from this core that missed the LLC"}, |
| 125 | |
| 126 | /* event:0x2e counters:cpuid um:x4f minimum:6000 filter:4 name:LLC_REFS : |
| 127 | * Last level cache demand requests from this core |
| 128 | */ |
| 129 | {0x2e, 0x4f, "LLC_REFS", |
| 130 | "Last level cache demand requests from this core"}, |
| 131 | |
| 132 | /* event:0xc4 counters:cpuid um:zero minimum:500 filter:6 name:BR_INST_RETIRED : |
| 133 | * number of branch instructions retired |
| 134 | */ |
| 135 | {0xc4, 0, "BR_INST_RETIRED", |
| 136 | "number of branch instructions retired"}, |
| 137 | |
| 138 | /* event:0xc5 counters:cpuid um:zero minimum:500 filter:7 name:BR_MISS_PRED_RETIRED : |
| 139 | * number of mispredicted branches retired (precise) |
| 140 | */ |
| 141 | {0xc5, 0, "BR_MISS_PRED_RETIRED", |
| 142 | "number of mispredicted branches retired (precise)"}, |
| 143 | |
| 144 | #elif !defined(WITH_ARM_V7_A) |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 145 | /* ARM V6 events */ |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 146 | {0x00, 0, "IFU_IFETCH_MISS", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 147 | "number of instruction fetch misses"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 148 | {0x01, 0, "CYCLES_IFU_MEM_STALL", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 149 | "cycles instruction fetch pipe is stalled"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 150 | {0x02, 0, "CYCLES_DATA_STALL", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 151 | "cycles stall occurs for due to data dependency"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 152 | {0x03, 0, "ITLB_MISS", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 153 | "number of Instruction MicroTLB misses"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 154 | {0x04, 0, "DTLB_MISS", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 155 | "number of Data MicroTLB misses"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 156 | {0x05, 0, "BR_INST_EXECUTED", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 157 | "branch instruction executed w/ or w/o program flow change"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 158 | {0x06, 0, "BR_INST_MISS_PRED", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 159 | "branch mispredicted"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 160 | {0x07, 0, "INSN_EXECUTED", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 161 | "instructions executed"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 162 | {0x09, 0, "DCACHE_ACCESS", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 163 | "data cache access, cacheable locations"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 164 | {0x0a, 0, "DCACHE_ACCESS_ALL", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 165 | "data cache access, all locations"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 166 | {0x0b, 0, "DCACHE_MISS", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 167 | "data cache miss"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 168 | {0x0c, 0, "DCACHE_WB", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 169 | "data cache writeback, 1 event for every half cacheline"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 170 | {0x0d, 0, "PC_CHANGE", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 171 | "number of times the program counter was changed without a mode switch"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 172 | {0x0f, 0, "TLB_MISS", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 173 | "Main TLB miss"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 174 | {0x10, 0, "EXP_EXTERNAL", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 175 | "Explicit external data access"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 176 | {0x11, 0, "LSU_STALL", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 177 | "cycles stalled because Load Store request queue is full"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 178 | {0x12, 0, "WRITE_DRAIN", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 179 | "Times write buffer was drained"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 180 | {0xff, 0, "CPU_CYCLES", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 181 | "clock cycles counter"}, |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 182 | #else |
| 183 | /* ARM V7 events */ |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 184 | {0x00, 0, "PMNC_SW_INCR", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 185 | "Software increment of PMNC registers"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 186 | {0x01, 0, "IFETCH_MISS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 187 | "Instruction fetch misses from cache or normal cacheable memory"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 188 | {0x02, 0, "ITLB_MISS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 189 | "Instruction fetch misses from TLB"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 190 | {0x03, 0, "DCACHE_REFILL", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 191 | "Data R/W operation that causes a refill from cache or normal cacheable" |
| 192 | "memory"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 193 | {0x04, 0, "DCACHE_ACCESS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 194 | "Data R/W from cache"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 195 | {0x05, 0, "DTLB_REFILL", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 196 | "Data R/W that causes a TLB refill"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 197 | {0x06, 0, "DREAD", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 198 | "Data read architecturally executed (note: architecturally executed = for" |
| 199 | "instructions that are unconditional or that pass the condition code)"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 200 | {0x07, 0, "DWRITE", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 201 | "Data write architecturally executed"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 202 | {0x08, 0, "INSTR_EXECUTED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 203 | "All executed instructions"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 204 | {0x09, 0, "EXC_TAKEN", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 205 | "Exception taken"}, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 206 | {0x0A, 0, "EXC_EXECUTED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 207 | "Exception return architecturally executed"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 208 | {0x0B, 0, "CID_WRITE", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 209 | "Instruction that writes to the Context ID Register architecturally" |
| 210 | "executed"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 211 | {0x0C, 0, "PC_WRITE", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 212 | "SW change of PC, architecturally executed (not by exceptions)"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 213 | {0x0D, 0, "PC_IMM_BRANCH", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 214 | "Immediate branch instruction executed (taken or not)"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 215 | {0x0E, 0, "PC_PROC_RETURN", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 216 | "Procedure return architecturally executed (not by exceptions)"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 217 | {0x0F, 0, "UNALIGNED_ACCESS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 218 | "Unaligned access architecturally executed"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 219 | {0x10, 0, "PC_BRANCH_MIS_PRED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 220 | "Branch mispredicted or not predicted. Counts pipeline flushes because of" |
| 221 | "misprediction"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 222 | {0x12, 0, "PC_BRANCH_MIS_USED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 223 | "Branch or change in program flow that could have been predicted"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 224 | {0x40, 0, "WRITE_BUFFER_FULL", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 225 | "Any write buffer full cycle"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 226 | {0x41, 0, "L2_STORE_MERGED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 227 | "Any store that is merged in L2 cache"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 228 | {0x42, 0, "L2_STORE_BUFF", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 229 | "Any bufferable store from load/store to L2 cache"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 230 | {0x43, 0, "L2_ACCESS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 231 | "Any access to L2 cache"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 232 | {0x44, 0, "L2_CACH_MISS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 233 | "Any cacheable miss in L2 cache"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 234 | {0x45, 0, "AXI_READ_CYCLES", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 235 | "Number of cycles for an active AXI read"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 236 | {0x46, 0, "AXI_WRITE_CYCLES", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 237 | "Number of cycles for an active AXI write"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 238 | {0x47, 0, "MEMORY_REPLAY", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 239 | "Any replay event in the memory subsystem"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 240 | {0x48, 0, "UNALIGNED_ACCESS_REPLAY", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 241 | "Unaligned access that causes a replay"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 242 | {0x49, 0, "L1_DATA_MISS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 243 | "L1 data cache miss as a result of the hashing algorithm"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 244 | {0x4A, 0, "L1_INST_MISS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 245 | "L1 instruction cache miss as a result of the hashing algorithm"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 246 | {0x4B, 0, "L1_DATA_COLORING", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 247 | "L1 data access in which a page coloring alias occurs"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 248 | {0x4C, 0, "L1_NEON_DATA", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 249 | "NEON data access that hits L1 cache"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 250 | {0x4D, 0, "L1_NEON_CACH_DATA", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 251 | "NEON cacheable data access that hits L1 cache"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 252 | {0x4E, 0, "L2_NEON", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 253 | "L2 access as a result of NEON memory access"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 254 | {0x4F, 0, "L2_NEON_HIT", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 255 | "Any NEON hit in L2 cache"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 256 | {0x50, 0, "L1_INST", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 257 | "Any L1 instruction cache access, excluding CP15 cache accesses"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 258 | {0x51, 0, "PC_RETURN_MIS_PRED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 259 | "Return stack misprediction at return stack pop" |
| 260 | "(incorrect target address)"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 261 | {0x52, 0, "PC_BRANCH_FAILED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 262 | "Branch prediction misprediction"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 263 | {0x53, 0, "PC_BRANCH_TAKEN", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 264 | "Any predicted branch that is taken"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 265 | {0x54, 0, "PC_BRANCH_EXECUTED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 266 | "Any taken branch that is executed"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 267 | {0x55, 0, "OP_EXECUTED", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 268 | "Number of operations executed" |
| 269 | "(in instruction or mutli-cycle instruction)"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 270 | {0x56, 0, "CYCLES_INST_STALL", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 271 | "Cycles where no instruction available"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 272 | {0x57, 0, "CYCLES_INST", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 273 | "Number of instructions issued in a cycle"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 274 | {0x58, 0, "CYCLES_NEON_DATA_STALL", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 275 | "Number of cycles the processor waits on MRC data from NEON"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 276 | {0x59, 0, "CYCLES_NEON_INST_STALL", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 277 | "Number of cycles the processor waits on NEON instruction queue or" |
| 278 | "NEON load queue"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 279 | {0x5A, 0, "NEON_CYCLES", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 280 | "Number of cycles NEON and integer processors are not idle"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 281 | {0x70, 0, "PMU0_EVENTS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 282 | "Number of events from external input source PMUEXTIN[0]"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 283 | {0x71, 0, "PMU1_EVENTS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 284 | "Number of events from external input source PMUEXTIN[1]"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 285 | {0x72, 0, "PMU_EVENTS", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 286 | "Number of events from both external input sources PMUEXTIN[0]" |
| 287 | "and PMUEXTIN[1]"}, |
Mike Lockwood | e815c52 | 2010-04-30 08:30:05 -0400 | [diff] [blame] | 288 | {0xFF, 0, "CPU_CYCLES", |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 289 | "Number of CPU cycles"}, |
| 290 | #endif |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 291 | }; |
| 292 | |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 293 | void usage() |
| 294 | { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 295 | printf("\nopcontrol: usage:\n" |
| 296 | " --list-events list event types\n" |
| 297 | " --help this message\n" |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 298 | " --verbose show extra status\n" |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 299 | " --verbose-log=lvl set daemon logging verbosity during setup\n" |
| 300 | " levels are: all,sfile,arcs,samples,module,misc\n" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 301 | " --setup setup directories\n" |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 302 | #if defined(__i386__) || defined(__x86_64__) |
| 303 | " --quick setup and select CPU_CLK_UNHALTED:60000\n" |
| 304 | #else |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 305 | " --quick setup and select CPU_CYCLES:150000\n" |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 306 | #endif |
Ben Cheng | a9404b8 | 2010-09-03 16:46:45 -0700 | [diff] [blame] | 307 | " --timer timer-based profiling\n" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 308 | " --status show configuration\n" |
| 309 | " --start start data collection\n" |
| 310 | " --stop stop data collection\n" |
| 311 | " --reset clears out data from current session\n" |
| 312 | " --shutdown kill the oprofile daeman\n" |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 313 | " --callgraph=depth callgraph depth\n" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 314 | " --event=eventspec\n" |
| 315 | " Choose an event. May be specified multiple times.\n" |
| 316 | " eventspec is in the form of name[:count], where :\n" |
| 317 | " name: event name, see \"opcontrol --list-events\"\n" |
| 318 | " count: reset counter value\n" |
| 319 | " --vmlinux=file vmlinux kernel image\n" |
| 320 | " --kernel-range=start,end\n" |
| 321 | " kernel range vma address in hexadecimal\n" |
| 322 | ); |
| 323 | } |
| 324 | |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 325 | void setup_session_dir() |
| 326 | { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 327 | int fd; |
| 328 | |
| 329 | fd = open(OP_DATA_DIR, O_RDONLY); |
| 330 | if (fd != -1) { |
| 331 | system("rm -r "OP_DATA_DIR); |
| 332 | close(fd); |
| 333 | } |
| 334 | |
Mike Playle | d095010 | 2010-07-09 17:02:22 +0100 | [diff] [blame] | 335 | if (mkdir(OP_DATA_DIR, 0755)) { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 336 | fprintf(stderr, "Cannot create directory \"%s\": %s\n", |
| 337 | OP_DATA_DIR, strerror(errno)); |
| 338 | } |
Mike Playle | d095010 | 2010-07-09 17:02:22 +0100 | [diff] [blame] | 339 | if (mkdir(OP_DATA_DIR"/samples", 0755)) { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 340 | fprintf(stderr, "Cannot create directory \"%s\": %s\n", |
| 341 | OP_DATA_DIR"/samples", strerror(errno)); |
| 342 | } |
| 343 | } |
| 344 | |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 345 | int do_setup() |
| 346 | { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 347 | char dir[1024]; |
| 348 | |
| 349 | setup_session_dir(); |
| 350 | |
Mike Playle | d095010 | 2010-07-09 17:02:22 +0100 | [diff] [blame] | 351 | if (mkdir(OP_DRIVER_BASE, 0755)) { |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 352 | if (errno != EEXIST) { |
| 353 | fprintf(stderr, "Cannot create directory "OP_DRIVER_BASE": %s\n", |
| 354 | strerror(errno)); |
| 355 | return -1; |
| 356 | } |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 357 | } |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 358 | |
| 359 | if (access(OP_DRIVER_BASE"/stats", F_OK)) { |
| 360 | if (system("mount -t oprofilefs nodev "OP_DRIVER_BASE)) { |
| 361 | return -1; |
| 362 | } |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | void do_list_events() |
| 368 | { |
| 369 | unsigned int i; |
| 370 | |
| 371 | printf("%-20s: %s\n", "name", "meaning"); |
| 372 | printf("----------------------------------------" |
| 373 | "--------------------------------------\n"); |
| 374 | for (i = 0; i < sizeof(event_info)/sizeof(struct event_info); i++) { |
| 375 | printf("%-20s: %s\n", event_info[i].name, event_info[i].explanation); |
| 376 | } |
| 377 | } |
| 378 | |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 379 | int find_event_idx_from_name(const char *name) |
| 380 | { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 381 | unsigned int i; |
| 382 | |
| 383 | for (i = 0; i < sizeof(event_info)/sizeof(struct event_info); i++) { |
| 384 | if (!strcmp(name, event_info[i].name)) { |
| 385 | return i; |
| 386 | } |
| 387 | } |
| 388 | return -1; |
| 389 | } |
| 390 | |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 391 | const char * find_event_name_from_id(int id) |
| 392 | { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 393 | unsigned int i; |
| 394 | |
| 395 | for (i = 0; i < sizeof(event_info)/sizeof(struct event_info); i++) { |
| 396 | if (event_info[i].id == id) { |
| 397 | return event_info[i].name; |
| 398 | } |
| 399 | } |
| 400 | return NULL; |
| 401 | } |
| 402 | |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 403 | int process_event(const char *event_spec) |
| 404 | { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 405 | char event_name[512]; |
| 406 | char count_name[512]; |
| 407 | unsigned int i; |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 408 | int event_idx; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 409 | int count_val; |
| 410 | |
| 411 | strncpy(event_name, event_spec, 512); |
| 412 | count_name[0] = 0; |
| 413 | |
| 414 | /* First, check if the name is followed by ":" */ |
| 415 | for (i = 0; i < strlen(event_name); i++) { |
| 416 | if (event_name[i] == 0) { |
| 417 | break; |
| 418 | } |
| 419 | if (event_name[i] == ':') { |
| 420 | strncpy(count_name, event_name+i+1, 512); |
| 421 | event_name[i] = 0; |
| 422 | break; |
| 423 | } |
| 424 | } |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 425 | event_idx = find_event_idx_from_name(event_name); |
| 426 | if (event_idx == -1) { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 427 | fprintf(stderr, "Unknown event name: %s\n", event_name); |
| 428 | return -1; |
| 429 | } |
| 430 | |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 431 | /* Use default count */ |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 432 | if (count_name[0] == 0) { |
| 433 | count_val = min_count[0]; |
| 434 | } else { |
| 435 | count_val = atoi(count_name); |
| 436 | } |
| 437 | |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 438 | selected_events[num_events] = event_idx; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 439 | selected_counts[num_events++] = count_val; |
Ben Cheng | 1434507 | 2009-09-16 11:31:39 -0700 | [diff] [blame] | 440 | verbose("event_id is %d\n", event_info[event_idx].id); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 441 | verbose("count_val is %d\n", count_val); |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | int echo_dev(const char* str, int val, const char* file, int counter) |
| 446 | { |
| 447 | char fullname[512]; |
| 448 | char content[128]; |
| 449 | int fd; |
| 450 | |
| 451 | if (counter >= 0) { |
| 452 | snprintf(fullname, 512, OP_DRIVER_BASE"/%d/%s", counter, file); |
| 453 | } |
| 454 | else { |
| 455 | snprintf(fullname, 512, OP_DRIVER_BASE"/%s", file); |
| 456 | } |
| 457 | fd = open(fullname, O_WRONLY); |
| 458 | if (fd<0) { |
| 459 | fprintf(stderr, "Cannot open %s: %s\n", fullname, strerror(errno)); |
| 460 | return fd; |
| 461 | } |
| 462 | if (str == 0) { |
| 463 | sprintf(content, "%d", val); |
| 464 | } |
| 465 | else { |
| 466 | strncpy(content, str, 128); |
| 467 | } |
| 468 | verbose("Configure %s (%s)\n", fullname, content); |
| 469 | write(fd, content, strlen(content)); |
| 470 | close(fd); |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | int read_num(const char* file) |
| 475 | { |
| 476 | char buffer[256]; |
| 477 | int fd = open(file, O_RDONLY); |
| 478 | if (fd<0) return -1; |
| 479 | int rd = read(fd, buffer, sizeof(buffer)-1); |
| 480 | buffer[rd] = 0; |
| 481 | return atoi(buffer); |
| 482 | } |
| 483 | |
| 484 | void do_status() |
| 485 | { |
| 486 | int num; |
| 487 | char fullname[512]; |
| 488 | int i; |
| 489 | |
| 490 | printf("Driver directory: %s\n", OP_DRIVER_BASE); |
| 491 | printf("Session directory: %s\n", OP_DATA_DIR); |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 492 | for (i = 0; i < MAX_EVENTS; i++) { |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 493 | sprintf(fullname, OP_DRIVER_BASE"/%d/enabled", i); |
| 494 | num = read_num(fullname); |
| 495 | if (num > 0) { |
| 496 | printf("Counter %d:\n", i); |
| 497 | |
| 498 | /* event name */ |
| 499 | sprintf(fullname, OP_DRIVER_BASE"/%d/event", i); |
| 500 | num = read_num(fullname); |
| 501 | printf(" name: %s\n", find_event_name_from_id(num)); |
| 502 | |
| 503 | /* profile interval */ |
| 504 | sprintf(fullname, OP_DRIVER_BASE"/%d/count", i); |
| 505 | num = read_num(fullname); |
| 506 | printf(" count: %d\n", num); |
| 507 | } |
| 508 | else { |
| 509 | printf("Counter %d disabled\n", i); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | num = read_num(OP_DATA_DIR"/lock"); |
| 514 | if (num >= 0) { |
| 515 | int fd; |
| 516 | /* Still needs to check if this lock is left-over */ |
| 517 | sprintf(fullname, "/proc/%d", num); |
| 518 | fd = open(fullname, O_RDONLY); |
| 519 | if (fd == -1) { |
| 520 | printf("Session directory is not clean - do \"opcontrol --setup\"" |
| 521 | " before you continue\n"); |
| 522 | return; |
| 523 | } |
| 524 | else { |
| 525 | close(fd); |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 526 | |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 527 | printf("oprofiled pid: %d\n", num); |
| 528 | num = read_num(OP_DRIVER_BASE"/enable"); |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 529 | |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 530 | printf("profiler is%s running\n", num == 0 ? " not" : ""); |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 531 | |
| 532 | DIR* dir = opendir(OP_DRIVER_BASE"/stats"); |
| 533 | if (dir) { |
| 534 | for (struct dirent* dirent; !!(dirent = readdir(dir));) { |
| 535 | if (strlen(dirent->d_name) >= 4 && memcmp(dirent->d_name, "cpu", 3) == 0) { |
| 536 | char cpupath[256]; |
| 537 | strcpy(cpupath, OP_DRIVER_BASE"/stats/"); |
| 538 | strcat(cpupath, dirent->d_name); |
| 539 | |
| 540 | strcpy(fullname, cpupath); |
| 541 | strcat(fullname, "/sample_received"); |
| 542 | num = read_num(fullname); |
| 543 | printf(" %s %9u samples received\n", dirent->d_name, num); |
| 544 | |
| 545 | strcpy(fullname, cpupath); |
| 546 | strcat(fullname, "/sample_lost_overflow"); |
| 547 | num = read_num(fullname); |
| 548 | printf(" %s %9u samples lost overflow\n", dirent->d_name, num); |
| 549 | |
| 550 | strcpy(fullname, cpupath); |
| 551 | strcat(fullname, "/sample_invalid_eip"); |
| 552 | num = read_num(fullname); |
| 553 | printf(" %s %9u samples invalid eip\n", dirent->d_name, num); |
| 554 | |
| 555 | strcpy(fullname, cpupath); |
| 556 | strcat(fullname, "/backtrace_aborted"); |
| 557 | num = read_num(fullname); |
| 558 | printf(" %s %9u backtrace aborted\n", dirent->d_name, num); |
| 559 | } |
| 560 | } |
| 561 | closedir(dir); |
| 562 | } |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 563 | |
| 564 | #if defined(__i386__) || defined(__x86_64__) |
| 565 | /* FIXME on ARM - backtrace seems broken there */ |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 566 | num = read_num(OP_DRIVER_BASE"/backtrace_depth"); |
| 567 | printf(" %9u backtrace_depth\n", num); |
| 568 | #endif |
| 569 | } |
| 570 | } |
| 571 | else { |
| 572 | printf("oprofiled is not running\n"); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | void do_reset() |
| 577 | { |
| 578 | int fd; |
| 579 | |
| 580 | fd = open(OP_DATA_DIR"/samples/current", O_RDONLY); |
| 581 | if (fd == -1) { |
| 582 | return; |
| 583 | } |
| 584 | close(fd); |
| 585 | system("rm -r "OP_DATA_DIR"/samples/current"); |
| 586 | } |
| 587 | |
| 588 | int main(int argc, char * const argv[]) |
| 589 | { |
| 590 | int option_index; |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 591 | bool show_status = false; |
| 592 | char* verbose_log = NULL; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 593 | |
| 594 | /* Initialize default strings */ |
| 595 | strcpy(vmlinux, "--no-vmlinux"); |
| 596 | strcpy(kernel_range, ""); |
| 597 | |
| 598 | while (1) { |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 599 | int c = getopt_long(argc, argv, "c:e:v:r:dhVtl:", long_options, &option_index); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 600 | if (c == -1) { |
| 601 | break; |
| 602 | } |
| 603 | switch (c) { |
| 604 | case 0: |
| 605 | break; |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 606 | /* --callgraph */ |
| 607 | case 'c': |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 608 | strncpy(callgraph, optarg, sizeof(callgraph)); |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 609 | break; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 610 | /* --event */ |
| 611 | case 'e': |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 612 | if (num_events == MAX_EVENTS) { |
| 613 | fprintf(stderr, "More than %d events specified\n", |
| 614 | MAX_EVENTS); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 615 | exit(1); |
| 616 | } |
| 617 | if (process_event(optarg)) { |
| 618 | exit(1); |
| 619 | } |
| 620 | break; |
| 621 | /* --vmlinux */ |
| 622 | case 'v': |
| 623 | sprintf(vmlinux, "-k %s", optarg); |
| 624 | break; |
| 625 | /* --kernel-range */ |
| 626 | case 'r': |
| 627 | sprintf(kernel_range, "-r %s", optarg); |
| 628 | break; |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 629 | case 'd': |
| 630 | /* --dump */ { |
| 631 | int pid = read_num(OP_DATA_DIR"/lock"); |
| 632 | echo_dev("1", 0, "dump", -1); |
| 633 | if (pid >= 0) { |
| 634 | sleep(1); |
| 635 | kill(pid, SIGHUP); |
| 636 | } |
| 637 | break; |
| 638 | } |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 639 | /* --shutdown */ |
| 640 | case 'h': { |
| 641 | int pid = read_num(OP_DATA_DIR"/lock"); |
| 642 | if (pid >= 0) { |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 643 | kill(pid, SIGHUP); /* Politely ask the daemon to close files */ |
| 644 | sleep(1); |
| 645 | kill(pid, SIGTERM);/* Politely ask the daemon to die */ |
| 646 | sleep(1); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 647 | kill(pid, SIGKILL); |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 648 | } |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 649 | setup_session_dir(); |
| 650 | break; |
| 651 | } |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 652 | /* --verbose */ |
| 653 | case 'V': |
| 654 | verbose_print++; |
| 655 | break; |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 656 | /* --verbose-log */ |
| 657 | case 'l': |
| 658 | verbose_log = strdup(optarg); |
| 659 | break; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 660 | /* --status */ |
| 661 | case 't': |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 662 | show_status = true; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 663 | break; |
| 664 | default: |
| 665 | usage(); |
| 666 | exit(1); |
| 667 | } |
| 668 | } |
| 669 | verbose("list_events = %d\n", list_events); |
| 670 | verbose("setup = %d\n", setup); |
| 671 | |
| 672 | if (list_events) { |
| 673 | do_list_events(); |
| 674 | } |
| 675 | |
| 676 | if (quick) { |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 677 | #if defined(__i386__) || defined(__x86_64__) |
| 678 | process_event("CPU_CLK_UNHALTED"); |
| 679 | #else |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 680 | process_event("CPU_CYCLES"); |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 681 | #endif |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 682 | setup = 1; |
| 683 | } |
| 684 | |
Ben Cheng | a9404b8 | 2010-09-03 16:46:45 -0700 | [diff] [blame] | 685 | if (timer) { |
| 686 | setup = 1; |
| 687 | } |
| 688 | |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 689 | if (reset) { |
| 690 | do_reset(); |
| 691 | } |
| 692 | |
| 693 | if (show_usage) { |
| 694 | usage(); |
| 695 | } |
| 696 | |
| 697 | if (setup) { |
| 698 | if (do_setup()) { |
| 699 | fprintf(stderr, "do_setup failed"); |
| 700 | exit(1); |
| 701 | } |
| 702 | } |
| 703 | |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 704 | if (strlen(callgraph)) { |
| 705 | echo_dev(callgraph, 0, "backtrace_depth", -1); |
| 706 | } |
| 707 | |
Ben Cheng | a9404b8 | 2010-09-03 16:46:45 -0700 | [diff] [blame] | 708 | if (num_events != 0 || timer != 0) { |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 709 | char command[1024]; |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 710 | int i; |
| 711 | |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 712 | strcpy(command, argv[0]); |
| 713 | char* slash = strrchr(command, '/'); |
| 714 | strcpy(slash ? slash + 1 : command, "oprofiled --session-dir="OP_DATA_DIR); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 715 | |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 716 | #if defined(__i386__) || defined(__x86_64__) |
| 717 | /* Nothing */ |
| 718 | #elif !defined(WITH_ARM_V7_A) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 719 | /* Since counter #3 can only handle CPU_CYCLES, check and shuffle the |
| 720 | * order a bit so that the maximal number of events can be profiled |
| 721 | * simultaneously |
| 722 | */ |
| 723 | if (num_events == 3) { |
| 724 | for (i = 0; i < num_events; i++) { |
| 725 | int event_idx = selected_events[i]; |
| 726 | |
| 727 | if (event_info[event_idx].id == 0xff) { |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* No CPU_CYCLES is found */ |
| 733 | if (i == 3) { |
| 734 | fprintf(stderr, "You can only specify three events if one of " |
| 735 | "them is CPU_CYCLES\n"); |
| 736 | exit(1); |
| 737 | } |
| 738 | /* Swap CPU_CYCLES to counter #2 (starting from #0)*/ |
| 739 | else if (i != 2) { |
| 740 | int temp; |
| 741 | |
| 742 | temp = selected_events[2]; |
| 743 | selected_events[2] = selected_events[i]; |
| 744 | selected_events[i] = temp; |
| 745 | |
| 746 | temp = selected_counts[2]; |
| 747 | selected_counts[2] = selected_counts[i]; |
| 748 | selected_counts[i] = temp; |
| 749 | } |
| 750 | } |
Ben Cheng | 5a4eb4e | 2009-09-14 16:00:41 -0700 | [diff] [blame] | 751 | #endif |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 752 | |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 753 | /* Configure the counters and enable them */ |
| 754 | for (i = 0; i < num_events; i++) { |
| 755 | int event_idx = selected_events[i]; |
| 756 | int setup_result = 0; |
| 757 | |
| 758 | if (i == 0) { |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 759 | snprintf(command + strlen(command), sizeof(command) - strlen(command), |
| 760 | " --events="); |
| 761 | } else { |
| 762 | snprintf(command + strlen(command), sizeof(command) - strlen(command), ","); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 763 | } |
| 764 | /* Compose name:id:count:unit_mask:kernel:user, something like |
| 765 | * --events=CYCLES_DATA_STALL:2:0:200000:0:1:1,.... |
| 766 | */ |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 767 | snprintf(command + strlen(command), sizeof(command) - strlen(command), |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 768 | "%s:%d:%d:%d:%d:1:1", |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 769 | event_info[event_idx].name, |
| 770 | event_info[event_idx].id, |
| 771 | i, |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 772 | selected_counts[i], |
| 773 | event_info[event_idx].um); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 774 | |
| 775 | setup_result |= echo_dev("1", 0, "user", i); |
| 776 | setup_result |= echo_dev("1", 0, "kernel", i); |
Bruce Beare | 751a443 | 2010-03-04 10:30:51 -0800 | [diff] [blame] | 777 | setup_result |= echo_dev(NULL, event_info[event_idx].um, "unit_mask", i); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 778 | setup_result |= echo_dev("1", 0, "enabled", i); |
| 779 | setup_result |= echo_dev(NULL, selected_counts[i], "count", i); |
| 780 | setup_result |= echo_dev(NULL, event_info[event_idx].id, |
| 781 | "event", i); |
| 782 | if (setup_result) { |
| 783 | fprintf(stderr, "Counter configuration failed for %s\n", |
| 784 | event_info[event_idx].name); |
| 785 | fprintf(stderr, "Did you do \"opcontrol --setup\" first?\n"); |
| 786 | exit(1); |
| 787 | } |
| 788 | } |
| 789 | |
Ben Cheng | a9404b8 | 2010-09-03 16:46:45 -0700 | [diff] [blame] | 790 | if (timer == 0) { |
| 791 | /* If not in timer mode, disable unused counters */ |
| 792 | for (i = num_events; i < MAX_EVENTS; i++) { |
| 793 | echo_dev("0", 0, "enabled", i); |
| 794 | } |
| 795 | } else { |
| 796 | /* Timer mode uses empty event list */ |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 797 | snprintf(command + strlen(command), sizeof(command) - strlen(command), |
| 798 | " --events="); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 801 | snprintf(command + strlen(command), sizeof(command) - strlen(command), |
| 802 | " %s", vmlinux); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 803 | if (kernel_range[0]) { |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 804 | snprintf(command + strlen(command), sizeof(command) - strlen(command), |
| 805 | " %s", kernel_range); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 806 | } |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 807 | |
| 808 | if (verbose_log) { |
| 809 | snprintf(command + strlen(command), sizeof(command) - strlen(command), |
| 810 | " --verbose=%s", verbose_log); |
| 811 | } |
| 812 | |
| 813 | printf("Starting oprofiled...\n"); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 814 | verbose("command: %s\n", command); |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 815 | |
| 816 | int rc = system(command); |
| 817 | if (rc) { |
| 818 | fprintf(stderr, "Failed, oprofile returned exit code: %d\n", rc); |
| 819 | } else { |
| 820 | sleep(2); |
| 821 | printf("Ready\n"); |
| 822 | } |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | if (start) { |
| 826 | echo_dev("1", 0, "enable", -1); |
| 827 | } |
| 828 | |
| 829 | if (stop) { |
Ben Cheng | afec5b9 | 2010-02-22 15:33:08 -0800 | [diff] [blame] | 830 | echo_dev("1", 0, "dump", -1); |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 831 | echo_dev("0", 0, "enable", -1); |
| 832 | } |
Jeff Brown | 87866d9 | 2011-02-02 13:27:17 -0800 | [diff] [blame^] | 833 | |
| 834 | if (show_status) { |
| 835 | do_status(); |
| 836 | } |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 837 | } |