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/logStream.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 _old_length(g1_heap->old_regions_count()), 36 _archive_length(g1_heap->archive_regions_count()), 37 _humongous_length(g1_heap->humongous_regions_count()), 38 _eden_length_per_node(NULL), 39 _survivor_length_per_node(NULL) { 40 41 uint node_count = G1NUMA::numa()->num_active_nodes(); 42 43 if (node_count > 1) { 44 LogTarget(Debug, gc, heap, numa) lt; 45 46 if (lt.is_enabled()) { 47 _eden_length_per_node = NEW_C_HEAP_ARRAY(uint, node_count, mtGC); 48 _survivor_length_per_node = NEW_C_HEAP_ARRAY(uint, node_count, mtGC); 49 50 for (uint i = 0; i < node_count; i++) { 51 _eden_length_per_node[i] = g1_heap->eden_regions_count(i); 52 _survivor_length_per_node[i] = g1_heap->survivor_regions_count(i); 53 } 54 } 55 } 56 } 57 58 G1HeapTransition::Data::~Data() { 59 FREE_C_HEAP_ARRAY(uint, _eden_length_per_node); 60 FREE_C_HEAP_ARRAY(uint, _survivor_length_per_node); 61 } 62 63 G1HeapTransition::G1HeapTransition(G1CollectedHeap* g1_heap) : _g1_heap(g1_heap), _before(g1_heap) { } 64 65 struct DetailedUsage : public StackObj { 66 size_t _eden_used; 67 size_t _survivor_used; 68 size_t _old_used; 69 size_t _archive_used; 70 size_t _humongous_used; 71 72 size_t _eden_region_count; 73 size_t _survivor_region_count; 74 size_t _old_region_count; 75 size_t _archive_region_count; 76 size_t _humongous_region_count; 77 78 DetailedUsage() : 79 _eden_used(0), _survivor_used(0), _old_used(0), _archive_used(0), _humongous_used(0), 80 _eden_region_count(0), _survivor_region_count(0), _old_region_count(0), 81 _archive_region_count(0), _humongous_region_count(0) {} 82 }; 83 84 class DetailedUsageClosure: public HeapRegionClosure { 85 public: 86 DetailedUsage _usage; 87 bool do_heap_region(HeapRegion* r) { 88 if (r->is_old()) { 89 _usage._old_used += r->used(); 90 _usage._old_region_count++; 91 } else if (r->is_archive()) { 92 _usage._archive_used += r->used(); 93 _usage._archive_region_count++; 94 } else if (r->is_survivor()) { 95 _usage._survivor_used += r->used(); 96 _usage._survivor_region_count++; 97 } else if (r->is_eden()) { 98 _usage._eden_used += r->used(); 99 _usage._eden_region_count++; 100 } else if (r->is_humongous()) { 101 _usage._humongous_used += r->used(); 102 _usage._humongous_region_count++; 103 } else { 104 assert(r->used() == 0, "Expected used to be 0 but it was " SIZE_FORMAT, r->used()); 105 } 106 return false; 107 } 108 }; 109 110 static void log_regions(const char* msg, size_t before_length, size_t after_length, size_t capacity, 111 uint* before_per_node_length, uint* after_per_node_length) { 112 LogTarget(Info, gc, heap) lt; 113 114 if (lt.is_enabled()) { 115 LogStream ls(lt); 116 117 ls.print("%s regions: " SIZE_FORMAT "->" SIZE_FORMAT "(" SIZE_FORMAT ")", 118 msg, before_length, after_length, capacity); 119 // Not NULL only if gc+heap+numa at Debug level is enabled. 120 if (before_per_node_length != NULL && after_per_node_length != NULL) { 121 G1NUMA* numa = G1NUMA::numa(); 122 uint num_nodes = numa->num_active_nodes(); 123 const int* node_ids = numa->node_ids(); 124 ls.print(" ("); 125 for (uint i = 0; i < num_nodes; i++) { 126 ls.print("%d: %u->%u", node_ids[i], before_per_node_length[i], after_per_node_length[i]); 127 // Skip adding below if it is the last one. 128 if (i != num_nodes - 1) { 129 ls.print(", "); 130 } 131 } 132 ls.print(")"); 133 } 134 ls.print_cr(""); 135 } 136 } 137 138 void G1HeapTransition::print() { 139 Data after(_g1_heap); 140 141 size_t eden_capacity_length_after_gc = _g1_heap->policy()->young_list_target_length() - after._survivor_length; 142 size_t survivor_capacity_length_before_gc = _g1_heap->policy()->max_survivor_regions(); 143 144 DetailedUsage usage; 145 if (log_is_enabled(Trace, gc, heap)) { 146 DetailedUsageClosure blk; 147 _g1_heap->heap_region_iterate(&blk); 148 usage = blk._usage; 149 assert(usage._eden_region_count == 0, "Expected no eden regions, but got " SIZE_FORMAT, usage._eden_region_count); 150 assert(usage._survivor_region_count == after._survivor_length, "Expected survivors to be " SIZE_FORMAT " but was " SIZE_FORMAT, 151 after._survivor_length, usage._survivor_region_count); 152 assert(usage._old_region_count == after._old_length, "Expected old to be " SIZE_FORMAT " but was " SIZE_FORMAT, 153 after._old_length, usage._old_region_count); 154 assert(usage._archive_region_count == after._archive_length, "Expected archive to be " SIZE_FORMAT " but was " SIZE_FORMAT, 155 after._archive_length, usage._archive_region_count); 156 assert(usage._humongous_region_count == after._humongous_length, "Expected humongous to be " SIZE_FORMAT " but was " SIZE_FORMAT, 157 after._humongous_length, usage._humongous_region_count); 158 } 159 160 log_regions("Eden", _before._eden_length, after._eden_length, eden_capacity_length_after_gc, 161 _before._eden_length_per_node, after._eden_length_per_node); 162 log_trace(gc, heap)(" Used: 0K, Waste: 0K"); 163 164 log_regions("Survivor", _before._survivor_length, after._survivor_length, survivor_capacity_length_before_gc, 165 _before._survivor_length_per_node, after._survivor_length_per_node); 166 log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K", 167 usage._survivor_used / K, ((after._survivor_length * HeapRegion::GrainBytes) - usage._survivor_used) / K); 168 169 log_info(gc, heap)("Old regions: " SIZE_FORMAT "->" SIZE_FORMAT, 170 _before._old_length, after._old_length); 171 log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K", 172 usage._old_used / K, ((after._old_length * HeapRegion::GrainBytes) - usage._old_used) / K); 173 174 log_info(gc, heap)("Archive regions: " SIZE_FORMAT "->" SIZE_FORMAT, 175 _before._archive_length, after._archive_length); 176 log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K", 177 usage._archive_used / K, ((after._archive_length * HeapRegion::GrainBytes) - usage._archive_used) / K); 178 179 log_info(gc, heap)("Humongous regions: " SIZE_FORMAT "->" SIZE_FORMAT, 180 _before._humongous_length, after._humongous_length); 181 log_trace(gc, heap)(" Used: " SIZE_FORMAT "K, Waste: " SIZE_FORMAT "K", 182 usage._humongous_used / K, ((after._humongous_length * HeapRegion::GrainBytes) - usage._humongous_used) / K); 183 184 MetaspaceUtils::print_metaspace_change(_before._meta_sizes); 185 }