src/share/vm/gc_implementation/g1/heapRegionSeq.cpp

Mon, 09 Mar 2009 13:28:46 -0700

author
xdono
date
Mon, 09 Mar 2009 13:28:46 -0700
changeset 1014
0fbdb4381b99
parent 923
569b3b226089
child 1112
96b229c54d1e
permissions
-rw-r--r--

6814575: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 03/09
Reviewed-by: katleman, tbell, ohair

     1 /*
     2  * Copyright 2001-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_heapRegionSeq.cpp.incl"
    28 // Local to this file.
    30 static int orderRegions(HeapRegion** hr1p, HeapRegion** hr2p) {
    31   if ((*hr1p)->end() <= (*hr2p)->bottom()) return -1;
    32   else if ((*hr2p)->end() <= (*hr1p)->bottom()) return 1;
    33   else if (*hr1p == *hr2p) return 0;
    34   else {
    35     assert(false, "We should never compare distinct overlapping regions.");
    36   }
    37   return 0;
    38 }
    40 HeapRegionSeq::HeapRegionSeq(const size_t max_size) :
    41   _alloc_search_start(0),
    42   // The line below is the worst bit of C++ hackery I've ever written
    43   // (Detlefs, 11/23).  You should think of it as equivalent to
    44   // "_regions(100, true)": initialize the growable array and inform it
    45   // that it should allocate its elem array(s) on the C heap.  The first
    46   // argument, however, is actually a comma expression (new-expr, 100).
    47   // The purpose of the new_expr is to inform the growable array that it
    48   // is *already* allocated on the C heap: it uses the placement syntax to
    49   // keep it from actually doing any allocation.
    50   _regions((ResourceObj::operator new (sizeof(GrowableArray<HeapRegion*>),
    51                                        (void*)&_regions,
    52                                        ResourceObj::C_HEAP),
    53             (int)max_size),
    54            true),
    55   _next_rr_candidate(0),
    56   _seq_bottom(NULL)
    57 {}
    59 // Private methods.
    61 HeapWord*
    62 HeapRegionSeq::alloc_obj_from_region_index(int ind, size_t word_size) {
    63   assert(G1CollectedHeap::isHumongous(word_size),
    64          "Allocation size should be humongous");
    65   int cur = ind;
    66   int first = cur;
    67   size_t sumSizes = 0;
    68   while (cur < _regions.length() && sumSizes < word_size) {
    69     // Loop invariant:
    70     //  For all i in [first, cur):
    71     //       _regions.at(i)->is_empty()
    72     //    && _regions.at(i) is contiguous with its predecessor, if any
    73     //  && sumSizes is the sum of the sizes of the regions in the interval
    74     //       [first, cur)
    75     HeapRegion* curhr = _regions.at(cur);
    76     if (curhr->is_empty()
    77         && !curhr->is_reserved()
    78         && (first == cur
    79             || (_regions.at(cur-1)->end() ==
    80                 curhr->bottom()))) {
    81       sumSizes += curhr->capacity() / HeapWordSize;
    82     } else {
    83       first = cur + 1;
    84       sumSizes = 0;
    85     }
    86     cur++;
    87   }
    88   if (sumSizes >= word_size) {
    89     _alloc_search_start = cur;
    90     // Mark the allocated regions as allocated.
    91     bool zf = G1CollectedHeap::heap()->allocs_are_zero_filled();
    92     HeapRegion* first_hr = _regions.at(first);
    93     for (int i = first; i < cur; i++) {
    94       HeapRegion* hr = _regions.at(i);
    95       if (zf)
    96         hr->ensure_zero_filled();
    97       {
    98         MutexLockerEx x(ZF_mon, Mutex::_no_safepoint_check_flag);
    99         hr->set_zero_fill_allocated();
   100       }
   101       size_t sz = hr->capacity() / HeapWordSize;
   102       HeapWord* tmp = hr->allocate(sz);
   103       assert(tmp != NULL, "Humongous allocation failure");
   104       MemRegion mr = MemRegion(tmp, sz);
   105       CollectedHeap::fill_with_object(mr);
   106       hr->declare_filled_region_to_BOT(mr);
   107       if (i == first) {
   108         first_hr->set_startsHumongous();
   109       } else {
   110         assert(i > first, "sanity");
   111         hr->set_continuesHumongous(first_hr);
   112       }
   113     }
   114     HeapWord* first_hr_bot = first_hr->bottom();
   115     HeapWord* obj_end = first_hr_bot + word_size;
   116     first_hr->set_top(obj_end);
   117     return first_hr_bot;
   118   } else {
   119     // If we started from the beginning, we want to know why we can't alloc.
   120     return NULL;
   121   }
   122 }
   124 void HeapRegionSeq::print_empty_runs(bool reserved_are_empty) {
   125   int empty_run = 0;
   126   int n_empty = 0;
   127   bool at_least_one_reserved = false;
   128   int empty_run_start;
   129   for (int i = 0; i < _regions.length(); i++) {
   130     HeapRegion* r = _regions.at(i);
   131     if (r->continuesHumongous()) continue;
   132     if (r->is_empty() && (reserved_are_empty || !r->is_reserved())) {
   133       assert(!r->isHumongous(), "H regions should not be empty.");
   134       if (empty_run == 0) empty_run_start = i;
   135       empty_run++;
   136       n_empty++;
   137       if (r->is_reserved()) {
   138         at_least_one_reserved = true;
   139       }
   140     } else {
   141       if (empty_run > 0) {
   142         gclog_or_tty->print("  %d:%d", empty_run_start, empty_run);
   143         if (reserved_are_empty && at_least_one_reserved)
   144           gclog_or_tty->print("(R)");
   145         empty_run = 0;
   146         at_least_one_reserved = false;
   147       }
   148     }
   149   }
   150   if (empty_run > 0) {
   151     gclog_or_tty->print(" %d:%d", empty_run_start, empty_run);
   152     if (reserved_are_empty && at_least_one_reserved) gclog_or_tty->print("(R)");
   153   }
   154   gclog_or_tty->print_cr(" [tot = %d]", n_empty);
   155 }
   157 int HeapRegionSeq::find(HeapRegion* hr) {
   158   // FIXME: optimized for adjacent regions of fixed size.
   159   int ind = hr->hrs_index();
   160   if (ind != -1) {
   161     assert(_regions.at(ind) == hr, "Mismatch");
   162   }
   163   return ind;
   164 }
   167 // Public methods.
   169 void HeapRegionSeq::insert(HeapRegion* hr) {
   170   assert(!_regions.is_full(), "Too many elements in HeapRegionSeq");
   171   if (_regions.length() == 0
   172       || _regions.top()->end() <= hr->bottom()) {
   173     hr->set_hrs_index(_regions.length());
   174     _regions.append(hr);
   175   } else {
   176     _regions.append(hr);
   177     _regions.sort(orderRegions);
   178     for (int i = 0; i < _regions.length(); i++) {
   179       _regions.at(i)->set_hrs_index(i);
   180     }
   181   }
   182   char* bot = (char*)_regions.at(0)->bottom();
   183   if (_seq_bottom == NULL || bot < _seq_bottom) _seq_bottom = bot;
   184 }
   186 size_t HeapRegionSeq::length() {
   187   return _regions.length();
   188 }
   190 size_t HeapRegionSeq::free_suffix() {
   191   size_t res = 0;
   192   int first = _regions.length() - 1;
   193   int cur = first;
   194   while (cur >= 0 &&
   195          (_regions.at(cur)->is_empty()
   196           && !_regions.at(cur)->is_reserved()
   197           && (first == cur
   198               || (_regions.at(cur+1)->bottom() ==
   199                   _regions.at(cur)->end())))) {
   200       res++;
   201       cur--;
   202   }
   203   return res;
   204 }
   206 HeapWord* HeapRegionSeq::obj_allocate(size_t word_size) {
   207   int cur = _alloc_search_start;
   208   // Make sure "cur" is a valid index.
   209   assert(cur >= 0, "Invariant.");
   210   HeapWord* res = alloc_obj_from_region_index(cur, word_size);
   211   if (res == NULL)
   212     res = alloc_obj_from_region_index(0, word_size);
   213   return res;
   214 }
   216 void HeapRegionSeq::iterate(HeapRegionClosure* blk) {
   217   iterate_from((HeapRegion*)NULL, blk);
   218 }
   220 // The first argument r is the heap region at which iteration begins.
   221 // This operation runs fastest when r is NULL, or the heap region for
   222 // which a HeapRegionClosure most recently returned true, or the
   223 // heap region immediately to its right in the sequence.  In all
   224 // other cases a linear search is required to find the index of r.
   226 void HeapRegionSeq::iterate_from(HeapRegion* r, HeapRegionClosure* blk) {
   228   // :::: FIXME ::::
   229   // Static cache value is bad, especially when we start doing parallel
   230   // remembered set update. For now just don't cache anything (the
   231   // code in the def'd out blocks).
   233 #if 0
   234   static int cached_j = 0;
   235 #endif
   236   int len = _regions.length();
   237   int j = 0;
   238   // Find the index of r.
   239   if (r != NULL) {
   240 #if 0
   241     assert(cached_j >= 0, "Invariant.");
   242     if ((cached_j < len) && (r == _regions.at(cached_j))) {
   243       j = cached_j;
   244     } else if ((cached_j + 1 < len) && (r == _regions.at(cached_j + 1))) {
   245       j = cached_j + 1;
   246     } else {
   247       j = find(r);
   248 #endif
   249       if (j < 0) {
   250         j = 0;
   251       }
   252 #if 0
   253     }
   254 #endif
   255   }
   256   int i;
   257   for (i = j; i < len; i += 1) {
   258     int res = blk->doHeapRegion(_regions.at(i));
   259     if (res) {
   260 #if 0
   261       cached_j = i;
   262 #endif
   263       blk->incomplete();
   264       return;
   265     }
   266   }
   267   for (i = 0; i < j; i += 1) {
   268     int res = blk->doHeapRegion(_regions.at(i));
   269     if (res) {
   270 #if 0
   271       cached_j = i;
   272 #endif
   273       blk->incomplete();
   274       return;
   275     }
   276   }
   277 }
   279 void HeapRegionSeq::iterate_from(int idx, HeapRegionClosure* blk) {
   280   int len = _regions.length();
   281   int i;
   282   for (i = idx; i < len; i++) {
   283     if (blk->doHeapRegion(_regions.at(i))) {
   284       blk->incomplete();
   285       return;
   286     }
   287   }
   288   for (i = 0; i < idx; i++) {
   289     if (blk->doHeapRegion(_regions.at(i))) {
   290       blk->incomplete();
   291       return;
   292     }
   293   }
   294 }
   296 MemRegion HeapRegionSeq::shrink_by(size_t shrink_bytes,
   297                                    size_t& num_regions_deleted) {
   298   assert(shrink_bytes % os::vm_page_size() == 0, "unaligned");
   299   assert(shrink_bytes % HeapRegion::GrainBytes == 0, "unaligned");
   301   if (_regions.length() == 0) {
   302     num_regions_deleted = 0;
   303     return MemRegion();
   304   }
   305   int j = _regions.length() - 1;
   306   HeapWord* end = _regions.at(j)->end();
   307   HeapWord* last_start = end;
   308   while (j >= 0 && shrink_bytes > 0) {
   309     HeapRegion* cur = _regions.at(j);
   310     // We have to leave humongous regions where they are,
   311     // and work around them.
   312     if (cur->isHumongous()) {
   313       return MemRegion(last_start, end);
   314     }
   315     cur->reset_zero_fill();
   316     assert(cur == _regions.top(), "Should be top");
   317     if (!cur->is_empty()) break;
   318     shrink_bytes -= cur->capacity();
   319     num_regions_deleted++;
   320     _regions.pop();
   321     last_start = cur->bottom();
   322     // We need to delete these somehow, but can't currently do so here: if
   323     // we do, the ZF thread may still access the deleted region.  We'll
   324     // leave this here as a reminder that we have to do something about
   325     // this.
   326     // delete cur;
   327     j--;
   328   }
   329   return MemRegion(last_start, end);
   330 }
   333 class PrintHeapRegionClosure : public  HeapRegionClosure {
   334 public:
   335   bool doHeapRegion(HeapRegion* r) {
   336     gclog_or_tty->print(PTR_FORMAT ":", r);
   337     r->print();
   338     return false;
   339   }
   340 };
   342 void HeapRegionSeq::print() {
   343   PrintHeapRegionClosure cl;
   344   iterate(&cl);
   345 }

mercurial