hotspot/src/share/vm/memory/filemap.cpp
author goetz
Fri, 04 Jul 2014 11:46:01 +0200
changeset 25715 d5a8dbdc5150
parent 24424 2658d7834c6e
child 26135 82b516c550f7
permissions -rw-r--r--
8049325: Introduce and clean up umbrella headers for the files in the cpu subdirectories. Summary: Introduce and clean up umbrella headers for the files in the cpu subdirectories. Reviewed-by: lfoltan, coleenp, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23186
diff changeset
     2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2131
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2131
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2131
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "classfile/classLoader.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "classfile/symbolTable.hpp"
14577
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
    28
#include "classfile/altHashing.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "memory/filemap.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "runtime/arguments.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "runtime/java.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "runtime/os.hpp"
25715
d5a8dbdc5150 8049325: Introduce and clean up umbrella headers for the files in the cpu subdirectories.
goetz
parents: 24424
diff changeset
    33
#include "runtime/vm_version.hpp"
14120
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 13728
diff changeset
    34
#include "services/memTracker.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    35
#include "utilities/defaultStream.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    36
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
# include <sys/stat.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
# include <errno.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#ifndef O_BINARY       // if defined (Win32) use binary files.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#define O_BINARY 0     // otherwise do nothing.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23186
diff changeset
    44
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
extern address JVM_FunctionAtStart();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
extern address JVM_FunctionAtEnd();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1
diff changeset
    49
// Complain and stop. All error conditions occurring during the writing of
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// an archive file should stop the process.  Unrecoverable errors during
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
// the reading of the archive file should stop the process.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
static void fail(const char *msg, va_list ap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // This occurs very early during initialization: tty is not initialized.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  jio_fprintf(defaultStream::error_stream(),
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1
diff changeset
    56
              "An error has occurred while processing the"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
              " shared archive file.\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  jio_vfprintf(defaultStream::error_stream(), msg, ap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  jio_fprintf(defaultStream::error_stream(), "\n");
19707
fecaabc8e462 8023381: VM fails to initialize in runtime/CDSCompressedKPtrs/XShareAuto.java runtime/SharedArchiveFile/CdsSameObjectAlignment.java
hseigel
parents: 19319
diff changeset
    60
  // Do not change the text of the below message because some tests check for it.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  vm_exit_during_initialization("Unable to use shared archive.", NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
void FileMapInfo::fail_stop(const char *msg, ...) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        va_list ap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  va_start(ap, msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  fail(msg, ap);        // Never returns.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  va_end(ap);           // for completeness.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
// Complain and continue.  Recoverable errors during the reading of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
// archive file may continue (with sharing disabled).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
// If we continue, then disable shared spaces and close the file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
void FileMapInfo::fail_continue(const char *msg, ...) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  va_list ap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  va_start(ap, msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  if (RequireSharedSpaces) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    fail(msg, ap);
23186
9396c6fb571b 8033990: Add message to verbose output to indicate when JVM was unable to use shared archive (with -Xshare:auto)
mseledtsov
parents: 19707
diff changeset
    83
  } else {
9396c6fb571b 8033990: Add message to verbose output to indicate when JVM was unable to use shared archive (with -Xshare:auto)
mseledtsov
parents: 19707
diff changeset
    84
    if (PrintSharedSpaces) {
9396c6fb571b 8033990: Add message to verbose output to indicate when JVM was unable to use shared archive (with -Xshare:auto)
mseledtsov
parents: 19707
diff changeset
    85
      tty->print_cr("UseSharedSpaces: %s", msg);
9396c6fb571b 8033990: Add message to verbose output to indicate when JVM was unable to use shared archive (with -Xshare:auto)
mseledtsov
parents: 19707
diff changeset
    86
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  va_end(ap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  UseSharedSpaces = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
14577
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
    93
// Fill in the fileMapInfo structure with data about this VM instance.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
14577
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
    95
// This method copies the vm version info into header_version.  If the version is too
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
    96
// long then a truncated version, which has a hash code appended to it, is copied.
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
    97
//
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
    98
// Using a template enables this method to verify that header_version is an array of
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
    99
// length JVM_IDENT_MAX.  This ensures that the code that writes to the CDS file and
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   100
// the code that reads the CDS file will both use the same size buffer.  Hence, will
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   101
// use identical truncation.  This is necessary for matching of truncated versions.
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   102
template <int N> static void get_header_version(char (&header_version) [N]) {
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   103
  assert(N == JVM_IDENT_MAX, "Bad header_version size");
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   104
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   105
  const char *vm_version = VM_Version::internal_vm_info_string();
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   106
  const int version_len = (int)strlen(vm_version);
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   107
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   108
  if (version_len < (JVM_IDENT_MAX-1)) {
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   109
    strcpy(header_version, vm_version);
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   110
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   111
  } else {
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   112
    // Get the hash value.  Use a static seed because the hash needs to return the same
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   113
    // value over multiple jvm invocations.
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   114
    unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   115
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   116
    // Truncate the ident, saving room for the 8 hex character hash value.
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   117
    strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   118
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   119
    // Append the hash code as eight hex digits.
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   120
    sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   121
    header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   122
  }
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   123
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
void FileMapInfo::populate_header(size_t alignment) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  _header._magic = 0xf00baba2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  _header._version = _current_version;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  _header._alignment = alignment;
15101
58d43bf04c45 8005076: Creating a CDS archive with one alignment and running another causes a crash.
hseigel
parents: 15100
diff changeset
   129
  _header._obj_alignment = ObjectAlignmentInBytes;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  // The following fields are for sanity checks for whether this archive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // will function correctly with this JVM and the bootclasspath it's
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  // invoked with.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // JVM version string ... changes on each build.
14577
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   136
  get_header_version(_header._jvm_ident);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // Build checks on classpath and jar files
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  _header._num_jars = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  for ( ; cpe != NULL; cpe = cpe->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    if (cpe->is_jar_file()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
        fail_stop("Too many jar files to share.", NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
      // Jar file - record timestamp and file size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      struct stat st;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      const char *path = cpe->name();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
      if (os::stat(path, &st) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        // If we can't access a jar file in the boot path, then we can't
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        // make assumptions about where classes get loaded from.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        fail_stop("Unable to open jar file %s.", path);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
      _header._jar[_header._num_jars]._timestamp = st.st_mtime;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      _header._jar[_header._num_jars]._filesize = st.st_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      _header._num_jars++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      // If directories appear in boot classpath, they must be empty to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      // avoid having to verify each individual class file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      const char* name = ((ClassPathDirEntry*)cpe)->name();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      if (!os::dir_is_empty(name)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
        fail_stop("Boot classpath directory %s is not empty.", name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
// Read the FileMapInfo information from the file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
bool FileMapInfo::init_from_file(int fd) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  size_t n = read(fd, &_header, sizeof(struct FileMapHeader));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  if (n != sizeof(struct FileMapHeader)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    fail_continue("Unable to read the file header.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  if (_header._version != current_version()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    fail_continue("The shared archive file has the wrong version.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  _file_offset = (long)n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
// Read the FileMapInfo information from the file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
bool FileMapInfo::open_for_read() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  _full_path = Arguments::GetSharedArchivePath();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  if (fd < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    if (errno == ENOENT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      // Not locating the shared archive is ok.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
      fail_continue("Specified shared archive not found.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      fail_continue("Failed to open shared archive file (%s).",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
                    strerror(errno));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  _fd = fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  _file_open = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
// Write the FileMapInfo information to the file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
void FileMapInfo::open_for_write() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
 _full_path = Arguments::GetSharedArchivePath();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  if (PrintSharedSpaces) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    tty->print_cr("Dumping shared data to file: ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    tty->print_cr("   %s", _full_path);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
15461
e3acbfdabafa 7197672: There are issues with shared data on windows
hseigel
parents: 15101
diff changeset
   220
#ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
e3acbfdabafa 7197672: There are issues with shared data on windows
hseigel
parents: 15101
diff changeset
   221
  chmod(_full_path, _S_IREAD | _S_IWRITE);
e3acbfdabafa 7197672: There are issues with shared data on windows
hseigel
parents: 15101
diff changeset
   222
#endif
e3acbfdabafa 7197672: There are issues with shared data on windows
hseigel
parents: 15101
diff changeset
   223
e3acbfdabafa 7197672: There are issues with shared data on windows
hseigel
parents: 15101
diff changeset
   224
  // Use remove() to delete the existing file because, on Unix, this will
e3acbfdabafa 7197672: There are issues with shared data on windows
hseigel
parents: 15101
diff changeset
   225
  // allow processes that have it open continued access to the file.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  remove(_full_path);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  if (fd < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    fail_stop("Unable to create shared archive file %s.", _full_path);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  _fd = fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  _file_offset = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  _file_open = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
// Write the header to the file, seek to the next allocation boundary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
void FileMapInfo::write_header() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  write_bytes_aligned(&_header, sizeof(FileMapHeader));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
// Dump shared spaces to file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   246
void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  align_file_position();
17109
90e6c31bbbe4 8008966: NPG: Inefficient Metaspace counter functions cause large young GC regressions
jmasa
parents: 16430
diff changeset
   248
  size_t used = space->used_bytes_slow(Metaspace::NonClassType);
90e6c31bbbe4 8008966: NPG: Inefficient Metaspace counter functions cause large young GC regressions
jmasa
parents: 16430
diff changeset
   249
  size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   251
  write_region(i, (char*)space->bottom(), used, capacity, read_only, false);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
// Dump region to file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
void FileMapInfo::write_region(int region, char* base, size_t size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
                               size_t capacity, bool read_only,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
                               bool allow_exec) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  if (_file_open) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    if (PrintSharedSpaces) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   265
      tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   266
                    " file offset 0x%6x", region, size, base, _file_offset);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    si->_file_offset = _file_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  si->_base = base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  si->_used = size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  si->_capacity = capacity;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  si->_read_only = read_only;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  si->_allow_exec = allow_exec;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  write_bytes_aligned(base, (int)size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
// Dump bytes to file -- at the current file position.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  if (_file_open) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
    int n = ::write(_fd, buffer, nbytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    if (n != nbytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
      // It is dangerous to leave the corrupted shared archive file around,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      // close and remove the file. See bug 6372906.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
      close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      remove(_full_path);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      fail_stop("Unable to write to shared archive file.", NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  _file_offset += nbytes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
// Align file position to an allocation unit boundary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
void FileMapInfo::align_file_position() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  if (new_file_offset != _file_offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    _file_offset = new_file_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    if (_file_open) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
      // Seek one byte back from the target and write a byte to insure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
      // that the written file is the correct length.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      _file_offset -= 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
      if (lseek(_fd, _file_offset, SEEK_SET) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
        fail_stop("Unable to seek.", NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
      char zero = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
      write_bytes(&zero, 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
// Dump bytes to file -- at the current file position.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  align_file_position();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  write_bytes(buffer, nbytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  align_file_position();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
// Close the shared archive file.  This does NOT unmap mapped regions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
void FileMapInfo::close() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  if (_file_open) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    if (::close(_fd) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
      fail_stop("Unable to close the shared archive file.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    _file_open = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    _fd = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
// JVM/TI RedefineClasses() support:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
// Remap the shared readonly space to shared readwrite, private.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
bool FileMapInfo::remap_shared_readonly_as_readwrite() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  if (!si->_read_only) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    // the space is already readwrite so we are done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  size_t used = si->_used;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  size_t size = align_size_up(used, os::vm_allocation_granularity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  if (!open_for_read()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
                                si->_base, size, false /* !read_only */,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
                                si->_allow_exec);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  if (base == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  if (base != si->_base) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    fail_continue("Unable to remap shared readonly space at required address.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  si->_read_only = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   368
// Map the whole region at once, assumed to be allocated contiguously.
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   369
ReservedSpace FileMapInfo::reserve_shared_memory() {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   370
  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   371
  char* requested_addr = si->_base;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   372
19319
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   373
  size_t size = FileMapInfo::shared_spaces_size();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   374
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   375
  // Reserve the space first, then map otherwise map will go right over some
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   376
  // other reserved memory (like the code cache).
19319
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   377
  ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   378
  if (!rs.is_reserved()) {
16430
882cddc35bec 8008217: CDS: Class data sharing limits the malloc heap on Solaris
coleenp
parents: 15461
diff changeset
   379
    fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr));
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   380
    return rs;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   381
  }
14120
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 13728
diff changeset
   382
  // the reserved virtual memory is for mapping class data sharing archive
15100
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14577
diff changeset
   383
  MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14577
diff changeset
   384
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   385
  return rs;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   386
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
// Memory map a region in the address space.
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   389
static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   391
char* FileMapInfo::map_region(int i) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  size_t used = si->_used;
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   394
  size_t alignment = os::vm_allocation_granularity();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   395
  size_t size = align_size_up(used, alignment);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   396
  char *requested_addr = si->_base;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   397
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   398
  // map the contents of the CDS archive in this memory
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  char *base = os::map_memory(_fd, _full_path, si->_file_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
                              requested_addr, size, si->_read_only,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
                              si->_allow_exec);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   402
  if (base == NULL || base != si->_base) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   403
    fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i]));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  }
15100
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14577
diff changeset
   406
#ifdef _WINDOWS
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14577
diff changeset
   407
  // This call is Windows-only because the memory_type gets recorded for the other platforms
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14577
diff changeset
   408
  // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14577
diff changeset
   409
  MemTracker::record_virtual_memory_type((address)base, mtClassShared);
0ae85ac7c8b0 8003705: CDS failed on Windows: can not map in the CDS.
hseigel
parents: 14577
diff changeset
   410
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  return base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
// Unmap a memory region in the address space.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
void FileMapInfo::unmap_region(int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  size_t used = si->_used;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  size_t size = align_size_up(used, os::vm_allocation_granularity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  if (!os::unmap_memory(si->_base, size)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
    fail_stop("Unable to unmap shared space.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
void FileMapInfo::assert_mark(bool check) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  if (!check) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
    fail_stop("Mark mismatch while restoring from shared file.", NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
FileMapInfo* FileMapInfo::_current_info = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
// Open the shared archive file, read and validate the header
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
// information (version, boot classpath, etc.).  If initialization
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
// fails, shared spaces are disabled and the file is closed. [See
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
// fail_continue.]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
bool FileMapInfo::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  assert(UseSharedSpaces, "UseSharedSpaces expected.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
    fail_continue("Tool agent requires sharing to be disabled.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  if (!open_for_read()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  init_from_file(_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  if (!validate()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  SharedReadOnlySize =  _header._space[0]._capacity;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  SharedReadWriteSize = _header._space[1]._capacity;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  SharedMiscDataSize =  _header._space[2]._capacity;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  SharedMiscCodeSize =  _header._space[3]._capacity;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
bool FileMapInfo::validate() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  if (_header._version != current_version()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
    fail_continue("The shared archive file is the wrong version.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  if (_header._magic != (int)0xf00baba2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    fail_continue("The shared archive file has a bad magic number.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  }
14577
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   475
  char header_version[JVM_IDENT_MAX];
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   476
  get_header_version(header_version);
fd68d810d86b 6924920: Class Data Sharing limit on the java version string can create failures
hseigel
parents: 14120
diff changeset
   477
  if (strncmp(_header._jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
    fail_continue("The shared archive file was created by a different"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
                  " version or build of HotSpot.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  }
15101
58d43bf04c45 8005076: Creating a CDS archive with one alignment and running another causes a crash.
hseigel
parents: 15100
diff changeset
   482
  if (_header._obj_alignment != ObjectAlignmentInBytes) {
58d43bf04c45 8005076: Creating a CDS archive with one alignment and running another causes a crash.
hseigel
parents: 15100
diff changeset
   483
    fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
58d43bf04c45 8005076: Creating a CDS archive with one alignment and running another causes a crash.
hseigel
parents: 15100
diff changeset
   484
                  " does not equal the current ObjectAlignmentInBytes of %d.",
58d43bf04c45 8005076: Creating a CDS archive with one alignment and running another causes a crash.
hseigel
parents: 15100
diff changeset
   485
                  _header._obj_alignment, ObjectAlignmentInBytes);
58d43bf04c45 8005076: Creating a CDS archive with one alignment and running another causes a crash.
hseigel
parents: 15100
diff changeset
   486
    return false;
58d43bf04c45 8005076: Creating a CDS archive with one alignment and running another causes a crash.
hseigel
parents: 15100
diff changeset
   487
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  // Cannot verify interpreter yet, as it can only be created after the GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  // heap has been initialized.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
    fail_continue("Too many jar files to share.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  // Build checks on classpath and jar files
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  int num_jars_now = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  for ( ; cpe != NULL; cpe = cpe->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
    if (cpe->is_jar_file()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
      if (num_jars_now < _header._num_jars) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
        // Jar file - verify timestamp and file size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
        struct stat st;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
        const char *path = cpe->name();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
        if (os::stat(path, &st) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
          fail_continue("Unable to open jar file %s.", path);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
        if (_header._jar[num_jars_now]._timestamp != st.st_mtime ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
            _header._jar[num_jars_now]._filesize != st.st_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
          fail_continue("A jar file is not the one used while building"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
                        " the shared archive file.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
      ++num_jars_now;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
      // If directories appear in boot classpath, they must be empty to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
      // avoid having to verify each individual class file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
      const char* name = ((ClassPathDirEntry*)cpe)->name();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
      if (!os::dir_is_empty(name)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
        fail_continue("Boot classpath directory %s is not empty.", name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  if (num_jars_now < _header._num_jars) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
    fail_continue("The number of jar files in the boot classpath is"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
                  " less than the number the shared archive was created with.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
// The following method is provided to see whether a given pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
// falls in the mapped shared space.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
// Param:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
// p, The given pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
// Return:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
// True if the p is within the mapped shared space, otherwise, false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
bool FileMapInfo::is_in_shared_space(const void* p) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 7405
diff changeset
   547
  for (int i = 0; i < MetaspaceShared::n_regions; i++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
    if (p >= _header._space[i]._base &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
        p < _header._space[i]._base + _header._space[i]._used) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
}
18483
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   556
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   557
void FileMapInfo::print_shared_spaces() {
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   558
  gclog_or_tty->print_cr("Shared Spaces:");
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   559
  for (int i = 0; i < MetaspaceShared::n_regions; i++) {
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   560
    struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   561
    gclog_or_tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   562
                        shared_region_name[i],
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   563
                        si->_base, si->_base + si->_used);
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   564
  }
c021907fa0a7 8016075: Win32 crash with CDS enabled and small heap size
iklam
parents: 17109
diff changeset
   565
}
19319
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   566
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   567
// Unmap mapped regions of shared space.
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   568
void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   569
  FileMapInfo *map_info = FileMapInfo::current_info();
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   570
  if (map_info) {
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   571
    map_info->fail_continue(msg);
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   572
    for (int i = 0; i < MetaspaceShared::n_regions; i++) {
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   573
      if (map_info->_header._space[i]._base != NULL) {
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   574
        map_info->unmap_region(i);
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   575
        map_info->_header._space[i]._base = NULL;
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   576
      }
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   577
    }
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   578
  } else if (DumpSharedSpaces) {
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   579
    fail_stop(msg, NULL);
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   580
  }
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 18483
diff changeset
   581
}