author | stefank |
Mon, 28 Oct 2019 11:26:53 +0100 | |
changeset 58814 | bfb419c66ae9 |
parent 58787 | 32d39d9525f9 |
permissions | -rw-r--r-- |
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
1 |
/* |
54834
39ba09047e19
8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents:
54617
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
4 |
* |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
8 |
* |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
13 |
* accompanied this code). |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
14 |
* |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
18 |
* |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
21 |
* questions. |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
22 |
*/ |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
23 |
|
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
24 |
#include "precompiled.hpp" |
58787 | 25 |
#include "gc/z/zAddressSpaceLimit.hpp" |
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
26 |
#include "gc/z/zGlobals.hpp" |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
27 |
#include "gc/z/zVirtualMemory.inline.hpp" |
54617 | 28 |
#include "logging/log.hpp" |
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
29 |
#include "services/memTracker.hpp" |
58787 | 30 |
#include "utilities/debug.hpp" |
31 |
#include "utilities/align.hpp" |
|
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
32 |
|
58287
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
33 |
ZVirtualMemoryManager::ZVirtualMemoryManager(size_t max_capacity) : |
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
34 |
_manager(), |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
35 |
_initialized(false) { |
54617 | 36 |
|
58468
97cd0aa39787
8231825: ZGC: Remove ZMaxHeapSize and ZMaxHeapSizeShift
pliden
parents:
58287
diff
changeset
|
37 |
// Check max supported heap size |
97cd0aa39787
8231825: ZGC: Remove ZMaxHeapSize and ZMaxHeapSizeShift
pliden
parents:
58287
diff
changeset
|
38 |
if (max_capacity > ZAddressOffsetMax) { |
97cd0aa39787
8231825: ZGC: Remove ZMaxHeapSize and ZMaxHeapSizeShift
pliden
parents:
58287
diff
changeset
|
39 |
log_error(gc)("Java heap too large (max supported heap size is " SIZE_FORMAT "G)", |
97cd0aa39787
8231825: ZGC: Remove ZMaxHeapSize and ZMaxHeapSizeShift
pliden
parents:
58287
diff
changeset
|
40 |
ZAddressOffsetMax / G); |
97cd0aa39787
8231825: ZGC: Remove ZMaxHeapSize and ZMaxHeapSizeShift
pliden
parents:
58287
diff
changeset
|
41 |
return; |
97cd0aa39787
8231825: ZGC: Remove ZMaxHeapSize and ZMaxHeapSizeShift
pliden
parents:
58287
diff
changeset
|
42 |
} |
97cd0aa39787
8231825: ZGC: Remove ZMaxHeapSize and ZMaxHeapSizeShift
pliden
parents:
58287
diff
changeset
|
43 |
|
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
44 |
// Reserve address space |
58787 | 45 |
if (!reserve(max_capacity)) { |
46 |
log_error(gc)("Failed to reserve enough address space for Java heap"); |
|
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
47 |
return; |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
48 |
} |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
49 |
|
58814
bfb419c66ae9
8232650: ZGC: Add initialization hooks for OS specific code
stefank
parents:
58787
diff
changeset
|
50 |
// Initialize OS specific parts |
bfb419c66ae9
8232650: ZGC: Add initialization hooks for OS specific code
stefank
parents:
58787
diff
changeset
|
51 |
initialize_os(); |
bfb419c66ae9
8232650: ZGC: Add initialization hooks for OS specific code
stefank
parents:
58787
diff
changeset
|
52 |
|
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
53 |
// Successfully initialized |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
54 |
_initialized = true; |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
55 |
} |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
56 |
|
58787 | 57 |
size_t ZVirtualMemoryManager::reserve_discontiguous(uintptr_t start, size_t size, size_t min_range) { |
58 |
if (size < min_range) { |
|
59 |
// Too small |
|
60 |
return 0; |
|
61 |
} |
|
62 |
||
63 |
assert(is_aligned(size, ZGranuleSize), "Misaligned"); |
|
64 |
||
65 |
if (reserve_contiguous_platform(start, size)) { |
|
66 |
// Make the address range free |
|
67 |
_manager.free(start, size); |
|
68 |
return size; |
|
69 |
} |
|
70 |
||
71 |
const size_t half = size / 2; |
|
72 |
if (half < min_range) { |
|
58287
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
73 |
// Too small |
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
74 |
return 0; |
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
75 |
} |
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
76 |
|
58787 | 77 |
// Divide and conquer |
78 |
const size_t first_part = align_down(half, ZGranuleSize); |
|
79 |
const size_t second_part = size - first_part; |
|
80 |
return reserve_discontiguous(start, first_part, min_range) + |
|
81 |
reserve_discontiguous(start + first_part, second_part, min_range); |
|
82 |
} |
|
83 |
||
84 |
size_t ZVirtualMemoryManager::reserve_discontiguous(size_t size) { |
|
85 |
// Don't try to reserve address ranges smaller than 1% of the requested size. |
|
86 |
// This avoids an explosion of reservation attempts in case large parts of the |
|
87 |
// address space is already occupied. |
|
88 |
const size_t min_range = align_up(size / 100, ZGranuleSize); |
|
89 |
size_t start = 0; |
|
90 |
size_t reserved = 0; |
|
91 |
||
92 |
// Reserve size somewhere between [0, ZAddressOffsetMax) |
|
93 |
while (reserved < size && start < ZAddressOffsetMax) { |
|
94 |
const size_t remaining = MIN2(size - reserved, ZAddressOffsetMax - start); |
|
95 |
reserved += reserve_discontiguous(start, remaining, min_range); |
|
96 |
start += remaining; |
|
58287
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
97 |
} |
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
98 |
|
58787 | 99 |
return reserved; |
100 |
} |
|
101 |
||
102 |
bool ZVirtualMemoryManager::reserve_contiguous(size_t size) { |
|
103 |
// Allow at most 8192 attempts spread evenly across [0, ZAddressOffsetMax) |
|
104 |
const size_t end = ZAddressOffsetMax - size; |
|
105 |
const size_t increment = align_up(end / 8192, ZGranuleSize); |
|
106 |
||
107 |
for (size_t start = 0; start <= end; start += increment) { |
|
108 |
if (reserve_contiguous_platform(start, size)) { |
|
109 |
// Make the address range free |
|
110 |
_manager.free(start, size); |
|
111 |
||
112 |
// Success |
|
113 |
return true; |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
// Failed |
|
118 |
return false; |
|
119 |
} |
|
58287
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
120 |
|
58787 | 121 |
bool ZVirtualMemoryManager::reserve(size_t max_capacity) { |
122 |
const size_t limit = MIN2(ZAddressOffsetMax, ZAddressSpaceLimit::heap_view()); |
|
123 |
const size_t size = MIN2(max_capacity * ZVirtualToPhysicalRatio, limit); |
|
124 |
||
125 |
size_t reserved = size; |
|
126 |
bool contiguous = true; |
|
127 |
||
128 |
// Prefer a contiguous address space |
|
129 |
if (!reserve_contiguous(size)) { |
|
130 |
// Fall back to a discontiguous address space |
|
131 |
reserved = reserve_discontiguous(size); |
|
132 |
contiguous = false; |
|
133 |
} |
|
134 |
||
135 |
log_info(gc, init)("Address Space Type: %s/%s/%s", |
|
136 |
(contiguous ? "Contiguous" : "Discontiguous"), |
|
137 |
(limit == ZAddressOffsetMax ? "Unrestricted" : "Restricted"), |
|
138 |
(reserved == size ? "Complete" : "Degraded")); |
|
139 |
log_info(gc, init)("Address Space Size: " SIZE_FORMAT "M x " SIZE_FORMAT " = " SIZE_FORMAT "M", |
|
140 |
reserved / M, ZHeapViews, (reserved * ZHeapViews) / M); |
|
141 |
||
142 |
return reserved >= max_capacity; |
|
58287
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
143 |
} |
a7f16447085e
8224820: ZGC: Support discontiguous heap reservations
eosterlund
parents:
54834
diff
changeset
|
144 |
|
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
145 |
void ZVirtualMemoryManager::nmt_reserve(uintptr_t start, size_t size) { |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
146 |
MemTracker::record_virtual_memory_reserve((void*)start, size, CALLER_PC); |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
147 |
MemTracker::record_virtual_memory_type((void*)start, mtJavaHeap); |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
148 |
} |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
149 |
|
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
150 |
bool ZVirtualMemoryManager::is_initialized() const { |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
151 |
return _initialized; |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
152 |
} |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
153 |
|
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
154 |
ZVirtualMemory ZVirtualMemoryManager::alloc(size_t size, bool alloc_from_front) { |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
155 |
uintptr_t start; |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
156 |
|
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
157 |
if (alloc_from_front || size <= ZPageSizeSmall) { |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
158 |
// Small page |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
159 |
start = _manager.alloc_from_front(size); |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
160 |
} else { |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
161 |
// Medium/Large page |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
162 |
start = _manager.alloc_from_back(size); |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
163 |
} |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
164 |
|
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
165 |
return ZVirtualMemory(start, size); |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
166 |
} |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
167 |
|
54834
39ba09047e19
8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents:
54617
diff
changeset
|
168 |
void ZVirtualMemoryManager::free(const ZVirtualMemory& vmem) { |
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
169 |
_manager.free(vmem.start(), vmem.size()); |
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff
changeset
|
170 |
} |