1 /*
   2  * Copyright (c) 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 package jdk.vm.ci.hotspot;
  24 
  25 import java.lang.ref.Reference;
  26 import java.lang.ref.ReferenceQueue;
  27 import java.lang.ref.WeakReference;
  28 import java.util.Arrays;
  29 import java.util.Iterator;
  30 import java.util.Map;
  31 import java.util.WeakHashMap;
  32 
  33 import jdk.vm.ci.meta.JavaKind;
  34 import jdk.vm.ci.meta.ResolvedJavaType;
  35 
  36 /**
  37  * This class manages the set of metadata roots that must be scanned during garbage collection.
  38  * Because of class redefinition Method* and ConstantPool* can be freed if they don't appear to be
  39  * in use so they must be tracked when there are live references to them from Java.
  40  *
  41  * The general theory of operation is that all {@link MetaspaceWrapperObject}s are created by
  42  * calling into the VM which calls back out to actually create the wrapper instance. During the call
  43  * the VM keeps the metadata reference alive through the use of metadata handles. Once the call
  44  * completes the wrapper object is registered here and will be scanned during metadata scanning. The
  45  * weakness of the reference to the wrapper object allows them to be reclaimed when they are no
  46  * longer used.
  47  *
  48  */
  49 public class HotSpotJVMCIMetaAccessContext {
  50 
  51     /**
  52      * The set of currently live contexts used for tracking of live metadata. Examined from the VM
  53      * during garbage collection.
  54      */
  55     private static WeakReference<?>[] allContexts = new WeakReference<?>[0];
  56 
  57     /**
  58      * This is a chunked list of metadata roots. It can be read from VM native code so it's been
  59      * marked volatile to ensure the order of updates are respected.
  60      */
  61     private volatile Object[] metadataRoots;
  62 
  63     private ChunkedList<WeakReference<MetaspaceWrapperObject>> list = new ChunkedList<>();
  64 
  65     /**
  66      * The number of weak references freed since the last time the list was shrunk.
  67      */
  68     private int freed;
  69 
  70     /**
  71      * The {@link ReferenceQueue} tracking the weak references created by this context.
  72      */
  73     private final ReferenceQueue<MetaspaceWrapperObject> queue = new ReferenceQueue<>();
  74 
  75     static synchronized void add(HotSpotJVMCIMetaAccessContext context) {
  76         for (int i = 0; i < allContexts.length; i++) {
  77             if (allContexts[i] == null || allContexts[i].get() == null) {
  78                 allContexts[i] = new WeakReference<>(context);
  79                 return;
  80             }
  81         }
  82         int index = allContexts.length;
  83         allContexts = Arrays.copyOf(allContexts, index + 2);
  84         allContexts[index] = new WeakReference<>(context);
  85     }
  86 
  87     HotSpotJVMCIMetaAccessContext() {
  88         add(this);
  89     }
  90 
  91     /**
  92      * Periodically trim the list of tracked metadata. A new list is created to replace the old to
  93      * avoid concurrent scanning issues.
  94      */
  95     private void clean() {
  96         Reference<?> ref = queue.poll();
  97         if (ref == null) {
  98             return;
  99         }
 100         while (ref != null) {
 101             freed++;
 102             ref = queue.poll();
 103         }
 104         if (freed > list.size() / 2) {
 105             ChunkedList<WeakReference<MetaspaceWrapperObject>> newList = new ChunkedList<>();
 106             for (WeakReference<MetaspaceWrapperObject> element : list) {
 107                 /*
 108                  * The referent could become null anywhere in here but it doesn't matter. It will
 109                  * get cleaned up next time.
 110                  */
 111                 if (element != null && element.get() != null) {
 112                     newList.add(element);
 113                 }
 114             }
 115             list = newList;
 116             metadataRoots = list.getHead();
 117             freed = 0;
 118         }
 119     }
 120 
 121     /**
 122      * Add a {@link MetaspaceWrapperObject} to tracked by the GC. It's assumed that the caller is
 123      * responsible for keeping the reference alive for the duration of the call. Once registration
 124      * is complete then the VM will ensure it's kept alive.
 125      *
 126      * @param metaspaceObject
 127      */
 128 
 129     public synchronized void add(MetaspaceWrapperObject metaspaceObject) {
 130         clean();
 131         list.add(new WeakReference<>(metaspaceObject, queue));
 132         if (list.getHead() != metadataRoots) {
 133             /*
 134              * The list enlarged so update the head.
 135              */
 136             metadataRoots = list.getHead();
 137         }
 138         assert isRegistered(metaspaceObject);
 139     }
 140 
 141     protected ResolvedJavaType createClass(Class<?> javaClass) {
 142         if (javaClass.isPrimitive()) {
 143             JavaKind kind = JavaKind.fromJavaClass(javaClass);
 144             return new HotSpotResolvedPrimitiveType(kind);
 145         } else {
 146             return new HotSpotResolvedObjectTypeImpl(javaClass, this);
 147         }
 148     }
 149 
 150     private final Map<Class<?>, WeakReference<ResolvedJavaType>> typeMap = new WeakHashMap<>();
 151 
 152     /**
 153      * Gets the JVMCI mirror for a {@link Class} object.
 154      *
 155      * @return the {@link ResolvedJavaType} corresponding to {@code javaClass}
 156      */
 157     public synchronized ResolvedJavaType fromClass(Class<?> javaClass) {
 158         WeakReference<ResolvedJavaType> typeRef = typeMap.get(javaClass);
 159         ResolvedJavaType type = typeRef != null ? typeRef.get() : null;
 160         if (type == null) {
 161             type = createClass(javaClass);
 162             typeMap.put(javaClass, new WeakReference<>(type));
 163         }
 164         return type;
 165     }
 166 
 167     /**
 168      * A very simple append only chunked list implementation.
 169      */
 170     static class ChunkedList<T> implements Iterable<T> {
 171         private static final int CHUNK_SIZE = 32;
 172 
 173         private static final int NEXT_CHUNK_INDEX = CHUNK_SIZE - 1;
 174 
 175         private Object[] head;
 176         private int index;
 177         private int size;
 178 
 179         ChunkedList() {
 180             head = new Object[CHUNK_SIZE];
 181             index = 0;
 182         }
 183 
 184         void add(T element) {
 185             if (index == NEXT_CHUNK_INDEX) {
 186                 Object[] newHead = new Object[CHUNK_SIZE];
 187                 newHead[index] = head;
 188                 head = newHead;
 189                 index = 0;
 190             }
 191             head[index++] = element;
 192             size++;
 193         }
 194 
 195         Object[] getHead() {
 196             return head;
 197         }
 198 
 199         public Iterator<T> iterator() {
 200             return new ChunkIterator<>();
 201         }
 202 
 203         int size() {
 204             return size;
 205         }
 206 
 207         class ChunkIterator<V> implements Iterator<V> {
 208 
 209             ChunkIterator() {
 210                 currentChunk = head;
 211                 currentIndex = -1;
 212                 next = findNext();
 213             }
 214 
 215             Object[] currentChunk;
 216             int currentIndex;
 217             V next;
 218 
 219             @SuppressWarnings("unchecked")
 220             V findNext() {
 221                 V result;
 222                 do {
 223                     currentIndex++;
 224                     if (currentIndex == NEXT_CHUNK_INDEX) {
 225                         currentChunk = (Object[]) currentChunk[currentIndex];
 226                         currentIndex = 0;
 227                         if (currentChunk == null) {
 228                             return null;
 229                         }
 230                     }
 231                     result = (V) currentChunk[currentIndex];
 232                 } while (result == null);
 233                 return result;
 234             }
 235 
 236             public boolean hasNext() {
 237                 return next != null;
 238             }
 239 
 240             public V next() {
 241                 V result = next;
 242                 next = findNext();
 243                 return result;
 244             }
 245 
 246         }
 247 
 248     }
 249 
 250     synchronized boolean isRegistered(MetaspaceWrapperObject wrapper) {
 251         for (WeakReference<MetaspaceWrapperObject> m : list) {
 252             if (m != null && m.get() == wrapper) {
 253                 return true;
 254             }
 255         }
 256         return false;
 257     }
 258 }