1 /*
   2  * Copyright (c) 2013, 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_implementation/g1/concurrentG1Refine.hpp"
  27 #include "gc_implementation/g1/concurrentG1RefineThread.hpp"
  28 #include "gc_implementation/g1/heapRegion.hpp"
  29 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  30 #include "gc_implementation/g1/g1RemSet.inline.hpp"
  31 #include "gc_implementation/g1/g1RemSetSummary.hpp"
  32 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  33 #include "runtime/thread.inline.hpp"
  34 
  35 class GetRSThreadVTimeClosure : public ThreadClosure {
  36 private:
  37   G1RemSetSummary * _summary;
  38   uint _counter;
  39 
  40 public:
  41   GetRSThreadVTimeClosure(G1RemSetSummary * summary) : ThreadClosure(), _summary(summary), _counter(0) {
  42     assert(_summary != NULL, "just checking");
  43   }
  44 
  45   virtual void do_thread(Thread *t) {
  46     ConcurrentG1RefineThread* crt = (ConcurrentG1RefineThread*) t;
  47     _summary->set_rs_thread_vtime(_counter, crt->vtime_accum());
  48     _counter++;
  49   }
  50 };
  51 
  52 void G1RemSetSummary::update() {
  53   _num_refined_cards = remset()->conc_refine_cards();
  54   DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
  55   _num_processed_buf_mutator = dcqs.processed_buffers_mut();
  56   _num_processed_buf_rs_threads = dcqs.processed_buffers_rs_thread();
  57 
  58   _num_coarsenings = HeapRegionRemSet::n_coarsenings();
  59 
  60   ConcurrentG1Refine * cg1r = G1CollectedHeap::heap()->concurrent_g1_refine();
  61   if (_rs_threads_vtimes != NULL) {
  62     GetRSThreadVTimeClosure p(this);
  63     cg1r->worker_threads_do(&p);
  64   }
  65   set_sampling_thread_vtime(cg1r->sampling_thread()->vtime_accum());
  66 }
  67 
  68 void G1RemSetSummary::set_rs_thread_vtime(uint thread, double value) {
  69   assert(_rs_threads_vtimes != NULL, "just checking");
  70   assert(thread < _num_vtimes, "just checking");
  71   _rs_threads_vtimes[thread] = value;
  72 }
  73 
  74 double G1RemSetSummary::rs_thread_vtime(uint thread) const {
  75   assert(_rs_threads_vtimes != NULL, "just checking");
  76   assert(thread < _num_vtimes, "just checking");
  77   return _rs_threads_vtimes[thread];
  78 }
  79 
  80 void G1RemSetSummary::initialize(G1RemSet * remset, uint num_workers) {
  81   assert(_rs_threads_vtimes == NULL, "just checking");
  82   assert(remset != NULL, "just checking");
  83 
  84   _remset = remset;
  85   _num_vtimes = num_workers;
  86   _rs_threads_vtimes = NEW_C_HEAP_ARRAY(double, _num_vtimes, mtGC);
  87   memset(_rs_threads_vtimes, 0, sizeof(double) * _num_vtimes);
  88 
  89   update();
  90 }
  91 
  92 void G1RemSetSummary::set(G1RemSetSummary * other) {
  93   assert(other != NULL, "just checking");
  94   assert(remset() == other->remset(), "just checking");
  95   assert(_num_vtimes == other->_num_vtimes, "just checking");
  96 
  97   _num_refined_cards = other->num_concurrent_refined_cards();
  98 
  99   _num_processed_buf_mutator = other->num_processed_buf_mutator();
 100   _num_processed_buf_rs_threads = other->num_processed_buf_rs_threads();
 101 
 102   _num_coarsenings = other->_num_coarsenings;
 103 
 104   memcpy(_rs_threads_vtimes, other->_rs_threads_vtimes, sizeof(double) * _num_vtimes);
 105 
 106   set_sampling_thread_vtime(other->sampling_thread_vtime());
 107 }
 108 
 109 void G1RemSetSummary::subtract_from(G1RemSetSummary * other) {
 110   assert(other != NULL, "just checking");
 111   assert(remset() == other->remset(), "just checking");
 112   assert(_num_vtimes == other->_num_vtimes, "just checking");
 113 
 114   _num_refined_cards = other->num_concurrent_refined_cards() - _num_refined_cards;
 115 
 116   _num_processed_buf_mutator = other->num_processed_buf_mutator() - _num_processed_buf_mutator;
 117   _num_processed_buf_rs_threads = other->num_processed_buf_rs_threads() - _num_processed_buf_rs_threads;
 118 
 119   _num_coarsenings = other->num_coarsenings() - _num_coarsenings;
 120 
 121   for (uint i = 0; i < _num_vtimes; i++) {
 122     set_rs_thread_vtime(i, other->rs_thread_vtime(i) - rs_thread_vtime(i));
 123   }
 124 
 125   _sampling_thread_vtime = other->sampling_thread_vtime() - _sampling_thread_vtime;
 126 }
 127 
 128 class HRRSStatsIter: public HeapRegionClosure {
 129 private:
 130    size_t _max_mem_sz;
 131    HeapRegion* _max_mem_sz_region;
 132 public:
 133   struct region_type_counter_t {
 134   private:
 135     size_t _mem_size;
 136     size_t _occupied;
 137     size_t _amount;
 138 
 139     static double percent_of(size_t* value, size_t total) {
 140       if (total != 0) {
 141         return ((double)*value / total) * 100.0f;
 142       } else {
 143         return 0.0f;
 144       }
 145     }
 146 
 147     double mem_size_percent_of(size_t total) {
 148       return percent_of(&_mem_size, total);
 149     }
 150 
 151     double occupied_percent_of(size_t total) {
 152       return percent_of(&_occupied, total);
 153     }
 154 
 155     size_t amount() const {
 156       return _amount;
 157     }
 158 
 159   public:
 160 
 161     region_type_counter_t() : _mem_size(0), _occupied(0), _amount(0) {
 162     }
 163 
 164     void add(size_t mem_size, size_t occupied) {
 165       _mem_size += mem_size;
 166       _occupied += occupied;
 167       _amount++;
 168     }
 169 
 170     size_t mem_size() const {
 171       return _mem_size;
 172     }
 173 
 174     size_t occupied() const {
 175       return _occupied;
 176     }
 177 
 178     void print_mem_info_on(outputStream * out, size_t total, char const * type) {
 179       out->print_cr("    %8dK (%5.1f%%) by %zd %s regions", mem_size()/K, mem_size_percent_of(total), amount(), type);
 180     }
 181 
 182     void print_occupied_info_on(outputStream * out, size_t total, char const * type) {
 183       out->print_cr("     %8d (%5.1f%%) entries by %zd %s regions", occupied(), occupied_percent_of(total), amount(), type);
 184     }
 185   };
 186 
 187 private:
 188   region_type_counter_t _young;
 189   region_type_counter_t _humonguous;
 190   region_type_counter_t _free;
 191   region_type_counter_t _other;
 192   region_type_counter_t _all;
 193 
 194 public:
 195   HRRSStatsIter() :
 196     _max_mem_sz(0), _max_mem_sz_region(NULL),
 197     _all(), _young(), _humonguous(), _free(), _other()
 198   {}
 199 
 200   bool doHeapRegion(HeapRegion* r) {
 201     size_t mem_sz = r->rem_set()->mem_size();
 202     if (mem_sz > _max_mem_sz) {
 203       _max_mem_sz = mem_sz;
 204       _max_mem_sz_region = r;
 205     }
 206     size_t occ = r->rem_set()->occupied();
 207 
 208     _all.add(mem_sz, occ);
 209     if (r->is_young()) {
 210       _young.add(mem_sz, occ);
 211     } else if (r->isHumongous()) {
 212       _humonguous.add(mem_sz, occ);
 213     } else if (r->is_empty()) {
 214       _free.add(mem_sz, occ);
 215     } else {
 216       _other.add(mem_sz, occ);
 217     }
 218 
 219     return false;
 220   }
 221 
 222   region_type_counter_t& young() {
 223     return _young;
 224   }
 225 
 226   region_type_counter_t& humonguous() {
 227     return _humonguous;
 228   }
 229 
 230   region_type_counter_t& free() {
 231     return _free;
 232   }
 233 
 234   region_type_counter_t& other() {
 235     return _other;
 236   }
 237 
 238   size_t total_mem_sz() { return _all.mem_size(); }
 239   size_t max_mem_sz() { return _max_mem_sz; }
 240   size_t occupied() { return _all.occupied(); }
 241   HeapRegion* max_mem_sz_region() { return _max_mem_sz_region; }
 242 };
 243 
 244 double calc_percentage(size_t numerator, size_t denominator) {
 245   if (denominator != 0) {
 246     return (double)numerator / denominator * 100.0;
 247   } else {
 248     return 0.0f;
 249   }
 250 }
 251 
 252 void G1RemSetSummary::print_on(outputStream * out) {
 253   out->print_cr("\n Concurrent RS processed "SIZE_FORMAT" cards",
 254                 num_concurrent_refined_cards());
 255   out->print_cr("  Of %d completed buffers:", num_processed_buf_total());
 256   out->print_cr("     %8d (%5.1f%%) by concurrent RS threads.",
 257                 num_processed_buf_total(),
 258                 calc_percentage(num_processed_buf_rs_threads(), num_processed_buf_total()));
 259   out->print_cr("     %8d (%5.1f%%) by mutator threads.",
 260                 num_processed_buf_mutator(),
 261                 calc_percentage(num_processed_buf_mutator(), num_processed_buf_total()));
 262   out->print_cr("  Concurrent RS threads times (s)");
 263   out->print("     ");
 264   for (uint i = 0; i < _num_vtimes; i++) {
 265     out->print("    %5.2f", rs_thread_vtime(i));
 266   }
 267   out->cr();
 268   out->print_cr("  Concurrent sampling threads times (s)");
 269   out->print_cr("         %5.2f", sampling_thread_vtime());
 270 
 271   HRRSStatsIter blk;
 272   G1CollectedHeap::heap()->heap_region_iterate(&blk);
 273   out->print_cr("  Total heap region rem set sizes = "SIZE_FORMAT"K."
 274                 "  Max = "SIZE_FORMAT"K.",
 275                 blk.total_mem_sz()/K, blk.max_mem_sz()/K);
 276   blk.young().print_mem_info_on(out, blk.total_mem_sz(), "Young");
 277   blk.humonguous().print_mem_info_on(out, blk.total_mem_sz(), "Humonguous");
 278   blk.free().print_mem_info_on(out, blk.total_mem_sz(), "Free");
 279   blk.other().print_mem_info_on(out, blk.total_mem_sz(), "Other");
 280   out->print_cr("  Static structures = "SIZE_FORMAT"K,"
 281                 " free_lists = "SIZE_FORMAT"K.",
 282                 HeapRegionRemSet::static_mem_size() / K,
 283                 HeapRegionRemSet::fl_mem_size() / K);
 284   out->print_cr("    "SIZE_FORMAT" occupied cards represented.",
 285                 blk.occupied());
 286   blk.young().print_occupied_info_on(out, blk.occupied(), "Young");
 287   blk.humonguous().print_occupied_info_on(out, blk.occupied(), "Humonguous");
 288   blk.free().print_occupied_info_on(out, blk.occupied(), "Free");
 289   blk.other().print_occupied_info_on(out, blk.occupied(), "Other");
 290   HeapRegion* max_mem_sz_region = blk.max_mem_sz_region();
 291   HeapRegionRemSet* rem_set = max_mem_sz_region->rem_set();
 292   out->print_cr("    Max size region = "HR_FORMAT", "
 293                 "size = "SIZE_FORMAT "K, occupied = "SIZE_FORMAT"K.",
 294                 HR_FORMAT_PARAMS(max_mem_sz_region),
 295                 (rem_set->mem_size() + K - 1)/K,
 296                 (rem_set->occupied() + K - 1)/K);
 297 
 298   out->print_cr("    Did %d coarsenings.", num_coarsenings());
 299 }