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