|
| 1 | +/* |
| 2 | + * (C) Copyright Department of Computer Science, |
| 3 | + * Australian National University. 2002 |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.ibm.JikesRVM.memoryManagers.JMTk; |
| 8 | + |
| 9 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.VM_Interface; |
| 10 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.VM_CollectorThread; |
| 11 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.Constants; |
| 12 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.AddressSet; |
| 13 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.AddressPairSet; |
| 14 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.AddressTripleSet; |
| 15 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.ScanObject; |
| 16 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.ScanThread; |
| 17 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.ScanStatics; |
| 18 | +import com.ibm.JikesRVM.memoryManagers.vmInterface.SynchronizationBarrier; |
| 19 | + |
| 20 | +import com.ibm.JikesRVM.VM; |
| 21 | +import com.ibm.JikesRVM.VM_Time; |
| 22 | +import com.ibm.JikesRVM.VM_Address; |
| 23 | +import com.ibm.JikesRVM.VM_Offset; |
| 24 | +import com.ibm.JikesRVM.VM_Magic; |
| 25 | +import com.ibm.JikesRVM.VM_Uninterruptible; |
| 26 | +import com.ibm.JikesRVM.VM_PragmaInline; |
| 27 | +import com.ibm.JikesRVM.VM_PragmaNoInline; |
| 28 | +import com.ibm.JikesRVM.VM_PragmaUninterruptible; |
| 29 | +import com.ibm.JikesRVM.VM_PragmaInterruptible; |
| 30 | +import com.ibm.JikesRVM.VM_Processor; |
| 31 | +import com.ibm.JikesRVM.VM_Scheduler; |
| 32 | +import com.ibm.JikesRVM.VM_Thread; |
| 33 | + |
| 34 | +/** |
| 35 | + * |
| 36 | + * @author <a href="http://cs.anu.edu.au/~Steve.Blackburn">Steve Blackburn</a> |
| 37 | + * @version $Revision$ |
| 38 | + * @date $Date$ |
| 39 | + */ |
| 40 | +public abstract class BasePlan implements Constants, VM_Uninterruptible { |
| 41 | + |
| 42 | + public final static String Id = "$Id$"; |
| 43 | + |
| 44 | + public static int verbose = 0; |
| 45 | + |
| 46 | + protected WorkQueue workQueue; |
| 47 | + public AddressSet values; // gray objects |
| 48 | + public AddressSet locations; // locations containing white objects |
| 49 | + public AddressPairSet interiorLocations; // interior locations |
| 50 | + // private AddressQueue values; // gray objects |
| 51 | + // private AddressPairQueue interiorLocations; // interior locations |
| 52 | + |
| 53 | + BasePlan() { |
| 54 | + workQueue = new WorkQueue(); |
| 55 | + values = new AddressSet(64 * 1024); |
| 56 | + locations = new AddressSet(32 * 1024); |
| 57 | + interiorLocations = new AddressPairSet(16 * 1024); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * The boot method is called early in the boot process before any allocation. |
| 62 | + */ |
| 63 | + static public void boot() throws VM_PragmaInterruptible { |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * The boot method is called by the runtime immediately after command-line |
| 68 | + * arguments are available. Note that allocation must be supported |
| 69 | + * prior to this point because the runtime infrastructure may |
| 70 | + * require allocation in order to parse the command line arguments. |
| 71 | + * For this reason all plans should operate gracefully on the |
| 72 | + * default minimum heap size until the point that boot is called. |
| 73 | + */ |
| 74 | + static public void postBoot() { |
| 75 | + } |
| 76 | + |
| 77 | + protected static boolean gcInProgress = false; // This flag should be turned on/off by subclasses. |
| 78 | + protected static int gcCount = 0; |
| 79 | + |
| 80 | + static public boolean gcInProgress() { |
| 81 | + return gcInProgress; |
| 82 | + } |
| 83 | + |
| 84 | + static public int gcCount() { |
| 85 | + return gcCount; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Prepare for a collection. In this case, it means flipping |
| 90 | + * semi-spaces and preparing each of the collectors. |
| 91 | + */ |
| 92 | + protected final void prepare() { |
| 93 | + SynchronizationBarrier barrier = VM_CollectorThread.gcBarrier; |
| 94 | + double tmp = VM_Time.now(); |
| 95 | + int id = barrier.rendezvous(); |
| 96 | + if (id == 1) { |
| 97 | + gcInProgress = true; |
| 98 | + gcCount++; |
| 99 | + startTime = tmp; |
| 100 | + singlePrepare(); |
| 101 | + resetComputeRoots(); |
| 102 | + VM_Interface.prepareNonParticipating(); // The will fix collector threads that are not participating in thie GC. |
| 103 | + } |
| 104 | + VM_Interface.prepareParticipating(); // Every participating thread needs to adjust its context registers. |
| 105 | + barrier.rendezvous(); |
| 106 | + allPrepare(); |
| 107 | + barrier.rendezvous(); |
| 108 | + } |
| 109 | + |
| 110 | + protected final void release() { |
| 111 | + SynchronizationBarrier barrier = VM_CollectorThread.gcBarrier; |
| 112 | + barrier.rendezvous(); |
| 113 | + allRelease(); |
| 114 | + int id = barrier.rendezvous(); |
| 115 | + if (id == 1) { |
| 116 | + singleRelease(); |
| 117 | + gcInProgress = false; // GC is in progress until after release! |
| 118 | + stopTime = VM_Time.now(); |
| 119 | + if (verbose > 0) { |
| 120 | + VM.sysWrite(" Collection time: ", (stopTime - startTime)); |
| 121 | + VM.sysWriteln(" seconds"); |
| 122 | + } |
| 123 | + } |
| 124 | + barrier.rendezvous(); |
| 125 | + } |
| 126 | + |
| 127 | + // These abstract methods are called in the order singlePrepare, allPrepare, allRelease, |
| 128 | + // and singleRelease. They are all separated by a barrier. |
| 129 | + abstract protected void singlePrepare(); |
| 130 | + abstract protected void allPrepare(); |
| 131 | + abstract protected void allRelease(); |
| 132 | + abstract protected void singleRelease(); |
| 133 | + |
| 134 | + static SynchronizedCounter threadCounter = new SynchronizedCounter(); |
| 135 | + static double startTime; |
| 136 | + static double stopTime; |
| 137 | + |
| 138 | + private void resetComputeRoots() { |
| 139 | + threadCounter.reset(); |
| 140 | + } |
| 141 | + |
| 142 | + private void computeRoots() { |
| 143 | + |
| 144 | + AddressPairSet codeLocations = VM_Interface.MOVES_OBJECTS ? interiorLocations : null; |
| 145 | + |
| 146 | + ScanStatics.scanStatics(locations); |
| 147 | + |
| 148 | + while (true) { |
| 149 | + int threadIndex = threadCounter.increment(); |
| 150 | + if (threadIndex >= VM_Scheduler.threads.length) break; |
| 151 | + VM_Thread th = VM_Scheduler.threads[threadIndex]; |
| 152 | + if (th == null) continue; |
| 153 | + // VM.sysWrite("Proc ", VM_Processor.getCurrentProcessor().id); VM.sysWriteln(" scanning thread ", threadIndex); |
| 154 | + // See comment of ScanThread.scanThread |
| 155 | + // |
| 156 | + VM_Thread th2 = VM_Magic.addressAsThread(Plan.traceObject(VM_Magic.objectAsAddress(th))); |
| 157 | + Plan.traceObject(VM_Magic.objectAsAddress(th.stack)); |
| 158 | + if (th.jniEnv != null) { |
| 159 | + Plan.traceObject(VM_Magic.objectAsAddress(th.jniEnv)); |
| 160 | + Plan.traceObject(VM_Magic.objectAsAddress(th.jniEnv.JNIRefs)); |
| 161 | + } |
| 162 | + Plan.traceObject(VM_Magic.objectAsAddress(th.contextRegisters)); |
| 163 | + Plan.traceObject(VM_Magic.objectAsAddress(th.contextRegisters.gprs)); |
| 164 | + Plan.traceObject(VM_Magic.objectAsAddress(th.hardwareExceptionRegisters)); |
| 165 | + Plan.traceObject(VM_Magic.objectAsAddress(th.hardwareExceptionRegisters.gprs)); |
| 166 | + ScanThread.scanThread(th2, locations, codeLocations); |
| 167 | + } |
| 168 | + |
| 169 | + // VM.sysWriteln("locations size is ", locations.size()); |
| 170 | + // VM.sysWriteln("values size is ", values.size()); |
| 171 | + // VM.sysWriteln("interiorLocations size is ", interiorLocations.size()); |
| 172 | + } |
| 173 | + |
| 174 | + // Add a gray object |
| 175 | + // |
| 176 | + static public void enqueue(VM_Address obj) throws VM_PragmaInline { |
| 177 | + VM_Interface.getPlan().values.push(obj); |
| 178 | + } |
| 179 | + |
| 180 | + private void processAllWork() throws VM_PragmaNoInline { |
| 181 | + |
| 182 | + while (true) { |
| 183 | + while (!values.isEmpty()) { |
| 184 | + VM_Address v = values.pop(); |
| 185 | + ScanObject.scan(v); // NOT traceObject |
| 186 | + } |
| 187 | + while (!locations.isEmpty()) { |
| 188 | + VM_Address loc = locations.pop(); |
| 189 | + traceObjectLocation(loc); |
| 190 | + } |
| 191 | + while (!interiorLocations.isEmpty()) { |
| 192 | + VM_Address obj = interiorLocations.pop1(); |
| 193 | + VM_Address interiorLoc = interiorLocations.pop2(); |
| 194 | + VM_Address interior = VM_Magic.getMemoryAddress(interiorLoc); |
| 195 | + VM_Address newInterior = traceInteriorReference(obj, interior); |
| 196 | + VM_Magic.setMemoryAddress(interiorLoc, newInterior); |
| 197 | + } |
| 198 | + |
| 199 | + if (values.isEmpty() && locations.isEmpty() && interiorLocations.isEmpty()) |
| 200 | + break; |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + protected void collect() { |
| 206 | + computeRoots(); |
| 207 | + processAllWork(); |
| 208 | + } |
| 209 | + |
| 210 | + public static int getTotalBlocks() throws VM_PragmaUninterruptible { |
| 211 | + heapBlocks = Conversions.bytesToBlocks(Options.initialHeapSize); |
| 212 | + return heapBlocks; |
| 213 | + } |
| 214 | + |
| 215 | + private static int heapBlocks; |
| 216 | + |
| 217 | + /** |
| 218 | + * Perform a write barrier operation for the putField bytecode.<p> <b>By default do nothing, |
| 219 | + * override if appropriate.</b> |
| 220 | + * |
| 221 | + * @param srcObj The address of the object containing the pointer to |
| 222 | + * be written to |
| 223 | + * @param offset The offset from srcObj of the slot the pointer is to be written into |
| 224 | + * @param tgt The address to which the source will point |
| 225 | + */ |
| 226 | + public void putFieldWriteBarrier(VM_Address srcObj, int offset, VM_Address tgt){ |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Perform a write barrier operation for the putStatic bytecode.<p> <b>By default do nothing, |
| 231 | + * override if appropriate.</b> |
| 232 | + * |
| 233 | + * @param srcObj The address of the object containing the pointer to |
| 234 | + * be written to |
| 235 | + * @param offset The offset from static table (JTOC) of the slot the pointer is to be written into |
| 236 | + * @param tgt The address to which the source will point |
| 237 | + */ |
| 238 | + public void putStaticWriteBarrier(int staticOffset, VM_Address tgt){ |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Perform a read barrier operation of the getField bytecode.<p> <b>By default do nothing, |
| 243 | + * override if appropriate.</b> |
| 244 | + * |
| 245 | + * @param tgtObj The address of the object containing the pointer |
| 246 | + * about to be read |
| 247 | + * @param offset The offset from tgtObj of the field to be read from |
| 248 | + */ |
| 249 | + public void getFieldReadBarrier(VM_Address tgtObj, int offset) {} |
| 250 | + |
| 251 | + /** |
| 252 | + * Perform a read barrier operation for the getStatic bytecode.<p> <b>By default do nothing, |
| 253 | + * override if appropriate.</b> |
| 254 | + * |
| 255 | + * @param tgtObj The address of the object containing the pointer |
| 256 | + * about to be read |
| 257 | + * @param offset The offset from tgtObj of the field to be read from |
| 258 | + */ |
| 259 | + public void getStaticReadBarrier(int staticOffset) {} |
| 260 | + |
| 261 | + /** |
| 262 | + * Trace a reference during GC. This involves determining which |
| 263 | + * collection policy applies and calling the appropriate |
| 264 | + * <code>trace</code> method. |
| 265 | + * |
| 266 | + * @param obj The object reference to be traced. This is <i>NOT</i> an |
| 267 | + * interior pointer. |
| 268 | + * @return The possibly moved reference. |
| 269 | + */ |
| 270 | + // abstract static public VM_Address traceObject(VM_Address obj); |
| 271 | + |
| 272 | + /** |
| 273 | + * Answers true if the given object will not move during this GC |
| 274 | + * or else has already moved. |
| 275 | + * |
| 276 | + * @param obj The object reference whose movability status must be answered. |
| 277 | + * @return whether object has moved or will not move. |
| 278 | + */ |
| 279 | + // abstract static public boolean hasMoved(VM_Address obj); |
| 280 | + |
| 281 | + |
| 282 | + /** |
| 283 | + * Trace a reference during GC. This involves determining which |
| 284 | + * collection policy applies and calling the appropriate |
| 285 | + * <code>trace</code> method. |
| 286 | + * |
| 287 | + * @param objLoc The location containing the object reference to be traced. |
| 288 | + * The object reference is <i>NOT</i> an interior pointer. |
| 289 | + * @return void |
| 290 | + */ |
| 291 | + final static public void traceObjectLocation(VM_Address objLoc) throws VM_PragmaInline { |
| 292 | + VM_Address obj = VM_Magic.getMemoryAddress(objLoc); |
| 293 | + VM_Address newObj = Plan.traceObject(obj); |
| 294 | + VM_Magic.setMemoryAddress(objLoc, newObj); |
| 295 | + } |
| 296 | + |
| 297 | + |
| 298 | + /** |
| 299 | + * Trace a reference during GC. This involves determining which |
| 300 | + * collection policy applies and calling the appropriate |
| 301 | + * <code>trace</code> method. |
| 302 | + * |
| 303 | + * @param obj The object reference to be traced. This is <i>NOT</i> an |
| 304 | + * interior pointer. |
| 305 | + * @param interiorRef The interior reference inside obj that must be traced. |
| 306 | + * @return The possibly moved interior reference. |
| 307 | + */ |
| 308 | + final static public VM_Address traceInteriorReference(VM_Address obj, VM_Address interiorRef) { |
| 309 | + VM_Offset offset = interiorRef.diff(obj); |
| 310 | + VM_Address newObj = Plan.traceObject(obj); |
| 311 | + if (VM.VerifyAssertions) { |
| 312 | + if (offset.toInt() > (1<<24)) { // There is probably no object this large |
| 313 | + VM.sysWriteln("ERROR: Suspciously large delta of interior pointer from object base"); |
| 314 | + VM.sysWriteln(" object base = ", obj); |
| 315 | + VM.sysWriteln(" interior reference = ", interiorRef); |
| 316 | + VM.sysWriteln(" delta = ", offset.toInt()); |
| 317 | + VM._assert(false); |
| 318 | + } |
| 319 | + } |
| 320 | + return newObj.add(offset); |
| 321 | + } |
| 322 | + |
| 323 | +} |
0 commit comments