1 /*
   2  * Copyright (c) 2016, 2019, Red Hat, 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 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 "precompiled.hpp"
  26 
  27 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  29 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  30 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  31 #include "logging/logStream.hpp"
  32 #include "runtime/orderAccess.hpp"
  33 
  34 ShenandoahFreeSet::ShenandoahFreeSet(ShenandoahHeap* heap, size_t max_regions) :
  35   _heap(heap),
  36   _mutator_free_bitmap(max_regions, mtGC),
  37   _collector_free_bitmap(max_regions, mtGC),
  38   _max(max_regions)
  39 {
  40   clear_internal();
  41 }
  42 
  43 void ShenandoahFreeSet::increase_used(size_t num_bytes) {
  44   shenandoah_assert_heaplocked();
  45   _used += num_bytes;
  46 
  47   assert(_used <= _capacity, "must not use more than we have: used: " SIZE_FORMAT
  48          ", capacity: " SIZE_FORMAT ", num_bytes: " SIZE_FORMAT, _used, _capacity, num_bytes);
  49 }
  50 
  51 bool ShenandoahFreeSet::is_mutator_free(size_t idx) const {
  52   assert (idx < _max, "index is sane: " SIZE_FORMAT " < " SIZE_FORMAT " (left: " SIZE_FORMAT ", right: " SIZE_FORMAT ")",
  53           idx, _max, _mutator_leftmost, _mutator_rightmost);
  54   return _mutator_free_bitmap.at(idx);
  55 }
  56 
  57 bool ShenandoahFreeSet::is_collector_free(size_t idx) const {
  58   assert (idx < _max, "index is sane: " SIZE_FORMAT " < " SIZE_FORMAT " (left: " SIZE_FORMAT ", right: " SIZE_FORMAT ")",
  59           idx, _max, _collector_leftmost, _collector_rightmost);
  60   return _collector_free_bitmap.at(idx);
  61 }
  62 
  63 HeapWord* ShenandoahFreeSet::allocate_single(ShenandoahAllocRequest& req, bool& in_new_region) {
  64   // Scan the bitmap looking for a first fit.
  65   //
  66   // Leftmost and rightmost bounds provide enough caching to walk bitmap efficiently. Normally,
  67   // we would find the region to allocate at right away.
  68   //
  69   // Allocations are biased: new application allocs go to beginning of the heap, and GC allocs
  70   // go to the end. This makes application allocation faster, because we would clear lots
  71   // of regions from the beginning most of the time.
  72   //
  73   // Free set maintains mutator and collector views, and normally they allocate in their views only,
  74   // unless we special cases for stealing and mixed allocations.
  75 
  76   switch (req.type()) {
  77     case ShenandoahAllocRequest::_alloc_tlab:
  78     case ShenandoahAllocRequest::_alloc_shared: {
  79 
  80       // Try to allocate in the mutator view
  81       for (size_t idx = _mutator_leftmost; idx <= _mutator_rightmost; idx++) {
  82         if (is_mutator_free(idx)) {
  83           HeapWord* result = try_allocate_in(_heap->get_region(idx), req, in_new_region);
  84           if (result != NULL) {
  85             return result;
  86           }
  87         }
  88       }
  89 
  90       // There is no recovery. Mutator does not touch collector view at all.
  91       break;
  92     }
  93     case ShenandoahAllocRequest::_alloc_gclab:
  94     case ShenandoahAllocRequest::_alloc_shared_gc: {
  95       // size_t is unsigned, need to dodge underflow when _leftmost = 0
  96 
  97       // Fast-path: try to allocate in the collector view first
  98       for (size_t c = _collector_rightmost + 1; c > _collector_leftmost; c--) {
  99         size_t idx = c - 1;
 100         if (is_collector_free(idx)) {
 101           HeapWord* result = try_allocate_in(_heap->get_region(idx), req, in_new_region);
 102           if (result != NULL) {
 103             return result;
 104           }
 105         }
 106       }
 107 
 108       // No dice. Can we borrow space from mutator view?
 109       if (!ShenandoahEvacReserveOverflow) {
 110         return NULL;
 111       }
 112 
 113       // Try to steal the empty region from the mutator view
 114       for (size_t c = _mutator_rightmost + 1; c > _mutator_leftmost; c--) {
 115         size_t idx = c - 1;
 116         if (is_mutator_free(idx)) {
 117           ShenandoahHeapRegion* r = _heap->get_region(idx);
 118           if (can_allocate_from(r)) {
 119             flip_to_gc(r);
 120             HeapWord *result = try_allocate_in(r, req, in_new_region);
 121             if (result != NULL) {
 122               return result;
 123             }
 124           }
 125         }
 126       }
 127 
 128       // No dice. Do not try to mix mutator and GC allocations, because
 129       // URWM moves due to GC allocations would expose unparsable mutator
 130       // allocations.
 131 
 132       break;
 133     }
 134     default:
 135       ShouldNotReachHere();
 136   }
 137 
 138   return NULL;
 139 }
 140 
 141 HeapWord* ShenandoahFreeSet::try_allocate_in(ShenandoahHeapRegion* r, ShenandoahAllocRequest& req, bool& in_new_region) {
 142   assert (!has_no_alloc_capacity(r), "Performance: should avoid full regions on this path: " SIZE_FORMAT, r->index());
 143 
 144   if (_heap->is_concurrent_weak_root_in_progress() &&
 145       r->is_trash()) {
 146     return NULL;
 147   }
 148 
 149   try_recycle_trashed(r);
 150 
 151   in_new_region = r->is_empty();
 152 
 153   HeapWord* result = NULL;
 154   size_t size = req.size();
 155 
 156   if (ShenandoahElasticTLAB && req.is_lab_alloc()) {
 157     size_t free = align_down(r->free() >> LogHeapWordSize, MinObjAlignment);
 158     if (size > free) {
 159       size = free;
 160     }
 161     if (size >= req.min_size()) {
 162       result = r->allocate(size, req.type());
 163       assert (result != NULL, "Allocation must succeed: free " SIZE_FORMAT ", actual " SIZE_FORMAT, free, size);
 164     }
 165   } else {
 166     result = r->allocate(size, req.type());
 167   }
 168 
 169   if (result != NULL) {
 170     // Allocation successful, bump stats:
 171     if (req.is_mutator_alloc()) {
 172       increase_used(size * HeapWordSize);
 173     }
 174 
 175     // Record actual allocation size
 176     req.set_actual_size(size);
 177 
 178     if (req.is_gc_alloc()) {
 179       r->set_update_watermark(r->top());
 180     }
 181   }
 182 
 183   if (result == NULL || has_no_alloc_capacity(r)) {
 184     // Region cannot afford this or future allocations. Retire it.
 185     //
 186     // While this seems a bit harsh, especially in the case when this large allocation does not
 187     // fit, but the next small one would, we are risking to inflate scan times when lots of
 188     // almost-full regions precede the fully-empty region where we want allocate the entire TLAB.
 189     // TODO: Record first fully-empty region, and use that for large allocations
 190 
 191     // Record the remainder as allocation waste
 192     if (req.is_mutator_alloc()) {
 193       size_t waste = r->free();
 194       if (waste > 0) {
 195         increase_used(waste);
 196         _heap->notify_mutator_alloc_words(waste >> LogHeapWordSize, true);
 197       }
 198     }
 199 
 200     size_t num = r->index();
 201     _collector_free_bitmap.clear_bit(num);
 202     _mutator_free_bitmap.clear_bit(num);
 203     // Touched the bounds? Need to update:
 204     if (touches_bounds(num)) {
 205       adjust_bounds();
 206     }
 207     assert_bounds();
 208   }
 209   return result;
 210 }
 211 
 212 bool ShenandoahFreeSet::touches_bounds(size_t num) const {
 213   return num == _collector_leftmost || num == _collector_rightmost || num == _mutator_leftmost || num == _mutator_rightmost;
 214 }
 215 
 216 void ShenandoahFreeSet::recompute_bounds() {
 217   // Reset to the most pessimistic case:
 218   _mutator_rightmost = _max - 1;
 219   _mutator_leftmost = 0;
 220   _collector_rightmost = _max - 1;
 221   _collector_leftmost = 0;
 222 
 223   // ...and adjust from there
 224   adjust_bounds();
 225 }
 226 
 227 void ShenandoahFreeSet::adjust_bounds() {
 228   // Rewind both mutator bounds until the next bit.
 229   while (_mutator_leftmost < _max && !is_mutator_free(_mutator_leftmost)) {
 230     _mutator_leftmost++;
 231   }
 232   while (_mutator_rightmost > 0 && !is_mutator_free(_mutator_rightmost)) {
 233     _mutator_rightmost--;
 234   }
 235   // Rewind both collector bounds until the next bit.
 236   while (_collector_leftmost < _max && !is_collector_free(_collector_leftmost)) {
 237     _collector_leftmost++;
 238   }
 239   while (_collector_rightmost > 0 && !is_collector_free(_collector_rightmost)) {
 240     _collector_rightmost--;
 241   }
 242 }
 243 
 244 HeapWord* ShenandoahFreeSet::allocate_contiguous(ShenandoahAllocRequest& req) {
 245   shenandoah_assert_heaplocked();
 246 
 247   size_t words_size = req.size();
 248   size_t num = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 249 
 250   // No regions left to satisfy allocation, bye.
 251   if (num > mutator_count()) {
 252     return NULL;
 253   }
 254 
 255   // Find the continuous interval of $num regions, starting from $beg and ending in $end,
 256   // inclusive. Contiguous allocations are biased to the beginning.
 257 
 258   size_t beg = _mutator_leftmost;
 259   size_t end = beg;
 260 
 261   while (true) {
 262     if (end >= _max) {
 263       // Hit the end, goodbye
 264       return NULL;
 265     }
 266 
 267     // If regions are not adjacent, then current [beg; end] is useless, and we may fast-forward.
 268     // If region is not completely free, the current [beg; end] is useless, and we may fast-forward.
 269     if (!is_mutator_free(end) || !can_allocate_from(_heap->get_region(end))) {
 270       end++;
 271       beg = end;
 272       continue;
 273     }
 274 
 275     if ((end - beg + 1) == num) {
 276       // found the match
 277       break;
 278     }
 279 
 280     end++;
 281   };
 282 
 283   size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
 284 
 285   // Initialize regions:
 286   for (size_t i = beg; i <= end; i++) {
 287     ShenandoahHeapRegion* r = _heap->get_region(i);
 288     try_recycle_trashed(r);
 289 
 290     assert(i == beg || _heap->get_region(i - 1)->index() + 1 == r->index(), "Should be contiguous");
 291     assert(r->is_empty(), "Should be empty");
 292 
 293     if (i == beg) {
 294       r->make_humongous_start();
 295     } else {
 296       r->make_humongous_cont();
 297     }
 298 
 299     // Trailing region may be non-full, record the remainder there
 300     size_t used_words;
 301     if ((i == end) && (remainder != 0)) {
 302       used_words = remainder;
 303     } else {
 304       used_words = ShenandoahHeapRegion::region_size_words();
 305     }
 306 
 307     r->set_top(r->bottom() + used_words);
 308 
 309     _mutator_free_bitmap.clear_bit(r->index());
 310   }
 311 
 312   // While individual regions report their true use, all humongous regions are
 313   // marked used in the free set.
 314   increase_used(ShenandoahHeapRegion::region_size_bytes() * num);
 315 
 316   if (remainder != 0) {
 317     // Record this remainder as allocation waste
 318     _heap->notify_mutator_alloc_words(ShenandoahHeapRegion::region_size_words() - remainder, true);
 319   }
 320 
 321   // Allocated at left/rightmost? Move the bounds appropriately.
 322   if (beg == _mutator_leftmost || end == _mutator_rightmost) {
 323     adjust_bounds();
 324   }
 325   assert_bounds();
 326 
 327   req.set_actual_size(words_size);
 328   return _heap->get_region(beg)->bottom();
 329 }
 330 
 331 bool ShenandoahFreeSet::can_allocate_from(ShenandoahHeapRegion *r) {
 332   return r->is_empty() || (r->is_trash() && !_heap->is_concurrent_weak_root_in_progress());
 333 }
 334 
 335 size_t ShenandoahFreeSet::alloc_capacity(ShenandoahHeapRegion *r) {
 336   if (r->is_trash()) {
 337     // This would be recycled on allocation path
 338     return ShenandoahHeapRegion::region_size_bytes();
 339   } else {
 340     return r->free();
 341   }
 342 }
 343 
 344 bool ShenandoahFreeSet::has_no_alloc_capacity(ShenandoahHeapRegion *r) {
 345   return alloc_capacity(r) == 0;
 346 }
 347 
 348 void ShenandoahFreeSet::try_recycle_trashed(ShenandoahHeapRegion *r) {
 349   if (r->is_trash()) {
 350     _heap->decrease_used(r->used());
 351     r->recycle();
 352   }
 353 }
 354 
 355 void ShenandoahFreeSet::recycle_trash() {
 356   // lock is not reentrable, check we don't have it
 357   shenandoah_assert_not_heaplocked();
 358 
 359   for (size_t i = 0; i < _heap->num_regions(); i++) {
 360     ShenandoahHeapRegion* r = _heap->get_region(i);
 361     if (r->is_trash()) {
 362       ShenandoahHeapLocker locker(_heap->lock());
 363       try_recycle_trashed(r);
 364     }
 365     SpinPause(); // allow allocators to take the lock
 366   }
 367 }
 368 
 369 void ShenandoahFreeSet::flip_to_gc(ShenandoahHeapRegion* r) {
 370   size_t idx = r->index();
 371 
 372   assert(_mutator_free_bitmap.at(idx), "Should be in mutator view");
 373   assert(can_allocate_from(r), "Should not be allocated");
 374 
 375   _mutator_free_bitmap.clear_bit(idx);
 376   _collector_free_bitmap.set_bit(idx);
 377   _collector_leftmost = MIN2(idx, _collector_leftmost);
 378   _collector_rightmost = MAX2(idx, _collector_rightmost);
 379 
 380   _capacity -= alloc_capacity(r);
 381 
 382   if (touches_bounds(idx)) {
 383     adjust_bounds();
 384   }
 385   assert_bounds();
 386 }
 387 
 388 void ShenandoahFreeSet::clear() {
 389   shenandoah_assert_heaplocked();
 390   clear_internal();
 391 }
 392 
 393 void ShenandoahFreeSet::clear_internal() {
 394   _mutator_free_bitmap.clear();
 395   _collector_free_bitmap.clear();
 396   _mutator_leftmost = _max;
 397   _mutator_rightmost = 0;
 398   _collector_leftmost = _max;
 399   _collector_rightmost = 0;
 400   _capacity = 0;
 401   _used = 0;
 402 }
 403 
 404 void ShenandoahFreeSet::rebuild() {
 405   shenandoah_assert_heaplocked();
 406   clear();
 407 
 408   for (size_t idx = 0; idx < _heap->num_regions(); idx++) {
 409     ShenandoahHeapRegion* region = _heap->get_region(idx);
 410     if (region->is_alloc_allowed() || region->is_trash()) {
 411       assert(!region->is_cset(), "Shouldn't be adding those to the free set");
 412 
 413       // Do not add regions that would surely fail allocation
 414       if (has_no_alloc_capacity(region)) continue;
 415 
 416       _capacity += alloc_capacity(region);
 417       assert(_used <= _capacity, "must not use more than we have");
 418 
 419       assert(!is_mutator_free(idx), "We are about to add it, it shouldn't be there already");
 420       _mutator_free_bitmap.set_bit(idx);
 421     }
 422   }
 423 
 424   // Evac reserve: reserve trailing space for evacuations
 425   size_t to_reserve = _heap->max_capacity() / 100 * ShenandoahEvacReserve;
 426   size_t reserved = 0;
 427 
 428   for (size_t idx = _heap->num_regions() - 1; idx > 0; idx--) {
 429     if (reserved >= to_reserve) break;
 430 
 431     ShenandoahHeapRegion* region = _heap->get_region(idx);
 432     if (_mutator_free_bitmap.at(idx) && can_allocate_from(region)) {
 433       _mutator_free_bitmap.clear_bit(idx);
 434       _collector_free_bitmap.set_bit(idx);
 435       size_t ac = alloc_capacity(region);
 436       _capacity -= ac;
 437       reserved += ac;
 438     }
 439   }
 440 
 441   recompute_bounds();
 442   assert_bounds();
 443 }
 444 
 445 void ShenandoahFreeSet::log_status() {
 446   shenandoah_assert_heaplocked();
 447 
 448   LogTarget(Info, gc, ergo) lt;
 449   if (lt.is_enabled()) {
 450     ResourceMark rm;
 451     LogStream ls(lt);
 452 
 453     {
 454       size_t last_idx = 0;
 455       size_t max = 0;
 456       size_t max_contig = 0;
 457       size_t empty_contig = 0;
 458 
 459       size_t total_used = 0;
 460       size_t total_free = 0;
 461       size_t total_free_ext = 0;
 462 
 463       for (size_t idx = _mutator_leftmost; idx <= _mutator_rightmost; idx++) {
 464         if (is_mutator_free(idx)) {
 465           ShenandoahHeapRegion *r = _heap->get_region(idx);
 466           size_t free = alloc_capacity(r);
 467 
 468           max = MAX2(max, free);
 469 
 470           if (r->is_empty()) {
 471             total_free_ext += free;
 472             if (last_idx + 1 == idx) {
 473               empty_contig++;
 474             } else {
 475               empty_contig = 1;
 476             }
 477           } else {
 478             empty_contig = 0;
 479           }
 480 
 481           total_used += r->used();
 482           total_free += free;
 483 
 484           max_contig = MAX2(max_contig, empty_contig);
 485           last_idx = idx;
 486         }
 487       }
 488 
 489       size_t max_humongous = max_contig * ShenandoahHeapRegion::region_size_bytes();
 490       size_t free = capacity() - used();
 491 
 492       ls.print("Free: " SIZE_FORMAT "%s, Max: " SIZE_FORMAT "%s regular, " SIZE_FORMAT "%s humongous, ",
 493                byte_size_in_proper_unit(total_free),    proper_unit_for_byte_size(total_free),
 494                byte_size_in_proper_unit(max),           proper_unit_for_byte_size(max),
 495                byte_size_in_proper_unit(max_humongous), proper_unit_for_byte_size(max_humongous)
 496       );
 497 
 498       ls.print("Frag: ");
 499       size_t frag_ext;
 500       if (total_free_ext > 0) {
 501         frag_ext = 100 - (100 * max_humongous / total_free_ext);
 502       } else {
 503         frag_ext = 0;
 504       }
 505       ls.print(SIZE_FORMAT "%% external, ", frag_ext);
 506 
 507       size_t frag_int;
 508       if (mutator_count() > 0) {
 509         frag_int = (100 * (total_used / mutator_count()) / ShenandoahHeapRegion::region_size_bytes());
 510       } else {
 511         frag_int = 0;
 512       }
 513       ls.print(SIZE_FORMAT "%% internal; ", frag_int);
 514     }
 515 
 516     {
 517       size_t max = 0;
 518       size_t total_free = 0;
 519 
 520       for (size_t idx = _collector_leftmost; idx <= _collector_rightmost; idx++) {
 521         if (is_collector_free(idx)) {
 522           ShenandoahHeapRegion *r = _heap->get_region(idx);
 523           size_t free = alloc_capacity(r);
 524           max = MAX2(max, free);
 525           total_free += free;
 526         }
 527       }
 528 
 529       ls.print_cr("Reserve: " SIZE_FORMAT "%s, Max: " SIZE_FORMAT "%s",
 530                   byte_size_in_proper_unit(total_free), proper_unit_for_byte_size(total_free),
 531                   byte_size_in_proper_unit(max),        proper_unit_for_byte_size(max));
 532     }
 533   }
 534 }
 535 
 536 HeapWord* ShenandoahFreeSet::allocate(ShenandoahAllocRequest& req, bool& in_new_region) {
 537   shenandoah_assert_heaplocked();
 538   assert_bounds();
 539 
 540   if (req.size() > ShenandoahHeapRegion::humongous_threshold_words()) {
 541     switch (req.type()) {
 542       case ShenandoahAllocRequest::_alloc_shared:
 543       case ShenandoahAllocRequest::_alloc_shared_gc:
 544         in_new_region = true;
 545         return allocate_contiguous(req);
 546       case ShenandoahAllocRequest::_alloc_gclab:
 547       case ShenandoahAllocRequest::_alloc_tlab:
 548         in_new_region = false;
 549         assert(false, "Trying to allocate TLAB larger than the humongous threshold: " SIZE_FORMAT " > " SIZE_FORMAT,
 550                req.size(), ShenandoahHeapRegion::humongous_threshold_words());
 551         return NULL;
 552       default:
 553         ShouldNotReachHere();
 554         return NULL;
 555     }
 556   } else {
 557     return allocate_single(req, in_new_region);
 558   }
 559 }
 560 
 561 size_t ShenandoahFreeSet::unsafe_peek_free() const {
 562   // Deliberately not locked, this method is unsafe when free set is modified.
 563 
 564   for (size_t index = _mutator_leftmost; index <= _mutator_rightmost; index++) {
 565     if (index < _max && is_mutator_free(index)) {
 566       ShenandoahHeapRegion* r = _heap->get_region(index);
 567       if (r->free() >= MinTLABSize) {
 568         return r->free();
 569       }
 570     }
 571   }
 572 
 573   // It appears that no regions left
 574   return 0;
 575 }
 576 
 577 void ShenandoahFreeSet::print_on(outputStream* out) const {
 578   out->print_cr("Mutator Free Set: " SIZE_FORMAT "", mutator_count());
 579   for (size_t index = _mutator_leftmost; index <= _mutator_rightmost; index++) {
 580     if (is_mutator_free(index)) {
 581       _heap->get_region(index)->print_on(out);
 582     }
 583   }
 584   out->print_cr("Collector Free Set: " SIZE_FORMAT "", collector_count());
 585   for (size_t index = _collector_leftmost; index <= _collector_rightmost; index++) {
 586     if (is_collector_free(index)) {
 587       _heap->get_region(index)->print_on(out);
 588     }
 589   }
 590 }
 591 
 592 /*
 593  * Internal fragmentation metric: describes how fragmented the heap regions are.
 594  *
 595  * It is derived as:
 596  *
 597  *               sum(used[i]^2, i=0..k)
 598  *   IF = 1 - ------------------------------
 599  *              C * sum(used[i], i=0..k)
 600  *
 601  * ...where k is the number of regions in computation, C is the region capacity, and
 602  * used[i] is the used space in the region.
 603  *
 604  * The non-linearity causes IF to be lower for the cases where the same total heap
 605  * used is densely packed. For example:
 606  *   a) Heap is completely full  => IF = 0
 607  *   b) Heap is half full, first 50% regions are completely full => IF = 0
 608  *   c) Heap is half full, each region is 50% full => IF = 1/2
 609  *   d) Heap is quarter full, first 50% regions are completely full => IF = 0
 610  *   e) Heap is quarter full, each region is 25% full => IF = 3/4
 611  *   f) Heap has one small object per each region => IF =~ 1
 612  */
 613 double ShenandoahFreeSet::internal_fragmentation() {
 614   double squared = 0;
 615   double linear = 0;
 616   int count = 0;
 617 
 618   for (size_t index = _mutator_leftmost; index <= _mutator_rightmost; index++) {
 619     if (is_mutator_free(index)) {
 620       ShenandoahHeapRegion* r = _heap->get_region(index);
 621       size_t used = r->used();
 622       squared += used * used;
 623       linear += used;
 624       count++;
 625     }
 626   }
 627 
 628   if (count > 0) {
 629     double s = squared / (ShenandoahHeapRegion::region_size_bytes() * linear);
 630     return 1 - s;
 631   } else {
 632     return 0;
 633   }
 634 }
 635 
 636 /*
 637  * External fragmentation metric: describes how fragmented the heap is.
 638  *
 639  * It is derived as:
 640  *
 641  *   EF = 1 - largest_contiguous_free / total_free
 642  *
 643  * For example:
 644  *   a) Heap is completely empty => EF = 0
 645  *   b) Heap is completely full => EF = 0
 646  *   c) Heap is first-half full => EF = 1/2
 647  *   d) Heap is half full, full and empty regions interleave => EF =~ 1
 648  */
 649 double ShenandoahFreeSet::external_fragmentation() {
 650   size_t last_idx = 0;
 651   size_t max_contig = 0;
 652   size_t empty_contig = 0;
 653 
 654   size_t free = 0;
 655 
 656   for (size_t index = _mutator_leftmost; index <= _mutator_rightmost; index++) {
 657     if (is_mutator_free(index)) {
 658       ShenandoahHeapRegion* r = _heap->get_region(index);
 659       if (r->is_empty()) {
 660         free += ShenandoahHeapRegion::region_size_bytes();
 661         if (last_idx + 1 == index) {
 662           empty_contig++;
 663         } else {
 664           empty_contig = 1;
 665         }
 666       } else {
 667         empty_contig = 0;
 668       }
 669 
 670       max_contig = MAX2(max_contig, empty_contig);
 671       last_idx = index;
 672     }
 673   }
 674 
 675   if (free > 0) {
 676     return 1 - (1.0 * max_contig * ShenandoahHeapRegion::region_size_bytes() / free);
 677   } else {
 678     return 0;
 679   }
 680 }
 681 
 682 #ifdef ASSERT
 683 void ShenandoahFreeSet::assert_bounds() const {
 684   // Performance invariants. Failing these would not break the free set, but performance
 685   // would suffer.
 686   assert (_mutator_leftmost <= _max, "leftmost in bounds: "  SIZE_FORMAT " < " SIZE_FORMAT, _mutator_leftmost,  _max);
 687   assert (_mutator_rightmost < _max, "rightmost in bounds: " SIZE_FORMAT " < " SIZE_FORMAT, _mutator_rightmost, _max);
 688 
 689   assert (_mutator_leftmost == _max || is_mutator_free(_mutator_leftmost),  "leftmost region should be free: " SIZE_FORMAT,  _mutator_leftmost);
 690   assert (_mutator_rightmost == 0   || is_mutator_free(_mutator_rightmost), "rightmost region should be free: " SIZE_FORMAT, _mutator_rightmost);
 691 
 692   size_t beg_off = _mutator_free_bitmap.get_next_one_offset(0);
 693   size_t end_off = _mutator_free_bitmap.get_next_one_offset(_mutator_rightmost + 1);
 694   assert (beg_off >= _mutator_leftmost, "free regions before the leftmost: " SIZE_FORMAT ", bound " SIZE_FORMAT, beg_off, _mutator_leftmost);
 695   assert (end_off == _max,      "free regions past the rightmost: " SIZE_FORMAT ", bound " SIZE_FORMAT,  end_off, _mutator_rightmost);
 696 
 697   assert (_collector_leftmost <= _max, "leftmost in bounds: "  SIZE_FORMAT " < " SIZE_FORMAT, _collector_leftmost,  _max);
 698   assert (_collector_rightmost < _max, "rightmost in bounds: " SIZE_FORMAT " < " SIZE_FORMAT, _collector_rightmost, _max);
 699 
 700   assert (_collector_leftmost == _max || is_collector_free(_collector_leftmost),  "leftmost region should be free: " SIZE_FORMAT,  _collector_leftmost);
 701   assert (_collector_rightmost == 0   || is_collector_free(_collector_rightmost), "rightmost region should be free: " SIZE_FORMAT, _collector_rightmost);
 702 
 703   beg_off = _collector_free_bitmap.get_next_one_offset(0);
 704   end_off = _collector_free_bitmap.get_next_one_offset(_collector_rightmost + 1);
 705   assert (beg_off >= _collector_leftmost, "free regions before the leftmost: " SIZE_FORMAT ", bound " SIZE_FORMAT, beg_off, _collector_leftmost);
 706   assert (end_off == _max,      "free regions past the rightmost: " SIZE_FORMAT ", bound " SIZE_FORMAT,  end_off, _collector_rightmost);
 707 }
 708 #endif