annotate src/share/vm/gc_implementation/shared/mutableNUMASpace.cpp @ 141:fcbfc50865ab

6684395: Port NUMA-aware allocator to linux Summary: NUMA-aware allocator port to Linux Reviewed-by: jmasa, apetrusenko
author iveresov
date Tue, 29 Apr 2008 13:51:26 +0400
parents a61af66fc99e
children e3729351c946
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1
a61af66fc99e Initial load
duke
parents:
diff changeset
2 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
5 *
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
8 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
9 *
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
14 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
15 *
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
19 *
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
22 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
23 *
a61af66fc99e Initial load
duke
parents:
diff changeset
24 */
a61af66fc99e Initial load
duke
parents:
diff changeset
25
a61af66fc99e Initial load
duke
parents:
diff changeset
26 # include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27 # include "incls/_mutableNUMASpace.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 MutableNUMASpace::MutableNUMASpace() {
a61af66fc99e Initial load
duke
parents:
diff changeset
31 _lgrp_spaces = new (ResourceObj::C_HEAP) GrowableArray<LGRPSpace*>(0, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
32 _page_size = os::vm_page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
33 _adaptation_cycles = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 _samples_count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 update_layout(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
36 }
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 MutableNUMASpace::~MutableNUMASpace() {
a61af66fc99e Initial load
duke
parents:
diff changeset
39 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
40 delete lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
41 }
a61af66fc99e Initial load
duke
parents:
diff changeset
42 delete lgrp_spaces();
a61af66fc99e Initial load
duke
parents:
diff changeset
43 }
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 void MutableNUMASpace::mangle_unused_area() {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
47 LGRPSpace *ls = lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
48 MutableSpace *s = ls->space();
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
49 if (!os::numa_has_static_binding()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
50 HeapWord *top = MAX2((HeapWord*)round_down((intptr_t)s->top(), page_size()), s->bottom());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
51 if (top < s->end()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
52 ls->add_invalid_region(MemRegion(top, s->end()));
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
53 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55 s->mangle_unused_area();
a61af66fc99e Initial load
duke
parents:
diff changeset
56 }
a61af66fc99e Initial load
duke
parents:
diff changeset
57 }
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // There may be unallocated holes in the middle chunks
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // that should be filled with dead objects to ensure parseability.
a61af66fc99e Initial load
duke
parents:
diff changeset
61 void MutableNUMASpace::ensure_parsability() {
a61af66fc99e Initial load
duke
parents:
diff changeset
62 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
63 LGRPSpace *ls = lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
64 MutableSpace *s = ls->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
65 if (!s->contains(top())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 if (s->free_in_words() > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 SharedHeap::fill_region_with_object(MemRegion(s->top(), s->end()));
a61af66fc99e Initial load
duke
parents:
diff changeset
68 size_t area_touched_words = pointer_delta(s->end(), s->top(), sizeof(HeapWordSize));
a61af66fc99e Initial load
duke
parents:
diff changeset
69 #ifndef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
70 if (!ZapUnusedHeapArea) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 area_touched_words = MIN2((size_t)align_object_size(typeArrayOopDesc::header_size(T_INT)),
a61af66fc99e Initial load
duke
parents:
diff changeset
72 area_touched_words);
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74 #endif
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
75 if (!os::numa_has_static_binding()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
76 MemRegion invalid;
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
77 HeapWord *crossing_start = (HeapWord*)round_to((intptr_t)s->top(), os::vm_page_size());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
78 HeapWord *crossing_end = (HeapWord*)round_to((intptr_t)(s->top() + area_touched_words),
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
79 os::vm_page_size());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
80 if (crossing_start != crossing_end) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
81 // If object header crossed a small page boundary we mark the area
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
82 // as invalid rounding it to a page_size().
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
83 HeapWord *start = MAX2((HeapWord*)round_down((intptr_t)s->top(), page_size()), s->bottom());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
84 HeapWord *end = MIN2((HeapWord*)round_to((intptr_t)(s->top() + area_touched_words), page_size()),
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
85 s->end());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
86 invalid = MemRegion(start, end);
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
87 }
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
88
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
89 ls->add_invalid_region(invalid);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
90 }
a61af66fc99e Initial load
duke
parents:
diff changeset
91 s->set_top(s->end());
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93 } else {
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
94 if (!os::numa_has_static_binding()) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
95 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
96 MemRegion invalid(s->top(), s->end());
a61af66fc99e Initial load
duke
parents:
diff changeset
97 ls->add_invalid_region(invalid);
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
98 #else
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
99 if (ZapUnusedHeapArea) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
100 MemRegion invalid(s->top(), s->end());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
101 ls->add_invalid_region(invalid);
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
102 } else break;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
103 #endif
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
104 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 }
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 size_t MutableNUMASpace::used_in_words() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 size_t s = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
111 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
112 s += lgrp_spaces()->at(i)->space()->used_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114 return s;
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116
a61af66fc99e Initial load
duke
parents:
diff changeset
117 size_t MutableNUMASpace::free_in_words() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 size_t s = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
119 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 s += lgrp_spaces()->at(i)->space()->free_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122 return s;
a61af66fc99e Initial load
duke
parents:
diff changeset
123 }
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125
a61af66fc99e Initial load
duke
parents:
diff changeset
126 size_t MutableNUMASpace::tlab_capacity(Thread *thr) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
127 guarantee(thr != NULL, "No thread");
a61af66fc99e Initial load
duke
parents:
diff changeset
128 int lgrp_id = thr->lgrp_id();
a61af66fc99e Initial load
duke
parents:
diff changeset
129 assert(lgrp_id != -1, "No lgrp_id set");
a61af66fc99e Initial load
duke
parents:
diff changeset
130 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
a61af66fc99e Initial load
duke
parents:
diff changeset
131 if (i == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
132 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134 return lgrp_spaces()->at(i)->space()->capacity_in_bytes();
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136
a61af66fc99e Initial load
duke
parents:
diff changeset
137 size_t MutableNUMASpace::unsafe_max_tlab_alloc(Thread *thr) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 guarantee(thr != NULL, "No thread");
a61af66fc99e Initial load
duke
parents:
diff changeset
139 int lgrp_id = thr->lgrp_id();
a61af66fc99e Initial load
duke
parents:
diff changeset
140 assert(lgrp_id != -1, "No lgrp_id set");
a61af66fc99e Initial load
duke
parents:
diff changeset
141 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
a61af66fc99e Initial load
duke
parents:
diff changeset
142 if (i == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
144 }
a61af66fc99e Initial load
duke
parents:
diff changeset
145 return lgrp_spaces()->at(i)->space()->free_in_bytes();
a61af66fc99e Initial load
duke
parents:
diff changeset
146 }
a61af66fc99e Initial load
duke
parents:
diff changeset
147
a61af66fc99e Initial load
duke
parents:
diff changeset
148 // Check if the NUMA topology has changed. Add and remove spaces if needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
149 // The update can be forced by setting the force parameter equal to true.
a61af66fc99e Initial load
duke
parents:
diff changeset
150 bool MutableNUMASpace::update_layout(bool force) {
a61af66fc99e Initial load
duke
parents:
diff changeset
151 // Check if the topology had changed.
a61af66fc99e Initial load
duke
parents:
diff changeset
152 bool changed = os::numa_topology_changed();
a61af66fc99e Initial load
duke
parents:
diff changeset
153 if (force || changed) {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 // Compute lgrp intersection. Add/remove spaces.
a61af66fc99e Initial load
duke
parents:
diff changeset
155 int lgrp_limit = (int)os::numa_get_groups_num();
a61af66fc99e Initial load
duke
parents:
diff changeset
156 int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit);
a61af66fc99e Initial load
duke
parents:
diff changeset
157 int lgrp_num = (int)os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);
a61af66fc99e Initial load
duke
parents:
diff changeset
158 assert(lgrp_num > 0, "There should be at least one locality group");
a61af66fc99e Initial load
duke
parents:
diff changeset
159 // Add new spaces for the new nodes
a61af66fc99e Initial load
duke
parents:
diff changeset
160 for (int i = 0; i < lgrp_num; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
161 bool found = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
162 for (int j = 0; j < lgrp_spaces()->length(); j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 if (lgrp_spaces()->at(j)->lgrp_id() == lgrp_ids[i]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
164 found = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
165 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167 }
a61af66fc99e Initial load
duke
parents:
diff changeset
168 if (!found) {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 lgrp_spaces()->append(new LGRPSpace(lgrp_ids[i]));
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171 }
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173 // Remove spaces for the removed nodes.
a61af66fc99e Initial load
duke
parents:
diff changeset
174 for (int i = 0; i < lgrp_spaces()->length();) {
a61af66fc99e Initial load
duke
parents:
diff changeset
175 bool found = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
176 for (int j = 0; j < lgrp_num; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 if (lgrp_spaces()->at(i)->lgrp_id() == lgrp_ids[j]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 found = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
179 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182 if (!found) {
a61af66fc99e Initial load
duke
parents:
diff changeset
183 delete lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
184 lgrp_spaces()->remove_at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
185 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 i++;
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188 }
a61af66fc99e Initial load
duke
parents:
diff changeset
189
a61af66fc99e Initial load
duke
parents:
diff changeset
190 FREE_C_HEAP_ARRAY(int, lgrp_ids);
a61af66fc99e Initial load
duke
parents:
diff changeset
191
a61af66fc99e Initial load
duke
parents:
diff changeset
192 if (changed) {
a61af66fc99e Initial load
duke
parents:
diff changeset
193 for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 thread->set_lgrp_id(-1);
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
200 }
a61af66fc99e Initial load
duke
parents:
diff changeset
201
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // Bias region towards the first-touching lgrp. Set the right page sizes.
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
203 void MutableNUMASpace::bias_region(MemRegion mr, int lgrp_id) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
204 HeapWord *start = (HeapWord*)round_to((intptr_t)mr.start(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
205 HeapWord *end = (HeapWord*)round_down((intptr_t)mr.end(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
206 if (end > start) {
a61af66fc99e Initial load
duke
parents:
diff changeset
207 MemRegion aligned_region(start, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
208 assert((intptr_t)aligned_region.start() % page_size() == 0 &&
a61af66fc99e Initial load
duke
parents:
diff changeset
209 (intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");
a61af66fc99e Initial load
duke
parents:
diff changeset
210 assert(region().contains(aligned_region), "Sanity");
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
211 // First we tell the OS which page size we want in the given range. The underlying
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
212 // large page can be broken down if we require small pages.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
213 os::realign_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
214 // Then we uncommit the pages in the range.
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
215 os::free_memory((char*)aligned_region.start(), aligned_region.byte_size());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
216 // And make them local/first-touch biased.
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
217 os::numa_make_local((char*)aligned_region.start(), aligned_region.byte_size(), lgrp_id);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
218 }
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220
a61af66fc99e Initial load
duke
parents:
diff changeset
221 // Free all pages in the region.
a61af66fc99e Initial load
duke
parents:
diff changeset
222 void MutableNUMASpace::free_region(MemRegion mr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
223 HeapWord *start = (HeapWord*)round_to((intptr_t)mr.start(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
224 HeapWord *end = (HeapWord*)round_down((intptr_t)mr.end(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
225 if (end > start) {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 MemRegion aligned_region(start, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
227 assert((intptr_t)aligned_region.start() % page_size() == 0 &&
a61af66fc99e Initial load
duke
parents:
diff changeset
228 (intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");
a61af66fc99e Initial load
duke
parents:
diff changeset
229 assert(region().contains(aligned_region), "Sanity");
a61af66fc99e Initial load
duke
parents:
diff changeset
230 os::free_memory((char*)aligned_region.start(), aligned_region.byte_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
231 }
a61af66fc99e Initial load
duke
parents:
diff changeset
232 }
a61af66fc99e Initial load
duke
parents:
diff changeset
233
a61af66fc99e Initial load
duke
parents:
diff changeset
234 // Update space layout. Perform adaptation.
a61af66fc99e Initial load
duke
parents:
diff changeset
235 void MutableNUMASpace::update() {
a61af66fc99e Initial load
duke
parents:
diff changeset
236 if (update_layout(false)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
237 // If the topology has changed, make all chunks zero-sized.
a61af66fc99e Initial load
duke
parents:
diff changeset
238 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 MutableSpace *s = lgrp_spaces()->at(i)->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
240 s->set_end(s->bottom());
a61af66fc99e Initial load
duke
parents:
diff changeset
241 s->set_top(s->bottom());
a61af66fc99e Initial load
duke
parents:
diff changeset
242 }
a61af66fc99e Initial load
duke
parents:
diff changeset
243 initialize(region(), true);
a61af66fc99e Initial load
duke
parents:
diff changeset
244 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 bool should_initialize = false;
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
246 if (!os::numa_has_static_binding()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
247 for (int i = 0; i < lgrp_spaces()->length(); i++) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
248 if (!lgrp_spaces()->at(i)->invalid_region().is_empty()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
249 should_initialize = true;
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
250 break;
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
251 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
252 }
a61af66fc99e Initial load
duke
parents:
diff changeset
253 }
a61af66fc99e Initial load
duke
parents:
diff changeset
254
a61af66fc99e Initial load
duke
parents:
diff changeset
255 if (should_initialize ||
a61af66fc99e Initial load
duke
parents:
diff changeset
256 (UseAdaptiveNUMAChunkSizing && adaptation_cycles() < samples_count())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
257 initialize(region(), true);
a61af66fc99e Initial load
duke
parents:
diff changeset
258 }
a61af66fc99e Initial load
duke
parents:
diff changeset
259 }
a61af66fc99e Initial load
duke
parents:
diff changeset
260
a61af66fc99e Initial load
duke
parents:
diff changeset
261 if (NUMAStats) {
a61af66fc99e Initial load
duke
parents:
diff changeset
262 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 lgrp_spaces()->at(i)->accumulate_statistics(page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
264 }
a61af66fc99e Initial load
duke
parents:
diff changeset
265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
266
a61af66fc99e Initial load
duke
parents:
diff changeset
267 scan_pages(NUMAPageScanRate);
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 // Scan pages. Free pages that have smaller size or wrong placement.
a61af66fc99e Initial load
duke
parents:
diff changeset
271 void MutableNUMASpace::scan_pages(size_t page_count)
a61af66fc99e Initial load
duke
parents:
diff changeset
272 {
a61af66fc99e Initial load
duke
parents:
diff changeset
273 size_t pages_per_chunk = page_count / lgrp_spaces()->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
274 if (pages_per_chunk > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
275 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 LGRPSpace *ls = lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
277 ls->scan_pages(page_size(), pages_per_chunk);
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279 }
a61af66fc99e Initial load
duke
parents:
diff changeset
280 }
a61af66fc99e Initial load
duke
parents:
diff changeset
281
a61af66fc99e Initial load
duke
parents:
diff changeset
282 // Accumulate statistics about the allocation rate of each lgrp.
a61af66fc99e Initial load
duke
parents:
diff changeset
283 void MutableNUMASpace::accumulate_statistics() {
a61af66fc99e Initial load
duke
parents:
diff changeset
284 if (UseAdaptiveNUMAChunkSizing) {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
286 lgrp_spaces()->at(i)->sample();
a61af66fc99e Initial load
duke
parents:
diff changeset
287 }
a61af66fc99e Initial load
duke
parents:
diff changeset
288 increment_samples_count();
a61af66fc99e Initial load
duke
parents:
diff changeset
289 }
a61af66fc99e Initial load
duke
parents:
diff changeset
290
a61af66fc99e Initial load
duke
parents:
diff changeset
291 if (NUMAStats) {
a61af66fc99e Initial load
duke
parents:
diff changeset
292 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
293 lgrp_spaces()->at(i)->accumulate_statistics(page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295 }
a61af66fc99e Initial load
duke
parents:
diff changeset
296 }
a61af66fc99e Initial load
duke
parents:
diff changeset
297
a61af66fc99e Initial load
duke
parents:
diff changeset
298 // Get the current size of a chunk.
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // This function computes the size of the chunk based on the
a61af66fc99e Initial load
duke
parents:
diff changeset
300 // difference between chunk ends. This allows it to work correctly in
a61af66fc99e Initial load
duke
parents:
diff changeset
301 // case the whole space is resized and during the process of adaptive
a61af66fc99e Initial load
duke
parents:
diff changeset
302 // chunk resizing.
a61af66fc99e Initial load
duke
parents:
diff changeset
303 size_t MutableNUMASpace::current_chunk_size(int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
304 HeapWord *cur_end, *prev_end;
a61af66fc99e Initial load
duke
parents:
diff changeset
305 if (i == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
306 prev_end = bottom();
a61af66fc99e Initial load
duke
parents:
diff changeset
307 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
308 prev_end = lgrp_spaces()->at(i - 1)->space()->end();
a61af66fc99e Initial load
duke
parents:
diff changeset
309 }
a61af66fc99e Initial load
duke
parents:
diff changeset
310 if (i == lgrp_spaces()->length() - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
311 cur_end = end();
a61af66fc99e Initial load
duke
parents:
diff changeset
312 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 cur_end = lgrp_spaces()->at(i)->space()->end();
a61af66fc99e Initial load
duke
parents:
diff changeset
314 }
a61af66fc99e Initial load
duke
parents:
diff changeset
315 if (cur_end > prev_end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 return pointer_delta(cur_end, prev_end, sizeof(char));
a61af66fc99e Initial load
duke
parents:
diff changeset
317 }
a61af66fc99e Initial load
duke
parents:
diff changeset
318 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320
a61af66fc99e Initial load
duke
parents:
diff changeset
321 // Return the default chunk size by equally diving the space.
a61af66fc99e Initial load
duke
parents:
diff changeset
322 // page_size() aligned.
a61af66fc99e Initial load
duke
parents:
diff changeset
323 size_t MutableNUMASpace::default_chunk_size() {
a61af66fc99e Initial load
duke
parents:
diff changeset
324 return base_space_size() / lgrp_spaces()->length() * page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
325 }
a61af66fc99e Initial load
duke
parents:
diff changeset
326
a61af66fc99e Initial load
duke
parents:
diff changeset
327 // Produce a new chunk size. page_size() aligned.
a61af66fc99e Initial load
duke
parents:
diff changeset
328 size_t MutableNUMASpace::adaptive_chunk_size(int i, size_t limit) {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 size_t pages_available = base_space_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
330 for (int j = 0; j < i; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
331 pages_available -= round_down(current_chunk_size(j), page_size()) / page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
332 }
a61af66fc99e Initial load
duke
parents:
diff changeset
333 pages_available -= lgrp_spaces()->length() - i - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
334 assert(pages_available > 0, "No pages left");
a61af66fc99e Initial load
duke
parents:
diff changeset
335 float alloc_rate = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
336 for (int j = i; j < lgrp_spaces()->length(); j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
337 alloc_rate += lgrp_spaces()->at(j)->alloc_rate()->average();
a61af66fc99e Initial load
duke
parents:
diff changeset
338 }
a61af66fc99e Initial load
duke
parents:
diff changeset
339 size_t chunk_size = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
340 if (alloc_rate > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 LGRPSpace *ls = lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
342 chunk_size = (size_t)(ls->alloc_rate()->average() * pages_available / alloc_rate) * page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344 chunk_size = MAX2(chunk_size, page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
345
a61af66fc99e Initial load
duke
parents:
diff changeset
346 if (limit > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
347 limit = round_down(limit, page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
348 if (chunk_size > current_chunk_size(i)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 chunk_size = MIN2((off_t)chunk_size, (off_t)current_chunk_size(i) + (off_t)limit);
a61af66fc99e Initial load
duke
parents:
diff changeset
350 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
351 chunk_size = MAX2((off_t)chunk_size, (off_t)current_chunk_size(i) - (off_t)limit);
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
a61af66fc99e Initial load
duke
parents:
diff changeset
353 }
a61af66fc99e Initial load
duke
parents:
diff changeset
354 assert(chunk_size <= pages_available * page_size(), "Chunk size out of range");
a61af66fc99e Initial load
duke
parents:
diff changeset
355 return chunk_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357
a61af66fc99e Initial load
duke
parents:
diff changeset
358
a61af66fc99e Initial load
duke
parents:
diff changeset
359 // Return the bottom_region and the top_region. Align them to page_size() boundary.
a61af66fc99e Initial load
duke
parents:
diff changeset
360 // |------------------new_region---------------------------------|
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // |----bottom_region--|---intersection---|------top_region------|
a61af66fc99e Initial load
duke
parents:
diff changeset
362 void MutableNUMASpace::select_tails(MemRegion new_region, MemRegion intersection,
a61af66fc99e Initial load
duke
parents:
diff changeset
363 MemRegion* bottom_region, MemRegion *top_region) {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // Is there bottom?
a61af66fc99e Initial load
duke
parents:
diff changeset
365 if (new_region.start() < intersection.start()) { // Yes
a61af66fc99e Initial load
duke
parents:
diff changeset
366 // Try to coalesce small pages into a large one.
a61af66fc99e Initial load
duke
parents:
diff changeset
367 if (UseLargePages && page_size() >= os::large_page_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
368 HeapWord* p = (HeapWord*)round_to((intptr_t) intersection.start(), os::large_page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
369 if (new_region.contains(p)
a61af66fc99e Initial load
duke
parents:
diff changeset
370 && pointer_delta(p, new_region.start(), sizeof(char)) >= os::large_page_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
371 if (intersection.contains(p)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
372 intersection = MemRegion(p, intersection.end());
a61af66fc99e Initial load
duke
parents:
diff changeset
373 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
374 intersection = MemRegion(p, p);
a61af66fc99e Initial load
duke
parents:
diff changeset
375 }
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 *bottom_region = MemRegion(new_region.start(), intersection.start());
a61af66fc99e Initial load
duke
parents:
diff changeset
379 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
380 *bottom_region = MemRegion();
a61af66fc99e Initial load
duke
parents:
diff changeset
381 }
a61af66fc99e Initial load
duke
parents:
diff changeset
382
a61af66fc99e Initial load
duke
parents:
diff changeset
383 // Is there top?
a61af66fc99e Initial load
duke
parents:
diff changeset
384 if (intersection.end() < new_region.end()) { // Yes
a61af66fc99e Initial load
duke
parents:
diff changeset
385 // Try to coalesce small pages into a large one.
a61af66fc99e Initial load
duke
parents:
diff changeset
386 if (UseLargePages && page_size() >= os::large_page_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
387 HeapWord* p = (HeapWord*)round_down((intptr_t) intersection.end(), os::large_page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
388 if (new_region.contains(p)
a61af66fc99e Initial load
duke
parents:
diff changeset
389 && pointer_delta(new_region.end(), p, sizeof(char)) >= os::large_page_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
390 if (intersection.contains(p)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
391 intersection = MemRegion(intersection.start(), p);
a61af66fc99e Initial load
duke
parents:
diff changeset
392 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
393 intersection = MemRegion(p, p);
a61af66fc99e Initial load
duke
parents:
diff changeset
394 }
a61af66fc99e Initial load
duke
parents:
diff changeset
395 }
a61af66fc99e Initial load
duke
parents:
diff changeset
396 }
a61af66fc99e Initial load
duke
parents:
diff changeset
397 *top_region = MemRegion(intersection.end(), new_region.end());
a61af66fc99e Initial load
duke
parents:
diff changeset
398 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
399 *top_region = MemRegion();
a61af66fc99e Initial load
duke
parents:
diff changeset
400 }
a61af66fc99e Initial load
duke
parents:
diff changeset
401 }
a61af66fc99e Initial load
duke
parents:
diff changeset
402
a61af66fc99e Initial load
duke
parents:
diff changeset
403 // Try to merge the invalid region with the bottom or top region by decreasing
a61af66fc99e Initial load
duke
parents:
diff changeset
404 // the intersection area. Return the invalid_region aligned to the page_size()
a61af66fc99e Initial load
duke
parents:
diff changeset
405 // boundary if it's inside the intersection. Return non-empty invalid_region
a61af66fc99e Initial load
duke
parents:
diff changeset
406 // if it lies inside the intersection (also page-aligned).
a61af66fc99e Initial load
duke
parents:
diff changeset
407 // |------------------new_region---------------------------------|
a61af66fc99e Initial load
duke
parents:
diff changeset
408 // |----------------|-------invalid---|--------------------------|
a61af66fc99e Initial load
duke
parents:
diff changeset
409 // |----bottom_region--|---intersection---|------top_region------|
a61af66fc99e Initial load
duke
parents:
diff changeset
410 void MutableNUMASpace::merge_regions(MemRegion new_region, MemRegion* intersection,
a61af66fc99e Initial load
duke
parents:
diff changeset
411 MemRegion *invalid_region) {
a61af66fc99e Initial load
duke
parents:
diff changeset
412 if (intersection->start() >= invalid_region->start() && intersection->contains(invalid_region->end())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
413 *intersection = MemRegion(invalid_region->end(), intersection->end());
a61af66fc99e Initial load
duke
parents:
diff changeset
414 *invalid_region = MemRegion();
a61af66fc99e Initial load
duke
parents:
diff changeset
415 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
416 if (intersection->end() <= invalid_region->end() && intersection->contains(invalid_region->start())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
417 *intersection = MemRegion(intersection->start(), invalid_region->start());
a61af66fc99e Initial load
duke
parents:
diff changeset
418 *invalid_region = MemRegion();
a61af66fc99e Initial load
duke
parents:
diff changeset
419 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
420 if (intersection->equals(*invalid_region) || invalid_region->contains(*intersection)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
421 *intersection = MemRegion(new_region.start(), new_region.start());
a61af66fc99e Initial load
duke
parents:
diff changeset
422 *invalid_region = MemRegion();
a61af66fc99e Initial load
duke
parents:
diff changeset
423 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
424 if (intersection->contains(invalid_region)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 // That's the only case we have to make an additional bias_region() call.
a61af66fc99e Initial load
duke
parents:
diff changeset
426 HeapWord* start = invalid_region->start();
a61af66fc99e Initial load
duke
parents:
diff changeset
427 HeapWord* end = invalid_region->end();
a61af66fc99e Initial load
duke
parents:
diff changeset
428 if (UseLargePages && page_size() >= os::large_page_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
429 HeapWord *p = (HeapWord*)round_down((intptr_t) start, os::large_page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
430 if (new_region.contains(p)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
431 start = p;
a61af66fc99e Initial load
duke
parents:
diff changeset
432 }
a61af66fc99e Initial load
duke
parents:
diff changeset
433 p = (HeapWord*)round_to((intptr_t) end, os::large_page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
434 if (new_region.contains(end)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
435 end = p;
a61af66fc99e Initial load
duke
parents:
diff changeset
436 }
a61af66fc99e Initial load
duke
parents:
diff changeset
437 }
a61af66fc99e Initial load
duke
parents:
diff changeset
438 if (intersection->start() > start) {
a61af66fc99e Initial load
duke
parents:
diff changeset
439 *intersection = MemRegion(start, intersection->end());
a61af66fc99e Initial load
duke
parents:
diff changeset
440 }
a61af66fc99e Initial load
duke
parents:
diff changeset
441 if (intersection->end() < end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
442 *intersection = MemRegion(intersection->start(), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
443 }
a61af66fc99e Initial load
duke
parents:
diff changeset
444 *invalid_region = MemRegion(start, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
445 }
a61af66fc99e Initial load
duke
parents:
diff changeset
446 }
a61af66fc99e Initial load
duke
parents:
diff changeset
447
a61af66fc99e Initial load
duke
parents:
diff changeset
448 void MutableNUMASpace::initialize(MemRegion mr, bool clear_space) {
a61af66fc99e Initial load
duke
parents:
diff changeset
449 assert(clear_space, "Reallocation will destory data!");
a61af66fc99e Initial load
duke
parents:
diff changeset
450 assert(lgrp_spaces()->length() > 0, "There should be at least one space");
a61af66fc99e Initial load
duke
parents:
diff changeset
451
a61af66fc99e Initial load
duke
parents:
diff changeset
452 MemRegion old_region = region(), new_region;
a61af66fc99e Initial load
duke
parents:
diff changeset
453 set_bottom(mr.start());
a61af66fc99e Initial load
duke
parents:
diff changeset
454 set_end(mr.end());
a61af66fc99e Initial load
duke
parents:
diff changeset
455 MutableSpace::set_top(bottom());
a61af66fc99e Initial load
duke
parents:
diff changeset
456
a61af66fc99e Initial load
duke
parents:
diff changeset
457 // Compute chunk sizes
a61af66fc99e Initial load
duke
parents:
diff changeset
458 size_t prev_page_size = page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
459 set_page_size(UseLargePages ? os::large_page_size() : os::vm_page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
460 HeapWord* rounded_bottom = (HeapWord*)round_to((intptr_t) bottom(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
461 HeapWord* rounded_end = (HeapWord*)round_down((intptr_t) end(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
462 size_t base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
463
a61af66fc99e Initial load
duke
parents:
diff changeset
464 // Try small pages if the chunk size is too small
a61af66fc99e Initial load
duke
parents:
diff changeset
465 if (base_space_size_pages / lgrp_spaces()->length() == 0
a61af66fc99e Initial load
duke
parents:
diff changeset
466 && page_size() > (size_t)os::vm_page_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
467 set_page_size(os::vm_page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
468 rounded_bottom = (HeapWord*)round_to((intptr_t) bottom(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
469 rounded_end = (HeapWord*)round_down((intptr_t) end(), page_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
470 base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
471 }
a61af66fc99e Initial load
duke
parents:
diff changeset
472 guarantee(base_space_size_pages / lgrp_spaces()->length() > 0, "Space too small");
a61af66fc99e Initial load
duke
parents:
diff changeset
473 set_base_space_size(base_space_size_pages);
a61af66fc99e Initial load
duke
parents:
diff changeset
474
a61af66fc99e Initial load
duke
parents:
diff changeset
475 // Handle space resize
a61af66fc99e Initial load
duke
parents:
diff changeset
476 MemRegion top_region, bottom_region;
a61af66fc99e Initial load
duke
parents:
diff changeset
477 if (!old_region.equals(region())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
478 new_region = MemRegion(rounded_bottom, rounded_end);
a61af66fc99e Initial load
duke
parents:
diff changeset
479 MemRegion intersection = new_region.intersection(old_region);
a61af66fc99e Initial load
duke
parents:
diff changeset
480 if (intersection.start() == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
481 intersection.end() == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
482 prev_page_size > page_size()) { // If the page size got smaller we have to change
a61af66fc99e Initial load
duke
parents:
diff changeset
483 // the page size preference for the whole space.
a61af66fc99e Initial load
duke
parents:
diff changeset
484 intersection = MemRegion(new_region.start(), new_region.start());
a61af66fc99e Initial load
duke
parents:
diff changeset
485 }
a61af66fc99e Initial load
duke
parents:
diff changeset
486 select_tails(new_region, intersection, &bottom_region, &top_region);
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
487 bias_region(bottom_region, lgrp_spaces()->at(0)->lgrp_id());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
488 bias_region(top_region, lgrp_spaces()->at(lgrp_spaces()->length() - 1)->lgrp_id());
0
a61af66fc99e Initial load
duke
parents:
diff changeset
489 }
a61af66fc99e Initial load
duke
parents:
diff changeset
490
a61af66fc99e Initial load
duke
parents:
diff changeset
491 // Check if the space layout has changed significantly?
a61af66fc99e Initial load
duke
parents:
diff changeset
492 // This happens when the space has been resized so that either head or tail
a61af66fc99e Initial load
duke
parents:
diff changeset
493 // chunk became less than a page.
a61af66fc99e Initial load
duke
parents:
diff changeset
494 bool layout_valid = UseAdaptiveNUMAChunkSizing &&
a61af66fc99e Initial load
duke
parents:
diff changeset
495 current_chunk_size(0) > page_size() &&
a61af66fc99e Initial load
duke
parents:
diff changeset
496 current_chunk_size(lgrp_spaces()->length() - 1) > page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
497
a61af66fc99e Initial load
duke
parents:
diff changeset
498
a61af66fc99e Initial load
duke
parents:
diff changeset
499 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
500 LGRPSpace *ls = lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
501 MutableSpace *s = ls->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
502 old_region = s->region();
a61af66fc99e Initial load
duke
parents:
diff changeset
503
a61af66fc99e Initial load
duke
parents:
diff changeset
504 size_t chunk_byte_size = 0, old_chunk_byte_size = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
505 if (i < lgrp_spaces()->length() - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
506 if (!UseAdaptiveNUMAChunkSizing ||
a61af66fc99e Initial load
duke
parents:
diff changeset
507 (UseAdaptiveNUMAChunkSizing && NUMAChunkResizeWeight == 0) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
508 samples_count() < AdaptiveSizePolicyReadyThreshold) {
a61af66fc99e Initial load
duke
parents:
diff changeset
509 // No adaptation. Divide the space equally.
a61af66fc99e Initial load
duke
parents:
diff changeset
510 chunk_byte_size = default_chunk_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
511 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
512 if (!layout_valid || NUMASpaceResizeRate == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
513 // Fast adaptation. If no space resize rate is set, resize
a61af66fc99e Initial load
duke
parents:
diff changeset
514 // the chunks instantly.
a61af66fc99e Initial load
duke
parents:
diff changeset
515 chunk_byte_size = adaptive_chunk_size(i, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
516 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
517 // Slow adaptation. Resize the chunks moving no more than
a61af66fc99e Initial load
duke
parents:
diff changeset
518 // NUMASpaceResizeRate bytes per collection.
a61af66fc99e Initial load
duke
parents:
diff changeset
519 size_t limit = NUMASpaceResizeRate /
a61af66fc99e Initial load
duke
parents:
diff changeset
520 (lgrp_spaces()->length() * (lgrp_spaces()->length() + 1) / 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
521 chunk_byte_size = adaptive_chunk_size(i, MAX2(limit * (i + 1), page_size()));
a61af66fc99e Initial load
duke
parents:
diff changeset
522 }
a61af66fc99e Initial load
duke
parents:
diff changeset
523
a61af66fc99e Initial load
duke
parents:
diff changeset
524 assert(chunk_byte_size >= page_size(), "Chunk size too small");
a61af66fc99e Initial load
duke
parents:
diff changeset
525 assert(chunk_byte_size <= capacity_in_bytes(), "Sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
526 }
a61af66fc99e Initial load
duke
parents:
diff changeset
527
a61af66fc99e Initial load
duke
parents:
diff changeset
528 if (i == 0) { // Bottom chunk
a61af66fc99e Initial load
duke
parents:
diff changeset
529 if (i != lgrp_spaces()->length() - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
530 new_region = MemRegion(bottom(), rounded_bottom + (chunk_byte_size >> LogHeapWordSize));
a61af66fc99e Initial load
duke
parents:
diff changeset
531 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
532 new_region = MemRegion(bottom(), end());
a61af66fc99e Initial load
duke
parents:
diff changeset
533 }
a61af66fc99e Initial load
duke
parents:
diff changeset
534 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
535 if (i < lgrp_spaces()->length() - 1) { // Middle chunks
a61af66fc99e Initial load
duke
parents:
diff changeset
536 MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
537 new_region = MemRegion(ps->end(),
a61af66fc99e Initial load
duke
parents:
diff changeset
538 ps->end() + (chunk_byte_size >> LogHeapWordSize));
a61af66fc99e Initial load
duke
parents:
diff changeset
539 } else { // Top chunk
a61af66fc99e Initial load
duke
parents:
diff changeset
540 MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
541 new_region = MemRegion(ps->end(), end());
a61af66fc99e Initial load
duke
parents:
diff changeset
542 }
a61af66fc99e Initial load
duke
parents:
diff changeset
543 guarantee(region().contains(new_region), "Region invariant");
a61af66fc99e Initial load
duke
parents:
diff changeset
544
a61af66fc99e Initial load
duke
parents:
diff changeset
545
a61af66fc99e Initial load
duke
parents:
diff changeset
546 // The general case:
a61af66fc99e Initial load
duke
parents:
diff changeset
547 // |---------------------|--invalid---|--------------------------|
a61af66fc99e Initial load
duke
parents:
diff changeset
548 // |------------------new_region---------------------------------|
a61af66fc99e Initial load
duke
parents:
diff changeset
549 // |----bottom_region--|---intersection---|------top_region------|
a61af66fc99e Initial load
duke
parents:
diff changeset
550 // |----old_region----|
a61af66fc99e Initial load
duke
parents:
diff changeset
551 // The intersection part has all pages in place we don't need to migrate them.
a61af66fc99e Initial load
duke
parents:
diff changeset
552 // Pages for the top and bottom part should be freed and then reallocated.
a61af66fc99e Initial load
duke
parents:
diff changeset
553
a61af66fc99e Initial load
duke
parents:
diff changeset
554 MemRegion intersection = old_region.intersection(new_region);
a61af66fc99e Initial load
duke
parents:
diff changeset
555
a61af66fc99e Initial load
duke
parents:
diff changeset
556 if (intersection.start() == NULL || intersection.end() == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
557 intersection = MemRegion(new_region.start(), new_region.start());
a61af66fc99e Initial load
duke
parents:
diff changeset
558 }
a61af66fc99e Initial load
duke
parents:
diff changeset
559
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
560 if (!os::numa_has_static_binding()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
561 MemRegion invalid_region = ls->invalid_region().intersection(new_region);
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
562 // Invalid region is a range of memory that could've possibly
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
563 // been allocated on the other node. That's relevant only on Solaris where
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
564 // there is no static memory binding.
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
565 if (!invalid_region.is_empty()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
566 merge_regions(new_region, &intersection, &invalid_region);
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
567 free_region(invalid_region);
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
568 ls->set_invalid_region(MemRegion());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
569 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
570 }
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
571
0
a61af66fc99e Initial load
duke
parents:
diff changeset
572 select_tails(new_region, intersection, &bottom_region, &top_region);
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
573
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
574 if (!os::numa_has_static_binding()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
575 // If that's a system with the first-touch policy then it's enough
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
576 // to free the pages.
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
577 free_region(bottom_region);
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
578 free_region(top_region);
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
579 } else {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
580 // In a system with static binding we have to change the bias whenever
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
581 // we reshape the heap.
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
582 bias_region(bottom_region, ls->lgrp_id());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
583 bias_region(top_region, ls->lgrp_id());
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
584 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
585
a61af66fc99e Initial load
duke
parents:
diff changeset
586 // If we clear the region, we would mangle it in debug. That would cause page
a61af66fc99e Initial load
duke
parents:
diff changeset
587 // allocation in a different place. Hence setting the top directly.
a61af66fc99e Initial load
duke
parents:
diff changeset
588 s->initialize(new_region, false);
a61af66fc99e Initial load
duke
parents:
diff changeset
589 s->set_top(s->bottom());
a61af66fc99e Initial load
duke
parents:
diff changeset
590
a61af66fc99e Initial load
duke
parents:
diff changeset
591 set_adaptation_cycles(samples_count());
a61af66fc99e Initial load
duke
parents:
diff changeset
592 }
a61af66fc99e Initial load
duke
parents:
diff changeset
593 }
a61af66fc99e Initial load
duke
parents:
diff changeset
594
a61af66fc99e Initial load
duke
parents:
diff changeset
595 // Set the top of the whole space.
a61af66fc99e Initial load
duke
parents:
diff changeset
596 // Mark the the holes in chunks below the top() as invalid.
a61af66fc99e Initial load
duke
parents:
diff changeset
597 void MutableNUMASpace::set_top(HeapWord* value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
598 bool found_top = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
599 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
600 LGRPSpace *ls = lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
601 MutableSpace *s = ls->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
602 HeapWord *top = MAX2((HeapWord*)round_down((intptr_t)s->top(), page_size()), s->bottom());
a61af66fc99e Initial load
duke
parents:
diff changeset
603
a61af66fc99e Initial load
duke
parents:
diff changeset
604 if (s->contains(value)) {
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
605 if (!os::numa_has_static_binding() && top < value && top < s->end()) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
606 ls->add_invalid_region(MemRegion(top, value));
a61af66fc99e Initial load
duke
parents:
diff changeset
607 }
a61af66fc99e Initial load
duke
parents:
diff changeset
608 s->set_top(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
609 found_top = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
610 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
611 if (found_top) {
a61af66fc99e Initial load
duke
parents:
diff changeset
612 s->set_top(s->bottom());
a61af66fc99e Initial load
duke
parents:
diff changeset
613 } else {
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
614 if (!os::numa_has_static_binding() && top < s->end()) {
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
615 ls->add_invalid_region(MemRegion(top, s->end()));
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
616 }
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
617 s->set_top(s->end());
0
a61af66fc99e Initial load
duke
parents:
diff changeset
618 }
a61af66fc99e Initial load
duke
parents:
diff changeset
619 }
a61af66fc99e Initial load
duke
parents:
diff changeset
620 }
a61af66fc99e Initial load
duke
parents:
diff changeset
621 MutableSpace::set_top(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
622 }
a61af66fc99e Initial load
duke
parents:
diff changeset
623
a61af66fc99e Initial load
duke
parents:
diff changeset
624 void MutableNUMASpace::clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
625 MutableSpace::set_top(bottom());
a61af66fc99e Initial load
duke
parents:
diff changeset
626 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
627 lgrp_spaces()->at(i)->space()->clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
628 }
a61af66fc99e Initial load
duke
parents:
diff changeset
629 }
a61af66fc99e Initial load
duke
parents:
diff changeset
630
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
631 /*
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
632 Linux supports static memory binding, therefore the most part of the
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
633 logic dealing with the possible invalid page allocation is effectively
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
634 disabled. Besides there is no notion of the home node in Linux. A
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
635 thread is allowed to migrate freely. Although the scheduler is rather
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
636 reluctant to move threads between the nodes. We check for the current
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
637 node every allocation. And with a high probability a thread stays on
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
638 the same node for some time allowing local access to recently allocated
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
639 objects.
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
640 */
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
641
0
a61af66fc99e Initial load
duke
parents:
diff changeset
642 HeapWord* MutableNUMASpace::allocate(size_t size) {
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
643 Thread* thr = Thread::current();
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
644 int lgrp_id = thr->lgrp_id();
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
645 if (lgrp_id == -1 || !os::numa_has_group_homing()) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
646 lgrp_id = os::numa_get_group_id();
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
647 thr->set_lgrp_id(lgrp_id);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
648 }
a61af66fc99e Initial load
duke
parents:
diff changeset
649
a61af66fc99e Initial load
duke
parents:
diff changeset
650 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
a61af66fc99e Initial load
duke
parents:
diff changeset
651
a61af66fc99e Initial load
duke
parents:
diff changeset
652 // It is possible that a new CPU has been hotplugged and
a61af66fc99e Initial load
duke
parents:
diff changeset
653 // we haven't reshaped the space accordingly.
a61af66fc99e Initial load
duke
parents:
diff changeset
654 if (i == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
655 i = os::random() % lgrp_spaces()->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
656 }
a61af66fc99e Initial load
duke
parents:
diff changeset
657
a61af66fc99e Initial load
duke
parents:
diff changeset
658 MutableSpace *s = lgrp_spaces()->at(i)->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
659 HeapWord *p = s->allocate(size);
a61af66fc99e Initial load
duke
parents:
diff changeset
660
a61af66fc99e Initial load
duke
parents:
diff changeset
661 if (p != NULL && s->free_in_words() < (size_t)oopDesc::header_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
662 s->set_top(s->top() - size);
a61af66fc99e Initial load
duke
parents:
diff changeset
663 p = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
664 }
a61af66fc99e Initial load
duke
parents:
diff changeset
665 if (p != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
666 if (top() < s->top()) { // Keep _top updated.
a61af66fc99e Initial load
duke
parents:
diff changeset
667 MutableSpace::set_top(s->top());
a61af66fc99e Initial load
duke
parents:
diff changeset
668 }
a61af66fc99e Initial load
duke
parents:
diff changeset
669 }
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
670 // Make the page allocation happen here if there is no static binding..
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
671 if (p != NULL && !os::numa_has_static_binding()) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
672 for (HeapWord *i = p; i < p + size; i += os::vm_page_size() >> LogHeapWordSize) {
a61af66fc99e Initial load
duke
parents:
diff changeset
673 *(int*)i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
674 }
a61af66fc99e Initial load
duke
parents:
diff changeset
675 }
a61af66fc99e Initial load
duke
parents:
diff changeset
676 return p;
a61af66fc99e Initial load
duke
parents:
diff changeset
677 }
a61af66fc99e Initial load
duke
parents:
diff changeset
678
a61af66fc99e Initial load
duke
parents:
diff changeset
679 // This version is lock-free.
a61af66fc99e Initial load
duke
parents:
diff changeset
680 HeapWord* MutableNUMASpace::cas_allocate(size_t size) {
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
681 Thread* thr = Thread::current();
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
682 int lgrp_id = thr->lgrp_id();
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
683 if (lgrp_id == -1 || !os::numa_has_group_homing()) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
684 lgrp_id = os::numa_get_group_id();
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
685 thr->set_lgrp_id(lgrp_id);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
686 }
a61af66fc99e Initial load
duke
parents:
diff changeset
687
a61af66fc99e Initial load
duke
parents:
diff changeset
688 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
a61af66fc99e Initial load
duke
parents:
diff changeset
689 // It is possible that a new CPU has been hotplugged and
a61af66fc99e Initial load
duke
parents:
diff changeset
690 // we haven't reshaped the space accordingly.
a61af66fc99e Initial load
duke
parents:
diff changeset
691 if (i == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
692 i = os::random() % lgrp_spaces()->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
693 }
a61af66fc99e Initial load
duke
parents:
diff changeset
694 MutableSpace *s = lgrp_spaces()->at(i)->space();
a61af66fc99e Initial load
duke
parents:
diff changeset
695 HeapWord *p = s->cas_allocate(size);
a61af66fc99e Initial load
duke
parents:
diff changeset
696 if (p != NULL && s->free_in_words() < (size_t)oopDesc::header_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
697 if (s->cas_deallocate(p, size)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
698 // We were the last to allocate and created a fragment less than
a61af66fc99e Initial load
duke
parents:
diff changeset
699 // a minimal object.
a61af66fc99e Initial load
duke
parents:
diff changeset
700 p = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
701 }
a61af66fc99e Initial load
duke
parents:
diff changeset
702 }
a61af66fc99e Initial load
duke
parents:
diff changeset
703 if (p != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
704 HeapWord* cur_top, *cur_chunk_top = p + size;
a61af66fc99e Initial load
duke
parents:
diff changeset
705 while ((cur_top = top()) < cur_chunk_top) { // Keep _top updated.
a61af66fc99e Initial load
duke
parents:
diff changeset
706 if (Atomic::cmpxchg_ptr(cur_chunk_top, top_addr(), cur_top) == cur_top) {
a61af66fc99e Initial load
duke
parents:
diff changeset
707 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
708 }
a61af66fc99e Initial load
duke
parents:
diff changeset
709 }
a61af66fc99e Initial load
duke
parents:
diff changeset
710 }
a61af66fc99e Initial load
duke
parents:
diff changeset
711
141
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
712 // Make the page allocation happen here if there is no static binding.
fcbfc50865ab 6684395: Port NUMA-aware allocator to linux
iveresov
parents: 0
diff changeset
713 if (p != NULL && !os::numa_has_static_binding() ) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
714 for (HeapWord *i = p; i < p + size; i += os::vm_page_size() >> LogHeapWordSize) {
a61af66fc99e Initial load
duke
parents:
diff changeset
715 *(int*)i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
716 }
a61af66fc99e Initial load
duke
parents:
diff changeset
717 }
a61af66fc99e Initial load
duke
parents:
diff changeset
718 return p;
a61af66fc99e Initial load
duke
parents:
diff changeset
719 }
a61af66fc99e Initial load
duke
parents:
diff changeset
720
a61af66fc99e Initial load
duke
parents:
diff changeset
721 void MutableNUMASpace::print_short_on(outputStream* st) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
722 MutableSpace::print_short_on(st);
a61af66fc99e Initial load
duke
parents:
diff changeset
723 st->print(" (");
a61af66fc99e Initial load
duke
parents:
diff changeset
724 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
725 st->print("lgrp %d: ", lgrp_spaces()->at(i)->lgrp_id());
a61af66fc99e Initial load
duke
parents:
diff changeset
726 lgrp_spaces()->at(i)->space()->print_short_on(st);
a61af66fc99e Initial load
duke
parents:
diff changeset
727 if (i < lgrp_spaces()->length() - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
728 st->print(", ");
a61af66fc99e Initial load
duke
parents:
diff changeset
729 }
a61af66fc99e Initial load
duke
parents:
diff changeset
730 }
a61af66fc99e Initial load
duke
parents:
diff changeset
731 st->print(")");
a61af66fc99e Initial load
duke
parents:
diff changeset
732 }
a61af66fc99e Initial load
duke
parents:
diff changeset
733
a61af66fc99e Initial load
duke
parents:
diff changeset
734 void MutableNUMASpace::print_on(outputStream* st) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
735 MutableSpace::print_on(st);
a61af66fc99e Initial load
duke
parents:
diff changeset
736 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
737 LGRPSpace *ls = lgrp_spaces()->at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
738 st->print(" lgrp %d", ls->lgrp_id());
a61af66fc99e Initial load
duke
parents:
diff changeset
739 ls->space()->print_on(st);
a61af66fc99e Initial load
duke
parents:
diff changeset
740 if (NUMAStats) {
a61af66fc99e Initial load
duke
parents:
diff changeset
741 st->print(" local/remote/unbiased/uncommitted: %dK/%dK/%dK/%dK, large/small pages: %d/%d\n",
a61af66fc99e Initial load
duke
parents:
diff changeset
742 ls->space_stats()->_local_space / K,
a61af66fc99e Initial load
duke
parents:
diff changeset
743 ls->space_stats()->_remote_space / K,
a61af66fc99e Initial load
duke
parents:
diff changeset
744 ls->space_stats()->_unbiased_space / K,
a61af66fc99e Initial load
duke
parents:
diff changeset
745 ls->space_stats()->_uncommited_space / K,
a61af66fc99e Initial load
duke
parents:
diff changeset
746 ls->space_stats()->_large_pages,
a61af66fc99e Initial load
duke
parents:
diff changeset
747 ls->space_stats()->_small_pages);
a61af66fc99e Initial load
duke
parents:
diff changeset
748 }
a61af66fc99e Initial load
duke
parents:
diff changeset
749 }
a61af66fc99e Initial load
duke
parents:
diff changeset
750 }
a61af66fc99e Initial load
duke
parents:
diff changeset
751
a61af66fc99e Initial load
duke
parents:
diff changeset
752 void MutableNUMASpace::verify(bool allow_dirty) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
753 for (int i = 0; i < lgrp_spaces()->length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
754 lgrp_spaces()->at(i)->space()->verify(allow_dirty);
a61af66fc99e Initial load
duke
parents:
diff changeset
755 }
a61af66fc99e Initial load
duke
parents:
diff changeset
756 }
a61af66fc99e Initial load
duke
parents:
diff changeset
757
a61af66fc99e Initial load
duke
parents:
diff changeset
758 // Scan pages and gather stats about page placement and size.
a61af66fc99e Initial load
duke
parents:
diff changeset
759 void MutableNUMASpace::LGRPSpace::accumulate_statistics(size_t page_size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
760 clear_space_stats();
a61af66fc99e Initial load
duke
parents:
diff changeset
761 char *start = (char*)round_to((intptr_t) space()->bottom(), page_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
762 char* end = (char*)round_down((intptr_t) space()->end(), page_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
763 if (start < end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
764 for (char *p = start; p < end;) {
a61af66fc99e Initial load
duke
parents:
diff changeset
765 os::page_info info;
a61af66fc99e Initial load
duke
parents:
diff changeset
766 if (os::get_page_info(p, &info)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
767 if (info.size > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
768 if (info.size > (size_t)os::vm_page_size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
769 space_stats()->_large_pages++;
a61af66fc99e Initial load
duke
parents:
diff changeset
770 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
771 space_stats()->_small_pages++;
a61af66fc99e Initial load
duke
parents:
diff changeset
772 }
a61af66fc99e Initial load
duke
parents:
diff changeset
773 if (info.lgrp_id == lgrp_id()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
774 space_stats()->_local_space += info.size;
a61af66fc99e Initial load
duke
parents:
diff changeset
775 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
776 space_stats()->_remote_space += info.size;
a61af66fc99e Initial load
duke
parents:
diff changeset
777 }
a61af66fc99e Initial load
duke
parents:
diff changeset
778 p += info.size;
a61af66fc99e Initial load
duke
parents:
diff changeset
779 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
780 p += os::vm_page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
781 space_stats()->_uncommited_space += os::vm_page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
782 }
a61af66fc99e Initial load
duke
parents:
diff changeset
783 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
784 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
785 }
a61af66fc99e Initial load
duke
parents:
diff changeset
786 }
a61af66fc99e Initial load
duke
parents:
diff changeset
787 }
a61af66fc99e Initial load
duke
parents:
diff changeset
788 space_stats()->_unbiased_space = pointer_delta(start, space()->bottom(), sizeof(char)) +
a61af66fc99e Initial load
duke
parents:
diff changeset
789 pointer_delta(space()->end(), end, sizeof(char));
a61af66fc99e Initial load
duke
parents:
diff changeset
790
a61af66fc99e Initial load
duke
parents:
diff changeset
791 }
a61af66fc99e Initial load
duke
parents:
diff changeset
792
a61af66fc99e Initial load
duke
parents:
diff changeset
793 // Scan page_count pages and verify if they have the right size and right placement.
a61af66fc99e Initial load
duke
parents:
diff changeset
794 // If invalid pages are found they are freed in hope that subsequent reallocation
a61af66fc99e Initial load
duke
parents:
diff changeset
795 // will be more successful.
a61af66fc99e Initial load
duke
parents:
diff changeset
796 void MutableNUMASpace::LGRPSpace::scan_pages(size_t page_size, size_t page_count)
a61af66fc99e Initial load
duke
parents:
diff changeset
797 {
a61af66fc99e Initial load
duke
parents:
diff changeset
798 char* range_start = (char*)round_to((intptr_t) space()->bottom(), page_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
799 char* range_end = (char*)round_down((intptr_t) space()->end(), page_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
800
a61af66fc99e Initial load
duke
parents:
diff changeset
801 if (range_start > last_page_scanned() || last_page_scanned() >= range_end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
802 set_last_page_scanned(range_start);
a61af66fc99e Initial load
duke
parents:
diff changeset
803 }
a61af66fc99e Initial load
duke
parents:
diff changeset
804
a61af66fc99e Initial load
duke
parents:
diff changeset
805 char *scan_start = last_page_scanned();
a61af66fc99e Initial load
duke
parents:
diff changeset
806 char* scan_end = MIN2(scan_start + page_size * page_count, range_end);
a61af66fc99e Initial load
duke
parents:
diff changeset
807
a61af66fc99e Initial load
duke
parents:
diff changeset
808 os::page_info page_expected, page_found;
a61af66fc99e Initial load
duke
parents:
diff changeset
809 page_expected.size = page_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
810 page_expected.lgrp_id = lgrp_id();
a61af66fc99e Initial load
duke
parents:
diff changeset
811
a61af66fc99e Initial load
duke
parents:
diff changeset
812 char *s = scan_start;
a61af66fc99e Initial load
duke
parents:
diff changeset
813 while (s < scan_end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
814 char *e = os::scan_pages(s, (char*)scan_end, &page_expected, &page_found);
a61af66fc99e Initial load
duke
parents:
diff changeset
815 if (e == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
816 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
817 }
a61af66fc99e Initial load
duke
parents:
diff changeset
818 if (e != scan_end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
819 if ((page_expected.size != page_size || page_expected.lgrp_id != lgrp_id())
a61af66fc99e Initial load
duke
parents:
diff changeset
820 && page_expected.size != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
821 os::free_memory(s, pointer_delta(e, s, sizeof(char)));
a61af66fc99e Initial load
duke
parents:
diff changeset
822 }
a61af66fc99e Initial load
duke
parents:
diff changeset
823 page_expected = page_found;
a61af66fc99e Initial load
duke
parents:
diff changeset
824 }
a61af66fc99e Initial load
duke
parents:
diff changeset
825 s = e;
a61af66fc99e Initial load
duke
parents:
diff changeset
826 }
a61af66fc99e Initial load
duke
parents:
diff changeset
827
a61af66fc99e Initial load
duke
parents:
diff changeset
828 set_last_page_scanned(scan_end);
a61af66fc99e Initial load
duke
parents:
diff changeset
829 }