1 /* 2 * Copyright (c) 2000, 2015, 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 package sun.jvm.hotspot.gc.shared; 26 27 import java.io.*; 28 import java.util.*; 29 30 import sun.jvm.hotspot.debugger.*; 31 import sun.jvm.hotspot.runtime.*; 32 import sun.jvm.hotspot.types.*; 33 import sun.jvm.hotspot.utilities.*; 34 35 public class GenCollectedHeap extends CollectedHeap { 36 private static AddressField youngGenField; 37 private static AddressField oldGenField; 38 39 private static AddressField youngGenSpecField; 40 private static AddressField oldGenSpecField; 41 42 private static GenerationFactory genFactory; 43 44 static { 45 VM.registerVMInitializedObserver(new Observer() { 46 public void update(Observable o, Object data) { 47 initialize(VM.getVM().getTypeDataBase()); 48 } 49 }); 50 } 51 52 private static synchronized void initialize(TypeDataBase db) { 53 Type type = db.lookupType("GenCollectedHeap"); 54 55 youngGenField = type.getAddressField("_young_gen"); 56 oldGenField = type.getAddressField("_old_gen"); 57 youngGenSpecField = type.getAddressField("_young_gen_spec"); 58 oldGenSpecField = type.getAddressField("_old_gen_spec"); 59 60 genFactory = new GenerationFactory(); 61 } 62 63 public GenCollectedHeap(Address addr) { 64 super(addr); 65 } 66 67 public int nGens() { 68 return 2; // Young + Old 69 } 70 71 public Generation getGen(int i) { 72 if (Assert.ASSERTS_ENABLED) { 73 Assert.that((i == 0) || (i == 1), "Index " + i + 74 " out of range (should be 0 or 1)"); 75 } 76 77 switch (i) { 78 case 0: 79 return genFactory.newObject(youngGenField.getValue(addr)); 80 case 1: 81 return genFactory.newObject(oldGenField.getValue(addr)); 82 default: 83 // no generation for i, and assertions disabled. 84 return null; 85 } 86 } 87 88 public boolean isIn(Address a) { 89 for (int i = 0; i < nGens(); i++) { 90 Generation gen = getGen(i); 91 if (gen.isIn(a)) { 92 return true; 93 } 94 } 95 96 return false; 97 } 98 99 public long capacity() { 100 long capacity = 0; 101 for (int i = 0; i < nGens(); i++) { 102 capacity += getGen(i).capacity(); 103 } 104 return capacity; 105 } 106 107 public long used() { 108 long used = 0; 109 for (int i = 0; i < nGens(); i++) { 110 used += getGen(i).used(); 111 } 112 return used; 113 } 114 115 /** Package-private access to GenerationSpecs */ 116 GenerationSpec spec(int level) { 117 if (Assert.ASSERTS_ENABLED) { 118 Assert.that((level == 0) || (level == 1), "Index " + level + 119 " out of range (should be 0 or 1)"); 120 } 121 122 if ((level != 0) && (level != 1)) { 123 return null; 124 } 125 126 if (level == 0) { 127 return (GenerationSpec) 128 VMObjectFactory.newObject(GenerationSpec.class, 129 youngGenSpecField.getAddress()); 130 } else { 131 return (GenerationSpec) 132 VMObjectFactory.newObject(GenerationSpec.class, 133 oldGenSpecField.getAddress()); 134 } 135 } 136 137 public CollectedHeapName kind() { 138 return CollectedHeapName.GEN_COLLECTED_HEAP; 139 } 140 141 public void printOn(PrintStream tty) { 142 for (int i = 0; i < nGens(); i++) { 143 tty.print("Gen " + i + ": "); 144 getGen(i).printOn(tty); 145 tty.println("Invocations: " + getGen(i).invocations()); 146 tty.println(); 147 } 148 } 149 }