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

Wed, 26 Mar 2014 14:15:02 +0100

author
ehelin
date
Wed, 26 Mar 2014 14:15:02 +0100
changeset 6608
fa21c9537e6e
parent 6198
55fb97c4c58d
child 6680
78bbf4d43a14
permissions
-rw-r--r--

8035667: EventMetaspaceSummary doesn't report committed Metaspace memory
Reviewed-by: jmasa, stefank

     1 /*
     2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "utilities/macros.hpp"
    27 #if INCLUDE_ALL_GCS
    28 #include "gc_implementation/shared/mutableSpace.hpp"
    29 #include "gc_implementation/shared/spaceDecorator.hpp"
    30 #include "oops/oop.inline.hpp"
    31 #include "runtime/safepoint.hpp"
    32 #include "runtime/thread.hpp"
    33 #endif // INCLUDE_ALL_GCS
    35 MutableSpace::MutableSpace(size_t alignment): ImmutableSpace(), _top(NULL), _alignment(alignment) {
    36   assert(MutableSpace::alignment() >= 0 &&
    37          MutableSpace::alignment() % os::vm_page_size() == 0,
    38          "Space should be aligned");
    39   _mangler = new MutableSpaceMangler(this);
    40 }
    42 MutableSpace::~MutableSpace() {
    43   delete _mangler;
    44 }
    46 void MutableSpace::numa_setup_pages(MemRegion mr, bool clear_space) {
    47   if (!mr.is_empty()) {
    48     size_t page_size = UseLargePages ? alignment() : os::vm_page_size();
    49     HeapWord *start = (HeapWord*)round_to((intptr_t) mr.start(), page_size);
    50     HeapWord *end =  (HeapWord*)round_down((intptr_t) mr.end(), page_size);
    51     if (end > start) {
    52       size_t size = pointer_delta(end, start, sizeof(char));
    53       if (clear_space) {
    54         // Prefer page reallocation to migration.
    55         os::free_memory((char*)start, size, page_size);
    56       }
    57       os::numa_make_global((char*)start, size);
    58     }
    59   }
    60 }
    62 void MutableSpace::pretouch_pages(MemRegion mr) {
    63   for (volatile char *p = (char*)mr.start(); p < (char*)mr.end(); p += os::vm_page_size()) {
    64     char t = *p; *p = t;
    65   }
    66 }
    68 void MutableSpace::initialize(MemRegion mr,
    69                               bool clear_space,
    70                               bool mangle_space,
    71                               bool setup_pages) {
    73   assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()),
    74          "invalid space boundaries");
    76   if (setup_pages && (UseNUMA || AlwaysPreTouch)) {
    77     // The space may move left and right or expand/shrink.
    78     // We'd like to enforce the desired page placement.
    79     MemRegion head, tail;
    80     if (last_setup_region().is_empty()) {
    81       // If it's the first initialization don't limit the amount of work.
    82       head = mr;
    83       tail = MemRegion(mr.end(), mr.end());
    84     } else {
    85       // Is there an intersection with the address space?
    86       MemRegion intersection = last_setup_region().intersection(mr);
    87       if (intersection.is_empty()) {
    88         intersection = MemRegion(mr.end(), mr.end());
    89       }
    90       // All the sizes below are in words.
    91       size_t head_size = 0, tail_size = 0;
    92       if (mr.start() <= intersection.start()) {
    93         head_size = pointer_delta(intersection.start(), mr.start());
    94       }
    95       if(intersection.end() <= mr.end()) {
    96         tail_size = pointer_delta(mr.end(), intersection.end());
    97       }
    98       // Limit the amount of page manipulation if necessary.
    99       if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) {
   100         const size_t change_size = head_size + tail_size;
   101         const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord;
   102         head_size = MIN2((size_t)(setup_rate_words * head_size / change_size),
   103                          head_size);
   104         tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size),
   105                          tail_size);
   106       }
   107       head = MemRegion(intersection.start() - head_size, intersection.start());
   108       tail = MemRegion(intersection.end(), intersection.end() + tail_size);
   109     }
   110     assert(mr.contains(head) && mr.contains(tail), "Sanity");
   112     if (UseNUMA) {
   113       numa_setup_pages(head, clear_space);
   114       numa_setup_pages(tail, clear_space);
   115     }
   117     if (AlwaysPreTouch) {
   118       pretouch_pages(head);
   119       pretouch_pages(tail);
   120     }
   122     // Remember where we stopped so that we can continue later.
   123     set_last_setup_region(MemRegion(head.start(), tail.end()));
   124   }
   126   set_bottom(mr.start());
   127   set_end(mr.end());
   129   if (clear_space) {
   130     clear(mangle_space);
   131   }
   132 }
   134 void MutableSpace::clear(bool mangle_space) {
   135   set_top(bottom());
   136   if (ZapUnusedHeapArea && mangle_space) {
   137     mangle_unused_area();
   138   }
   139 }
   141 #ifndef PRODUCT
   142 void MutableSpace::check_mangled_unused_area(HeapWord* limit) {
   143   mangler()->check_mangled_unused_area(limit);
   144 }
   146 void MutableSpace::check_mangled_unused_area_complete() {
   147   mangler()->check_mangled_unused_area_complete();
   148 }
   150 // Mangle only the unused space that has not previously
   151 // been mangled and that has not been allocated since being
   152 // mangled.
   153 void MutableSpace::mangle_unused_area() {
   154   mangler()->mangle_unused_area();
   155 }
   157 void MutableSpace::mangle_unused_area_complete() {
   158   mangler()->mangle_unused_area_complete();
   159 }
   161 void MutableSpace::mangle_region(MemRegion mr) {
   162   SpaceMangler::mangle_region(mr);
   163 }
   165 void MutableSpace::set_top_for_allocations(HeapWord* v) {
   166   mangler()->set_top_for_allocations(v);
   167 }
   169 void MutableSpace::set_top_for_allocations() {
   170   mangler()->set_top_for_allocations(top());
   171 }
   172 #endif
   174 // This version requires locking. */
   175 HeapWord* MutableSpace::allocate(size_t size) {
   176   assert(Heap_lock->owned_by_self() ||
   177          (SafepointSynchronize::is_at_safepoint() &&
   178           Thread::current()->is_VM_thread()),
   179          "not locked");
   180   HeapWord* obj = top();
   181   if (pointer_delta(end(), obj) >= size) {
   182     HeapWord* new_top = obj + size;
   183     set_top(new_top);
   184     assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
   185            "checking alignment");
   186     return obj;
   187   } else {
   188     return NULL;
   189   }
   190 }
   192 // This version is lock-free.
   193 HeapWord* MutableSpace::cas_allocate(size_t size) {
   194   do {
   195     HeapWord* obj = top();
   196     if (pointer_delta(end(), obj) >= size) {
   197       HeapWord* new_top = obj + size;
   198       HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
   199       // result can be one of two:
   200       //  the old top value: the exchange succeeded
   201       //  otherwise: the new value of the top is returned.
   202       if (result != obj) {
   203         continue; // another thread beat us to the allocation, try again
   204       }
   205       assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
   206              "checking alignment");
   207       return obj;
   208     } else {
   209       return NULL;
   210     }
   211   } while (true);
   212 }
   214 // Try to deallocate previous allocation. Returns true upon success.
   215 bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
   216   HeapWord* expected_top = obj + size;
   217   return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top;
   218 }
   220 void MutableSpace::oop_iterate(ExtendedOopClosure* cl) {
   221   HeapWord* obj_addr = bottom();
   222   HeapWord* t = top();
   223   // Could call objects iterate, but this is easier.
   224   while (obj_addr < t) {
   225     obj_addr += oop(obj_addr)->oop_iterate(cl);
   226   }
   227 }
   229 void MutableSpace::oop_iterate_no_header(OopClosure* cl) {
   230   HeapWord* obj_addr = bottom();
   231   HeapWord* t = top();
   232   // Could call objects iterate, but this is easier.
   233   while (obj_addr < t) {
   234     obj_addr += oop(obj_addr)->oop_iterate_no_header(cl);
   235   }
   236 }
   238 void MutableSpace::object_iterate(ObjectClosure* cl) {
   239   HeapWord* p = bottom();
   240   while (p < top()) {
   241     cl->do_object(oop(p));
   242     p += oop(p)->size();
   243   }
   244 }
   246 void MutableSpace::print_short() const { print_short_on(tty); }
   247 void MutableSpace::print_short_on( outputStream* st) const {
   248   st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,
   249             (int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));
   250 }
   252 void MutableSpace::print() const { print_on(tty); }
   253 void MutableSpace::print_on(outputStream* st) const {
   254   MutableSpace::print_short_on(st);
   255   st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",
   256                  bottom(), top(), end());
   257 }
   259 void MutableSpace::verify() {
   260   HeapWord* p = bottom();
   261   HeapWord* t = top();
   262   HeapWord* prev_p = NULL;
   263   while (p < t) {
   264     oop(p)->verify();
   265     prev_p = p;
   266     p += oop(p)->size();
   267   }
   268   guarantee(p == top(), "end of last object must match end of space");
   269 }

mercurial