The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | */ |
Andy McFadden | 8c880b9 | 2009-03-24 18:15:56 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Android's method call profiling goodies. |
| 19 | */ |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 20 | #ifndef DALVIK_PROFILE_H_ |
| 21 | #define DALVIK_PROFILE_H_ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | |
| 23 | #ifndef NOT_VM /* for utilities that sneakily include this file */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 27 | struct Thread; // extern |
| 28 | |
| 29 | |
| 30 | /* boot init */ |
| 31 | bool dvmProfilingStartup(void); |
| 32 | void dvmProfilingShutdown(void); |
| 33 | |
| 34 | /* |
| 35 | * Method trace state. This is currently global. In theory we could make |
| 36 | * most of this per-thread. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 38 | struct MethodTraceState { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 39 | /* active state */ |
| 40 | pthread_mutex_t startStopLock; |
| 41 | pthread_cond_t threadExitCond; |
| 42 | FILE* traceFile; |
Andy McFadden | 0171812 | 2010-01-22 16:36:30 -0800 | [diff] [blame] | 43 | bool directToDdms; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | int bufferSize; |
| 45 | int flags; |
| 46 | |
Andy McFadden | fc3d316 | 2010-08-05 14:34:26 -0700 | [diff] [blame] | 47 | int traceEnabled; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 48 | u1* buf; |
| 49 | volatile int curOffset; |
| 50 | u8 startWhen; |
| 51 | int overflow; |
Jeff Brown | 949c3ec | 2011-06-24 17:05:33 -0700 | [diff] [blame] | 52 | |
| 53 | int traceVersion; |
| 54 | size_t recordSize; |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 55 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Memory allocation profiler state. This is used both globally and |
| 59 | * per-thread. |
| 60 | * |
| 61 | * If you add a field here, zero it out in dvmStartAllocCounting(). |
| 62 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 63 | struct AllocProfState { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 64 | bool enabled; // is allocation tracking enabled? |
| 65 | |
| 66 | int allocCount; // #of objects allocated |
| 67 | int allocSize; // cumulative size of objects |
| 68 | |
| 69 | int failedAllocCount; // #of times an allocation failed |
| 70 | int failedAllocSize; // cumulative size of failed allocations |
| 71 | |
| 72 | int freeCount; // #of objects freed |
| 73 | int freeSize; // cumulative size of freed objects |
| 74 | |
| 75 | int gcCount; // #of times an allocation triggered a GC |
| 76 | |
Andy McFadden | e15a8eb | 2010-02-22 17:07:23 -0800 | [diff] [blame] | 77 | int classInitCount; // #of initialized classes |
| 78 | u8 classInitTime; // cumulative time spent in class init (nsec) |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 79 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 80 | |
| 81 | |
| 82 | /* |
| 83 | * Start/stop method tracing. |
| 84 | */ |
Dianne Hackborn | 0f0ae02 | 2009-06-23 19:21:10 -0700 | [diff] [blame] | 85 | void dvmMethodTraceStart(const char* traceFileName, int traceFd, int bufferSize, |
Andy McFadden | 0171812 | 2010-01-22 16:36:30 -0800 | [diff] [blame] | 86 | int flags, bool directToDdms); |
Andy McFadden | 8c880b9 | 2009-03-24 18:15:56 -0700 | [diff] [blame] | 87 | bool dvmIsMethodTraceActive(void); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 88 | void dvmMethodTraceStop(void); |
| 89 | |
| 90 | /* |
| 91 | * Start/stop emulator tracing. |
| 92 | */ |
| 93 | void dvmEmulatorTraceStart(void); |
| 94 | void dvmEmulatorTraceStop(void); |
| 95 | |
| 96 | /* |
| 97 | * Start/stop Dalvik instruction counting. |
| 98 | */ |
| 99 | void dvmStartInstructionCounting(); |
| 100 | void dvmStopInstructionCounting(); |
| 101 | |
| 102 | /* |
| 103 | * Bit flags for dvmMethodTraceStart "flags" argument. These must match |
| 104 | * the values in android.os.Debug. |
| 105 | */ |
| 106 | enum { |
| 107 | TRACE_ALLOC_COUNTS = 0x01, |
| 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * Call these when a method enters or exits. |
| 112 | */ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 113 | #define TRACE_METHOD_ENTER(_self, _method) \ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 114 | do { \ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 115 | if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace) \ |
buzbee | cb3081f | 2011-01-14 13:37:31 -0800 | [diff] [blame] | 116 | dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER); \ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 117 | if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace) \ |
buzbee | cb3081f | 2011-01-14 13:37:31 -0800 | [diff] [blame] | 118 | dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER); \ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 119 | } while(0); |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 120 | #define TRACE_METHOD_EXIT(_self, _method) \ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | do { \ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 122 | if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace) \ |
buzbee | cb3081f | 2011-01-14 13:37:31 -0800 | [diff] [blame] | 123 | dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT); \ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 124 | if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace) \ |
buzbee | cb3081f | 2011-01-14 13:37:31 -0800 | [diff] [blame] | 125 | dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT); \ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 126 | } while(0); |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 127 | #define TRACE_METHOD_UNROLL(_self, _method) \ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 128 | do { \ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 129 | if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace) \ |
buzbee | cb3081f | 2011-01-14 13:37:31 -0800 | [diff] [blame] | 130 | dvmMethodTraceAdd(_self, _method, METHOD_TRACE_UNROLL); \ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 131 | if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace) \ |
buzbee | cb3081f | 2011-01-14 13:37:31 -0800 | [diff] [blame] | 132 | dvmEmitEmulatorTrace(_method, METHOD_TRACE_UNROLL); \ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 133 | } while(0); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 134 | |
| 135 | void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action); |
| 136 | void dvmEmitEmulatorTrace(const Method* method, int action); |
| 137 | |
| 138 | void dvmMethodTraceGCBegin(void); |
| 139 | void dvmMethodTraceGCEnd(void); |
| 140 | void dvmMethodTraceClassPrepBegin(void); |
| 141 | void dvmMethodTraceClassPrepEnd(void); |
| 142 | |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 143 | extern "C" void dvmFastMethodTraceEnter(const Method* method, struct Thread* self); |
| 144 | extern "C" void dvmFastMethodTraceExit(struct Thread* self); |
| 145 | extern "C" void dvmFastNativeMethodTraceExit(const Method* method, struct Thread* self); |
Ben Cheng | 5cc61d7 | 2010-08-31 09:30:37 -0700 | [diff] [blame] | 146 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 147 | /* |
| 148 | * Start/stop alloc counting. |
| 149 | */ |
| 150 | void dvmStartAllocCounting(void); |
| 151 | void dvmStopAllocCounting(void); |
| 152 | |
| 153 | #endif |
| 154 | |
| 155 | |
| 156 | /* |
| 157 | * Enumeration for the two "action" bits. |
| 158 | */ |
| 159 | enum { |
| 160 | METHOD_TRACE_ENTER = 0x00, // method entry |
| 161 | METHOD_TRACE_EXIT = 0x01, // method exit |
| 162 | METHOD_TRACE_UNROLL = 0x02, // method exited by exception unrolling |
| 163 | // 0x03 currently unused |
| 164 | }; |
| 165 | |
| 166 | #define TOKEN_CHAR '*' |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * Common definitions, shared with the dump tool. |
| 170 | */ |
| 171 | #define METHOD_ACTION_MASK 0x03 /* two bits */ |
| 172 | #define METHOD_ID(_method) ((_method) & (~METHOD_ACTION_MASK)) |
| 173 | #define METHOD_ACTION(_method) (((unsigned int)(_method)) & METHOD_ACTION_MASK) |
| 174 | #define METHOD_COMBINE(_method, _action) ((_method) | (_action)) |
| 175 | |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 176 | #endif // DALVIK_PROFILE_H_ |