1 /*
   2  * Copyright (c) 2013, 2020, 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 "precompiled.hpp"
  26 #include "gc/g1/g1BarrierSet.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/g1ConcurrentRefine.hpp"
  29 #include "gc/g1/g1ConcurrentRefineThread.hpp"
  30 #include "gc/g1/g1DirtyCardQueue.hpp"
  31 #include "gc/g1/g1RemSet.hpp"
  32 #include "gc/g1/g1RemSetSummary.hpp"
  33 #include "gc/g1/g1YoungRemSetSamplingThread.hpp"
  34 #include "gc/g1/heapRegion.hpp"
  35 #include "gc/g1/heapRegionRemSet.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/iterator.hpp"
  38 #include "runtime/thread.inline.hpp"
  39 
  40 void G1RemSetSummary::update() {
  41   class CollectData : public ThreadClosure {
  42     G1RemSetSummary* _summary;
  43     uint _counter;
  44   public:
  45     CollectData(G1RemSetSummary * summary) : _summary(summary),  _counter(0) {}
  46     virtual void do_thread(Thread* t) {
  47       G1ConcurrentRefineThread* crt = static_cast<G1ConcurrentRefineThread*>(t);
  48       _summary->set_rs_thread_vtime(_counter, crt->vtime_accum());
  49       _counter++;
  50     }
  51   } collector(this);
  52   G1CollectedHeap* g1h = G1CollectedHeap::heap();
  53   g1h->concurrent_refine()->threads_do(&collector);
  54   _num_coarsenings = HeapRegionRemSet::n_coarsenings();
  55 
  56   set_sampling_thread_vtime(g1h->sampling_thread()->vtime_accum());
  57 }
  58 
  59 void G1RemSetSummary::set_rs_thread_vtime(uint thread, double value) {
  60   assert(_rs_threads_vtimes != NULL, "just checking");
  61   assert(thread < _num_vtimes, "just checking");
  62   _rs_threads_vtimes[thread] = value;
  63 }
  64 
  65 double G1RemSetSummary::rs_thread_vtime(uint thread) const {
  66   assert(_rs_threads_vtimes != NULL, "just checking");
  67   assert(thread < _num_vtimes, "just checking");
  68   return _rs_threads_vtimes[thread];
  69 }
  70 
  71 G1RemSetSummary::G1RemSetSummary(bool should_update) :
  72   _num_coarsenings(0),
  73   _num_vtimes(G1ConcurrentRefine::max_num_threads()),
  74   _rs_threads_vtimes(NEW_C_HEAP_ARRAY(double, _num_vtimes, mtGC)),
  75   _sampling_thread_vtime(0.0f) {
  76 
  77   memset(_rs_threads_vtimes, 0, sizeof(double) * _num_vtimes);
  78 
  79   if (should_update) {
  80     update();
  81   }
  82 }
  83 
  84 G1RemSetSummary::~G1RemSetSummary() {
  85   FREE_C_HEAP_ARRAY(double, _rs_threads_vtimes);
  86 }
  87 
  88 void G1RemSetSummary::set(G1RemSetSummary* other) {
  89   assert(other != NULL, "just checking");
  90   assert(_num_vtimes == other->_num_vtimes, "just checking");
  91 
  92   _num_coarsenings = other->num_coarsenings();
  93 
  94   memcpy(_rs_threads_vtimes, other->_rs_threads_vtimes, sizeof(double) * _num_vtimes);
  95 
  96   set_sampling_thread_vtime(other->sampling_thread_vtime());
  97 }
  98 
  99 void G1RemSetSummary::subtract_from(G1RemSetSummary* other) {
 100   assert(other != NULL, "just checking");
 101   assert(_num_vtimes == other->_num_vtimes, "just checking");
 102 
 103   _num_coarsenings = other->num_coarsenings() - _num_coarsenings;
 104 
 105   for (uint i = 0; i < _num_vtimes; i++) {
 106     set_rs_thread_vtime(i, other->rs_thread_vtime(i) - rs_thread_vtime(i));
 107   }
 108 
 109   _sampling_thread_vtime = other->sampling_thread_vtime() - _sampling_thread_vtime;
 110 }
 111 
 112 class RegionTypeCounter {
 113 private:
 114   const char* _name;
 115 
 116   size_t _rs_mem_size;
 117   size_t _cards_occupied;
 118   size_t _amount;
 119 
 120   size_t _code_root_mem_size;
 121   size_t _code_root_elems;
 122 
 123   double rs_mem_size_percent_of(size_t total) {
 124     return percent_of(_rs_mem_size, total);
 125   }
 126 
 127   double cards_occupied_percent_of(size_t total) {
 128     return percent_of(_cards_occupied, total);
 129   }
 130 
 131   double code_root_mem_size_percent_of(size_t total) {
 132     return percent_of(_code_root_mem_size, total);
 133   }
 134 
 135   double code_root_elems_percent_of(size_t total) {
 136     return percent_of(_code_root_elems, total);
 137   }
 138 
 139   size_t amount() const { return _amount; }
 140 
 141 public:
 142 
 143   RegionTypeCounter(const char* name) : _name(name), _rs_mem_size(0), _cards_occupied(0),
 144     _amount(0), _code_root_mem_size(0), _code_root_elems(0) { }
 145 
 146   void add(size_t rs_mem_size, size_t cards_occupied, size_t code_root_mem_size,
 147     size_t code_root_elems) {
 148     _rs_mem_size += rs_mem_size;
 149     _cards_occupied += cards_occupied;
 150     _code_root_mem_size += code_root_mem_size;
 151     _code_root_elems += code_root_elems;
 152     _amount++;
 153   }
 154 
 155   size_t rs_mem_size() const { return _rs_mem_size; }
 156   size_t cards_occupied() const { return _cards_occupied; }
 157 
 158   size_t code_root_mem_size() const { return _code_root_mem_size; }
 159   size_t code_root_elems() const { return _code_root_elems; }
 160 
 161   void print_rs_mem_info_on(outputStream * out, size_t total) {
 162     out->print_cr("    " SIZE_FORMAT_W(8) "%s (%5.1f%%) by " SIZE_FORMAT " %s regions",
 163         byte_size_in_proper_unit(rs_mem_size()),
 164         proper_unit_for_byte_size(rs_mem_size()),
 165         rs_mem_size_percent_of(total), amount(), _name);
 166   }
 167 
 168   void print_cards_occupied_info_on(outputStream * out, size_t total) {
 169     out->print_cr("     " SIZE_FORMAT_W(8) " (%5.1f%%) entries by " SIZE_FORMAT " %s regions",
 170         cards_occupied(), cards_occupied_percent_of(total), amount(), _name);
 171   }
 172 
 173   void print_code_root_mem_info_on(outputStream * out, size_t total) {
 174     out->print_cr("    " SIZE_FORMAT_W(8) "%s (%5.1f%%) by " SIZE_FORMAT " %s regions",
 175         byte_size_in_proper_unit(code_root_mem_size()),
 176         proper_unit_for_byte_size(code_root_mem_size()),
 177         code_root_mem_size_percent_of(total), amount(), _name);
 178   }
 179 
 180   void print_code_root_elems_info_on(outputStream * out, size_t total) {
 181     out->print_cr("     " SIZE_FORMAT_W(8) " (%5.1f%%) elements by " SIZE_FORMAT " %s regions",
 182         code_root_elems(), code_root_elems_percent_of(total), amount(), _name);
 183   }
 184 };
 185 
 186 
 187 class HRRSStatsIter: public HeapRegionClosure {
 188 private:
 189   RegionTypeCounter _young;
 190   RegionTypeCounter _humongous;
 191   RegionTypeCounter _free;
 192   RegionTypeCounter _old;
 193   RegionTypeCounter _archive;
 194   RegionTypeCounter _all;
 195 
 196   size_t _max_rs_mem_sz;
 197   HeapRegion* _max_rs_mem_sz_region;
 198 
 199   size_t total_rs_mem_sz() const            { return _all.rs_mem_size(); }
 200   size_t total_cards_occupied() const       { return _all.cards_occupied(); }
 201 
 202   size_t max_rs_mem_sz() const              { return _max_rs_mem_sz; }
 203   HeapRegion* max_rs_mem_sz_region() const  { return _max_rs_mem_sz_region; }
 204 
 205   size_t _max_code_root_mem_sz;
 206   HeapRegion* _max_code_root_mem_sz_region;
 207 
 208   size_t total_code_root_mem_sz() const     { return _all.code_root_mem_size(); }
 209   size_t total_code_root_elems() const      { return _all.code_root_elems(); }
 210 
 211   size_t max_code_root_mem_sz() const       { return _max_code_root_mem_sz; }
 212   HeapRegion* max_code_root_mem_sz_region() const { return _max_code_root_mem_sz_region; }
 213 
 214 public:
 215   HRRSStatsIter() : _young("Young"), _humongous("Humongous"),
 216     _free("Free"), _old("Old"), _archive("Archive"), _all("All"),
 217     _max_rs_mem_sz(0), _max_rs_mem_sz_region(NULL),
 218     _max_code_root_mem_sz(0), _max_code_root_mem_sz_region(NULL)
 219   {}
 220 
 221   bool do_heap_region(HeapRegion* r) {
 222     HeapRegionRemSet* hrrs = r->rem_set();
 223 
 224     // HeapRegionRemSet::mem_size() includes the
 225     // size of the strong code roots
 226     size_t rs_mem_sz = hrrs->mem_size();
 227     if (rs_mem_sz > _max_rs_mem_sz) {
 228       _max_rs_mem_sz = rs_mem_sz;
 229       _max_rs_mem_sz_region = r;
 230     }
 231     size_t occupied_cards = hrrs->occupied();
 232     size_t code_root_mem_sz = hrrs->strong_code_roots_mem_size();
 233     if (code_root_mem_sz > max_code_root_mem_sz()) {
 234       _max_code_root_mem_sz = code_root_mem_sz;
 235       _max_code_root_mem_sz_region = r;
 236     }
 237     size_t code_root_elems = hrrs->strong_code_roots_list_length();
 238 
 239     RegionTypeCounter* current = NULL;
 240     if (r->is_free()) {
 241       current = &_free;
 242     } else if (r->is_young()) {
 243       current = &_young;
 244     } else if (r->is_humongous()) {
 245       current = &_humongous;
 246     } else if (r->is_old()) {
 247       current = &_old;
 248     } else if (r->is_archive()) {
 249       current = &_archive;
 250     } else {
 251       ShouldNotReachHere();
 252     }
 253     current->add(rs_mem_sz, occupied_cards, code_root_mem_sz, code_root_elems);
 254     _all.add(rs_mem_sz, occupied_cards, code_root_mem_sz, code_root_elems);
 255 
 256     return false;
 257   }
 258 
 259   void print_summary_on(outputStream* out) {
 260     RegionTypeCounter* counters[] = { &_young, &_humongous, &_free, &_old, &_archive, NULL };
 261 
 262     out->print_cr(" Current rem set statistics");
 263     out->print_cr("  Total per region rem sets sizes = " SIZE_FORMAT "%s."
 264                   " Max = " SIZE_FORMAT "%s.",
 265                   byte_size_in_proper_unit(total_rs_mem_sz()),
 266                   proper_unit_for_byte_size(total_rs_mem_sz()),
 267                   byte_size_in_proper_unit(max_rs_mem_sz()),
 268                   proper_unit_for_byte_size(max_rs_mem_sz()));
 269     for (RegionTypeCounter** current = &counters[0]; *current != NULL; current++) {
 270       (*current)->print_rs_mem_info_on(out, total_rs_mem_sz());
 271     }
 272 
 273     out->print_cr("   Static structures = " SIZE_FORMAT "%s,"
 274                   " free_lists = " SIZE_FORMAT "%s.",
 275                   byte_size_in_proper_unit(HeapRegionRemSet::static_mem_size()),
 276                   proper_unit_for_byte_size(HeapRegionRemSet::static_mem_size()),
 277                   byte_size_in_proper_unit(HeapRegionRemSet::fl_mem_size()),
 278                   proper_unit_for_byte_size(HeapRegionRemSet::fl_mem_size()));
 279 
 280     out->print_cr("    " SIZE_FORMAT " occupied cards represented.",
 281                   total_cards_occupied());
 282     for (RegionTypeCounter** current = &counters[0]; *current != NULL; current++) {
 283       (*current)->print_cards_occupied_info_on(out, total_cards_occupied());
 284     }
 285 
 286     // Largest sized rem set region statistics
 287     HeapRegionRemSet* rem_set = max_rs_mem_sz_region()->rem_set();
 288     out->print_cr("    Region with largest rem set = " HR_FORMAT ", "
 289                   "size = " SIZE_FORMAT "%s, occupied = " SIZE_FORMAT "%s.",
 290                   HR_FORMAT_PARAMS(max_rs_mem_sz_region()),
 291                   byte_size_in_proper_unit(rem_set->mem_size()),
 292                   proper_unit_for_byte_size(rem_set->mem_size()),
 293                   byte_size_in_proper_unit(rem_set->occupied()),
 294                   proper_unit_for_byte_size(rem_set->occupied()));
 295     // Strong code root statistics
 296     HeapRegionRemSet* max_code_root_rem_set = max_code_root_mem_sz_region()->rem_set();
 297     out->print_cr("  Total heap region code root sets sizes = " SIZE_FORMAT "%s."
 298                   "  Max = " SIZE_FORMAT "%s.",
 299                   byte_size_in_proper_unit(total_code_root_mem_sz()),
 300                   proper_unit_for_byte_size(total_code_root_mem_sz()),
 301                   byte_size_in_proper_unit(max_code_root_rem_set->strong_code_roots_mem_size()),
 302                   proper_unit_for_byte_size(max_code_root_rem_set->strong_code_roots_mem_size()));
 303     for (RegionTypeCounter** current = &counters[0]; *current != NULL; current++) {
 304       (*current)->print_code_root_mem_info_on(out, total_code_root_mem_sz());
 305     }
 306 
 307     out->print_cr("    " SIZE_FORMAT " code roots represented.",
 308                   total_code_root_elems());
 309     for (RegionTypeCounter** current = &counters[0]; *current != NULL; current++) {
 310       (*current)->print_code_root_elems_info_on(out, total_code_root_elems());
 311     }
 312 
 313     out->print_cr("    Region with largest amount of code roots = " HR_FORMAT ", "
 314                   "size = " SIZE_FORMAT "%s, num_elems = " SIZE_FORMAT ".",
 315                   HR_FORMAT_PARAMS(max_code_root_mem_sz_region()),
 316                   byte_size_in_proper_unit(max_code_root_rem_set->strong_code_roots_mem_size()),
 317                   proper_unit_for_byte_size(max_code_root_rem_set->strong_code_roots_mem_size()),
 318                   max_code_root_rem_set->strong_code_roots_list_length());
 319   }
 320 };
 321 
 322 void G1RemSetSummary::print_on(outputStream* out) {
 323   out->print_cr("  Did " SIZE_FORMAT " coarsenings.", num_coarsenings());
 324   out->print_cr("  Concurrent refinement threads times (s)");
 325   out->print("     ");
 326   for (uint i = 0; i < _num_vtimes; i++) {
 327     out->print("    %5.2f", rs_thread_vtime(i));
 328   }
 329   out->cr();
 330   out->print_cr("  Concurrent sampling threads times (s)");
 331   out->print_cr("         %5.2f", sampling_thread_vtime());
 332 
 333   HRRSStatsIter blk;
 334   G1CollectedHeap::heap()->heap_region_iterate(&blk);
 335   blk.print_summary_on(out);
 336 }