1 /*
   2  * Copyright (c) 2016, 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.hpp"
  27 #include "gc/g1/g1HeapTransition.hpp"
  28 #include "gc/g1/g1Policy.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/metaspace.hpp"
  31 
  32 G1HeapTransition::Data::Data(G1CollectedHeap* g1_heap) {
  33   _eden_length = g1_heap->eden_regions_count();
  34   _survivor_length = g1_heap->survivor_regions_count();
  35   _retained_length = g1_heap->survivor()->retained_length();
  36   _old_length = g1_heap->old_regions_count();
  37   _archive_length = g1_heap->archive_regions_count();
  38   _humongous_length = g1_heap->humongous_regions_count();
  39   _metaspace_used_bytes = MetaspaceUtils::used_bytes();
  40 }
  41 
  42 G1HeapTransition::G1HeapTransition(G1CollectedHeap* g1_heap) : _g1_heap(g1_heap), _before(g1_heap) { }
  43 
  44 struct DetailedUsage : public StackObj {
  45   size_t _eden_used;
  46   size_t _survivor_used;
  47   size_t _old_used;
  48   size_t _archive_used;
  49   size_t _humongous_used;
  50 
  51   size_t _eden_region_count;
  52   size_t _survivor_region_count;
  53   size_t _old_region_count;
  54   size_t _archive_region_count;
  55   size_t _humongous_region_count;
  56 
  57   DetailedUsage() :
  58     _eden_used(0), _survivor_used(0), _old_used(0), _archive_used(0), _humongous_used(0),
  59     _eden_region_count(0), _survivor_region_count(0), _old_region_count(0),
  60     _archive_region_count(0), _humongous_region_count(0) {}
  61 };
  62 
  63 class DetailedUsageClosure: public HeapRegionClosure {
  64 public:
  65   DetailedUsage _usage;
  66   bool do_heap_region(HeapRegion* r) {
  67     if (r->is_old()) {
  68       _usage._old_used += r->used();
  69       _usage._old_region_count++;
  70     } else if (r->is_archive()) {
  71       _usage._archive_used += r->used();
  72       _usage._archive_region_count++;
  73     } else if (r->is_survivor()) {
  74       _usage._survivor_used += r->used();
  75       _usage._survivor_region_count++;
  76     } else if (r->is_eden()) {
  77       _usage._eden_used += r->used();
  78       _usage._eden_region_count++;
  79     } else if (r->is_humongous()) {
  80       _usage._humongous_used += r->used();
  81       _usage._humongous_region_count++;
  82     } else {
  83       assert(r->used() == 0, "Expected used to be 0 but it was " SIZE_FORMAT, r->used());
  84     }
  85     return false;
  86   }
  87 };
  88 
  89 void G1HeapTransition::print() {
  90   Data after(_g1_heap);
  91 
  92   size_t eden_capacity_length_after_gc = _g1_heap->policy()->young_list_target_length() - after._survivor_length;
  93   size_t survivor_capacity_length_before_gc = _g1_heap->policy()->max_survivor_regions();
  94 
  95   DetailedUsage usage;
  96   if (log_is_enabled(Trace, gc, heap)) {
  97     DetailedUsageClosure blk;
  98     _g1_heap->heap_region_iterate(&blk);
  99     usage = blk._usage;
 100     assert(usage._eden_region_count == after._retained_length, "Expected eden " SIZE_FORMAT " to be same as retained region " SIZE_FORMAT,
 101            usage._eden_region_count, after._retained_length);
 102     assert((usage._survivor_region_count == after._survivor_length - after._retained_length),
 103            "Expected survivors to be " SIZE_FORMAT " but was " SIZE_FORMAT,
 104            after._survivor_length - after._retained_length, usage._survivor_region_count);
 105     assert(usage._eden_region_count + usage._survivor_region_count == after._survivor_length, "Sum (" SIZE_FORMAT ") of eden ("
 106            SIZE_FORMAT ") and survivors (" SIZE_FORMAT ") should be same as expected survivor count (" SIZE_FORMAT ")",
 107            usage._eden_region_count + usage._survivor_region_count,
 108            usage._eden_region_count, usage._survivor_region_count, after._survivor_length);
 109     assert(usage._old_region_count == after._old_length, "Expected old to be " SIZE_FORMAT " but was " SIZE_FORMAT,
 110            after._old_length, usage._old_region_count);
 111     assert(usage._archive_region_count == after._archive_length, "Expected archive to be " SIZE_FORMAT " but was " SIZE_FORMAT,
 112            after._archive_length, usage._archive_region_count);
 113     assert(usage._humongous_region_count == after._humongous_length, "Expected humongous to be " SIZE_FORMAT " but was " SIZE_FORMAT,
 114            after._humongous_length, usage._humongous_region_count);
 115   }
 116 
 117   log_info(gc, heap)("Eden regions: " SIZE_FORMAT "->" SIZE_FORMAT "("  SIZE_FORMAT ")",
 118                      _before._eden_length, after._eden_length, eden_capacity_length_after_gc);
 119   log_trace(gc, heap)(" Used: 0K, Waste: 0K");
 120 
 121   log_info(gc, heap)("Survivor regions: " SIZE_FORMAT "->" SIZE_FORMAT "("  SIZE_FORMAT ")",
 122                      _before._survivor_length, after._survivor_length, survivor_capacity_length_before_gc);
 123   log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K",
 124       usage._survivor_used / K, ((after._survivor_length * HeapRegion::GrainBytes) - usage._survivor_used) / K);
 125 
 126   log_info(gc, heap)("Old regions: " SIZE_FORMAT "->" SIZE_FORMAT,
 127                      _before._old_length, after._old_length);
 128   log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K",
 129       usage._old_used / K, ((after._old_length * HeapRegion::GrainBytes) - usage._old_used) / K);
 130 
 131   log_info(gc, heap)("Archive regions: " SIZE_FORMAT "->" SIZE_FORMAT,
 132                      _before._archive_length, after._archive_length);
 133   log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K",
 134       usage._archive_used / K, ((after._archive_length * HeapRegion::GrainBytes) - usage._archive_used) / K);
 135 
 136   log_info(gc, heap)("Humongous regions: " SIZE_FORMAT "->" SIZE_FORMAT,
 137                      _before._humongous_length, after._humongous_length);
 138   log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K",
 139       usage._humongous_used / K, ((after._humongous_length * HeapRegion::GrainBytes) - usage._humongous_used) / K);
 140 
 141   MetaspaceUtils::print_metaspace_change(_before._metaspace_used_bytes);
 142 }