8224820: ZGC: Support discontiguous heap reservations
authoreosterlund
Tue, 24 Sep 2019 10:04:13 +0000
changeset 58287 a7f16447085e
parent 58286 e4d90117c5de
child 58288 48e480e56aad
8224820: ZGC: Support discontiguous heap reservations Reviewed-by: pliden, stefank
src/hotspot/os/linux/gc/z/zVirtualMemory_linux.cpp
src/hotspot/os/posix/gc/z/zVirtualMemory_posix.cpp
src/hotspot/share/gc/z/zPageAllocator.cpp
src/hotspot/share/gc/z/zVirtualMemory.cpp
src/hotspot/share/gc/z/zVirtualMemory.hpp
--- a/src/hotspot/os/linux/gc/z/zVirtualMemory_linux.cpp	Tue Sep 24 11:49:48 2019 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
- * 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.
- *
- * 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.
- */
-
-#include "precompiled.hpp"
-#include "gc/z/zVirtualMemory.hpp"
-#include "logging/log.hpp"
-
-#include <sys/mman.h>
-#include <sys/types.h>
-
-bool ZVirtualMemoryManager::reserve(uintptr_t start, size_t size) {
-  // Reserve address space
-  const uintptr_t actual_start = (uintptr_t)mmap((void*)start, size, PROT_NONE,
-                                                 MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
-  if (actual_start != start) {
-    log_error(gc)("Failed to reserve address space for Java heap");
-    return false;
-  }
-
-  return true;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hotspot/os/posix/gc/z/zVirtualMemory_posix.cpp	Tue Sep 24 10:04:13 2019 +0000
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+#include "precompiled.hpp"
+#include "gc/z/zAddress.inline.hpp"
+#include "gc/z/zVirtualMemory.hpp"
+#include "logging/log.hpp"
+
+#include <sys/mman.h>
+#include <sys/types.h>
+
+static void unmap(uintptr_t start, size_t size) {
+  const int res = munmap((void*)start, size);
+  assert(res == 0, "Failed to unmap memory");
+}
+
+static bool map(uintptr_t start, size_t size) {
+  const void* const res = mmap((void*)start, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
+  if (res == MAP_FAILED) {
+    // Failed to reserve memory
+    return false;
+  }
+
+  if ((uintptr_t)res != start) {
+    // Failed to reserve memory at the requested address
+    unmap(start, size);
+    return false;
+  }
+
+  // Success
+  return true;
+}
+
+bool ZVirtualMemoryManager::reserve_platform(uintptr_t start, size_t size) {
+  // Reserve address views
+  const uintptr_t marked0 = ZAddress::marked0(start);
+  const uintptr_t marked1 = ZAddress::marked1(start);
+  const uintptr_t remapped = ZAddress::remapped(start);
+
+  if (!map(marked0, size)) {
+    return false;
+  }
+
+  if (!map(marked1, size)) {
+    unmap(marked0, size);
+    return false;
+  }
+
+  if (!map(remapped, size)) {
+    unmap(marked0, size);
+    unmap(marked1, size);
+    return false;
+  }
+
+  // Register address views with native memory tracker
+  nmt_reserve(marked0, size);
+  nmt_reserve(marked1, size);
+  nmt_reserve(remapped, size);
+
+  return true;
+}
--- a/src/hotspot/share/gc/z/zPageAllocator.cpp	Tue Sep 24 11:49:48 2019 +0200
+++ b/src/hotspot/share/gc/z/zPageAllocator.cpp	Tue Sep 24 10:04:13 2019 +0000
@@ -94,7 +94,7 @@
                                size_t max_capacity,
                                size_t max_reserve) :
     _lock(),
-    _virtual(),
+    _virtual(max_capacity),
     _physical(),
     _cache(),
     _min_capacity(min_capacity),
--- a/src/hotspot/share/gc/z/zVirtualMemory.cpp	Tue Sep 24 11:49:48 2019 +0200
+++ b/src/hotspot/share/gc/z/zVirtualMemory.cpp	Tue Sep 24 10:04:13 2019 +0000
@@ -27,28 +27,39 @@
 #include "logging/log.hpp"
 #include "services/memTracker.hpp"
 
-ZVirtualMemoryManager::ZVirtualMemoryManager() :
+ZVirtualMemoryManager::ZVirtualMemoryManager(size_t max_capacity) :
     _manager(),
     _initialized(false) {
 
-  log_info(gc, init)("Address Space: " PTR_FORMAT " - " PTR_FORMAT " (" SIZE_FORMAT "T)",
-                     ZAddressSpaceStart, ZAddressSpaceEnd, ZAddressSpaceSize / K / G);
+  log_info(gc, init)("Address Space: " SIZE_FORMAT "T", ZAddressOffsetMax / K / G);
 
   // Reserve address space
-  if (!reserve(ZAddressSpaceStart, ZAddressSpaceSize)) {
+  if (reserve(0, ZAddressOffsetMax) < max_capacity) {
+    log_error(gc)("Failed to reserve address space for Java heap");
     return;
   }
 
-  // Make the complete address view free
-  _manager.free(0, ZAddressOffsetMax);
-
-  // Register address space with native memory tracker
-  nmt_reserve(ZAddressSpaceStart, ZAddressSpaceSize);
-
   // Successfully initialized
   _initialized = true;
 }
 
+size_t ZVirtualMemoryManager::reserve(uintptr_t start, size_t size) {
+  if (size < ZGranuleSize) {
+    // Too small
+    return 0;
+  }
+
+  if (!reserve_platform(start, size)) {
+    const size_t half = size / 2;
+    return reserve(start, half) + reserve(start + half, half);
+  }
+
+  // Make the address range free
+  _manager.free(start, size);
+
+  return size;
+}
+
 void ZVirtualMemoryManager::nmt_reserve(uintptr_t start, size_t size) {
   MemTracker::record_virtual_memory_reserve((void*)start, size, CALLER_PC);
   MemTracker::record_virtual_memory_type((void*)start, mtJavaHeap);
--- a/src/hotspot/share/gc/z/zVirtualMemory.hpp	Tue Sep 24 11:49:48 2019 +0200
+++ b/src/hotspot/share/gc/z/zVirtualMemory.hpp	Tue Sep 24 10:04:13 2019 +0000
@@ -50,11 +50,12 @@
   ZMemoryManager _manager;
   bool           _initialized;
 
-  bool reserve(uintptr_t start, size_t size);
+  bool reserve_platform(uintptr_t start, size_t size);
+  size_t reserve(uintptr_t start, size_t size);
   void nmt_reserve(uintptr_t start, size_t size);
 
 public:
-  ZVirtualMemoryManager();
+  ZVirtualMemoryManager(size_t max_capacity);
 
   bool is_initialized() const;