Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/services/heapDumper.cpp
41144 views
1
/*
2
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "jvm.h"
27
#include "classfile/classLoaderData.inline.hpp"
28
#include "classfile/classLoaderDataGraph.hpp"
29
#include "classfile/javaClasses.inline.hpp"
30
#include "classfile/symbolTable.hpp"
31
#include "classfile/vmClasses.hpp"
32
#include "classfile/vmSymbols.hpp"
33
#include "gc/shared/gcLocker.hpp"
34
#include "gc/shared/gcVMOperations.hpp"
35
#include "gc/shared/workgroup.hpp"
36
#include "jfr/jfrEvents.hpp"
37
#include "memory/allocation.inline.hpp"
38
#include "memory/resourceArea.hpp"
39
#include "memory/universe.hpp"
40
#include "oops/klass.inline.hpp"
41
#include "oops/objArrayKlass.hpp"
42
#include "oops/objArrayOop.inline.hpp"
43
#include "oops/oop.inline.hpp"
44
#include "oops/typeArrayOop.inline.hpp"
45
#include "runtime/frame.inline.hpp"
46
#include "runtime/handles.inline.hpp"
47
#include "runtime/javaCalls.hpp"
48
#include "runtime/jniHandles.hpp"
49
#include "runtime/os.hpp"
50
#include "runtime/reflectionUtils.hpp"
51
#include "runtime/thread.inline.hpp"
52
#include "runtime/threadSMR.hpp"
53
#include "runtime/vframe.hpp"
54
#include "runtime/vmThread.hpp"
55
#include "runtime/vmOperations.hpp"
56
#include "services/heapDumper.hpp"
57
#include "services/heapDumperCompression.hpp"
58
#include "services/threadService.hpp"
59
#include "utilities/macros.hpp"
60
#include "utilities/ostream.hpp"
61
62
/*
63
* HPROF binary format - description copied from:
64
* src/share/demo/jvmti/hprof/hprof_io.c
65
*
66
*
67
* header "JAVA PROFILE 1.0.2" (0-terminated)
68
*
69
* u4 size of identifiers. Identifiers are used to represent
70
* UTF8 strings, objects, stack traces, etc. They usually
71
* have the same size as host pointers. For example, on
72
* Solaris and Win32, the size is 4.
73
* u4 high word
74
* u4 low word number of milliseconds since 0:00 GMT, 1/1/70
75
* [record]* a sequence of records.
76
*
77
*
78
* Record format:
79
*
80
* u1 a TAG denoting the type of the record
81
* u4 number of *microseconds* since the time stamp in the
82
* header. (wraps around in a little more than an hour)
83
* u4 number of bytes *remaining* in the record. Note that
84
* this number excludes the tag and the length field itself.
85
* [u1]* BODY of the record (a sequence of bytes)
86
*
87
*
88
* The following TAGs are supported:
89
*
90
* TAG BODY notes
91
*----------------------------------------------------------
92
* HPROF_UTF8 a UTF8-encoded name
93
*
94
* id name ID
95
* [u1]* UTF8 characters (no trailing zero)
96
*
97
* HPROF_LOAD_CLASS a newly loaded class
98
*
99
* u4 class serial number (> 0)
100
* id class object ID
101
* u4 stack trace serial number
102
* id class name ID
103
*
104
* HPROF_UNLOAD_CLASS an unloading class
105
*
106
* u4 class serial_number
107
*
108
* HPROF_FRAME a Java stack frame
109
*
110
* id stack frame ID
111
* id method name ID
112
* id method signature ID
113
* id source file name ID
114
* u4 class serial number
115
* i4 line number. >0: normal
116
* -1: unknown
117
* -2: compiled method
118
* -3: native method
119
*
120
* HPROF_TRACE a Java stack trace
121
*
122
* u4 stack trace serial number
123
* u4 thread serial number
124
* u4 number of frames
125
* [id]* stack frame IDs
126
*
127
*
128
* HPROF_ALLOC_SITES a set of heap allocation sites, obtained after GC
129
*
130
* u2 flags 0x0001: incremental vs. complete
131
* 0x0002: sorted by allocation vs. live
132
* 0x0004: whether to force a GC
133
* u4 cutoff ratio
134
* u4 total live bytes
135
* u4 total live instances
136
* u8 total bytes allocated
137
* u8 total instances allocated
138
* u4 number of sites that follow
139
* [u1 is_array: 0: normal object
140
* 2: object array
141
* 4: boolean array
142
* 5: char array
143
* 6: float array
144
* 7: double array
145
* 8: byte array
146
* 9: short array
147
* 10: int array
148
* 11: long array
149
* u4 class serial number (may be zero during startup)
150
* u4 stack trace serial number
151
* u4 number of bytes alive
152
* u4 number of instances alive
153
* u4 number of bytes allocated
154
* u4]* number of instance allocated
155
*
156
* HPROF_START_THREAD a newly started thread.
157
*
158
* u4 thread serial number (> 0)
159
* id thread object ID
160
* u4 stack trace serial number
161
* id thread name ID
162
* id thread group name ID
163
* id thread group parent name ID
164
*
165
* HPROF_END_THREAD a terminating thread.
166
*
167
* u4 thread serial number
168
*
169
* HPROF_HEAP_SUMMARY heap summary
170
*
171
* u4 total live bytes
172
* u4 total live instances
173
* u8 total bytes allocated
174
* u8 total instances allocated
175
*
176
* HPROF_HEAP_DUMP denote a heap dump
177
*
178
* [heap dump sub-records]*
179
*
180
* There are four kinds of heap dump sub-records:
181
*
182
* u1 sub-record type
183
*
184
* HPROF_GC_ROOT_UNKNOWN unknown root
185
*
186
* id object ID
187
*
188
* HPROF_GC_ROOT_THREAD_OBJ thread object
189
*
190
* id thread object ID (may be 0 for a
191
* thread newly attached through JNI)
192
* u4 thread sequence number
193
* u4 stack trace sequence number
194
*
195
* HPROF_GC_ROOT_JNI_GLOBAL JNI global ref root
196
*
197
* id object ID
198
* id JNI global ref ID
199
*
200
* HPROF_GC_ROOT_JNI_LOCAL JNI local ref
201
*
202
* id object ID
203
* u4 thread serial number
204
* u4 frame # in stack trace (-1 for empty)
205
*
206
* HPROF_GC_ROOT_JAVA_FRAME Java stack frame
207
*
208
* id object ID
209
* u4 thread serial number
210
* u4 frame # in stack trace (-1 for empty)
211
*
212
* HPROF_GC_ROOT_NATIVE_STACK Native stack
213
*
214
* id object ID
215
* u4 thread serial number
216
*
217
* HPROF_GC_ROOT_STICKY_CLASS System class
218
*
219
* id object ID
220
*
221
* HPROF_GC_ROOT_THREAD_BLOCK Reference from thread block
222
*
223
* id object ID
224
* u4 thread serial number
225
*
226
* HPROF_GC_ROOT_MONITOR_USED Busy monitor
227
*
228
* id object ID
229
*
230
* HPROF_GC_CLASS_DUMP dump of a class object
231
*
232
* id class object ID
233
* u4 stack trace serial number
234
* id super class object ID
235
* id class loader object ID
236
* id signers object ID
237
* id protection domain object ID
238
* id reserved
239
* id reserved
240
*
241
* u4 instance size (in bytes)
242
*
243
* u2 size of constant pool
244
* [u2, constant pool index,
245
* ty, type
246
* 2: object
247
* 4: boolean
248
* 5: char
249
* 6: float
250
* 7: double
251
* 8: byte
252
* 9: short
253
* 10: int
254
* 11: long
255
* vl]* and value
256
*
257
* u2 number of static fields
258
* [id, static field name,
259
* ty, type,
260
* vl]* and value
261
*
262
* u2 number of inst. fields (not inc. super)
263
* [id, instance field name,
264
* ty]* type
265
*
266
* HPROF_GC_INSTANCE_DUMP dump of a normal object
267
*
268
* id object ID
269
* u4 stack trace serial number
270
* id class object ID
271
* u4 number of bytes that follow
272
* [vl]* instance field values (class, followed
273
* by super, super's super ...)
274
*
275
* HPROF_GC_OBJ_ARRAY_DUMP dump of an object array
276
*
277
* id array object ID
278
* u4 stack trace serial number
279
* u4 number of elements
280
* id array class ID
281
* [id]* elements
282
*
283
* HPROF_GC_PRIM_ARRAY_DUMP dump of a primitive array
284
*
285
* id array object ID
286
* u4 stack trace serial number
287
* u4 number of elements
288
* u1 element type
289
* 4: boolean array
290
* 5: char array
291
* 6: float array
292
* 7: double array
293
* 8: byte array
294
* 9: short array
295
* 10: int array
296
* 11: long array
297
* [u1]* elements
298
*
299
* HPROF_CPU_SAMPLES a set of sample traces of running threads
300
*
301
* u4 total number of samples
302
* u4 # of traces
303
* [u4 # of samples
304
* u4]* stack trace serial number
305
*
306
* HPROF_CONTROL_SETTINGS the settings of on/off switches
307
*
308
* u4 0x00000001: alloc traces on/off
309
* 0x00000002: cpu sampling on/off
310
* u2 stack trace depth
311
*
312
*
313
* When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
314
* be generated as a sequence of heap dump segments. This sequence is
315
* terminated by an end record. The additional tags allowed by format
316
* "JAVA PROFILE 1.0.2" are:
317
*
318
* HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment
319
*
320
* [heap dump sub-records]*
321
* The same sub-record types allowed by HPROF_HEAP_DUMP
322
*
323
* HPROF_HEAP_DUMP_END denotes the end of a heap dump
324
*
325
*/
326
327
328
// HPROF tags
329
330
typedef enum {
331
// top-level records
332
HPROF_UTF8 = 0x01,
333
HPROF_LOAD_CLASS = 0x02,
334
HPROF_UNLOAD_CLASS = 0x03,
335
HPROF_FRAME = 0x04,
336
HPROF_TRACE = 0x05,
337
HPROF_ALLOC_SITES = 0x06,
338
HPROF_HEAP_SUMMARY = 0x07,
339
HPROF_START_THREAD = 0x0A,
340
HPROF_END_THREAD = 0x0B,
341
HPROF_HEAP_DUMP = 0x0C,
342
HPROF_CPU_SAMPLES = 0x0D,
343
HPROF_CONTROL_SETTINGS = 0x0E,
344
345
// 1.0.2 record types
346
HPROF_HEAP_DUMP_SEGMENT = 0x1C,
347
HPROF_HEAP_DUMP_END = 0x2C,
348
349
// field types
350
HPROF_ARRAY_OBJECT = 0x01,
351
HPROF_NORMAL_OBJECT = 0x02,
352
HPROF_BOOLEAN = 0x04,
353
HPROF_CHAR = 0x05,
354
HPROF_FLOAT = 0x06,
355
HPROF_DOUBLE = 0x07,
356
HPROF_BYTE = 0x08,
357
HPROF_SHORT = 0x09,
358
HPROF_INT = 0x0A,
359
HPROF_LONG = 0x0B,
360
361
// data-dump sub-records
362
HPROF_GC_ROOT_UNKNOWN = 0xFF,
363
HPROF_GC_ROOT_JNI_GLOBAL = 0x01,
364
HPROF_GC_ROOT_JNI_LOCAL = 0x02,
365
HPROF_GC_ROOT_JAVA_FRAME = 0x03,
366
HPROF_GC_ROOT_NATIVE_STACK = 0x04,
367
HPROF_GC_ROOT_STICKY_CLASS = 0x05,
368
HPROF_GC_ROOT_THREAD_BLOCK = 0x06,
369
HPROF_GC_ROOT_MONITOR_USED = 0x07,
370
HPROF_GC_ROOT_THREAD_OBJ = 0x08,
371
HPROF_GC_CLASS_DUMP = 0x20,
372
HPROF_GC_INSTANCE_DUMP = 0x21,
373
HPROF_GC_OBJ_ARRAY_DUMP = 0x22,
374
HPROF_GC_PRIM_ARRAY_DUMP = 0x23
375
} hprofTag;
376
377
// Default stack trace ID (used for dummy HPROF_TRACE record)
378
enum {
379
STACK_TRACE_ID = 1,
380
INITIAL_CLASS_COUNT = 200
381
};
382
383
// Supports I/O operations for a dump
384
385
class DumpWriter : public StackObj {
386
private:
387
enum {
388
io_buffer_max_size = 1*M,
389
io_buffer_max_waste = 10*K,
390
dump_segment_header_size = 9
391
};
392
393
char* _buffer; // internal buffer
394
size_t _size;
395
size_t _pos;
396
397
bool _in_dump_segment; // Are we currently in a dump segment?
398
bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
399
DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
400
DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
401
402
CompressionBackend _backend; // Does the actual writing.
403
404
void flush();
405
406
char* buffer() const { return _buffer; }
407
size_t buffer_size() const { return _size; }
408
size_t position() const { return _pos; }
409
void set_position(size_t pos) { _pos = pos; }
410
411
// Can be called if we have enough room in the buffer.
412
void write_fast(void* s, size_t len);
413
414
// Returns true if we have enough room in the buffer for 'len' bytes.
415
bool can_write_fast(size_t len);
416
417
public:
418
// Takes ownership of the writer and compressor.
419
DumpWriter(AbstractWriter* writer, AbstractCompressor* compressor);
420
421
~DumpWriter();
422
423
// total number of bytes written to the disk
424
julong bytes_written() const { return (julong) _backend.get_written(); }
425
426
char const* error() const { return _backend.error(); }
427
428
// writer functions
429
void write_raw(void* s, size_t len);
430
void write_u1(u1 x);
431
void write_u2(u2 x);
432
void write_u4(u4 x);
433
void write_u8(u8 x);
434
void write_objectID(oop o);
435
void write_symbolID(Symbol* o);
436
void write_classID(Klass* k);
437
void write_id(u4 x);
438
439
// Start a new sub-record. Starts a new heap dump segment if needed.
440
void start_sub_record(u1 tag, u4 len);
441
// Ends the current sub-record.
442
void end_sub_record();
443
// Finishes the current dump segment if not already finished.
444
void finish_dump_segment();
445
446
// Called by threads used for parallel writing.
447
void writer_loop() { _backend.thread_loop(); }
448
// Called when finished to release the threads.
449
void deactivate() { flush(); _backend.deactivate(); }
450
};
451
452
// Check for error after constructing the object and destroy it in case of an error.
453
DumpWriter::DumpWriter(AbstractWriter* writer, AbstractCompressor* compressor) :
454
_buffer(NULL),
455
_size(0),
456
_pos(0),
457
_in_dump_segment(false),
458
_backend(writer, compressor, io_buffer_max_size, io_buffer_max_waste) {
459
flush();
460
}
461
462
DumpWriter::~DumpWriter() {
463
flush();
464
}
465
466
void DumpWriter::write_fast(void* s, size_t len) {
467
assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
468
assert(buffer_size() - position() >= len, "Must fit");
469
debug_only(_sub_record_left -= len);
470
471
memcpy(buffer() + position(), s, len);
472
set_position(position() + len);
473
}
474
475
bool DumpWriter::can_write_fast(size_t len) {
476
return buffer_size() - position() >= len;
477
}
478
479
// write raw bytes
480
void DumpWriter::write_raw(void* s, size_t len) {
481
assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
482
debug_only(_sub_record_left -= len);
483
484
// flush buffer to make room.
485
while (len > buffer_size() - position()) {
486
assert(!_in_dump_segment || _is_huge_sub_record,
487
"Cannot overflow in non-huge sub-record.");
488
489
size_t to_write = buffer_size() - position();
490
memcpy(buffer() + position(), s, to_write);
491
s = (void*) ((char*) s + to_write);
492
len -= to_write;
493
set_position(position() + to_write);
494
flush();
495
}
496
497
memcpy(buffer() + position(), s, len);
498
set_position(position() + len);
499
}
500
501
// flush any buffered bytes to the file
502
void DumpWriter::flush() {
503
_backend.get_new_buffer(&_buffer, &_pos, &_size);
504
}
505
506
// Makes sure we inline the fast write into the write_u* functions. This is a big speedup.
507
#define WRITE_KNOWN_TYPE(p, len) do { if (can_write_fast((len))) write_fast((p), (len)); \
508
else write_raw((p), (len)); } while (0)
509
510
void DumpWriter::write_u1(u1 x) {
511
WRITE_KNOWN_TYPE((void*) &x, 1);
512
}
513
514
void DumpWriter::write_u2(u2 x) {
515
u2 v;
516
Bytes::put_Java_u2((address)&v, x);
517
WRITE_KNOWN_TYPE((void*)&v, 2);
518
}
519
520
void DumpWriter::write_u4(u4 x) {
521
u4 v;
522
Bytes::put_Java_u4((address)&v, x);
523
WRITE_KNOWN_TYPE((void*)&v, 4);
524
}
525
526
void DumpWriter::write_u8(u8 x) {
527
u8 v;
528
Bytes::put_Java_u8((address)&v, x);
529
WRITE_KNOWN_TYPE((void*)&v, 8);
530
}
531
532
void DumpWriter::write_objectID(oop o) {
533
address a = cast_from_oop<address>(o);
534
#ifdef _LP64
535
write_u8((u8)a);
536
#else
537
write_u4((u4)a);
538
#endif
539
}
540
541
void DumpWriter::write_symbolID(Symbol* s) {
542
address a = (address)((uintptr_t)s);
543
#ifdef _LP64
544
write_u8((u8)a);
545
#else
546
write_u4((u4)a);
547
#endif
548
}
549
550
void DumpWriter::write_id(u4 x) {
551
#ifdef _LP64
552
write_u8((u8) x);
553
#else
554
write_u4(x);
555
#endif
556
}
557
558
// We use java mirror as the class ID
559
void DumpWriter::write_classID(Klass* k) {
560
write_objectID(k->java_mirror());
561
}
562
563
void DumpWriter::finish_dump_segment() {
564
if (_in_dump_segment) {
565
assert(_sub_record_left == 0, "Last sub-record not written completely");
566
assert(_sub_record_ended, "sub-record must have ended");
567
568
// Fix up the dump segment length if we haven't written a huge sub-record last
569
// (in which case the segment length was already set to the correct value initially).
570
if (!_is_huge_sub_record) {
571
assert(position() > dump_segment_header_size, "Dump segment should have some content");
572
Bytes::put_Java_u4((address) (buffer() + 5),
573
(u4) (position() - dump_segment_header_size));
574
}
575
576
flush();
577
_in_dump_segment = false;
578
}
579
}
580
581
void DumpWriter::start_sub_record(u1 tag, u4 len) {
582
if (!_in_dump_segment) {
583
if (position() > 0) {
584
flush();
585
}
586
587
assert(position() == 0, "Must be at the start");
588
589
write_u1(HPROF_HEAP_DUMP_SEGMENT);
590
write_u4(0); // timestamp
591
// Will be fixed up later if we add more sub-records. If this is a huge sub-record,
592
// this is already the correct length, since we don't add more sub-records.
593
write_u4(len);
594
_in_dump_segment = true;
595
_is_huge_sub_record = len > buffer_size() - dump_segment_header_size;
596
} else if (_is_huge_sub_record || (len > buffer_size() - position())) {
597
// This object will not fit in completely or the last sub-record was huge.
598
// Finish the current segement and try again.
599
finish_dump_segment();
600
start_sub_record(tag, len);
601
602
return;
603
}
604
605
debug_only(_sub_record_left = len);
606
debug_only(_sub_record_ended = false);
607
608
write_u1(tag);
609
}
610
611
void DumpWriter::end_sub_record() {
612
assert(_in_dump_segment, "must be in dump segment");
613
assert(_sub_record_left == 0, "sub-record not written completely");
614
assert(!_sub_record_ended, "Must not have ended yet");
615
debug_only(_sub_record_ended = true);
616
}
617
618
// Support class with a collection of functions used when dumping the heap
619
620
class DumperSupport : AllStatic {
621
public:
622
623
// write a header of the given type
624
static void write_header(DumpWriter* writer, hprofTag tag, u4 len);
625
626
// returns hprof tag for the given type signature
627
static hprofTag sig2tag(Symbol* sig);
628
// returns hprof tag for the given basic type
629
static hprofTag type2tag(BasicType type);
630
// Returns the size of the data to write.
631
static u4 sig2size(Symbol* sig);
632
633
// returns the size of the instance of the given class
634
static u4 instance_size(Klass* k);
635
636
// dump a jfloat
637
static void dump_float(DumpWriter* writer, jfloat f);
638
// dump a jdouble
639
static void dump_double(DumpWriter* writer, jdouble d);
640
// dumps the raw value of the given field
641
static void dump_field_value(DumpWriter* writer, char type, oop obj, int offset);
642
// returns the size of the static fields; also counts the static fields
643
static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
644
// dumps static fields of the given class
645
static void dump_static_fields(DumpWriter* writer, Klass* k);
646
// dump the raw values of the instance fields of the given object
647
static void dump_instance_fields(DumpWriter* writer, oop o);
648
// get the count of the instance fields for a given class
649
static u2 get_instance_fields_count(InstanceKlass* ik);
650
// dumps the definition of the instance fields for a given class
651
static void dump_instance_field_descriptors(DumpWriter* writer, Klass* k);
652
// creates HPROF_GC_INSTANCE_DUMP record for the given object
653
static void dump_instance(DumpWriter* writer, oop o);
654
// creates HPROF_GC_CLASS_DUMP record for the given class and each of its
655
// array classes
656
static void dump_class_and_array_classes(DumpWriter* writer, Klass* k);
657
// creates HPROF_GC_CLASS_DUMP record for a given primitive array
658
// class (and each multi-dimensional array class too)
659
static void dump_basic_type_array_class(DumpWriter* writer, Klass* k);
660
661
// creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
662
static void dump_object_array(DumpWriter* writer, objArrayOop array);
663
// creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
664
static void dump_prim_array(DumpWriter* writer, typeArrayOop array);
665
// create HPROF_FRAME record for the given method and bci
666
static void dump_stack_frame(DumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
667
668
// check if we need to truncate an array
669
static int calculate_array_max_length(DumpWriter* writer, arrayOop array, short header_size);
670
671
// fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
672
static void end_of_dump(DumpWriter* writer);
673
674
static oop mask_dormant_archived_object(oop o) {
675
if (o != NULL && o->klass()->java_mirror() == NULL) {
676
// Ignore this object since the corresponding java mirror is not loaded.
677
// Might be a dormant archive object.
678
return NULL;
679
} else {
680
return o;
681
}
682
}
683
};
684
685
// write a header of the given type
686
void DumperSupport:: write_header(DumpWriter* writer, hprofTag tag, u4 len) {
687
writer->write_u1((u1)tag);
688
writer->write_u4(0); // current ticks
689
writer->write_u4(len);
690
}
691
692
// returns hprof tag for the given type signature
693
hprofTag DumperSupport::sig2tag(Symbol* sig) {
694
switch (sig->char_at(0)) {
695
case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
696
case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
697
case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
698
case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
699
case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
700
case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
701
case JVM_SIGNATURE_INT : return HPROF_INT;
702
case JVM_SIGNATURE_LONG : return HPROF_LONG;
703
case JVM_SIGNATURE_SHORT : return HPROF_SHORT;
704
case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN;
705
default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
706
}
707
}
708
709
hprofTag DumperSupport::type2tag(BasicType type) {
710
switch (type) {
711
case T_BYTE : return HPROF_BYTE;
712
case T_CHAR : return HPROF_CHAR;
713
case T_FLOAT : return HPROF_FLOAT;
714
case T_DOUBLE : return HPROF_DOUBLE;
715
case T_INT : return HPROF_INT;
716
case T_LONG : return HPROF_LONG;
717
case T_SHORT : return HPROF_SHORT;
718
case T_BOOLEAN : return HPROF_BOOLEAN;
719
default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
720
}
721
}
722
723
u4 DumperSupport::sig2size(Symbol* sig) {
724
switch (sig->char_at(0)) {
725
case JVM_SIGNATURE_CLASS:
726
case JVM_SIGNATURE_ARRAY: return sizeof(address);
727
case JVM_SIGNATURE_BOOLEAN:
728
case JVM_SIGNATURE_BYTE: return 1;
729
case JVM_SIGNATURE_SHORT:
730
case JVM_SIGNATURE_CHAR: return 2;
731
case JVM_SIGNATURE_INT:
732
case JVM_SIGNATURE_FLOAT: return 4;
733
case JVM_SIGNATURE_LONG:
734
case JVM_SIGNATURE_DOUBLE: return 8;
735
default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
736
}
737
}
738
739
// dump a jfloat
740
void DumperSupport::dump_float(DumpWriter* writer, jfloat f) {
741
if (g_isnan(f)) {
742
writer->write_u4(0x7fc00000); // collapsing NaNs
743
} else {
744
union {
745
int i;
746
float f;
747
} u;
748
u.f = (float)f;
749
writer->write_u4((u4)u.i);
750
}
751
}
752
753
// dump a jdouble
754
void DumperSupport::dump_double(DumpWriter* writer, jdouble d) {
755
union {
756
jlong l;
757
double d;
758
} u;
759
if (g_isnan(d)) { // collapsing NaNs
760
u.l = (jlong)(0x7ff80000);
761
u.l = (u.l << 32);
762
} else {
763
u.d = (double)d;
764
}
765
writer->write_u8((u8)u.l);
766
}
767
768
// dumps the raw value of the given field
769
void DumperSupport::dump_field_value(DumpWriter* writer, char type, oop obj, int offset) {
770
switch (type) {
771
case JVM_SIGNATURE_CLASS :
772
case JVM_SIGNATURE_ARRAY : {
773
oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
774
if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
775
ResourceMark rm;
776
log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
777
p2i(o), o->klass()->external_name(),
778
p2i(obj), obj->klass()->external_name());
779
}
780
o = mask_dormant_archived_object(o);
781
assert(oopDesc::is_oop_or_null(o), "Expected an oop or NULL at " PTR_FORMAT, p2i(o));
782
writer->write_objectID(o);
783
break;
784
}
785
case JVM_SIGNATURE_BYTE : {
786
jbyte b = obj->byte_field(offset);
787
writer->write_u1((u1)b);
788
break;
789
}
790
case JVM_SIGNATURE_CHAR : {
791
jchar c = obj->char_field(offset);
792
writer->write_u2((u2)c);
793
break;
794
}
795
case JVM_SIGNATURE_SHORT : {
796
jshort s = obj->short_field(offset);
797
writer->write_u2((u2)s);
798
break;
799
}
800
case JVM_SIGNATURE_FLOAT : {
801
jfloat f = obj->float_field(offset);
802
dump_float(writer, f);
803
break;
804
}
805
case JVM_SIGNATURE_DOUBLE : {
806
jdouble d = obj->double_field(offset);
807
dump_double(writer, d);
808
break;
809
}
810
case JVM_SIGNATURE_INT : {
811
jint i = obj->int_field(offset);
812
writer->write_u4((u4)i);
813
break;
814
}
815
case JVM_SIGNATURE_LONG : {
816
jlong l = obj->long_field(offset);
817
writer->write_u8((u8)l);
818
break;
819
}
820
case JVM_SIGNATURE_BOOLEAN : {
821
jboolean b = obj->bool_field(offset);
822
writer->write_u1((u1)b);
823
break;
824
}
825
default : {
826
ShouldNotReachHere();
827
break;
828
}
829
}
830
}
831
832
// returns the size of the instance of the given class
833
u4 DumperSupport::instance_size(Klass* k) {
834
InstanceKlass* ik = InstanceKlass::cast(k);
835
u4 size = 0;
836
837
for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
838
if (!fld.access_flags().is_static()) {
839
size += sig2size(fld.signature());
840
}
841
}
842
return size;
843
}
844
845
u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
846
field_count = 0;
847
u4 size = 0;
848
849
for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
850
if (fldc.access_flags().is_static()) {
851
field_count++;
852
size += sig2size(fldc.signature());
853
}
854
}
855
856
// Add in resolved_references which is referenced by the cpCache
857
// The resolved_references is an array per InstanceKlass holding the
858
// strings and other oops resolved from the constant pool.
859
oop resolved_references = ik->constants()->resolved_references_or_null();
860
if (resolved_references != NULL) {
861
field_count++;
862
size += sizeof(address);
863
864
// Add in the resolved_references of the used previous versions of the class
865
// in the case of RedefineClasses
866
InstanceKlass* prev = ik->previous_versions();
867
while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
868
field_count++;
869
size += sizeof(address);
870
prev = prev->previous_versions();
871
}
872
}
873
874
// Also provide a pointer to the init_lock if present, so there aren't unreferenced int[0]
875
// arrays.
876
oop init_lock = ik->init_lock();
877
if (init_lock != NULL) {
878
field_count++;
879
size += sizeof(address);
880
}
881
882
// We write the value itself plus a name and a one byte type tag per field.
883
return size + field_count * (sizeof(address) + 1);
884
}
885
886
// dumps static fields of the given class
887
void DumperSupport::dump_static_fields(DumpWriter* writer, Klass* k) {
888
InstanceKlass* ik = InstanceKlass::cast(k);
889
890
// dump the field descriptors and raw values
891
for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
892
if (fld.access_flags().is_static()) {
893
Symbol* sig = fld.signature();
894
895
writer->write_symbolID(fld.name()); // name
896
writer->write_u1(sig2tag(sig)); // type
897
898
// value
899
dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
900
}
901
}
902
903
// Add resolved_references for each class that has them
904
oop resolved_references = ik->constants()->resolved_references_or_null();
905
if (resolved_references != NULL) {
906
writer->write_symbolID(vmSymbols::resolved_references_name()); // name
907
writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
908
writer->write_objectID(resolved_references);
909
910
// Also write any previous versions
911
InstanceKlass* prev = ik->previous_versions();
912
while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
913
writer->write_symbolID(vmSymbols::resolved_references_name()); // name
914
writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
915
writer->write_objectID(prev->constants()->resolved_references());
916
prev = prev->previous_versions();
917
}
918
}
919
920
// Add init lock to the end if the class is not yet initialized
921
oop init_lock = ik->init_lock();
922
if (init_lock != NULL) {
923
writer->write_symbolID(vmSymbols::init_lock_name()); // name
924
writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
925
writer->write_objectID(init_lock);
926
}
927
}
928
929
// dump the raw values of the instance fields of the given object
930
void DumperSupport::dump_instance_fields(DumpWriter* writer, oop o) {
931
InstanceKlass* ik = InstanceKlass::cast(o->klass());
932
933
for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
934
if (!fld.access_flags().is_static()) {
935
Symbol* sig = fld.signature();
936
dump_field_value(writer, sig->char_at(0), o, fld.offset());
937
}
938
}
939
}
940
941
// dumps the definition of the instance fields for a given class
942
u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
943
u2 field_count = 0;
944
945
for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
946
if (!fldc.access_flags().is_static()) field_count++;
947
}
948
949
return field_count;
950
}
951
952
// dumps the definition of the instance fields for a given class
953
void DumperSupport::dump_instance_field_descriptors(DumpWriter* writer, Klass* k) {
954
InstanceKlass* ik = InstanceKlass::cast(k);
955
956
// dump the field descriptors
957
for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
958
if (!fld.access_flags().is_static()) {
959
Symbol* sig = fld.signature();
960
961
writer->write_symbolID(fld.name()); // name
962
writer->write_u1(sig2tag(sig)); // type
963
}
964
}
965
}
966
967
// creates HPROF_GC_INSTANCE_DUMP record for the given object
968
void DumperSupport::dump_instance(DumpWriter* writer, oop o) {
969
InstanceKlass* ik = InstanceKlass::cast(o->klass());
970
u4 is = instance_size(ik);
971
u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
972
973
writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
974
writer->write_objectID(o);
975
writer->write_u4(STACK_TRACE_ID);
976
977
// class ID
978
writer->write_classID(ik);
979
980
// number of bytes that follow
981
writer->write_u4(is);
982
983
// field values
984
dump_instance_fields(writer, o);
985
986
writer->end_sub_record();
987
}
988
989
// creates HPROF_GC_CLASS_DUMP record for the given class and each of
990
// its array classes
991
void DumperSupport::dump_class_and_array_classes(DumpWriter* writer, Klass* k) {
992
InstanceKlass* ik = InstanceKlass::cast(k);
993
994
// We can safepoint and do a heap dump at a point where we have a Klass,
995
// but no java mirror class has been setup for it. So we need to check
996
// that the class is at least loaded, to avoid crash from a null mirror.
997
if (!ik->is_loaded()) {
998
return;
999
}
1000
1001
u2 static_fields_count = 0;
1002
u4 static_size = get_static_fields_size(ik, static_fields_count);
1003
u2 instance_fields_count = get_instance_fields_count(ik);
1004
u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1005
u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size;
1006
1007
writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1008
1009
// class ID
1010
writer->write_classID(ik);
1011
writer->write_u4(STACK_TRACE_ID);
1012
1013
// super class ID
1014
InstanceKlass* java_super = ik->java_super();
1015
if (java_super == NULL) {
1016
writer->write_objectID(oop(NULL));
1017
} else {
1018
writer->write_classID(java_super);
1019
}
1020
1021
writer->write_objectID(ik->class_loader());
1022
writer->write_objectID(ik->signers());
1023
writer->write_objectID(ik->protection_domain());
1024
1025
// reserved
1026
writer->write_objectID(oop(NULL));
1027
writer->write_objectID(oop(NULL));
1028
1029
// instance size
1030
writer->write_u4(DumperSupport::instance_size(ik));
1031
1032
// size of constant pool - ignored by HAT 1.1
1033
writer->write_u2(0);
1034
1035
// static fields
1036
writer->write_u2(static_fields_count);
1037
dump_static_fields(writer, ik);
1038
1039
// description of instance fields
1040
writer->write_u2(instance_fields_count);
1041
dump_instance_field_descriptors(writer, ik);
1042
1043
writer->end_sub_record();
1044
1045
// array classes
1046
k = ik->array_klass_or_null();
1047
while (k != NULL) {
1048
assert(k->is_objArray_klass(), "not an ObjArrayKlass");
1049
1050
u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1051
writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1052
writer->write_classID(k);
1053
writer->write_u4(STACK_TRACE_ID);
1054
1055
// super class of array classes is java.lang.Object
1056
java_super = k->java_super();
1057
assert(java_super != NULL, "checking");
1058
writer->write_classID(java_super);
1059
1060
writer->write_objectID(ik->class_loader());
1061
writer->write_objectID(ik->signers());
1062
writer->write_objectID(ik->protection_domain());
1063
1064
writer->write_objectID(oop(NULL)); // reserved
1065
writer->write_objectID(oop(NULL));
1066
writer->write_u4(0); // instance size
1067
writer->write_u2(0); // constant pool
1068
writer->write_u2(0); // static fields
1069
writer->write_u2(0); // instance fields
1070
1071
writer->end_sub_record();
1072
1073
// get the array class for the next rank
1074
k = k->array_klass_or_null();
1075
}
1076
}
1077
1078
// creates HPROF_GC_CLASS_DUMP record for a given primitive array
1079
// class (and each multi-dimensional array class too)
1080
void DumperSupport::dump_basic_type_array_class(DumpWriter* writer, Klass* k) {
1081
// array classes
1082
while (k != NULL) {
1083
Klass* klass = k;
1084
1085
u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1086
writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1087
writer->write_classID(klass);
1088
writer->write_u4(STACK_TRACE_ID);
1089
1090
// super class of array classes is java.lang.Object
1091
InstanceKlass* java_super = klass->java_super();
1092
assert(java_super != NULL, "checking");
1093
writer->write_classID(java_super);
1094
1095
writer->write_objectID(oop(NULL)); // loader
1096
writer->write_objectID(oop(NULL)); // signers
1097
writer->write_objectID(oop(NULL)); // protection domain
1098
1099
writer->write_objectID(oop(NULL)); // reserved
1100
writer->write_objectID(oop(NULL));
1101
writer->write_u4(0); // instance size
1102
writer->write_u2(0); // constant pool
1103
writer->write_u2(0); // static fields
1104
writer->write_u2(0); // instance fields
1105
1106
writer->end_sub_record();
1107
1108
// get the array class for the next rank
1109
k = klass->array_klass_or_null();
1110
}
1111
}
1112
1113
// Hprof uses an u4 as record length field,
1114
// which means we need to truncate arrays that are too long.
1115
int DumperSupport::calculate_array_max_length(DumpWriter* writer, arrayOop array, short header_size) {
1116
BasicType type = ArrayKlass::cast(array->klass())->element_type();
1117
assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1118
1119
int length = array->length();
1120
1121
int type_size;
1122
if (type == T_OBJECT) {
1123
type_size = sizeof(address);
1124
} else {
1125
type_size = type2aelembytes(type);
1126
}
1127
1128
size_t length_in_bytes = (size_t)length * type_size;
1129
uint max_bytes = max_juint - header_size;
1130
1131
if (length_in_bytes > max_bytes) {
1132
length = max_bytes / type_size;
1133
length_in_bytes = (size_t)length * type_size;
1134
1135
warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1136
type2name_tab[type], array->length(), length);
1137
}
1138
return length;
1139
}
1140
1141
// creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1142
void DumperSupport::dump_object_array(DumpWriter* writer, objArrayOop array) {
1143
// sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1144
short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1145
int length = calculate_array_max_length(writer, array, header_size);
1146
u4 size = header_size + length * sizeof(address);
1147
1148
writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1149
writer->write_objectID(array);
1150
writer->write_u4(STACK_TRACE_ID);
1151
writer->write_u4(length);
1152
1153
// array class ID
1154
writer->write_classID(array->klass());
1155
1156
// [id]* elements
1157
for (int index = 0; index < length; index++) {
1158
oop o = array->obj_at(index);
1159
if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
1160
ResourceMark rm;
1161
log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1162
p2i(o), o->klass()->external_name(),
1163
p2i(array), array->klass()->external_name());
1164
}
1165
o = mask_dormant_archived_object(o);
1166
writer->write_objectID(o);
1167
}
1168
1169
writer->end_sub_record();
1170
}
1171
1172
#define WRITE_ARRAY(Array, Type, Size, Length) \
1173
for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1174
1175
// creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1176
void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) {
1177
BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1178
1179
// 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1180
short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1181
1182
int length = calculate_array_max_length(writer, array, header_size);
1183
int type_size = type2aelembytes(type);
1184
u4 length_in_bytes = (u4)length * type_size;
1185
u4 size = header_size + length_in_bytes;
1186
1187
writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1188
writer->write_objectID(array);
1189
writer->write_u4(STACK_TRACE_ID);
1190
writer->write_u4(length);
1191
writer->write_u1(type2tag(type));
1192
1193
// nothing to copy
1194
if (length == 0) {
1195
writer->end_sub_record();
1196
return;
1197
}
1198
1199
// If the byte ordering is big endian then we can copy most types directly
1200
1201
switch (type) {
1202
case T_INT : {
1203
if (Endian::is_Java_byte_ordering_different()) {
1204
WRITE_ARRAY(array, int, u4, length);
1205
} else {
1206
writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
1207
}
1208
break;
1209
}
1210
case T_BYTE : {
1211
writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
1212
break;
1213
}
1214
case T_CHAR : {
1215
if (Endian::is_Java_byte_ordering_different()) {
1216
WRITE_ARRAY(array, char, u2, length);
1217
} else {
1218
writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
1219
}
1220
break;
1221
}
1222
case T_SHORT : {
1223
if (Endian::is_Java_byte_ordering_different()) {
1224
WRITE_ARRAY(array, short, u2, length);
1225
} else {
1226
writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
1227
}
1228
break;
1229
}
1230
case T_BOOLEAN : {
1231
if (Endian::is_Java_byte_ordering_different()) {
1232
WRITE_ARRAY(array, bool, u1, length);
1233
} else {
1234
writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
1235
}
1236
break;
1237
}
1238
case T_LONG : {
1239
if (Endian::is_Java_byte_ordering_different()) {
1240
WRITE_ARRAY(array, long, u8, length);
1241
} else {
1242
writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
1243
}
1244
break;
1245
}
1246
1247
// handle float/doubles in a special value to ensure than NaNs are
1248
// written correctly. TO DO: Check if we can avoid this on processors that
1249
// use IEEE 754.
1250
1251
case T_FLOAT : {
1252
for (int i = 0; i < length; i++) {
1253
dump_float(writer, array->float_at(i));
1254
}
1255
break;
1256
}
1257
case T_DOUBLE : {
1258
for (int i = 0; i < length; i++) {
1259
dump_double(writer, array->double_at(i));
1260
}
1261
break;
1262
}
1263
default : ShouldNotReachHere();
1264
}
1265
1266
writer->end_sub_record();
1267
}
1268
1269
// create a HPROF_FRAME record of the given Method* and bci
1270
void DumperSupport::dump_stack_frame(DumpWriter* writer,
1271
int frame_serial_num,
1272
int class_serial_num,
1273
Method* m,
1274
int bci) {
1275
int line_number;
1276
if (m->is_native()) {
1277
line_number = -3; // native frame
1278
} else {
1279
line_number = m->line_number_from_bci(bci);
1280
}
1281
1282
write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1283
writer->write_id(frame_serial_num); // frame serial number
1284
writer->write_symbolID(m->name()); // method's name
1285
writer->write_symbolID(m->signature()); // method's signature
1286
1287
assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1288
writer->write_symbolID(m->method_holder()->source_file_name()); // source file name
1289
writer->write_u4(class_serial_num); // class serial number
1290
writer->write_u4((u4) line_number); // line number
1291
}
1292
1293
1294
// Support class used to generate HPROF_UTF8 records from the entries in the
1295
// SymbolTable.
1296
1297
class SymbolTableDumper : public SymbolClosure {
1298
private:
1299
DumpWriter* _writer;
1300
DumpWriter* writer() const { return _writer; }
1301
public:
1302
SymbolTableDumper(DumpWriter* writer) { _writer = writer; }
1303
void do_symbol(Symbol** p);
1304
};
1305
1306
void SymbolTableDumper::do_symbol(Symbol** p) {
1307
ResourceMark rm;
1308
Symbol* sym = load_symbol(p);
1309
int len = sym->utf8_length();
1310
if (len > 0) {
1311
char* s = sym->as_utf8();
1312
DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1313
writer()->write_symbolID(sym);
1314
writer()->write_raw(s, len);
1315
}
1316
}
1317
1318
// Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
1319
1320
class JNILocalsDumper : public OopClosure {
1321
private:
1322
DumpWriter* _writer;
1323
u4 _thread_serial_num;
1324
int _frame_num;
1325
DumpWriter* writer() const { return _writer; }
1326
public:
1327
JNILocalsDumper(DumpWriter* writer, u4 thread_serial_num) {
1328
_writer = writer;
1329
_thread_serial_num = thread_serial_num;
1330
_frame_num = -1; // default - empty stack
1331
}
1332
void set_frame_number(int n) { _frame_num = n; }
1333
void do_oop(oop* obj_p);
1334
void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1335
};
1336
1337
1338
void JNILocalsDumper::do_oop(oop* obj_p) {
1339
// ignore null handles
1340
oop o = *obj_p;
1341
if (o != NULL) {
1342
u4 size = 1 + sizeof(address) + 4 + 4;
1343
writer()->start_sub_record(HPROF_GC_ROOT_JNI_LOCAL, size);
1344
writer()->write_objectID(o);
1345
writer()->write_u4(_thread_serial_num);
1346
writer()->write_u4((u4)_frame_num);
1347
writer()->end_sub_record();
1348
}
1349
}
1350
1351
1352
// Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
1353
1354
class JNIGlobalsDumper : public OopClosure {
1355
private:
1356
DumpWriter* _writer;
1357
DumpWriter* writer() const { return _writer; }
1358
1359
public:
1360
JNIGlobalsDumper(DumpWriter* writer) {
1361
_writer = writer;
1362
}
1363
void do_oop(oop* obj_p);
1364
void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1365
};
1366
1367
void JNIGlobalsDumper::do_oop(oop* obj_p) {
1368
oop o = *obj_p;
1369
1370
// ignore these
1371
if (o == NULL) return;
1372
1373
// we ignore global ref to symbols and other internal objects
1374
if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
1375
u4 size = 1 + 2 * sizeof(address);
1376
writer()->start_sub_record(HPROF_GC_ROOT_JNI_GLOBAL, size);
1377
writer()->write_objectID(o);
1378
writer()->write_objectID((oopDesc*)obj_p); // global ref ID
1379
writer()->end_sub_record();
1380
}
1381
};
1382
1383
// Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
1384
1385
class StickyClassDumper : public KlassClosure {
1386
private:
1387
DumpWriter* _writer;
1388
DumpWriter* writer() const { return _writer; }
1389
public:
1390
StickyClassDumper(DumpWriter* writer) {
1391
_writer = writer;
1392
}
1393
void do_klass(Klass* k) {
1394
if (k->is_instance_klass()) {
1395
InstanceKlass* ik = InstanceKlass::cast(k);
1396
u4 size = 1 + sizeof(address);
1397
writer()->start_sub_record(HPROF_GC_ROOT_STICKY_CLASS, size);
1398
writer()->write_classID(ik);
1399
writer()->end_sub_record();
1400
}
1401
}
1402
};
1403
1404
1405
class VM_HeapDumper;
1406
1407
// Support class using when iterating over the heap.
1408
1409
class HeapObjectDumper : public ObjectClosure {
1410
private:
1411
DumpWriter* _writer;
1412
1413
DumpWriter* writer() { return _writer; }
1414
1415
public:
1416
HeapObjectDumper(DumpWriter* writer) {
1417
_writer = writer;
1418
}
1419
1420
// called for each object in the heap
1421
void do_object(oop o);
1422
};
1423
1424
void HeapObjectDumper::do_object(oop o) {
1425
// skip classes as these emitted as HPROF_GC_CLASS_DUMP records
1426
if (o->klass() == vmClasses::Class_klass()) {
1427
if (!java_lang_Class::is_primitive(o)) {
1428
return;
1429
}
1430
}
1431
1432
if (DumperSupport::mask_dormant_archived_object(o) == NULL) {
1433
log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o), o->klass()->external_name());
1434
return;
1435
}
1436
1437
if (o->is_instance()) {
1438
// create a HPROF_GC_INSTANCE record for each object
1439
DumperSupport::dump_instance(writer(), o);
1440
} else if (o->is_objArray()) {
1441
// create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1442
DumperSupport::dump_object_array(writer(), objArrayOop(o));
1443
} else if (o->is_typeArray()) {
1444
// create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1445
DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1446
}
1447
}
1448
1449
// The VM operation that performs the heap dump
1450
class VM_HeapDumper : public VM_GC_Operation, public AbstractGangTask {
1451
private:
1452
static VM_HeapDumper* _global_dumper;
1453
static DumpWriter* _global_writer;
1454
DumpWriter* _local_writer;
1455
JavaThread* _oome_thread;
1456
Method* _oome_constructor;
1457
bool _gc_before_heap_dump;
1458
GrowableArray<Klass*>* _klass_map;
1459
ThreadStackTrace** _stack_traces;
1460
int _num_threads;
1461
1462
// accessors and setters
1463
static VM_HeapDumper* dumper() { assert(_global_dumper != NULL, "Error"); return _global_dumper; }
1464
static DumpWriter* writer() { assert(_global_writer != NULL, "Error"); return _global_writer; }
1465
void set_global_dumper() {
1466
assert(_global_dumper == NULL, "Error");
1467
_global_dumper = this;
1468
}
1469
void set_global_writer() {
1470
assert(_global_writer == NULL, "Error");
1471
_global_writer = _local_writer;
1472
}
1473
void clear_global_dumper() { _global_dumper = NULL; }
1474
void clear_global_writer() { _global_writer = NULL; }
1475
1476
bool skip_operation() const;
1477
1478
// writes a HPROF_LOAD_CLASS record
1479
class ClassesDo;
1480
static void do_load_class(Klass* k);
1481
1482
// writes a HPROF_GC_CLASS_DUMP record for the given class
1483
// (and each array class too)
1484
static void do_class_dump(Klass* k);
1485
1486
// writes a HPROF_GC_CLASS_DUMP records for a given basic type
1487
// array (and each multi-dimensional array too)
1488
static void do_basic_type_array_class_dump(Klass* k);
1489
1490
// HPROF_GC_ROOT_THREAD_OBJ records
1491
int do_thread(JavaThread* thread, u4 thread_serial_num);
1492
void do_threads();
1493
1494
void add_class_serial_number(Klass* k, int serial_num) {
1495
_klass_map->at_put_grow(serial_num, k);
1496
}
1497
1498
// HPROF_TRACE and HPROF_FRAME records
1499
void dump_stack_traces();
1500
1501
public:
1502
VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome) :
1503
VM_GC_Operation(0 /* total collections, dummy, ignored */,
1504
GCCause::_heap_dump /* GC Cause */,
1505
0 /* total full collections, dummy, ignored */,
1506
gc_before_heap_dump),
1507
AbstractGangTask("dump heap") {
1508
_local_writer = writer;
1509
_gc_before_heap_dump = gc_before_heap_dump;
1510
_klass_map = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, mtServiceability);
1511
_stack_traces = NULL;
1512
_num_threads = 0;
1513
if (oome) {
1514
assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
1515
// get OutOfMemoryError zero-parameter constructor
1516
InstanceKlass* oome_ik = vmClasses::OutOfMemoryError_klass();
1517
_oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
1518
vmSymbols::void_method_signature());
1519
// get thread throwing OOME when generating the heap dump at OOME
1520
_oome_thread = JavaThread::current();
1521
} else {
1522
_oome_thread = NULL;
1523
_oome_constructor = NULL;
1524
}
1525
}
1526
~VM_HeapDumper() {
1527
if (_stack_traces != NULL) {
1528
for (int i=0; i < _num_threads; i++) {
1529
delete _stack_traces[i];
1530
}
1531
FREE_C_HEAP_ARRAY(ThreadStackTrace*, _stack_traces);
1532
}
1533
delete _klass_map;
1534
}
1535
1536
VMOp_Type type() const { return VMOp_HeapDumper; }
1537
void doit();
1538
void work(uint worker_id);
1539
};
1540
1541
1542
VM_HeapDumper* VM_HeapDumper::_global_dumper = NULL;
1543
DumpWriter* VM_HeapDumper::_global_writer = NULL;
1544
1545
bool VM_HeapDumper::skip_operation() const {
1546
return false;
1547
}
1548
1549
// fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
1550
void DumperSupport::end_of_dump(DumpWriter* writer) {
1551
writer->finish_dump_segment();
1552
1553
writer->write_u1(HPROF_HEAP_DUMP_END);
1554
writer->write_u4(0);
1555
writer->write_u4(0);
1556
}
1557
1558
// writes a HPROF_LOAD_CLASS record for the class (and each of its
1559
// array classes)
1560
void VM_HeapDumper::do_load_class(Klass* k) {
1561
static u4 class_serial_num = 0;
1562
1563
// len of HPROF_LOAD_CLASS record
1564
u4 remaining = 2*oopSize + 2*sizeof(u4);
1565
1566
// write a HPROF_LOAD_CLASS for the class and each array class
1567
do {
1568
DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
1569
1570
// class serial number is just a number
1571
writer()->write_u4(++class_serial_num);
1572
1573
// class ID
1574
Klass* klass = k;
1575
writer()->write_classID(klass);
1576
1577
// add the Klass* and class serial number pair
1578
dumper()->add_class_serial_number(klass, class_serial_num);
1579
1580
writer()->write_u4(STACK_TRACE_ID);
1581
1582
// class name ID
1583
Symbol* name = klass->name();
1584
writer()->write_symbolID(name);
1585
1586
// write a LOAD_CLASS record for the array type (if it exists)
1587
k = klass->array_klass_or_null();
1588
} while (k != NULL);
1589
}
1590
1591
// writes a HPROF_GC_CLASS_DUMP record for the given class
1592
void VM_HeapDumper::do_class_dump(Klass* k) {
1593
if (k->is_instance_klass()) {
1594
DumperSupport::dump_class_and_array_classes(writer(), k);
1595
}
1596
}
1597
1598
// writes a HPROF_GC_CLASS_DUMP records for a given basic type
1599
// array (and each multi-dimensional array too)
1600
void VM_HeapDumper::do_basic_type_array_class_dump(Klass* k) {
1601
DumperSupport::dump_basic_type_array_class(writer(), k);
1602
}
1603
1604
// Walk the stack of the given thread.
1605
// Dumps a HPROF_GC_ROOT_JAVA_FRAME record for each local
1606
// Dumps a HPROF_GC_ROOT_JNI_LOCAL record for each JNI local
1607
//
1608
// It returns the number of Java frames in this thread stack
1609
int VM_HeapDumper::do_thread(JavaThread* java_thread, u4 thread_serial_num) {
1610
JNILocalsDumper blk(writer(), thread_serial_num);
1611
1612
oop threadObj = java_thread->threadObj();
1613
assert(threadObj != NULL, "sanity check");
1614
1615
int stack_depth = 0;
1616
if (java_thread->has_last_Java_frame()) {
1617
1618
// vframes are resource allocated
1619
Thread* current_thread = Thread::current();
1620
ResourceMark rm(current_thread);
1621
HandleMark hm(current_thread);
1622
1623
RegisterMap reg_map(java_thread);
1624
frame f = java_thread->last_frame();
1625
vframe* vf = vframe::new_vframe(&f, &reg_map, java_thread);
1626
frame* last_entry_frame = NULL;
1627
int extra_frames = 0;
1628
1629
if (java_thread == _oome_thread && _oome_constructor != NULL) {
1630
extra_frames++;
1631
}
1632
while (vf != NULL) {
1633
blk.set_frame_number(stack_depth);
1634
if (vf->is_java_frame()) {
1635
1636
// java frame (interpreted, compiled, ...)
1637
javaVFrame *jvf = javaVFrame::cast(vf);
1638
if (!(jvf->method()->is_native())) {
1639
StackValueCollection* locals = jvf->locals();
1640
for (int slot=0; slot<locals->size(); slot++) {
1641
if (locals->at(slot)->type() == T_OBJECT) {
1642
oop o = locals->obj_at(slot)();
1643
1644
if (o != NULL) {
1645
u4 size = 1 + sizeof(address) + 4 + 4;
1646
writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
1647
writer()->write_objectID(o);
1648
writer()->write_u4(thread_serial_num);
1649
writer()->write_u4((u4) (stack_depth + extra_frames));
1650
writer()->end_sub_record();
1651
}
1652
}
1653
}
1654
StackValueCollection *exprs = jvf->expressions();
1655
for(int index = 0; index < exprs->size(); index++) {
1656
if (exprs->at(index)->type() == T_OBJECT) {
1657
oop o = exprs->obj_at(index)();
1658
if (o != NULL) {
1659
u4 size = 1 + sizeof(address) + 4 + 4;
1660
writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
1661
writer()->write_objectID(o);
1662
writer()->write_u4(thread_serial_num);
1663
writer()->write_u4((u4) (stack_depth + extra_frames));
1664
writer()->end_sub_record();
1665
}
1666
}
1667
}
1668
} else {
1669
// native frame
1670
if (stack_depth == 0) {
1671
// JNI locals for the top frame.
1672
java_thread->active_handles()->oops_do(&blk);
1673
} else {
1674
if (last_entry_frame != NULL) {
1675
// JNI locals for the entry frame
1676
assert(last_entry_frame->is_entry_frame(), "checking");
1677
last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk);
1678
}
1679
}
1680
}
1681
// increment only for Java frames
1682
stack_depth++;
1683
last_entry_frame = NULL;
1684
1685
} else {
1686
// externalVFrame - if it's an entry frame then report any JNI locals
1687
// as roots when we find the corresponding native javaVFrame
1688
frame* fr = vf->frame_pointer();
1689
assert(fr != NULL, "sanity check");
1690
if (fr->is_entry_frame()) {
1691
last_entry_frame = fr;
1692
}
1693
}
1694
vf = vf->sender();
1695
}
1696
} else {
1697
// no last java frame but there may be JNI locals
1698
java_thread->active_handles()->oops_do(&blk);
1699
}
1700
return stack_depth;
1701
}
1702
1703
1704
// write a HPROF_GC_ROOT_THREAD_OBJ record for each java thread. Then walk
1705
// the stack so that locals and JNI locals are dumped.
1706
void VM_HeapDumper::do_threads() {
1707
for (int i=0; i < _num_threads; i++) {
1708
JavaThread* thread = _stack_traces[i]->thread();
1709
oop threadObj = thread->threadObj();
1710
u4 thread_serial_num = i+1;
1711
u4 stack_serial_num = thread_serial_num + STACK_TRACE_ID;
1712
u4 size = 1 + sizeof(address) + 4 + 4;
1713
writer()->start_sub_record(HPROF_GC_ROOT_THREAD_OBJ, size);
1714
writer()->write_objectID(threadObj);
1715
writer()->write_u4(thread_serial_num); // thread number
1716
writer()->write_u4(stack_serial_num); // stack trace serial number
1717
writer()->end_sub_record();
1718
int num_frames = do_thread(thread, thread_serial_num);
1719
assert(num_frames == _stack_traces[i]->get_stack_depth(),
1720
"total number of Java frames not matched");
1721
}
1722
}
1723
1724
1725
// The VM operation that dumps the heap. The dump consists of the following
1726
// records:
1727
//
1728
// HPROF_HEADER
1729
// [HPROF_UTF8]*
1730
// [HPROF_LOAD_CLASS]*
1731
// [[HPROF_FRAME]*|HPROF_TRACE]*
1732
// [HPROF_GC_CLASS_DUMP]*
1733
// [HPROF_HEAP_DUMP_SEGMENT]*
1734
// HPROF_HEAP_DUMP_END
1735
//
1736
// The HPROF_TRACE records represent the stack traces where the heap dump
1737
// is generated and a "dummy trace" record which does not include
1738
// any frames. The dummy trace record is used to be referenced as the
1739
// unknown object alloc site.
1740
//
1741
// Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
1742
// To allow the heap dump be generated in a single pass we remember the position
1743
// of the dump length and fix it up after all sub-records have been written.
1744
// To generate the sub-records we iterate over the heap, writing
1745
// HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
1746
// records as we go. Once that is done we write records for some of the GC
1747
// roots.
1748
1749
void VM_HeapDumper::doit() {
1750
1751
CollectedHeap* ch = Universe::heap();
1752
1753
ch->ensure_parsability(false); // must happen, even if collection does
1754
// not happen (e.g. due to GCLocker)
1755
1756
if (_gc_before_heap_dump) {
1757
if (GCLocker::is_active()) {
1758
warning("GC locker is held; pre-heapdump GC was skipped");
1759
} else {
1760
ch->collect_as_vm_thread(GCCause::_heap_dump);
1761
}
1762
}
1763
1764
// At this point we should be the only dumper active, so
1765
// the following should be safe.
1766
set_global_dumper();
1767
set_global_writer();
1768
1769
WorkGang* gang = ch->safepoint_workers();
1770
1771
if (gang == NULL) {
1772
work(0);
1773
} else {
1774
gang->run_task(this, gang->active_workers(), true);
1775
}
1776
1777
// Now we clear the global variables, so that a future dumper can run.
1778
clear_global_dumper();
1779
clear_global_writer();
1780
}
1781
1782
void VM_HeapDumper::work(uint worker_id) {
1783
if (!Thread::current()->is_VM_thread()) {
1784
writer()->writer_loop();
1785
return;
1786
}
1787
1788
// Write the file header - we always use 1.0.2
1789
const char* header = "JAVA PROFILE 1.0.2";
1790
1791
// header is few bytes long - no chance to overflow int
1792
writer()->write_raw((void*)header, (int)strlen(header));
1793
writer()->write_u1(0); // terminator
1794
writer()->write_u4(oopSize);
1795
// timestamp is current time in ms
1796
writer()->write_u8(os::javaTimeMillis());
1797
1798
// HPROF_UTF8 records
1799
SymbolTableDumper sym_dumper(writer());
1800
SymbolTable::symbols_do(&sym_dumper);
1801
1802
// write HPROF_LOAD_CLASS records
1803
{
1804
LockedClassesDo locked_load_classes(&do_load_class);
1805
ClassLoaderDataGraph::classes_do(&locked_load_classes);
1806
}
1807
Universe::basic_type_classes_do(&do_load_class);
1808
1809
// write HPROF_FRAME and HPROF_TRACE records
1810
// this must be called after _klass_map is built when iterating the classes above.
1811
dump_stack_traces();
1812
1813
// Writes HPROF_GC_CLASS_DUMP records
1814
{
1815
LockedClassesDo locked_dump_class(&do_class_dump);
1816
ClassLoaderDataGraph::classes_do(&locked_dump_class);
1817
}
1818
Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
1819
1820
// writes HPROF_GC_INSTANCE_DUMP records.
1821
// After each sub-record is written check_segment_length will be invoked
1822
// to check if the current segment exceeds a threshold. If so, a new
1823
// segment is started.
1824
// The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
1825
// of the heap dump.
1826
HeapObjectDumper obj_dumper(writer());
1827
Universe::heap()->object_iterate(&obj_dumper);
1828
1829
// HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
1830
do_threads();
1831
1832
// HPROF_GC_ROOT_JNI_GLOBAL
1833
JNIGlobalsDumper jni_dumper(writer());
1834
JNIHandles::oops_do(&jni_dumper);
1835
// technically not jni roots, but global roots
1836
// for things like preallocated throwable backtraces
1837
Universe::vm_global()->oops_do(&jni_dumper);
1838
1839
// HPROF_GC_ROOT_STICKY_CLASS
1840
// These should be classes in the NULL class loader data, and not all classes
1841
// if !ClassUnloading
1842
StickyClassDumper class_dumper(writer());
1843
ClassLoaderData::the_null_class_loader_data()->classes_do(&class_dumper);
1844
1845
// Writes the HPROF_HEAP_DUMP_END record.
1846
DumperSupport::end_of_dump(writer());
1847
1848
// We are done with writing. Release the worker threads.
1849
writer()->deactivate();
1850
}
1851
1852
void VM_HeapDumper::dump_stack_traces() {
1853
// write a HPROF_TRACE record without any frames to be referenced as object alloc sites
1854
DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
1855
writer()->write_u4((u4) STACK_TRACE_ID);
1856
writer()->write_u4(0); // thread number
1857
writer()->write_u4(0); // frame count
1858
1859
_stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal);
1860
int frame_serial_num = 0;
1861
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
1862
oop threadObj = thread->threadObj();
1863
if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
1864
// dump thread stack trace
1865
ResourceMark rm;
1866
ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);
1867
stack_trace->dump_stack_at_safepoint(-1);
1868
_stack_traces[_num_threads++] = stack_trace;
1869
1870
// write HPROF_FRAME records for this thread's stack trace
1871
int depth = stack_trace->get_stack_depth();
1872
int thread_frame_start = frame_serial_num;
1873
int extra_frames = 0;
1874
// write fake frame that makes it look like the thread, which caused OOME,
1875
// is in the OutOfMemoryError zero-parameter constructor
1876
if (thread == _oome_thread && _oome_constructor != NULL) {
1877
int oome_serial_num = _klass_map->find(_oome_constructor->method_holder());
1878
// the class serial number starts from 1
1879
assert(oome_serial_num > 0, "OutOfMemoryError class not found");
1880
DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, oome_serial_num,
1881
_oome_constructor, 0);
1882
extra_frames++;
1883
}
1884
for (int j=0; j < depth; j++) {
1885
StackFrameInfo* frame = stack_trace->stack_frame_at(j);
1886
Method* m = frame->method();
1887
int class_serial_num = _klass_map->find(m->method_holder());
1888
// the class serial number starts from 1
1889
assert(class_serial_num > 0, "class not found");
1890
DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, class_serial_num, m, frame->bci());
1891
}
1892
depth += extra_frames;
1893
1894
// write HPROF_TRACE record for one thread
1895
DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4) + depth*oopSize);
1896
int stack_serial_num = _num_threads + STACK_TRACE_ID;
1897
writer()->write_u4(stack_serial_num); // stack trace serial number
1898
writer()->write_u4((u4) _num_threads); // thread serial number
1899
writer()->write_u4(depth); // frame count
1900
for (int j=1; j <= depth; j++) {
1901
writer()->write_id(thread_frame_start + j);
1902
}
1903
}
1904
}
1905
}
1906
1907
// dump the heap to given path.
1908
int HeapDumper::dump(const char* path, outputStream* out, int compression) {
1909
assert(path != NULL && strlen(path) > 0, "path missing");
1910
1911
// print message in interactive case
1912
if (out != NULL) {
1913
out->print_cr("Dumping heap to %s ...", path);
1914
timer()->start();
1915
}
1916
1917
// create JFR event
1918
EventHeapDump event;
1919
1920
AbstractCompressor* compressor = NULL;
1921
1922
if (compression > 0) {
1923
compressor = new (std::nothrow) GZipCompressor(compression);
1924
1925
if (compressor == NULL) {
1926
set_error("Could not allocate gzip compressor");
1927
return -1;
1928
}
1929
}
1930
1931
DumpWriter writer(new (std::nothrow) FileWriter(path), compressor);
1932
1933
if (writer.error() != NULL) {
1934
set_error(writer.error());
1935
if (out != NULL) {
1936
out->print_cr("Unable to create %s: %s", path,
1937
(error() != NULL) ? error() : "reason unknown");
1938
}
1939
return -1;
1940
}
1941
1942
// generate the dump
1943
VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome);
1944
if (Thread::current()->is_VM_thread()) {
1945
assert(SafepointSynchronize::is_at_safepoint(), "Expected to be called at a safepoint");
1946
dumper.doit();
1947
} else {
1948
VMThread::execute(&dumper);
1949
}
1950
1951
// record any error that the writer may have encountered
1952
set_error(writer.error());
1953
1954
// emit JFR event
1955
if (error() == NULL) {
1956
event.set_destination(path);
1957
event.set_gcBeforeDump(_gc_before_heap_dump);
1958
event.set_size(writer.bytes_written());
1959
event.set_onOutOfMemoryError(_oome);
1960
event.commit();
1961
}
1962
1963
// print message in interactive case
1964
if (out != NULL) {
1965
timer()->stop();
1966
if (error() == NULL) {
1967
out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
1968
writer.bytes_written(), timer()->seconds());
1969
} else {
1970
out->print_cr("Dump file is incomplete: %s", writer.error());
1971
}
1972
}
1973
1974
return (writer.error() == NULL) ? 0 : -1;
1975
}
1976
1977
// stop timer (if still active), and free any error string we might be holding
1978
HeapDumper::~HeapDumper() {
1979
if (timer()->is_active()) {
1980
timer()->stop();
1981
}
1982
set_error(NULL);
1983
}
1984
1985
1986
// returns the error string (resource allocated), or NULL
1987
char* HeapDumper::error_as_C_string() const {
1988
if (error() != NULL) {
1989
char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
1990
strcpy(str, error());
1991
return str;
1992
} else {
1993
return NULL;
1994
}
1995
}
1996
1997
// set the error string
1998
void HeapDumper::set_error(char const* error) {
1999
if (_error != NULL) {
2000
os::free(_error);
2001
}
2002
if (error == NULL) {
2003
_error = NULL;
2004
} else {
2005
_error = os::strdup(error);
2006
assert(_error != NULL, "allocation failure");
2007
}
2008
}
2009
2010
// Called by out-of-memory error reporting by a single Java thread
2011
// outside of a JVM safepoint
2012
void HeapDumper::dump_heap_from_oome() {
2013
HeapDumper::dump_heap(true);
2014
}
2015
2016
// Called by error reporting by a single Java thread outside of a JVM safepoint,
2017
// or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various
2018
// callers are strictly serialized and guaranteed not to interfere below. For more
2019
// general use, however, this method will need modification to prevent
2020
// inteference when updating the static variables base_path and dump_file_seq below.
2021
void HeapDumper::dump_heap() {
2022
HeapDumper::dump_heap(false);
2023
}
2024
2025
void HeapDumper::dump_heap(bool oome) {
2026
static char base_path[JVM_MAXPATHLEN] = {'\0'};
2027
static uint dump_file_seq = 0;
2028
char* my_path;
2029
const int max_digit_chars = 20;
2030
2031
const char* dump_file_name = "java_pid";
2032
const char* dump_file_ext = HeapDumpGzipLevel > 0 ? ".hprof.gz" : ".hprof";
2033
2034
// The dump file defaults to java_pid<pid>.hprof in the current working
2035
// directory. HeapDumpPath=<file> can be used to specify an alternative
2036
// dump file name or a directory where dump file is created.
2037
if (dump_file_seq == 0) { // first time in, we initialize base_path
2038
// Calculate potentially longest base path and check if we have enough
2039
// allocated statically.
2040
const size_t total_length =
2041
(HeapDumpPath == NULL ? 0 : strlen(HeapDumpPath)) +
2042
strlen(os::file_separator()) + max_digit_chars +
2043
strlen(dump_file_name) + strlen(dump_file_ext) + 1;
2044
if (total_length > sizeof(base_path)) {
2045
warning("Cannot create heap dump file. HeapDumpPath is too long.");
2046
return;
2047
}
2048
2049
bool use_default_filename = true;
2050
if (HeapDumpPath == NULL || HeapDumpPath[0] == '\0') {
2051
// HeapDumpPath=<file> not specified
2052
} else {
2053
strcpy(base_path, HeapDumpPath);
2054
// check if the path is a directory (must exist)
2055
DIR* dir = os::opendir(base_path);
2056
if (dir == NULL) {
2057
use_default_filename = false;
2058
} else {
2059
// HeapDumpPath specified a directory. We append a file separator
2060
// (if needed).
2061
os::closedir(dir);
2062
size_t fs_len = strlen(os::file_separator());
2063
if (strlen(base_path) >= fs_len) {
2064
char* end = base_path;
2065
end += (strlen(base_path) - fs_len);
2066
if (strcmp(end, os::file_separator()) != 0) {
2067
strcat(base_path, os::file_separator());
2068
}
2069
}
2070
}
2071
}
2072
// If HeapDumpPath wasn't a file name then we append the default name
2073
if (use_default_filename) {
2074
const size_t dlen = strlen(base_path); // if heap dump dir specified
2075
jio_snprintf(&base_path[dlen], sizeof(base_path)-dlen, "%s%d%s",
2076
dump_file_name, os::current_process_id(), dump_file_ext);
2077
}
2078
const size_t len = strlen(base_path) + 1;
2079
my_path = (char*)os::malloc(len, mtInternal);
2080
if (my_path == NULL) {
2081
warning("Cannot create heap dump file. Out of system memory.");
2082
return;
2083
}
2084
strncpy(my_path, base_path, len);
2085
} else {
2086
// Append a sequence number id for dumps following the first
2087
const size_t len = strlen(base_path) + max_digit_chars + 2; // for '.' and \0
2088
my_path = (char*)os::malloc(len, mtInternal);
2089
if (my_path == NULL) {
2090
warning("Cannot create heap dump file. Out of system memory.");
2091
return;
2092
}
2093
jio_snprintf(my_path, len, "%s.%d", base_path, dump_file_seq);
2094
}
2095
dump_file_seq++; // increment seq number for next time we dump
2096
2097
HeapDumper dumper(false /* no GC before heap dump */,
2098
oome /* pass along out-of-memory-error flag */);
2099
dumper.dump(my_path, tty, HeapDumpGzipLevel);
2100
os::free(my_path);
2101
}
2102
2103