blob: e42bd10361e61eb7d599569935b7b086b109daa4 [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#if WITH_HPROF_STACK
Andy McFadden6bf992c2010-01-28 17:01:39 -080044 hprofStartup_StackFrame();
45 hprofStartup_Stack();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080046#endif
47
Andy McFadden6bf992c2010-01-28 17:01:39 -080048 hprof_context_t *ctx = malloc(sizeof(*ctx));
49 if (ctx == NULL) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080050 LOGE("hprof: can't allocate context.\n");
Andy McFadden6bf992c2010-01-28 17:01:39 -080051 return NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080052 }
53
Andy McFadden4b851a72010-07-09 16:50:05 -070054 /* pass in name or descriptor of the output file */
55 hprofContextInit(ctx, strdup(outputFileName), fd, false, directToDdms);
Andy McFadden6bf992c2010-01-28 17:01:39 -080056
Andy McFadden4b851a72010-07-09 16:50:05 -070057 assert(ctx->memFp != NULL);
Andy McFadden6bf992c2010-01-28 17:01:39 -080058
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080059 return ctx;
60}
61
62/*
The Android Open Source Project99409882009-03-18 22:20:24 -070063 * Finish up the hprof dump. Returns true on success.
64 */
65bool
Andy McFadden6bf992c2010-01-28 17:01:39 -080066hprofShutdown(hprof_context_t *tailCtx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080067{
Andy McFadden4b851a72010-07-09 16:50:05 -070068 /* flush the "tail" portion of the output */
Andy McFadden6bf992c2010-01-28 17:01:39 -080069 hprofFlushCurrentRecord(tailCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080070
Andy McFadden6bf992c2010-01-28 17:01:39 -080071 /*
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 McFadden6bf992c2010-01-28 17:01:39 -080078 hprofFreeContext(tailCtx);
Andy McFadden4b851a72010-07-09 16:50:05 -070079 return false;
Andy McFadden6bf992c2010-01-28 17:01:39 -080080 }
Andy McFadden4b851a72010-07-09 16:50:05 -070081 hprofContextInit(headCtx, strdup(tailCtx->fileName), tailCtx->fd, true,
Andy McFadden6bf992c2010-01-28 17:01:39 -080082 tailCtx->directToDdms);
83
Andy McFadden4b851a72010-07-09 16:50:05 -070084 LOGI("hprof: dumping heap strings to \"%s\".\n", tailCtx->fileName);
Andy McFadden6bf992c2010-01-28 17:01:39 -080085 hprofDumpStrings(headCtx);
86 hprofDumpClasses(headCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080087
88 /* Write a dummy stack trace record so the analysis
89 * tools don't freak out.
90 */
Andy McFadden6bf992c2010-01-28 17:01:39 -080091 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 Projectf6c38712009-03-03 19:28:47 -080095
96#if WITH_HPROF_STACK
Andy McFadden6bf992c2010-01-28 17:01:39 -080097 hprofDumpStackFrames(headCtx);
98 hprofDumpStacks(headCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080099#endif
100
Andy McFadden6bf992c2010-01-28 17:01:39 -0800101 hprofFlushCurrentRecord(headCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800102
103 hprofShutdown_Class();
104 hprofShutdown_String();
105#if WITH_HPROF_STACK
106 hprofShutdown_Stack();
107 hprofShutdown_StackFrame();
108#endif
109
Andy McFadden4b851a72010-07-09 16:50:05 -0700110 /* flush to ensure memstream pointer and size are updated */
111 fflush(headCtx->memFp);
112 fflush(tailCtx->memFp);
Andy McFadden6bf992c2010-01-28 17:01:39 -0800113
Andy McFadden4b851a72010-07-09 16:50:05 -0700114 if (tailCtx->directToDdms) {
Andy McFadden6bf992c2010-01-28 17:01:39 -0800115 /* 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 McFadden4b851a72010-07-09 16:50:05 -0700124 * Open the output file, and copy the head and tail to it.
Andy McFadden6bf992c2010-01-28 17:01:39 -0800125 */
Andy McFadden4b851a72010-07-09 16:50:05 -0700126 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 Carlstrom8f192cc2010-10-18 15:37:24 -0700136 outFd = open(tailCtx->fileName, O_WRONLY|O_CREAT|O_TRUNC, 0644);
Andy McFadden4b851a72010-07-09 16:50:05 -0700137 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 McFadden6bf992c2010-01-28 17:01:39 -0800158 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800159 }
160
Andy McFadden4b851a72010-07-09 16:50:05 -0700161 /* 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 McFadden6bf992c2010-01-28 17:01:39 -0800165 hprofFreeContext(headCtx);
166 hprofFreeContext(tailCtx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800167
The Android Open Source Project99409882009-03-18 22:20:24 -0700168 return true;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800169}
Andy McFadden6bf992c2010-01-28 17:01:39 -0800170
171/*
172 * Free any heap-allocated items in "ctx", and then free "ctx" itself.
173 */
174void
175hprofFreeContext(hprof_context_t *ctx)
176{
177 assert(ctx != NULL);
178
Andy McFadden4b851a72010-07-09 16:50:05 -0700179 /* we don't own ctx->fd, do not close */
180
181 if (ctx->memFp != NULL)
182 fclose(ctx->memFp);
Andy McFadden6bf992c2010-01-28 17:01:39 -0800183 free(ctx->curRec.body);
184 free(ctx->fileName);
185 free(ctx->fileDataPtr);
186 free(ctx);
187}
Carl Shapiro07018e22010-10-26 21:07:41 -0700188
189/*
190 * Visitor invoked on every root reference.
191 */
192static 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 */
226static 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 */
250int 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 Shapiroc921c822010-10-29 19:08:57 -0700267 dvmResumeAllThreads(SUSPEND_FOR_HPROF);
Carl Shapiro07018e22010-10-26 21:07:41 -0700268 dvmUnlockHeap();
269 return success;
270}