/*
* This file is part of the Jikes RVM project (http://jikesrvm.org).
*
* This file is licensed to You under the Eclipse Public License (EPL);
* You may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.opensource.org/licenses/eclipse-1.0.php
*
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership.
*/
package org.mmtk.policy.immix;
import static org.mmtk.policy.immix.ImmixConstants.*;
import static org.mmtk.utility.Constants.*;
import org.mmtk.utility.Log;
import org.mmtk.utility.heap.FreeListPageResource;
import org.mmtk.utility.options.DefragFreeHeadroom;
import org.mmtk.utility.options.DefragFreeHeadroomFraction;
import org.mmtk.utility.options.DefragHeadroom;
import org.mmtk.utility.options.DefragHeadroomFraction;
import org.mmtk.utility.options.DefragLineReuseRatio;
import org.mmtk.utility.options.DefragSimpleSpillThreshold;
import org.mmtk.utility.options.DefragStress;
import org.mmtk.utility.options.Options;
import org.mmtk.utility.statistics.EventCounter;
import org.mmtk.utility.statistics.SizeCounter;
import org.mmtk.vm.VM;
import org.vmmagic.pragma.Interruptible;
import org.vmmagic.pragma.Uninterruptible;
@Uninterruptible
public class Defrag {
private boolean inDefragCollection = false;
private int debugBytesDefraged = 0;
private int availableCleanPagesForDefrag;
private boolean defragSpaceExhausted = true;
private int[][] spillMarkHistograms;
private final int[] spillAvailHistogram = new int[SPILL_HISTOGRAM_BUCKETS];
public static SizeCounter defragCleanBytesUsed = new SizeCounter("cleanUsed");
/* verbose stats (used only on stats runs since they induce overhead when gathered) */
public static SizeCounter defragBytesNotFreed = new SizeCounter("bytesNotFreed");
public static SizeCounter defragBytesFreed = new SizeCounter("bytesFreed");
public static SizeCounter defragCleanBytesAvailable = new SizeCounter("cleanAvail");
private final FreeListPageResource pr;
private boolean debugCollectionTypeDetermined = false;
static short defragSpillThreshold = 0;
static short defragReusableMarkStateThreshold = 0;
public static EventCounter defrags = new EventCounter("defrags");
static {
Options.defragLineReuseRatio = new DefragLineReuseRatio();
Options.defragHeadroom = new DefragHeadroom();
Options.defragHeadroomFraction = new DefragHeadroomFraction();
Options.defragFreeHeadroom = new DefragFreeHeadroom();
Options.defragFreeHeadroomFraction = new DefragFreeHeadroomFraction();
Options.defragSimpleSpillThreshold = new DefragSimpleSpillThreshold();
Options.defragStress = new DefragStress();
defragReusableMarkStateThreshold = (short) (Options.defragLineReuseRatio.getValue() * MAX_BLOCK_MARK_STATE);
}
Defrag(FreeListPageResource pr) {
this.pr = pr;
}
/**
* Prepares the histograms.<p>
*
* This needs to happen at runtime because the collector count is not known
* at build time.
*/
@Interruptible
void prepareHistograms() {
int collectorCount = VM.activePlan.collectorCount();
spillMarkHistograms = new int[collectorCount][SPILL_HISTOGRAM_BUCKETS];
}
boolean inDefrag() {
return inDefragCollection;
}
void prepare(ChunkList chunkMap, ImmixSpace space) {
availableCleanPagesForDefrag = VM.activePlan.global().getTotalPages() - VM.activePlan.global().getPagesReserved() + getDefragHeadroomPages();
if (availableCleanPagesForDefrag < 0) availableCleanPagesForDefrag = 0;
defragSpaceExhausted = false;
/* Free defrag pages (not budgeted, used for experimentation) */
if (Options.defragFreeHeadroom.getPages() > 0) {
availableCleanPagesForDefrag += Options.defragFreeHeadroom.getPages();
} else if (Options.defragFreeHeadroomFraction.getValue() > 0) {
availableCleanPagesForDefrag += (int) (pr.reservedPages() * Options.defragFreeHeadroomFraction.getValue());
}
if (inDefragCollection) {
if (Options.verbose.getValue() > 0) {
Log.write("[Defrag]");
}
chunkMap.consolidateMap();
establishDefragSpillThreshold(chunkMap, space);
defrags.inc();
defragCleanBytesAvailable.inc(availableCleanPagesForDefrag << LOG_BYTES_IN_PAGE);
}
availableCleanPagesForDefrag += VM.activePlan.global().getCollectionReserve();
}
void globalRelease() {
if (inDefragCollection && Options.verbose.getValue() > 2) {
Log.write("(Defrag summary: cu: ");
defragCleanBytesUsed.printCurrentVolume();
Log.write(" nf: ");
defragBytesNotFreed.printCurrentVolume();
Log.write(" fr: ");
defragBytesFreed.printCurrentVolume();
Log.write(" av: ");
defragCleanBytesAvailable.printCurrentVolume();
Log.write(")");
}
inDefragCollection = false;
debugCollectionTypeDetermined = false;
}
int getDefragHeadroomPages() {
if (Options.defragHeadroom.getPages() > 0) {
return Options.defragHeadroom.getPages();
} else if (Options.defragHeadroomFraction.getValue() > 0) {
return (int) (pr.reservedPages() * Options.defragHeadroomFraction.getValue());
}
return 0;
}
void decideWhetherToDefrag(boolean emergencyCollection, boolean collectWholeHeap, int collectionAttempt, boolean userTriggered, boolean exhaustedReusableSpace) {
inDefragCollection = (collectionAttempt > 1) ||
emergencyCollection ||
collectWholeHeap && (Options.defragStress.getValue() || (userTriggered && Options.fullHeapSystemGC.getValue()));
if (inDefragCollection) {
debugBytesDefraged = 0;
}
debugCollectionTypeDetermined = true;
}
boolean determined(boolean inDefrag) {
return debugCollectionTypeDetermined && !(inDefrag ^ inDefragCollection);
}
void getBlock() {
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(!inDefragCollection || !defragSpaceExhausted);
if (availableCleanPagesForDefrag <= 0)
defragSpaceExhausted = true;
availableCleanPagesForDefrag -= PAGES_IN_BLOCK;
debugBytesDefraged += BYTES_IN_BLOCK;
Defrag.defragCleanBytesUsed.inc(BYTES_IN_BLOCK);
}
private void establishDefragSpillThreshold(ChunkList chunkMap, ImmixSpace space) {
int cleanLines = space.getAvailableLines(spillAvailHistogram);
int availableLines = cleanLines + availableCleanPagesForDefrag << (LOG_BYTES_IN_PAGE - LOG_BYTES_IN_LINE);
int requiredLines = 0;
short threshold = MAX_CONSV_SPILL_COUNT;
int limit = (int) (availableLines / Options.defragLineReuseRatio.getValue());
if (VM.VERIFY_ASSERTIONS && Options.verbose.getValue() > 2) {
Log.write("[threshold: ");
Log.write("cl: ", cleanLines);
Log.write(" al: ", availableLines);
Log.write(" lm: ", limit);
}
int collectors = VM.activePlan.collectorCount();
for (short index = MAX_CONSV_SPILL_COUNT; index >= TMP_MIN_SPILL_THRESHOLD && limit > requiredLines; index--) {
threshold = index;
int thisBucketMark = 0;
int thisBucketAvail = 0;
for (int c = 0; c < collectors; c++) thisBucketMark += spillMarkHistograms[c][threshold];
thisBucketAvail = spillAvailHistogram[threshold];
limit -= thisBucketAvail;
requiredLines += thisBucketMark;
if (VM.VERIFY_ASSERTIONS && Options.verbose.getValue() > 2) {
Log.write(" (", index);
Log.write(" ", limit);
Log.write(",", requiredLines);
Log.write(")");
}
}
if (VM.VERIFY_ASSERTIONS && Options.verbose.getValue() > 2) {
Log.write(" threshold: ", threshold);
Log.write("]");
}
defragSpillThreshold = threshold;
}
boolean spaceExhausted() {
return defragSpaceExhausted;
}
int[] getAndZeroSpillMarkHistogram(int ordinal) {
int[] rtn = spillMarkHistograms[ordinal];
for (int i = 0; i < SPILL_HISTOGRAM_BUCKETS; i++)
rtn[i] = 0;
return rtn;
}
}