1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shenandoah/shenandoahConnectionMatrix.hpp"
  26 #include "gc/shenandoah/shenandoahHeap.hpp"
  27 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  28 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "utilities/copy.hpp"
  31 
  32 ShenandoahConnectionMatrix::ShenandoahConnectionMatrix(size_t max_regions) :
  33   _stride(max_regions),
  34   _region_shift(ShenandoahHeapRegion::region_size_shift()),
  35   _matrix(NEW_C_HEAP_ARRAY(char, max_regions * max_regions, mtGC)),
  36   _magic_offset( ((uintptr_t) _matrix) - ( ((uintptr_t) ShenandoahHeap::heap()->first_region_bottom()) >> _region_shift) * (_stride + 1))
  37 
  38 {
  39   if (UseShenandoahMatrix) {
  40     clear_all();
  41   }
  42 }
  43 
  44 ShenandoahConnectionMatrix::~ShenandoahConnectionMatrix() {
  45   FREE_C_HEAP_ARRAY(char, _matrix);
  46 }
  47 
  48 size_t ShenandoahConnectionMatrix::index_of(size_t from_idx, size_t to_idx) const {
  49   assert (UseShenandoahMatrix, "call only when matrix is enabled");
  50   return from_idx * _stride + to_idx;
  51 }
  52 
  53 bool ShenandoahConnectionMatrix::is_connected(size_t from_idx, size_t to_idx) const {
  54   assert (UseShenandoahMatrix, "call only when matrix is enabled");
  55   return _matrix[index_of(from_idx, to_idx)] > 0;
  56 }
  57 
  58 void ShenandoahConnectionMatrix::clear_connected(size_t from_idx, size_t to_idx) {
  59   assert (UseShenandoahMatrix, "call only when matrix is enabled");
  60   _matrix[index_of(from_idx, to_idx)] = 0;
  61 }
  62 
  63 void ShenandoahConnectionMatrix::clear_region(size_t idx) {
  64   assert (UseShenandoahMatrix, "call only when matrix is enabled");
  65   for (uint i = 0; i < _stride; i++) {
  66     clear_connected(i, idx);
  67     clear_connected(idx, i);
  68   }
  69 }
  70 
  71 void ShenandoahConnectionMatrix::clear_region_outbound(size_t idx) {
  72   assert (UseShenandoahMatrix, "call only when matrix is enabled");
  73   for (uint i = 0; i < _stride; i++) {
  74     clear_connected(idx, i);
  75   }
  76 }
  77 
  78 void ShenandoahConnectionMatrix::clear_all() {
  79   assert (UseShenandoahMatrix, "call only when matrix is enabled");
  80   size_t count = sizeof(char) * _stride * _stride;
  81   Copy::fill_to_bytes(_matrix, count, 0);
  82 }
  83 
  84 void ShenandoahConnectionMatrix::print_on(outputStream* st) const {
  85   assert (UseShenandoahMatrix, "call only when matrix is enabled");
  86   st->print_cr("Connection Matrix:");
  87   st->print_cr("%8s, %10s, %10s, %10s, %8s, %s", "Region", "Live", "Used", "Garbage", "Refcnt", "Referenced by");
  88 
  89   ShenandoahHeap* heap = ShenandoahHeap::heap();
  90   size_t num_regions = heap->num_regions();
  91   for (uint from_idx = 0; from_idx < num_regions; from_idx++) {
  92     ShenandoahHeapRegion* r = heap->regions()->get(from_idx);
  93     if (! r->is_empty()) {
  94       uint count = 0;
  95       for (uint to_idx = 0; to_idx < _stride; to_idx++) {
  96         if (is_connected(to_idx, from_idx)) {
  97           count++;
  98         }
  99       }
 100 
 101       if (count > 0) {
 102         st->print("%8u, "SIZE_FORMAT_W(10)", "SIZE_FORMAT_W(10)", "SIZE_FORMAT_W(10)", %8u, {",
 103                 from_idx, r->get_live_data_bytes(), r->used(), r->garbage(), count);
 104         for (uint to_idx = 0; to_idx < _stride; to_idx++) {
 105           if (is_connected(to_idx, from_idx)) {
 106             st->print("%u, ", to_idx);
 107           }
 108         }
 109         st->print_cr("}");
 110       }
 111     }
 112   }
 113 }