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