cloudsim源代码学习:CloudletSchedulerDynamicWorkload.java

/*
 * Title:        CloudSim Toolkit
 * Description:  CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (c) 2009-2010, The University of Melbourne, Australia
 */

package org.cloudbus.cloudsim;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CloudletSchedulerDynamicWorkload implements a policy of
 * scheduling performed by a virtual machine assuming
 * that there is just one cloudlet which is working as
 * an online service.
 *
 * @author		Anton Beloglazov
 * @since		CloudSim Toolkit 2.0
 */
 //该类实现了VM执行的调度策略。假设仅有一个cloudlet作为在线服务。
public class CloudletSchedulerDynamicWorkload extends CloudletSchedulerTimeShared {

	//mips
	/** The mips. */
	private double mips;
	//pe数量
	/** The pes number. */
	private int pesNumber;
	//总mips
	/** The total mips. */
	private double totalMips;
	//分配的mips
	/** The under allocated mips. */
	private Map<String, Double> underAllocatedMips;
	
	private double cachePreviousTime;

	private List<Double> cacheCurrentRequestedMips;

	/**
	 * Instantiates a new vM scheduler time shared.
	 *
	 * @param pesNumber the pes number
	 * @param mips the mips
	 */
	 //构造函数
	public CloudletSchedulerDynamicWorkload(double mips, int pesNumber) {
		super();
		setMips(mips);
		setPesNumber(pesNumber);
		setTotalMips(getPesNumber() * getMips());
		setUnderAllocatedMips(new HashMap<String, Double>());
		setCachePreviousTime(-1);
	}

	/**
	 * Updates the processing of cloudlets running under management of this scheduler.
	 *
	 * @param currentTime current simulation time
	 * @param mipsShare array with MIPS share of each Pe available to the scheduler
	 *
	 * @return time predicted completion time of the earliest finishing cloudlet, or 0
	 * if there is no next events
	 *
	 * @pre currentTime >= 0
	 * @post $none
	 */
	 //更新cloudlet处理
	@Override
	public double updateVmProcessing(double currentTime, List<Double> mipsShare) {
		setCurrentMipsShare(mipsShare);

		double timeSpan = currentTime - getPreviousTime();						//经过的时间
		double nextEvent = Double.MAX_VALUE;									//下一事件
		List<ResCloudlet> cloudletsToFinish = new ArrayList<ResCloudlet>();		//将完成的cloudlet列表

		for (ResCloudlet rcl : getCloudletExecList()) {
			rcl.updateCloudletFinishedSoFar((long) (timeSpan * getTotalCurrentAllocatedMipsForCloudlet(rcl, getPreviousTime())));		//span时间内完成的指令数

            if (rcl.getRemainingCloudletLength() == 0.0) { //finished: remove from the list		完成,从列表中删除
            	cloudletsToFinish.add(rcl);					//添加到完成列表
                continue;
            } else { //not finish: estimate the finish time			未完成,估算完成时间
            	double estimatedFinishTime = getEstimatedFinishTime(rcl, currentTime);	
				if (estimatedFinishTime - currentTime < 0.1) {
					estimatedFinishTime = currentTime + 0.1;
				}
            	if (estimatedFinishTime < nextEvent) {
            		nextEvent = estimatedFinishTime;			//更新下一事件时间
            	}
            }
		}

		for (ResCloudlet rgl : cloudletsToFinish) {				//移除完成的时间
			getCloudletExecList().remove(rgl);
			cloudletFinish(rgl);
		}

		setPreviousTime(currentTime);							//设置previous时间

		if (getCloudletExecList().isEmpty()) {					
			return 0;
		}

		return nextEvent;										//返回估算的下一事件时间
	}

	/**
	 * Receives an cloudlet to be executed in the VM managed by this scheduler.
	 *
	 * @param cl the cl
	 *
	 * @return predicted completion time
	 *
	 * @pre _gl != null
	 * @post $none
	 */
	 //收到cloudlet
	@Override
	public double cloudletSubmit(Cloudlet cl) {
		return cloudletSubmit(cl, 0);				//提交cloudlet
	}

	/**
	 * Receives an cloudlet to be executed in the VM managed by this scheduler.
	 *
	 * @param cl the cl
	 * @param fileTransferTime the file transfer time
	 *
	 * @return predicted completion time
	 *
	 * @pre _gl != null
	 * @post $none
	 */
	 //收到cloudlet
	@Override
	public double cloudletSubmit(Cloudlet cl, double fileTransferTime) {
		ResCloudlet rcl = new ResCloudlet(cl);
		rcl.setCloudletStatus(Cloudlet.INEXEC);				//设置cloudlet状态

		for(int i=0; i<cl.getPesNumber(); i++) {
			rcl.setMachineAndPeId(0, i);					//设置机器和PE的ID
		}

		getCloudletExecList().add(rcl);						//增加到执行cloudlet的列表
		return getEstimatedFinishTime(rcl, getPreviousTime());	
	}

	/**
	 * Processes a finished cloudlet.
	 *
	 * @param rcl finished cloudlet
	 *
	 * @pre rgl != $null
	 * @post $none
	 */
	 //处理某个完成的cloudlet
	@Override
	public void cloudletFinish(ResCloudlet rcl) {
		rcl.setCloudletStatus(Cloudlet.SUCCESS);			//设置cloudlet状态为SUCCESS
        rcl.finalizeCloudlet();
        getCloudletFinishedList().add(rcl);					//添加到完成列表
	}

	/**
	 * Get utilization created by all cloudlets.
	 *
	 * @param time the time
	 *
	 * @return total utilization
	 */
	 //获取所有cloudlet的利用率
	@Override
	public double getTotalUtilizationOfCpu(double time) {
		double totalUtilization = 0;
		for (ResCloudlet rcl : getCloudletExecList()) {
			totalUtilization += rcl.getCloudlet().getUtilizationOfCpu(time);
		}
		return totalUtilization;
	}

	/**
	 * Gets the current mips.
	 *
	 * @return the current mips
	 */
	 //返回当前mips
	@Override
	public List<Double> getCurrentRequestedMips() {
		if (getCachePreviousTime() == getPreviousTime()) {			//如果getCachePreviousTime() == getPreviousTime()
			return getCacheCurrentRequestedMips();					//返回getCacheCurrentRequestedMips()
		}
		List<Double> currentMips = new ArrayList<Double>();
		double totalMips = getTotalUtilizationOfCpu(getPreviousTime()) * getTotalMips();	//总MIPS = CPU利用率 * 总MIPS
		double mipsForPe = totalMips / getPesNumber();							//mips/pe

		

		for (int i = 0; i < getPesNumber(); i++) {
			currentMips.add(mipsForPe);							//为每个pe设置mips
		}

		setCachePreviousTime(getPreviousTime());
		setCacheCurrentRequestedMips(currentMips);

		return currentMips;
	}

	/**
	 * Gets the current mips.
	 *
	 * @param rcl the rcl
	 * @param time the time
	 *
	 * @return the current mips
	 */
	 //返回当前mips
	@Override
	public double getTotalCurrentRequestedMipsForCloudlet(ResCloudlet rcl, double time) {
		return rcl.getCloudlet().getUtilizationOfCpu(time) * getTotalMips();		//利用率* 总MIPS
	}

	/**
	 * Gets the total current mips for the clouddlet.
	 *
	 * @param rcl the rcl
	 * @param mipsShare the mips share
	 *
	 * @return the total current mips
	 */
	 //获取cloudlet当前可得到MPIS
	@Override
	public double getTotalCurrentAvailableMipsForCloudlet(ResCloudlet rcl, List<Double> mipsShare) {
		double totalCurrentMips = 0.0;
		if (mipsShare != null) {
			int neededPEs = rcl.getPesNumber();
			for (double mips : mipsShare) {					//获取所有PE的共享mpis
				totalCurrentMips += mips;
				neededPEs--;
				if (neededPEs <= 0) {
					break;
				}
			}
		}
		return totalCurrentMips;
	}

	/**
	 * Gets the current mips.
	 *
	 * @param rcl the rcl
	 * @param time the time
	 *
	 * @return the current mips
	 */
	 //获取cloudlet当前分配的所有MIPS
	@Override
	public double getTotalCurrentAllocatedMipsForCloudlet(ResCloudlet rcl, double time) {
		double totalCurrentRequestedMips = getTotalCurrentRequestedMipsForCloudlet(rcl, time);
		double totalCurrentAvailableMips = getTotalCurrentAvailableMipsForCloudlet(rcl, getCurrentMipsShare());
		if (totalCurrentRequestedMips > totalCurrentAvailableMips) {		//若果请求MIPS 大于 可用MIPS, 返回可用MPIS,否则返回请求MIPS
			return totalCurrentAvailableMips;
		}
		return totalCurrentRequestedMips;
	}

	/**
	 * Update under allocated mips for cloudlet.
	 *
	 * @param rcl the rgl
	 * @param mips the mips
	 */
	 //为cloudlet更新分配mips
	public void updateUnderAllocatedMipsForCloudlet(ResCloudlet rcl, double mips) {
		if (getUnderAllocatedMips().containsKey(rcl.getUid())) {
			mips += getUnderAllocatedMips().get(rcl.getUid());
		}
		getUnderAllocatedMips().put(rcl.getUid(), mips);
	}

	/**
	 * Get estimated cloudlet completion time.
	 *
	 * @param time the time
	 * @param rcl the rcl
	 *
	 * @return the estimated finish time
	 */
	 //获取估算完成时间
	public double getEstimatedFinishTime(ResCloudlet rcl, double time) {
    	return time + ((rcl.getRemainingCloudletLength()) / getTotalCurrentAllocatedMipsForCloudlet(rcl, time)); //time + (剩余mips/可用MIPS)
	}

	/**
	 * Gets the total current mips.
	 *
	 * @return the total current mips
	 */
	 //获取当前总MIPS
	public int getTotalCurrentMips() {
		int totalCurrentMips = 0;
		for (double mips : getCurrentMipsShare()) {
			totalCurrentMips += mips;
		}
		return totalCurrentMips;
	}

	/**
	 * Sets the total mips.
	 *
	 * @param mips the new total mips
	 */
	 //设置总MIPS
	public void setTotalMips(double mips) {
		this.totalMips = mips;
	}

	/**
	 * Gets the total mips.
	 *
	 * @return the total mips
	 */
	 //获取总MIPS
	public double getTotalMips() {
		return totalMips;
	}

	/**
	 * Sets the pes number.
	 *
	 * @param pesNumber the new pes number
	 */
	 //设置PE数
	public void setPesNumber(int pesNumber) {
		this.pesNumber = pesNumber;
	}

	/**
	 * Gets the pes number.
	 *
	 * @return the pes number
	 */
	 //获取PE数
	public int getPesNumber() {
		return pesNumber;
	}

	/**
	 * Sets the mips.
	 *
	 * @param mips the new mips
	 */
	 //设置mips
	public void setMips(double mips) {
		this.mips = mips;
	}

	/**
	 * Gets the mips.
	 *
	 * @return the mips
	 */
	 //获取mips
	public double getMips() {
		return mips;
	}

	/**
	 * Sets the under allocated mips.
	 *
	 * @param underAllocatedMips the under allocated mips
	 */
	 //设置under allocated mips
	public void setUnderAllocatedMips(Map<String, Double> underAllocatedMips) {
		this.underAllocatedMips = underAllocatedMips;
	}

	/**
	 * Gets the under allocated mips.
	 *
	 * @return the under allocated mips
	 */
	 //获取under allocated mips
	public Map<String, Double> getUnderAllocatedMips() {
		return underAllocatedMips;
	}
	//获取cache previous time
	protected double getCachePreviousTime() {
		return cachePreviousTime;
	}

	//设置cache previous time
	protected void setCachePreviousTime(double cachePreviousTime) {
		this.cachePreviousTime = cachePreviousTime;
	}

	//获取当前cache请求MIPS
	protected List<Double> getCacheCurrentRequestedMips() {
		return cacheCurrentRequestedMips;
	}
	//设置当前cache请求MIPS
	protected void setCacheCurrentRequestedMips(
			List<Double> cacheCurrentRequestedMips) {
		this.cacheCurrentRequestedMips = cacheCurrentRequestedMips;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值