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