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 * DDM-related heap functions
18 */
19 #include <sys/time.h>
20 #include <time.h>
21
22 #include "Dalvik.h"
23 #include "alloc/Heap.h"
24 #include "alloc/HeapInternal.h"
25 #include "alloc/DdmHeap.h"
26 #include "alloc/HeapSource.h"
27
28 #define DEFAULT_HEAP_ID 1
29
30 enum HpifWhen {
31 HPIF_WHEN_NEVER = 0,
32 HPIF_WHEN_NOW = 1,
33 HPIF_WHEN_NEXT_GC = 2,
34 HPIF_WHEN_EVERY_GC = 3
35 };
36
37 /*
38 * Chunk HPIF (client --> server)
39 *
40 * Heap Info. General information about the heap,
41 * suitable for a summary display.
42 *
43 * [u4]: number of heaps
44 *
45 * For each heap:
46 * [u4]: heap ID
47 * [u8]: timestamp in ms since Unix epoch
48 * [u1]: capture reason (same as 'when' value from server)
49 * [u4]: max heap size in bytes (-Xmx)
50 * [u4]: current heap size in bytes
51 * [u4]: current number of bytes allocated
52 * [u4]: current number of objects allocated
53 */
54 #define HPIF_SIZE(numHeaps) \
55 (sizeof(u4) + (numHeaps) * (5 * sizeof(u4) + sizeof(u1) + sizeof(u8)))
56 void
dvmDdmSendHeapInfo(int reason,bool shouldLock)57 dvmDdmSendHeapInfo(int reason, bool shouldLock)
58 {
59 struct timeval now;
60 u8 nowMs;
61 u1 *buf, *b;
62
63 buf = (u1 *)malloc(HPIF_SIZE(1));
64 if (buf == NULL) {
65 return;
66 }
67 b = buf;
68
69 /* If there's a one-shot 'when', reset it.
70 */
71 if (reason == gDvm.gcHeap->ddmHpifWhen) {
72 if (shouldLock && ! dvmLockHeap()) {
73 LOGW("%s(): can't lock heap to clear when\n", __func__);
74 goto skip_when;
75 }
76 if (reason == gDvm.gcHeap->ddmHpifWhen) {
77 if (gDvm.gcHeap->ddmHpifWhen == HPIF_WHEN_NEXT_GC) {
78 gDvm.gcHeap->ddmHpifWhen = HPIF_WHEN_NEVER;
79 }
80 }
81 if (shouldLock) {
82 dvmUnlockHeap();
83 }
84 }
85 skip_when:
86
87 /* The current time, in milliseconds since 0:00 GMT, 1/1/70.
88 */
89 if (gettimeofday(&now, NULL) < 0) {
90 nowMs = 0;
91 } else {
92 nowMs = (u8)now.tv_sec * 1000 + now.tv_usec / 1000;
93 }
94
95 /* number of heaps */
96 set4BE(b, 1); b += 4;
97
98 /* For each heap (of which there is one) */
99 {
100 /* heap ID */
101 set4BE(b, DEFAULT_HEAP_ID); b += 4;
102
103 /* timestamp */
104 set8BE(b, nowMs); b += 8;
105
106 /* 'when' value */
107 *b++ = (u1)reason;
108
109 /* max allowed heap size in bytes */
110 set4BE(b, gDvm.heapSizeMax); b += 4;
111
112 /* current heap size in bytes */
113 set4BE(b, dvmHeapSourceGetValue(HS_FOOTPRINT, NULL, 0)); b += 4;
114
115 /* number of bytes allocated */
116 set4BE(b, dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0)); b += 4;
117
118 /* number of objects allocated */
119 set4BE(b, dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0)); b += 4;
120 }
121 assert((intptr_t)b == (intptr_t)buf + (intptr_t)HPIF_SIZE(1));
122
123 dvmDbgDdmSendChunk(CHUNK_TYPE("HPIF"), b - buf, buf);
124 }
125
126 bool
dvmDdmHandleHpifChunk(int when)127 dvmDdmHandleHpifChunk(int when)
128 {
129 switch (when) {
130 case HPIF_WHEN_NOW:
131 dvmDdmSendHeapInfo(when, true);
132 break;
133 case HPIF_WHEN_NEVER:
134 case HPIF_WHEN_NEXT_GC:
135 case HPIF_WHEN_EVERY_GC:
136 if (dvmLockHeap()) {
137 gDvm.gcHeap->ddmHpifWhen = when;
138 dvmUnlockHeap();
139 } else {
140 LOGI("%s(): can't lock heap to set when\n", __func__);
141 return false;
142 }
143 break;
144 default:
145 LOGI("%s(): bad when value 0x%08x\n", __func__, when);
146 return false;
147 }
148
149 return true;
150 }
151
152 enum HpsgSolidity {
153 SOLIDITY_FREE = 0,
154 SOLIDITY_HARD = 1,
155 SOLIDITY_SOFT = 2,
156 SOLIDITY_WEAK = 3,
157 SOLIDITY_PHANTOM = 4,
158 SOLIDITY_FINALIZABLE = 5,
159 SOLIDITY_SWEEP = 6,
160 };
161
162 enum HpsgKind {
163 KIND_OBJECT = 0,
164 KIND_CLASS_OBJECT = 1,
165 KIND_ARRAY_1 = 2,
166 KIND_ARRAY_2 = 3,
167 KIND_ARRAY_4 = 4,
168 KIND_ARRAY_8 = 5,
169 KIND_UNKNOWN = 6,
170 KIND_NATIVE = 7,
171 };
172
173 #define HPSG_PARTIAL (1<<7)
174 #define HPSG_STATE(solidity, kind) \
175 ((u1)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
176
177 typedef struct HeapChunkContext {
178 u1 *buf;
179 u1 *p;
180 u1 *pieceLenField;
181 size_t bufLen;
182 size_t totalAllocationUnits;
183 int type;
184 bool merge;
185 bool needHeader;
186 } HeapChunkContext;
187
188 #define ALLOCATION_UNIT_SIZE 8
189
190 static void
flush_hpsg_chunk(HeapChunkContext * ctx)191 flush_hpsg_chunk(HeapChunkContext *ctx)
192 {
193 /* Patch the "length of piece" field.
194 */
195 assert(ctx->buf <= ctx->pieceLenField &&
196 ctx->pieceLenField <= ctx->p);
197 set4BE(ctx->pieceLenField, ctx->totalAllocationUnits);
198
199 /* Send the chunk.
200 */
201 dvmDbgDdmSendChunk(ctx->type, ctx->p - ctx->buf, ctx->buf);
202
203 /* Reset the context.
204 */
205 ctx->p = ctx->buf;
206 ctx->totalAllocationUnits = 0;
207 ctx->needHeader = true;
208 ctx->pieceLenField = NULL;
209 }
210
211 static void
heap_chunk_callback(const void * chunkptr,size_t chunklen,const void * userptr,size_t userlen,void * arg)212 heap_chunk_callback(const void *chunkptr, size_t chunklen,
213 const void *userptr, size_t userlen, void *arg)
214 {
215 HeapChunkContext *ctx = (HeapChunkContext *)arg;
216 u1 state;
217
218 UNUSED_PARAMETER(userlen);
219
220 assert((chunklen & (ALLOCATION_UNIT_SIZE-1)) == 0);
221
222 /* Make sure there's enough room left in the buffer.
223 * We need to use two bytes for every fractional 256
224 * allocation units used by the chunk.
225 */
226 {
227 size_t bytesLeft = ctx->bufLen - (size_t)(ctx->p - ctx->buf);
228 if (bytesLeft < (((chunklen/ALLOCATION_UNIT_SIZE + 255) / 256) * 2)) {
229 flush_hpsg_chunk(ctx);
230 }
231 }
232
233 //TODO: notice when there's a gap and start a new heap, or at least a new range.
234 if (ctx->needHeader) {
235 /*
236 * Start a new HPSx chunk.
237 */
238
239 /* [u4]: heap ID */
240 set4BE(ctx->p, DEFAULT_HEAP_ID); ctx->p += 4;
241
242 /* [u1]: size of allocation unit, in bytes */
243 *ctx->p++ = 8;
244
245 /* [u4]: virtual address of segment start */
246 set4BE(ctx->p, (uintptr_t)chunkptr); ctx->p += 4;
247
248 /* [u4]: offset of this piece (relative to the virtual address) */
249 set4BE(ctx->p, 0); ctx->p += 4;
250
251 /* [u4]: length of piece, in allocation units
252 * We won't know this until we're done, so save the offset
253 * and stuff in a dummy value.
254 */
255 ctx->pieceLenField = ctx->p;
256 set4BE(ctx->p, 0x55555555); ctx->p += 4;
257
258 ctx->needHeader = false;
259 }
260
261 /* Determine the type of this chunk.
262 */
263 if (userptr == NULL) {
264 /* It's a free chunk.
265 */
266 state = HPSG_STATE(SOLIDITY_FREE, 0);
267 } else {
268 const DvmHeapChunk *hc = (const DvmHeapChunk *)userptr;
269 const Object *obj = chunk2ptr(hc);
270 /* If we're looking at the native heap, we'll just return
271 * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
272 */
273 bool native = ctx->type == CHUNK_TYPE("NHSG");
274
275 /* It's an allocated chunk. Figure out what it is.
276 */
277 //TODO: if ctx.merge, see if this chunk is different from the last chunk.
278 // If it's the same, we should combine them.
279 if (!native && dvmIsValidObject(obj)) {
280 ClassObject *clazz = obj->clazz;
281 if (clazz == NULL) {
282 /* The object was probably just created
283 * but hasn't been initialized yet.
284 */
285 state = HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
286 } else if (clazz == gDvm.unlinkedJavaLangClass ||
287 clazz == gDvm.classJavaLangClass)
288 {
289 state = HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
290 } else if (IS_CLASS_FLAG_SET(clazz, CLASS_ISARRAY)) {
291 if (IS_CLASS_FLAG_SET(clazz, CLASS_ISOBJECTARRAY)) {
292 state = HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
293 } else {
294 switch (clazz->elementClass->primitiveType) {
295 case PRIM_BOOLEAN:
296 case PRIM_BYTE:
297 state = HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
298 break;
299 case PRIM_CHAR:
300 case PRIM_SHORT:
301 state = HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
302 break;
303 case PRIM_INT:
304 case PRIM_FLOAT:
305 state = HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
306 break;
307 case PRIM_DOUBLE:
308 case PRIM_LONG:
309 state = HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
310 break;
311 default:
312 assert(!"Unknown GC heap object type");
313 state = HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
314 break;
315 }
316 }
317 } else {
318 state = HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
319 }
320 } else {
321 obj = NULL; // it's not actually an object
322 state = HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
323 }
324 }
325
326 /* Write out the chunk description.
327 */
328 chunklen /= ALLOCATION_UNIT_SIZE; // convert to allocation units
329 ctx->totalAllocationUnits += chunklen;
330 while (chunklen > 256) {
331 *ctx->p++ = state | HPSG_PARTIAL;
332 *ctx->p++ = 255; // length - 1
333 chunklen -= 256;
334 }
335 *ctx->p++ = state;
336 *ctx->p++ = chunklen - 1;
337 }
338
339 enum HpsgWhen {
340 HPSG_WHEN_NEVER = 0,
341 HPSG_WHEN_EVERY_GC = 1,
342 };
343 enum HpsgWhat {
344 HPSG_WHAT_MERGED_OBJECTS = 0,
345 HPSG_WHAT_DISTINCT_OBJECTS = 1,
346 };
347
348 #define HPSx_CHUNK_SIZE (4096 - 16)
349
350 void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*),void*);
351
352 static void
walkHeap(bool merge,bool native)353 walkHeap(bool merge, bool native)
354 {
355 HeapChunkContext ctx;
356
357 memset(&ctx, 0, sizeof(ctx));
358 ctx.bufLen = HPSx_CHUNK_SIZE;
359 ctx.buf = (u1 *)malloc(ctx.bufLen);
360 if (ctx.buf == NULL) {
361 return;
362 }
363
364 ctx.merge = merge;
365 if (native) {
366 ctx.type = CHUNK_TYPE("NHSG");
367 } else {
368 if (ctx.merge) {
369 ctx.type = CHUNK_TYPE("HPSG");
370 } else {
371 ctx.type = CHUNK_TYPE("HPSO");
372 }
373 }
374
375 ctx.p = ctx.buf;
376 ctx.needHeader = true;
377 if (native) {
378 dlmalloc_walk_heap(heap_chunk_callback, (void *)&ctx);
379 } else {
380 dvmHeapSourceWalk(heap_chunk_callback, (void *)&ctx);
381 }
382 if (ctx.p > ctx.buf) {
383 flush_hpsg_chunk(&ctx);
384 }
385
386 free(ctx.buf);
387 }
388
389 void
dvmDdmSendHeapSegments(bool shouldLock,bool native)390 dvmDdmSendHeapSegments(bool shouldLock, bool native)
391 {
392 u1 heapId[sizeof(u4)];
393 GcHeap *gcHeap = gDvm.gcHeap;
394 int when, what;
395 bool merge;
396
397 /* Don't even grab the lock if there's nothing to do when we're called.
398 */
399 if (!native) {
400 when = gcHeap->ddmHpsgWhen;
401 what = gcHeap->ddmHpsgWhat;
402 if (when == HPSG_WHEN_NEVER) {
403 return;
404 }
405 } else {
406 when = gcHeap->ddmNhsgWhen;
407 what = gcHeap->ddmNhsgWhat;
408 if (when == HPSG_WHEN_NEVER) {
409 return;
410 }
411 }
412 if (shouldLock && !dvmLockHeap()) {
413 LOGW("Can't lock heap for DDM HPSx dump\n");
414 return;
415 }
416
417 /* Figure out what kind of chunks we'll be sending.
418 */
419 if (what == HPSG_WHAT_MERGED_OBJECTS) {
420 merge = true;
421 } else if (what == HPSG_WHAT_DISTINCT_OBJECTS) {
422 merge = false;
423 } else {
424 assert(!"bad HPSG.what value");
425 return;
426 }
427
428 /* First, send a heap start chunk.
429 */
430 set4BE(heapId, DEFAULT_HEAP_ID);
431 dvmDbgDdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"),
432 sizeof(u4), heapId);
433
434 /* Send a series of heap segment chunks.
435 */
436 walkHeap(merge, native);
437
438 /* Finally, send a heap end chunk.
439 */
440 dvmDbgDdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"),
441 sizeof(u4), heapId);
442
443 if (shouldLock) {
444 dvmUnlockHeap();
445 }
446 }
447
448 bool
dvmDdmHandleHpsgNhsgChunk(int when,int what,bool native)449 dvmDdmHandleHpsgNhsgChunk(int when, int what, bool native)
450 {
451 LOGI("dvmDdmHandleHpsgChunk(when %d, what %d, heap %d)\n", when, what,
452 native);
453 switch (when) {
454 case HPSG_WHEN_NEVER:
455 case HPSG_WHEN_EVERY_GC:
456 break;
457 default:
458 LOGI("%s(): bad when value 0x%08x\n", __func__, when);
459 return false;
460 }
461
462 switch (what) {
463 case HPSG_WHAT_MERGED_OBJECTS:
464 case HPSG_WHAT_DISTINCT_OBJECTS:
465 break;
466 default:
467 LOGI("%s(): bad what value 0x%08x\n", __func__, what);
468 return false;
469 }
470
471 if (dvmLockHeap()) {
472 if (!native) {
473 gDvm.gcHeap->ddmHpsgWhen = when;
474 gDvm.gcHeap->ddmHpsgWhat = what;
475 } else {
476 gDvm.gcHeap->ddmNhsgWhen = when;
477 gDvm.gcHeap->ddmNhsgWhat = what;
478 }
479 //TODO: if what says we should dump immediately, signal (or do) it from here
480 dvmUnlockHeap();
481 } else {
482 LOGI("%s(): can't lock heap to set when/what\n", __func__);
483 return false;
484 }
485
486 return true;
487 }
488