blob: 3f5e3162f2312c6b8e1587a632e3afe633d01428 [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
38hprof_context_t *
Andy McFadden4b851a72010-07-09 16:50:05 -070039hprofStartup(const char *outputFileName, int fd, 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) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080046 LOGE("hprof: can't allocate context.\n");
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 */
61bool
Andy McFadden6bf992c2010-01-28 17:01:39 -080062hprofShutdown(hprof_context_t *tailCtx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080063{
Andy McFadden4b851a72010-07-09 16:50:05 -070064 /* flush the "tail" portion of the output */
Andy McFadden6bf992c2010-01-28 17:01:39 -080065 hprofFlushCurrentRecord(tailCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080066
Andy McFadden6bf992c2010-01-28 17:01:39 -080067 /*
68 * Create a new context struct for the start of the file. We
69 * heap-allocate it so we can share the "free" function.
70 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -080071 hprof_context_t *headCtx = (hprof_context_t *)malloc(sizeof(*headCtx));
Andy McFadden6bf992c2010-01-28 17:01:39 -080072 if (headCtx == NULL) {
73 LOGE("hprof: can't allocate context.\n");
Andy McFadden6bf992c2010-01-28 17:01:39 -080074 hprofFreeContext(tailCtx);
Andy McFadden4b851a72010-07-09 16:50:05 -070075 return false;
Andy McFadden6bf992c2010-01-28 17:01:39 -080076 }
Andy McFadden4b851a72010-07-09 16:50:05 -070077 hprofContextInit(headCtx, strdup(tailCtx->fileName), tailCtx->fd, true,
Andy McFadden6bf992c2010-01-28 17:01:39 -080078 tailCtx->directToDdms);
79
Andy McFadden4b851a72010-07-09 16:50:05 -070080 LOGI("hprof: dumping heap strings to \"%s\".\n", tailCtx->fileName);
Andy McFadden6bf992c2010-01-28 17:01:39 -080081 hprofDumpStrings(headCtx);
82 hprofDumpClasses(headCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080083
84 /* Write a dummy stack trace record so the analysis
85 * tools don't freak out.
86 */
Andy McFadden6bf992c2010-01-28 17:01:39 -080087 hprofStartNewRecord(headCtx, HPROF_TAG_STACK_TRACE, HPROF_TIME);
88 hprofAddU4ToRecord(&headCtx->curRec, HPROF_NULL_STACK_TRACE);
89 hprofAddU4ToRecord(&headCtx->curRec, HPROF_NULL_THREAD);
90 hprofAddU4ToRecord(&headCtx->curRec, 0); // no frames
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080091
Andy McFadden6bf992c2010-01-28 17:01:39 -080092 hprofFlushCurrentRecord(headCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080093
94 hprofShutdown_Class();
95 hprofShutdown_String();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080096
Andy McFadden4b851a72010-07-09 16:50:05 -070097 /* flush to ensure memstream pointer and size are updated */
98 fflush(headCtx->memFp);
99 fflush(tailCtx->memFp);
Andy McFadden6bf992c2010-01-28 17:01:39 -0800100
Andy McFadden4b851a72010-07-09 16:50:05 -0700101 if (tailCtx->directToDdms) {
Andy McFadden6bf992c2010-01-28 17:01:39 -0800102 /* send the data off to DDMS */
103 struct iovec iov[2];
104 iov[0].iov_base = headCtx->fileDataPtr;
105 iov[0].iov_len = headCtx->fileDataSize;
106 iov[1].iov_base = tailCtx->fileDataPtr;
107 iov[1].iov_len = tailCtx->fileDataSize;
108 dvmDbgDdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
109 } else {
110 /*
Andy McFadden4b851a72010-07-09 16:50:05 -0700111 * Open the output file, and copy the head and tail to it.
Andy McFadden6bf992c2010-01-28 17:01:39 -0800112 */
Andy McFadden4b851a72010-07-09 16:50:05 -0700113 assert(headCtx->fd == tailCtx->fd);
114
115 int outFd;
116 if (headCtx->fd >= 0) {
117 outFd = dup(headCtx->fd);
118 if (outFd < 0) {
119 LOGE("dup(%d) failed: %s\n", headCtx->fd, strerror(errno));
120 /* continue to fail-handler below */
121 }
122 } else {
Brian Carlstrom8f192cc2010-10-18 15:37:24 -0700123 outFd = open(tailCtx->fileName, O_WRONLY|O_CREAT|O_TRUNC, 0644);
Andy McFadden4b851a72010-07-09 16:50:05 -0700124 if (outFd < 0) {
125 LOGE("can't open %s: %s\n", headCtx->fileName, strerror(errno));
126 /* continue to fail-handler below */
127 }
128 }
129 if (outFd < 0) {
130 hprofFreeContext(headCtx);
131 hprofFreeContext(tailCtx);
132 return false;
133 }
134
135 int result;
136 result = sysWriteFully(outFd, headCtx->fileDataPtr,
137 headCtx->fileDataSize, "hprof-head");
138 result |= sysWriteFully(outFd, tailCtx->fileDataPtr,
139 tailCtx->fileDataSize, "hprof-tail");
140 close(outFd);
141 if (result != 0) {
142 hprofFreeContext(headCtx);
143 hprofFreeContext(tailCtx);
144 return false;
Andy McFadden6bf992c2010-01-28 17:01:39 -0800145 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800146 }
147
Andy McFadden4b851a72010-07-09 16:50:05 -0700148 /* throw out a log message for the benefit of "runhat" */
149 LOGI("hprof: heap dump completed (%dKB)\n",
150 (headCtx->fileDataSize + tailCtx->fileDataSize + 1023) / 1024);
151
Andy McFadden6bf992c2010-01-28 17:01:39 -0800152 hprofFreeContext(headCtx);
153 hprofFreeContext(tailCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800154
The Android Open Source Project99409882009-03-18 22:20:24 -0700155 return true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800156}
Andy McFadden6bf992c2010-01-28 17:01:39 -0800157
158/*
159 * Free any heap-allocated items in "ctx", and then free "ctx" itself.
160 */
161void
162hprofFreeContext(hprof_context_t *ctx)
163{
164 assert(ctx != NULL);
165
Andy McFadden4b851a72010-07-09 16:50:05 -0700166 /* we don't own ctx->fd, do not close */
167
168 if (ctx->memFp != NULL)
169 fclose(ctx->memFp);
Andy McFadden6bf992c2010-01-28 17:01:39 -0800170 free(ctx->curRec.body);
171 free(ctx->fileName);
172 free(ctx->fileDataPtr);
173 free(ctx);
174}
Carl Shapiro07018e22010-10-26 21:07:41 -0700175
176/*
177 * Visitor invoked on every root reference.
178 */
179static void hprofRootVisitor(void *addr, u4 threadId, RootType type, void *arg)
180{
181 static const hprof_heap_tag_t xlate[] = {
182 HPROF_ROOT_UNKNOWN,
183 HPROF_ROOT_JNI_GLOBAL,
184 HPROF_ROOT_JNI_LOCAL,
185 HPROF_ROOT_JAVA_FRAME,
186 HPROF_ROOT_NATIVE_STACK,
187 HPROF_ROOT_STICKY_CLASS,
188 HPROF_ROOT_THREAD_BLOCK,
189 HPROF_ROOT_MONITOR_USED,
190 HPROF_ROOT_THREAD_OBJECT,
191 HPROF_ROOT_INTERNED_STRING,
192 HPROF_ROOT_FINALIZING,
193 HPROF_ROOT_DEBUGGER,
194 HPROF_ROOT_REFERENCE_CLEANUP,
195 HPROF_ROOT_VM_INTERNAL,
196 HPROF_ROOT_JNI_MONITOR,
197 };
198 hprof_context_t *ctx;
Carl Shapiro8a691682010-12-13 12:13:47 -0800199 Object *obj;
Carl Shapiro07018e22010-10-26 21:07:41 -0700200
Carl Shapiro8a691682010-12-13 12:13:47 -0800201 assert(addr != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700202 assert(arg != NULL);
203 assert(type < NELEM(xlate));
Carl Shapiro8a691682010-12-13 12:13:47 -0800204 obj = *(Object **)addr;
Carl Shapiroc95738a2010-12-15 13:36:59 -0800205 if (obj == NULL) {
206 return;
207 }
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800208 ctx = (hprof_context_t *)arg;
Carl Shapiro07018e22010-10-26 21:07:41 -0700209 ctx->gcScanState = xlate[type];
210 ctx->gcThreadSerialNumber = threadId;
Carl Shapiro8a691682010-12-13 12:13:47 -0800211 hprofMarkRootObject(ctx, obj, 0);
Carl Shapiro07018e22010-10-26 21:07:41 -0700212 ctx->gcScanState = 0;
213 ctx->gcThreadSerialNumber = 0;
214}
215
216/*
217 * Visitor invoked on every heap object.
218 */
219static void hprofBitmapCallback(void *ptr, void *arg)
220{
221 Object *obj;
222 hprof_context_t *ctx;
223
224 assert(ptr != NULL);
225 assert(arg != NULL);
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800226 obj = (Object *)ptr;
227 ctx = (hprof_context_t *)arg;
Carl Shapiro07018e22010-10-26 21:07:41 -0700228 hprofDumpHeapObject(ctx, obj);
229}
230
231/*
232 * Walk the roots and heap writing heap information to the specified
233 * file.
234 *
235 * If "fd" is >= 0, the output will be written to that file descriptor.
236 * Otherwise, "fileName" is used to create an output file.
237 *
238 * If "directToDdms" is set, the other arguments are ignored, and data is
239 * sent directly to DDMS.
240 *
241 * Returns 0 on success, or an error code on failure.
242 */
243int hprofDumpHeap(const char* fileName, int fd, bool directToDdms)
244{
245 hprof_context_t *ctx;
246 int success;
247
248 assert(fileName != NULL);
249 dvmLockHeap();
250 dvmSuspendAllThreads(SUSPEND_FOR_HPROF);
251 ctx = hprofStartup(fileName, fd, directToDdms);
252 if (ctx == NULL) {
253 return -1;
254 }
255 dvmVisitRoots(hprofRootVisitor, ctx);
256 dvmHeapBitmapWalk(dvmHeapSourceGetLiveBits(), hprofBitmapCallback, ctx);
257 hprofFinishHeapDump(ctx);
258//TODO: write a HEAP_SUMMARY record
259 success = hprofShutdown(ctx) ? 0 : -1;
Carl Shapiroc921c822010-10-29 19:08:57 -0700260 dvmResumeAllThreads(SUSPEND_FOR_HPROF);
Carl Shapiro07018e22010-10-26 21:07:41 -0700261 dvmUnlockHeap();
262 return success;
263}