hotmethodmanager.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 131 行

JAVA
131
字号
/*
 * $Id: HotMethodManager.java,v 1.4 2004/02/02 18:42:26 epr Exp $
 */
package org.jnode.vm.compiler;

import java.io.PrintStream;

import org.jnode.system.BootLog;
import org.jnode.vm.ObjectVisitor;
import org.jnode.vm.Vm;
import org.jnode.vm.VmArchitecture;
import org.jnode.vm.classmgr.VmMethod;
import org.jnode.vm.classmgr.VmStatics;
import org.jnode.vm.memmgr.VmHeapManager;

/**
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class HotMethodManager extends Thread {

	/** Statics table */
	private final VmStatics statics;
	/** Number of compiler levels */
	private final int levels;
	private final VmMethod[] methods;
	private final int[] newInvocations;
	private int recompileCount = 0;
	private int maxInvocations;

	private static final int MIN_INVOCATIONS = 5;

	/**
	 * Initialize this instance
	 */
	public HotMethodManager(VmArchitecture arch, VmStatics statics) {
		setName("HotMethodManager");
		//setPriority(NORM_PRIORITY - 1);
		this.statics = statics;
		this.levels = arch.getCompilers().length;
		this.methods = new VmMethod[levels];
		this.newInvocations = new int[levels];
	}

	public void run() {
		final HotMethodFinder hotMethodFinder = new HotMethodFinder();
		final VmHeapManager heapMgr = Vm.getVm().getHeapManager();

		// Set all last invocation counts
		statics.walkMethods(new InitVisitor());

		int lastRecompileCount = 0;
		while (true) {
			try {
				if (heapMgr.isLowOnMemory()) {
					// Do not recompile when low on memory
					sleep(500);
				} else {
					if (lastRecompileCount == 0) {
						// Now wait for a while
						sleep(500);
					}

					// Initialize the local tables
					for (int i = 0; i < levels; i++) {
						newInvocations[i] = MIN_INVOCATIONS;
						methods[i] = null;
					}
					// Find "hot" methods
					statics.walkMethods(hotMethodFinder);

					// Recompile "hot" methods
					lastRecompileCount = 0;
					for (int i = 0; i < levels - 1; i++) {
						final VmMethod method = methods[i];
						if (method != null) {
							//BootLog.debug("recompile " + method);
							lastRecompileCount++;
							recompileCount++;
							method.recompile();
						}
					}
				}
			} catch (Exception ex) {
				BootLog.error("Method compile error", ex);
			}
		}
	}

	public void dumpStatistics(PrintStream out) {
		out.println("#max invocations " + maxInvocations);
		out.println("#recompiles      " + recompileCount);
	}

	protected void processMethod(VmMethod method) {
		final int level = Math.max(0, method.getNativeCodeOptLevel());
		if (level < levels - 1) {
			final int totInvCount = method.getInvocationCount();
			final int newInvCount = totInvCount - method.getLastInvocationCount();
			if (newInvCount > newInvocations[level]) {
				newInvocations[level] = newInvCount;
				methods[level] = method;
			}
			maxInvocations = Math.max(maxInvocations, newInvCount);
			method.setLastInvocationCount();
		}
	}

	class InitVisitor extends ObjectVisitor {

		/**
		 * @see org.jnode.vm.ObjectVisitor#visit(java.lang.Object)
		 */
		public boolean visit(Object object) {
			final VmMethod method = (VmMethod) object;
			method.setLastInvocationCount();
			return true;
		}
	}

	class HotMethodFinder extends ObjectVisitor {

		/**
		 * @see org.jnode.vm.ObjectVisitor#visit(java.lang.Object)
		 */
		public boolean visit(Object object) {
			processMethod((VmMethod) object);
			return true;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?