cloudsim源代码学习:CloudletSchedulerSpaceShared.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.List;

import org.cloudbus.cloudsim.core.CloudSim;

/**
 * CloudletSchedulerSpaceShared implements a policy of
 * scheduling performed by a virtual machine. It consider
 * that there will be only one cloudlet per VM. Other cloudlets will be in a
 * waiting list. We consider that file transfer from cloudlets waiting happens
 * before cloudlet execution. I.e., even though cloudlets must wait for CPU,
 * data transfer happens as soon as cloudlets are submitted.
 *
 * @author		Rodrigo N. Calheiros
 * @author		Anton Beloglazov
 * @since		CloudSim Toolkit 1.0
 */
public class CloudletSchedulerSpaceShared extends CloudletScheduler {

	//等待cloudlet列表
	/** The cloudlet waiting list. */
	private List<? extends ResCloudlet> cloudletWaitingList;

	//执行中的cloudlet列表
	/** The cloudlet exec list. */
	private List<? extends ResCloudlet> cloudletExecList;

	//暂停的cloudlet列表
	/** The cloudlet paused list. */
	private List<? extends ResCloudlet> cloudletPausedList;

	//完成的cloudlet列表
	/** The cloudlet finished list. */
	private List<? extends ResCloudlet> cloudletFinishedList;

	//当前cpu
	/** The current CPUs. */
	protected int currentCpus;

	//使用的PE
	/** The used PEs. */
	protected int usedPes;

	/**
	 * Creates a new CloudletSchedulerSpaceShared object. This method must be invoked
	 * before starting the actual simulation.
	 *
	 * @pre $none
	 * @post $none
	 */
	 //构造函数,在模拟器starting前创建该对象
	public CloudletSchedulerSpaceShared() {
		super();
		this.cloudletWaitingList = new ArrayList<ResCloudlet>();
		this.cloudletExecList = new ArrayList<ResCloudlet>();
		this.cloudletPausedList = new ArrayList<ResCloudlet>();
		this.cloudletFinishedList = new ArrayList<ResCloudlet>();
		this.usedPes = 0;
		this.currentCpus = 0;
	}

	/**
	 * Updates the processing of cloudlets running under management of this scheduler.
	 *
	 * @param currentTime current simulation time
	 * @param mipsShare array with MIPS share of each processor 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 timeSpam = currentTime - getPreviousTime(); // time since last update     自上次更新后的时间
		double capacity = 0.0;
		int cpus = 0;

		for (Double mips : mipsShare) { // count the CPUs available to the VMM  统计可用CPU数, 和MIPS(capacity)
			capacity += mips;
			if (mips > 0) {
				cpus++;
			}
		}
		currentCpus = cpus;
		capacity /= cpus; // average capacity of each cpu         每个cpu的平均能力

		for (ResCloudlet rcl : getCloudletExecList()) { // each machine in the exec list has the same amount of cpu 、、
		//execl列表中的每个机器又同样的cpu能力
			rcl.updateCloudletFinishedSoFar((long) (capacity * timeSpam * rcl.getPesNumber())); //cpu能力 * 时间 * cpu数 = span时间内完成任务长度
		}

		if (getCloudletExecList().size() == 0 && getCloudletWaitingList().size() == 0) { // no more cloudlets in this scheduler    没有cloudlet
			setPreviousTime(currentTime);
			return 0.0;
		}

		//update each cloudlet
		int finished = 0;
		int cont = 0;
		List<ResCloudlet> toRemove = new ArrayList<ResCloudlet>();
		for (ResCloudlet rcl : getCloudletExecList()) {
			if (rcl.getRemainingCloudletLength() == 0.0) {// finished anyway, rounding issue...    cloudlet处理完成
				toRemove.add(rcl);							//增加到toRemove队列
				cloudletFinish(rcl);						//完成cloudlet
				finished++;
			}
			cont++;
		}
		getCloudletExecList().removeAll(toRemove);		//移除所有的完成cloudlet

        //for each finished cloudlet, add a new one from the waiting list 对每个移除的cloudlet从waiting队列中增加一个新的
		if (!getCloudletWaitingList().isEmpty()) {
			for (int i = 0; i < finished; i++) {
				toRemove.clear();
				for (ResCloudlet rcl : getCloudletWaitingList()) {	//对每个等待cloudlet,
					if ((currentCpus - usedPes) >= rcl.getPesNumber()) {  //若果请求PES数得到满足
						rcl.setCloudletStatus(Cloudlet.INEXEC);					//设置该cloudlet为执行
						for (int k = 0; k < rcl.getPesNumber(); k++) {			//对该cloudlet的每个PE
							rcl.setMachineAndPeId(0, i);						
						}
						getCloudletExecList().add(rcl);							//增加到执行列表
						usedPes += rcl.getPesNumber();							
						toRemove.add(rcl);										//增加到toRemove列表
						break;
					}
				}
				getCloudletWaitingList().removeAll(toRemove);		//从wating列表移除
			}// for(cont)
        }

        //estimate finish time of cloudlets in the execution queue	估算执行队列中的cloudlet的最早完成时间
		double nextEvent = Double.MAX_VALUE;
		for (ResCloudlet rcl : getCloudletExecList()) {
			double remainingLength = rcl.getRemainingCloudletLength();
			double estimatedFinishTime = currentTime + (remainingLength / (capacity * rcl.getPesNumber()));
			if (estimatedFinishTime - currentTime < 0.1) {	//精度为0.1
				estimatedFinishTime = currentTime + 0.1;
			}
			if (estimatedFinishTime < nextEvent) {
				nextEvent = estimatedFinishTime;
			}
		}
		setPreviousTime(currentTime);
		return nextEvent;
	}

	/**
	 * Cancels execution of a cloudlet.
	 *
	 * @param cloudletId ID of the cloudlet being cancealed
	 *
	 * @return the canceled cloudlet, $null if not found
	 *
	 * @pre $none
	 * @post $none
	 */
	 //取消cloudlet的执行
	@Override
	public Cloudlet cloudletCancel(int cloudletId) {
		//First, looks in the finished queue  首先,查询完成队列
		for (ResCloudlet rcl : getCloudletFinishedList()) {
			if (rcl.getCloudletId() == cloudletId) {
				getCloudletFinishedList().remove(rcl);
				return rcl.getCloudlet();
			}
		}

		//Then searches in the exec list 然后查询执行队列
		for (ResCloudlet rcl : getCloudletExecList()) {
			if (rcl.getCloudletId() == cloudletId) {
				getCloudletExecList().remove(rcl);
				if (rcl.getRemainingCloudletLength() == 0.0) {	//如果执行完成,增加到完成队列
					cloudletFinish(rcl);
				} else {										//否则,设置状态为CANCELED
					rcl.setCloudletStatus(Cloudlet.CANCELED);
				}
				return rcl.getCloudlet();
			}
		}

		//Now, looks in the paused queue						//查询暂停队列
		for (ResCloudlet rcl : getCloudletPausedList()) {
			if (rcl.getCloudletId() == cloudletId) {
				getCloudletPausedList().remove(rcl);
				return rcl.getCloudlet();
			}
		}

		//Finally, looks in the waiting list					//查询等待队列
		for (ResCloudlet rcl : getCloudletWaitingList()) {
			if (rcl.getCloudletId() == cloudletId) {
				rcl.setCloudletStatus(Cloudlet.CANCELED);		//设置状态
				getCloudletWaitingList().remove(rcl);
				return rcl.getCloudlet();
			}
		}

		return null;

	}

	/**
	 * Pauses execution of a cloudlet.
	 *
	 * @param cloudletId ID of the cloudlet being paused
	 *
	 * @return $true if cloudlet paused, $false otherwise
	 *
	 * @pre $none
	 * @post $none
	 */
	 //暂停cloudlet
	@Override
	public boolean cloudletPause(int cloudletId) {
		boolean found = false;
		int position = 0;

		//first, looks for the cloudlet in the exec list 首先,查询执行队列
		for (ResCloudlet rcl : getCloudletExecList()) {
			if (rcl.getCloudletId() == cloudletId) {
				found = true;
				break;
			}
			position++;
		}

		if (found){
			//moves to the paused list	移到暂停队列
			ResCloudlet rgl = getCloudletExecList().remove(position);
			if (rgl.getRemainingCloudletLength() == 0.0) {	//完成,移入完成队列
				cloudletFinish(rgl);
			} else {										//否则,移入暂停队列
				rgl.setCloudletStatus(Cloudlet.PAUSED);		
				getCloudletPausedList().add(rgl);
			}
			return true;

		}

		//now, look for the cloudlet in the waiting list	//等待队列中查找
		position = 0;
		found = false;
		for (ResCloudlet rcl : getCloudletWaitingList()) {
			if (rcl.getCloudletId() == cloudletId) {
				found = true;
				break;
			}
			position++;
		}

		if (found) {
			// moves to the paused list
			ResCloudlet rgl = getCloudletWaitingList().remove(position);
			if (rgl.getRemainingCloudletLength() == 0.0) {
				cloudletFinish(rgl);
			} else {
				rgl.setCloudletStatus(Cloudlet.PAUSED);
				getCloudletPausedList().add(rgl);
			}
			return true;

		}

		return false;
	}

	/**
	 * Processes a finished cloudlet.
	 *
	 * @param rcl finished cloudlet
	 *
	 * @pre rgl != $null
	 * @post $none
	 */
	 //处理完成cloudlet
	@Override
	public void cloudletFinish(ResCloudlet rcl) {
		rcl.setCloudletStatus(Cloudlet.SUCCESS);
		rcl.finalizeCloudlet();
		getCloudletFinishedList().add(rcl);
		usedPes -= rcl.getPesNumber();
	}

	/**
	 * Resumes execution of a paused cloudlet.
	 *
	 * @param cloudletId ID of the cloudlet being resumed
	 *
	 * @return $true if the cloudlet was resumed, $false otherwise
	 *
	 * @pre $none
	 * @post $none
	 */
	 //恢复暂停的cloudlet
	@Override
	public double cloudletResume(int cloudletId) {
		boolean found=false;
		int position=0;

		//look for the cloudlet in the paused list 在暂停列表中查询
		for (ResCloudlet rcl : getCloudletPausedList()) {
			if (rcl.getCloudletId() == cloudletId) {
				found = true;
				break;
			}
			position++;
		}

		if (found){
			ResCloudlet rcl = getCloudletPausedList().remove(position);  //移除pasused队列

			if ((currentCpus - usedPes) >= rcl.getPesNumber()) {// it can go to the exec list  如果可以进入执行队列
				rcl.setCloudletStatus(Cloudlet.INEXEC);
				for (int i = 0; i < rcl.getPesNumber(); i++) {
					rcl.setMachineAndPeId(0, i);
				}

				long size = rcl.getRemainingCloudletLength();
				size *= rcl.getPesNumber();
				rcl.getCloudlet().setCloudletLength(size);

				getCloudletExecList().add(rcl);
				usedPes += rcl.getPesNumber();

				// calculate the expected time for cloudlet completion  计算完成时间
				double capacity = 0.0;
				int cpus = 0;
				for (Double mips : getCurrentMipsShare()) {
					capacity += mips;
					if (mips > 0) {
						cpus++;
					}
				}
				currentCpus = cpus;
				capacity /= cpus;

				long remainingLength = rcl.getRemainingCloudletLength();
				double estimatedFinishTime = CloudSim.clock() + (remainingLength / (capacity * rcl.getPesNumber()));

				return estimatedFinishTime;
			} else {// no enough free PEs: go to the waiting queue	没有足够资源, 加入等待队列
				rcl.setCloudletStatus(Cloudlet.QUEUED);

				long size = rcl.getRemainingCloudletLength();
				size *= rcl.getPesNumber();
				rcl.getCloudlet().setCloudletLength(size);

				getCloudletWaitingList().add(rcl);
				return 0.0;
			}

		}

		//not found in the paused list: either it is in in the queue, executing or not exist
		return 0.0;

	}

	/**
	 * Receives an cloudlet to be executed in the VM managed by this scheduler.
	 *
	 * @param cloudlet the submited cloudlet
	 * @param fileTransferTime time required to move the required files from the SAN to the VM
	 *
	 * @return expected finish time of this cloudlet, or 0 if it is in the waiting queue
	 *
	 * @pre gl != null
	 * @post $none
	 */
	 //收到cloudlet
	@Override
	public double cloudletSubmit(Cloudlet cloudlet, double fileTransferTime) {
		if ((currentCpus - usedPes) >= cloudlet.getPesNumber()) {// it can go to the exec list 资源足够,可以加入执行队列
			ResCloudlet rcl = new ResCloudlet(cloudlet);
			rcl.setCloudletStatus(Cloudlet.INEXEC);
			for (int i = 0; i < cloudlet.getPesNumber(); i++) {
				rcl.setMachineAndPeId(0, i);
			}

			getCloudletExecList().add(rcl);
			usedPes += cloudlet.getPesNumber();
		} else {// no enough free PEs: go to the waiting queue	没有足够资源,加入等待队列
			ResCloudlet rcl = new ResCloudlet(cloudlet);
			rcl.setCloudletStatus(Cloudlet.QUEUED);
			getCloudletWaitingList().add(rcl);
			return 0.0;
		}

		// calculate the expected time for cloudlet completion 计算预期完成时间
		double capacity = 0.0;
		int cpus = 0;
		for (Double mips : getCurrentMipsShare()) {
			capacity += mips;
			if (mips > 0) {
				cpus++;
			}
		}

		currentCpus = cpus;
		capacity /= cpus;

		// use the current capacity to estimate the extra amount of
		// time to file transferring. It must be added to the cloudlet length
		double extraSize = capacity * fileTransferTime;
		long length = cloudlet.getCloudletLength();
		length += extraSize;
		cloudlet.setCloudletLength(length);
		return cloudlet.getCloudletLength() / capacity;
	}

	/* (non-Javadoc)
	 * @see cloudsim.CloudletScheduler#cloudletSubmit(cloudsim.Cloudlet)
	 */
	@Override
	public double cloudletSubmit(Cloudlet cloudlet) {
		cloudletSubmit(cloudlet, 0);
		return 0;
	}

	/**
	 * Gets the status of a cloudlet.
	 *
	 * @param cloudletId ID of the cloudlet
	 *
	 * @return status of the cloudlet, -1 if cloudlet not found
	 *
	 * @pre $none
	 * @post $none
	 */
	 //获取cloudlet状态
	@Override
	public int getCloudletStatus(int cloudletId) {
		for (ResCloudlet rcl : getCloudletExecList()) {
			if (rcl.getCloudletId() == cloudletId) {
				return rcl.getCloudletStatus();
			}
		}

		for (ResCloudlet rcl : getCloudletPausedList()) {
			if (rcl.getCloudletId() == cloudletId) {
				return rcl.getCloudletStatus();
			}
		}

		for (ResCloudlet rcl : getCloudletWaitingList()) {
			if (rcl.getCloudletId() == cloudletId) {
				return rcl.getCloudletStatus();
			}
		}

		return -1;
	}

	/**
	 * 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 gl : getCloudletExecList()) {
			totalUtilization += gl.getCloudlet().getUtilizationOfCpu(time);
		}
		return totalUtilization;
	}

	/**
	 * Informs about completion of some cloudlet in the VM managed
	 * by this scheduler.
	 *
	 * @return $true if there is at least one finished cloudlet; $false otherwise
	 *
	 * @pre $none
	 * @post $none
	 */
	 //判读完成队列是否为空
	@Override
	public boolean isFinishedCloudlets() {
		return getCloudletFinishedList().size() > 0;
	}

	/**,,
	 * Returns the next cloudlet in the finished list, $null if this list is empty.
	 *
	 * @return a finished cloudlet
	 *
	 * @pre $none
	 * @post $none
	 */
	 //返回完成队列中下一个cloudlet, 移除队首元素并返回
	@Override
	public Cloudlet getNextFinishedCloudlet() {
		if (getCloudletFinishedList().size() > 0) {
			return getCloudletFinishedList().remove(0).getCloudlet();
		}
		return null;
	}

	/**
	 * Returns the number of cloudlets runnning in the virtual machine.
	 *
	 * @return number of cloudlets runnning
	 *
	 * @pre $none
	 * @post $none
	 */
	 //返回VM中运行的cloudlet数
	@Override
	public int runningCloudlets() {
		return getCloudletExecList().size();
	}

	/**
	 * Returns one cloudlet to migrate to another vm.
	 *
	 * @return one running cloudlet
	 *
	 * @pre $none
	 * @post $none
	 */
	 //cloudlet迁移
	@Override
	public Cloudlet migrateCloudlet() {
		ResCloudlet rcl = getCloudletExecList().remove(0);
		rcl.finalizeCloudlet();
		Cloudlet cl = rcl.getCloudlet();
		usedPes -= cl.getPesNumber();
		return cl;
	}

	/**
	 * Gets the cloudlet waiting list.
	 *
	 * @param <T> the generic type
	 * @return the cloudlet waiting list
	 */
	 //获取等待队列
	@SuppressWarnings("unchecked")
	protected <T extends ResCloudlet> List<T> getCloudletWaitingList() {
		return (List<T>) cloudletWaitingList;
	}

	/**
	 * Cloudlet waiting list.
	 *
	 * @param <T> the generic type
	 * @param cloudletWaitingList the cloudlet waiting list
	 */
	 //设置等待队列
	protected <T extends ResCloudlet> void cloudletWaitingList(List<T> cloudletWaitingList) {
		this.cloudletWaitingList = cloudletWaitingList;
	}

	/**
	 * Gets the cloudlet exec list.
	 *
	 * @param <T> the generic type
	 * @return the cloudlet exec list
	 */
	 //获取执行队列
	@SuppressWarnings("unchecked")
	protected <T extends ResCloudlet> List<T> getCloudletExecList() {
		return (List<T>) cloudletExecList;
	}

	/**
	 * Sets the cloudlet exec list.
	 *
	 * @param <T> the generic type
	 * @param cloudletExecList the new cloudlet exec list
	 */
	 //设置执行列表
	protected <T extends ResCloudlet> void setCloudletExecList(List<T> cloudletExecList) {
		this.cloudletExecList = cloudletExecList;
	}

	/**
	 * Gets the cloudlet paused list.
	 *
	 * @param <T> the generic type
	 * @return the cloudlet paused list
	 */
	 //获取暂停列表
	@SuppressWarnings("unchecked")
	protected <T extends ResCloudlet> List<T> getCloudletPausedList() {
		return (List<T>) cloudletPausedList;
	}

	/**
	 * Sets the cloudlet paused list.
	 *
	 * @param <T> the generic type
	 * @param cloudletPausedList the new cloudlet paused list
	 */
	 //设置暂停列表
	protected <T extends ResCloudlet> void setCloudletPausedList(List<T> cloudletPausedList) {
		this.cloudletPausedList = cloudletPausedList;
	}

	/**
	 * Gets the cloudlet finished list.
	 *
	 * @param <T> the generic type
	 * @return the cloudlet finished list
	 */
	 //获取完成列表
	@SuppressWarnings("unchecked")
	protected <T extends ResCloudlet> List<T> getCloudletFinishedList() {
		return (List<T>) cloudletFinishedList;
	}

	/**
	 * Sets the cloudlet finished list.
	 *
	 * @param <T> the generic type
	 * @param cloudletFinishedList the new cloudlet finished list
	 */
	 //设置完成列表
	protected <T extends ResCloudlet> void setCloudletFinishedList(List<T> cloudletFinishedList) {
		this.cloudletFinishedList = cloudletFinishedList;
	}

	/* (non-Javadoc)
	 * @see org.cloudbus.cloudsim.CloudletScheduler#getCurrentRequestedMips()
	 */
	@Override
	public List<Double> getCurrentRequestedMips() {
		List<Double> mipsShare = new ArrayList<Double>();
		if (getCurrentMipsShare() != null) {
			for (Double mips : getCurrentMipsShare()) {
				mipsShare.add(mips);
			}
		}
		return mipsShare;
	}

	/* (non-Javadoc)
	 * @see org.cloudbus.cloudsim.CloudletScheduler#getTotalCurrentAvailableMipsForCloudlet(org.cloudbus.cloudsim.ResCloudlet, java.util.List)
	 */
	@Override
	public double getTotalCurrentAvailableMipsForCloudlet(ResCloudlet rcl, List<Double> mipsShare) {
		double capacity = 0.0;
		int cpus = 0;
		for (Double mips : mipsShare) { // count the cpus available to the vmm
			capacity += mips;
			if (mips > 0) {
				cpus++;
			}
		}
		currentCpus = cpus;
		capacity /= cpus; // average capacity of each cpu
		return capacity;
	}:

	/* (non-Javadoc)
	 * @see org.cloudbus.cloudsim.CloudletScheduler#getTotalCurrentAllocatedMipsForCloudlet(org.cloudbus.cloudsim.ResCloudlet, double)
	 */
	@Override
	public double getTotalCurrentAllocatedMipsForCloudlet(ResCloudlet rcl, double time) {
		// TODO Auto-generated method stub
		return 0.0;
	}

	/* (non-Javadoc)
	 * @see org.cloudbus.cloudsim.CloudletScheduler#getTotalCurrentRequestedMipsForCloudlet(org.cloudbus.cloudsim.ResCloudlet, double)
	 */
	@Override
	public double getTotalCurrentRequestedMipsForCloudlet(ResCloudlet rcl, double time) {
		// TODO Auto-generated method stub
		return 0.0;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值