Sun, 13 Apr 2008 17:43:42 -0400
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv
Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold
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 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_heapDumper.cpp.incl"
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 */
295 // HPROF tags
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,
312 // 1.0.2 record types
313 HPROF_HEAP_DUMP_SEGMENT = 0x1C,
314 HPROF_HEAP_DUMP_END = 0x2C,
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,
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;
344 // Default stack trace ID (used for dummy HPROF_TRACE record)
345 enum {
346 STACK_TRACE_ID = 1
347 };
350 // Supports I/O operations on a dump file
352 class DumpWriter : public StackObj {
353 private:
354 enum {
355 io_buffer_size = 8*M
356 };
358 int _fd; // file descriptor (-1 if dump file not open)
359 jlong _bytes_written; // number of byte written to dump file
361 char* _buffer; // internal buffer
362 int _size;
363 int _pos;
365 char* _error; // error message when I/O fails
367 void set_file_descriptor(int fd) { _fd = fd; }
368 int file_descriptor() const { return _fd; }
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; }
375 void set_error(const char* error) { _error = (char*)os::strdup(error); }
377 // all I/O go through this function
378 void write_internal(void* s, int len);
380 public:
381 DumpWriter(const char* path);
382 ~DumpWriter();
384 void close();
385 bool is_open() const { return file_descriptor() >= 0; }
386 void flush();
388 // total number of bytes written to the disk
389 jlong bytes_written() const { return _bytes_written; }
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; }
395 // number of (buffered) bytes as yet unwritten to the dump file
396 jlong bytes_unwritten() const { return (jlong)position(); }
398 char* error() const { return _error; }
400 jlong current_offset();
401 void seek_to_offset(jlong pos);
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 };
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
429 // if the open failed we record the error
430 if (_fd < 0) {
431 _error = (char*)os::strdup(strerror(errno));
432 }
433 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
511 void DumpWriter::seek_to_offset(jlong off) {
512 assert(off >= 0, "bad offset");
514 // need to flush before seeking
515 flush();
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 }
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 }
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 }
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 }
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 }
551 // We use java mirror as the class ID
552 void DumpWriter::write_classID(Klass* k) {
553 write_objectID(k->java_mirror());
554 }
558 // Support class with a collection of functions used when dumping the heap
560 class DumperSupport : AllStatic {
561 public:
563 // write a header of the given type
564 static void write_header(DumpWriter* writer, hprofTag tag, u4 len);
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);
571 // returns the size of the instance of the given class
572 static u4 instance_size(klassOop k);
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);
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 };
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 }
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 }
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 }
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 }
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 }
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 o;
674 if (UseCompressedOops) {
675 o = oopDesc::load_decode_heap_oop((narrowOop*)addr);
676 } else {
677 o = oopDesc::load_decode_heap_oop((oop*)addr);
678 }
680 // reflection and sun.misc.Unsafe classes may have a reference to a
681 // klassOop so filter it out.
682 if (o != NULL && o->is_klass()) {
683 o = NULL;
684 }
686 // FIXME: When sharing is enabled we don't emit field references to objects
687 // in shared spaces. We can remove this once we write records for the classes
688 // and strings that are shared.
689 if (o != NULL && o->is_shared()) {
690 o = NULL;
691 }
692 writer->write_objectID(o);
693 break;
694 }
695 case JVM_SIGNATURE_BYTE : {
696 jbyte* b = (jbyte*)addr;
697 writer->write_u1((u1)*b);
698 break;
699 }
700 case JVM_SIGNATURE_CHAR : {
701 jchar* c = (jchar*)addr;
702 writer->write_u2((u2)*c);
703 break;
704 }
705 case JVM_SIGNATURE_SHORT : {
706 jshort* s = (jshort*)addr;
707 writer->write_u2((u2)*s);
708 break;
709 }
710 case JVM_SIGNATURE_FLOAT : {
711 jfloat* f = (jfloat*)addr;
712 dump_float(writer, *f);
713 break;
714 }
715 case JVM_SIGNATURE_DOUBLE : {
716 jdouble* f = (jdouble*)addr;
717 dump_double(writer, *f);
718 break;
719 }
720 case JVM_SIGNATURE_INT : {
721 jint* i = (jint*)addr;
722 writer->write_u4((u4)*i);
723 break;
724 }
725 case JVM_SIGNATURE_LONG : {
726 jlong* l = (jlong*)addr;
727 writer->write_u8((u8)*l);
728 break;
729 }
730 case JVM_SIGNATURE_BOOLEAN : {
731 jboolean* b = (jboolean*)addr;
732 writer->write_u1((u1)*b);
733 break;
734 }
735 default : ShouldNotReachHere();
736 }
737 }
739 // returns the size of the instance of the given class
740 u4 DumperSupport::instance_size(klassOop k) {
741 HandleMark hm;
742 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
744 int size = 0;
746 for (FieldStream fld(ikh, false, false); !fld.eos(); fld.next()) {
747 if (!fld.access_flags().is_static()) {
748 symbolOop sig = fld.signature();
749 switch (sig->byte_at(0)) {
750 case JVM_SIGNATURE_CLASS :
751 case JVM_SIGNATURE_ARRAY : size += oopSize; break;
753 case JVM_SIGNATURE_BYTE :
754 case JVM_SIGNATURE_BOOLEAN : size += 1; break;
756 case JVM_SIGNATURE_CHAR :
757 case JVM_SIGNATURE_SHORT : size += 2; break;
759 case JVM_SIGNATURE_INT :
760 case JVM_SIGNATURE_FLOAT : size += 4; break;
762 case JVM_SIGNATURE_LONG :
763 case JVM_SIGNATURE_DOUBLE : size += 8; break;
765 default : ShouldNotReachHere();
766 }
767 }
768 }
769 return (u4)size;
770 }
772 // dumps static fields of the given class
773 void DumperSupport::dump_static_fields(DumpWriter* writer, klassOop k) {
774 HandleMark hm;
775 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
777 // pass 1 - count the static fields
778 u2 field_count = 0;
779 for (FieldStream fldc(ikh, true, true); !fldc.eos(); fldc.next()) {
780 if (fldc.access_flags().is_static()) field_count++;
781 }
783 writer->write_u2(field_count);
785 // pass 2 - dump the field descriptors and raw values
786 for (FieldStream fld(ikh, true, true); !fld.eos(); fld.next()) {
787 if (fld.access_flags().is_static()) {
788 symbolOop sig = fld.signature();
790 writer->write_objectID(fld.name()); // name
791 writer->write_u1(sig2tag(sig)); // type
793 // value
794 int offset = fld.offset();
795 address addr = (address)k + offset;
797 dump_field_value(writer, sig->byte_at(0), addr);
798 }
799 }
800 }
802 // dump the raw values of the instance fields of the given object
803 void DumperSupport::dump_instance_fields(DumpWriter* writer, oop o) {
804 HandleMark hm;
805 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), o->klass());
807 for (FieldStream fld(ikh, false, false); !fld.eos(); fld.next()) {
808 if (!fld.access_flags().is_static()) {
809 symbolOop sig = fld.signature();
810 address addr = (address)o + fld.offset();
812 dump_field_value(writer, sig->byte_at(0), addr);
813 }
814 }
815 }
817 // dumps the definition of the instance fields for a given class
818 void DumperSupport::dump_instance_field_descriptors(DumpWriter* writer, klassOop k) {
819 HandleMark hm;
820 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
822 // pass 1 - count the instance fields
823 u2 field_count = 0;
824 for (FieldStream fldc(ikh, true, true); !fldc.eos(); fldc.next()) {
825 if (!fldc.access_flags().is_static()) field_count++;
826 }
828 writer->write_u2(field_count);
830 // pass 2 - dump the field descriptors
831 for (FieldStream fld(ikh, true, true); !fld.eos(); fld.next()) {
832 if (!fld.access_flags().is_static()) {
833 symbolOop sig = fld.signature();
835 writer->write_objectID(fld.name()); // name
836 writer->write_u1(sig2tag(sig)); // type
837 }
838 }
839 }
841 // creates HPROF_GC_INSTANCE_DUMP record for the given object
842 void DumperSupport::dump_instance(DumpWriter* writer, oop o) {
843 klassOop k = o->klass();
845 writer->write_u1(HPROF_GC_INSTANCE_DUMP);
846 writer->write_objectID(o);
847 writer->write_u4(STACK_TRACE_ID);
849 // class ID
850 writer->write_classID(Klass::cast(k));
852 // number of bytes that follow
853 writer->write_u4(instance_size(k) );
855 // field values
856 dump_instance_fields(writer, o);
857 }
859 // creates HPROF_GC_CLASS_DUMP record for the given class and each of
860 // its array classes
861 void DumperSupport::dump_class_and_array_classes(DumpWriter* writer, klassOop k) {
862 Klass* klass = Klass::cast(k);
863 assert(klass->oop_is_instance(), "not an instanceKlass");
864 instanceKlass* ik = (instanceKlass*)klass;
866 writer->write_u1(HPROF_GC_CLASS_DUMP);
868 // class ID
869 writer->write_classID(ik);
870 writer->write_u4(STACK_TRACE_ID);
872 // super class ID
873 klassOop java_super = ik->java_super();
874 if (java_super == NULL) {
875 writer->write_objectID(NULL);
876 } else {
877 writer->write_classID(Klass::cast(java_super));
878 }
880 writer->write_objectID(ik->class_loader());
881 writer->write_objectID(ik->signers());
882 writer->write_objectID(ik->protection_domain());
884 // reserved
885 writer->write_objectID(NULL);
886 writer->write_objectID(NULL);
888 // instance size
889 writer->write_u4(DumperSupport::instance_size(k));
891 // size of constant pool - ignored by HAT 1.1
892 writer->write_u2(0);
894 // number of static fields
895 dump_static_fields(writer, k);
897 // description of instance fields
898 dump_instance_field_descriptors(writer, k);
900 // array classes
901 k = klass->array_klass_or_null();
902 while (k != NULL) {
903 Klass* klass = Klass::cast(k);
904 assert(klass->oop_is_objArray(), "not an objArrayKlass");
906 writer->write_u1(HPROF_GC_CLASS_DUMP);
907 writer->write_classID(klass);
908 writer->write_u4(STACK_TRACE_ID);
910 // super class of array classes is java.lang.Object
911 java_super = klass->java_super();
912 assert(java_super != NULL, "checking");
913 writer->write_classID(Klass::cast(java_super));
915 writer->write_objectID(ik->class_loader());
916 writer->write_objectID(ik->signers());
917 writer->write_objectID(ik->protection_domain());
919 writer->write_objectID(NULL); // reserved
920 writer->write_objectID(NULL);
921 writer->write_u4(0); // instance size
922 writer->write_u2(0); // constant pool
923 writer->write_u2(0); // static fields
924 writer->write_u2(0); // instance fields
926 // get the array class for the next rank
927 k = klass->array_klass_or_null();
928 }
929 }
931 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
932 // class (and each multi-dimensional array class too)
933 void DumperSupport::dump_basic_type_array_class(DumpWriter* writer, klassOop k) {
934 // array classes
935 while (k != NULL) {
936 Klass* klass = Klass::cast(k);
938 writer->write_u1(HPROF_GC_CLASS_DUMP);
939 writer->write_classID(klass);
940 writer->write_u4(STACK_TRACE_ID);
942 // super class of array classes is java.lang.Object
943 klassOop java_super = klass->java_super();
944 assert(java_super != NULL, "checking");
945 writer->write_classID(Klass::cast(java_super));
947 writer->write_objectID(NULL); // loader
948 writer->write_objectID(NULL); // signers
949 writer->write_objectID(NULL); // protection domain
951 writer->write_objectID(NULL); // reserved
952 writer->write_objectID(NULL);
953 writer->write_u4(0); // instance size
954 writer->write_u2(0); // constant pool
955 writer->write_u2(0); // static fields
956 writer->write_u2(0); // instance fields
958 // get the array class for the next rank
959 k = klass->array_klass_or_null();
960 }
961 }
963 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
964 void DumperSupport::dump_object_array(DumpWriter* writer, objArrayOop array) {
966 // filter this
967 if (array->klass() == Universe::systemObjArrayKlassObj()) return;
969 writer->write_u1(HPROF_GC_OBJ_ARRAY_DUMP);
970 writer->write_objectID(array);
971 writer->write_u4(STACK_TRACE_ID);
972 writer->write_u4((u4)array->length());
974 // array class ID
975 writer->write_classID(Klass::cast(array->klass()));
977 // [id]* elements
978 for (int index=0; index<array->length(); index++) {
979 oop o = array->obj_at(index);
980 writer->write_objectID(o);
981 }
982 }
984 #define WRITE_ARRAY(Array, Type, Size) \
985 for (int i=0; i<Array->length(); i++) { writer->write_##Size((Size)array->Type##_at(i)); }
988 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
989 void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) {
990 BasicType type = typeArrayKlass::cast(array->klass())->element_type();
992 writer->write_u1(HPROF_GC_PRIM_ARRAY_DUMP);
993 writer->write_objectID(array);
994 writer->write_u4(STACK_TRACE_ID);
995 writer->write_u4((u4)array->length());
996 writer->write_u1(type2tag(type));
998 // nothing to copy
999 if (array->length() == 0) {
1000 return;
1001 }
1003 // If the byte ordering is big endian then we can copy most types directly
1004 int length_in_bytes = array->length() * type2aelembytes(type);
1005 assert(length_in_bytes > 0, "nothing to copy");
1007 switch (type) {
1008 case T_INT : {
1009 if (Bytes::is_Java_byte_ordering_different()) {
1010 WRITE_ARRAY(array, int, u4);
1011 } else {
1012 writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
1013 }
1014 break;
1015 }
1016 case T_BYTE : {
1017 writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
1018 break;
1019 }
1020 case T_CHAR : {
1021 if (Bytes::is_Java_byte_ordering_different()) {
1022 WRITE_ARRAY(array, char, u2);
1023 } else {
1024 writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
1025 }
1026 break;
1027 }
1028 case T_SHORT : {
1029 if (Bytes::is_Java_byte_ordering_different()) {
1030 WRITE_ARRAY(array, short, u2);
1031 } else {
1032 writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
1033 }
1034 break;
1035 }
1036 case T_BOOLEAN : {
1037 if (Bytes::is_Java_byte_ordering_different()) {
1038 WRITE_ARRAY(array, bool, u1);
1039 } else {
1040 writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
1041 }
1042 break;
1043 }
1044 case T_LONG : {
1045 if (Bytes::is_Java_byte_ordering_different()) {
1046 WRITE_ARRAY(array, long, u8);
1047 } else {
1048 writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
1049 }
1050 break;
1051 }
1053 // handle float/doubles in a special value to ensure than NaNs are
1054 // written correctly. TO DO: Check if we can avoid this on processors that
1055 // use IEEE 754.
1057 case T_FLOAT : {
1058 for (int i=0; i<array->length(); i++) {
1059 dump_float( writer, array->float_at(i) );
1060 }
1061 break;
1062 }
1063 case T_DOUBLE : {
1064 for (int i=0; i<array->length(); i++) {
1065 dump_double( writer, array->double_at(i) );
1066 }
1067 break;
1068 }
1069 default : ShouldNotReachHere();
1070 }
1071 }
1074 // Support class used to generate HPROF_UTF8 records from the entries in the
1075 // SymbolTable.
1077 class SymbolTableDumper : public OopClosure {
1078 private:
1079 DumpWriter* _writer;
1080 DumpWriter* writer() const { return _writer; }
1081 public:
1082 SymbolTableDumper(DumpWriter* writer) { _writer = writer; }
1083 void do_oop(oop* obj_p);
1084 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1085 };
1087 void SymbolTableDumper::do_oop(oop* obj_p) {
1088 ResourceMark rm;
1089 symbolOop sym = (symbolOop)*obj_p;
1091 int len = sym->utf8_length();
1092 if (len > 0) {
1093 char* s = sym->as_utf8();
1094 DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1095 writer()->write_objectID(sym);
1096 writer()->write_raw(s, len);
1097 }
1098 }
1101 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
1103 class JNILocalsDumper : public OopClosure {
1104 private:
1105 DumpWriter* _writer;
1106 u4 _thread_serial_num;
1107 DumpWriter* writer() const { return _writer; }
1108 public:
1109 JNILocalsDumper(DumpWriter* writer, u4 thread_serial_num) {
1110 _writer = writer;
1111 _thread_serial_num = thread_serial_num;
1112 }
1113 void do_oop(oop* obj_p);
1114 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1115 };
1118 void JNILocalsDumper::do_oop(oop* obj_p) {
1119 // ignore null or deleted handles
1120 oop o = *obj_p;
1121 if (o != NULL && o != JNIHandles::deleted_handle()) {
1122 writer()->write_u1(HPROF_GC_ROOT_JNI_LOCAL);
1123 writer()->write_objectID(o);
1124 writer()->write_u4(_thread_serial_num);
1125 writer()->write_u4((u4)-1); // empty
1126 }
1127 }
1130 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
1132 class JNIGlobalsDumper : public OopClosure {
1133 private:
1134 DumpWriter* _writer;
1135 DumpWriter* writer() const { return _writer; }
1137 public:
1138 JNIGlobalsDumper(DumpWriter* writer) {
1139 _writer = writer;
1140 }
1141 void do_oop(oop* obj_p);
1142 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1143 };
1145 void JNIGlobalsDumper::do_oop(oop* obj_p) {
1146 oop o = *obj_p;
1148 // ignore these
1149 if (o == NULL || o == JNIHandles::deleted_handle()) return;
1151 // we ignore global ref to symbols and other internal objects
1152 if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
1153 writer()->write_u1(HPROF_GC_ROOT_JNI_GLOBAL);
1154 writer()->write_objectID(o);
1155 writer()->write_objectID((oopDesc*)obj_p); // global ref ID
1156 }
1157 };
1160 // Support class used to generate HPROF_GC_ROOT_MONITOR_USED records
1162 class MonitorUsedDumper : public OopClosure {
1163 private:
1164 DumpWriter* _writer;
1165 DumpWriter* writer() const { return _writer; }
1166 public:
1167 MonitorUsedDumper(DumpWriter* writer) {
1168 _writer = writer;
1169 }
1170 void do_oop(oop* obj_p) {
1171 writer()->write_u1(HPROF_GC_ROOT_MONITOR_USED);
1172 writer()->write_objectID(*obj_p);
1173 }
1174 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1175 };
1178 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
1180 class StickyClassDumper : public OopClosure {
1181 private:
1182 DumpWriter* _writer;
1183 DumpWriter* writer() const { return _writer; }
1184 public:
1185 StickyClassDumper(DumpWriter* writer) {
1186 _writer = writer;
1187 }
1188 void do_oop(oop* obj_p);
1189 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1190 };
1192 void StickyClassDumper::do_oop(oop* obj_p) {
1193 if (*obj_p != NULL) {
1194 oop o = *obj_p;
1195 if (o->is_klass()) {
1196 klassOop k = klassOop(o);
1197 if (Klass::cast(k)->oop_is_instance()) {
1198 instanceKlass* ik = instanceKlass::cast(k);
1199 writer()->write_u1(HPROF_GC_ROOT_STICKY_CLASS);
1200 writer()->write_classID(ik);
1201 }
1202 }
1203 }
1204 }
1207 class VM_HeapDumper;
1209 // Support class using when iterating over the heap.
1211 class HeapObjectDumper : public ObjectClosure {
1212 private:
1213 VM_HeapDumper* _dumper;
1214 DumpWriter* _writer;
1216 VM_HeapDumper* dumper() { return _dumper; }
1217 DumpWriter* writer() { return _writer; }
1219 // used to indicate that a record has been writen
1220 void mark_end_of_record();
1222 public:
1223 HeapObjectDumper(VM_HeapDumper* dumper, DumpWriter* writer) {
1224 _dumper = dumper;
1225 _writer = writer;
1226 }
1228 // called for each object in the heap
1229 void do_object(oop o);
1230 };
1232 void HeapObjectDumper::do_object(oop o) {
1233 // hide the sentinel for deleted handles
1234 if (o == JNIHandles::deleted_handle()) return;
1236 // ignore KlassKlass objects
1237 if (o->is_klass()) return;
1239 // skip classes as these emitted as HPROF_GC_CLASS_DUMP records
1240 if (o->klass() == SystemDictionary::class_klass()) {
1241 if (!java_lang_Class::is_primitive(o)) {
1242 return;
1243 }
1244 }
1246 // create a HPROF_GC_INSTANCE record for each object
1247 if (o->is_instance()) {
1248 DumperSupport::dump_instance(writer(), o);
1249 mark_end_of_record();
1250 } else {
1251 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1252 if (o->is_objArray()) {
1253 DumperSupport::dump_object_array(writer(), objArrayOop(o));
1254 mark_end_of_record();
1255 } else {
1256 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1257 if (o->is_typeArray()) {
1258 DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1259 mark_end_of_record();
1260 }
1261 }
1262 }
1263 }
1265 // The VM operation that performs the heap dump
1266 class VM_HeapDumper : public VM_GC_Operation {
1267 private:
1268 DumpWriter* _writer;
1269 bool _gc_before_heap_dump;
1270 bool _is_segmented_dump;
1271 jlong _dump_start;
1273 // accessors
1274 DumpWriter* writer() const { return _writer; }
1275 bool is_segmented_dump() const { return _is_segmented_dump; }
1276 void set_segmented_dump() { _is_segmented_dump = true; }
1277 jlong dump_start() const { return _dump_start; }
1278 void set_dump_start(jlong pos);
1280 bool skip_operation() const;
1282 // writes a HPROF_LOAD_CLASS record
1283 static void do_load_class(klassOop k);
1285 // writes a HPROF_GC_CLASS_DUMP record for the given class
1286 // (and each array class too)
1287 static void do_class_dump(klassOop k);
1289 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
1290 // array (and each multi-dimensional array too)
1291 static void do_basic_type_array_class_dump(klassOop k);
1293 // HPROF_GC_ROOT_THREAD_OBJ records
1294 void do_thread(JavaThread* thread, u4 thread_serial_num);
1295 void do_threads();
1297 // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
1298 void write_dump_header();
1300 // fixes up the length of the current dump record
1301 void write_current_dump_record_length();
1303 // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
1304 // record in the case of a segmented heap dump)
1305 void end_of_dump();
1307 public:
1308 VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump) :
1309 VM_GC_Operation(0 /* total collections, dummy, ignored */,
1310 0 /* total full collections, dummy, ignored */,
1311 gc_before_heap_dump) {
1312 _writer = writer;
1313 _gc_before_heap_dump = gc_before_heap_dump;
1314 _is_segmented_dump = false;
1315 _dump_start = (jlong)-1;
1316 }
1318 VMOp_Type type() const { return VMOp_HeapDumper; }
1319 // used to mark sub-record boundary
1320 void check_segment_length();
1321 void doit();
1322 };
1324 bool VM_HeapDumper::skip_operation() const {
1325 return false;
1326 }
1328 // sets the dump starting position
1329 void VM_HeapDumper::set_dump_start(jlong pos) {
1330 _dump_start = pos;
1331 }
1333 // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
1334 void VM_HeapDumper::write_dump_header() {
1335 if (writer()->is_open()) {
1336 if (is_segmented_dump()) {
1337 writer()->write_u1(HPROF_HEAP_DUMP_SEGMENT);
1338 } else {
1339 writer()->write_u1(HPROF_HEAP_DUMP);
1340 }
1341 writer()->write_u4(0); // current ticks
1343 // record the starting position for the dump (its length will be fixed up later)
1344 set_dump_start(writer()->current_offset());
1345 writer()->write_u4(0);
1346 }
1347 }
1349 // fixes up the length of the current dump record
1350 void VM_HeapDumper::write_current_dump_record_length() {
1351 if (writer()->is_open()) {
1352 assert(dump_start() >= 0, "no dump start recorded");
1354 // calculate the size of the dump record
1355 jlong dump_end = writer()->current_offset();
1356 jlong dump_len = (dump_end - dump_start() - 4);
1358 // record length must fit in a u4
1359 if (dump_len > (jlong)(4L*(jlong)G)) {
1360 warning("record is too large");
1361 }
1363 // seek to the dump start and fix-up the length
1364 writer()->seek_to_offset(dump_start());
1365 writer()->write_u4((u4)dump_len);
1367 // adjust the total size written to keep the bytes written correct.
1368 writer()->adjust_bytes_written(-((long) sizeof(u4)));
1370 // seek to dump end so we can continue
1371 writer()->seek_to_offset(dump_end);
1373 // no current dump record
1374 set_dump_start((jlong)-1);
1375 }
1376 }
1378 // used on a sub-record boundary to check if we need to start a
1379 // new segment.
1380 void VM_HeapDumper::check_segment_length() {
1381 if (writer()->is_open()) {
1382 if (is_segmented_dump()) {
1383 // don't use current_offset that would be too expensive on a per record basis
1384 jlong dump_end = writer()->bytes_written() + writer()->bytes_unwritten();
1385 assert(dump_end == writer()->current_offset(), "checking");
1386 jlong dump_len = (dump_end - dump_start() - 4);
1387 assert(dump_len >= 0 && dump_len <= max_juint, "bad dump length");
1389 if (dump_len > (jlong)HeapDumpSegmentSize) {
1390 write_current_dump_record_length();
1391 write_dump_header();
1392 }
1393 }
1394 }
1395 }
1397 // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
1398 // record in the case of a segmented heap dump)
1399 void VM_HeapDumper::end_of_dump() {
1400 if (writer()->is_open()) {
1401 write_current_dump_record_length();
1403 // for segmented dump we write the end record
1404 if (is_segmented_dump()) {
1405 writer()->write_u1(HPROF_HEAP_DUMP_END);
1406 writer()->write_u4(0);
1407 writer()->write_u4(0);
1408 }
1409 }
1410 }
1412 // marks sub-record boundary
1413 void HeapObjectDumper::mark_end_of_record() {
1414 dumper()->check_segment_length();
1415 }
1417 // writes a HPROF_LOAD_CLASS record for the class (and each of its
1418 // array classes)
1419 void VM_HeapDumper::do_load_class(klassOop k) {
1420 static u4 class_serial_num = 0;
1422 VM_HeapDumper* dumper = ((VM_HeapDumper*)VMThread::vm_operation());
1423 DumpWriter* writer = dumper->writer();
1425 // len of HPROF_LOAD_CLASS record
1426 u4 remaining = 2*oopSize + 2*sizeof(u4);
1428 // write a HPROF_LOAD_CLASS for the class and each array class
1429 do {
1430 DumperSupport::write_header(writer, HPROF_LOAD_CLASS, remaining);
1432 // class serial number is just a number
1433 writer->write_u4(++class_serial_num);
1435 // class ID
1436 Klass* klass = Klass::cast(k);
1437 writer->write_classID(klass);
1439 writer->write_u4(STACK_TRACE_ID);
1441 // class name ID
1442 symbolOop name = klass->name();
1443 writer->write_objectID(name);
1445 // write a LOAD_CLASS record for the array type (if it exists)
1446 k = klass->array_klass_or_null();
1447 } while (k != NULL);
1448 }
1450 // writes a HPROF_GC_CLASS_DUMP record for the given class
1451 void VM_HeapDumper::do_class_dump(klassOop k) {
1452 VM_HeapDumper* dumper = ((VM_HeapDumper*)VMThread::vm_operation());
1453 DumpWriter* writer = dumper->writer();
1454 DumperSupport::dump_class_and_array_classes(writer, k);
1455 }
1457 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
1458 // array (and each multi-dimensional array too)
1459 void VM_HeapDumper::do_basic_type_array_class_dump(klassOop k) {
1460 VM_HeapDumper* dumper = ((VM_HeapDumper*)VMThread::vm_operation());
1461 DumpWriter* writer = dumper->writer();
1462 DumperSupport::dump_basic_type_array_class(writer, k);
1463 }
1465 // Walk the stack of the given thread.
1466 // Dumps a HPROF_GC_ROOT_JAVA_FRAME record for each local
1467 // Dumps a HPROF_GC_ROOT_JNI_LOCAL record for each JNI local
1468 void VM_HeapDumper::do_thread(JavaThread* java_thread, u4 thread_serial_num) {
1469 JNILocalsDumper blk(writer(), thread_serial_num);
1471 oop threadObj = java_thread->threadObj();
1472 assert(threadObj != NULL, "sanity check");
1474 // JNI locals for the top frame
1475 java_thread->active_handles()->oops_do(&blk);
1477 if (java_thread->has_last_Java_frame()) {
1479 // vframes are resource allocated
1480 Thread* current_thread = Thread::current();
1481 ResourceMark rm(current_thread);
1482 HandleMark hm(current_thread);
1484 RegisterMap reg_map(java_thread);
1485 frame f = java_thread->last_frame();
1486 vframe* vf = vframe::new_vframe(&f, ®_map, java_thread);
1488 while (vf != NULL) {
1489 if (vf->is_java_frame()) {
1491 // java frame (interpreted, compiled, ...)
1492 javaVFrame *jvf = javaVFrame::cast(vf);
1494 if (!(jvf->method()->is_native())) {
1495 StackValueCollection* locals = jvf->locals();
1496 for (int slot=0; slot<locals->size(); slot++) {
1497 if (locals->at(slot)->type() == T_OBJECT) {
1498 oop o = locals->obj_at(slot)();
1500 if (o != NULL) {
1501 writer()->write_u1(HPROF_GC_ROOT_JAVA_FRAME);
1502 writer()->write_objectID(o);
1503 writer()->write_u4(thread_serial_num);
1504 writer()->write_u4((u4)-1); // empty
1505 }
1506 }
1507 }
1508 }
1509 } else {
1511 // externalVFrame - if it's an entry frame then report any JNI locals
1512 // as roots
1513 frame* fr = vf->frame_pointer();
1514 assert(fr != NULL, "sanity check");
1515 if (fr->is_entry_frame()) {
1516 fr->entry_frame_call_wrapper()->handles()->oops_do(&blk);
1517 }
1518 }
1520 vf = vf->sender();
1521 }
1522 }
1523 }
1526 // write a HPROF_GC_ROOT_THREAD_OBJ record for each java thread. Then walk
1527 // the stack so that locals and JNI locals are dumped.
1528 void VM_HeapDumper::do_threads() {
1529 u4 thread_serial_num = 0;
1530 for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
1531 oop threadObj = thread->threadObj();
1532 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
1533 ++thread_serial_num;
1535 writer()->write_u1(HPROF_GC_ROOT_THREAD_OBJ);
1536 writer()->write_objectID(threadObj);
1537 writer()->write_u4(thread_serial_num);
1538 writer()->write_u4(STACK_TRACE_ID);
1540 do_thread(thread, thread_serial_num);
1541 }
1542 }
1543 }
1546 // The VM operation that dumps the heap. The dump consists of the following
1547 // records:
1548 //
1549 // HPROF_HEADER
1550 // HPROF_TRACE
1551 // [HPROF_UTF8]*
1552 // [HPROF_LOAD_CLASS]*
1553 // [HPROF_GC_CLASS_DUMP]*
1554 // HPROF_HEAP_DUMP
1555 //
1556 // The HPROF_TRACE record after the header is "dummy trace" record which does
1557 // not include any frames. Other records which require a stack trace ID will
1558 // specify the trace ID of this record (1). It also means we can run HAT without
1559 // needing the -stack false option.
1560 //
1561 // The HPROF_HEAP_DUMP record has a length following by sub-records. To allow
1562 // the heap dump be generated in a single pass we remember the position of
1563 // the dump length and fix it up after all sub-records have been written.
1564 // To generate the sub-records we iterate over the heap, writing
1565 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
1566 // records as we go. Once that is done we write records for some of the GC
1567 // roots.
1569 void VM_HeapDumper::doit() {
1571 HandleMark hm;
1572 CollectedHeap* ch = Universe::heap();
1573 if (_gc_before_heap_dump) {
1574 ch->collect_as_vm_thread(GCCause::_heap_dump);
1575 } else {
1576 // make the heap parsable (no need to retire TLABs)
1577 ch->ensure_parsability(false);
1578 }
1580 // Write the file header - use 1.0.2 for large heaps, otherwise 1.0.1
1581 size_t used;
1582 const char* header;
1583 #ifndef SERIALGC
1584 if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap) {
1585 used = GenCollectedHeap::heap()->used();
1586 } else {
1587 used = ParallelScavengeHeap::heap()->used();
1588 }
1589 #else // SERIALGC
1590 used = GenCollectedHeap::heap()->used();
1591 #endif // SERIALGC
1592 if (used > (size_t)SegmentedHeapDumpThreshold) {
1593 set_segmented_dump();
1594 header = "JAVA PROFILE 1.0.2";
1595 } else {
1596 header = "JAVA PROFILE 1.0.1";
1597 }
1598 // header is few bytes long - no chance to overflow int
1599 writer()->write_raw((void*)header, (int)strlen(header));
1600 writer()->write_u1(0); // terminator
1601 writer()->write_u4(oopSize);
1602 writer()->write_u8(os::javaTimeMillis());
1604 // HPROF_TRACE record without any frames
1605 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
1606 writer()->write_u4(STACK_TRACE_ID);
1607 writer()->write_u4(0); // thread number
1608 writer()->write_u4(0); // frame count
1610 // HPROF_UTF8 records
1611 SymbolTableDumper sym_dumper(writer());
1612 SymbolTable::oops_do(&sym_dumper);
1614 // write HPROF_LOAD_CLASS records
1615 SystemDictionary::classes_do(&do_load_class);
1616 Universe::basic_type_classes_do(&do_load_class);
1618 // write HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT
1619 write_dump_header();
1621 // Writes HPROF_GC_CLASS_DUMP records
1622 SystemDictionary::classes_do(&do_class_dump);
1623 Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
1624 check_segment_length();
1626 // writes HPROF_GC_INSTANCE_DUMP records.
1627 // After each sub-record is written check_segment_length will be invoked. When
1628 // generated a segmented heap dump this allows us to check if the current
1629 // segment exceeds a threshold and if so, then a new segment is started.
1630 // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
1631 // of the heap dump.
1632 HeapObjectDumper obj_dumper(this, writer());
1633 Universe::heap()->object_iterate(&obj_dumper);
1635 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
1636 do_threads();
1637 check_segment_length();
1639 // HPROF_GC_ROOT_MONITOR_USED
1640 MonitorUsedDumper mon_dumper(writer());
1641 ObjectSynchronizer::oops_do(&mon_dumper);
1642 check_segment_length();
1644 // HPROF_GC_ROOT_JNI_GLOBAL
1645 JNIGlobalsDumper jni_dumper(writer());
1646 JNIHandles::oops_do(&jni_dumper);
1647 check_segment_length();
1649 // HPROF_GC_ROOT_STICKY_CLASS
1650 StickyClassDumper class_dumper(writer());
1651 SystemDictionary::always_strong_oops_do(&class_dumper);
1653 // fixes up the length of the dump record. In the case of a segmented
1654 // heap then the HPROF_HEAP_DUMP_END record is also written.
1655 end_of_dump();
1656 }
1659 // dump the heap to given path.
1660 int HeapDumper::dump(const char* path) {
1661 assert(path != NULL && strlen(path) > 0, "path missing");
1663 // print message in interactive case
1664 if (print_to_tty()) {
1665 tty->print_cr("Dumping heap to %s ...", path);
1666 timer()->start();
1667 }
1669 // create the dump writer. If the file can be opened then bail
1670 DumpWriter writer(path);
1671 if (!writer.is_open()) {
1672 set_error(writer.error());
1673 if (print_to_tty()) {
1674 tty->print_cr("Unable to create %s: %s", path,
1675 (error() != NULL) ? error() : "reason unknown");
1676 }
1677 return -1;
1678 }
1680 // generate the dump
1681 VM_HeapDumper dumper(&writer, _gc_before_heap_dump);
1682 VMThread::execute(&dumper);
1684 // close dump file and record any error that the writer may have encountered
1685 writer.close();
1686 set_error(writer.error());
1688 // print message in interactive case
1689 if (print_to_tty()) {
1690 timer()->stop();
1691 if (error() == NULL) {
1692 char msg[256];
1693 sprintf(msg, "Heap dump file created [%s bytes in %3.3f secs]",
1694 os::jlong_format_specifier(), timer()->seconds());
1695 tty->print_cr(msg, writer.bytes_written());
1696 } else {
1697 tty->print_cr("Dump file is incomplete: %s", writer.error());
1698 }
1699 }
1701 return (writer.error() == NULL) ? 0 : -1;
1702 }
1704 // stop timer (if still active), and free any error string we might be holding
1705 HeapDumper::~HeapDumper() {
1706 if (timer()->is_active()) {
1707 timer()->stop();
1708 }
1709 set_error(NULL);
1710 }
1713 // returns the error string (resource allocated), or NULL
1714 char* HeapDumper::error_as_C_string() const {
1715 if (error() != NULL) {
1716 char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
1717 strcpy(str, error());
1718 return str;
1719 } else {
1720 return NULL;
1721 }
1722 }
1724 // set the error string
1725 void HeapDumper::set_error(char* error) {
1726 if (_error != NULL) {
1727 os::free(_error);
1728 }
1729 if (error == NULL) {
1730 _error = NULL;
1731 } else {
1732 _error = os::strdup(error);
1733 assert(_error != NULL, "allocation failure");
1734 }
1735 }
1738 // Called by error reporting
1739 void HeapDumper::dump_heap() {
1740 static char path[JVM_MAXPATHLEN];
1742 // The dump file defaults to java_pid<pid>.hprof in the current working
1743 // directory. HeapDumpPath=<file> can be used to specify an alternative
1744 // dump file name or a directory where dump file is created.
1745 bool use_default_filename = true;
1746 if (HeapDumpPath == NULL || HeapDumpPath[0] == '\0') {
1747 path[0] = '\0'; // HeapDumpPath=<file> not specified
1748 } else {
1749 assert(strlen(HeapDumpPath) < sizeof(path), "HeapDumpPath too long");
1750 strcpy(path, HeapDumpPath);
1751 // check if the path is a directory (must exist)
1752 DIR* dir = os::opendir(path);
1753 if (dir == NULL) {
1754 use_default_filename = false;
1755 } else {
1756 // HeapDumpPath specified a directory. We append a file separator
1757 // (if needed).
1758 os::closedir(dir);
1759 size_t fs_len = strlen(os::file_separator());
1760 if (strlen(path) >= fs_len) {
1761 char* end = path;
1762 end += (strlen(path) - fs_len);
1763 if (strcmp(end, os::file_separator()) != 0) {
1764 assert(strlen(path) + strlen(os::file_separator()) < sizeof(path),
1765 "HeapDumpPath too long");
1766 strcat(path, os::file_separator());
1767 }
1768 }
1769 }
1770 }
1771 // If HeapDumpPath wasn't a file name then we append the default name
1772 if (use_default_filename) {
1773 char fn[32];
1774 sprintf(fn, "java_pid%d.hprof", os::current_process_id());
1775 assert(strlen(path) + strlen(fn) < sizeof(path), "HeapDumpPath too long");
1776 strcat(path, fn);
1777 }
1779 HeapDumper dumper(false /* no GC before heap dump */,
1780 true /* send to tty */);
1781 dumper.dump(path);
1782 }