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