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