blob: ff8e8728765ece95c1007b9e6422ca49bd609848 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 Shapiro07018e22010-10-26 21:07:41 -070024
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080025#include "Hprof.h"
Carl Shapiro07018e22010-10-26 21:07:41 -070026#include "alloc/HeapInternal.h"
27#include "alloc/Visit.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080028
29#include <string.h>
30#include <unistd.h>
Andy McFadden4b851a72010-07-09 16:50:05 -070031#include <fcntl.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080032#include <errno.h>
33#include <sys/time.h>
34#include <time.h>
35
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080036#define kHeadSuffix "-hptemp"
37
Carl Shapiro06f120f2011-05-11 13:15:32 -070038hprof_context_t* hprofStartup(const char *outputFileName, int fd,
39 bool directToDdms)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080040{
Andy McFadden6bf992c2010-01-28 17:01:39 -080041 hprofStartup_String();
42 hprofStartup_Class();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080043
Carl Shapirofc75f3e2010-12-07 11:43:38 -080044 hprof_context_t *ctx = (hprof_context_t *)malloc(sizeof(*ctx));
Andy McFadden6bf992c2010-01-28 17:01:39 -080045 if (ctx == NULL) {
Dan Bornstein60fc8062011-05-26 10:11:58 -070046 LOGE("hprof: can't allocate context.");
Andy McFadden6bf992c2010-01-28 17:01:39 -080047 return NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080048 }
49
Andy McFadden4b851a72010-07-09 16:50:05 -070050 /* pass in name or descriptor of the output file */
51 hprofContextInit(ctx, strdup(outputFileName), fd, false, directToDdms);
Andy McFadden6bf992c2010-01-28 17:01:39 -080052
Andy McFadden4b851a72010-07-09 16:50:05 -070053 assert(ctx->memFp != NULL);
Andy McFadden6bf992c2010-01-28 17:01:39 -080054
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080055 return ctx;
56}
57
58/*
The Android Open Source Project99409882009-03-18 22:20:24 -070059 * Finish up the hprof dump. Returns true on success.
60 */
Carl Shapiro06f120f2011-05-11 13:15:32 -070061bool hprofShutdown(hprof_context_t *tailCtx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080062{
Andy McFadden4b851a72010-07-09 16:50:05 -070063 /* flush the "tail" portion of the output */
Andy McFadden6bf992c2010-01-28 17:01:39 -080064 hprofFlushCurrentRecord(tailCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080065
Andy McFadden6bf992c2010-01-28 17:01:39 -080066 /*
67 * Create a new context struct for the start of the file. We
68 * heap-allocate it so we can share the "free" function.
69 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -080070 hprof_context_t *headCtx = (hprof_context_t *)malloc(sizeof(*headCtx));
Andy McFadden6bf992c2010-01-28 17:01:39 -080071 if (headCtx == NULL) {
Dan Bornstein60fc8062011-05-26 10:11:58 -070072 LOGE("hprof: can't allocate context.");
Andy McFadden6bf992c2010-01-28 17:01:39 -080073 hprofFreeContext(tailCtx);
Andy McFadden4b851a72010-07-09 16:50:05 -070074 return false;
Andy McFadden6bf992c2010-01-28 17:01:39 -080075 }
Andy McFadden4b851a72010-07-09 16:50:05 -070076 hprofContextInit(headCtx, strdup(tailCtx->fileName), tailCtx->fd, true,
Andy McFadden6bf992c2010-01-28 17:01:39 -080077 tailCtx->directToDdms);
78
Dan Bornstein60fc8062011-05-26 10:11:58 -070079 LOGI("hprof: dumping heap strings to \"%s\".", tailCtx->fileName);
Andy McFadden6bf992c2010-01-28 17:01:39 -080080 hprofDumpStrings(headCtx);
81 hprofDumpClasses(headCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080082
83 /* Write a dummy stack trace record so the analysis
84 * tools don't freak out.
85 */
Andy McFadden6bf992c2010-01-28 17:01:39 -080086 hprofStartNewRecord(headCtx, HPROF_TAG_STACK_TRACE, HPROF_TIME);
87 hprofAddU4ToRecord(&headCtx->curRec, HPROF_NULL_STACK_TRACE);
88 hprofAddU4ToRecord(&headCtx->curRec, HPROF_NULL_THREAD);
89 hprofAddU4ToRecord(&headCtx->curRec, 0); // no frames
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080090
Andy McFadden6bf992c2010-01-28 17:01:39 -080091 hprofFlushCurrentRecord(headCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080092
93 hprofShutdown_Class();
94 hprofShutdown_String();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080095
Andy McFadden4b851a72010-07-09 16:50:05 -070096 /* flush to ensure memstream pointer and size are updated */
97 fflush(headCtx->memFp);
98 fflush(tailCtx->memFp);
Andy McFadden6bf992c2010-01-28 17:01:39 -080099
Andy McFadden4b851a72010-07-09 16:50:05 -0700100 if (tailCtx->directToDdms) {
Andy McFadden6bf992c2010-01-28 17:01:39 -0800101 /* send the data off to DDMS */
102 struct iovec iov[2];
103 iov[0].iov_base = headCtx->fileDataPtr;
104 iov[0].iov_len = headCtx->fileDataSize;
105 iov[1].iov_base = tailCtx->fileDataPtr;
106 iov[1].iov_len = tailCtx->fileDataSize;
107 dvmDbgDdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
108 } else {
109 /*
Andy McFadden4b851a72010-07-09 16:50:05 -0700110 * Open the output file, and copy the head and tail to it.
Andy McFadden6bf992c2010-01-28 17:01:39 -0800111 */
Andy McFadden4b851a72010-07-09 16:50:05 -0700112 assert(headCtx->fd == tailCtx->fd);
113
114 int outFd;
115 if (headCtx->fd >= 0) {
116 outFd = dup(headCtx->fd);
117 if (outFd < 0) {
Dan Bornstein60fc8062011-05-26 10:11:58 -0700118 LOGE("dup(%d) failed: %s", headCtx->fd, strerror(errno));
Andy McFadden4b851a72010-07-09 16:50:05 -0700119 /* continue to fail-handler below */
120 }
121 } else {
Brian Carlstrom8f192cc2010-10-18 15:37:24 -0700122 outFd = open(tailCtx->fileName, O_WRONLY|O_CREAT|O_TRUNC, 0644);
Andy McFadden4b851a72010-07-09 16:50:05 -0700123 if (outFd < 0) {
Dan Bornstein60fc8062011-05-26 10:11:58 -0700124 LOGE("can't open %s: %s", headCtx->fileName, strerror(errno));
Andy McFadden4b851a72010-07-09 16:50:05 -0700125 /* continue to fail-handler below */
126 }
127 }
128 if (outFd < 0) {
129 hprofFreeContext(headCtx);
130 hprofFreeContext(tailCtx);
131 return false;
132 }
133
134 int result;
135 result = sysWriteFully(outFd, headCtx->fileDataPtr,
136 headCtx->fileDataSize, "hprof-head");
137 result |= sysWriteFully(outFd, tailCtx->fileDataPtr,
138 tailCtx->fileDataSize, "hprof-tail");
139 close(outFd);
140 if (result != 0) {
141 hprofFreeContext(headCtx);
142 hprofFreeContext(tailCtx);
143 return false;
Andy McFadden6bf992c2010-01-28 17:01:39 -0800144 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800145 }
146
Andy McFadden4b851a72010-07-09 16:50:05 -0700147 /* throw out a log message for the benefit of "runhat" */
Dan Bornstein60fc8062011-05-26 10:11:58 -0700148 LOGI("hprof: heap dump completed (%dKB)",
Andy McFadden4b851a72010-07-09 16:50:05 -0700149 (headCtx->fileDataSize + tailCtx->fileDataSize + 1023) / 1024);
150
Andy McFadden6bf992c2010-01-28 17:01:39 -0800151 hprofFreeContext(headCtx);
152 hprofFreeContext(tailCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800153
The Android Open Source Project99409882009-03-18 22:20:24 -0700154 return true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800155}
Andy McFadden6bf992c2010-01-28 17:01:39 -0800156
157/*
158 * Free any heap-allocated items in "ctx", and then free "ctx" itself.
159 */
Carl Shapiro06f120f2011-05-11 13:15:32 -0700160void hprofFreeContext(hprof_context_t *ctx)
Andy McFadden6bf992c2010-01-28 17:01:39 -0800161{
162 assert(ctx != NULL);
163
Andy McFadden4b851a72010-07-09 16:50:05 -0700164 /* we don't own ctx->fd, do not close */
165
166 if (ctx->memFp != NULL)
167 fclose(ctx->memFp);
Andy McFadden6bf992c2010-01-28 17:01:39 -0800168 free(ctx->curRec.body);
169 free(ctx->fileName);
170 free(ctx->fileDataPtr);
171 free(ctx);
172}
Carl Shapiro07018e22010-10-26 21:07:41 -0700173
174/*
175 * Visitor invoked on every root reference.
176 */
177static void hprofRootVisitor(void *addr, u4 threadId, RootType type, void *arg)
178{
179 static const hprof_heap_tag_t xlate[] = {
180 HPROF_ROOT_UNKNOWN,
181 HPROF_ROOT_JNI_GLOBAL,
182 HPROF_ROOT_JNI_LOCAL,
183 HPROF_ROOT_JAVA_FRAME,
184 HPROF_ROOT_NATIVE_STACK,
185 HPROF_ROOT_STICKY_CLASS,
186 HPROF_ROOT_THREAD_BLOCK,
187 HPROF_ROOT_MONITOR_USED,
188 HPROF_ROOT_THREAD_OBJECT,
189 HPROF_ROOT_INTERNED_STRING,
190 HPROF_ROOT_FINALIZING,
191 HPROF_ROOT_DEBUGGER,
192 HPROF_ROOT_REFERENCE_CLEANUP,
193 HPROF_ROOT_VM_INTERNAL,
194 HPROF_ROOT_JNI_MONITOR,
195 };
196 hprof_context_t *ctx;
Carl Shapiro8a691682010-12-13 12:13:47 -0800197 Object *obj;
Carl Shapiro07018e22010-10-26 21:07:41 -0700198
Carl Shapiro8a691682010-12-13 12:13:47 -0800199 assert(addr != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700200 assert(arg != NULL);
201 assert(type < NELEM(xlate));
Carl Shapiro8a691682010-12-13 12:13:47 -0800202 obj = *(Object **)addr;
Carl Shapiroc95738a2010-12-15 13:36:59 -0800203 if (obj == NULL) {
204 return;
205 }
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800206 ctx = (hprof_context_t *)arg;
Carl Shapiro07018e22010-10-26 21:07:41 -0700207 ctx->gcScanState = xlate[type];
208 ctx->gcThreadSerialNumber = threadId;
Carl Shapiro8a691682010-12-13 12:13:47 -0800209 hprofMarkRootObject(ctx, obj, 0);
Carl Shapiro07018e22010-10-26 21:07:41 -0700210 ctx->gcScanState = 0;
211 ctx->gcThreadSerialNumber = 0;
212}
213
214/*
215 * Visitor invoked on every heap object.
216 */
217static void hprofBitmapCallback(void *ptr, void *arg)
218{
219 Object *obj;
220 hprof_context_t *ctx;
221
222 assert(ptr != NULL);
223 assert(arg != NULL);
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800224 obj = (Object *)ptr;
225 ctx = (hprof_context_t *)arg;
Carl Shapiro07018e22010-10-26 21:07:41 -0700226 hprofDumpHeapObject(ctx, obj);
227}
228
229/*
230 * Walk the roots and heap writing heap information to the specified
231 * file.
232 *
233 * If "fd" is >= 0, the output will be written to that file descriptor.
234 * Otherwise, "fileName" is used to create an output file.
235 *
236 * If "directToDdms" is set, the other arguments are ignored, and data is
237 * sent directly to DDMS.
238 *
239 * Returns 0 on success, or an error code on failure.
240 */
241int hprofDumpHeap(const char* fileName, int fd, bool directToDdms)
242{
243 hprof_context_t *ctx;
244 int success;
245
246 assert(fileName != NULL);
247 dvmLockHeap();
248 dvmSuspendAllThreads(SUSPEND_FOR_HPROF);
249 ctx = hprofStartup(fileName, fd, directToDdms);
250 if (ctx == NULL) {
251 return -1;
252 }
253 dvmVisitRoots(hprofRootVisitor, ctx);
254 dvmHeapBitmapWalk(dvmHeapSourceGetLiveBits(), hprofBitmapCallback, ctx);
255 hprofFinishHeapDump(ctx);
256//TODO: write a HEAP_SUMMARY record
257 success = hprofShutdown(ctx) ? 0 : -1;
Carl Shapiroc921c822010-10-29 19:08:57 -0700258 dvmResumeAllThreads(SUSPEND_FOR_HPROF);
Carl Shapiro07018e22010-10-26 21:07:41 -0700259 dvmUnlockHeap();
260 return success;
261}