| 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 | */ |
| 16 | |
| 17 | /* |
| 18 | * Preparation and completion of hprof data generation. The output is |
| 19 | * written into two files and then combined. This is necessary because |
| 20 | * we generate some of the data (strings and classes) while we dump the |
| 21 | * heap, and some analysis tools require that the class and string data |
| 22 | * appear first. |
| 23 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 24 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | #include "Hprof.h" |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 26 | #include "alloc/HeapInternal.h" |
| 27 | #include "alloc/Visit.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 31 | #include <fcntl.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <time.h> |
| 35 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 36 | #define kHeadSuffix "-hptemp" |
| 37 | |
| 38 | hprof_context_t * |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 39 | hprofStartup(const char *outputFileName, int fd, bool directToDdms) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 40 | { |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 41 | hprofStartup_String(); |
| 42 | hprofStartup_Class(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 43 | #if WITH_HPROF_STACK |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 44 | hprofStartup_StackFrame(); |
| 45 | hprofStartup_Stack(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | #endif |
| 47 | |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 48 | hprof_context_t *ctx = malloc(sizeof(*ctx)); |
| 49 | if (ctx == NULL) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 50 | LOGE("hprof: can't allocate context.\n"); |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 51 | return NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 54 | /* pass in name or descriptor of the output file */ |
| 55 | hprofContextInit(ctx, strdup(outputFileName), fd, false, directToDdms); |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 56 | |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 57 | assert(ctx->memFp != NULL); |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 58 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | return ctx; |
| 60 | } |
| 61 | |
| 62 | /* |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 63 | * Finish up the hprof dump. Returns true on success. |
| 64 | */ |
| 65 | bool |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 66 | hprofShutdown(hprof_context_t *tailCtx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 67 | { |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 68 | /* flush the "tail" portion of the output */ |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 69 | hprofFlushCurrentRecord(tailCtx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 70 | |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 71 | /* |
| 72 | * Create a new context struct for the start of the file. We |
| 73 | * heap-allocate it so we can share the "free" function. |
| 74 | */ |
| 75 | hprof_context_t *headCtx = malloc(sizeof(*headCtx)); |
| 76 | if (headCtx == NULL) { |
| 77 | LOGE("hprof: can't allocate context.\n"); |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 78 | hprofFreeContext(tailCtx); |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 79 | return false; |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 80 | } |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 81 | hprofContextInit(headCtx, strdup(tailCtx->fileName), tailCtx->fd, true, |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 82 | tailCtx->directToDdms); |
| 83 | |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 84 | LOGI("hprof: dumping heap strings to \"%s\".\n", tailCtx->fileName); |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 85 | hprofDumpStrings(headCtx); |
| 86 | hprofDumpClasses(headCtx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 87 | |
| 88 | /* Write a dummy stack trace record so the analysis |
| 89 | * tools don't freak out. |
| 90 | */ |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 91 | hprofStartNewRecord(headCtx, HPROF_TAG_STACK_TRACE, HPROF_TIME); |
| 92 | hprofAddU4ToRecord(&headCtx->curRec, HPROF_NULL_STACK_TRACE); |
| 93 | hprofAddU4ToRecord(&headCtx->curRec, HPROF_NULL_THREAD); |
| 94 | hprofAddU4ToRecord(&headCtx->curRec, 0); // no frames |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 95 | |
| 96 | #if WITH_HPROF_STACK |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 97 | hprofDumpStackFrames(headCtx); |
| 98 | hprofDumpStacks(headCtx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 99 | #endif |
| 100 | |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 101 | hprofFlushCurrentRecord(headCtx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | |
| 103 | hprofShutdown_Class(); |
| 104 | hprofShutdown_String(); |
| 105 | #if WITH_HPROF_STACK |
| 106 | hprofShutdown_Stack(); |
| 107 | hprofShutdown_StackFrame(); |
| 108 | #endif |
| 109 | |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 110 | /* flush to ensure memstream pointer and size are updated */ |
| 111 | fflush(headCtx->memFp); |
| 112 | fflush(tailCtx->memFp); |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 113 | |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 114 | if (tailCtx->directToDdms) { |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 115 | /* send the data off to DDMS */ |
| 116 | struct iovec iov[2]; |
| 117 | iov[0].iov_base = headCtx->fileDataPtr; |
| 118 | iov[0].iov_len = headCtx->fileDataSize; |
| 119 | iov[1].iov_base = tailCtx->fileDataPtr; |
| 120 | iov[1].iov_len = tailCtx->fileDataSize; |
| 121 | dvmDbgDdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2); |
| 122 | } else { |
| 123 | /* |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 124 | * Open the output file, and copy the head and tail to it. |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 125 | */ |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 126 | assert(headCtx->fd == tailCtx->fd); |
| 127 | |
| 128 | int outFd; |
| 129 | if (headCtx->fd >= 0) { |
| 130 | outFd = dup(headCtx->fd); |
| 131 | if (outFd < 0) { |
| 132 | LOGE("dup(%d) failed: %s\n", headCtx->fd, strerror(errno)); |
| 133 | /* continue to fail-handler below */ |
| 134 | } |
| 135 | } else { |
| Brian Carlstrom | 8f192cc | 2010-10-18 15:37:24 -0700 | [diff] [blame] | 136 | outFd = open(tailCtx->fileName, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 137 | if (outFd < 0) { |
| 138 | LOGE("can't open %s: %s\n", headCtx->fileName, strerror(errno)); |
| 139 | /* continue to fail-handler below */ |
| 140 | } |
| 141 | } |
| 142 | if (outFd < 0) { |
| 143 | hprofFreeContext(headCtx); |
| 144 | hprofFreeContext(tailCtx); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | int result; |
| 149 | result = sysWriteFully(outFd, headCtx->fileDataPtr, |
| 150 | headCtx->fileDataSize, "hprof-head"); |
| 151 | result |= sysWriteFully(outFd, tailCtx->fileDataPtr, |
| 152 | tailCtx->fileDataSize, "hprof-tail"); |
| 153 | close(outFd); |
| 154 | if (result != 0) { |
| 155 | hprofFreeContext(headCtx); |
| 156 | hprofFreeContext(tailCtx); |
| 157 | return false; |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 158 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 161 | /* throw out a log message for the benefit of "runhat" */ |
| 162 | LOGI("hprof: heap dump completed (%dKB)\n", |
| 163 | (headCtx->fileDataSize + tailCtx->fileDataSize + 1023) / 1024); |
| 164 | |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 165 | hprofFreeContext(headCtx); |
| 166 | hprofFreeContext(tailCtx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 167 | |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 168 | return true; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 169 | } |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Free any heap-allocated items in "ctx", and then free "ctx" itself. |
| 173 | */ |
| 174 | void |
| 175 | hprofFreeContext(hprof_context_t *ctx) |
| 176 | { |
| 177 | assert(ctx != NULL); |
| 178 | |
| Andy McFadden | 4b851a7 | 2010-07-09 16:50:05 -0700 | [diff] [blame] | 179 | /* we don't own ctx->fd, do not close */ |
| 180 | |
| 181 | if (ctx->memFp != NULL) |
| 182 | fclose(ctx->memFp); |
| Andy McFadden | 6bf992c | 2010-01-28 17:01:39 -0800 | [diff] [blame] | 183 | free(ctx->curRec.body); |
| 184 | free(ctx->fileName); |
| 185 | free(ctx->fileDataPtr); |
| 186 | free(ctx); |
| 187 | } |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * Visitor invoked on every root reference. |
| 191 | */ |
| 192 | static void hprofRootVisitor(void *addr, u4 threadId, RootType type, void *arg) |
| 193 | { |
| 194 | static const hprof_heap_tag_t xlate[] = { |
| 195 | HPROF_ROOT_UNKNOWN, |
| 196 | HPROF_ROOT_JNI_GLOBAL, |
| 197 | HPROF_ROOT_JNI_LOCAL, |
| 198 | HPROF_ROOT_JAVA_FRAME, |
| 199 | HPROF_ROOT_NATIVE_STACK, |
| 200 | HPROF_ROOT_STICKY_CLASS, |
| 201 | HPROF_ROOT_THREAD_BLOCK, |
| 202 | HPROF_ROOT_MONITOR_USED, |
| 203 | HPROF_ROOT_THREAD_OBJECT, |
| 204 | HPROF_ROOT_INTERNED_STRING, |
| 205 | HPROF_ROOT_FINALIZING, |
| 206 | HPROF_ROOT_DEBUGGER, |
| 207 | HPROF_ROOT_REFERENCE_CLEANUP, |
| 208 | HPROF_ROOT_VM_INTERNAL, |
| 209 | HPROF_ROOT_JNI_MONITOR, |
| 210 | }; |
| 211 | hprof_context_t *ctx; |
| 212 | |
| 213 | assert(arg != NULL); |
| 214 | assert(type < NELEM(xlate)); |
| 215 | ctx = arg; |
| 216 | ctx->gcScanState = xlate[type]; |
| 217 | ctx->gcThreadSerialNumber = threadId; |
| 218 | hprofMarkRootObject(ctx, addr, 0); |
| 219 | ctx->gcScanState = 0; |
| 220 | ctx->gcThreadSerialNumber = 0; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Visitor invoked on every heap object. |
| 225 | */ |
| 226 | static void hprofBitmapCallback(void *ptr, void *arg) |
| 227 | { |
| 228 | Object *obj; |
| 229 | hprof_context_t *ctx; |
| 230 | |
| 231 | assert(ptr != NULL); |
| 232 | assert(arg != NULL); |
| 233 | obj = ptr; |
| 234 | ctx = arg; |
| 235 | hprofDumpHeapObject(ctx, obj); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Walk the roots and heap writing heap information to the specified |
| 240 | * file. |
| 241 | * |
| 242 | * If "fd" is >= 0, the output will be written to that file descriptor. |
| 243 | * Otherwise, "fileName" is used to create an output file. |
| 244 | * |
| 245 | * If "directToDdms" is set, the other arguments are ignored, and data is |
| 246 | * sent directly to DDMS. |
| 247 | * |
| 248 | * Returns 0 on success, or an error code on failure. |
| 249 | */ |
| 250 | int hprofDumpHeap(const char* fileName, int fd, bool directToDdms) |
| 251 | { |
| 252 | hprof_context_t *ctx; |
| 253 | int success; |
| 254 | |
| 255 | assert(fileName != NULL); |
| 256 | dvmLockHeap(); |
| 257 | dvmSuspendAllThreads(SUSPEND_FOR_HPROF); |
| 258 | ctx = hprofStartup(fileName, fd, directToDdms); |
| 259 | if (ctx == NULL) { |
| 260 | return -1; |
| 261 | } |
| 262 | dvmVisitRoots(hprofRootVisitor, ctx); |
| 263 | dvmHeapBitmapWalk(dvmHeapSourceGetLiveBits(), hprofBitmapCallback, ctx); |
| 264 | hprofFinishHeapDump(ctx); |
| 265 | //TODO: write a HEAP_SUMMARY record |
| 266 | success = hprofShutdown(ctx) ? 0 : -1; |
| Carl Shapiro | c921c82 | 2010-10-29 19:08:57 -0700 | [diff] [blame^] | 267 | dvmResumeAllThreads(SUSPEND_FOR_HPROF); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 268 | dvmUnlockHeap(); |
| 269 | return success; |
| 270 | } |