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 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.memory.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.types.*;
  33 
  34 /** <P> The (supported) Generation hierarchy currently looks like this: </P>
  35 
  36     <ul>
  37     <li> Generation
  38       <ul>
  39       <li> CardGeneration
  40         <ul>
  41         <li> TenuredGeneration
  42         </ul>
  43       <li> DefNewGeneration
  44       </ul>
  45     </ul>
  46 */
  47 
  48 
  49 public abstract class Generation extends VMObject {
  50   private static long          reservedFieldOffset;
  51   private static long          virtualSpaceFieldOffset;
  52   protected static final int  K = 1024;
  53   // Fields for class StatRecord
  54   private static Field         statRecordField;
  55   private static CIntegerField invocationField;
  56 
  57   // constants from Name enum
  58   private static int NAME_DEF_NEW;
  59   private static int NAME_PAR_NEW;
  60   private static int NAME_MARK_SWEEP_COMPACT;
  61   private static int NAME_CONCURRENT_MARK_SWEEP;
  62   private static int NAME_OTHER;
  63 
  64   static {
  65     VM.registerVMInitializedObserver(new Observer() {
  66         public void update(Observable o, Object data) {
  67           initialize(VM.getVM().getTypeDataBase());
  68         }
  69       });
  70   }
  71 
  72   private static synchronized void initialize(TypeDataBase db) {
  73     Type type = db.lookupType("Generation");
  74 
  75     reservedFieldOffset     = type.getField("_reserved").getOffset();
  76     virtualSpaceFieldOffset = type.getField("_virtual_space").getOffset();
  77     // StatRecord
  78     statRecordField         = type.getField("_stat_record");
  79     type                    = db.lookupType("Generation::StatRecord");
  80     invocationField         = type.getCIntegerField("invocations");
  81 
  82     // constants from Generation::Name
  83     NAME_DEF_NEW = db.lookupIntConstant("Generation::DefNew").intValue();
  84     NAME_PAR_NEW = db.lookupIntConstant("Generation::ParNew").intValue();
  85     NAME_MARK_SWEEP_COMPACT = db.lookupIntConstant("Generation::MarkSweepCompact").intValue();
  86     NAME_CONCURRENT_MARK_SWEEP = db.lookupIntConstant("Generation::ConcurrentMarkSweep").intValue();
  87     NAME_OTHER = db.lookupIntConstant("Generation::Other").intValue();
  88   }
  89 
  90   public Generation(Address addr) {
  91     super(addr);
  92   }
  93 
  94   public static class Name {
  95     public static final Name DEF_NEW = new Name("DefNew");
  96     public static final Name PAR_NEW = new Name("ParNew");
  97     public static final Name MARK_SWEEP_COMPACT = new Name("MarkSweepCompact");
  98     public static final Name CONCURRENT_MARK_SWEEP = new Name("ConcurrentMarkSweep");
  99     public static final Name OTHER = new Name("Other");
 100 
 101     private Name(String value) {
 102       this.value = value;
 103     }
 104 
 105     private String value;
 106     public String toString() {
 107       return value;
 108     }
 109   }
 110 
 111   public Generation.Name kind() {
 112     return Generation.Name.OTHER;
 113   }
 114 
 115   static Generation.Name nameForEnum(int value) {
 116      if (value == NAME_DEF_NEW) {
 117         return Name.DEF_NEW;
 118      } else if (value == NAME_PAR_NEW) {
 119         return Name.PAR_NEW;
 120      } else if (value == NAME_MARK_SWEEP_COMPACT) {
 121         return Name.MARK_SWEEP_COMPACT;
 122      } else if (value == NAME_CONCURRENT_MARK_SWEEP) {
 123         return Name.CONCURRENT_MARK_SWEEP;
 124      } else if (value == NAME_OTHER) {
 125         return Name.OTHER;
 126      } else {
 127         throw new RuntimeException("should not reach here");
 128      }
 129   }
 130 
 131   public int invocations() {
 132     return getStatRecord().getInvocations();
 133   }
 134 
 135   /** The maximum number of object bytes the generation can currently
 136       hold. */
 137   public abstract long capacity();
 138 
 139   /** The number of used bytes in the gen. */
 140   public abstract long used();
 141 
 142   /** The number of free bytes in the gen. */
 143   public abstract long free();
 144 
 145   /** The largest number of contiguous free words in the generation,
 146       including expansion. (VM's version assumes it is called at a
 147       safepoint.)  */
 148   public abstract long contiguousAvailable();
 149 
 150   public MemRegion reserved() {
 151     return new MemRegion(addr.addOffsetTo(reservedFieldOffset));
 152   }
 153 
 154   /** Returns a region guaranteed to contain all the objects in the
 155       generation. */
 156   public MemRegion usedRegion() {
 157     return reserved();
 158   }
 159 
 160   /* Returns "TRUE" iff "p" points into an allocated object in the
 161      generation. */
 162   public boolean isIn(Address p) {
 163     GenerationIsInClosure blk = new GenerationIsInClosure(p);
 164     spaceIterate(blk);
 165     return (blk.space() != null);
 166   }
 167 
 168   /** Returns "TRUE" iff "p" points into the reserved area of the
 169      generation. */
 170   public boolean isInReserved(Address p) {
 171     return reserved().contains(p);
 172   }
 173 
 174   protected VirtualSpace virtualSpace() {
 175     return (VirtualSpace) VMObjectFactory.newObject(VirtualSpace.class, addr.addOffsetTo(virtualSpaceFieldOffset));
 176   }
 177 
 178   public abstract String name();
 179 
 180   /** Equivalent to spaceIterate(blk, false) */
 181   public void spaceIterate(SpaceClosure blk) {
 182     spaceIterate(blk, false);
 183   }
 184 
 185   /** Iteration - do not use for time critical operations */
 186   public abstract void spaceIterate(SpaceClosure blk, boolean usedOnly);
 187 
 188   public void print() { printOn(System.out); }
 189   public abstract void printOn(PrintStream tty);
 190 
 191   public static class StatRecord extends VMObject {
 192     public StatRecord(Address addr) {
 193       super(addr);
 194     }
 195 
 196     public int getInvocations() {
 197       return (int) invocationField.getValue(addr);
 198     }
 199 
 200   }
 201 
 202   private StatRecord getStatRecord() {
 203     return (StatRecord) VMObjectFactory.newObject(Generation.StatRecord.class, addr.addOffsetTo(statRecordField.getOffset()));
 204   }
 205 }