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