blob: 075ade69b296ef89b1cfda8e2696b7351c5f45f9 [file] [log] [blame]
The Android Open Source Project10e23ee2009-03-03 19:30:30 -08001/*
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 Querud850f372010-05-03 15:16:06 -070026#include <string.h>
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080027#include <errno.h>
28#include <fcntl.h>
29#include <signal.h>
Jeff Brown87866d92011-02-02 13:27:17 -080030#include <dirent.h>
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080031#include <sys/stat.h>
Jeff Brown87866d92011-02-02 13:27:17 -080032#include <sys/types.h>
33#include <sys/wait.h>
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080034
35#include "op_config.h"
36
Bruce Beare751a4432010-03-04 10:30:51 -080037#define verbose(fmt...) if (verbose_print) printf(fmt)
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080038
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 Cheng5a4eb4e2009-09-14 16:00:41 -070042
Bruce Beare751a4432010-03-04 10:30:51 -080043#if defined(__i386__) || defined(__x86_64__)
44#define MAX_EVENTS 2
45int min_count[MAX_EVENTS] = {60000, 100000};
46#elif !defined(WITH_ARM_V7_A)
Ben Cheng5a4eb4e2009-09-14 16:00:41 -070047#define MAX_EVENTS 3
Ben Cheng14345072009-09-16 11:31:39 -070048int min_count[MAX_EVENTS] = {150000, 200000, 250000};
Ben Cheng5a4eb4e2009-09-14 16:00:41 -070049#else
Ben Cheng14345072009-09-16 11:31:39 -070050#define MAX_EVENTS 5
Ben Chengafec5b92010-02-22 15:33:08 -080051int min_count[MAX_EVENTS] = {150000, 20000, 25000, 30000, 35000};
Ben Cheng5a4eb4e2009-09-14 16:00:41 -070052#endif
53
Bruce Beare751a4432010-03-04 10:30:51 -080054int verbose_print;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080055int list_events;
56int show_usage;
57int setup;
58int quick;
Ben Chenga9404b82010-09-03 16:46:45 -070059int timer;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080060int num_events;
61int start;
62int stop;
63int reset;
64
Ben Cheng5a4eb4e2009-09-14 16:00:41 -070065int selected_events[MAX_EVENTS];
66int selected_counts[MAX_EVENTS];
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080067
Bruce Beare751a4432010-03-04 10:30:51 -080068char callgraph[8];
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080069char kernel_range[512];
70char vmlinux[512];
71
72struct 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 Chenga9404b82010-09-03 16:46:45 -070078 {"timer", 0, &timer, 1},
Bruce Beare751a4432010-03-04 10:30:51 -080079 {"callgraph", 1, 0, 'c'},
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080080 {"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 Beare751a4432010-03-04 10:30:51 -080085 {"dump", 0, 0, 'd'},
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080086 {"shutdown", 0, 0, 'h'},
87 {"status", 0, 0, 't'},
Bruce Beare751a4432010-03-04 10:30:51 -080088 {"verbose", 0, 0, 'V'},
Jeff Brown87866d92011-02-02 13:27:17 -080089 {"verbose-log", 1, 0, 'l'},
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080090 {0, 0, 0, 0},
91};
92
93struct event_info {
94 int id;
Bruce Beare751a4432010-03-04 10:30:51 -080095 int um;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080096 const char *name;
97 const char *explanation;
98} event_info[] = {
Bruce Beare751a4432010-03-04 10:30:51 -080099#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 Cheng5a4eb4e2009-09-14 16:00:41 -0700145 /* ARM V6 events */
Bruce Beare751a4432010-03-04 10:30:51 -0800146 {0x00, 0, "IFU_IFETCH_MISS",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800147 "number of instruction fetch misses"},
Bruce Beare751a4432010-03-04 10:30:51 -0800148 {0x01, 0, "CYCLES_IFU_MEM_STALL",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800149 "cycles instruction fetch pipe is stalled"},
Bruce Beare751a4432010-03-04 10:30:51 -0800150 {0x02, 0, "CYCLES_DATA_STALL",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800151 "cycles stall occurs for due to data dependency"},
Bruce Beare751a4432010-03-04 10:30:51 -0800152 {0x03, 0, "ITLB_MISS",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800153 "number of Instruction MicroTLB misses"},
Bruce Beare751a4432010-03-04 10:30:51 -0800154 {0x04, 0, "DTLB_MISS",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800155 "number of Data MicroTLB misses"},
Bruce Beare751a4432010-03-04 10:30:51 -0800156 {0x05, 0, "BR_INST_EXECUTED",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800157 "branch instruction executed w/ or w/o program flow change"},
Bruce Beare751a4432010-03-04 10:30:51 -0800158 {0x06, 0, "BR_INST_MISS_PRED",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800159 "branch mispredicted"},
Bruce Beare751a4432010-03-04 10:30:51 -0800160 {0x07, 0, "INSN_EXECUTED",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800161 "instructions executed"},
Bruce Beare751a4432010-03-04 10:30:51 -0800162 {0x09, 0, "DCACHE_ACCESS",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800163 "data cache access, cacheable locations"},
Bruce Beare751a4432010-03-04 10:30:51 -0800164 {0x0a, 0, "DCACHE_ACCESS_ALL",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800165 "data cache access, all locations"},
Bruce Beare751a4432010-03-04 10:30:51 -0800166 {0x0b, 0, "DCACHE_MISS",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800167 "data cache miss"},
Bruce Beare751a4432010-03-04 10:30:51 -0800168 {0x0c, 0, "DCACHE_WB",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800169 "data cache writeback, 1 event for every half cacheline"},
Bruce Beare751a4432010-03-04 10:30:51 -0800170 {0x0d, 0, "PC_CHANGE",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800171 "number of times the program counter was changed without a mode switch"},
Bruce Beare751a4432010-03-04 10:30:51 -0800172 {0x0f, 0, "TLB_MISS",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800173 "Main TLB miss"},
Bruce Beare751a4432010-03-04 10:30:51 -0800174 {0x10, 0, "EXP_EXTERNAL",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800175 "Explicit external data access"},
Bruce Beare751a4432010-03-04 10:30:51 -0800176 {0x11, 0, "LSU_STALL",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800177 "cycles stalled because Load Store request queue is full"},
Bruce Beare751a4432010-03-04 10:30:51 -0800178 {0x12, 0, "WRITE_DRAIN",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800179 "Times write buffer was drained"},
Bruce Beare751a4432010-03-04 10:30:51 -0800180 {0xff, 0, "CPU_CYCLES",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800181 "clock cycles counter"},
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700182#else
183 /* ARM V7 events */
Bruce Beare751a4432010-03-04 10:30:51 -0800184 {0x00, 0, "PMNC_SW_INCR",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700185 "Software increment of PMNC registers"},
Bruce Beare751a4432010-03-04 10:30:51 -0800186 {0x01, 0, "IFETCH_MISS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700187 "Instruction fetch misses from cache or normal cacheable memory"},
Bruce Beare751a4432010-03-04 10:30:51 -0800188 {0x02, 0, "ITLB_MISS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700189 "Instruction fetch misses from TLB"},
Bruce Beare751a4432010-03-04 10:30:51 -0800190 {0x03, 0, "DCACHE_REFILL",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700191 "Data R/W operation that causes a refill from cache or normal cacheable"
192 "memory"},
Bruce Beare751a4432010-03-04 10:30:51 -0800193 {0x04, 0, "DCACHE_ACCESS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700194 "Data R/W from cache"},
Bruce Beare751a4432010-03-04 10:30:51 -0800195 {0x05, 0, "DTLB_REFILL",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700196 "Data R/W that causes a TLB refill"},
Bruce Beare751a4432010-03-04 10:30:51 -0800197 {0x06, 0, "DREAD",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700198 "Data read architecturally executed (note: architecturally executed = for"
199 "instructions that are unconditional or that pass the condition code)"},
Bruce Beare751a4432010-03-04 10:30:51 -0800200 {0x07, 0, "DWRITE",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700201 "Data write architecturally executed"},
Bruce Beare751a4432010-03-04 10:30:51 -0800202 {0x08, 0, "INSTR_EXECUTED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700203 "All executed instructions"},
Bruce Beare751a4432010-03-04 10:30:51 -0800204 {0x09, 0, "EXC_TAKEN",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700205 "Exception taken"},
Bruce Beare751a4432010-03-04 10:30:51 -0800206 {0x0A, 0, "EXC_EXECUTED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700207 "Exception return architecturally executed"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400208 {0x0B, 0, "CID_WRITE",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700209 "Instruction that writes to the Context ID Register architecturally"
210 "executed"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400211 {0x0C, 0, "PC_WRITE",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700212 "SW change of PC, architecturally executed (not by exceptions)"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400213 {0x0D, 0, "PC_IMM_BRANCH",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700214 "Immediate branch instruction executed (taken or not)"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400215 {0x0E, 0, "PC_PROC_RETURN",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700216 "Procedure return architecturally executed (not by exceptions)"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400217 {0x0F, 0, "UNALIGNED_ACCESS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700218 "Unaligned access architecturally executed"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400219 {0x10, 0, "PC_BRANCH_MIS_PRED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700220 "Branch mispredicted or not predicted. Counts pipeline flushes because of"
221 "misprediction"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400222 {0x12, 0, "PC_BRANCH_MIS_USED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700223 "Branch or change in program flow that could have been predicted"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400224 {0x40, 0, "WRITE_BUFFER_FULL",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700225 "Any write buffer full cycle"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400226 {0x41, 0, "L2_STORE_MERGED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700227 "Any store that is merged in L2 cache"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400228 {0x42, 0, "L2_STORE_BUFF",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700229 "Any bufferable store from load/store to L2 cache"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400230 {0x43, 0, "L2_ACCESS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700231 "Any access to L2 cache"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400232 {0x44, 0, "L2_CACH_MISS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700233 "Any cacheable miss in L2 cache"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400234 {0x45, 0, "AXI_READ_CYCLES",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700235 "Number of cycles for an active AXI read"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400236 {0x46, 0, "AXI_WRITE_CYCLES",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700237 "Number of cycles for an active AXI write"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400238 {0x47, 0, "MEMORY_REPLAY",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700239 "Any replay event in the memory subsystem"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400240 {0x48, 0, "UNALIGNED_ACCESS_REPLAY",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700241 "Unaligned access that causes a replay"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400242 {0x49, 0, "L1_DATA_MISS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700243 "L1 data cache miss as a result of the hashing algorithm"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400244 {0x4A, 0, "L1_INST_MISS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700245 "L1 instruction cache miss as a result of the hashing algorithm"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400246 {0x4B, 0, "L1_DATA_COLORING",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700247 "L1 data access in which a page coloring alias occurs"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400248 {0x4C, 0, "L1_NEON_DATA",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700249 "NEON data access that hits L1 cache"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400250 {0x4D, 0, "L1_NEON_CACH_DATA",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700251 "NEON cacheable data access that hits L1 cache"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400252 {0x4E, 0, "L2_NEON",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700253 "L2 access as a result of NEON memory access"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400254 {0x4F, 0, "L2_NEON_HIT",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700255 "Any NEON hit in L2 cache"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400256 {0x50, 0, "L1_INST",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700257 "Any L1 instruction cache access, excluding CP15 cache accesses"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400258 {0x51, 0, "PC_RETURN_MIS_PRED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700259 "Return stack misprediction at return stack pop"
260 "(incorrect target address)"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400261 {0x52, 0, "PC_BRANCH_FAILED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700262 "Branch prediction misprediction"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400263 {0x53, 0, "PC_BRANCH_TAKEN",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700264 "Any predicted branch that is taken"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400265 {0x54, 0, "PC_BRANCH_EXECUTED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700266 "Any taken branch that is executed"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400267 {0x55, 0, "OP_EXECUTED",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700268 "Number of operations executed"
269 "(in instruction or mutli-cycle instruction)"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400270 {0x56, 0, "CYCLES_INST_STALL",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700271 "Cycles where no instruction available"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400272 {0x57, 0, "CYCLES_INST",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700273 "Number of instructions issued in a cycle"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400274 {0x58, 0, "CYCLES_NEON_DATA_STALL",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700275 "Number of cycles the processor waits on MRC data from NEON"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400276 {0x59, 0, "CYCLES_NEON_INST_STALL",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700277 "Number of cycles the processor waits on NEON instruction queue or"
278 "NEON load queue"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400279 {0x5A, 0, "NEON_CYCLES",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700280 "Number of cycles NEON and integer processors are not idle"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400281 {0x70, 0, "PMU0_EVENTS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700282 "Number of events from external input source PMUEXTIN[0]"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400283 {0x71, 0, "PMU1_EVENTS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700284 "Number of events from external input source PMUEXTIN[1]"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400285 {0x72, 0, "PMU_EVENTS",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700286 "Number of events from both external input sources PMUEXTIN[0]"
287 "and PMUEXTIN[1]"},
Mike Lockwoode815c522010-04-30 08:30:05 -0400288 {0xFF, 0, "CPU_CYCLES",
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700289 "Number of CPU cycles"},
290#endif
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800291};
292
Ben Cheng14345072009-09-16 11:31:39 -0700293void usage()
294{
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800295 printf("\nopcontrol: usage:\n"
296 " --list-events list event types\n"
297 " --help this message\n"
Bruce Beare751a4432010-03-04 10:30:51 -0800298 " --verbose show extra status\n"
Jeff Brown87866d92011-02-02 13:27:17 -0800299 " --verbose-log=lvl set daemon logging verbosity during setup\n"
300 " levels are: all,sfile,arcs,samples,module,misc\n"
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800301 " --setup setup directories\n"
Bruce Beare751a4432010-03-04 10:30:51 -0800302#if defined(__i386__) || defined(__x86_64__)
303 " --quick setup and select CPU_CLK_UNHALTED:60000\n"
304#else
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800305 " --quick setup and select CPU_CYCLES:150000\n"
Bruce Beare751a4432010-03-04 10:30:51 -0800306#endif
Ben Chenga9404b82010-09-03 16:46:45 -0700307 " --timer timer-based profiling\n"
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800308 " --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 Beare751a4432010-03-04 10:30:51 -0800313 " --callgraph=depth callgraph depth\n"
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800314 " --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 Cheng14345072009-09-16 11:31:39 -0700325void setup_session_dir()
326{
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800327 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 Playled0950102010-07-09 17:02:22 +0100335 if (mkdir(OP_DATA_DIR, 0755)) {
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800336 fprintf(stderr, "Cannot create directory \"%s\": %s\n",
337 OP_DATA_DIR, strerror(errno));
338 }
Mike Playled0950102010-07-09 17:02:22 +0100339 if (mkdir(OP_DATA_DIR"/samples", 0755)) {
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800340 fprintf(stderr, "Cannot create directory \"%s\": %s\n",
341 OP_DATA_DIR"/samples", strerror(errno));
342 }
343}
344
Ben Cheng14345072009-09-16 11:31:39 -0700345int do_setup()
346{
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800347 char dir[1024];
348
349 setup_session_dir();
350
Mike Playled0950102010-07-09 17:02:22 +0100351 if (mkdir(OP_DRIVER_BASE, 0755)) {
Jeff Brown87866d92011-02-02 13:27:17 -0800352 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 Project10e23ee2009-03-03 19:30:30 -0800357 }
Jeff Brown87866d92011-02-02 13:27:17 -0800358
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 Project10e23ee2009-03-03 19:30:30 -0800363 }
364 return 0;
365}
366
367void 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 Cheng14345072009-09-16 11:31:39 -0700379int find_event_idx_from_name(const char *name)
380{
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800381 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 Cheng14345072009-09-16 11:31:39 -0700391const char * find_event_name_from_id(int id)
392{
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800393 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 Cheng14345072009-09-16 11:31:39 -0700403int process_event(const char *event_spec)
404{
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800405 char event_name[512];
406 char count_name[512];
407 unsigned int i;
Ben Cheng14345072009-09-16 11:31:39 -0700408 int event_idx;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800409 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 Cheng14345072009-09-16 11:31:39 -0700425 event_idx = find_event_idx_from_name(event_name);
426 if (event_idx == -1) {
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800427 fprintf(stderr, "Unknown event name: %s\n", event_name);
428 return -1;
429 }
430
Jeff Brown87866d92011-02-02 13:27:17 -0800431 /* Use default count */
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800432 if (count_name[0] == 0) {
433 count_val = min_count[0];
434 } else {
435 count_val = atoi(count_name);
436 }
437
Ben Cheng14345072009-09-16 11:31:39 -0700438 selected_events[num_events] = event_idx;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800439 selected_counts[num_events++] = count_val;
Ben Cheng14345072009-09-16 11:31:39 -0700440 verbose("event_id is %d\n", event_info[event_idx].id);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800441 verbose("count_val is %d\n", count_val);
442 return 0;
443}
444
445int 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
474int 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
484void 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 Cheng5a4eb4e2009-09-14 16:00:41 -0700492 for (i = 0; i < MAX_EVENTS; i++) {
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800493 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 Brown87866d92011-02-02 13:27:17 -0800526
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800527 printf("oprofiled pid: %d\n", num);
528 num = read_num(OP_DRIVER_BASE"/enable");
Jeff Brown87866d92011-02-02 13:27:17 -0800529
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800530 printf("profiler is%s running\n", num == 0 ? " not" : "");
Jeff Brown87866d92011-02-02 13:27:17 -0800531
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 Beare751a4432010-03-04 10:30:51 -0800563
564#if defined(__i386__) || defined(__x86_64__)
565 /* FIXME on ARM - backtrace seems broken there */
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800566 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
576void 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
588int main(int argc, char * const argv[])
589{
590 int option_index;
Jeff Brown87866d92011-02-02 13:27:17 -0800591 bool show_status = false;
592 char* verbose_log = NULL;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800593
594 /* Initialize default strings */
595 strcpy(vmlinux, "--no-vmlinux");
596 strcpy(kernel_range, "");
597
598 while (1) {
Jeff Brown87866d92011-02-02 13:27:17 -0800599 int c = getopt_long(argc, argv, "c:e:v:r:dhVtl:", long_options, &option_index);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800600 if (c == -1) {
601 break;
602 }
603 switch (c) {
604 case 0:
605 break;
Bruce Beare751a4432010-03-04 10:30:51 -0800606 /* --callgraph */
607 case 'c':
Jeff Brown87866d92011-02-02 13:27:17 -0800608 strncpy(callgraph, optarg, sizeof(callgraph));
Bruce Beare751a4432010-03-04 10:30:51 -0800609 break;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800610 /* --event */
611 case 'e':
Ben Cheng5a4eb4e2009-09-14 16:00:41 -0700612 if (num_events == MAX_EVENTS) {
613 fprintf(stderr, "More than %d events specified\n",
614 MAX_EVENTS);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800615 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 Beare751a4432010-03-04 10:30:51 -0800629 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 Project10e23ee2009-03-03 19:30:30 -0800639 /* --shutdown */
640 case 'h': {
641 int pid = read_num(OP_DATA_DIR"/lock");
642 if (pid >= 0) {
Bruce Beare751a4432010-03-04 10:30:51 -0800643 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 Project10e23ee2009-03-03 19:30:30 -0800647 kill(pid, SIGKILL);
Jeff Brown87866d92011-02-02 13:27:17 -0800648 }
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800649 setup_session_dir();
650 break;
651 }
Bruce Beare751a4432010-03-04 10:30:51 -0800652 /* --verbose */
653 case 'V':
654 verbose_print++;
655 break;
Jeff Brown87866d92011-02-02 13:27:17 -0800656 /* --verbose-log */
657 case 'l':
658 verbose_log = strdup(optarg);
659 break;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800660 /* --status */
661 case 't':
Jeff Brown87866d92011-02-02 13:27:17 -0800662 show_status = true;
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800663 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 Beare751a4432010-03-04 10:30:51 -0800677#if defined(__i386__) || defined(__x86_64__)
678 process_event("CPU_CLK_UNHALTED");
679#else
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800680 process_event("CPU_CYCLES");
Bruce Beare751a4432010-03-04 10:30:51 -0800681#endif
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800682 setup = 1;
683 }
684
Ben Chenga9404b82010-09-03 16:46:45 -0700685 if (timer) {
686 setup = 1;
687 }
688
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800689 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 Beare751a4432010-03-04 10:30:51 -0800704 if (strlen(callgraph)) {
705 echo_dev(callgraph, 0, "backtrace_depth", -1);
706 }
707
Ben Chenga9404b82010-09-03 16:46:45 -0700708 if (num_events != 0 || timer != 0) {
Jeff Brown87866d92011-02-02 13:27:17 -0800709 char command[1024];
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800710 int i;
711
Jeff Brown87866d92011-02-02 13:27:17 -0800712 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 Project10e23ee2009-03-03 19:30:30 -0800715
Bruce Beare751a4432010-03-04 10:30:51 -0800716#if defined(__i386__) || defined(__x86_64__)
717 /* Nothing */
718#elif !defined(WITH_ARM_V7_A)
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800719 /* 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 Cheng5a4eb4e2009-09-14 16:00:41 -0700751#endif
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800752
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800753 /* 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 Brown87866d92011-02-02 13:27:17 -0800759 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 Project10e23ee2009-03-03 19:30:30 -0800763 }
764 /* Compose name:id:count:unit_mask:kernel:user, something like
765 * --events=CYCLES_DATA_STALL:2:0:200000:0:1:1,....
766 */
Jeff Brown87866d92011-02-02 13:27:17 -0800767 snprintf(command + strlen(command), sizeof(command) - strlen(command),
Bruce Beare751a4432010-03-04 10:30:51 -0800768 "%s:%d:%d:%d:%d:1:1",
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800769 event_info[event_idx].name,
770 event_info[event_idx].id,
771 i,
Bruce Beare751a4432010-03-04 10:30:51 -0800772 selected_counts[i],
773 event_info[event_idx].um);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800774
775 setup_result |= echo_dev("1", 0, "user", i);
776 setup_result |= echo_dev("1", 0, "kernel", i);
Bruce Beare751a4432010-03-04 10:30:51 -0800777 setup_result |= echo_dev(NULL, event_info[event_idx].um, "unit_mask", i);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800778 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 Chenga9404b82010-09-03 16:46:45 -0700790 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 Brown87866d92011-02-02 13:27:17 -0800797 snprintf(command + strlen(command), sizeof(command) - strlen(command),
798 " --events=");
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800799 }
800
Jeff Brown87866d92011-02-02 13:27:17 -0800801 snprintf(command + strlen(command), sizeof(command) - strlen(command),
802 " %s", vmlinux);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800803 if (kernel_range[0]) {
Jeff Brown87866d92011-02-02 13:27:17 -0800804 snprintf(command + strlen(command), sizeof(command) - strlen(command),
805 " %s", kernel_range);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800806 }
Jeff Brown87866d92011-02-02 13:27:17 -0800807
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 Project10e23ee2009-03-03 19:30:30 -0800814 verbose("command: %s\n", command);
Jeff Brown87866d92011-02-02 13:27:17 -0800815
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 Project10e23ee2009-03-03 19:30:30 -0800823 }
824
825 if (start) {
826 echo_dev("1", 0, "enable", -1);
827 }
828
829 if (stop) {
Ben Chengafec5b92010-02-22 15:33:08 -0800830 echo_dev("1", 0, "dump", -1);
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800831 echo_dev("0", 0, "enable", -1);
832 }
Jeff Brown87866d92011-02-02 13:27:17 -0800833
834 if (show_status) {
835 do_status();
836 }
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800837}