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