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