CloudSim仿真流程研究(二)[通俗易懂]

CloudSim仿真流程研究(二)[通俗易懂]org.cloudbus.cloudsim.examples.power.random里的例子IqrMc:publicclassIqrMc{ /** *Themainmethod. * *@paramargsthearguments *@throwsIOExceptionSignalsthatanI/Oexceptionhasocc…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

org.cloudbus.cloudsim.examples.power.random里的例子IqrMc

public class IqrMc {

	/**
	 * The main method.
	 * 
	 * @param args the arguments
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static void main(String[] args) throws IOException {
		boolean enableOutput = true;
		boolean outputToFile = true;
		String inputFolder = "";
		String outputFolder = "C:\\Users\\yangdi\\Desktop\\cloudsim_data";
		String workload = "random"; // Random workload
		String vmAllocationPolicy = "iqr"; // Inter Quartile Range (IQR) VM allocation policy
		String vmSelectionPolicy = "mc"; // Maximum Correlation (MC) VM selection policy
		String parameter = "1.5"; // the safety parameter of the IQR policy
		
		new RandomRunner(
				enableOutput,
				outputToFile,
				inputFolder,
				outputFolder,
				workload,
				vmAllocationPolicy,
				vmSelectionPolicy,
				parameter);
	}

}

运行类在类RandomRunner中,该类继承于RunnerAbstract类,并且直重写了一个方法:init()

protected void init(String inputFolder) {
		try {
			CloudSim.init(1, Calendar.getInstance(), false);

			broker = Helper.createBroker();
			int brokerId = broker.getId();

			cloudletList = RandomHelper.createCloudletList(brokerId, RandomConstants.NUMBER_OF_VMS);
			vmList = Helper.createVmList(brokerId, cloudletList.size());
			hostList = Helper.createHostList(RandomConstants.NUMBER_OF_HOSTS);
		} catch (Exception e) {
			e.printStackTrace();
			Log.printLine("The simulation has been terminated due to an unexpected error");
			System.exit(0);
		}
	}

该方法中Cloudlet是随即创建的,使用的是RandomHelper里的createCloudletList()方法,在里面创建Cloudlet:

cloudlet = new Cloudlet(
    i,
    Constants.CLOUDLET_LENGTH,
    Constants.CLOUDLET_PES,
    fileSize,
    outputSize,
    new UtilizationModelStochastic(),
    utilizationModelNull,  
    utilizationModelNull
    // new UtilizationModelStochastic(),
    // new UtilizationModelStochastic()
);

本来自带的Cloudlet中传入的CPU使用率模型,RAM使用率模型,和BW使用率模型,这个暂时不用去管,因为其本身自己就有简单的随机模型。之后的仿真流程跟前一篇一样。之后研究在Planetlab的工作流或者随机cloudlet的工作流之下其CPU利用率,RAM利用率,Bw利用率和Storage利用率。

利用率这一段日志的输出在powerDatacenter类中(节取):

protected double updateCloudetProcessingWithoutSchedulingFutureEventsForce() {
		double currentTime = CloudSim.clock();
		double minTime = Double.MAX_VALUE;
		double timeDiff = currentTime - getLastProcessTime();
		double timeFrameDatacenterEnergy = 0.0;

		Log.printLine("\n\n--------------------------------------------------------------\n\n");
		Log.formatLine("New resource usage for the time frame starting at %.2f:", currentTime);

		for (PowerHost host : this.<PowerHost> getHostList()) {
			Log.printLine();

			double time = host.updateVmsProcessing(currentTime); // inform VMs to update processing
			if (time < minTime) {
				minTime = time;
			}

			Log.formatLine(
					"%.2f: [Host #%d] utilization is %.2f%%",
					currentTime,
					host.getId(),
					host.getUtilizationOfCpu() * 100);

其本身自带的只有CPU的利用率,所以需要去添加其他的利用率输出代码:

Log.formatLine(
    "%.2f: [Host #%d] utilization of RAM is %.2f%%",
    currentTime,
    host.getId(),
    host.getUtilizationOfRam() * 100);
			
Log.formatLine(
    "%.2f: [Host #%d] utilization of bw is %.2f%%",
    currentTime,
    host.getId(),
    host.getUtilizationOfBw() * 100);
			
Log.formatLine(
    "%.2f: [Host #%d] utilization of storge is %.2f%%",
    currentTime,
    host.getId(),
    host.getUtilizationOfStorage() * 100);

这之中的getUtilizationOfRam()和getUtilizationOfBw()方法在类HostDynamicWorkload中。但其本身的代码有点错误,名字是获取Ram的使用率,但是代码却是:

public double getUtilizationOfRam() {
		return getRamProvisioner().getUsedRam();  //这里有点错误,这里返回的是使用的Ram,而不是
	}

之后我稍微改写了一下,改成了利用率:

public double getUtilizationOfRam() {
		// return getRamProvisioner().getUsedRam();  //这里有点错误,这里返回的是使用的Ram,而不是使用率
		double result = (double)getRamProvisioner().getUsedRam() / (double)getRamProvisioner().getRam();
		return result;
	}

Bw的利用率同理,但是这个类里没有storage的利用率,需要自己去写,所以我在这个类的父类Host里去写,因为HostDynamicWorkload类里面没有Host所带的vmlist,vmlist在Host类中是private修饰的成员变量,无法被子类调用,我也不敢把它改成publiic什么的,所以我把storage的利用率写在Host类中:

/**
	 * Gets the utilization of Storage(in absolute values).
	 * @return
	 */
	public double getUtilizationOfStorage() {
		double result = (double)getUsedStorage() / (double)getStorage();
		return result;
	}

	/**
	 * Gets the host Used stroge.
	 */
	public long getUsedStorage() {
		long usedStorage = 0;
		for(Vm vm : vmList) {
			usedStorage += vm.getSize();
		}
		return usedStorage;
	}

之后运行程序,但是发现只有在第一个时钟时间的时候正常显示了利用率,在之后的所有时钟时间内,Ram利用率跟Bw利用率的使用率都是0.0%,这个至今没搞清楚什么原因。之后我将HostDynamicWorkload中的Ram利用率跟Bw利用率注释掉,在Host类中重新写了一下代码,不从ramProvisioner中获取主机的已经使用的usedRam,而是通过Host类中的vmlist成员变量来获取已经被使用的usedRam,Bw同理:

/**
	 * Gets the utilization of ram (in absolute values).
	 * 
	 * @return the utilization of ram
	 */
	public double getUtilizationOfRam() {
		double result = (double)getUsedRam() / (double)getRam();
		return result;
	}
	
	/**
	 * Gets the host Used Bw.
	 */
	public long getUsedRam() {
		long usedRam = 0;
		for(Vm vm : vmList) {
			usedRam += vm.getRam();
		}
		return usedRam;
	}
/**
	 * Gets the utilization of bw (in absolute values).
	 * 
	 * @return the utilization of bw
	 */
	public double getUtilizationOfBw() {
		double result = (double)getUsedBw() / (double)getBw();
		return result;
	}
	
	/**
	 * Gets the host Used Bw.
	 */
	public long getUsedBw() {
		long usedBw = 0;
		for(Vm vm : vmList) {
			usedBw += vm.getBw();
		}
		return usedBw;
	}

之后运行程序,就能在每个时钟时间正确的打印出结果:

CloudSim仿真流程研究(二)[通俗易懂]

不同时钟时刻的例子:

CloudSim仿真流程研究(二)[通俗易懂]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182478.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号