filemap.hpp 5.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_MEMORY_FILEMAP_HPP
#define SHARE_VM_MEMORY_FILEMAP_HPP

28
#include "memory/metaspaceShared.hpp"
29
#include "memory/metaspace.hpp"
30

D
duke 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// Layout of the file:
//  header: dump of archive instance plus versioning info, datestamp, etc.
//   [magic # = 0xF00BABA2]
//  ... padding to align on page-boundary
//  read-write space from CompactingPermGenGen
//  read-only space from CompactingPermGenGen
//  misc data (block offset table, string table, symbols, dictionary, etc.)
//  tag(666)

static const int JVM_SHARED_JARS_MAX = 128;
static const int JVM_SPACENAME_MAX = 128;
static const int JVM_IDENT_MAX = 256;
static const int JVM_ARCH_MAX = 12;


46
class Metaspace;
D
duke 已提交
47

Z
zgu 已提交
48
class FileMapInfo : public CHeapObj<mtInternal> {
D
duke 已提交
49 50 51 52 53 54 55 56
private:
  enum {
    _invalid_version = -1,
    _current_version = 1
  };

  bool  _file_open;
  int   _fd;
J
jiangli 已提交
57
  size_t  _file_offset;
D
duke 已提交
58 59 60 61 62 63 64

  // FileMapHeader describes the shared space data in the file to be
  // mapped.  This structure gets written to a file.  It is not a class, so
  // that the compilers don't add any compiler-private data to it.

  struct FileMapHeader {
    int    _magic;                    // identify file type.
J
jiangli 已提交
65
    int    _crc;                      // header crc checksum.
D
duke 已提交
66 67
    int    _version;                  // (from enum, above.)
    size_t _alignment;                // how shared archive should be aligned
68
    int    _obj_alignment;            // value of ObjectAlignmentInBytes
D
duke 已提交
69 70

    struct space_info {
J
jiangli 已提交
71 72
      int    _crc;           // crc checksum of the current space
      size_t _file_offset;   // sizeof(this) rounded to vm page size
D
duke 已提交
73 74 75 76 77
      char*  _base;          // copy-on-write base address
      size_t _capacity;      // for validity checking
      size_t _used;          // for setting space top on read
      bool   _read_only;     // read only space?
      bool   _allow_exec;    // executable code in space?
78
    } _space[MetaspaceShared::n_regions];
D
duke 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    // The following fields are all sanity checks for whether this archive
    // will function correctly with this JVM and the bootclasspath it's
    // invoked with.
    char  _arch[JVM_ARCH_MAX];            // architecture
    char  _jvm_ident[JVM_IDENT_MAX];      // identifier for jvm
    int   _num_jars;              // Number of jars in bootclasspath

    // Per jar file data:  timestamp, size.

    struct {
      time_t _timestamp;          // jar timestamp.
      long   _filesize;           // jar file size.
    } _jar[JVM_SHARED_JARS_MAX];
  } _header;
  const char* _full_path;

  static FileMapInfo* _current_info;

  bool  init_from_file(int fd);
  void  align_file_position();

public:
  FileMapInfo() {
    _file_offset = 0;
    _file_open = false;
    _header._version = _invalid_version;
  }

  static int current_version()        { return _current_version; }
J
jiangli 已提交
109 110
  int    compute_header_crc();
  void   set_header_crc(int crc)      { _header._crc = crc; }
D
duke 已提交
111 112 113 114 115 116 117 118 119
  void   populate_header(size_t alignment);
  bool   validate();
  void   invalidate();
  int    version()                    { return _header._version; }
  size_t alignment()                  { return _header._alignment; }
  size_t space_capacity(int i)        { return _header._space[i]._capacity; }
  char*  region_base(int i)           { return _header._space[i]._base; }
  struct FileMapHeader* header()      { return &_header; }

120 121 122 123 124 125 126 127 128
  static void set_current_info(FileMapInfo* info) {
    CDS_ONLY(_current_info = info;)
  }

  static FileMapInfo* current_info() {
    CDS_ONLY(return _current_info;)
    NOT_CDS(return NULL;)
  }

D
duke 已提交
129 130 131
  static void assert_mark(bool check);

  // File manipulation.
132
  bool  initialize() NOT_CDS_RETURN_(false);
D
duke 已提交
133 134 135
  bool  open_for_read();
  void  open_for_write();
  void  write_header();
136
  void  write_space(int i, Metaspace* space, bool read_only);
D
duke 已提交
137 138 139 140
  void  write_region(int region, char* base, size_t size,
                     size_t capacity, bool read_only, bool allow_exec);
  void  write_bytes(const void* buffer, int count);
  void  write_bytes_aligned(const void* buffer, int count);
141
  char* map_region(int i);
D
duke 已提交
142
  void  unmap_region(int i);
J
jiangli 已提交
143
  bool  verify_region_checksum(int i);
D
duke 已提交
144 145
  void  close();
  bool  is_open() { return _file_open; }
146
  ReservedSpace reserve_shared_memory();
D
duke 已提交
147 148 149 150 151 152 153 154 155 156

  // JVM/TI RedefineClasses() support:
  // Remap the shared readonly space to shared readwrite, private.
  bool  remap_shared_readonly_as_readwrite();

  // Errors.
  static void fail_stop(const char *msg, ...);
  void fail_continue(const char *msg, ...);

  // Return true if given address is in the mapped shared space.
157
  bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false);
158
  void print_shared_spaces() NOT_CDS_RETURN;
159 160 161 162 163 164 165 166 167

  static size_t shared_spaces_size() {
    return align_size_up(SharedReadOnlySize + SharedReadWriteSize +
                         SharedMiscDataSize + SharedMiscCodeSize,
                         os::vm_allocation_granularity());
  }

  // Stop CDS sharing and unmap CDS regions.
  static void stop_sharing_and_unmap(const char* msg);
D
duke 已提交
168
};
169 170

#endif // SHARE_VM_MEMORY_FILEMAP_HPP