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