blob: b93b79841e08b28bab060c311ff39b419c5d68ae [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2010, Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.natives;
import org.eclipse.viatra2.core.IModelSpace;
import org.eclipse.viatra2.errors.VPMRuntimeException;
import org.eclipse.viatra2.natives.NativeFunctionParameter.ParameterType;
/**
* Native function to provide a way for estimating the memory footprint of Viatra2.
* The actual metric used is the heap size.
* Several garbage collection runs (the amount is configurable) are initiated before measurement, which may have a severe runtime impact.
*
* @author Istvan Rath
*
*/
@VIATRANativeFunction(name = "measureMemoryFootprint", returns = ParameterType.INTEGER, remark = "Returns the heaps size measured after Garbage Collection. "
+ "Caution: Garbage Collector runs are initiated before measurement, which may have a severe runtime impact.",
params = @NativeFunctionParameter(name = "gcRuns",
description = "The number of GC runs to be initiated before measurement.",
type = { NativeFunctionParameter.ParameterType.INTEGER }))
public class MeasureMemoryFootprintFunction implements ASMNativeFunction {
public Object evaluate(IModelSpace msp, Object[] params)
throws VPMRuntimeException {
if (params.length != 1) throw new VPMRuntimeException("Illegal parameter number for measureMemoryFootprint(): a single parameter is required");
if (! (params[0] instanceof Integer)) throw new VPMRuntimeException("Illegal argument type for measureMemoryFootprint(): Integer is required");
Integer gcRuns = (Integer) params[0];
for (int i = 0; i<gcRuns; ++i) Runtime.getRuntime().gc();
return (int)((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024));
}
public String getDescription() {
return "Returns the heaps size measured after Garbage Collection. "
+ "Caution: Garbage Collector runs are initiated before measurement, which may have a severe runtime impact.";
}
public String getID() {
return this.getClass().getCanonicalName();
}
public String getName() {
return "measureMemoryFootprint";
}
}