2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Android's method call profiling goodies.
21 #include <interp/InterpDefs.h>
33 #include <cutils/open_memstream.h>
35 #ifdef HAVE_ANDROID_OS
36 # define UPDATE_MAGIC_PAGE 1
50 * u8 start date/time in usec
51 * u2 record size in bytes (version >= 2 only)
52 * ... padding to 32 bytes
56 * u4 method ID | method action
57 * u4 time delta since start, in usec
61 * u4 method ID | method action
62 * u4 time delta since start, in usec
66 * u4 method ID | method action
67 * u4 time delta since start, in usec
68 * u4 wall time since start, in usec (when clock == "dual" only)
70 * 32 bits of microseconds is 70 minutes.
72 * All values are stored in little-endian order.
74 #define TRACE_REC_SIZE_SINGLE_CLOCK 10 // using v2
75 #define TRACE_REC_SIZE_DUAL_CLOCK 14 // using v3 with two timestamps
76 #define TRACE_MAGIC 0x574f4c53
77 #define TRACE_HEADER_LEN 32
79 #define FILL_PATTERN 0xeeeeeeee
83 * Returns true if the thread CPU clock should be used.
85 static inline bool useThreadCpuClock() {
86 #if defined(HAVE_POSIX_CLOCKS)
87 return gDvm.profilerClockSource != kProfilerClockSourceWall;
94 * Returns true if the wall clock should be used.
96 static inline bool useWallClock() {
97 #if defined(HAVE_POSIX_CLOCKS)
98 return gDvm.profilerClockSource != kProfilerClockSourceThreadCpu;
105 * Get the wall-clock date/time, in usec.
107 static inline u8 getWallTimeInUsec()
111 gettimeofday(&tv, NULL);
112 return tv.tv_sec * 1000000LL + tv.tv_usec;
115 #if defined(HAVE_POSIX_CLOCKS)
117 * Get the thread-cpu time, in usec.
118 * We use this clock when we can because it enables us to track the time that
119 * a thread spends running and not blocked.
121 static inline u8 getThreadCpuTimeInUsec()
125 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm);
126 if (!(tm.tv_nsec >= 0 && tm.tv_nsec < 1*1000*1000*1000)) {
127 ALOGE("bad nsec: %ld", tm.tv_nsec);
130 return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000;
135 * Get the clock used for stopwatch-like timing measurements on a single thread.
137 static inline u8 getStopwatchClock()
139 #if defined(HAVE_POSIX_CLOCKS)
140 return getThreadCpuTimeInUsec();
142 return getWallTimeInUsec();
147 * Write little-endian data.
149 static inline void storeShortLE(u1* buf, u2 val)
152 *buf++ = (u1) (val >> 8);
154 static inline void storeIntLE(u1* buf, u4 val)
157 *buf++ = (u1) (val >> 8);
158 *buf++ = (u1) (val >> 16);
159 *buf++ = (u1) (val >> 24);
161 static inline void storeLongLE(u1* buf, u8 val)
164 *buf++ = (u1) (val >> 8);
165 *buf++ = (u1) (val >> 16);
166 *buf++ = (u1) (val >> 24);
167 *buf++ = (u1) (val >> 32);
168 *buf++ = (u1) (val >> 40);
169 *buf++ = (u1) (val >> 48);
170 *buf++ = (u1) (val >> 56);
176 bool dvmProfilingStartup()
179 * Initialize "dmtrace" method profiling.
181 memset(&gDvm.methodTrace, 0, sizeof(gDvm.methodTrace));
182 dvmInitMutex(&gDvm.methodTrace.startStopLock);
183 pthread_cond_init(&gDvm.methodTrace.threadExitCond, NULL);
185 assert(!dvmCheckException(dvmThreadSelf()));
188 * Allocate storage for instruction counters.
190 gDvm.executedInstrCounts = (int*) calloc(kNumPackedOpcodes, sizeof(int));
191 if (gDvm.executedInstrCounts == NULL)
194 #ifdef UPDATE_MAGIC_PAGE
196 * If we're running on the emulator, there's a magic page into which
197 * we can put interpreted method information. This allows interpreted
198 * methods to show up in the emulator's code traces.
200 * We could key this off of the "ro.kernel.qemu" property, but there's
201 * no real harm in doing this on a real device.
203 int fd = open("/dev/qemu_trace", O_RDWR);
205 ALOGV("Unable to open /dev/qemu_trace");
207 gDvm.emulatorTracePage = mmap(0, SYSTEM_PAGE_SIZE, PROT_READ|PROT_WRITE,
210 if (gDvm.emulatorTracePage == MAP_FAILED) {
211 ALOGE("Unable to mmap /dev/qemu_trace");
212 gDvm.emulatorTracePage = NULL;
214 *(u4*) gDvm.emulatorTracePage = 0;
218 assert(gDvm.emulatorTracePage == NULL);
225 * Free up profiling resources.
227 void dvmProfilingShutdown()
229 #ifdef UPDATE_MAGIC_PAGE
230 if (gDvm.emulatorTracePage != NULL)
231 munmap(gDvm.emulatorTracePage, SYSTEM_PAGE_SIZE);
233 free(gDvm.executedInstrCounts);
237 * Update the set of active profilers
239 static void updateActiveProfilers(ExecutionSubModes newMode, bool enable)
241 int oldValue, newValue;
243 // Update global count
245 oldValue = gDvm.activeProfilers;
246 newValue = oldValue + (enable ? 1 : -1);
248 ALOGE("Can't have %d active profilers", newValue);
251 } while (android_atomic_release_cas(oldValue, newValue,
252 &gDvm.activeProfilers) != 0);
256 dvmEnableAllSubMode(newMode);
258 dvmDisableAllSubMode(newMode);
261 #if defined(WITH_JIT)
262 dvmCompilerUpdateGlobalState();
265 ALOGD("+++ active profiler count now %d", newValue);
270 * Reset the "cpuClockBase" field in all threads.
272 static void resetCpuClockBase()
276 dvmLockThreadList(NULL);
277 for (thread = gDvm.threadList; thread != NULL; thread = thread->next) {
278 thread->cpuClockBaseSet = false;
279 thread->cpuClockBase = 0;
281 dvmUnlockThreadList();
285 * Dump the thread list to the specified file.
287 static void dumpThreadList(FILE* fp) {
288 dvmLockThreadList(NULL);
289 for (Thread* thread = gDvm.threadList; thread != NULL; thread = thread->next) {
290 std::string threadName(dvmGetThreadName(thread));
291 fprintf(fp, "%d\t%s\n", thread->threadId, threadName.c_str());
293 dvmUnlockThreadList();
297 * This is a dvmHashForeach callback.
299 static int dumpMarkedMethods(void* vclazz, void* vfp)
301 DexStringCache stringCache;
302 ClassObject* clazz = (ClassObject*) vclazz;
303 FILE* fp = (FILE*) vfp;
308 dexStringCacheInit(&stringCache);
310 for (i = 0; i < clazz->virtualMethodCount; i++) {
311 meth = &clazz->virtualMethods[i];
312 if (meth->inProfile) {
313 name = dvmDescriptorToName(meth->clazz->descriptor);
314 fprintf(fp, "0x%08x\t%s\t%s\t%s\t%s\t%d\n", (int) meth,
316 dexProtoGetMethodDescriptor(&meth->prototype, &stringCache),
317 dvmGetMethodSourceFile(meth), dvmLineNumFromPC(meth, 0));
318 meth->inProfile = false;
323 for (i = 0; i < clazz->directMethodCount; i++) {
324 meth = &clazz->directMethods[i];
325 if (meth->inProfile) {
326 name = dvmDescriptorToName(meth->clazz->descriptor);
327 fprintf(fp, "0x%08x\t%s\t%s\t%s\t%s\t%d\n", (int) meth,
329 dexProtoGetMethodDescriptor(&meth->prototype, &stringCache),
330 dvmGetMethodSourceFile(meth), dvmLineNumFromPC(meth, 0));
331 meth->inProfile = false;
336 dexStringCacheRelease(&stringCache);
342 * Dump the list of "marked" methods to the specified file.
344 static void dumpMethodList(FILE* fp)
346 dvmHashTableLock(gDvm.loadedClasses);
347 dvmHashForeach(gDvm.loadedClasses, dumpMarkedMethods, (void*) fp);
348 dvmHashTableUnlock(gDvm.loadedClasses);
352 * Start method tracing. Method tracing is global to the VM (i.e. we
353 * trace all threads).
355 * This opens the output file (if an already open fd has not been supplied,
356 * and we're not going direct to DDMS) and allocates the data buffer. This
357 * takes ownership of the file descriptor, closing it on completion.
359 * On failure, we throw an exception and return.
361 void dvmMethodTraceStart(const char* traceFileName, int traceFd, int bufferSize,
362 int flags, bool directToDdms)
364 MethodTraceState* state = &gDvm.methodTrace;
366 assert(bufferSize > 0);
368 dvmLockMutex(&state->startStopLock);
369 while (state->traceEnabled != 0) {
370 ALOGI("TRACE start requested, but already in progress; stopping");
371 dvmUnlockMutex(&state->startStopLock);
372 dvmMethodTraceStop();
373 dvmLockMutex(&state->startStopLock);
375 ALOGI("TRACE STARTED: '%s' %dKB", traceFileName, bufferSize / 1024);
378 * Allocate storage and open files.
380 * We don't need to initialize the buffer, but doing so might remove
381 * some fault overhead if the pages aren't mapped until touched.
383 state->buf = (u1*) malloc(bufferSize);
384 if (state->buf == NULL) {
385 dvmThrowInternalError("buffer alloc failed");
390 state->traceFile = fopen(traceFileName, "w");
392 state->traceFile = fdopen(traceFd, "w");
394 if (state->traceFile == NULL) {
396 ALOGE("Unable to open trace file '%s': %s",
397 traceFileName, strerror(err));
398 dvmThrowExceptionFmt(gDvm.exRuntimeException,
399 "Unable to open trace file '%s': %s",
400 traceFileName, strerror(err));
405 memset(state->buf, (char)FILL_PATTERN, bufferSize);
407 state->directToDdms = directToDdms;
408 state->bufferSize = bufferSize;
409 state->overflow = false;
412 * Enable alloc counts if we've been requested to do so.
414 state->flags = flags;
415 if ((flags & TRACE_ALLOC_COUNTS) != 0)
416 dvmStartAllocCounting();
418 /* reset our notion of the start time for all CPU threads */
421 state->startWhen = getWallTimeInUsec();
423 if (useThreadCpuClock() && useWallClock()) {
424 state->traceVersion = 3;
425 state->recordSize = TRACE_REC_SIZE_DUAL_CLOCK;
427 state->traceVersion = 2;
428 state->recordSize = TRACE_REC_SIZE_SINGLE_CLOCK;
434 memset(state->buf, 0, TRACE_HEADER_LEN);
435 storeIntLE(state->buf + 0, TRACE_MAGIC);
436 storeShortLE(state->buf + 4, state->traceVersion);
437 storeShortLE(state->buf + 6, TRACE_HEADER_LEN);
438 storeLongLE(state->buf + 8, state->startWhen);
439 if (state->traceVersion >= 3) {
440 storeShortLE(state->buf + 16, state->recordSize);
442 state->curOffset = TRACE_HEADER_LEN;
445 * Set the "enabled" flag. Once we do this, threads will wait to be
446 * signaled before exiting, so we have to make sure we wake them up.
448 android_atomic_release_store(true, &state->traceEnabled);
451 * ENHANCEMENT: To trace just a single thread, modify the
452 * following to take a Thread* argument, and set the appropriate
453 * interpBreak flags only on the target thread.
455 updateActiveProfilers(kSubModeMethodTrace, true);
457 dvmUnlockMutex(&state->startStopLock);
461 if (state->traceFile != NULL) {
462 fclose(state->traceFile);
463 state->traceFile = NULL;
465 if (state->buf != NULL) {
471 dvmUnlockMutex(&state->startStopLock);
475 * Run through the data buffer and pull out the methods that were visited.
476 * Set a mark so that we know which ones to output.
478 static void markTouchedMethods(int endOffset)
480 u1* ptr = gDvm.methodTrace.buf + TRACE_HEADER_LEN;
481 u1* end = gDvm.methodTrace.buf + endOffset;
482 size_t recordSize = gDvm.methodTrace.recordSize;
483 unsigned int methodVal;
487 methodVal = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16)
489 method = (Method*) METHOD_ID(methodVal);
491 method->inProfile = true;
497 * Exercises the clocks in the same way they will be during profiling.
499 static inline void measureClockOverhead()
501 #if defined(HAVE_POSIX_CLOCKS)
502 if (useThreadCpuClock()) {
503 getThreadCpuTimeInUsec();
506 if (useWallClock()) {
512 * Compute the amount of overhead in a clock call, in nsec.
514 * This value is going to vary depending on what else is going on in the
515 * system. When examined across several runs a pattern should emerge.
517 static u4 getClockOverhead()
519 u8 calStart, calElapsed;
522 calStart = getStopwatchClock();
523 for (i = 1000 * 4; i > 0; i--) {
524 measureClockOverhead();
525 measureClockOverhead();
526 measureClockOverhead();
527 measureClockOverhead();
528 measureClockOverhead();
529 measureClockOverhead();
530 measureClockOverhead();
531 measureClockOverhead();
534 calElapsed = getStopwatchClock() - calStart;
535 return (int) (calElapsed / (8*4));
539 * Returns "true" if method tracing is currently active.
541 bool dvmIsMethodTraceActive()
543 const MethodTraceState* state = &gDvm.methodTrace;
544 return state->traceEnabled;
548 * Stop method tracing. We write the buffer to disk and generate a key
549 * file so we can interpret it.
551 void dvmMethodTraceStop()
553 MethodTraceState* state = &gDvm.methodTrace;
557 * We need this to prevent somebody from starting a new trace while
558 * we're in the process of stopping the old.
560 dvmLockMutex(&state->startStopLock);
562 if (!state->traceEnabled) {
563 /* somebody already stopped it, or it was never started */
564 ALOGD("TRACE stop requested, but not running");
565 dvmUnlockMutex(&state->startStopLock);
568 updateActiveProfilers(kSubModeMethodTrace, false);
571 /* compute elapsed time */
572 elapsed = getWallTimeInUsec() - state->startWhen;
575 * Globally disable it, and allow other threads to notice. We want
576 * to stall here for at least as long as dvmMethodTraceAdd needs
577 * to finish. There's no real risk though -- it will take a while to
578 * write the data to disk, and we don't clear the buffer pointer until
579 * after that completes.
581 state->traceEnabled = false;
582 ANDROID_MEMBAR_FULL();
586 if ((state->flags & TRACE_ALLOC_COUNTS) != 0)
587 dvmStopAllocCounting();
590 * It's possible under some circumstances for a thread to have advanced
591 * the data pointer but not written the method value. It's possible
592 * (though less likely) for the data pointer to be advanced, or partial
593 * data written, while we're doing work here.
595 * To avoid seeing partially-written data, we grab state->curOffset here,
596 * and use our local copy from here on. We then scan through what's
597 * already written. If we see the fill pattern in what should be the
598 * method pointer, we cut things off early. (If we don't, we'll fail
599 * when we dereference the pointer.)
601 * There's a theoretical possibility of interrupting another thread
602 * after it has partially written the method pointer, in which case
603 * we'll likely crash when we dereference it. The possibility of
604 * this actually happening should be at or near zero. Fixing it
605 * completely could be done by writing the thread number last and
606 * using a sentinel value to indicate a partially-written record,
607 * but that requires memory barriers.
609 int finalCurOffset = state->curOffset;
611 size_t recordSize = state->recordSize;
612 if (finalCurOffset > TRACE_HEADER_LEN) {
613 u4 fillVal = METHOD_ID(FILL_PATTERN);
614 u1* scanPtr = state->buf + TRACE_HEADER_LEN;
616 while (scanPtr < state->buf + finalCurOffset) {
617 u4 methodVal = scanPtr[2] | (scanPtr[3] << 8) | (scanPtr[4] << 16)
618 | (scanPtr[5] << 24);
619 if (METHOD_ID(methodVal) == fillVal) {
620 u1* scanBase = state->buf + TRACE_HEADER_LEN;
621 ALOGW("Found unfilled record at %d (of %d)",
622 (scanPtr - scanBase) / recordSize,
623 (finalCurOffset - TRACE_HEADER_LEN) / recordSize);
624 finalCurOffset = scanPtr - state->buf;
628 scanPtr += recordSize;
632 ALOGI("TRACE STOPPED%s: writing %d records",
633 state->overflow ? " (NOTE: overflowed buffer)" : "",
634 (finalCurOffset - TRACE_HEADER_LEN) / recordSize);
635 if (gDvm.debuggerActive) {
636 ALOGW("WARNING: a debugger is active; method-tracing results "
641 * Do a quick calibration test to see how expensive our clock call is.
643 u4 clockNsec = getClockOverhead();
645 markTouchedMethods(finalCurOffset);
648 size_t memStreamSize;
649 if (state->directToDdms) {
650 assert(state->traceFile == NULL);
651 state->traceFile = open_memstream(&memStreamPtr, &memStreamSize);
652 if (state->traceFile == NULL) {
654 ALOGE("Unable to open memstream");
658 assert(state->traceFile != NULL);
660 fprintf(state->traceFile, "%cversion\n", TOKEN_CHAR);
661 fprintf(state->traceFile, "%d\n", state->traceVersion);
662 fprintf(state->traceFile, "data-file-overflow=%s\n",
663 state->overflow ? "true" : "false");
664 if (useThreadCpuClock()) {
665 if (useWallClock()) {
666 fprintf(state->traceFile, "clock=dual\n");
668 fprintf(state->traceFile, "clock=thread-cpu\n");
671 fprintf(state->traceFile, "clock=wall\n");
673 fprintf(state->traceFile, "elapsed-time-usec=%llu\n", elapsed);
674 fprintf(state->traceFile, "num-method-calls=%d\n",
675 (finalCurOffset - TRACE_HEADER_LEN) / state->recordSize);
676 fprintf(state->traceFile, "clock-call-overhead-nsec=%d\n", clockNsec);
677 fprintf(state->traceFile, "vm=dalvik\n");
678 if ((state->flags & TRACE_ALLOC_COUNTS) != 0) {
679 fprintf(state->traceFile, "alloc-count=%d\n",
680 gDvm.allocProf.allocCount);
681 fprintf(state->traceFile, "alloc-size=%d\n",
682 gDvm.allocProf.allocSize);
683 fprintf(state->traceFile, "gc-count=%d\n",
684 gDvm.allocProf.gcCount);
686 fprintf(state->traceFile, "%cthreads\n", TOKEN_CHAR);
687 dumpThreadList(state->traceFile);
688 fprintf(state->traceFile, "%cmethods\n", TOKEN_CHAR);
689 dumpMethodList(state->traceFile);
690 fprintf(state->traceFile, "%cend\n", TOKEN_CHAR);
692 if (state->directToDdms) {
694 * Data is in two places: memStreamPtr and state->buf. Send
695 * the whole thing to DDMS, wrapped in an MPSE packet.
697 fflush(state->traceFile);
700 iov[0].iov_base = memStreamPtr;
701 iov[0].iov_len = memStreamSize;
702 iov[1].iov_base = state->buf;
703 iov[1].iov_len = finalCurOffset;
704 dvmDbgDdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
706 /* append the profiling data */
707 if (fwrite(state->buf, finalCurOffset, 1, state->traceFile) != 1) {
709 ALOGE("trace fwrite(%d) failed: %s",
710 finalCurOffset, strerror(err));
711 dvmThrowExceptionFmt(gDvm.exRuntimeException,
712 "Trace data write failed: %s", strerror(err));
719 fclose(state->traceFile);
720 state->traceFile = NULL;
722 /* wake any threads that were waiting for profiling to complete */
723 dvmBroadcastCond(&state->threadExitCond);
724 dvmUnlockMutex(&state->startStopLock);
728 * We just did something with a method. Emit a record.
730 * Multiple threads may be banging on this all at once. We use atomic ops
731 * rather than mutexes for speed.
733 void dvmMethodTraceAdd(Thread* self, const Method* method, int action)
735 MethodTraceState* state = &gDvm.methodTrace;
737 int oldOffset, newOffset;
740 assert(method != NULL);
742 #if defined(HAVE_POSIX_CLOCKS)
744 * We can only access the per-thread CPU clock from within the
745 * thread, so we have to initialize the base time on the first use.
746 * (Looks like pthread_getcpuclockid(thread, &id) will do what we
747 * want, but it doesn't appear to be defined on the device.)
749 if (!self->cpuClockBaseSet) {
750 self->cpuClockBase = getThreadCpuTimeInUsec();
751 self->cpuClockBaseSet = true;
752 //ALOGI("thread base id=%d 0x%llx",
753 // self->threadId, self->cpuClockBase);
758 * Advance "curOffset" atomically.
761 oldOffset = state->curOffset;
762 newOffset = oldOffset + state->recordSize;
763 if (newOffset > state->bufferSize) {
764 state->overflow = true;
767 } while (android_atomic_release_cas(oldOffset, newOffset,
768 &state->curOffset) != 0);
770 //assert(METHOD_ACTION((u4) method) == 0);
772 methodVal = METHOD_COMBINE((u4) method, action);
775 * Write data into "oldOffset".
777 ptr = state->buf + oldOffset;
778 *ptr++ = (u1) self->threadId;
779 *ptr++ = (u1) (self->threadId >> 8);
780 *ptr++ = (u1) methodVal;
781 *ptr++ = (u1) (methodVal >> 8);
782 *ptr++ = (u1) (methodVal >> 16);
783 *ptr++ = (u1) (methodVal >> 24);
785 #if defined(HAVE_POSIX_CLOCKS)
786 if (useThreadCpuClock()) {
787 u4 cpuClockDiff = (u4) (getThreadCpuTimeInUsec() - self->cpuClockBase);
788 *ptr++ = (u1) cpuClockDiff;
789 *ptr++ = (u1) (cpuClockDiff >> 8);
790 *ptr++ = (u1) (cpuClockDiff >> 16);
791 *ptr++ = (u1) (cpuClockDiff >> 24);
795 if (useWallClock()) {
796 u4 wallClockDiff = (u4) (getWallTimeInUsec() - state->startWhen);
797 *ptr++ = (u1) wallClockDiff;
798 *ptr++ = (u1) (wallClockDiff >> 8);
799 *ptr++ = (u1) (wallClockDiff >> 16);
800 *ptr++ = (u1) (wallClockDiff >> 24);
806 * Register the METHOD_TRACE_ENTER action for the fast interpreter and
809 void dvmFastMethodTraceEnter(const Method* method, Thread* self)
811 if (self->interpBreak.ctl.subMode & kSubModeMethodTrace) {
812 dvmMethodTraceAdd(self, method, METHOD_TRACE_ENTER);
817 * Register the METHOD_TRACE_EXIT action for the fast interpreter and
818 * JIT'ed code for methods. The about-to-return callee method can be
819 * retrieved from self->interpSave.method.
821 void dvmFastMethodTraceExit(Thread* self)
823 if (self->interpBreak.ctl.subMode & kSubModeMethodTrace) {
824 dvmMethodTraceAdd(self, self->interpSave.method,
830 * Register the METHOD_TRACE_EXIT action for the fast interpreter and
831 * JIT'ed code for JNI methods. The about-to-return JNI callee method is passed
832 * in explicitly. Also used for inline-execute.
834 void dvmFastNativeMethodTraceExit(const Method* method, Thread* self)
836 if (self->interpBreak.ctl.subMode & kSubModeMethodTrace) {
837 dvmMethodTraceAdd(self, method, METHOD_TRACE_EXIT);
842 * We just did something with a method. Emit a record by setting a value
843 * in a magic memory location.
845 void dvmEmitEmulatorTrace(const Method* method, int action)
847 #ifdef UPDATE_MAGIC_PAGE
849 * We store the address of the Dalvik bytecodes to the memory-mapped
850 * trace page for normal methods. We also trace calls to native
851 * functions by storing the address of the native function to the
853 * Abstract methods don't have any bytecodes, so we don't trace them.
854 * (Abstract methods are never called, but in Dalvik they can be
855 * because we do a "late trap" to a native method to generate the
856 * abstract method exception.)
858 if (dvmIsAbstractMethod(method))
861 u4* pMagic = (u4*) gDvm.emulatorTracePage;
864 if (dvmIsNativeMethod(method)) {
866 * The "action" parameter is one of:
870 * To help the trace tools reconstruct the runtime stack containing
871 * a mix of normal plus native methods, we add 4 to the action if this
872 * is a native method.
877 * Get the address of the native function.
878 * This isn't the right address -- how do I get it?
879 * Fortunately, the trace tools can get by without the address, but
880 * it would be nice to fix this.
882 addr = (u4) method->nativeFunc;
885 * The dexlist output shows the &DexCode.insns offset value, which
886 * is offset from the start of the base DEX header. Method.insns
887 * is the absolute address, effectively offset from the start of
888 * the optimized DEX header. We either need to return the
889 * optimized DEX base file address offset by the right amount, or
890 * take the "real" address and subtract off the size of the
891 * optimized DEX header.
893 * Would be nice to factor this out at dexlist time, but we can't count
894 * on having access to the correct optimized DEX file.
896 assert(method->insns != NULL);
897 const DexOptHeader* pOptHdr = method->clazz->pDvmDex->pDexFile->pOptHeader;
898 addr = (u4) method->insns - pOptHdr->dexOffset;
901 *(pMagic+action) = addr;
902 LOGVV("Set %p = 0x%08x (%s.%s)",
903 pMagic+action, addr, method->clazz->descriptor, method->name);
908 * The GC calls this when it's about to start. We add a marker to the
909 * trace output so the tool can exclude the GC cost from the results.
911 void dvmMethodTraceGCBegin()
913 TRACE_METHOD_ENTER(dvmThreadSelf(), gDvm.methodTraceGcMethod);
915 void dvmMethodTraceGCEnd()
917 TRACE_METHOD_EXIT(dvmThreadSelf(), gDvm.methodTraceGcMethod);
921 * The class loader calls this when it's loading or initializing a class.
923 void dvmMethodTraceClassPrepBegin()
925 TRACE_METHOD_ENTER(dvmThreadSelf(), gDvm.methodTraceClassPrepMethod);
927 void dvmMethodTraceClassPrepEnd()
929 TRACE_METHOD_EXIT(dvmThreadSelf(), gDvm.methodTraceClassPrepMethod);
934 * Enable emulator trace info.
936 void dvmEmulatorTraceStart()
938 /* If we could not map the emulator trace page, then do not enable tracing */
939 if (gDvm.emulatorTracePage == NULL)
942 /* in theory we should make this an atomic inc; in practice not important */
943 gDvm.emulatorTraceEnableCount++;
944 if (gDvm.emulatorTraceEnableCount == 1)
945 ALOGD("--- emulator method traces enabled");
946 updateActiveProfilers(kSubModeEmulatorTrace, true);
950 * Disable emulator trace info.
952 void dvmEmulatorTraceStop()
954 if (gDvm.emulatorTraceEnableCount == 0) {
955 ALOGE("ERROR: emulator tracing not enabled");
958 /* in theory we should make this an atomic inc; in practice not important */
959 gDvm.emulatorTraceEnableCount--;
960 if (gDvm.emulatorTraceEnableCount == 0)
961 ALOGD("--- emulator method traces disabled");
962 updateActiveProfilers(kSubModeEmulatorTrace,
963 (gDvm.emulatorTraceEnableCount != 0));
968 * Start instruction counting.
970 void dvmStartInstructionCounting()
972 /* in theory we should make this an atomic inc; in practice not important */
973 gDvm.instructionCountEnableCount++;
974 updateActiveProfilers(kSubModeInstCounting, true);
978 * Stop instruction counting.
980 void dvmStopInstructionCounting()
982 if (gDvm.instructionCountEnableCount == 0) {
983 ALOGE("ERROR: instruction counting not enabled");
986 gDvm.instructionCountEnableCount--;
987 updateActiveProfilers(kSubModeInstCounting,
988 (gDvm.instructionCountEnableCount != 0));
993 * Start alloc counting. Note this doesn't affect the "active profilers"
994 * count, since the interpreter loop is not involved.
996 void dvmStartAllocCounting()
998 gDvm.allocProf.enabled = true;
1002 * Stop alloc counting.
1004 void dvmStopAllocCounting()
1006 gDvm.allocProf.enabled = false;