1 /*
   2  * Copyright (c) 2003, 2018, 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.parallel;
  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.memory.MemRegion;
  33 import sun.jvm.hotspot.runtime.*;
  34 import sun.jvm.hotspot.types.*;
  35 
  36 public class ParallelScavengeHeap extends CollectedHeap {
  37    static {
  38       VM.registerVMInitializedObserver(new Observer() {
  39          public void update(Observable o, Object data) {
  40             initialize(VM.getVM().getTypeDataBase());
  41          }
  42       });
  43    }
  44 
  45    private static synchronized void initialize(TypeDataBase db) {
  46       Type type = db.lookupType("ParallelScavengeHeap");
  47       youngGenField = type.getAddressField("_young_gen");
  48       oldGenField    = type.getAddressField("_old_gen");
  49    }
  50 
  51    public ParallelScavengeHeap(Address addr) {
  52       super(addr);
  53    }
  54 
  55    // Fields
  56    private static AddressField youngGenField;
  57    private static AddressField oldGenField;
  58 
  59    // Accessors
  60    public PSYoungGen youngGen() {
  61       return (PSYoungGen) VMObjectFactory.newObject(PSYoungGen.class, youngGenField.getValue());
  62    }
  63 
  64    public PSOldGen oldGen() {
  65       return (PSOldGen) VMObjectFactory.newObject(PSOldGen.class, oldGenField.getValue());
  66    }
  67 
  68    public long capacity() {
  69       return youngGen().capacity() + oldGen().capacity();
  70    }
  71 
  72    public long used() {
  73       return youngGen().used() + oldGen().used();
  74    }
  75 
  76    public boolean isIn(Address a) {
  77       if (youngGen().isIn(a)) {
  78          return true;
  79       }
  80 
  81       if (oldGen().isIn(a)) {
  82          return true;
  83       }
  84 
  85       return false;
  86    }
  87 
  88    public CollectedHeapName kind() {
  89       return CollectedHeapName.PARALLEL;
  90    }
  91 
  92    // Simple wrapper to provide toString() usable for debugging.
  93    private class LiveRegionProviderImpl implements LiveRegionsProvider {
  94       private String name;
  95       private MutableSpace space;
  96 
  97       public LiveRegionProviderImpl(String name, MutableSpace space) {
  98          this.name = name;
  99          this.space = space;
 100       }
 101 
 102       @Override
 103       public List<MemRegion> getLiveRegions() {
 104          return space.getLiveRegions();
 105       }
 106       @Override
 107       public String toString() {
 108          return name;
 109       }
 110    }
 111 
 112    public void liveRegionsIterate(LiveRegionsClosure closure) {
 113       // Add eden space
 114       closure.doLiveRegions(new LiveRegionProviderImpl("eden", youngGen().edenSpace()));
 115       // Add from-space but not to-space
 116       closure.doLiveRegions(new LiveRegionProviderImpl("from", youngGen().fromSpace()));
 117 
 118       closure.doLiveRegions(new LiveRegionProviderImpl("old ", oldGen().objectSpace()));
 119    }
 120 
 121    public void printOn(PrintStream tty) {
 122       tty.print("ParallelScavengeHeap [ ");
 123       youngGen().printOn(tty);
 124       oldGen().printOn(tty);
 125       tty.print(" ] ");
 126    }
 127 }