1 /*
   2  * Copyright (c) 2000, 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 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.gc.shared.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.types.*;
  34 import sun.jvm.hotspot.utilities.*;
  35 
  36 abstract public class GenCollectedHeap extends CollectedHeap {
  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     youngGenSpecField = type.getAddressField("_young_gen_spec");
  59     oldGenSpecField = type.getAddressField("_old_gen_spec");
  60 
  61     genFactory = new GenerationFactory();
  62   }
  63 
  64   public GenCollectedHeap(Address addr) {
  65     super(addr);
  66   }
  67 
  68   public int nGens() {
  69     return 2; // Young + Old
  70   }
  71 
  72   public Generation getGen(int i) {
  73     if (Assert.ASSERTS_ENABLED) {
  74       Assert.that((i == 0) || (i == 1), "Index " + i +
  75                   " out of range (should be 0 or 1)");
  76     }
  77 
  78     switch (i) {
  79     case 0:
  80       return genFactory.newObject(youngGenField.getValue(addr));
  81     case 1:
  82       return genFactory.newObject(oldGenField.getValue(addr));
  83     default:
  84       // no generation for i, and assertions disabled.
  85       return null;
  86     }
  87   }
  88 
  89   public boolean isIn(Address a) {
  90     for (int i = 0; i < nGens(); i++) {
  91       Generation gen = getGen(i);
  92       if (gen.isIn(a)) {
  93         return true;
  94       }
  95     }
  96 
  97     return false;
  98   }
  99 
 100   public long capacity() {
 101     long capacity = 0;
 102     for (int i = 0; i < nGens(); i++) {
 103       capacity += getGen(i).capacity();
 104     }
 105     return capacity;
 106   }
 107 
 108   public long used() {
 109     long used = 0;
 110     for (int i = 0; i < nGens(); i++) {
 111       used += getGen(i).used();
 112     }
 113     return used;
 114   }
 115 
 116   /** Package-private access to GenerationSpecs */
 117   GenerationSpec spec(int level) {
 118     if (Assert.ASSERTS_ENABLED) {
 119       Assert.that((level == 0) || (level == 1), "Index " + level +
 120                   " out of range (should be 0 or 1)");
 121     }
 122 
 123     if ((level != 0) && (level != 1)) {
 124       return null;
 125     }
 126 
 127     if (level == 0) {
 128       return (GenerationSpec)
 129               VMObjectFactory.newObject(GenerationSpec.class,
 130                       youngGenSpecField.getAddress());
 131     } else {
 132       return (GenerationSpec)
 133               VMObjectFactory.newObject(GenerationSpec.class,
 134                       oldGenSpecField.getAddress());
 135     }
 136   }
 137 
 138   public void liveRegionsIterate(LiveRegionsClosure closure) {
 139     // Run through all generations, obtaining bottom-top pairs.
 140     for (int i = 0; i < nGens(); i++) {
 141       Generation gen = getGen(i);
 142       gen.liveRegionsIterate(closure);
 143     }
 144   }
 145 
 146   public void printOn(PrintStream tty) {
 147     for (int i = 0; i < nGens(); i++) {
 148       tty.print("Gen " + i + ": ");
 149       getGen(i).printOn(tty);
 150       tty.println("Invocations: " + getGen(i).invocations());
 151       tty.println();
 152     }
 153   }
 154 }