package s3.services.memory.mostlyCopying;

import ovm.core.services.memory.VM_Address;
import ovm.core.services.io.BasicIO;

/**
 * A statistic-gathering mixin for the mostly copying collector.
 *
 * Collect a histogram of allocated block sizes on a log_2 scale, and
 * report results after every GC.
 */
public class SizeHistManager extends Manager {
    int[] histogram = new int[32];

    public SizeHistManager(String heapSize, String blockSize,
			   String dumpOom) {
	super(heapSize, blockSize, dumpOom);
    }

    VM_Address getBlock(int size) {
	int i = 31;
	while ((size & (1 << i)) == 0)
	    i--;
	histogram[i]++;
	return super.getBlock(size);
    }

    public void doGC() {
	super.doGC();
	BasicIO.err.println("[Log Block Size|# Allocated");
	for (int i = 0; i < 32; i++) {
	    BasicIO.err.println((i < 10 ? "              " : "             ") +
				i + ": " + histogram[i]);
	    histogram[i] = 0;
	}
	BasicIO.err.println("                           ]");
    }
}
