1 /*
   2  * Copyright (c) 2018, 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.z;
  26 
  27 import java.util.Iterator;
  28 
  29 import sun.jvm.hotspot.debugger.Address;
  30 import sun.jvm.hotspot.runtime.VM;
  31 import sun.jvm.hotspot.runtime.VMObject;
  32 import sun.jvm.hotspot.runtime.VMObjectFactory;
  33 import sun.jvm.hotspot.types.Type;
  34 import sun.jvm.hotspot.types.TypeDataBase;
  35 
  36 public class ZPageTable extends VMObject {
  37     private static long mapFieldOffset;
  38 
  39     static {
  40         VM.registerVMInitializedObserver((o, d) -> initialize(VM.getVM().getTypeDataBase()));
  41     }
  42 
  43     static private synchronized void initialize(TypeDataBase db) {
  44         Type type = db.lookupType("ZPageTable");
  45 
  46         mapFieldOffset = type.getAddressField("_map").getOffset();
  47     }
  48 
  49     public ZPageTable(Address addr) {
  50         super(addr);
  51     }
  52 
  53     private ZGranuleMapForPageTable map() {
  54         return (ZGranuleMapForPageTable)VMObjectFactory.newObject(ZGranuleMapForPageTable.class, addr.addOffsetTo(mapFieldOffset));
  55     }
  56 
  57     private ZPageTableEntry getEntry(Address o) {
  58         return new ZPageTableEntry(map().get(o));
  59     }
  60 
  61     ZPage get(Address o) {
  62         return getEntry(o).page();
  63     }
  64 
  65     boolean is_relocating(Address o) {
  66         return getEntry(o).relocating();
  67     }
  68 
  69     private class ZPagesIterator implements Iterator<ZPage> {
  70         private ZGranuleMapForPageTable.Iterator mapIter;
  71         private ZPage next;
  72 
  73         ZPagesIterator() {
  74             mapIter = map().new Iterator();
  75             positionToNext();
  76         }
  77 
  78         private ZPage positionToNext() {
  79             ZPage current = next;
  80 
  81             // Find next
  82             ZPage found = null;
  83             while (mapIter.hasNext()) {
  84                 ZPageTableEntry entry = new ZPageTableEntry(mapIter.next());
  85                 if (!entry.isEmpty()) {
  86                     ZPage page = entry.page();
  87                     // Medium pages have repeated entries for all covered slots,
  88                     // therefore we need to compare against the current page.
  89                     if (page != null && !page.equals(current)) {
  90                         found = page;
  91                         break;
  92                     }
  93                 }
  94             }
  95 
  96             next = found;
  97 
  98             return current;
  99         }
 100 
 101         @Override
 102         public boolean hasNext() {
 103             return next != null;
 104         }
 105 
 106         @Override
 107         public ZPage next() {
 108             return positionToNext();
 109         }
 110 
 111         @Override
 112         public void remove() {
 113             /* not supported */
 114         }
 115     }
 116 
 117     abstract class ZPageFilter {
 118         public abstract boolean accept(ZPage page);
 119     }
 120 
 121     class ZPagesFilteredIterator implements Iterator<ZPage> {
 122         private ZPage next;
 123         private ZPagesIterator iter = new ZPagesIterator();
 124         private ZPageFilter filter;
 125 
 126         ZPagesFilteredIterator(ZPageFilter filter) {
 127             this.filter = filter;
 128             positionToNext();
 129         }
 130 
 131         public ZPage positionToNext() {
 132             ZPage current = next;
 133 
 134             // Find next
 135             ZPage found = null;
 136             while (iter.hasNext()) {
 137                 ZPage page = iter.next();
 138                 if (filter.accept(page)) {
 139                     found = page;
 140                     break;
 141                 }
 142             }
 143 
 144             next = found;
 145 
 146             return current;
 147         }
 148 
 149         @Override
 150         public boolean hasNext() {
 151             return next != null;
 152         }
 153 
 154         @Override
 155         public ZPage next() {
 156             return positionToNext();
 157         }
 158 
 159         @Override
 160         public void remove() {
 161             /* not supported */
 162         }
 163     }
 164 
 165     public Iterator<ZPage> iterator() {
 166         return new ZPagesIterator();
 167     }
 168 
 169     public Iterator<ZPage> activePagesIterator() {
 170         return new ZPagesFilteredIterator(new ZPageFilter() {
 171             public boolean accept(ZPage page) {
 172                 return page.is_active();
 173             }
 174         });
 175     }
 176 }