src/share/vm/gc_implementation/shared/mutableSpace.cpp

changeset 0
f90c822e73f8
child 1
2d8a650513c2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/gc_implementation/shared/mutableSpace.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,271 @@
     1.4 +/*
     1.5 + * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "utilities/macros.hpp"
    1.30 +#if INCLUDE_ALL_GCS
    1.31 +#include "gc_implementation/shared/mutableSpace.hpp"
    1.32 +#include "gc_implementation/shared/spaceDecorator.hpp"
    1.33 +#include "oops/oop.inline.hpp"
    1.34 +#include "runtime/safepoint.hpp"
    1.35 +#include "runtime/thread.hpp"
    1.36 +#endif // INCLUDE_ALL_GCS
    1.37 +
    1.38 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    1.39 +
    1.40 +MutableSpace::MutableSpace(size_t alignment): ImmutableSpace(), _top(NULL), _alignment(alignment) {
    1.41 +  assert(MutableSpace::alignment() >= 0 &&
    1.42 +         MutableSpace::alignment() % os::vm_page_size() == 0,
    1.43 +         "Space should be aligned");
    1.44 +  _mangler = new MutableSpaceMangler(this);
    1.45 +}
    1.46 +
    1.47 +MutableSpace::~MutableSpace() {
    1.48 +  delete _mangler;
    1.49 +}
    1.50 +
    1.51 +void MutableSpace::numa_setup_pages(MemRegion mr, bool clear_space) {
    1.52 +  if (!mr.is_empty()) {
    1.53 +    size_t page_size = UseLargePages ? alignment() : os::vm_page_size();
    1.54 +    HeapWord *start = (HeapWord*)round_to((intptr_t) mr.start(), page_size);
    1.55 +    HeapWord *end =  (HeapWord*)round_down((intptr_t) mr.end(), page_size);
    1.56 +    if (end > start) {
    1.57 +      size_t size = pointer_delta(end, start, sizeof(char));
    1.58 +      if (clear_space) {
    1.59 +        // Prefer page reallocation to migration.
    1.60 +        os::free_memory((char*)start, size, page_size);
    1.61 +      }
    1.62 +      os::numa_make_global((char*)start, size);
    1.63 +    }
    1.64 +  }
    1.65 +}
    1.66 +
    1.67 +void MutableSpace::pretouch_pages(MemRegion mr) {
    1.68 +  for (volatile char *p = (char*)mr.start(); p < (char*)mr.end(); p += os::vm_page_size()) {
    1.69 +    char t = *p; *p = t;
    1.70 +  }
    1.71 +}
    1.72 +
    1.73 +void MutableSpace::initialize(MemRegion mr,
    1.74 +                              bool clear_space,
    1.75 +                              bool mangle_space,
    1.76 +                              bool setup_pages) {
    1.77 +
    1.78 +  assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()),
    1.79 +         "invalid space boundaries");
    1.80 +
    1.81 +  if (setup_pages && (UseNUMA || AlwaysPreTouch)) {
    1.82 +    // The space may move left and right or expand/shrink.
    1.83 +    // We'd like to enforce the desired page placement.
    1.84 +    MemRegion head, tail;
    1.85 +    if (last_setup_region().is_empty()) {
    1.86 +      // If it's the first initialization don't limit the amount of work.
    1.87 +      head = mr;
    1.88 +      tail = MemRegion(mr.end(), mr.end());
    1.89 +    } else {
    1.90 +      // Is there an intersection with the address space?
    1.91 +      MemRegion intersection = last_setup_region().intersection(mr);
    1.92 +      if (intersection.is_empty()) {
    1.93 +        intersection = MemRegion(mr.end(), mr.end());
    1.94 +      }
    1.95 +      // All the sizes below are in words.
    1.96 +      size_t head_size = 0, tail_size = 0;
    1.97 +      if (mr.start() <= intersection.start()) {
    1.98 +        head_size = pointer_delta(intersection.start(), mr.start());
    1.99 +      }
   1.100 +      if(intersection.end() <= mr.end()) {
   1.101 +        tail_size = pointer_delta(mr.end(), intersection.end());
   1.102 +      }
   1.103 +      // Limit the amount of page manipulation if necessary.
   1.104 +      if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) {
   1.105 +        const size_t change_size = head_size + tail_size;
   1.106 +        const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord;
   1.107 +        head_size = MIN2((size_t)(setup_rate_words * head_size / change_size),
   1.108 +                         head_size);
   1.109 +        tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size),
   1.110 +                         tail_size);
   1.111 +      }
   1.112 +      head = MemRegion(intersection.start() - head_size, intersection.start());
   1.113 +      tail = MemRegion(intersection.end(), intersection.end() + tail_size);
   1.114 +    }
   1.115 +    assert(mr.contains(head) && mr.contains(tail), "Sanity");
   1.116 +
   1.117 +    if (UseNUMA) {
   1.118 +      numa_setup_pages(head, clear_space);
   1.119 +      numa_setup_pages(tail, clear_space);
   1.120 +    }
   1.121 +
   1.122 +    if (AlwaysPreTouch) {
   1.123 +      pretouch_pages(head);
   1.124 +      pretouch_pages(tail);
   1.125 +    }
   1.126 +
   1.127 +    // Remember where we stopped so that we can continue later.
   1.128 +    set_last_setup_region(MemRegion(head.start(), tail.end()));
   1.129 +  }
   1.130 +
   1.131 +  set_bottom(mr.start());
   1.132 +  set_end(mr.end());
   1.133 +
   1.134 +  if (clear_space) {
   1.135 +    clear(mangle_space);
   1.136 +  }
   1.137 +}
   1.138 +
   1.139 +void MutableSpace::clear(bool mangle_space) {
   1.140 +  set_top(bottom());
   1.141 +  if (ZapUnusedHeapArea && mangle_space) {
   1.142 +    mangle_unused_area();
   1.143 +  }
   1.144 +}
   1.145 +
   1.146 +#ifndef PRODUCT
   1.147 +void MutableSpace::check_mangled_unused_area(HeapWord* limit) {
   1.148 +  mangler()->check_mangled_unused_area(limit);
   1.149 +}
   1.150 +
   1.151 +void MutableSpace::check_mangled_unused_area_complete() {
   1.152 +  mangler()->check_mangled_unused_area_complete();
   1.153 +}
   1.154 +
   1.155 +// Mangle only the unused space that has not previously
   1.156 +// been mangled and that has not been allocated since being
   1.157 +// mangled.
   1.158 +void MutableSpace::mangle_unused_area() {
   1.159 +  mangler()->mangle_unused_area();
   1.160 +}
   1.161 +
   1.162 +void MutableSpace::mangle_unused_area_complete() {
   1.163 +  mangler()->mangle_unused_area_complete();
   1.164 +}
   1.165 +
   1.166 +void MutableSpace::mangle_region(MemRegion mr) {
   1.167 +  SpaceMangler::mangle_region(mr);
   1.168 +}
   1.169 +
   1.170 +void MutableSpace::set_top_for_allocations(HeapWord* v) {
   1.171 +  mangler()->set_top_for_allocations(v);
   1.172 +}
   1.173 +
   1.174 +void MutableSpace::set_top_for_allocations() {
   1.175 +  mangler()->set_top_for_allocations(top());
   1.176 +}
   1.177 +#endif
   1.178 +
   1.179 +// This version requires locking. */
   1.180 +HeapWord* MutableSpace::allocate(size_t size) {
   1.181 +  assert(Heap_lock->owned_by_self() ||
   1.182 +         (SafepointSynchronize::is_at_safepoint() &&
   1.183 +          Thread::current()->is_VM_thread()),
   1.184 +         "not locked");
   1.185 +  HeapWord* obj = top();
   1.186 +  if (pointer_delta(end(), obj) >= size) {
   1.187 +    HeapWord* new_top = obj + size;
   1.188 +    set_top(new_top);
   1.189 +    assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
   1.190 +           "checking alignment");
   1.191 +    return obj;
   1.192 +  } else {
   1.193 +    return NULL;
   1.194 +  }
   1.195 +}
   1.196 +
   1.197 +// This version is lock-free.
   1.198 +HeapWord* MutableSpace::cas_allocate(size_t size) {
   1.199 +  do {
   1.200 +    HeapWord* obj = top();
   1.201 +    if (pointer_delta(end(), obj) >= size) {
   1.202 +      HeapWord* new_top = obj + size;
   1.203 +      HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
   1.204 +      // result can be one of two:
   1.205 +      //  the old top value: the exchange succeeded
   1.206 +      //  otherwise: the new value of the top is returned.
   1.207 +      if (result != obj) {
   1.208 +        continue; // another thread beat us to the allocation, try again
   1.209 +      }
   1.210 +      assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
   1.211 +             "checking alignment");
   1.212 +      return obj;
   1.213 +    } else {
   1.214 +      return NULL;
   1.215 +    }
   1.216 +  } while (true);
   1.217 +}
   1.218 +
   1.219 +// Try to deallocate previous allocation. Returns true upon success.
   1.220 +bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
   1.221 +  HeapWord* expected_top = obj + size;
   1.222 +  return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top;
   1.223 +}
   1.224 +
   1.225 +void MutableSpace::oop_iterate(ExtendedOopClosure* cl) {
   1.226 +  HeapWord* obj_addr = bottom();
   1.227 +  HeapWord* t = top();
   1.228 +  // Could call objects iterate, but this is easier.
   1.229 +  while (obj_addr < t) {
   1.230 +    obj_addr += oop(obj_addr)->oop_iterate(cl);
   1.231 +  }
   1.232 +}
   1.233 +
   1.234 +void MutableSpace::oop_iterate_no_header(OopClosure* cl) {
   1.235 +  HeapWord* obj_addr = bottom();
   1.236 +  HeapWord* t = top();
   1.237 +  // Could call objects iterate, but this is easier.
   1.238 +  while (obj_addr < t) {
   1.239 +    obj_addr += oop(obj_addr)->oop_iterate_no_header(cl);
   1.240 +  }
   1.241 +}
   1.242 +
   1.243 +void MutableSpace::object_iterate(ObjectClosure* cl) {
   1.244 +  HeapWord* p = bottom();
   1.245 +  while (p < top()) {
   1.246 +    cl->do_object(oop(p));
   1.247 +    p += oop(p)->size();
   1.248 +  }
   1.249 +}
   1.250 +
   1.251 +void MutableSpace::print_short() const { print_short_on(tty); }
   1.252 +void MutableSpace::print_short_on( outputStream* st) const {
   1.253 +  st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,
   1.254 +            (int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));
   1.255 +}
   1.256 +
   1.257 +void MutableSpace::print() const { print_on(tty); }
   1.258 +void MutableSpace::print_on(outputStream* st) const {
   1.259 +  MutableSpace::print_short_on(st);
   1.260 +  st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",
   1.261 +                 bottom(), top(), end());
   1.262 +}
   1.263 +
   1.264 +void MutableSpace::verify() {
   1.265 +  HeapWord* p = bottom();
   1.266 +  HeapWord* t = top();
   1.267 +  HeapWord* prev_p = NULL;
   1.268 +  while (p < t) {
   1.269 +    oop(p)->verify();
   1.270 +    prev_p = p;
   1.271 +    p += oop(p)->size();
   1.272 +  }
   1.273 +  guarantee(p == top(), "end of last object must match end of space");
   1.274 +}

mercurial