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