comparison src/share/vm/services/heapDumper.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children d5fc211aea19
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 # include "incls/_precompiled.incl"
26 # include "incls/_heapDumper.cpp.incl"
27
28 /*
29 * HPROF binary format - description copied from:
30 * src/share/demo/jvmti/hprof/hprof_io.c
31 *
32 *
33 * header "JAVA PROFILE 1.0.1" or "JAVA PROFILE 1.0.2"
34 * (0-terminated)
35 *
36 * u4 size of identifiers. Identifiers are used to represent
37 * UTF8 strings, objects, stack traces, etc. They usually
38 * have the same size as host pointers. For example, on
39 * Solaris and Win32, the size is 4.
40 * u4 high word
41 * u4 low word number of milliseconds since 0:00 GMT, 1/1/70
42 * [record]* a sequence of records.
43 *
44 *
45 * Record format:
46 *
47 * u1 a TAG denoting the type of the record
48 * u4 number of *microseconds* since the time stamp in the
49 * header. (wraps around in a little more than an hour)
50 * u4 number of bytes *remaining* in the record. Note that
51 * this number excludes the tag and the length field itself.
52 * [u1]* BODY of the record (a sequence of bytes)
53 *
54 *
55 * The following TAGs are supported:
56 *
57 * TAG BODY notes
58 *----------------------------------------------------------
59 * HPROF_UTF8 a UTF8-encoded name
60 *
61 * id name ID
62 * [u1]* UTF8 characters (no trailing zero)
63 *
64 * HPROF_LOAD_CLASS a newly loaded class
65 *
66 * u4 class serial number (> 0)
67 * id class object ID
68 * u4 stack trace serial number
69 * id class name ID
70 *
71 * HPROF_UNLOAD_CLASS an unloading class
72 *
73 * u4 class serial_number
74 *
75 * HPROF_FRAME a Java stack frame
76 *
77 * id stack frame ID
78 * id method name ID
79 * id method signature ID
80 * id source file name ID
81 * u4 class serial number
82 * i4 line number. >0: normal
83 * -1: unknown
84 * -2: compiled method
85 * -3: native method
86 *
87 * HPROF_TRACE a Java stack trace
88 *
89 * u4 stack trace serial number
90 * u4 thread serial number
91 * u4 number of frames
92 * [id]* stack frame IDs
93 *
94 *
95 * HPROF_ALLOC_SITES a set of heap allocation sites, obtained after GC
96 *
97 * u2 flags 0x0001: incremental vs. complete
98 * 0x0002: sorted by allocation vs. live
99 * 0x0004: whether to force a GC
100 * u4 cutoff ratio
101 * u4 total live bytes
102 * u4 total live instances
103 * u8 total bytes allocated
104 * u8 total instances allocated
105 * u4 number of sites that follow
106 * [u1 is_array: 0: normal object
107 * 2: object array
108 * 4: boolean array
109 * 5: char array
110 * 6: float array
111 * 7: double array
112 * 8: byte array
113 * 9: short array
114 * 10: int array
115 * 11: long array
116 * u4 class serial number (may be zero during startup)
117 * u4 stack trace serial number
118 * u4 number of bytes alive
119 * u4 number of instances alive
120 * u4 number of bytes allocated
121 * u4]* number of instance allocated
122 *
123 * HPROF_START_THREAD a newly started thread.
124 *
125 * u4 thread serial number (> 0)
126 * id thread object ID
127 * u4 stack trace serial number
128 * id thread name ID
129 * id thread group name ID
130 * id thread group parent name ID
131 *
132 * HPROF_END_THREAD a terminating thread.
133 *
134 * u4 thread serial number
135 *
136 * HPROF_HEAP_SUMMARY heap summary
137 *
138 * u4 total live bytes
139 * u4 total live instances
140 * u8 total bytes allocated
141 * u8 total instances allocated
142 *
143 * HPROF_HEAP_DUMP denote a heap dump
144 *
145 * [heap dump sub-records]*
146 *
147 * There are four kinds of heap dump sub-records:
148 *
149 * u1 sub-record type
150 *
151 * HPROF_GC_ROOT_UNKNOWN unknown root
152 *
153 * id object ID
154 *
155 * HPROF_GC_ROOT_THREAD_OBJ thread object
156 *
157 * id thread object ID (may be 0 for a
158 * thread newly attached through JNI)
159 * u4 thread sequence number
160 * u4 stack trace sequence number
161 *
162 * HPROF_GC_ROOT_JNI_GLOBAL JNI global ref root
163 *
164 * id object ID
165 * id JNI global ref ID
166 *
167 * HPROF_GC_ROOT_JNI_LOCAL JNI local ref
168 *
169 * id object ID
170 * u4 thread serial number
171 * u4 frame # in stack trace (-1 for empty)
172 *
173 * HPROF_GC_ROOT_JAVA_FRAME Java stack frame
174 *
175 * id object ID
176 * u4 thread serial number
177 * u4 frame # in stack trace (-1 for empty)
178 *
179 * HPROF_GC_ROOT_NATIVE_STACK Native stack
180 *
181 * id object ID
182 * u4 thread serial number
183 *
184 * HPROF_GC_ROOT_STICKY_CLASS System class
185 *
186 * id object ID
187 *
188 * HPROF_GC_ROOT_THREAD_BLOCK Reference from thread block
189 *
190 * id object ID
191 * u4 thread serial number
192 *
193 * HPROF_GC_ROOT_MONITOR_USED Busy monitor
194 *
195 * id object ID
196 *
197 * HPROF_GC_CLASS_DUMP dump of a class object
198 *
199 * id class object ID
200 * u4 stack trace serial number
201 * id super class object ID
202 * id class loader object ID
203 * id signers object ID
204 * id protection domain object ID
205 * id reserved
206 * id reserved
207 *
208 * u4 instance size (in bytes)
209 *
210 * u2 size of constant pool
211 * [u2, constant pool index,
212 * ty, type
213 * 2: object
214 * 4: boolean
215 * 5: char
216 * 6: float
217 * 7: double
218 * 8: byte
219 * 9: short
220 * 10: int
221 * 11: long
222 * vl]* and value
223 *
224 * u2 number of static fields
225 * [id, static field name,
226 * ty, type,
227 * vl]* and value
228 *
229 * u2 number of inst. fields (not inc. super)
230 * [id, instance field name,
231 * ty]* type
232 *
233 * HPROF_GC_INSTANCE_DUMP dump of a normal object
234 *
235 * id object ID
236 * u4 stack trace serial number
237 * id class object ID
238 * u4 number of bytes that follow
239 * [vl]* instance field values (class, followed
240 * by super, super's super ...)
241 *
242 * HPROF_GC_OBJ_ARRAY_DUMP dump of an object array
243 *
244 * id array object ID
245 * u4 stack trace serial number
246 * u4 number of elements
247 * id array class ID
248 * [id]* elements
249 *
250 * HPROF_GC_PRIM_ARRAY_DUMP dump of a primitive array
251 *
252 * id array object ID
253 * u4 stack trace serial number
254 * u4 number of elements
255 * u1 element type
256 * 4: boolean array
257 * 5: char array
258 * 6: float array
259 * 7: double array
260 * 8: byte array
261 * 9: short array
262 * 10: int array
263 * 11: long array
264 * [u1]* elements
265 *
266 * HPROF_CPU_SAMPLES a set of sample traces of running threads
267 *
268 * u4 total number of samples
269 * u4 # of traces
270 * [u4 # of samples
271 * u4]* stack trace serial number
272 *
273 * HPROF_CONTROL_SETTINGS the settings of on/off switches
274 *
275 * u4 0x00000001: alloc traces on/off
276 * 0x00000002: cpu sampling on/off
277 * u2 stack trace depth
278 *
279 *
280 * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
281 * be generated as a sequence of heap dump segments. This sequence is
282 * terminated by an end record. The additional tags allowed by format
283 * "JAVA PROFILE 1.0.2" are:
284 *
285 * HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment
286 *
287 * [heap dump sub-records]*
288 * The same sub-record types allowed by HPROF_HEAP_DUMP
289 *
290 * HPROF_HEAP_DUMP_END denotes the end of a heap dump
291 *
292 */
293
294
295 // HPROF tags
296
297 typedef enum {
298 // top-level records
299 HPROF_UTF8 = 0x01,
300 HPROF_LOAD_CLASS = 0x02,
301 HPROF_UNLOAD_CLASS = 0x03,
302 HPROF_FRAME = 0x04,
303 HPROF_TRACE = 0x05,
304 HPROF_ALLOC_SITES = 0x06,
305 HPROF_HEAP_SUMMARY = 0x07,
306 HPROF_START_THREAD = 0x0A,
307 HPROF_END_THREAD = 0x0B,
308 HPROF_HEAP_DUMP = 0x0C,
309 HPROF_CPU_SAMPLES = 0x0D,
310 HPROF_CONTROL_SETTINGS = 0x0E,
311
312 // 1.0.2 record types
313 HPROF_HEAP_DUMP_SEGMENT = 0x1C,
314 HPROF_HEAP_DUMP_END = 0x2C,
315
316 // field types
317 HPROF_ARRAY_OBJECT = 0x01,
318 HPROF_NORMAL_OBJECT = 0x02,
319 HPROF_BOOLEAN = 0x04,
320 HPROF_CHAR = 0x05,
321 HPROF_FLOAT = 0x06,
322 HPROF_DOUBLE = 0x07,
323 HPROF_BYTE = 0x08,
324 HPROF_SHORT = 0x09,
325 HPROF_INT = 0x0A,
326 HPROF_LONG = 0x0B,
327
328 // data-dump sub-records
329 HPROF_GC_ROOT_UNKNOWN = 0xFF,
330 HPROF_GC_ROOT_JNI_GLOBAL = 0x01,
331 HPROF_GC_ROOT_JNI_LOCAL = 0x02,
332 HPROF_GC_ROOT_JAVA_FRAME = 0x03,
333 HPROF_GC_ROOT_NATIVE_STACK = 0x04,
334 HPROF_GC_ROOT_STICKY_CLASS = 0x05,
335 HPROF_GC_ROOT_THREAD_BLOCK = 0x06,
336 HPROF_GC_ROOT_MONITOR_USED = 0x07,
337 HPROF_GC_ROOT_THREAD_OBJ = 0x08,
338 HPROF_GC_CLASS_DUMP = 0x20,
339 HPROF_GC_INSTANCE_DUMP = 0x21,
340 HPROF_GC_OBJ_ARRAY_DUMP = 0x22,
341 HPROF_GC_PRIM_ARRAY_DUMP = 0x23
342 } hprofTag;
343
344 // Default stack trace ID (used for dummy HPROF_TRACE record)
345 enum {
346 STACK_TRACE_ID = 1
347 };
348
349
350 // Supports I/O operations on a dump file
351
352 class DumpWriter : public StackObj {
353 private:
354 enum {
355 io_buffer_size = 8*M
356 };
357
358 int _fd; // file descriptor (-1 if dump file not open)
359 jlong _bytes_written; // number of byte written to dump file
360
361 char* _buffer; // internal buffer
362 int _size;
363 int _pos;
364
365 char* _error; // error message when I/O fails
366
367 void set_file_descriptor(int fd) { _fd = fd; }
368 int file_descriptor() const { return _fd; }
369
370 char* buffer() const { return _buffer; }
371 int buffer_size() const { return _size; }
372 int position() const { return _pos; }
373 void set_position(int pos) { _pos = pos; }
374
375 void set_error(const char* error) { _error = (char*)os::strdup(error); }
376
377 // all I/O go through this function
378 void write_internal(void* s, int len);
379
380 public:
381 DumpWriter(const char* path);
382 ~DumpWriter();
383
384 void close();
385 bool is_open() const { return file_descriptor() >= 0; }
386 void flush();
387
388 // total number of bytes written to the disk
389 jlong bytes_written() const { return _bytes_written; }
390
391 // adjust the number of bytes written to disk (used to keep the count
392 // of the number of bytes written in case of rewrites)
393 void adjust_bytes_written(jlong n) { _bytes_written += n; }
394
395 // number of (buffered) bytes as yet unwritten to the dump file
396 jlong bytes_unwritten() const { return (jlong)position(); }
397
398 char* error() const { return _error; }
399
400 jlong current_offset();
401 void seek_to_offset(jlong pos);
402
403 // writer functions
404 void write_raw(void* s, int len);
405 void write_u1(u1 x) { write_raw((void*)&x, 1); }
406 void write_u2(u2 x);
407 void write_u4(u4 x);
408 void write_u8(u8 x);
409 void write_objectID(oop o);
410 void write_classID(Klass* k);
411 };
412
413 DumpWriter::DumpWriter(const char* path) {
414 // try to allocate an I/O buffer of io_buffer_size. If there isn't
415 // sufficient memory then reduce size until we can allocate something.
416 _size = io_buffer_size;
417 do {
418 _buffer = (char*)os::malloc(_size);
419 if (_buffer == NULL) {
420 _size = _size >> 1;
421 }
422 } while (_buffer == NULL && _size > 0);
423 assert((_size > 0 && _buffer != NULL) || (_size == 0 && _buffer == NULL), "sanity check");
424 _pos = 0;
425 _error = NULL;
426 _bytes_written = 0L;
427 _fd = os::create_binary_file(path, false); // don't replace existing file
428
429 // if the open failed we record the error
430 if (_fd < 0) {
431 _error = (char*)os::strdup(strerror(errno));
432 }
433 }
434
435 DumpWriter::~DumpWriter() {
436 // flush and close dump file
437 if (file_descriptor() >= 0) {
438 close();
439 }
440 if (_buffer != NULL) os::free(_buffer);
441 if (_error != NULL) os::free(_error);
442 }
443
444 // closes dump file (if open)
445 void DumpWriter::close() {
446 // flush and close dump file
447 if (file_descriptor() >= 0) {
448 flush();
449 ::close(file_descriptor());
450 }
451 }
452
453 // write directly to the file
454 void DumpWriter::write_internal(void* s, int len) {
455 if (is_open()) {
456 int n = ::write(file_descriptor(), s, len);
457 if (n > 0) {
458 _bytes_written += n;
459 }
460 if (n != len) {
461 if (n < 0) {
462 set_error(strerror(errno));
463 } else {
464 set_error("file size limit");
465 }
466 ::close(file_descriptor());
467 set_file_descriptor(-1);
468 }
469 }
470 }
471
472 // write raw bytes
473 void DumpWriter::write_raw(void* s, int len) {
474 if (is_open()) {
475 // flush buffer to make toom
476 if ((position()+ len) >= buffer_size()) {
477 flush();
478 }
479
480 // buffer not available or too big to buffer it
481 if ((buffer() == NULL) || (len >= buffer_size())) {
482 write_internal(s, len);
483 } else {
484 // Should optimize this for u1/u2/u4/u8 sizes.
485 memcpy(buffer() + position(), s, len);
486 set_position(position() + len);
487 }
488 }
489 }
490
491 // flush any buffered bytes to the file
492 void DumpWriter::flush() {
493 if (is_open() && position() > 0) {
494 write_internal(buffer(), position());
495 set_position(0);
496 }
497 }
498
499
500 jlong DumpWriter::current_offset() {
501 if (is_open()) {
502 // the offset is the file offset plus whatever we have buffered
503 jlong offset = os::current_file_offset(file_descriptor());
504 assert(offset >= 0, "lseek failed");
505 return offset + (jlong)position();
506 } else {
507 return (jlong)-1;
508 }
509 }
510
511 void DumpWriter::seek_to_offset(jlong off) {
512 assert(off >= 0, "bad offset");
513
514 // need to flush before seeking
515 flush();
516
517 // may be closed due to I/O error
518 if (is_open()) {
519 jlong n = os::seek_to_file_offset(file_descriptor(), off);
520 assert(n >= 0, "lseek failed");
521 }
522 }
523
524 void DumpWriter::write_u2(u2 x) {
525 u2 v;
526 Bytes::put_Java_u2((address)&v, x);
527 write_raw((void*)&v, 2);
528 }
529
530 void DumpWriter::write_u4(u4 x) {
531 u4 v;
532 Bytes::put_Java_u4((address)&v, x);
533 write_raw((void*)&v, 4);
534 }
535
536 void DumpWriter::write_u8(u8 x) {
537 u8 v;
538 Bytes::put_Java_u8((address)&v, x);
539 write_raw((void*)&v, 8);
540 }
541
542 void DumpWriter::write_objectID(oop o) {
543 address a = (address)((uintptr_t)o);
544 #ifdef _LP64
545 write_u8((u8)a);
546 #else
547 write_u4((u4)a);
548 #endif
549 }
550
551 // We use java mirror as the class ID
552 void DumpWriter::write_classID(Klass* k) {
553 write_objectID(k->java_mirror());
554 }
555
556
557
558 // Support class with a collection of functions used when dumping the heap
559
560 class DumperSupport : AllStatic {
561 public:
562
563 // write a header of the given type
564 static void write_header(DumpWriter* writer, hprofTag tag, u4 len);
565
566 // returns hprof tag for the given type signature
567 static hprofTag sig2tag(symbolOop sig);
568 // returns hprof tag for the given basic type
569 static hprofTag type2tag(BasicType type);
570
571 // returns the size of the instance of the given class
572 static u4 instance_size(klassOop k);
573
574 // dump a jfloat
575 static void dump_float(DumpWriter* writer, jfloat f);
576 // dump a jdouble
577 static void dump_double(DumpWriter* writer, jdouble d);
578 // dumps the raw value of the given field
579 static void dump_field_value(DumpWriter* writer, char type, address addr);
580 // dumps static fields of the given class
581 static void dump_static_fields(DumpWriter* writer, klassOop k);
582 // dump the raw values of the instance fields of the given object
583 static void dump_instance_fields(DumpWriter* writer, oop o);
584 // dumps the definition of the instance fields for a given class
585 static void dump_instance_field_descriptors(DumpWriter* writer, klassOop k);
586 // creates HPROF_GC_INSTANCE_DUMP record for the given object
587 static void dump_instance(DumpWriter* writer, oop o);
588 // creates HPROF_GC_CLASS_DUMP record for the given class and each of its
589 // array classes
590 static void dump_class_and_array_classes(DumpWriter* writer, klassOop k);
591 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
592 // class (and each multi-dimensional array class too)
593 static void dump_basic_type_array_class(DumpWriter* writer, klassOop k);
594
595 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
596 static void dump_object_array(DumpWriter* writer, objArrayOop array);
597 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
598 static void dump_prim_array(DumpWriter* writer, typeArrayOop array);
599 };
600
601 // write a header of the given type
602 void DumperSupport:: write_header(DumpWriter* writer, hprofTag tag, u4 len) {
603 writer->write_u1((u1)tag);
604 writer->write_u4(0); // current ticks
605 writer->write_u4(len);
606 }
607
608 // returns hprof tag for the given type signature
609 hprofTag DumperSupport::sig2tag(symbolOop sig) {
610 switch (sig->byte_at(0)) {
611 case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
612 case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
613 case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
614 case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
615 case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
616 case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
617 case JVM_SIGNATURE_INT : return HPROF_INT;
618 case JVM_SIGNATURE_LONG : return HPROF_LONG;
619 case JVM_SIGNATURE_SHORT : return HPROF_SHORT;
620 case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN;
621 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
622 }
623 }
624
625 hprofTag DumperSupport::type2tag(BasicType type) {
626 switch (type) {
627 case T_BYTE : return HPROF_BYTE;
628 case T_CHAR : return HPROF_CHAR;
629 case T_FLOAT : return HPROF_FLOAT;
630 case T_DOUBLE : return HPROF_DOUBLE;
631 case T_INT : return HPROF_INT;
632 case T_LONG : return HPROF_LONG;
633 case T_SHORT : return HPROF_SHORT;
634 case T_BOOLEAN : return HPROF_BOOLEAN;
635 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
636 }
637 }
638
639 // dump a jfloat
640 void DumperSupport::dump_float(DumpWriter* writer, jfloat f) {
641 if (g_isnan(f)) {
642 writer->write_u4(0x7fc00000); // collapsing NaNs
643 } else {
644 union {
645 int i;
646 float f;
647 } u;
648 u.f = (float)f;
649 writer->write_u4((u4)u.i);
650 }
651 }
652
653 // dump a jdouble
654 void DumperSupport::dump_double(DumpWriter* writer, jdouble d) {
655 union {
656 jlong l;
657 double d;
658 } u;
659 if (g_isnan(d)) { // collapsing NaNs
660 u.l = (jlong)(0x7ff80000);
661 u.l = (u.l << 32);
662 } else {
663 u.d = (double)d;
664 }
665 writer->write_u8((u8)u.l);
666 }
667
668 // dumps the raw value of the given field
669 void DumperSupport::dump_field_value(DumpWriter* writer, char type, address addr) {
670 switch (type) {
671 case JVM_SIGNATURE_CLASS :
672 case JVM_SIGNATURE_ARRAY : {
673 oop* f = (oop*)addr;
674 oop o = *f;
675
676 // reflection and sun.misc.Unsafe classes may have a reference to a
677 // klassOop so filter it out.
678 if (o != NULL && o->is_klass()) {
679 o = NULL;
680 }
681
682 // FIXME: When sharing is enabled we don't emit field references to objects
683 // in shared spaces. We can remove this once we write records for the classes
684 // and strings that are shared.
685 if (o != NULL && o->is_shared()) {
686 o = NULL;
687 }
688 writer->write_objectID(o);
689 break;
690 }
691 case JVM_SIGNATURE_BYTE : {
692 jbyte* b = (jbyte*)addr;
693 writer->write_u1((u1)*b);
694 break;
695 }
696 case JVM_SIGNATURE_CHAR : {
697 jchar* c = (jchar*)addr;
698 writer->write_u2((u2)*c);
699 break;
700 }
701 case JVM_SIGNATURE_SHORT : {
702 jshort* s = (jshort*)addr;
703 writer->write_u2((u2)*s);
704 break;
705 }
706 case JVM_SIGNATURE_FLOAT : {
707 jfloat* f = (jfloat*)addr;
708 dump_float(writer, *f);
709 break;
710 }
711 case JVM_SIGNATURE_DOUBLE : {
712 jdouble* f = (jdouble*)addr;
713 dump_double(writer, *f);
714 break;
715 }
716 case JVM_SIGNATURE_INT : {
717 jint* i = (jint*)addr;
718 writer->write_u4((u4)*i);
719 break;
720 }
721 case JVM_SIGNATURE_LONG : {
722 jlong* l = (jlong*)addr;
723 writer->write_u8((u8)*l);
724 break;
725 }
726 case JVM_SIGNATURE_BOOLEAN : {
727 jboolean* b = (jboolean*)addr;
728 writer->write_u1((u1)*b);
729 break;
730 }
731 default : ShouldNotReachHere();
732 }
733 }
734
735 // returns the size of the instance of the given class
736 u4 DumperSupport::instance_size(klassOop k) {
737 HandleMark hm;
738 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
739
740 int size = 0;
741
742 for (FieldStream fld(ikh, false, false); !fld.eos(); fld.next()) {
743 if (!fld.access_flags().is_static()) {
744 symbolOop sig = fld.signature();
745 switch (sig->byte_at(0)) {
746 case JVM_SIGNATURE_CLASS :
747 case JVM_SIGNATURE_ARRAY : size += oopSize; break;
748
749 case JVM_SIGNATURE_BYTE :
750 case JVM_SIGNATURE_BOOLEAN : size += 1; break;
751
752 case JVM_SIGNATURE_CHAR :
753 case JVM_SIGNATURE_SHORT : size += 2; break;
754
755 case JVM_SIGNATURE_INT :
756 case JVM_SIGNATURE_FLOAT : size += 4; break;
757
758 case JVM_SIGNATURE_LONG :
759 case JVM_SIGNATURE_DOUBLE : size += 8; break;
760
761 default : ShouldNotReachHere();
762 }
763 }
764 }
765 return (u4)size;
766 }
767
768 // dumps static fields of the given class
769 void DumperSupport::dump_static_fields(DumpWriter* writer, klassOop k) {
770 HandleMark hm;
771 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
772
773 // pass 1 - count the static fields
774 u2 field_count = 0;
775 for (FieldStream fldc(ikh, true, true); !fldc.eos(); fldc.next()) {
776 if (fldc.access_flags().is_static()) field_count++;
777 }
778
779 writer->write_u2(field_count);
780
781 // pass 2 - dump the field descriptors and raw values
782 for (FieldStream fld(ikh, true, true); !fld.eos(); fld.next()) {
783 if (fld.access_flags().is_static()) {
784 symbolOop sig = fld.signature();
785
786 writer->write_objectID(fld.name()); // name
787 writer->write_u1(sig2tag(sig)); // type
788
789 // value
790 int offset = fld.offset();
791 address addr = (address)k + offset;
792
793 dump_field_value(writer, sig->byte_at(0), addr);
794 }
795 }
796 }
797
798 // dump the raw values of the instance fields of the given object
799 void DumperSupport::dump_instance_fields(DumpWriter* writer, oop o) {
800 HandleMark hm;
801 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), o->klass());
802
803 for (FieldStream fld(ikh, false, false); !fld.eos(); fld.next()) {
804 if (!fld.access_flags().is_static()) {
805 symbolOop sig = fld.signature();
806 address addr = (address)o + fld.offset();
807
808 dump_field_value(writer, sig->byte_at(0), addr);
809 }
810 }
811 }
812
813 // dumps the definition of the instance fields for a given class
814 void DumperSupport::dump_instance_field_descriptors(DumpWriter* writer, klassOop k) {
815 HandleMark hm;
816 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
817
818 // pass 1 - count the instance fields
819 u2 field_count = 0;
820 for (FieldStream fldc(ikh, true, true); !fldc.eos(); fldc.next()) {
821 if (!fldc.access_flags().is_static()) field_count++;
822 }
823
824 writer->write_u2(field_count);
825
826 // pass 2 - dump the field descriptors
827 for (FieldStream fld(ikh, true, true); !fld.eos(); fld.next()) {
828 if (!fld.access_flags().is_static()) {
829 symbolOop sig = fld.signature();
830
831 writer->write_objectID(fld.name()); // name
832 writer->write_u1(sig2tag(sig)); // type
833 }
834 }
835 }
836
837 // creates HPROF_GC_INSTANCE_DUMP record for the given object
838 void DumperSupport::dump_instance(DumpWriter* writer, oop o) {
839 klassOop k = o->klass();
840
841 writer->write_u1(HPROF_GC_INSTANCE_DUMP);
842 writer->write_objectID(o);
843 writer->write_u4(STACK_TRACE_ID);
844
845 // class ID
846 writer->write_classID(Klass::cast(k));
847
848 // number of bytes that follow
849 writer->write_u4(instance_size(k) );
850
851 // field values
852 dump_instance_fields(writer, o);
853 }
854
855 // creates HPROF_GC_CLASS_DUMP record for the given class and each of
856 // its array classes
857 void DumperSupport::dump_class_and_array_classes(DumpWriter* writer, klassOop k) {
858 Klass* klass = Klass::cast(k);
859 assert(klass->oop_is_instance(), "not an instanceKlass");
860 instanceKlass* ik = (instanceKlass*)klass;
861
862 writer->write_u1(HPROF_GC_CLASS_DUMP);
863
864 // class ID
865 writer->write_classID(ik);
866 writer->write_u4(STACK_TRACE_ID);
867
868 // super class ID
869 klassOop java_super = ik->java_super();
870 if (java_super == NULL) {
871 writer->write_objectID(NULL);
872 } else {
873 writer->write_classID(Klass::cast(java_super));
874 }
875
876 writer->write_objectID(ik->class_loader());
877 writer->write_objectID(ik->signers());
878 writer->write_objectID(ik->protection_domain());
879
880 // reserved
881 writer->write_objectID(NULL);
882 writer->write_objectID(NULL);
883
884 // instance size
885 writer->write_u4(DumperSupport::instance_size(k));
886
887 // size of constant pool - ignored by HAT 1.1
888 writer->write_u2(0);
889
890 // number of static fields
891 dump_static_fields(writer, k);
892
893 // description of instance fields
894 dump_instance_field_descriptors(writer, k);
895
896 // array classes
897 k = klass->array_klass_or_null();
898 while (k != NULL) {
899 Klass* klass = Klass::cast(k);
900 assert(klass->oop_is_objArray(), "not an objArrayKlass");
901
902 writer->write_u1(HPROF_GC_CLASS_DUMP);
903 writer->write_classID(klass);
904 writer->write_u4(STACK_TRACE_ID);
905
906 // super class of array classes is java.lang.Object
907 java_super = klass->java_super();
908 assert(java_super != NULL, "checking");
909 writer->write_classID(Klass::cast(java_super));
910
911 writer->write_objectID(ik->class_loader());
912 writer->write_objectID(ik->signers());
913 writer->write_objectID(ik->protection_domain());
914
915 writer->write_objectID(NULL); // reserved
916 writer->write_objectID(NULL);
917 writer->write_u4(0); // instance size
918 writer->write_u2(0); // constant pool
919 writer->write_u2(0); // static fields
920 writer->write_u2(0); // instance fields
921
922 // get the array class for the next rank
923 k = klass->array_klass_or_null();
924 }
925 }
926
927 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
928 // class (and each multi-dimensional array class too)
929 void DumperSupport::dump_basic_type_array_class(DumpWriter* writer, klassOop k) {
930 // array classes
931 while (k != NULL) {
932 Klass* klass = Klass::cast(k);
933
934 writer->write_u1(HPROF_GC_CLASS_DUMP);
935 writer->write_classID(klass);
936 writer->write_u4(STACK_TRACE_ID);
937
938 // super class of array classes is java.lang.Object
939 klassOop java_super = klass->java_super();
940 assert(java_super != NULL, "checking");
941 writer->write_classID(Klass::cast(java_super));
942
943 writer->write_objectID(NULL); // loader
944 writer->write_objectID(NULL); // signers
945 writer->write_objectID(NULL); // protection domain
946
947 writer->write_objectID(NULL); // reserved
948 writer->write_objectID(NULL);
949 writer->write_u4(0); // instance size
950 writer->write_u2(0); // constant pool
951 writer->write_u2(0); // static fields
952 writer->write_u2(0); // instance fields
953
954 // get the array class for the next rank
955 k = klass->array_klass_or_null();
956 }
957 }
958
959 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
960 void DumperSupport::dump_object_array(DumpWriter* writer, objArrayOop array) {
961
962 // filter this
963 if (array->klass() == Universe::systemObjArrayKlassObj()) return;
964
965 writer->write_u1(HPROF_GC_OBJ_ARRAY_DUMP);
966 writer->write_objectID(array);
967 writer->write_u4(STACK_TRACE_ID);
968 writer->write_u4((u4)array->length());
969
970 // array class ID
971 writer->write_classID(Klass::cast(array->klass()));
972
973 // [id]* elements
974 for (int index=0; index<array->length(); index++) {
975 oop o = array->obj_at(index);
976 writer->write_objectID(o);
977 }
978 }
979
980 #define WRITE_ARRAY(Array, Type, Size) \
981 for (int i=0; i<Array->length(); i++) { writer->write_##Size((Size)array->Type##_at(i)); }
982
983
984 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
985 void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) {
986 BasicType type = typeArrayKlass::cast(array->klass())->element_type();
987
988 writer->write_u1(HPROF_GC_PRIM_ARRAY_DUMP);
989 writer->write_objectID(array);
990 writer->write_u4(STACK_TRACE_ID);
991 writer->write_u4((u4)array->length());
992 writer->write_u1(type2tag(type));
993
994 // nothing to copy
995 if (array->length() == 0) {
996 return;
997 }
998
999 // If the byte ordering is big endian then we can copy most types directly
1000 int length_in_bytes = array->length() * type2aelembytes[type];
1001 assert(length_in_bytes > 0, "nothing to copy");
1002
1003 switch (type) {
1004 case T_INT : {
1005 if (Bytes::is_Java_byte_ordering_different()) {
1006 WRITE_ARRAY(array, int, u4);
1007 } else {
1008 writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
1009 }
1010 break;
1011 }
1012 case T_BYTE : {
1013 writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
1014 break;
1015 }
1016 case T_CHAR : {
1017 if (Bytes::is_Java_byte_ordering_different()) {
1018 WRITE_ARRAY(array, char, u2);
1019 } else {
1020 writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
1021 }
1022 break;
1023 }
1024 case T_SHORT : {
1025 if (Bytes::is_Java_byte_ordering_different()) {
1026 WRITE_ARRAY(array, short, u2);
1027 } else {
1028 writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
1029 }
1030 break;
1031 }
1032 case T_BOOLEAN : {
1033 if (Bytes::is_Java_byte_ordering_different()) {
1034 WRITE_ARRAY(array, bool, u1);
1035 } else {
1036 writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
1037 }
1038 break;
1039 }
1040 case T_LONG : {
1041 if (Bytes::is_Java_byte_ordering_different()) {
1042 WRITE_ARRAY(array, long, u8);
1043 } else {
1044 writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
1045 }
1046 break;
1047 }
1048
1049 // handle float/doubles in a special value to ensure than NaNs are
1050 // written correctly. TO DO: Check if we can avoid this on processors that
1051 // use IEEE 754.
1052
1053 case T_FLOAT : {
1054 for (int i=0; i<array->length(); i++) {
1055 dump_float( writer, array->float_at(i) );
1056 }
1057 break;
1058 }
1059 case T_DOUBLE : {
1060 for (int i=0; i<array->length(); i++) {
1061 dump_double( writer, array->double_at(i) );
1062 }
1063 break;
1064 }
1065 default : ShouldNotReachHere();
1066 }
1067 }
1068
1069
1070 // Support class used to generate HPROF_UTF8 records from the entries in the
1071 // SymbolTable.
1072
1073 class SymbolTableDumper : public OopClosure {
1074 private:
1075 DumpWriter* _writer;
1076 DumpWriter* writer() const { return _writer; }
1077 public:
1078 SymbolTableDumper(DumpWriter* writer) { _writer = writer; }
1079 void do_oop(oop* obj_p);
1080 };
1081
1082 void SymbolTableDumper::do_oop(oop* obj_p) {
1083 ResourceMark rm;
1084 symbolOop sym = (symbolOop)*obj_p;
1085
1086 int len = sym->utf8_length();
1087 if (len > 0) {
1088 char* s = sym->as_utf8();
1089 DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1090 writer()->write_objectID(sym);
1091 writer()->write_raw(s, len);
1092 }
1093 }
1094
1095
1096 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
1097
1098 class JNILocalsDumper : public OopClosure {
1099 private:
1100 DumpWriter* _writer;
1101 u4 _thread_serial_num;
1102 DumpWriter* writer() const { return _writer; }
1103 public:
1104 JNILocalsDumper(DumpWriter* writer, u4 thread_serial_num) {
1105 _writer = writer;
1106 _thread_serial_num = thread_serial_num;
1107 }
1108 void do_oop(oop* obj_p);
1109 };
1110
1111
1112 void JNILocalsDumper::do_oop(oop* obj_p) {
1113 // ignore null or deleted handles
1114 oop o = *obj_p;
1115 if (o != NULL && o != JNIHandles::deleted_handle()) {
1116 writer()->write_u1(HPROF_GC_ROOT_JNI_LOCAL);
1117 writer()->write_objectID(o);
1118 writer()->write_u4(_thread_serial_num);
1119 writer()->write_u4((u4)-1); // empty
1120 }
1121 }
1122
1123
1124 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
1125
1126 class JNIGlobalsDumper : public OopClosure {
1127 private:
1128 DumpWriter* _writer;
1129 DumpWriter* writer() const { return _writer; }
1130
1131 public:
1132 JNIGlobalsDumper(DumpWriter* writer) {
1133 _writer = writer;
1134 }
1135 void do_oop(oop* obj_p);
1136 };
1137
1138 void JNIGlobalsDumper::do_oop(oop* obj_p) {
1139 oop o = *obj_p;
1140
1141 // ignore these
1142 if (o == NULL || o == JNIHandles::deleted_handle()) return;
1143
1144 // we ignore global ref to symbols and other internal objects
1145 if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
1146 writer()->write_u1(HPROF_GC_ROOT_JNI_GLOBAL);
1147 writer()->write_objectID(o);
1148 writer()->write_objectID((oopDesc*)obj_p); // global ref ID
1149 }
1150 };
1151
1152
1153 // Support class used to generate HPROF_GC_ROOT_MONITOR_USED records
1154
1155 class MonitorUsedDumper : public OopClosure {
1156 private:
1157 DumpWriter* _writer;
1158 DumpWriter* writer() const { return _writer; }
1159 public:
1160 MonitorUsedDumper(DumpWriter* writer) {
1161 _writer = writer;
1162 }
1163 void do_oop(oop* obj_p) {
1164 writer()->write_u1(HPROF_GC_ROOT_MONITOR_USED);
1165 writer()->write_objectID(*obj_p);
1166 }
1167 };
1168
1169
1170 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
1171
1172 class StickyClassDumper : public OopClosure {
1173 private:
1174 DumpWriter* _writer;
1175 DumpWriter* writer() const { return _writer; }
1176 public:
1177 StickyClassDumper(DumpWriter* writer) {
1178 _writer = writer;
1179 }
1180 void do_oop(oop* obj_p);
1181 };
1182
1183 void StickyClassDumper::do_oop(oop* obj_p) {
1184 if (*obj_p != NULL) {
1185 oop o = *obj_p;
1186 if (o->is_klass()) {
1187 klassOop k = klassOop(o);
1188 if (Klass::cast(k)->oop_is_instance()) {
1189 instanceKlass* ik = instanceKlass::cast(k);
1190 writer()->write_u1(HPROF_GC_ROOT_STICKY_CLASS);
1191 writer()->write_classID(ik);
1192 }
1193 }
1194 }
1195 }
1196
1197
1198 class VM_HeapDumper;
1199
1200 // Support class using when iterating over the heap.
1201
1202 class HeapObjectDumper : public ObjectClosure {
1203 private:
1204 VM_HeapDumper* _dumper;
1205 DumpWriter* _writer;
1206
1207 VM_HeapDumper* dumper() { return _dumper; }
1208 DumpWriter* writer() { return _writer; }
1209
1210 // used to indicate that a record has been writen
1211 void mark_end_of_record();
1212
1213 public:
1214 HeapObjectDumper(VM_HeapDumper* dumper, DumpWriter* writer) {
1215 _dumper = dumper;
1216 _writer = writer;
1217 }
1218
1219 // called for each object in the heap
1220 void do_object(oop o);
1221 };
1222
1223 void HeapObjectDumper::do_object(oop o) {
1224 // hide the sentinel for deleted handles
1225 if (o == JNIHandles::deleted_handle()) return;
1226
1227 // ignore KlassKlass objects
1228 if (o->is_klass()) return;
1229
1230 // skip classes as these emitted as HPROF_GC_CLASS_DUMP records
1231 if (o->klass() == SystemDictionary::class_klass()) {
1232 if (!java_lang_Class::is_primitive(o)) {
1233 return;
1234 }
1235 }
1236
1237 // create a HPROF_GC_INSTANCE record for each object
1238 if (o->is_instance()) {
1239 DumperSupport::dump_instance(writer(), o);
1240 mark_end_of_record();
1241 } else {
1242 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1243 if (o->is_objArray()) {
1244 DumperSupport::dump_object_array(writer(), objArrayOop(o));
1245 mark_end_of_record();
1246 } else {
1247 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1248 if (o->is_typeArray()) {
1249 DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1250 mark_end_of_record();
1251 }
1252 }
1253 }
1254 }
1255
1256 // The VM operation that performs the heap dump
1257 class VM_HeapDumper : public VM_GC_Operation {
1258 private:
1259 DumpWriter* _writer;
1260 bool _gc_before_heap_dump;
1261 bool _is_segmented_dump;
1262 jlong _dump_start;
1263
1264 // accessors
1265 DumpWriter* writer() const { return _writer; }
1266 bool is_segmented_dump() const { return _is_segmented_dump; }
1267 void set_segmented_dump() { _is_segmented_dump = true; }
1268 jlong dump_start() const { return _dump_start; }
1269 void set_dump_start(jlong pos);
1270
1271 bool skip_operation() const;
1272
1273 // writes a HPROF_LOAD_CLASS record
1274 static void do_load_class(klassOop k);
1275
1276 // writes a HPROF_GC_CLASS_DUMP record for the given class
1277 // (and each array class too)
1278 static void do_class_dump(klassOop k);
1279
1280 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
1281 // array (and each multi-dimensional array too)
1282 static void do_basic_type_array_class_dump(klassOop k);
1283
1284 // HPROF_GC_ROOT_THREAD_OBJ records
1285 void do_thread(JavaThread* thread, u4 thread_serial_num);
1286 void do_threads();
1287
1288 // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
1289 void write_dump_header();
1290
1291 // fixes up the length of the current dump record
1292 void write_current_dump_record_length();
1293
1294 // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
1295 // record in the case of a segmented heap dump)
1296 void end_of_dump();
1297
1298 public:
1299 VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump) :
1300 VM_GC_Operation(0 /* total collections, dummy, ignored */,
1301 0 /* total full collections, dummy, ignored */,
1302 gc_before_heap_dump) {
1303 _writer = writer;
1304 _gc_before_heap_dump = gc_before_heap_dump;
1305 _is_segmented_dump = false;
1306 _dump_start = (jlong)-1;
1307 }
1308
1309 VMOp_Type type() const { return VMOp_HeapDumper; }
1310 // used to mark sub-record boundary
1311 void check_segment_length();
1312 void doit();
1313 };
1314
1315 bool VM_HeapDumper::skip_operation() const {
1316 return false;
1317 }
1318
1319 // sets the dump starting position
1320 void VM_HeapDumper::set_dump_start(jlong pos) {
1321 _dump_start = pos;
1322 }
1323
1324 // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
1325 void VM_HeapDumper::write_dump_header() {
1326 if (writer()->is_open()) {
1327 if (is_segmented_dump()) {
1328 writer()->write_u1(HPROF_HEAP_DUMP_SEGMENT);
1329 } else {
1330 writer()->write_u1(HPROF_HEAP_DUMP);
1331 }
1332 writer()->write_u4(0); // current ticks
1333
1334 // record the starting position for the dump (its length will be fixed up later)
1335 set_dump_start(writer()->current_offset());
1336 writer()->write_u4(0);
1337 }
1338 }
1339
1340 // fixes up the length of the current dump record
1341 void VM_HeapDumper::write_current_dump_record_length() {
1342 if (writer()->is_open()) {
1343 assert(dump_start() >= 0, "no dump start recorded");
1344
1345 // calculate the size of the dump record
1346 jlong dump_end = writer()->current_offset();
1347 jlong dump_len = (dump_end - dump_start() - 4);
1348
1349 // record length must fit in a u4
1350 if (dump_len > (jlong)(4L*(jlong)G)) {
1351 warning("record is too large");
1352 }
1353
1354 // seek to the dump start and fix-up the length
1355 writer()->seek_to_offset(dump_start());
1356 writer()->write_u4((u4)dump_len);
1357
1358 // adjust the total size written to keep the bytes written correct.
1359 writer()->adjust_bytes_written(-((long) sizeof(u4)));
1360
1361 // seek to dump end so we can continue
1362 writer()->seek_to_offset(dump_end);
1363
1364 // no current dump record
1365 set_dump_start((jlong)-1);
1366 }
1367 }
1368
1369 // used on a sub-record boundary to check if we need to start a
1370 // new segment.
1371 void VM_HeapDumper::check_segment_length() {
1372 if (writer()->is_open()) {
1373 if (is_segmented_dump()) {
1374 // don't use current_offset that would be too expensive on a per record basis
1375 jlong dump_end = writer()->bytes_written() + writer()->bytes_unwritten();
1376 assert(dump_end == writer()->current_offset(), "checking");
1377 jlong dump_len = (dump_end - dump_start() - 4);
1378 assert(dump_len >= 0 && dump_len <= max_juint, "bad dump length");
1379
1380 if (dump_len > (jlong)HeapDumpSegmentSize) {
1381 write_current_dump_record_length();
1382 write_dump_header();
1383 }
1384 }
1385 }
1386 }
1387
1388 // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
1389 // record in the case of a segmented heap dump)
1390 void VM_HeapDumper::end_of_dump() {
1391 if (writer()->is_open()) {
1392 write_current_dump_record_length();
1393
1394 // for segmented dump we write the end record
1395 if (is_segmented_dump()) {
1396 writer()->write_u1(HPROF_HEAP_DUMP_END);
1397 writer()->write_u4(0);
1398 writer()->write_u4(0);
1399 }
1400 }
1401 }
1402
1403 // marks sub-record boundary
1404 void HeapObjectDumper::mark_end_of_record() {
1405 dumper()->check_segment_length();
1406 }
1407
1408 // writes a HPROF_LOAD_CLASS record for the class (and each of its
1409 // array classes)
1410 void VM_HeapDumper::do_load_class(klassOop k) {
1411 static u4 class_serial_num = 0;
1412
1413 VM_HeapDumper* dumper = ((VM_HeapDumper*)VMThread::vm_operation());
1414 DumpWriter* writer = dumper->writer();
1415
1416 // len of HPROF_LOAD_CLASS record
1417 u4 remaining = 2*oopSize + 2*sizeof(u4);
1418
1419 // write a HPROF_LOAD_CLASS for the class and each array class
1420 do {
1421 DumperSupport::write_header(writer, HPROF_LOAD_CLASS, remaining);
1422
1423 // class serial number is just a number
1424 writer->write_u4(++class_serial_num);
1425
1426 // class ID
1427 Klass* klass = Klass::cast(k);
1428 writer->write_classID(klass);
1429
1430 writer->write_u4(STACK_TRACE_ID);
1431
1432 // class name ID
1433 symbolOop name = klass->name();
1434 writer->write_objectID(name);
1435
1436 // write a LOAD_CLASS record for the array type (if it exists)
1437 k = klass->array_klass_or_null();
1438 } while (k != NULL);
1439 }
1440
1441 // writes a HPROF_GC_CLASS_DUMP record for the given class
1442 void VM_HeapDumper::do_class_dump(klassOop k) {
1443 VM_HeapDumper* dumper = ((VM_HeapDumper*)VMThread::vm_operation());
1444 DumpWriter* writer = dumper->writer();
1445 DumperSupport::dump_class_and_array_classes(writer, k);
1446 }
1447
1448 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
1449 // array (and each multi-dimensional array too)
1450 void VM_HeapDumper::do_basic_type_array_class_dump(klassOop k) {
1451 VM_HeapDumper* dumper = ((VM_HeapDumper*)VMThread::vm_operation());
1452 DumpWriter* writer = dumper->writer();
1453 DumperSupport::dump_basic_type_array_class(writer, k);
1454 }
1455
1456 // Walk the stack of the given thread.
1457 // Dumps a HPROF_GC_ROOT_JAVA_FRAME record for each local
1458 // Dumps a HPROF_GC_ROOT_JNI_LOCAL record for each JNI local
1459 void VM_HeapDumper::do_thread(JavaThread* java_thread, u4 thread_serial_num) {
1460 JNILocalsDumper blk(writer(), thread_serial_num);
1461
1462 oop threadObj = java_thread->threadObj();
1463 assert(threadObj != NULL, "sanity check");
1464
1465 // JNI locals for the top frame
1466 java_thread->active_handles()->oops_do(&blk);
1467
1468 if (java_thread->has_last_Java_frame()) {
1469
1470 // vframes are resource allocated
1471 Thread* current_thread = Thread::current();
1472 ResourceMark rm(current_thread);
1473 HandleMark hm(current_thread);
1474
1475 RegisterMap reg_map(java_thread);
1476 frame f = java_thread->last_frame();
1477 vframe* vf = vframe::new_vframe(&f, &reg_map, java_thread);
1478
1479 while (vf != NULL) {
1480 if (vf->is_java_frame()) {
1481
1482 // java frame (interpreted, compiled, ...)
1483 javaVFrame *jvf = javaVFrame::cast(vf);
1484
1485 if (!(jvf->method()->is_native())) {
1486 StackValueCollection* locals = jvf->locals();
1487 for (int slot=0; slot<locals->size(); slot++) {
1488 if (locals->at(slot)->type() == T_OBJECT) {
1489 oop o = locals->obj_at(slot)();
1490
1491 if (o != NULL) {
1492 writer()->write_u1(HPROF_GC_ROOT_JAVA_FRAME);
1493 writer()->write_objectID(o);
1494 writer()->write_u4(thread_serial_num);
1495 writer()->write_u4((u4)-1); // empty
1496 }
1497 }
1498 }
1499 }
1500 } else {
1501
1502 // externalVFrame - if it's an entry frame then report any JNI locals
1503 // as roots
1504 frame* fr = vf->frame_pointer();
1505 assert(fr != NULL, "sanity check");
1506 if (fr->is_entry_frame()) {
1507 fr->entry_frame_call_wrapper()->handles()->oops_do(&blk);
1508 }
1509 }
1510
1511 vf = vf->sender();
1512 }
1513 }
1514 }
1515
1516
1517 // write a HPROF_GC_ROOT_THREAD_OBJ record for each java thread. Then walk
1518 // the stack so that locals and JNI locals are dumped.
1519 void VM_HeapDumper::do_threads() {
1520 u4 thread_serial_num = 0;
1521 for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
1522 oop threadObj = thread->threadObj();
1523 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
1524 ++thread_serial_num;
1525
1526 writer()->write_u1(HPROF_GC_ROOT_THREAD_OBJ);
1527 writer()->write_objectID(threadObj);
1528 writer()->write_u4(thread_serial_num);
1529 writer()->write_u4(STACK_TRACE_ID);
1530
1531 do_thread(thread, thread_serial_num);
1532 }
1533 }
1534 }
1535
1536
1537 // The VM operation that dumps the heap. The dump consists of the following
1538 // records:
1539 //
1540 // HPROF_HEADER
1541 // HPROF_TRACE
1542 // [HPROF_UTF8]*
1543 // [HPROF_LOAD_CLASS]*
1544 // [HPROF_GC_CLASS_DUMP]*
1545 // HPROF_HEAP_DUMP
1546 //
1547 // The HPROF_TRACE record after the header is "dummy trace" record which does
1548 // not include any frames. Other records which require a stack trace ID will
1549 // specify the trace ID of this record (1). It also means we can run HAT without
1550 // needing the -stack false option.
1551 //
1552 // The HPROF_HEAP_DUMP record has a length following by sub-records. To allow
1553 // the heap dump be generated in a single pass we remember the position of
1554 // the dump length and fix it up after all sub-records have been written.
1555 // To generate the sub-records we iterate over the heap, writing
1556 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
1557 // records as we go. Once that is done we write records for some of the GC
1558 // roots.
1559
1560 void VM_HeapDumper::doit() {
1561
1562 HandleMark hm;
1563 CollectedHeap* ch = Universe::heap();
1564 if (_gc_before_heap_dump) {
1565 ch->collect_as_vm_thread(GCCause::_heap_dump);
1566 } else {
1567 // make the heap parsable (no need to retire TLABs)
1568 ch->ensure_parsability(false);
1569 }
1570
1571 // Write the file header - use 1.0.2 for large heaps, otherwise 1.0.1
1572 size_t used;
1573 const char* header;
1574 #ifndef SERIALGC
1575 if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap) {
1576 used = GenCollectedHeap::heap()->used();
1577 } else {
1578 used = ParallelScavengeHeap::heap()->used();
1579 }
1580 #else // SERIALGC
1581 used = GenCollectedHeap::heap()->used();
1582 #endif // SERIALGC
1583 if (used > (size_t)SegmentedHeapDumpThreshold) {
1584 set_segmented_dump();
1585 header = "JAVA PROFILE 1.0.2";
1586 } else {
1587 header = "JAVA PROFILE 1.0.1";
1588 }
1589 // header is few bytes long - no chance to overflow int
1590 writer()->write_raw((void*)header, (int)strlen(header));
1591 writer()->write_u1(0); // terminator
1592 writer()->write_u4(oopSize);
1593 writer()->write_u8(os::javaTimeMillis());
1594
1595 // HPROF_TRACE record without any frames
1596 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
1597 writer()->write_u4(STACK_TRACE_ID);
1598 writer()->write_u4(0); // thread number
1599 writer()->write_u4(0); // frame count
1600
1601 // HPROF_UTF8 records
1602 SymbolTableDumper sym_dumper(writer());
1603 SymbolTable::oops_do(&sym_dumper);
1604
1605 // write HPROF_LOAD_CLASS records
1606 SystemDictionary::classes_do(&do_load_class);
1607 Universe::basic_type_classes_do(&do_load_class);
1608
1609 // write HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT
1610 write_dump_header();
1611
1612 // Writes HPROF_GC_CLASS_DUMP records
1613 SystemDictionary::classes_do(&do_class_dump);
1614 Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
1615 check_segment_length();
1616
1617 // writes HPROF_GC_INSTANCE_DUMP records.
1618 // After each sub-record is written check_segment_length will be invoked. When
1619 // generated a segmented heap dump this allows us to check if the current
1620 // segment exceeds a threshold and if so, then a new segment is started.
1621 // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
1622 // of the heap dump.
1623 HeapObjectDumper obj_dumper(this, writer());
1624 Universe::heap()->object_iterate(&obj_dumper);
1625
1626 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
1627 do_threads();
1628 check_segment_length();
1629
1630 // HPROF_GC_ROOT_MONITOR_USED
1631 MonitorUsedDumper mon_dumper(writer());
1632 ObjectSynchronizer::oops_do(&mon_dumper);
1633 check_segment_length();
1634
1635 // HPROF_GC_ROOT_JNI_GLOBAL
1636 JNIGlobalsDumper jni_dumper(writer());
1637 JNIHandles::oops_do(&jni_dumper);
1638 check_segment_length();
1639
1640 // HPROF_GC_ROOT_STICKY_CLASS
1641 StickyClassDumper class_dumper(writer());
1642 SystemDictionary::always_strong_oops_do(&class_dumper);
1643
1644 // fixes up the length of the dump record. In the case of a segmented
1645 // heap then the HPROF_HEAP_DUMP_END record is also written.
1646 end_of_dump();
1647 }
1648
1649
1650 // dump the heap to given path.
1651 int HeapDumper::dump(const char* path) {
1652 assert(path != NULL && strlen(path) > 0, "path missing");
1653
1654 // print message in interactive case
1655 if (print_to_tty()) {
1656 tty->print_cr("Dumping heap to %s ...", path);
1657 timer()->start();
1658 }
1659
1660 // create the dump writer. If the file can be opened then bail
1661 DumpWriter writer(path);
1662 if (!writer.is_open()) {
1663 set_error(writer.error());
1664 if (print_to_tty()) {
1665 tty->print_cr("Unable to create %s: %s", path,
1666 (error() != NULL) ? error() : "reason unknown");
1667 }
1668 return -1;
1669 }
1670
1671 // generate the dump
1672 VM_HeapDumper dumper(&writer, _gc_before_heap_dump);
1673 VMThread::execute(&dumper);
1674
1675 // close dump file and record any error that the writer may have encountered
1676 writer.close();
1677 set_error(writer.error());
1678
1679 // print message in interactive case
1680 if (print_to_tty()) {
1681 timer()->stop();
1682 if (error() == NULL) {
1683 char msg[256];
1684 sprintf(msg, "Heap dump file created [%s bytes in %3.3f secs]",
1685 os::jlong_format_specifier(), timer()->seconds());
1686 tty->print_cr(msg, writer.bytes_written());
1687 } else {
1688 tty->print_cr("Dump file is incomplete: %s", writer.error());
1689 }
1690 }
1691
1692 return (writer.error() == NULL) ? 0 : -1;
1693 }
1694
1695 // stop timer (if still active), and free any error string we might be holding
1696 HeapDumper::~HeapDumper() {
1697 if (timer()->is_active()) {
1698 timer()->stop();
1699 }
1700 set_error(NULL);
1701 }
1702
1703
1704 // returns the error string (resource allocated), or NULL
1705 char* HeapDumper::error_as_C_string() const {
1706 if (error() != NULL) {
1707 char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
1708 strcpy(str, error());
1709 return str;
1710 } else {
1711 return NULL;
1712 }
1713 }
1714
1715 // set the error string
1716 void HeapDumper::set_error(char* error) {
1717 if (_error != NULL) {
1718 os::free(_error);
1719 }
1720 if (error == NULL) {
1721 _error = NULL;
1722 } else {
1723 _error = os::strdup(error);
1724 assert(_error != NULL, "allocation failure");
1725 }
1726 }
1727
1728
1729 // Called by error reporting
1730 void HeapDumper::dump_heap() {
1731 static char path[JVM_MAXPATHLEN];
1732
1733 // The dump file defaults to java_pid<pid>.hprof in the current working
1734 // directory. HeapDumpPath=<file> can be used to specify an alternative
1735 // dump file name or a directory where dump file is created.
1736 bool use_default_filename = true;
1737 if (HeapDumpPath == NULL || HeapDumpPath[0] == '\0') {
1738 path[0] = '\0'; // HeapDumpPath=<file> not specified
1739 } else {
1740 assert(strlen(HeapDumpPath) < sizeof(path), "HeapDumpPath too long");
1741 strcpy(path, HeapDumpPath);
1742 // check if the path is a directory (must exist)
1743 DIR* dir = os::opendir(path);
1744 if (dir == NULL) {
1745 use_default_filename = false;
1746 } else {
1747 // HeapDumpPath specified a directory. We append a file separator
1748 // (if needed).
1749 os::closedir(dir);
1750 size_t fs_len = strlen(os::file_separator());
1751 if (strlen(path) >= fs_len) {
1752 char* end = path;
1753 end += (strlen(path) - fs_len);
1754 if (strcmp(end, os::file_separator()) != 0) {
1755 assert(strlen(path) + strlen(os::file_separator()) < sizeof(path),
1756 "HeapDumpPath too long");
1757 strcat(path, os::file_separator());
1758 }
1759 }
1760 }
1761 }
1762 // If HeapDumpPath wasn't a file name then we append the default name
1763 if (use_default_filename) {
1764 char fn[32];
1765 sprintf(fn, "java_pid%d.hprof", os::current_process_id());
1766 assert(strlen(path) + strlen(fn) < sizeof(path), "HeapDumpPath too long");
1767 strcat(path, fn);
1768 }
1769
1770 HeapDumper dumper(false /* no GC before heap dump */,
1771 true /* send to tty */);
1772 dumper.dump(path);
1773 }