1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/z/zPage.inline.hpp"
  26 #include "gc/z/zPhysicalMemory.inline.hpp"
  27 #include "gc/z/zPreMappedMemory.inline.hpp"
  28 #include "gc/z/zVirtualMemory.inline.hpp"
  29 #include "logging/log.hpp"
  30 
  31 ZPreMappedMemory::ZPreMappedMemory(ZVirtualMemoryManager &vmm, ZPhysicalMemoryManager &pmm, size_t size) :
  32     _vmem(),
  33     _pmem(),
  34     _initialized(false) {
  35   if (!vmm.is_initialized() || !pmm.is_initialized()) {
  36     // Not initialized
  37     return;
  38   }
  39 
  40   // Pre-mapping and pre-touching memory can take a long time. Log a message
  41   // to help the user understand why the JVM might seem slow to start.
  42   log_info(gc, init)("Pre-touching: %s", AlwaysPreTouch ? "Enabled" : "Disabled");
  43   log_info(gc, init)("Pre-mapping: " SIZE_FORMAT "M", size / M);
  44 
  45   if (size > 0) {
  46     _pmem = pmm.alloc(size);
  47     if (_pmem.is_null()) {
  48       // Out of memory
  49       log_error(gc, init)("Failed to pre-map Java heap (Cannot allocate physical memory)");
  50       return;
  51     }
  52 
  53     _vmem = vmm.alloc(size, true /* alloc_from_front */);
  54     if (_vmem.is_null()) {
  55       // Out of address space
  56       log_error(gc, init)("Failed to pre-map Java heap (Cannot allocate virtual memory)");
  57       pmm.free(_pmem);
  58       return;
  59     }
  60 
  61     // Map physical memory
  62     pmm.map(_pmem, _vmem.start());
  63   }
  64 
  65   _initialized = true;
  66 }
  67 
  68 ZPage* ZPreMappedMemory::alloc_page(uint8_t type, size_t size) {
  69   if (size > available()) {
  70     // Not enough pre-mapped memory
  71     return NULL;
  72   }
  73 
  74   // Take a chunk of the pre-mapped memory
  75   const ZPhysicalMemory pmem = _pmem.split(size);
  76   const ZVirtualMemory  vmem = _vmem.split(size);
  77 
  78   ZPage* const page = new ZPage(type, vmem, pmem);
  79   page->set_pre_mapped();
  80 
  81   return page;
  82 }
  83 
  84 void ZPreMappedMemory::clear() {
  85   assert(_pmem.is_null(), "Should be detached");
  86   _vmem.clear();
  87 }