hotspot/src/share/vm/memory/metaspaceShared.cpp
author iklam
Wed, 02 Aug 2017 18:06:38 -0700
changeset 46746 ea379ebb9447
parent 46742 24ec8a039c90
child 46810 7dad333205cd
permissions -rw-r--r--
8072061: Automatically determine optimal sizes for the CDS regions Summary: See new C++ class MetaspaceClosure. Reviewed-by: coleenp, jiangli, mseledtsov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     1
/*
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     4
 *
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     7
 * published by the Free Software Foundation.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     8
 *
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    13
 * accompanied this code).
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    14
 *
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    18
 *
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    21
 * questions.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    22
 *
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    23
 */
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    24
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    25
#include "precompiled.hpp"
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
    26
#include "classfile/classListParser.hpp"
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
    27
#include "classfile/classLoaderExt.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    28
#include "classfile/dictionary.hpp"
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    29
#include "classfile/loaderConstraints.hpp"
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    30
#include "classfile/placeholders.hpp"
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
    31
#include "classfile/sharedClassUtil.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    32
#include "classfile/symbolTable.hpp"
46742
24ec8a039c90 8184994: Add Dictionary size logging and jcmd
coleenp
parents: 46625
diff changeset
    33
#include "classfile/stringTable.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    34
#include "classfile/systemDictionary.hpp"
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
    35
#include "classfile/systemDictionaryShared.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    36
#include "code/codeCache.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30120
diff changeset
    37
#include "gc/shared/gcLocker.hpp"
30117
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
    38
#include "interpreter/bytecodeStream.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30120
diff changeset
    39
#include "interpreter/bytecodes.hpp"
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
    40
#include "logging/log.hpp"
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
    41
#include "logging/logMessage.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    42
#include "memory/filemap.hpp"
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    43
#include "memory/metaspace.hpp"
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    44
#include "memory/metaspaceShared.hpp"
37248
11a660dbbb8e 8132524: Missing includes to resourceArea.hpp
jprovino
parents: 37179
diff changeset
    45
#include "memory/resourceArea.hpp"
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
    46
#include "oops/instanceClassLoaderKlass.hpp"
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
    47
#include "oops/instanceMirrorKlass.hpp"
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
    48
#include "oops/instanceRefKlass.hpp"
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
    49
#include "oops/objArrayKlass.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    50
#include "oops/objArrayOop.hpp"
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    51
#include "oops/oop.inline.hpp"
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
    52
#include "oops/typeArrayKlass.hpp"
46560
388aa8d67c80 8181449: Fix debug.hpp / globalDefinitions.hpp dependency inversion
kbarrett
parents: 46522
diff changeset
    53
#include "prims/jvm.h"
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    54
#include "prims/jvmtiRedefineClasses.hpp"
37161
e881f320966e 8150015: Integrate TraceTime with Unified Logging more seamlessly
rehn
parents: 37094
diff changeset
    55
#include "runtime/timerTrace.hpp"
25468
5331df506290 8048241: Introduce umbrella header os.inline.hpp and clean up includes
goetz
parents: 24424
diff changeset
    56
#include "runtime/os.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    57
#include "runtime/signature.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30120
diff changeset
    58
#include "runtime/vmThread.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    59
#include "runtime/vm_operations.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46619
diff changeset
    60
#include "utilities/align.hpp"
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
    61
#include "utilities/defaultStream.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    62
#include "utilities/hashtable.inline.hpp"
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    63
#include "memory/metaspaceClosure.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
    64
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    65
ReservedSpace MetaspaceShared::_shared_rs;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    66
VirtualSpace MetaspaceShared::_shared_vs;
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
    67
MetaspaceSharedStats MetaspaceShared::_stats;
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
    68
bool MetaspaceShared::_has_error_classes;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
    69
bool MetaspaceShared::_archive_loading_failed = false;
39714
976b97b59d87 8153312: Constrain AppCDS behavior
jiangli
parents: 39713
diff changeset
    70
bool MetaspaceShared::_remapped_readwrite = false;
37439
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
    71
address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL;
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
    72
size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0;
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    73
size_t MetaspaceShared::_core_spaces_size = 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    74
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    75
// The CDS archive is divided into the following regions:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    76
//     mc - misc code (the method entry trampolines)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    77
//     rw - read-write metadata
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    78
//     ro - read-only metadata and read-only tables
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    79
//     md - misc data (the c++ vtables)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    80
//     od - optional data (original class files)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    81
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    82
//     s0 - shared strings #0
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    83
//     s1 - shared strings #1 (may be empty)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    84
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    85
// Except for the s0/s1 regions, the other 5 regions are linearly allocated, starting from
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    86
// SharedBaseAddress, in the order of mc->rw->ro->md->od. The size of these 5 regions
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    87
// are page-aligned, and there's no gap between any consecutive regions.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    88
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    89
// These 5 regions are populated in the following steps:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    90
// [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    91
//     temporarily allocated outside of the shared regions. Only the method entry
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    92
//     trampolines are written into the mc region.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    93
// [2] ArchiveCompactor copies RW metadata into the rw region.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    94
// [3] ArchiveCompactor copies RO metadata into the ro region.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    95
// [4] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    96
//     are copied into the ro region as read-only tables.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    97
// [5] C++ vtables are copied into the md region.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    98
// [6] Original class files are copied into the od region.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
    99
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   100
// The s0/s1 regions are populated inside MetaspaceShared::dump_string_and_symbols. Their
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   101
// layout is independent of the other 5 regions.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   102
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   103
class DumpRegion {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   104
private:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   105
  const char* _name;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   106
  char* _base;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   107
  char* _top;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   108
  char* _end;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   109
  bool _is_packed;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   110
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   111
  char* expand_top_to(char* newtop) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   112
    assert(is_allocatable(), "must be initialized and not packed");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   113
    assert(newtop >= _top, "must not grow backwards");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   114
    if (newtop > _end) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   115
      MetaspaceShared::report_out_of_space(_name, newtop - _top);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   116
      ShouldNotReachHere();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   117
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   118
    MetaspaceShared::commit_shared_space_to(newtop);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   119
    _top = newtop;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   120
    return _top;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   121
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   122
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   123
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   124
  DumpRegion(const char* name) : _name(name), _base(NULL), _top(NULL), _end(NULL), _is_packed(false) {}
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   125
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   126
  char* allocate(size_t num_bytes, size_t alignment=BytesPerWord) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   127
    char* p = (char*)align_up(_top, alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   128
    char* newtop = p + align_up(num_bytes, alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   129
    expand_top_to(newtop);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   130
    memset(p, 0, newtop - p);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   131
    return p;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   132
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   133
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   134
  void append_intptr_t(intptr_t n) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   135
    assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   136
    intptr_t *p = (intptr_t*)_top;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   137
    char* newtop = _top + sizeof(intptr_t);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   138
    expand_top_to(newtop);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   139
    *p = n;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   140
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   141
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   142
  char* base()      const { return _base;        }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   143
  char* top()       const { return _top;         }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   144
  char* end()       const { return _end;         }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   145
  size_t reserved() const { return _end - _base; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   146
  size_t used()     const { return _top - _base; }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   147
  bool is_packed()  const { return _is_packed;   }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   148
  bool is_allocatable() const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   149
    return !is_packed() && _base != NULL;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   150
  }
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   151
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   152
  double perc(size_t used, size_t total) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   153
    if (total == 0) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   154
      total = 1;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   155
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   156
    return used / double(total) * 100.0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   157
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   158
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   159
  void print(size_t total_bytes) const {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   160
    tty->print_cr("%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   161
                  _name, used(), perc(used(), total_bytes), reserved(), perc(used(), reserved()), p2i(_base));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   162
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   163
  void print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   164
    tty->print("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   165
               _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   166
    if (strcmp(_name, failing_region) == 0) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   167
      tty->print_cr(" required = %d", int(needed_bytes));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   168
    } else {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   169
      tty->cr();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   170
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   171
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   172
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   173
  void init(const ReservedSpace* rs) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   174
    _base = _top = rs->base();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   175
    _end = rs->end();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   176
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   177
  void init(char* b, char* t, char* e) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   178
    _base = b;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   179
    _top = t;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   180
    _end = e;
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   181
  }
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   182
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   183
  void pack(DumpRegion* next = NULL) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   184
    assert(!is_packed(), "sanity");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   185
    _end = (char*)align_up(_top, Metaspace::reserve_alignment());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   186
    _is_packed = true;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   187
    if (next != NULL) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   188
      next->_base = next->_top = this->_end;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   189
      next->_end = MetaspaceShared::shared_rs()->end();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   190
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   191
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   192
  bool contains(char* p) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   193
    return base() <= p && p < top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   194
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   195
};
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   196
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   197
DumpRegion _mc_region("mc"), _ro_region("ro"), _rw_region("rw"), _md_region("md"), _od_region("od");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   198
DumpRegion _s0_region("s0"), _s1_region("s1");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   199
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   200
char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   201
  return _mc_region.allocate(num_bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   202
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   203
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   204
char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   205
  return _ro_region.allocate(num_bytes);
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   206
}
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   207
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   208
void MetaspaceShared::initialize_shared_rs() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   209
  const size_t reserve_alignment = Metaspace::reserve_alignment();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   210
  bool large_pages = false; // No large pages when dumping the CDS archive.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   211
  char* shared_base = (char*)align_up((char*)SharedBaseAddress, reserve_alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   212
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   213
#ifdef _LP64
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   214
  // On 64-bit VM, the heap and class space layout will be the same as if
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   215
  // you're running in -Xshare:on mode:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   216
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   217
  //                         +-- SharedBaseAddress (default = 0x800000000)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   218
  //                         v
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   219
  // +-..---------+----+ ... +----+----+----+----+----+---------------+
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   220
  // |    Heap    | ST |     | MC | RW | RO | MD | OD | class space   |
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   221
  // +-..---------+----+ ... +----+----+----+----+----+---------------+
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   222
  // |<--MaxHeapSize->|     |<-- UnscaledClassSpaceMax = 4GB ------->|
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   223
  //
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   224
  const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   225
  const size_t cds_total = align_down(UnscaledClassSpaceMax, reserve_alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   226
#else
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   227
  // We don't support archives larger than 256MB on 32-bit due to limited virtual address space.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   228
  size_t cds_total = align_down(256*M, reserve_alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   229
#endif
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   230
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   231
  // First try to reserve the space at the specified SharedBaseAddress.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   232
  _shared_rs = ReservedSpace(cds_total, reserve_alignment, large_pages, shared_base);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   233
  if (_shared_rs.is_reserved()) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   234
    assert(shared_base == 0 || _shared_rs.base() == shared_base, "should match");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   235
  } else {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   236
    // Get a mmap region anywhere if the SharedBaseAddress fails.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   237
    _shared_rs = ReservedSpace(cds_total, reserve_alignment, large_pages);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   238
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   239
  if (!_shared_rs.is_reserved()) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   240
    vm_exit_during_initialization("Unable to reserve memory for shared space",
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   241
                                  err_msg(SIZE_FORMAT " bytes.", cds_total));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   242
  }
41182
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents: 39714
diff changeset
   243
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   244
#ifdef _LP64
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   245
  // During dump time, we allocate 4GB (UnscaledClassSpaceMax) of space and split it up:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   246
  // + The upper 1 GB is used as the "temporary compressed class space" -- preload_classes()
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   247
  //   will store Klasses into this space.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   248
  // + The lower 3 GB is used for the archive -- when preload_classes() is done,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   249
  //   ArchiveCompactor will copy the class metadata into this space, first the RW parts,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   250
  //   then the RO parts.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   251
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   252
  assert(UseCompressedOops && UseCompressedClassPointers,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   253
      "UseCompressedOops and UseCompressedClassPointers must be set");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   254
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   255
  size_t max_archive_size = align_down(cds_total * 3 / 4, reserve_alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   256
  ReservedSpace tmp_class_space = _shared_rs.last_part(max_archive_size);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   257
  CompressedClassSpaceSize = align_down(tmp_class_space.size(), reserve_alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   258
  _shared_rs = _shared_rs.first_part(max_archive_size);
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   259
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   260
  // Set up compress class pointers.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   261
  Universe::set_narrow_klass_base((address)_shared_rs.base());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   262
  if (UseAOT || cds_total > UnscaledClassSpaceMax) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   263
    // AOT forces narrow_klass_shift=LogKlassAlignmentInBytes
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   264
    Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   265
  } else {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   266
    Universe::set_narrow_klass_shift(0);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   267
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   268
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   269
  Metaspace::initialize_class_space(tmp_class_space);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   270
  tty->print_cr("narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   271
                p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   272
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   273
  tty->print_cr("Allocated temporary class space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   274
                CompressedClassSpaceSize, p2i(tmp_class_space.base()));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   275
#endif
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   276
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   277
  // Start with 0 committed bytes. The memory will be committed as needed by
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   278
  // MetaspaceShared::commit_shared_space_to().
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   279
  if (!_shared_vs.initialize(_shared_rs, 0)) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   280
    vm_exit_during_initialization("Unable to allocate memory for shared space");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   281
  }
41182
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents: 39714
diff changeset
   282
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   283
  _mc_region.init(&_shared_rs);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   284
  tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   285
                _shared_rs.size(), p2i(_shared_rs.base()));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   286
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   287
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   288
void MetaspaceShared::commit_shared_space_to(char* newtop) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   289
  assert(DumpSharedSpaces, "dump-time only");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   290
  char* base = _shared_rs.base();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   291
  size_t need_committed_size = newtop - base;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   292
  size_t has_committed_size = _shared_vs.committed_size();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   293
  if (need_committed_size < has_committed_size) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   294
    return;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   295
  }
41182
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents: 39714
diff changeset
   296
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   297
  size_t min_bytes = need_committed_size - has_committed_size;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   298
  size_t preferred_bytes = 1 * M;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   299
  size_t uncommitted = _shared_vs.reserved_size() - has_committed_size;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   300
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   301
  size_t commit = MAX2(min_bytes, preferred_bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   302
  assert(commit <= uncommitted, "sanity");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   303
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   304
  bool result = _shared_vs.expand_by(commit, false);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   305
  if (!result) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   306
    vm_exit_during_initialization(err_msg("Failed to expand shared space to " SIZE_FORMAT " bytes",
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   307
                                          need_committed_size));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   308
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   309
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   310
  log_info(cds)("Expanding shared spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9)  " bytes ending at %p]",
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   311
                commit, _shared_vs.actual_committed_size(), _shared_vs.high());
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   312
}
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
   313
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   314
// Read/write a data stream for restoring/preserving metadata pointers and
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   315
// miscellaneous data from/to the shared archive file.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   316
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   317
void MetaspaceShared::serialize(SerializeClosure* soc) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   318
  int tag = 0;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   319
  soc->do_tag(--tag);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   320
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   321
  // Verify the sizes of various metadata in the system.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   322
  soc->do_tag(sizeof(Method));
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   323
  soc->do_tag(sizeof(ConstMethod));
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   324
  soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   325
  soc->do_tag(sizeof(ConstantPool));
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   326
  soc->do_tag(sizeof(ConstantPoolCache));
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   327
  soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   328
  soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   329
  soc->do_tag(sizeof(Symbol));
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   330
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   331
  // Dump/restore miscellaneous metadata.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   332
  Universe::serialize(soc, true);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   333
  soc->do_tag(--tag);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   334
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   335
  // Dump/restore references to commonly used names and signatures.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   336
  vmSymbols::serialize(soc);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   337
  soc->do_tag(--tag);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   338
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   339
  // Dump/restore the symbol and string tables
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   340
  SymbolTable::serialize(soc);
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   341
  StringTable::serialize(soc);
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   342
  soc->do_tag(--tag);
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   343
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   344
  soc->do_tag(666);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   345
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   346
37439
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   347
address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) {
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   348
  if (DumpSharedSpaces) {
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   349
    if (_cds_i2i_entry_code_buffers == NULL) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   350
      _cds_i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
37439
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   351
      _cds_i2i_entry_code_buffers_size = total_size;
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   352
    }
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   353
  } else if (UseSharedSpaces) {
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   354
    assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized");
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   355
  } else {
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   356
    return NULL;
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   357
  }
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   358
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   359
  assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change");
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   360
  return _cds_i2i_entry_code_buffers;
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
   361
}
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   362
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   363
// CDS code for dumping shared archive.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   364
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   365
// Global object for holding classes that have been loaded.  Since this
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   366
// is run at a safepoint just before exit, this is the entire set of classes.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   367
static GrowableArray<Klass*>* _global_klass_objects;
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
   368
class CollectClassesClosure : public KlassClosure {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
   369
  void do_klass(Klass* k) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   370
    if (!(k->is_instance_klass() && InstanceKlass::cast(k)->is_in_error_state())) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   371
      _global_klass_objects->append_if_missing(k);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   372
    }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   373
  }
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
   374
};
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
   375
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   376
static void remove_unshareable_in_classes() {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   377
  for (int i = 0; i < _global_klass_objects->length(); i++) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   378
    Klass* k = _global_klass_objects->at(i);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   379
    k->remove_unshareable_info();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   380
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   381
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   382
30117
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   383
static void rewrite_nofast_bytecode(Method* method) {
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   384
  RawBytecodeStream bcs(method);
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   385
  while (!bcs.is_last_bytecode()) {
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   386
    Bytecodes::Code opcode = bcs.raw_next();
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   387
    switch (opcode) {
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   388
    case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   389
    case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   390
    case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
38943
2e5c855d6b1e 8157189: 'iload_w' in shared class is not interpreted correctly.
jiangli
parents: 38151
diff changeset
   391
    case Bytecodes::_iload: {
2e5c855d6b1e 8157189: 'iload_w' in shared class is not interpreted correctly.
jiangli
parents: 38151
diff changeset
   392
      if (!bcs.is_wide()) {
2e5c855d6b1e 8157189: 'iload_w' in shared class is not interpreted correctly.
jiangli
parents: 38151
diff changeset
   393
        *bcs.bcp() = Bytecodes::_nofast_iload;
2e5c855d6b1e 8157189: 'iload_w' in shared class is not interpreted correctly.
jiangli
parents: 38151
diff changeset
   394
      }
2e5c855d6b1e 8157189: 'iload_w' in shared class is not interpreted correctly.
jiangli
parents: 38151
diff changeset
   395
      break;
2e5c855d6b1e 8157189: 'iload_w' in shared class is not interpreted correctly.
jiangli
parents: 38151
diff changeset
   396
    }
30117
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   397
    default: break;
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   398
    }
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   399
  }
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   400
}
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   401
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   402
// Walk all methods in the class list to ensure that they won't be modified at
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   403
// run time. This includes:
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   404
// [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   405
//     at run time by RewriteBytecodes/RewriteFrequentPairs
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   406
// [2] Assign a fingerprint, so one doesn't need to be assigned at run-time.
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   407
static void rewrite_nofast_bytecodes_and_calculate_fingerprints() {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   408
  for (int i = 0; i < _global_klass_objects->length(); i++) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   409
    Klass* k = _global_klass_objects->at(i);
33611
9abd65805e19 8139203: Consistent naming for klass type predicates
coleenp
parents: 33602
diff changeset
   410
    if (k->is_instance_klass()) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   411
      InstanceKlass* ik = InstanceKlass::cast(k);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   412
      for (int i = 0; i < ik->methods()->length(); i++) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   413
        Method* m = ik->methods()->at(i);
30117
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
   414
        rewrite_nofast_bytecode(m);
20059
c26474fd5ac0 8022887: Assertion hit while using class and redefining it with RedefineClasses simultaneously
coleenp
parents: 19319
diff changeset
   415
        Fingerprinter fp(m);
c26474fd5ac0 8022887: Assertion hit while using class and redefining it with RedefineClasses simultaneously
coleenp
parents: 19319
diff changeset
   416
        // The side effect of this call sets method's fingerprint field.
c26474fd5ac0 8022887: Assertion hit while using class and redefining it with RedefineClasses simultaneously
coleenp
parents: 19319
diff changeset
   417
        fp.fingerprint();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   418
      }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   419
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   420
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   421
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   422
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   423
static void relocate_cached_class_file() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   424
  for (int i = 0; i < _global_klass_objects->length(); i++) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   425
    Klass* k = _global_klass_objects->at(i);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   426
    if (k->is_instance_klass()) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   427
      InstanceKlass* ik = InstanceKlass::cast(k);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   428
      JvmtiCachedClassFileData* p = ik->get_archived_class_data();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   429
      if (p != NULL) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   430
        int size = offset_of(JvmtiCachedClassFileData, data) + p->length;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   431
        JvmtiCachedClassFileData* q = (JvmtiCachedClassFileData*)_od_region.allocate(size);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   432
        q->length = p->length;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   433
        memcpy(q->data, p->data, p->length);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   434
        ik->set_archived_class_data(q);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   435
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   436
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   437
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   438
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   439
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   440
// Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   441
// (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   442
//
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   443
// Addresses of the vtables and the methods may be different across JVM runs,
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   444
// if libjvm.so is dynamically loaded at a different base address.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   445
//
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   446
// To ensure that the Metadata objects in the CDS archive always have the correct vtable:
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   447
//
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   448
// + at dump time:  we redirect the _vptr to point to our own vtables inside
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   449
//                  the CDS image
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   450
// + at run time:   we clone the actual contents of the vtables from libjvm.so
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   451
//                  into our own tables.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   452
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   453
// Currently, the archive contain ONLY the following types of objects that have C++ vtables.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   454
#define CPP_VTABLE_PATCH_TYPES_DO(f) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   455
  f(ConstantPool) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   456
  f(InstanceKlass) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   457
  f(InstanceClassLoaderKlass) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   458
  f(InstanceMirrorKlass) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   459
  f(InstanceRefKlass) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   460
  f(Method) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   461
  f(ObjArrayKlass) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   462
  f(TypeArrayKlass)
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   463
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   464
class CppVtableInfo {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   465
  intptr_t _vtable_size;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   466
  intptr_t _cloned_vtable[1];
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   467
public:
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   468
  static int num_slots(int vtable_size) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   469
    return 1 + vtable_size; // Need to add the space occupied by _vtable_size;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   470
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   471
  int vtable_size()           { return int(uintx(_vtable_size)); }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   472
  void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   473
  intptr_t* cloned_vtable()   { return &_cloned_vtable[0]; }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   474
  void zero()                 { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   475
  // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   476
  static size_t byte_size(int vtable_size) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   477
    CppVtableInfo i;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   478
    return pointer_delta(&i._cloned_vtable[vtable_size], &i, sizeof(u1));
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   479
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   480
};
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   481
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   482
template <class T> class CppVtableCloner : public T {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   483
  static intptr_t* vtable_of(Metadata& m) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   484
    return *((intptr_t**)&m);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   485
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   486
  static CppVtableInfo* _info;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   487
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   488
  static int get_vtable_length(const char* name);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   489
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   490
public:
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   491
  // Allocate and initialize the C++ vtable, starting from top, but do not go past end.
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   492
  static intptr_t* allocate(const char* name);
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   493
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   494
  // Clone the vtable to ...
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   495
  static intptr_t* clone_vtable(const char* name, CppVtableInfo* info);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   496
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   497
  static void zero_vtable_clone() {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   498
    assert(DumpSharedSpaces, "dump-time only");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   499
    _info->zero();
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   500
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   501
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   502
  // Switch the vtable pointer to point to the cloned vtable.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   503
  static void patch(Metadata* obj) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   504
    assert(DumpSharedSpaces, "dump-time only");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   505
    *(void**)obj = (void*)(_info->cloned_vtable());
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   506
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   507
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   508
  static bool is_valid_shared_object(const T* obj) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   509
    intptr_t* vptr = *(intptr_t**)obj;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   510
    return vptr == _info->cloned_vtable();
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   511
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   512
};
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   513
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   514
template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   515
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   516
template <class T>
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   517
intptr_t* CppVtableCloner<T>::allocate(const char* name) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   518
  assert(is_aligned(_md_region.top(), sizeof(intptr_t)), "bad alignment");
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   519
  int n = get_vtable_length(name);
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   520
  _info = (CppVtableInfo*)_md_region.allocate(CppVtableInfo::byte_size(n), sizeof(intptr_t));
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   521
  _info->set_vtable_size(n);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   522
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   523
  intptr_t* p = clone_vtable(name, _info);
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   524
  assert((char*)p == _md_region.top(), "must be");
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   525
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   526
  return p;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   527
}
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   528
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   529
template <class T>
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   530
intptr_t* CppVtableCloner<T>::clone_vtable(const char* name, CppVtableInfo* info) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   531
  if (!DumpSharedSpaces) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   532
    assert(_info == 0, "_info is initialized only at dump time");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   533
    _info = info; // Remember it -- it will be used by MetaspaceShared::is_valid_shared_method()
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   534
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   535
  T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   536
  int n = info->vtable_size();
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   537
  intptr_t* srcvtable = vtable_of(tmp);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   538
  intptr_t* dstvtable = info->cloned_vtable();
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   539
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   540
  // We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   541
  // safe to do memcpy.
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   542
  log_debug(cds, vtables)("Copying %3d vtable entries for %s", n, name);
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   543
  memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   544
  return dstvtable + n;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   545
}
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   546
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   547
// To determine the size of the vtable for each type, we use the following
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   548
// trick by declaring 2 subclasses:
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   549
//
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   550
//   class CppVtableTesterA: public InstanceKlass {virtual int   last_virtual_method() {return 1;}    };
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   551
//   class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return NULL}; };
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   552
//
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   553
// CppVtableTesterA and CppVtableTesterB's vtables have the following properties:
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   554
// - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N)
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   555
// - The first N entries have are exactly the same as in InstanceKlass's vtable.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   556
// - Their last entry is different.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   557
//
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   558
// So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   559
// and find the first entry that's different.
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   560
//
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   561
// This works on all C++ compilers supported by Oracle, but you may need to tweak it for more
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   562
// esoteric compilers.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   563
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   564
template <class T> class CppVtableTesterB: public T {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   565
public:
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   566
  virtual int last_virtual_method() {return 1;}
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   567
};
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   568
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   569
template <class T> class CppVtableTesterA : public T {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   570
public:
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   571
  virtual void* last_virtual_method() {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   572
    // Make this different than CppVtableTesterB::last_virtual_method so the C++
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   573
    // compiler/linker won't alias the two functions.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   574
    return NULL;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   575
  }
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   576
};
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   577
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   578
template <class T>
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   579
int CppVtableCloner<T>::get_vtable_length(const char* name) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   580
  CppVtableTesterA<T> a;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   581
  CppVtableTesterB<T> b;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   582
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   583
  intptr_t* avtable = vtable_of(a);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   584
  intptr_t* bvtable = vtable_of(b);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   585
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   586
  // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   587
  int vtable_len = 1;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   588
  for (; ; vtable_len++) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   589
    if (avtable[vtable_len] != bvtable[vtable_len]) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   590
      break;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   591
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   592
  }
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   593
  log_debug(cds, vtables)("Found   %3d vtable entries for %s", vtable_len, name);
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   594
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   595
  return vtable_len;
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   596
}
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   597
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   598
#define ALLOC_CPP_VTABLE_CLONE(c) \
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   599
  CppVtableCloner<c>::allocate(#c);
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   600
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   601
#define CLONE_CPP_VTABLE(c) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   602
  p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   603
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   604
#define ZERO_CPP_VTABLE(c) \
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   605
 CppVtableCloner<c>::zero_vtable_clone();
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   606
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   607
// This can be called at both dump time and run time.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   608
intptr_t* MetaspaceShared::clone_cpp_vtables(intptr_t* p) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   609
  assert(DumpSharedSpaces || UseSharedSpaces, "sanity");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   610
  CPP_VTABLE_PATCH_TYPES_DO(CLONE_CPP_VTABLE);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   611
  return p;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   612
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   613
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   614
void MetaspaceShared::zero_cpp_vtable_clones_for_writing() {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   615
  assert(DumpSharedSpaces, "dump-time only");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   616
  CPP_VTABLE_PATCH_TYPES_DO(ZERO_CPP_VTABLE);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   617
}
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   618
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   619
// Allocate and initialize the C++ vtables, starting from top, but do not go past end.
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   620
void MetaspaceShared::allocate_cpp_vtable_clones() {
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   621
  assert(DumpSharedSpaces, "dump-time only");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   622
  // Layout (each slot is a intptr_t):
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   623
  //   [number of slots in the first vtable = n1]
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   624
  //   [ <n1> slots for the first vtable]
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   625
  //   [number of slots in the first second = n2]
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   626
  //   [ <n2> slots for the second vtable]
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   627
  //   ...
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   628
  // The order of the vtables is the same as the CPP_VTAB_PATCH_TYPES_DO macro.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   629
  CPP_VTABLE_PATCH_TYPES_DO(ALLOC_CPP_VTABLE_CLONE);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   630
}
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   631
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   632
// Switch the vtable pointer to point to the cloned vtable. We assume the
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   633
// vtable pointer is in first slot in object.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   634
void MetaspaceShared::patch_cpp_vtable_pointers() {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   635
  int n = _global_klass_objects->length();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   636
  for (int i = 0; i < n; i++) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   637
    Klass* obj = _global_klass_objects->at(i);
33611
9abd65805e19 8139203: Consistent naming for klass type predicates
coleenp
parents: 33602
diff changeset
   638
    if (obj->is_instance_klass()) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   639
      InstanceKlass* ik = InstanceKlass::cast(obj);
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   640
      if (ik->is_class_loader_instance_klass()) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   641
        CppVtableCloner<InstanceClassLoaderKlass>::patch(ik);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   642
      } else if (ik->is_reference_instance_klass()) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   643
        CppVtableCloner<InstanceRefKlass>::patch(ik);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   644
      } else if (ik->is_mirror_instance_klass()) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   645
        CppVtableCloner<InstanceMirrorKlass>::patch(ik);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   646
      } else {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   647
        CppVtableCloner<InstanceKlass>::patch(ik);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   648
      }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   649
      ConstantPool* cp = ik->constants();
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   650
      CppVtableCloner<ConstantPool>::patch(cp);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   651
      for (int j = 0; j < ik->methods()->length(); j++) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   652
        Method* m = ik->methods()->at(j);
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   653
        CppVtableCloner<Method>::patch(m);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   654
        assert(CppVtableCloner<Method>::is_valid_shared_object(m), "must be");
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   655
      }
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   656
    } else if (obj->is_objArray_klass()) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   657
      CppVtableCloner<ObjArrayKlass>::patch(obj);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   658
    } else {
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   659
      assert(obj->is_typeArray_klass(), "sanity");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   660
      CppVtableCloner<TypeArrayKlass>::patch(obj);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   661
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   662
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   663
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   664
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   665
bool MetaspaceShared::is_valid_shared_method(const Method* m) {
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   666
  assert(is_in_shared_space(m), "must be");
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   667
  return CppVtableCloner<Method>::is_valid_shared_object(m);
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   668
}
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
   669
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   670
// Closure for serializing initialization data out to a data area to be
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   671
// written to the shared file.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   672
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   673
class WriteClosure : public SerializeClosure {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   674
private:
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   675
  DumpRegion* _dump_region;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   676
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   677
public:
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   678
  WriteClosure(DumpRegion* r) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   679
    _dump_region = r;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   680
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   681
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   682
  void do_ptr(void** p) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   683
    _dump_region->append_intptr_t((intptr_t)*p);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   684
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   685
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   686
  void do_u4(u4* p) {
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   687
    void* ptr = (void*)(uintx(*p));
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   688
    do_ptr(&ptr);
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   689
  }
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   690
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   691
  void do_tag(int tag) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   692
    _dump_region->append_intptr_t((intptr_t)tag);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   693
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   694
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   695
  void do_region(u_char* start, size_t size) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   696
    assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   697
    assert(size % sizeof(intptr_t) == 0, "bad size");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   698
    do_tag((int)size);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   699
    while (size > 0) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   700
      _dump_region->append_intptr_t(*(intptr_t*)start);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   701
      start += sizeof(intptr_t);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   702
      size -= sizeof(intptr_t);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   703
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   704
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   705
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   706
  bool reading() const { return false; }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   707
};
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   708
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   709
// This is for dumping detailed statistics for the allocations
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   710
// in the shared spaces.
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   711
class DumpAllocStats : public ResourceObj {
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   712
public:
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   713
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   714
  // Here's poor man's enum inheritance
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   715
#define SHAREDSPACE_OBJ_TYPES_DO(f) \
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   716
  METASPACE_OBJ_TYPES_DO(f) \
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   717
  f(SymbolHashentry) \
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
   718
  f(SymbolBucket) \
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   719
  f(StringHashentry) \
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   720
  f(StringBucket) \
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   721
  f(Other)
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   722
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   723
  enum Type {
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   724
    // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   725
    SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   726
    _number_of_types
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   727
  };
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   728
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   729
  static const char * type_name(Type type) {
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   730
    switch(type) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   731
    SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   732
    default:
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   733
      ShouldNotReachHere();
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   734
      return NULL;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   735
    }
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   736
  }
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   737
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   738
public:
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   739
  enum { RO = 0, RW = 1 };
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   740
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   741
  int _counts[2][_number_of_types];
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   742
  int _bytes [2][_number_of_types];
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   743
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   744
  DumpAllocStats() {
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   745
    memset(_counts, 0, sizeof(_counts));
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   746
    memset(_bytes,  0, sizeof(_bytes));
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   747
  };
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   748
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   749
  void record(MetaspaceObj::Type type, int byte_size, bool read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   750
    assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   751
    int which = (read_only) ? RO : RW;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   752
    _counts[which][type] ++;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   753
    _bytes [which][type] += byte_size;
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   754
  }
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   755
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   756
  void record_other_type(int byte_size, bool read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   757
    int which = (read_only) ? RO : RW;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   758
    _bytes [which][OtherType] += byte_size;
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   759
  }
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   760
  void print_stats(int ro_all, int rw_all, int mc_all, int md_all);
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   761
};
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   762
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   763
void DumpAllocStats::print_stats(int ro_all, int rw_all, int mc_all, int md_all) {
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   764
  // Calculate size of data that was not allocated by Metaspace::allocate()
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
   765
  MetaspaceSharedStats *stats = MetaspaceShared::stats();
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   766
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
   767
  // symbols
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   768
  _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count;
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   769
  _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes;
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   770
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   771
  _counts[RO][SymbolBucketType] = stats->symbol.bucket_count;
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   772
  _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes;
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   773
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   774
  // strings
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   775
  _counts[RO][StringHashentryType] = stats->string.hashentry_count;
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   776
  _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes;
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   777
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   778
  _counts[RO][StringBucketType] = stats->string.bucket_count;
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
   779
  _bytes [RO][StringBucketType] = stats->string.bucket_bytes;
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   780
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   781
  // TODO: count things like dictionary, vtable, etc
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   782
  _bytes[RW][OtherType] += mc_all + md_all;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   783
  rw_all += mc_all + md_all; // mc/md are mapped Read/Write
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   784
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   785
  // prevent divide-by-zero
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   786
  if (ro_all < 1) {
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   787
    ro_all = 1;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   788
  }
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   789
  if (rw_all < 1) {
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   790
    rw_all = 1;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   791
  }
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   792
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   793
  int all_ro_count = 0;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   794
  int all_ro_bytes = 0;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   795
  int all_rw_count = 0;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   796
  int all_rw_bytes = 0;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   797
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23540
diff changeset
   798
// To make fmt_stats be a syntactic constant (for format warnings), use #define.
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23540
diff changeset
   799
#define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f"
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   800
  const char *sep = "--------------------+---------------------------+---------------------------+--------------------------";
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   801
  const char *hdr = "                        ro_cnt   ro_bytes     % |   rw_cnt   rw_bytes     % |  all_cnt  all_bytes     %";
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   802
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   803
  ResourceMark rm;
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   804
  LogMessage(cds) msg;
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   805
  stringStream info_stream;
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   806
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   807
  info_stream.print_cr("Detailed metadata info (excluding od/st regions; rw stats include md/mc regions):");
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   808
  info_stream.print_cr("%s", hdr);
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   809
  info_stream.print_cr("%s", sep);
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   810
  for (int type = 0; type < int(_number_of_types); type ++) {
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   811
    const char *name = type_name((Type)type);
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   812
    int ro_count = _counts[RO][type];
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   813
    int ro_bytes = _bytes [RO][type];
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   814
    int rw_count = _counts[RW][type];
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   815
    int rw_bytes = _bytes [RW][type];
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   816
    int count = ro_count + rw_count;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   817
    int bytes = ro_bytes + rw_bytes;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   818
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   819
    double ro_perc = 100.0 * double(ro_bytes) / double(ro_all);
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   820
    double rw_perc = 100.0 * double(rw_bytes) / double(rw_all);
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   821
    double perc    = 100.0 * double(bytes)    / double(ro_all + rw_all);
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   822
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   823
    info_stream.print_cr(fmt_stats, name,
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   824
                         ro_count, ro_bytes, ro_perc,
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   825
                         rw_count, rw_bytes, rw_perc,
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   826
                         count, bytes, perc);
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   827
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   828
    all_ro_count += ro_count;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   829
    all_ro_bytes += ro_bytes;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   830
    all_rw_count += rw_count;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   831
    all_rw_bytes += rw_bytes;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   832
  }
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   833
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   834
  int all_count = all_ro_count + all_rw_count;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   835
  int all_bytes = all_ro_bytes + all_rw_bytes;
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   836
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   837
  double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all);
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   838
  double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all);
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   839
  double all_perc    = 100.0 * double(all_bytes)    / double(ro_all + rw_all);
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   840
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   841
  info_stream.print_cr("%s", sep);
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   842
  info_stream.print_cr(fmt_stats, "Total",
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   843
                       all_ro_count, all_ro_bytes, all_ro_perc,
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   844
                       all_rw_count, all_rw_bytes, all_rw_perc,
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   845
                       all_count, all_bytes, all_perc);
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   846
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   847
  assert(all_ro_bytes == ro_all, "everything should have been counted");
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   848
  assert(all_rw_bytes == rw_all, "everything should have been counted");
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   849
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
   850
  msg.info("%s", info_stream.as_string());
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23540
diff changeset
   851
#undef fmt_stats
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
   852
}
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   853
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   854
// Populate the shared space.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   855
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   856
class VM_PopulateDumpSharedSpace: public VM_Operation {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   857
private:
31345
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
   858
  GrowableArray<MemRegion> *_string_regions;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   859
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   860
  void dump_string_and_symbols();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   861
  char* dump_read_only_tables();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   862
  void print_region_stats();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   863
public:
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   864
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   865
  VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
   866
  void doit();   // outline because gdb sucks
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   867
  static void write_region(FileMapInfo* mapinfo, int region, DumpRegion* space, bool read_only,  bool allow_exec);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   868
}; // class VM_PopulateDumpSharedSpace
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   869
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   870
class SortedSymbolClosure: public SymbolClosure {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   871
  GrowableArray<Symbol*> _symbols;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   872
  virtual void do_symbol(Symbol** sym) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   873
    assert((*sym)->is_permanent(), "archived symbols must be permanent");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   874
    _symbols.append(*sym);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   875
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   876
  static int compare_symbols_by_address(Symbol** a, Symbol** b) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   877
    if (a[0] < b[0]) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   878
      return -1;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   879
    } else if (a[0] == b[0]) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   880
      return 0;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   881
    } else {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   882
      return 1;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   883
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   884
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   885
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   886
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   887
  SortedSymbolClosure() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   888
    SymbolTable::symbols_do(this);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   889
    _symbols.sort(compare_symbols_by_address);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   890
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   891
  GrowableArray<Symbol*>* get_sorted_symbols() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   892
    return &_symbols;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   893
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   894
};
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   895
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   896
// ArchiveCompactor --
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   897
//
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   898
// This class is the central piece of shared archive compaction -- all metaspace data are
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   899
// initially allocated outside of the shared regions. ArchiveCompactor copies the
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   900
// metaspace data into their final location in the shared regions.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   901
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   902
class ArchiveCompactor : AllStatic {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   903
  static DumpAllocStats* _alloc_stats;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   904
  static SortedSymbolClosure* _ssc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   905
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   906
  static unsigned my_hash(const address& a) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   907
    return primitive_hash<address>(a);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   908
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   909
  static bool my_equals(const address& a0, const address& a1) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   910
    return primitive_equals<address>(a0, a1);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   911
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   912
  typedef ResourceHashtable<
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   913
      address, address,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   914
      ArchiveCompactor::my_hash,   // solaris compiler doesn't like: primitive_hash<address>
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   915
      ArchiveCompactor::my_equals, // solaris compiler doesn't like: primitive_equals<address>
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   916
      16384, ResourceObj::C_HEAP> RelocationTable;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   917
  static RelocationTable* _new_loc_table;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   918
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   919
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   920
  static void initialize() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   921
    _alloc_stats = new(ResourceObj::C_HEAP, mtInternal)DumpAllocStats;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   922
    _new_loc_table = new(ResourceObj::C_HEAP, mtInternal)RelocationTable;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   923
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   924
  static DumpAllocStats* alloc_stats() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   925
    return _alloc_stats;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   926
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   927
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   928
  static void allocate(MetaspaceClosure::Ref* ref, bool read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   929
    address obj = ref->obj();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   930
    int bytes = ref->size() * BytesPerWord;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   931
    char* p;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   932
    size_t alignment = BytesPerWord;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   933
    char* oldtop;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   934
    char* newtop;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   935
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   936
    if (read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   937
      oldtop = _ro_region.top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   938
      p = _ro_region.allocate(bytes, alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   939
      newtop = _ro_region.top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   940
    } else {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   941
      oldtop = _rw_region.top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   942
      p = _rw_region.allocate(bytes, alignment);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   943
      newtop = _rw_region.top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   944
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   945
    memcpy(p, obj, bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   946
    bool isnew = _new_loc_table->put(obj, (address)p);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   947
    assert(isnew, "must be");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   948
    log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   949
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   950
    _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   951
    if (ref->msotype() == MetaspaceObj::SymbolType) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   952
      uintx delta = MetaspaceShared::object_delta(p);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   953
      if (delta > MAX_SHARED_DELTA) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   954
        // This is just a sanity check and should not appear in any real world usage. This
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   955
        // happens only if you allocate more than 2GB of Symbols and would require
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   956
        // millions of shared classes.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   957
        vm_exit_during_initialization("Too many Symbols in the CDS archive",
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   958
                                      "Please reduce the number of shared classes.");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   959
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   960
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   961
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   962
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   963
  static address get_new_loc(MetaspaceClosure::Ref* ref) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   964
    address* pp = _new_loc_table->get(ref->obj());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   965
    assert(pp != NULL, "must be");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   966
    return *pp;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   967
  }
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
   968
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
   969
private:
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   970
  // Makes a shallow copy of visited MetaspaceObj's
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   971
  class ShallowCopier: public UniqueMetaspaceClosure {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   972
    bool _read_only;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   973
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   974
    ShallowCopier(bool read_only) : _read_only(read_only) {}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   975
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   976
    virtual void do_unique_ref(Ref* ref, bool read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   977
      if (read_only == _read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   978
        allocate(ref, read_only);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   979
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   980
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   981
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   982
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   983
  // Relocate embedded pointers within a MetaspaceObj's shallow copy
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   984
  class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   985
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   986
    virtual void do_unique_ref(Ref* ref, bool read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   987
      address new_loc = get_new_loc(ref);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   988
      RefRelocator refer;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   989
      ref->metaspace_pointers_do_at(&refer, new_loc);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   990
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   991
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   992
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   993
  // Relocate a reference to point to its shallow copy
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   994
  class RefRelocator: public MetaspaceClosure {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   995
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   996
    virtual bool do_ref(Ref* ref, bool read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   997
      if (ref->not_null()) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   998
        ref->update(get_new_loc(ref));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
   999
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1000
      return false; // Do not recurse.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1001
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1002
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1003
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1004
#ifdef ASSERT
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1005
  class IsRefInArchiveChecker: public MetaspaceClosure {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1006
  public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1007
    virtual bool do_ref(Ref* ref, bool read_only) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1008
      if (ref->not_null()) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1009
        char* obj = (char*)ref->obj();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1010
        assert(_ro_region.contains(obj) || _rw_region.contains(obj),
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1011
               "must be relocated to point to CDS archive");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1012
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1013
      return false; // Do not recurse.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1014
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1015
  };
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1016
#endif
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1017
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1018
public:
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1019
  static void copy_and_compact() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1020
    // We should no longer allocate anything from the metaspace, so that
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1021
    // we can have a stable set of MetaspaceObjs to work with.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1022
    Metaspace::freeze();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1023
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1024
    ResourceMark rm;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1025
    SortedSymbolClosure the_ssc; // StackObj
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1026
    _ssc = &the_ssc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1027
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1028
    tty->print_cr("Scanning all metaspace objects ... ");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1029
    {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1030
      // allocate and shallow-copy RW objects, immediately following the MC region
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1031
      tty->print_cr("Allocating RW objects ... ");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1032
      _mc_region.pack(&_rw_region);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1033
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1034
      ResourceMark rm;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1035
      ShallowCopier rw_copier(false);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1036
      iterate_roots(&rw_copier);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1037
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1038
    {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1039
      // allocate and shallow-copy of RO object, immediately following the RW region
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1040
      tty->print_cr("Allocating RO objects ... ");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1041
      _rw_region.pack(&_ro_region);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1042
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1043
      ResourceMark rm;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1044
      ShallowCopier ro_copier(true);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1045
      iterate_roots(&ro_copier);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1046
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1047
    {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1048
      tty->print_cr("Relocating embedded pointers ... ");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1049
      ResourceMark rm;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1050
      ShallowCopyEmbeddedRefRelocator emb_reloc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1051
      iterate_roots(&emb_reloc);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1052
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1053
    {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1054
      tty->print_cr("Relocating external roots ... ");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1055
      ResourceMark rm;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1056
      RefRelocator ext_reloc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1057
      iterate_roots(&ext_reloc);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1058
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1059
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1060
#ifdef ASSERT
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1061
    {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1062
      tty->print_cr("Verifying external roots ... ");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1063
      ResourceMark rm;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1064
      IsRefInArchiveChecker checker;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1065
      iterate_roots(&checker);
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
  1066
    }
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1067
#endif
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1068
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1069
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1070
    // cleanup
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1071
    _ssc = NULL;
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
  1072
  }
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1073
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1074
  // We must relocate the System::_well_known_klasses only after we have copied the
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1075
  // strings in during dump_string_and_symbols(): during the string copy, we operate on old
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1076
  // String objects which assert that their klass is the old
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1077
  // SystemDictionary::String_klass().
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1078
  static void relocate_well_known_klasses() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1079
    {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1080
      tty->print_cr("Relocating SystemDictionary::_well_known_klasses[] ... ");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1081
      ResourceMark rm;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1082
      RefRelocator ext_reloc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1083
      SystemDictionary::well_known_klasses_do(&ext_reloc);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1084
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1085
    // NOTE: after this point, we shouldn't have any globals that can reach the old
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1086
    // objects.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1087
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1088
    // We cannot use any of the objects in the heap anymore (except for the objects
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1089
    // in the CDS shared string regions) because their headers no longer point to
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1090
    // valid Klasses.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1091
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1092
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1093
  static void iterate_roots(MetaspaceClosure* it) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1094
    GrowableArray<Symbol*>* symbols = _ssc->get_sorted_symbols();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1095
    for (int i=0; i<symbols->length(); i++) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1096
      it->push(symbols->adr_at(i));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1097
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1098
    if (_global_klass_objects != NULL) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1099
      // Need to fix up the pointers
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1100
      for (int i = 0; i < _global_klass_objects->length(); i++) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1101
        // NOTE -- this requires that the vtable is NOT yet patched, or else we are hosed.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1102
        it->push(_global_klass_objects->adr_at(i));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1103
      }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1104
    }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1105
    FileMapInfo::metaspace_pointers_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1106
    SystemDictionary::classes_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1107
    Universe::metaspace_pointers_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1108
    SymbolTable::metaspace_pointers_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1109
    vmSymbols::metaspace_pointers_do(it);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1110
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1111
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1112
  static Klass* get_relocated_klass(Klass* orig_klass) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1113
    address* pp = _new_loc_table->get((address)orig_klass);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1114
    assert(pp != NULL, "must be");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1115
    Klass* klass = (Klass*)(*pp);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1116
    assert(klass->is_klass(), "must be");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1117
    return klass;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1118
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1119
};
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1120
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1121
DumpAllocStats* ArchiveCompactor::_alloc_stats;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1122
SortedSymbolClosure* ArchiveCompactor::_ssc;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1123
ArchiveCompactor::RelocationTable* ArchiveCompactor::_new_loc_table;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1124
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1125
void VM_PopulateDumpSharedSpace::write_region(FileMapInfo* mapinfo, int region_idx,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1126
                                              DumpRegion* dump_region, bool read_only,  bool allow_exec) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1127
  mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1128
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1129
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1130
void VM_PopulateDumpSharedSpace::dump_string_and_symbols() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1131
  tty->print_cr("Dumping string and symbol tables ...");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1132
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1133
  NOT_PRODUCT(SymbolTable::verify());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1134
  NOT_PRODUCT(StringTable::verify());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1135
  SymbolTable::write_to_archive();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1136
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1137
  // The string space has maximum two regions. See FileMapInfo::write_string_regions() for details.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1138
  _string_regions = new GrowableArray<MemRegion>(2);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1139
  StringTable::write_to_archive(_string_regions);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1140
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1141
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1142
char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1143
  char* oldtop = _ro_region.top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1144
  // Reorder the system dictionary. Moving the symbols affects
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1145
  // how the hash table indices are calculated.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1146
  SystemDictionary::reorder_dictionary_for_sharing();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1147
  NOT_PRODUCT(SystemDictionary::verify();)
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1148
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1149
  size_t buckets_bytes = SystemDictionary::count_bytes_for_buckets();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1150
  char* buckets_top = _ro_region.allocate(buckets_bytes, sizeof(intptr_t));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1151
  SystemDictionary::copy_buckets(buckets_top, _ro_region.top());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1152
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1153
  size_t table_bytes = SystemDictionary::count_bytes_for_table();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1154
  char* table_top = _ro_region.allocate(table_bytes, sizeof(intptr_t));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1155
  SystemDictionary::copy_table(table_top, _ro_region.top());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1156
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1157
  // Write the other data to the output array.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1158
  WriteClosure wc(&_ro_region);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1159
  MetaspaceShared::serialize(&wc);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1160
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1161
  char* newtop = _ro_region.top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1162
  ArchiveCompactor::alloc_stats()->record_other_type(int(newtop - oldtop), true);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1163
  return buckets_top;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1164
}
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1165
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1166
void VM_PopulateDumpSharedSpace::doit() {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1167
  Thread* THREAD = VMThread::vm_thread();
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1168
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1169
  NOT_PRODUCT(SystemDictionary::verify();)
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1170
  // The following guarantee is meant to ensure that no loader constraints
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1171
  // exist yet, since the constraints table is not shared.  This becomes
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1172
  // more important now that we don't re-initialize vtables/itables for
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1173
  // shared classes at runtime, where constraints were previously created.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1174
  guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1175
            "loader constraints are not saved");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1176
  guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1177
          "placeholders are not saved");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1178
  // Revisit and implement this if we prelink method handle call sites:
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1179
  guarantee(SystemDictionary::invoke_method_table() == NULL ||
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1180
            SystemDictionary::invoke_method_table()->number_of_entries() == 0,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1181
            "invoke method table is not saved");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1182
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1183
  // At this point, many classes have been loaded.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1184
  // Gather systemDictionary classes in a global array and do everything to
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1185
  // that so we don't have to walk the SystemDictionary again.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1186
  _global_klass_objects = new GrowableArray<Klass*>(1000);
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1187
  CollectClassesClosure collect_classes;
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1188
  ClassLoaderDataGraph::loaded_classes_do(&collect_classes);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1189
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1190
  tty->print_cr("Number of classes %d", _global_klass_objects->length());
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1191
  {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1192
    int num_type_array = 0, num_obj_array = 0, num_inst = 0;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1193
    for (int i = 0; i < _global_klass_objects->length(); i++) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1194
      Klass* k = _global_klass_objects->at(i);
33611
9abd65805e19 8139203: Consistent naming for klass type predicates
coleenp
parents: 33602
diff changeset
  1195
      if (k->is_instance_klass()) {
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1196
        num_inst ++;
33611
9abd65805e19 8139203: Consistent naming for klass type predicates
coleenp
parents: 33602
diff changeset
  1197
      } else if (k->is_objArray_klass()) {
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1198
        num_obj_array ++;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1199
      } else {
33611
9abd65805e19 8139203: Consistent naming for klass type predicates
coleenp
parents: 33602
diff changeset
  1200
        assert(k->is_typeArray_klass(), "sanity");
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1201
        num_type_array ++;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1202
      }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1203
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1204
    tty->print_cr("    instance classes   = %5d", num_inst);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1205
    tty->print_cr("    obj array classes  = %5d", num_obj_array);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1206
    tty->print_cr("    type array classes = %5d", num_type_array);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1207
  }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1208
30117
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
  1209
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
  1210
  // Ensure the ConstMethods won't be modified at run-time
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
  1211
  tty->print("Updating ConstMethods ... ");
cce2cdac56dc 8074345: Enable RewriteBytecodes when VM runs with CDS
minqi
parents: 28363
diff changeset
  1212
  rewrite_nofast_bytecodes_and_calculate_fingerprints();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1213
  tty->print_cr("done. ");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1214
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1215
  // Remove all references outside the metadata
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1216
  tty->print("Removing unshareable information ... ");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1217
  remove_unshareable_in_classes();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1218
  tty->print_cr("done. ");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1219
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1220
  ArchiveCompactor::initialize();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1221
  ArchiveCompactor::copy_and_compact();
37439
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1222
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1223
  dump_string_and_symbols();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1224
  ArchiveCompactor::relocate_well_known_klasses();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1225
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1226
  char* read_only_tables_start = dump_read_only_tables();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1227
  _ro_region.pack(&_md_region);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1228
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1229
  char* vtbl_list = _md_region.top();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1230
  MetaspaceShared::allocate_cpp_vtable_clones();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1231
  _md_region.pack(&_od_region);
15197
cef3fb881307 8005467: CDS size information is incorrect and unfriendly
coleenp
parents: 15100
diff changeset
  1232
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1233
  // Relocate the archived class file data into the od region
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1234
  relocate_cached_class_file();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1235
  _od_region.pack();
15197
cef3fb881307 8005467: CDS size information is incorrect and unfriendly
coleenp
parents: 15100
diff changeset
  1236
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1237
  // The 5 core spaces are allocated consecutively mc->rw->ro->md->od, so there total size
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1238
  // is just the spaces between the two ends.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1239
  size_t core_spaces_size = _od_region.end() - _mc_region.base();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1240
  assert(core_spaces_size == (size_t)align_up(core_spaces_size, Metaspace::reserve_alignment()),
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1241
         "should already be aligned");
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1242
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1243
  // During patching, some virtual methods may be called, so at this point
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1244
  // the vtables must contain valid methods (as filled in by CppVtableCloner::allocate).
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1245
  MetaspaceShared::patch_cpp_vtable_pointers();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1246
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1247
  // The vtable clones contain addresses of the current process.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1248
  // We don't want to write these addresses into the archive.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1249
  MetaspaceShared::zero_cpp_vtable_clones_for_writing();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1250
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1251
  // Create and write the archive file that maps the shared spaces.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1252
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1253
  FileMapInfo* mapinfo = new FileMapInfo();
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1254
  mapinfo->populate_header(os::vm_allocation_granularity());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1255
  mapinfo->set_read_only_tables_start(read_only_tables_start);
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1256
  mapinfo->set_misc_data_patching_start(vtbl_list);
37439
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1257
  mapinfo->set_cds_i2i_entry_code_buffers(MetaspaceShared::cds_i2i_entry_code_buffers());
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1258
  mapinfo->set_cds_i2i_entry_code_buffers_size(MetaspaceShared::cds_i2i_entry_code_buffers_size());
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1259
  mapinfo->set_core_spaces_size(core_spaces_size);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1260
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1261
  char* s0_start, *s0_top, *s0_end;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1262
  char* s1_start, *s1_top, *s1_end;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1263
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1264
  for (int pass=1; pass<=2; pass++) {
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1265
    if (pass == 1) {
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1266
      // The first pass doesn't actually write the data to disk. All it
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1267
      // does is to update the fields in the mapinfo->_header.
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1268
    } else {
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1269
      // After the first pass, the contents of mapinfo->_header are finalized,
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1270
      // so we can compute the header's CRC, and write the contents of the header
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1271
      // and the regions into disk.
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1272
      mapinfo->open_for_write();
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1273
      mapinfo->set_header_crc(mapinfo->compute_header_crc());
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1274
    }
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1275
    mapinfo->write_header();
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1276
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1277
    // NOTE: md contains the trampoline code for method entries, which are patched at run time,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1278
    // so it needs to be read/write.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1279
    write_region(mapinfo, MetaspaceShared::mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1280
    write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1281
    write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1282
    write_region(mapinfo, MetaspaceShared::md, &_md_region, /*read_only=*/false,/*allow_exec=*/false);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1283
    write_region(mapinfo, MetaspaceShared::od, &_od_region, /*read_only=*/true, /*allow_exec=*/false);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1284
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1285
    mapinfo->write_string_regions(_string_regions,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1286
                                  &s0_start, &s0_top, &s0_end,
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1287
                                  &s1_start, &s1_top, &s1_end);
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1288
  }
31345
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1289
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1290
  mapinfo->close();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1291
46324
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1292
  // Restore the vtable in case we invoke any virtual methods.
8764956ec928 8005165: Remove CPU-dependent code in self-patching vtables
iklam
parents: 43410
diff changeset
  1293
  MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list);
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
  1294
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1295
  _s0_region.init(s0_start, s0_top, s0_end);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1296
  _s1_region.init(s1_start, s1_top, s1_end);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1297
  print_region_stats();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1298
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
  1299
  if (log_is_enabled(Info, cds)) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1300
    ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()),
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1301
                                                 int(_mc_region.used()), int(_md_region.used()));
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1302
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1303
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1304
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1305
void VM_PopulateDumpSharedSpace::print_region_stats() {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1306
  // Print statistics of all the regions
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1307
  const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() +
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1308
                                _mc_region.reserved() + _md_region.reserved() +
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1309
                                _od_region.reserved() +
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1310
                                _s0_region.reserved() + _s1_region.reserved();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1311
  const size_t total_bytes = _ro_region.used() + _rw_region.used() +
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1312
                             _mc_region.used() + _md_region.used() +
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1313
                             _od_region.used() +
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1314
                             _s0_region.used() + _s1_region.used();
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1315
  const double total_u_perc = total_bytes / double(total_reserved) * 100.0;
17858
c292f8791cca 8014912: Restore PrintSharedSpaces functionality after NPG
iklam
parents: 17109
diff changeset
  1316
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1317
  _mc_region.print(total_reserved);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1318
  _rw_region.print(total_reserved);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1319
  _ro_region.print(total_reserved);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1320
  _md_region.print(total_reserved);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1321
  _od_region.print(total_reserved);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1322
  _s0_region.print(total_reserved);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1323
  _s1_region.print(total_reserved);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1324
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1325
  tty->print_cr("total   : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1326
                 total_bytes, total_reserved, total_u_perc);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1327
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1328
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1329
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1330
// Update a Java object to point its Klass* to the new location after
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1331
// shared archive has been compacted.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1332
void MetaspaceShared::relocate_klass_ptr(oop o) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1333
  assert(DumpSharedSpaces, "sanity");
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1334
  Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1335
  o->set_klass(k);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1336
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1337
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1338
class LinkSharedClassesClosure : public KlassClosure {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1339
  Thread* THREAD;
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1340
  bool    _made_progress;
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1341
 public:
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1342
  LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1343
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1344
  void reset()               { _made_progress = false; }
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1345
  bool made_progress() const { return _made_progress; }
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1346
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1347
  void do_klass(Klass* k) {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1348
    if (k->is_instance_klass()) {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1349
      InstanceKlass* ik = InstanceKlass::cast(k);
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1350
      // Link the class to cause the bytecodes to be rewritten and the
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1351
      // cpcache to be created. Class verification is done according
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1352
      // to -Xverify setting.
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1353
      _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1354
      guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1355
    }
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1356
  }
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1357
};
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1358
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1359
class CheckSharedClassesClosure : public KlassClosure {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1360
  bool    _made_progress;
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1361
 public:
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1362
  CheckSharedClassesClosure() : _made_progress(false) {}
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1363
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1364
  void reset()               { _made_progress = false; }
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1365
  bool made_progress() const { return _made_progress; }
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1366
  void do_klass(Klass* k) {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1367
    if (k->is_instance_klass() && InstanceKlass::cast(k)->check_sharing_error_state()) {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1368
      _made_progress = true;
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1369
    }
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1370
  }
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1371
};
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1372
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1373
void MetaspaceShared::check_shared_class_loader_type(Klass* k) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1374
  if (k->is_instance_klass()) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1375
    InstanceKlass* ik = InstanceKlass::cast(k);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1376
    u2 loader_type = ik->loader_type();
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1377
    ResourceMark rm;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1378
    guarantee(loader_type != 0,
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1379
              "Class loader type is not set for this class %s", ik->name()->as_C_string());
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1380
  }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1381
}
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1382
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1383
void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1384
  // We need to iterate because verification may cause additional classes
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1385
  // to be loaded.
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1386
  LinkSharedClassesClosure link_closure(THREAD);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1387
  do {
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1388
    link_closure.reset();
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1389
    ClassLoaderDataGraph::loaded_classes_do(&link_closure);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1390
    guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1391
  } while (link_closure.made_progress());
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1392
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1393
  if (_has_error_classes) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1394
    // Mark all classes whose super class or interfaces failed verification.
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1395
    CheckSharedClassesClosure check_closure;
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1396
    do {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1397
      // Not completely sure if we need to do this iteratively. Anyway,
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1398
      // we should come here only if there are unverifiable classes, which
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1399
      // shouldn't happen in normal cases. So better safe than sorry.
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1400
      check_closure.reset();
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1401
      ClassLoaderDataGraph::loaded_classes_do(&check_closure);
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1402
    } while (check_closure.made_progress());
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1403
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1404
    if (IgnoreUnverifiableClassesDuringDump) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1405
      // This is useful when running JCK or SQE tests. You should not
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1406
      // enable this when running real apps.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1407
      SystemDictionary::remove_classes_in_error_state();
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1408
    } else {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1409
      tty->print_cr("Please remove the unverifiable classes from your class list and try again");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1410
      exit(1);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1411
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1412
  }
39713
29ece76096cb 8150752: Share Class Data
iklam
parents: 38943
diff changeset
  1413
29ece76096cb 8150752: Share Class Data
iklam
parents: 38943
diff changeset
  1414
  // Copy the verification constraints from C_HEAP-alloced GrowableArrays to RO-alloced
29ece76096cb 8150752: Share Class Data
iklam
parents: 38943
diff changeset
  1415
  // Arrays
29ece76096cb 8150752: Share Class Data
iklam
parents: 38943
diff changeset
  1416
  SystemDictionaryShared::finalize_verification_constraints();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1417
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1418
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1419
void MetaspaceShared::prepare_for_dumping() {
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1420
  Arguments::check_unsupported_dumping_properties();
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1421
  ClassLoader::initialize_shared_path();
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1422
  FileMapInfo::allocate_classpath_entry_table();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1423
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1424
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1425
// Preload classes from a list, populate the shared spaces and dump to a
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1426
// file.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1427
void MetaspaceShared::preload_and_dump(TRAPS) {
37161
e881f320966e 8150015: Integrate TraceTime with Unified Logging more seamlessly
rehn
parents: 37094
diff changeset
  1428
  { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime));
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1429
    ResourceMark rm;
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1430
    char class_list_path_str[JVM_MAXPATHLEN];
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1431
    // Preload classes to be shared.
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1432
    // Should use some os:: method rather than fopen() here. aB.
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1433
    const char* class_list_path;
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1434
    if (SharedClassListFile == NULL) {
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1435
      // Construct the path to the class list (in jre/lib)
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1436
      // Walk up two directories from the location of the VM and
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1437
      // optionally tack on "lib" (depending on platform)
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1438
      os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1439
      for (int i = 0; i < 3; i++) {
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1440
        char *end = strrchr(class_list_path_str, *os::file_separator());
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1441
        if (end != NULL) *end = '\0';
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1442
      }
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1443
      int class_list_path_len = (int)strlen(class_list_path_str);
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1444
      if (class_list_path_len >= 3) {
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1445
        if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) {
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1446
          if (class_list_path_len < JVM_MAXPATHLEN - 4) {
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1447
            jio_snprintf(class_list_path_str + class_list_path_len,
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1448
                         sizeof(class_list_path_str) - class_list_path_len,
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1449
                         "%slib", os::file_separator());
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1450
            class_list_path_len += 4;
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1451
          }
27471
6e56277909f1 8062370: Various minor code improvements
goetz
parents: 27025
diff changeset
  1452
        }
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1453
      }
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1454
      if (class_list_path_len < JVM_MAXPATHLEN - 10) {
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1455
        jio_snprintf(class_list_path_str + class_list_path_len,
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1456
                     sizeof(class_list_path_str) - class_list_path_len,
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1457
                     "%sclasslist", os::file_separator());
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1458
      }
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1459
      class_list_path = class_list_path_str;
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1460
    } else {
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1461
      class_list_path = SharedClassListFile;
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1462
    }
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1463
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1464
    tty->print_cr("Loading classes to share ...");
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1465
    _has_error_classes = false;
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1466
    int class_count = preload_classes(class_list_path, THREAD);
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1467
    if (ExtraSharedClassListFile) {
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1468
      class_count += preload_classes(ExtraSharedClassListFile, THREAD);
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1469
    }
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1470
    tty->print_cr("Loading classes to share: done.");
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1471
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
  1472
    log_info(cds)("Shared spaces: preloaded %d classes", class_count);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1473
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1474
    // Rewrite and link classes
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1475
    tty->print_cr("Rewriting and linking classes ...");
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1476
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1477
    // Link any classes which got missed. This would happen if we have loaded classes that
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1478
    // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1479
    // fails verification, all other interfaces that were not specified in the classlist but
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1480
    // are implemented by K are not verified.
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1481
    link_and_cleanup_shared_classes(CATCH);
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1482
    tty->print_cr("Rewriting and linking classes: done");
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1483
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1484
    VM_PopulateDumpSharedSpace op;
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1485
    VMThread::execute(&op);
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35231
diff changeset
  1486
  }
46464
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1487
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1488
  if (PrintSystemDictionaryAtExit) {
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1489
    SystemDictionary::print();
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1490
  }
6432a858a220 8180325: Use ClassLoaderData::classes_do for CDS classes
coleenp
parents: 46435
diff changeset
  1491
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1492
  // Since various initialization steps have been undone by this process,
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1493
  // it is not reasonable to continue running a java process.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1494
  exit(0);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1495
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1496
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
  1497
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1498
int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
  1499
  ClassListParser parser(class_list_path);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1500
  int class_count = 0;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1501
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
  1502
    while (parser.parse_one_line()) {
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
  1503
      Klass* klass = ClassLoaderExt::load_one_class(&parser, THREAD);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1504
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1505
      CLEAR_PENDING_EXCEPTION;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1506
      if (klass != NULL) {
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
  1507
        if (log_is_enabled(Trace, cds)) {
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
  1508
          ResourceMark rm;
46522
86b13b03a053 8176132: -XX:+PrintSharedSpaces should be converted to use Unified Logging.
rprotacio
parents: 46464
diff changeset
  1509
          log_trace(cds)("Shared spaces preloaded: %s", klass->external_name());
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1510
        }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1511
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1512
        InstanceKlass* ik = InstanceKlass::cast(klass);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1513
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1514
        // Link the class to cause the bytecodes to be rewritten and the
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1515
        // cpcache to be created. The linking is done as soon as classes
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1516
        // are loaded in order that the related data structures (klass and
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1517
        // cpCache) are located together.
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1518
        try_link_class(ik, THREAD);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1519
        guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1520
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1521
        class_count++;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1522
      }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1523
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1524
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1525
  return class_count;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1526
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1527
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1528
// Returns true if the class's status has changed
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1529
bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1530
  assert(DumpSharedSpaces, "should only be called during dumping");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1531
  if (ik->init_state() < InstanceKlass::linked) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1532
    bool saved = BytecodeVerificationLocal;
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36178
diff changeset
  1533
    if (!(ik->is_shared_boot_class())) {
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1534
      // The verification decision is based on BytecodeVerificationRemote
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1535
      // for non-system classes. Since we are using the NULL classloader
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1536
      // to load non-system classes during dumping, we need to temporarily
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1537
      // change BytecodeVerificationLocal to be the same as
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1538
      // BytecodeVerificationRemote. Note this can cause the parent system
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1539
      // classes also being verified. The extra overhead is acceptable during
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1540
      // dumping.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1541
      BytecodeVerificationLocal = BytecodeVerificationRemote;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1542
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1543
    ik->link_class(THREAD);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1544
    if (HAS_PENDING_EXCEPTION) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1545
      ResourceMark rm;
27618
790a8bf5488b 8064375: Change certain errors to warnings in CDS output.
jiangli
parents: 27025
diff changeset
  1546
      tty->print_cr("Preload Warning: Verification failed for %s",
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1547
                    ik->external_name());
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1548
      CLEAR_PENDING_EXCEPTION;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1549
      ik->set_in_error_state();
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1550
      _has_error_classes = true;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1551
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1552
    BytecodeVerificationLocal = saved;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1553
    return true;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1554
  } else {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1555
    return false;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1556
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1557
}
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1558
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1559
// Closure for serializing initialization data in from a data area
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1560
// (ptr_array) read from the shared file.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1561
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1562
class ReadClosure : public SerializeClosure {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1563
private:
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1564
  intptr_t** _ptr_array;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1565
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1566
  inline intptr_t nextPtr() {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1567
    return *(*_ptr_array)++;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1568
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1569
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1570
public:
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1571
  ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1572
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1573
  void do_ptr(void** p) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1574
    assert(*p == NULL, "initializing previous initialized pointer.");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1575
    intptr_t obj = nextPtr();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1576
    assert((intptr_t)obj >= 0 || (intptr_t)obj < -100,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1577
           "hit tag while initializing ptrs.");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1578
    *p = (void*)obj;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1579
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1580
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1581
  void do_u4(u4* p) {
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1582
    intptr_t obj = nextPtr();
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1583
    *p = (u4)(uintx(obj));
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1584
  }
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1585
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1586
  void do_tag(int tag) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1587
    int old_tag;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1588
    old_tag = (int)(intptr_t)nextPtr();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1589
    // do_int(&old_tag);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1590
    assert(tag == old_tag, "old tag doesn't match");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1591
    FileMapInfo::assert_mark(tag == old_tag);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1592
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1593
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1594
  void do_region(u_char* start, size_t size) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1595
    assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1596
    assert(size % sizeof(intptr_t) == 0, "bad size");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1597
    do_tag((int)size);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1598
    while (size > 0) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1599
      *(intptr_t*)start = nextPtr();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1600
      start += sizeof(intptr_t);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1601
      size -= sizeof(intptr_t);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1602
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1603
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1604
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1605
  bool reading() const { return true; }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1606
};
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1607
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1608
// Return true if given address is in the mapped shared space.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1609
bool MetaspaceShared::is_in_shared_space(const void* p) {
18483
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1610
  return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1611
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1612
37439
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1613
// Return true if given address is in the misc data region
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1614
bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1615
  return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1616
}
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1617
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1618
bool MetaspaceShared::is_in_trampoline_frame(address addr) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1619
  if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1620
    return true;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1621
  }
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1622
  return false;
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1623
}
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1624
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1625
void MetaspaceShared::print_shared_spaces() {
18483
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1626
  if (UseSharedSpaces) {
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1627
    FileMapInfo::current_info()->print_shared_spaces();
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1628
  }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1629
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1630
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1631
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1632
// Map shared spaces at requested addresses and return if succeeded.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1633
bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1634
  size_t image_alignment = mapinfo->alignment();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1635
15100
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1636
#ifndef _WINDOWS
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1637
  // Map in the shared memory and then map the regions on top of it.
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1638
  // On Windows, don't map the memory here because it will cause the
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1639
  // mappings of the regions to fail.
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1640
  ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1641
  if (!shared_rs.is_reserved()) return false;
15100
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1642
#endif
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1643
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1644
  assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1645
18483
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1646
  char* _ro_base = NULL;
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1647
  char* _rw_base = NULL;
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1648
  char* _mc_base = NULL;
18483
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1649
  char* _md_base = NULL;
41182
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents: 39714
diff changeset
  1650
  char* _od_base = NULL;
18483
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17858
diff changeset
  1651
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1652
  // Map each shared region
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1653
  if ((_mc_base = mapinfo->map_region(mc)) != NULL &&
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1654
      mapinfo->verify_region_checksum(mc) &&
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1655
      (_rw_base = mapinfo->map_region(rw)) != NULL &&
31345
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1656
      mapinfo->verify_region_checksum(rw) &&
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1657
      (_ro_base = mapinfo->map_region(ro)) != NULL &&
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1658
      mapinfo->verify_region_checksum(ro) &&
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1659
      (_md_base = mapinfo->map_region(md)) != NULL &&
31345
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1660
      mapinfo->verify_region_checksum(md) &&
41182
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents: 39714
diff changeset
  1661
      (_od_base = mapinfo->map_region(od)) != NULL &&
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents: 39714
diff changeset
  1662
      mapinfo->verify_region_checksum(od) &&
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1663
      (image_alignment == (size_t)os::vm_allocation_granularity()) &&
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1664
      mapinfo->validate_classpath_entry_table()) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1665
    // Success (no need to do anything)
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1666
    return true;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1667
  } else {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1668
    // If there was a failure in mapping any of the spaces, unmap the ones
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1669
    // that succeeded
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1670
    if (_ro_base != NULL) mapinfo->unmap_region(ro);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1671
    if (_rw_base != NULL) mapinfo->unmap_region(rw);
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1672
    if (_mc_base != NULL) mapinfo->unmap_region(mc);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1673
    if (_md_base != NULL) mapinfo->unmap_region(md);
41182
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents: 39714
diff changeset
  1674
    if (_od_base != NULL) mapinfo->unmap_region(od);
15100
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1675
#ifndef _WINDOWS
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1676
    // Release the entire mapped region
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1677
    shared_rs.release();
15100
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14488
diff changeset
  1678
#endif
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1679
    // If -Xshare:on is specified, print out the error message and exit VM,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1680
    // otherwise, set UseSharedSpaces to false and continue.
28017
84009002b566 8066670: PrintSharedArchiveAndExit does not exit the VM when the archive is invalid
iklam
parents: 27619
diff changeset
  1681
    if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1682
      vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1683
    } else {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1684
      FLAG_SET_DEFAULT(UseSharedSpaces, false);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1685
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1686
    return false;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1687
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1688
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1689
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1690
// Read the miscellaneous data from the shared file, and
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1691
// serialize it out to its various destinations.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1692
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1693
void MetaspaceShared::initialize_shared_spaces() {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1694
  FileMapInfo *mapinfo = FileMapInfo::current_info();
37439
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1695
  _cds_i2i_entry_code_buffers = mapinfo->cds_i2i_entry_code_buffers();
e8970711113b 8145221: Use trampolines for i2i and i2c entries in Methods that are stored in CDS archive
ccheung
parents: 37248
diff changeset
  1696
  _cds_i2i_entry_code_buffers_size = mapinfo->cds_i2i_entry_code_buffers_size();
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1697
  _core_spaces_size = mapinfo->core_spaces_size();
35231
e89989198037 8145593: Clean up metaspaceShared.cpp
iklam
parents: 34257
diff changeset
  1698
  char* buffer = mapinfo->misc_data_patching_start();
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1699
  clone_cpp_vtables((intptr_t*)buffer);
34257
4be3504cc03b 8140802: Clean up and refactor of class loading code for CDS
iklam
parents: 33638
diff changeset
  1700
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1701
  // The rest of the data is now stored in the RW region
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1702
  buffer = mapinfo->read_only_tables_start();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1703
  int sharedDictionaryLen = *(intptr_t*)buffer;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1704
  buffer += sizeof(intptr_t);
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
  1705
  int number_of_entries = *(intptr_t*)buffer;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1706
  buffer += sizeof(intptr_t);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1707
  SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1708
                                          sharedDictionaryLen,
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1709
                                          number_of_entries);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1710
  buffer += sharedDictionaryLen;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1711
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1712
  // The following data are the linked list elements
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1713
  // (HashtableEntry objects) for the shared dictionary table.
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1714
28363
047115468f16 8059510: Compact symbol table layout inside shared archive.
jiangli
parents: 28017
diff changeset
  1715
  int len = *(intptr_t*)buffer;     // skip over shared dictionary entries
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1716
  buffer += sizeof(intptr_t);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1717
  buffer += len;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1718
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1719
  // Verify various attributes of the archive, plus initialize the
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1720
  // shared string/symbol tables
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1721
  intptr_t* array = (intptr_t*)buffer;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1722
  ReadClosure rc(&array);
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1723
  serialize(&rc);
37995
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1724
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1725
  // Initialize the run-time symbol table.
92aec042a43b 8150607: Clean up CompactHashtable
iklam
parents: 37439
diff changeset
  1726
  SymbolTable::create_table();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1727
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1728
  // Close the mapinfo file
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1729
  mapinfo->close();
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1730
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1731
  if (PrintSharedArchiveAndExit) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1732
    if (PrintSharedDictionary) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1733
      tty->print_cr("\nShared classes:\n");
46742
24ec8a039c90 8184994: Add Dictionary size logging and jcmd
coleenp
parents: 46625
diff changeset
  1734
      SystemDictionary::print_shared(tty);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1735
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1736
    if (_archive_loading_failed) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1737
      tty->print_cr("archive is invalid");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1738
      vm_exit(1);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1739
    } else {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1740
      tty->print_cr("archive is valid");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1741
      vm_exit(0);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1742
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents: 25468
diff changeset
  1743
  }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1744
}
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1745
31345
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1746
void MetaspaceShared::fixup_shared_string_regions() {
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1747
  FileMapInfo *mapinfo = FileMapInfo::current_info();
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1748
  mapinfo->fixup_string_regions();
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1749
}
1bba15125d8d 8015086: add interned strings to the shared archive.
jiangli
parents: 30773
diff changeset
  1750
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1751
// JVM/TI RedefineClasses() support:
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1752
bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1753
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1754
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1755
  if (UseSharedSpaces) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1756
    // remap the shared readonly space to shared readwrite, private
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1757
    FileMapInfo* mapinfo = FileMapInfo::current_info();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1758
    if (!mapinfo->remap_shared_readonly_as_readwrite()) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1759
      return false;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1760
    }
39714
976b97b59d87 8153312: Constrain AppCDS behavior
jiangli
parents: 39713
diff changeset
  1761
    _remapped_readwrite = true;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1762
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1763
  return true;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
diff changeset
  1764
}
26304
a28e6335ce60 8048150: Allow easy configurations for large CDS archives
ccheung
parents: 26135
diff changeset
  1765
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1766
void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) {
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1767
  // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1768
  // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1769
  // or so.
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1770
  _mc_region.print_out_of_space_msg(name, needed_bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1771
  _rw_region.print_out_of_space_msg(name, needed_bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1772
  _ro_region.print_out_of_space_msg(name, needed_bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1773
  _md_region.print_out_of_space_msg(name, needed_bytes);
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1774
  _od_region.print_out_of_space_msg(name, needed_bytes);
26304
a28e6335ce60 8048150: Allow easy configurations for large CDS archives
ccheung
parents: 26135
diff changeset
  1775
46746
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1776
  vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
ea379ebb9447 8072061: Automatically determine optimal sizes for the CDS regions
iklam
parents: 46742
diff changeset
  1777
                                "Please reduce the number of shared classes.");
26304
a28e6335ce60 8048150: Allow easy configurations for large CDS archives
ccheung
parents: 26135
diff changeset
  1778
}