CloudSim源代码学习——任务单元(Cloudlet)
/*
* 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.text.DecimalFormat;//小数的 十进制的
import java.util.ArrayList;//数组
import java.util.LinkedList;//链接表
import java.util.List;//java.util集合框架、遗留的 collection 类、事件模型、日期和时间设施、国际化和各种实用工具类 import org.cloudbus.cloudsim.core.CloudSim; /**
* Cloudlet is an extension to the cloudlet. It stores, despite all the information
* encapsulated in the Cloudlet, the ID of the VM running it.
*
* @author Rodrigo N. Calheiros
* @author Anton Beloglazov
* @since CloudSim Toolkit 1.0
*/
public class Cloudlet { // the User or Broker ID. It is advisable that broker set this ID
// with its own ID, so that CloudResource returns to it after the execution
/** The user id. */ //用户ID
private int userId; // the size of this Cloudlet to be executed in a CloudResource (unit: in MI)
/** The cloudlet length. */ //云任务长度 单位:MI
private long cloudletLength; // the input file size of this Cloudlet before execution (unit: in byte)
/** The cloudlet file size. */ //云任务文件大小 单位:字节
private final long cloudletFileSize; // in byte = program + input data size //字节=程序+输入数据大小 // the output file size of this Cloudlet after execution (unit: in byte)
/** The cloudlet output size. */ //云任务输出文件大小
private final long cloudletOutputSize; /** The pes number. *///执行任务请求的PE数
private int pesNumber; // num of Pe required to execute this job /** The cloudlet id. */ //云任务ID
private final int cloudletId; // this Cloudlet ID /** The status. */ //云任务状态
private int status; // status of this Cloudlet /** The num. */ //十进制格式
private DecimalFormat num; // to format the decimal number /** The finish time. */ //云任务完成时间
private double finishTime; // the time where this Cloudlet completes // start time of executing this Cloudlet.
// With new functionalities, such as CANCEL, PAUSED and RESUMED, this
// attribute only stores the latest execution time. Previous execution time
// are ignored.
/** The exec start time. */ //执行仿真开始时间
private double execStartTime; // in simulation time /** The reservation id. */ //云任务预约ID 资源的ID?(预约资源)
private int reservationId = -1; // the ID of a reservation made for this cloudlet // records the transaction history for this Cloudlet //记录云任务的交易历史
/** The record. */ //是否记录?
private final boolean record; // record a history or not /** The newline. */ //换行?
private String newline; /** The history. */ //交易历史 String类是字符串常量,是不可更改的常量。而StringBuffer是字符串变量,它的对象是可以扩充和修改的
private StringBuffer history; /** The res list. */ //资源列表?
private final List<Resource> resList; /** The index. */ //索引
private int index; // differentiated service 不同的服务
/** The class type. */ //云任务类别
private int classType; // class type of Cloudlet for resource scheduling /** The net to s. */ //TOS(terms of service)服务条款 ?
private int netToS; // ToS for sending Cloudlet over the network //私有访问控制符private 只能被该类自身所访问和修改,而且不能被任何其他类(包括该类的子类)来获取和引用。private修饰符用来声明那些类的私有成员,它提供了最高的保护级别
////////////////////////////////////////////
// Below are CONSTANTS attributes 以下是常量属性
/** The Cloudlet has been created and added to the CloudletList object. */
public static final int CREATED = 0;//云任务创建并加入云任务列表对象 /** The Cloudlet has been assigned to a CloudResource object as planned. */
public static final int READY = 1; //云任务按照计划分配给资源 /** The Cloudlet has moved to a Cloud node. */
public static final int QUEUED = 2; //云任务提交给一个【云节点】 虚拟机吗?? /** The Cloudlet is in execution in a Cloud node. */
public static final int INEXEC = 3; //云任务执行 /** The Cloudlet has been executed successfully. */
public static final int SUCCESS = 4; //云任务成功执行 /** The Cloudlet is failed. */
public static final int FAILED = 5; //执行失败 /** The Cloudlet has been canceled. */
public static final int CANCELED = 6; //云任务被取消 /** The Cloudlet has been paused. It can be resumed by changing the status into <tt>RESUMED</tt>. */
public static final int PAUSED = 7; //云任务执行暂停 /** The Cloudlet has been resumed from <tt>PAUSED</tt> state. */
public static final int RESUMED = 8; //重启执行云任务 /** The cloudlet has failed due to a resource failure. */
public static final int FAILED_RESOURCE_UNAVAILABLE = 9;//由于资源失败 云任务失败 //公有访问控制符public Java的类是通过包的概念来组织的,包是类的一个松散的集合。具有了被其他包中的类访问的可能性,只要这些其他包中的类在程序中使用import语句引入public类,就可以访问和引用这个类
/** The vm id. */ //虚拟机编号
protected int vmId; /** The cost per bw. */ //带宽费用
protected double costPerBw; /** The accumulated bw cost. */ //累计的带宽费用
protected double accumulatedBwCost; //protected 被三种类所引用:该类自身、与它在同一个包中的其它类、在其他包中的该类的子类。使用protected修饰符的主要作用是允许其他包中该类的子类来访问父类的特定属性。 // Utilization 【利用率】 /** The utilization of cpu model. */ //CPU利用模型
private UtilizationModel utilizationModelCpu; /** The utilization of memory model. */ //memory利用模型
private UtilizationModel utilizationModelRam; /** The utilization of bw model. */ //BW利用模型
private UtilizationModel utilizationModelBw; // Data cloudlet 数据 云任务 ???
/** The required files. */ //请求的文件
private List<String> requiredFiles = null; // list of required filenames /**【分配一个新的云任务对象】
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
* By default this constructor sets the history of this object.
*
* @param cloudletId the unique ID of this Cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId, //加了一个final表示不能修改传入的参数的值
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw) {
this(cloudletId, cloudletLength, pesNumber, cloudletFileSize, cloudletOutputSize, utilizationModelCpu, utilizationModelRam, utilizationModelBw, true);
this.vmId=-1;
this.accumulatedBwCost=0.0;
this.costPerBw=0.0; this.requiredFiles = new LinkedList<String>();
}//this通常指当前对象 引用当前对象的某种东西,比如当前对象的某个方法,或当前对象的某个成员,你便可以利用this来实现这个目的,this的另一个用途是调用当前对象的另一个构造函数 /**
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
*
* @param cloudletId the unique ID of this cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param record record the history of this object or not
* @param fileList list of files required by this cloudlet
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId,
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw,
final boolean record,
final List<String> fileList) {
this(cloudletId, cloudletLength, pesNumber, cloudletFileSize, cloudletOutputSize, utilizationModelCpu, utilizationModelRam, utilizationModelBw, record);
this.vmId=-1;
this.accumulatedBwCost=0.0;//统计的带宽费用
this.costPerBw=0.0; this.requiredFiles = fileList;
} //与上一个对象大部分一样! /**
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
* By default this constructor sets the history of this object.
*
* @param cloudletId the unique ID of this Cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param fileList list of files required by this cloudlet
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId,
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw,
final List<String> fileList) {
this(cloudletId, cloudletLength, pesNumber, cloudletFileSize, cloudletOutputSize, utilizationModelCpu, utilizationModelRam, utilizationModelBw, true);
this.vmId=-1;
this.accumulatedBwCost=0.0;
this.costPerBw=0.0; this.requiredFiles = fileList;
} /**
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
*
* @param cloudletId the unique ID of this cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param record record the history of this object or not
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId,
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw,
final boolean record) {
this.userId = -1; // 代理或用户设置 to be set by a Broker or user
this.status = CREATED; //任务状态
this.cloudletId = cloudletId;
this.pesNumber = pesNumber;
this.execStartTime = 0.0;
this.finishTime = -1.0; // 任务还没还没完成 meaning this Cloudlet hasn't finished yet
this.classType = 0; //类别
this.netToS = 0; // Cloudlet length, Input and Output size should be at least 1 byte.至少一个字节
this.cloudletLength = Math.max(1, cloudletLength);
this.cloudletFileSize = Math.max(1, cloudletFileSize);
this.cloudletOutputSize = Math.max(1, cloudletOutputSize); // Normally, a Cloudlet is only executed on a resource without being
// migrated to others. Hence, to reduce memory consumption, set the
// size of this ArrayList to be less than the default one.设置内存大小数组列表小于默认值
this.resList = new ArrayList<Resource>(2);
this.index = -1;
this.record = record; this.vmId = -1;
this.accumulatedBwCost = 0.0;
this.costPerBw = 0.0; this.requiredFiles = new LinkedList<String>(); setUtilizationModelCpu(utilizationModelCpu);
setUtilizationModelRam(utilizationModelRam);
setUtilizationModelBw(utilizationModelBw);
} //【内部类】
//////////////////////// INTERNAL CLASS /////////////////////////////////// /**
* Internal class that keeps track Cloudlet's movement in different
* CloudResources.(不同的资源中登记云任务动向)
*/
private static class Resource { /** Cloudlet's submission time to a CloudResource. */
public double submissionTime = 0.0;//云任务的提交时间 /** The time of this Cloudlet resides in a CloudResource (from arrival time until departure time). */
public double wallClockTime = 0.0; //云任务从到达到撤离时间 /** The total execution time of this Cloudlet in a CloudResource. */
public double actualCPUTime = 0.0;//云任务执行时间 /** Cost per second a CloudResource charge to execute this Cloudlet. */
public double costPerSec = 0.0;//云资源每秒费用 /** Cloudlet's length finished so far. */
public long finishedSoFar = 0; //已完成的云任务长度 /** a CloudResource id. */
public int resourceId = -1;//云资源ID /** a CloudResource name. */
public String resourceName = null;//云资源名 } // end of internal class
//【结束内部类】
//////////////////////// End of Internal Class ////////////////////////// /**设置预约ID
* Sets the id of the reservation made for this cloudlet.
*
* @param resId the reservation ID
*
* @return <tt>true</tt> if the ID has successfully been set or
* <tt>false</tt> otherwise.
*/
public boolean setReservationId(final int resId) {//预约的ID是资源的ID?
if(resId <= 0) {
return false;
}
reservationId = resId;
return true;
} /**获取预约ID
* Gets the reservation ID that owns this Cloudlet.
*
* @return a reservation ID
*
* @pre $none
* @post $none
*/
public int getReservationId() {
return reservationId;
} /**云任务是否通过预约来提交
* Checks whether this Cloudlet is submitted by reserving or not.
*
* @return <tt>true</tt> if this Cloudlet has reserved before,
* <tt>false</tt> otherwise
*/
public boolean hasReserved() {
if (reservationId == -1) {
return false;
}
return true;
} /**设置云任务长度
* Sets the length or size (in MI) of this Cloudlet
* to be executed in a CloudResource.
* This Cloudlet length is calculated for 1 Pe only <tt>not</tt> the total
* length.
*
* @param cloudletLength the length or size (in MI) of this Cloudlet
* to be executed in a CloudResource
*
* @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise
*
* @pre cloudletLength > 0
* @post $none
*/
public boolean setCloudletLength(final long cloudletLength) {
if (cloudletLength <= 0) {
return false;
} this.cloudletLength = cloudletLength;
return true;
} /**设置服务等级【可以尝试】
* Sets the network service level for sending this cloudlet over a network.
*
* @param netServiceLevel determines the kind of service this cloudlet
* receives in the network (applicable to
* selected PacketScheduler class only)
*
* @return <code>true</code> if successful.
*
* @pre netServiceLevel >= 0
* @post $none
*/
public boolean setNetServiceLevel(final int netServiceLevel) {
boolean success = false;
if (netServiceLevel > 0) {
netToS = netServiceLevel;
success = true;
} return success;
} /**获取服务等级
* Gets the network service level for sending this cloudlet over a network.
*
* @return the network service level
*
* @pre $none
* @post $none
*/
public int getNetServiceLevel() {
return netToS;
} /**获取云任务等待时间
* Gets the waiting time of this cloudlet executed on a resource.
*
* @return the waiting time
*
* @pre $none
* @post $none
*/
public double getWaitingTime() {
if (index == -1) {
return 0;
} // use the latest resource submission time
final double subTime = resList.get(index).submissionTime;
return execStartTime - subTime;//执行开始时间-提交时间
} /**任务类别(优先权)【尝试扩展使用】
* Sets the classType or priority of this Cloudlet for scheduling on a
* resource.
*
* @param classType classType of this Cloudlet
*
* @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise
*
* @pre classType > 0
* @post $none
*/
public boolean setClassType(final int classType) {
boolean success = false;
if (classType > 0) {
this.classType = classType;
success = true;
} return success;
} /**获取任务类别(优先权)
* Gets the classtype or priority of this Cloudlet for scheduling on a
* resource.
*
* @return classtype of this cloudlet
*
* @pre $none
* @post $none
*/
public int getClassType() {
return classType;
} /**任务请求的PE
* Sets the number of PEs required to run this Cloudlet. <br>
* NOTE: The Cloudlet length is computed only for 1 Pe for simplicity. <br>
* For example, this Cloudlet has a length of 500 MI and requires 2 PEs.
* This means each Pe will execute 500 MI of this Cloudlet.每个PE都执行500 不是各分担250???
*
* @param pesNumber number of Pe
*
* @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise
*
* @pre numPE > 0
* @post $none
*/
public boolean setPesNumber(final int pesNumber) {
if (pesNumber > 0) {
this.pesNumber = pesNumber;
return true;
}
return false;
} /**
* Gets the number of PEs required to run this Cloudlet.
*
* @return number of PEs
*
* @pre $none
* @post $none
*/
public int getPesNumber() {
return pesNumber;
} /**云任务历史记录
* Gets the history of this Cloudlet. The layout of this history is in a
* readable table column with <tt>time</tt> and <tt>description</tt>
* as headers.
*
* @return a String containing the history of this Cloudlet object.
*
* @pre $none
* @post $result != null
*/
public String getCloudletHistory() {
String msg = null;
if (history == null) {
msg = "No history is recorded for Cloudlet #" + cloudletId;
} else {
msg = history.toString();
} return msg;
} /**任务已执行长度(适合 迁移任务或取消)
* Gets the length of this Cloudlet that has been executed so far
* from the latest CloudResource. This
* method is useful when trying to move this Cloudlet into different
* CloudResources or to cancel it.
*
* @return the length of a partially executed Cloudlet or the full Cloudlet
* length if it is completed
*
* @pre $none
* @post $result >= 0.0
*/
public long getCloudletFinishedSoFar() {
if (index == -1) {
return cloudletLength;
} final long finish = resList.get(index).finishedSoFar;//resList 资源列表?
if (finish > cloudletLength) {
return cloudletLength;
} return finish;
} /**检验云任务是否执行完毕
* Checks whether this Cloudlet has finished execution or not.
*
* @return <tt>true</tt> if this Cloudlet has finished execution,
* <tt>false</tt> otherwise
*
* @pre $none
* @post $none
*/
public boolean isFinished() {
if (index == -1) {
return false;
} boolean completed = false; // if result is 0 or -ve then this Cloudlet has finished
final long finish = resList.get(index).finishedSoFar;
final long result = cloudletLength - finish;
if (result <= 0.0) {
completed = true;
} return completed;
} /**设置任务执行长度
* Sets the length of this Cloudlet that has been executed so far.
* This method is used by ResCloudlet class when an application
* is decided to cancel or to move this Cloudlet into different
* CloudResources.
*
* @param length length of this Cloudlet
*
* @see gridsim.AllocPolicy
* @see gridsim.ResCloudlet
* @pre length >= 0.0
* @post $none
*/
public void setCloudletFinishedSoFar(final long length) {
// if length is -ve then ignore
if (length < 0.0 || index < 0) {
return;
} final Resource res = resList.get(index);
res.finishedSoFar = length; if (record) {
write("Sets the length's finished so far to " + length);
}
} /**设置用户ID
* Sets the user or owner ID of this Cloudlet. It is <tt>VERY</tt> important
* to set the user ID, otherwise this Cloudlet will not be executed in a
* CloudResource.
*
* @param id the user ID
*
* @pre id >= 0
* @post $none
*/
public void setUserId(final int id) {
userId = id;
if (record) {
write("Assigns the Cloudlet to " + CloudSim.getEntityName(id) + " (ID #" + id + ")");
}
} /**
* Gets the user or owner ID of this Cloudlet.
*
* @return the user ID or <tt>-1</tt> if the user ID has not been set before
*
* @pre $none
* @post $result >= -1
*/
public int getUserId() {
return userId;
} /**资源ID
* Gets the latest resource ID that processes this Cloudlet.
*
* @return the resource ID or <tt>-1</tt> if none
*
* @pre $none
* @post $result >= -1
*/
public int getResourceId() {
if (index == -1) {
return -1;
}
return resList.get(index).resourceId;
} /**获取输入文件大小
* Gets the input file size of this Cloudlet <tt>BEFORE</tt>
* submitting to a CloudResource.
*
* @return the input file size of this Cloudlet
*
* @pre $none
* @post $result >= 1
*/
public long getCloudletFileSize() {
return cloudletFileSize;
} /**获取输出文件大小
* Gets the output size of this Cloudlet <tt>AFTER</tt> submitting and
* executing to a CloudResource.
*
* @return the Cloudlet output file size
*
* @pre $none
* @post $result >= 1
*/
public long getCloudletOutputSize() {
return cloudletOutputSize;
} /**通过资源实体设置资源参数
* Sets the resource parameters for which this Cloudlet is going to be
* executed. <br>
* NOTE: This method <tt>should</tt> be called only by a 【resource entity】,
* not the user or owner of this Cloudlet.
*
* @param resourceID the CloudResource ID
* @param cost the cost running this CloudResource per second
*
* @pre resourceID >= 0
* @pre cost > 0.0
* @post $none
*/
public void setResourceParameter(final int resourceID, final double cost) {
final Resource res = new Resource();
res.resourceId = resourceID;
res.costPerSec = cost;
res.resourceName = CloudSim.getEntityName(resourceID); // add into a list if moving to a new grid resource
resList.add(res); if (index == -1 && record) {
write("Allocates this Cloudlet to " + res.resourceName + " (ID #" + resourceID + ") with cost = $" + cost + "/sec");
} else if (record) {
final int id = resList.get(index).resourceId;
final String name = resList.get(index).resourceName;
write("Moves Cloudlet from " + name + " (ID #" + id + ") to " +
res.resourceName + " (ID #" + resourceID +
") with cost = $" + cost + "/sec");
} index++; // initially, index = -1
} /**设置 提交 到达 时间
* Sets the submission or arrival time of this Cloudlet into a CloudResource.
*
* @param clockTime 提交时间 the submission time
*
* @pre clockTime >= 0.0
* @post $none
*/
public void setSubmissionTime(final double clockTime) {
if (clockTime < 0.0 || index < 0) {
return;
} final Resource res = resList.get(index);
res.submissionTime = clockTime; if (record) {
write( "Sets the submission time to " + num.format(clockTime) );
}
} /**
* Gets the submission or arrival time of this Cloudlet from
* the latest CloudResource.
*
* @return the submission time or <tt>0.0</tt> if none
*
* @pre $none
* @post $result >= 0.0
*/
public double getSubmissionTime() {
if (index == -1) {
return 0.0;
}
return resList.get(index).submissionTime;
} /**设置执行开始时间 (取消 暂停 重启)
* Sets the execution start time of this Cloudlet inside a CloudResource.
* <b>NOTE:</b> With new functionalities, such as being able to cancel /
* to pause / to resume this Cloudlet, the execution start time only holds
* the latest one. Meaning, all previous execution start time are ignored.
*
* @param clockTime the latest execution start time
*
* @pre clockTime >= 0.0
* @post $none
*/
public void setExecStartTime(final double clockTime) {
execStartTime = clockTime;
if (record) {
write("Sets the execution start time to " + num.format(clockTime));
}
} /**
* Gets the latest execution start time.
*
* @return the latest execution start time
*
* @pre $none
* @post $result >= 0.0
*/
public double getExecStartTime() {
return execStartTime;
} /**云任务执行参数设置
* Sets this Cloudlet's execution parameters. These parameters are set by
* the CloudResource before departure or sending back to the original
* Cloudlet's owner.
*
* @param wallTime (云任务停留时间) the time of this Cloudlet resides in
* a CloudResource (from arrival time until
* departure time).
* @param actualTime (执行时间) the total execution time of this Cloudlet in a
* CloudResource.
*
* @pre wallTime >= 0.0
* @pre actualTime >= 0.0
* @post $none
*/
public void setExecParam(final double wallTime, final double actualTime) {
if (wallTime < 0.0 || actualTime < 0.0 || index < 0) {
return;
} final Resource res = resList.get(index);
res.wallClockTime = wallTime;
res.actualCPUTime = actualTime; if (record) {
write("Sets the wall clock time to "+ num.format(wallTime)+
" and the actual CPU time to " + num.format(actualTime));
}
} /**云任务状态设置
* Sets the status code of this Cloudlet.
*
* @param newStatus the status code of this Cloudlet
*
* @throws Exception Invalid range of Cloudlet status
*
* @pre newStatus >= 0 && newStatus <= 8
* @post $none
*/
public void setCloudletStatus(final int newStatus) throws Exception {
// if the new status is same as current one, then ignore the rest
if (status == newStatus) {
return;
} // throws an exception if the new status is outside the range
if (newStatus < Cloudlet.CREATED || newStatus > Cloudlet.FAILED_RESOURCE_UNAVAILABLE) {
throw new Exception("Cloudlet.setCloudletStatus() : Error - Invalid integer range for Cloudlet status.");
}//newStatus Cloudlet.CREATED 有可比性 if (newStatus == Cloudlet.SUCCESS) {
finishTime = CloudSim.clock();
} if (record) {
write("Sets Cloudlet status from " + getCloudletStatusString() + " to " + Cloudlet.getStatusString(newStatus));
} this.status = newStatus;
} /**
* Gets the status code of this Cloudlet.
*
* @return the status code of this Cloudlet
*
* @pre $none
* @post $result >= 0
*/
public int getCloudletStatus() {
return status;
} /**代表此刻云任务状态
* Gets the string representation of the current Cloudlet status code.
*
* @return the Cloudlet status code as a string or <tt>null</tt> if the
* status code is unknown
*
* @pre $none
* @post $none
*/
public String getCloudletStatusString() {
return Cloudlet.getStatusString(status);
} /**
* Gets the string representation of the given Cloudlet status code.
*
* @param status the Cloudlet status code
*
* @return the Cloudlet status code as a string or <tt>null</tt> if the
* status code is unknown
*
* @pre $none
* @post $none
*/
public static String getStatusString(final int status) {
String statusString = null;
switch (status)
{
case Cloudlet.CREATED://创建
statusString = "Created";
break; case Cloudlet.READY://准备
statusString = "Ready";
break; case Cloudlet.INEXEC://执行
statusString = "InExec";
break; case Cloudlet.SUCCESS://成功
statusString = "Success";
break; case Cloudlet.QUEUED://排队等候
statusString = "Queued";
break; case Cloudlet.FAILED://失败
statusString = "Failed";
break; case Cloudlet.CANCELED://取消
statusString = "Canceled";
break; case Cloudlet.PAUSED://暂停
statusString = "Paused";
break; case Cloudlet.RESUMED://重启
statusString = "Resumed";
break; case Cloudlet.FAILED_RESOURCE_UNAVAILABLE ://无可用资源
statusString = "Failed_resource_unavailable";
break; default:
break;
} return statusString;
} /**获取云任务长度
* Gets the length of this Cloudlet.
*
* @return the length of this Cloudlet
*
* @pre $none
* @post $result >= 0.0
*/
public long getCloudletLength() {
return cloudletLength;
} /**获取任务总长度
* Gets the total length (across all PEs) of this Cloudlet.
*
* @return the total length of this Cloudlet
*
* @pre $none
* @post $result >= 0.0
*/
public long getCloudletTotalLength() {
return getCloudletLength() * getPesNumber();//为什么乘以PE数
} /**云资源上的执行费用
* Gets the cost running this Cloudlet in the latest CloudResource.
*
* @return the cost associated with running this Cloudlet
* or <tt>0.0</tt> if none
*
* @pre $none
* @post $result >= 0.0
*/
public double getCostPerSec() {
if (index == -1) {
return 0.0;
}
return resList.get(index).costPerSec;
} /**云任务停留时间在最近的云资源上 (云资源 是指 虚拟机??)
* Gets the time of this Cloudlet resides in the latest CloudResource
* (from arrival time until departure time).
*
* @return the time of this Cloudlet resides in a CloudResource
*
* @pre $none
* @post $result >= 0.0
*/
public double getWallClockTime() {
if (index == -1) {
return 0.0;
}
return resList.get(index).wallClockTime;
} /**执行云任务的所有资源名称
* Gets all the CloudResource names that executed this Cloudlet.
*
* @return an array of CloudResource names or <tt>null</tt> if it has none
*
* @pre $none
* @post $none
*/
public String[] getAllResourceName() {
final int size = resList.size();
String[] data = null; if (size > 0) {
data = new String[size];
for (int i = 0; i < size; i++) {
data[i] = resList.get(i).resourceName;
}
} return data;
} /**执行云任务的所有资源ID号
* Gets all the CloudResource IDs that executed this Cloudlet.
*
* @return an array of CloudResource IDs or <tt>null</tt> if it has none
*
* @pre $none
* @post $none
*/
public int[] getAllResourceId() {
final int size = resList.size();
int[] data = null; if (size > 0) {
data = new int[size];
for (int i = 0; i < size; i++) {
data[i] = resList.get(i).resourceId;
}
} return data;
} /**云任务执行的总时间
* Gets the total execution time of this Cloudlet in a given CloudResource ID.
*
* @param resId a CloudResource entity ID
*
* @return the total execution time of this Cloudlet in a CloudResource
* or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getActualCPUTime(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.actualCPUTime;
}
return 0.0;
} /**指定的资源上运行云任务的费用
* Gets the cost running this Cloudlet in a given CloudResource ID.
*
* @param resId a CloudResource entity ID
*
* @return the cost associated with running this Cloudlet
* or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getCostPerSec(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.costPerSec;
}
return 0.0;
} /**指定的资源上获取云任务已执行的长度 (取消 或 移动任务至不同的云资源)
* Gets the length of this Cloudlet that has been executed so far in a given
* CloudResource ID. This method is useful when trying to move this Cloudlet
* into different CloudResources or to cancel it.
*
* @param resId a CloudResource entity ID
*
* @return the length of a partially executed Cloudlet or the full Cloudlet
* length if it is completed or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public long getCloudletFinishedSoFar(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.finishedSoFar;
}
return 0;
} /**指定的资源上获取云任务提交 到达时间
* Gets the submission or arrival time of this Cloudlet in the
* given CloudResource ID.
*
* @param resId a CloudResource entity ID
*
* @return the submission time or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getSubmissionTime(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.submissionTime;
}
return 0.0;
} /**指定的资源上云任务停留的时间
* Gets the time of this Cloudlet resides in a given CloudResource ID
* (from arrival time until departure time).
*
* @param resId a CloudResource entity ID
*
* @return the time of this Cloudlet resides in the CloudResource
* or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getWallClockTime(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.wallClockTime;
}
return 0.0;
} /**在ID基础上获取云资源名
* Gets the CloudResource name based on its ID.
*
* @param resId a CloudResource entity ID
*
* @return the CloudResource name or <tt>null</tt> if not found
*
* @pre resId >= 0
* @post $none
*/
public String getResourceName(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.resourceName;
}
return null;
} /**由ID获取资源
* Gets the resource by id.
*
* @param resourceId the resource id
*
* @return the resource by id
*/
public Resource getResourceById(final int resourceId) {
for (Resource resource : resList) {
if (resource.resourceId == resourceId) {
return resource;
}
}
return null;
} /**获取云任务在资源中完成时间
* Gets the finish time of this Cloudlet in a CloudResource.
*
* @return the finish or completion time of this Cloudlet or <tt>-1</tt> if
* not finished yet.
*
* @pre $none
* @post $result >= -1
*/
public double getFinishTime() {
return finishTime;
} ////////////////////////// PROTECTED METHODS ////////////////////////////// /**记录云任务特别交易历史
* Writes this particular history transaction of this Cloudlet into a log.
*
* @param str a history transaction of this Cloudlet
*
* @pre str != null
* @post $none
*/
protected void write(final String str) {
if (!record) {
return;
} if (num == null || history == null) { // Creates the history or transactions of this Cloudlet
newline = System.getProperty("line.separator");
num = new DecimalFormat("#0.00#"); // with 3 decimal spaces
history = new StringBuffer(1000);
history.append("Time below denotes the simulation time.");
history.append( System.getProperty("line.separator") );
history.append("Time (sec) Description Cloudlet #"+cloudletId);
history.append( System.getProperty("line.separator") );
history.append("------------------------------------------");
history.append( System.getProperty("line.separator") );
history.append( num.format(CloudSim.clock()) );
history.append(" Creates Cloudlet ID #" + cloudletId);
history.append( System.getProperty("line.separator") );
} history.append(num.format(CloudSim.clock()));
history.append(" " + str + newline);
} /**云任务状态
* Get the status of the Cloudlet.
*
* @return status of the Cloudlet
*
* @pre $none
* @post $none
*/
public int getStatus(){
return getCloudletStatus();
} /**获取任务ID
* Gets the ID of this Cloudlet.
*
* @return Cloudlet Id
*
* @pre $none
* @post $none
*/
public int getCloudletId() {
return this.cloudletId;
} /**获取运行云任务的虚拟机ID
* Gets the ID of the VM that will run this Cloudlet.
*
* @return VM Id, -1 if the Cloudlet was not assigned to a VM
*
* @pre $none
* @post $none
*/
public int getVmId() {
return vmId;
} /**设置虚拟机ID
* Sets the ID of the VM that will run this Cloudlet.
*
* @param vmId the vm id
*
* @pre id >= 0
* @post $none
*/
public void setVmId(final int vmId) {
this.vmId = vmId;
} /**
* Returns the time the Cloudlet actually run.
*
* @return time in which the Cloudlet was running
*
* @pre $none
* @post $none
*/
public double getActualCPUTime(){
return getFinishTime() - getExecStartTime();
} /**设置资源参数
* Sets the resource parameters for which this Cloudlet is going to be
* executed. <br>
* NOTE: This method <tt>should</tt> be called only by a resource entity,
* not the user or owner of this Cloudlet.
*
* @param resourceID the CloudResource ID
* @param costPerCPU the cost running this Cloudlet per second
* @param costPerBw the cost of data transfer to this PowerDatacenter
*
* @pre resourceID >= 0
* @pre cost > 0.0
* @post $none
*/
public void setResourceParameter(final int resourceID, final double costPerCPU, final double costPerBw) {
setResourceParameter(resourceID, costPerCPU);
this.costPerBw = costPerBw;
this.accumulatedBwCost = costPerBw * getCloudletFileSize(); } /**执行云任务总费用
* Gets the total cost of processing or executing this Cloudlet
* <tt>Processing Cost = input data transfer + processing cost + output transfer cost</tt>.
*
* @return the total cost of processing Cloudlet
*
* @pre $none
* @post $result >= 0.0
*/
public double getProcessingCost() {
//cloudlet cost: execution cost...
//double cost = getProcessingCost();//处理费用
double cost = 0;
//...plus input data transfer cost...//数据传送费用
cost += this.accumulatedBwCost;
//...plus output cost输出费用
cost += this.costPerBw * getCloudletOutputSize();
return cost;
} // Data cloudlet /**
* Gets the required files.
*
* @return the required files
*/
public List<String> getRequiredFiles() {
return requiredFiles;
} /**设置请求文件 (是指什么呢???)
* Sets the required files.
*
* @param requiredFiles the new required files
*/
protected void setRequiredFiles(final List<String> requiredFiles) {
this.requiredFiles = requiredFiles;
} /**
* Adds the required filename to the list.
*
* @param fileName the required filename
*
* @return <tt>true</tt> if succesful, <tt>false</tt> otherwise
*/
public boolean addRequiredFile(final String fileName) {
// if the list is empty
if (getRequiredFiles() == null) {
setRequiredFiles(new LinkedList<String>());
} // then check whether filename already exists or not
boolean result = false;
for (int i = 0; i < getRequiredFiles().size(); i++) {
final String temp = getRequiredFiles().get(i);
if (temp.equals(fileName)) {
result = true;
break;
}
} if (!result) {
getRequiredFiles().add(fileName);
} return result;
} /**删除给出的问就爱你名
* Deletes the given filename from the list.
*
* @param filename the given filename to be deleted
*
* @return <tt>true</tt> if succesful, <tt>false</tt> otherwise
*/
public boolean deleteRequiredFile(final String filename) {
boolean result = false;
if (getRequiredFiles() == null) {
return result;
} for (int i = 0; i < getRequiredFiles().size(); i++) {
final String temp = getRequiredFiles().get(i); if (temp.equals(filename)) {
getRequiredFiles().remove(i);
result = true; break;
}
} return result;
} /**
* Checks whether this cloudlet requires any files or not.
*
* @return <tt>true</tt> if required, <tt>false</tt> otherwise
*/
public boolean requiresFiles() {
boolean result = false;
if (getRequiredFiles() != null && getRequiredFiles().size() > 0) {
result = true;
} return result;
} //【设置 cpu ram bw 利用率】
/**
* Gets the utilization model cpu.
*
* @return the utilization model cpu
*/
public UtilizationModel getUtilizationModelCpu() {
return utilizationModelCpu;
} /**
* Sets the utilization model cpu.
*
* @param utilizationModelCpu the new utilization model cpu
*/
public void setUtilizationModelCpu(final UtilizationModel utilizationModelCpu) {
this.utilizationModelCpu = utilizationModelCpu;
} /**
* Gets the utilization model ram.
*
* @return the utilization model ram
*/
public UtilizationModel getUtilizationModelRam() {
return utilizationModelRam;
} /**
* Sets the utilization model ram.
*
* @param utilizationModelRam the new utilization model ram
*/
public void setUtilizationModelRam(final UtilizationModel utilizationModelRam) {
this.utilizationModelRam = utilizationModelRam;
} /**
* Gets the utilization model bw.
*
* @return the utilization model bw
*/
public UtilizationModel getUtilizationModelBw() {
return utilizationModelBw;
} /**
* Sets the utilization model bw.
*
* @param utilizationModelBw the new utilization model bw
*/
public void setUtilizationModelBw(final UtilizationModel utilizationModelBw) {
this.utilizationModelBw = utilizationModelBw;
} /**
* Gets the total utilization of cpu.
*
* @param time the time
*
* @return the utilization of cpu
*/
public double getUtilizationOfCpu(final double time) {
return getUtilizationModelCpu().getUtilization(time);
} /**
* Gets the utilization of memory.
*
* @param time the time
*
* @return the utilization of memory
*/
public double getUtilizationOfRam(final double time) {
return getUtilizationModelRam().getUtilization(time);
} /**
* Gets the utilization of bw.
*
* @param time the time
*
* @return the utilization of bw
*/
public double getUtilizationOfBw(final double time) {
return getUtilizationModelBw().getUtilization(time);
} }
/*
* 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.text.DecimalFormat;//小数的 十进制的
import java.util.ArrayList;//数组
import java.util.LinkedList;//链接表
import java.util.List;//java.util集合框架、遗留的 collection 类、事件模型、日期和时间设施、国际化和各种实用工具类
import org.cloudbus.cloudsim.core.CloudSim;
/**
* Cloudlet is an extension to the cloudlet. It stores, despite all the information
* encapsulated in the Cloudlet, the ID of the VM running it.
*
* @authorRodrigo N. Calheiros
* @authorAnton Beloglazov
* @sinceCloudSim Toolkit 1.0
*/
public class Cloudlet {
// the User or Broker ID. It is advisable that broker set this ID
// with its own ID, so that CloudResource returns to it after the execution
/** The user id. */ //用户ID
private int userId;
// the size of this Cloudlet to be executed in a CloudResource (unit: in MI)
/** The cloudlet length. */ //云任务长度 单位:MI
private long cloudletLength;
// the input file size of this Cloudlet before execution (unit: in byte)
/** The cloudlet file size. */ //云任务文件大小 单位:字节
private final long cloudletFileSize; // in byte = program + input data size //字节=程序+输入数据大小
// the output file size of this Cloudlet after execution (unit: in byte)
/** The cloudlet output size. */ //云任务输出文件大小
private final long cloudletOutputSize;
/** The pes number. *///执行任务请求的PE数
private int pesNumber; // num of Pe required to execute this job
/** The cloudlet id. */ //云任务ID
private final int cloudletId; // this Cloudlet ID
/** The status. */ //云任务状态
private int status; // status of this Cloudlet
/** The num. */ //十进制格式
private DecimalFormat num; // to format the decimal number
/** The finish time. */ //云任务完成时间
private double finishTime; // the time where this Cloudlet completes
// start time of executing this Cloudlet.
// With new functionalities, such as CANCEL, PAUSED and RESUMED, this
// attribute only stores the latest execution time. Previous execution time
// are ignored.
/** The exec start time. */ //执行仿真开始时间
private double execStartTime; // in simulation time
/** The reservation id. */ //云任务预约ID 资源的ID?(预约资源)
private int reservationId = -1; // the ID of a reservation made for this cloudlet
// records the transaction history for this Cloudlet //记录云任务的交易历史
/** The record. */ //是否记录?
private final boolean record; // record a history or not
/** The newline. */ //换行?
private String newline;
/** The history. */ //交易历史 String类是字符串常量,是不可更改的常量。而StringBuffer是字符串变量,它的对象是可以扩充和修改的
private StringBuffer history;
/** The res list. */ //资源列表?
private final List<Resource> resList;
/** The index. */ //索引
private int index;
// differentiated service 不同的服务
/** The class type. */ //云任务类别
private int classType; // class type of Cloudlet for resource scheduling
/** The net to s. */ //TOS(terms of service)服务条款 ?
private int netToS; // ToS for sending Cloudlet over the network
//私有访问控制符private 只能被该类自身所访问和修改,而且不能被任何其他类(包括该类的子类)来获取和引用。private修饰符用来声明那些类的私有成员,它提供了最高的保护级别
////////////////////////////////////////////
// Below are CONSTANTS attributes 以下是常量属性
/** The Cloudlet has been created and added to the CloudletList object. */
public static final int CREATED = 0;//云任务创建并加入云任务列表对象
/** The Cloudlet has been assigned to a CloudResource object as planned. */
public static final int READY = 1; //云任务按照计划分配给资源
/** The Cloudlet has moved to a Cloud node. */
public static final int QUEUED = 2; //云任务提交给一个【云节点】 虚拟机吗??
/** The Cloudlet is in execution in a Cloud node. */
public static final int INEXEC = 3; //云任务执行
/** The Cloudlet has been executed successfully. */
public static final int SUCCESS = 4; //云任务成功执行
/** The Cloudlet is failed. */
public static final int FAILED = 5; //执行失败
/** The Cloudlet has been canceled. */
public static final int CANCELED = 6; //云任务被取消
/** The Cloudlet has been paused. It can be resumed by changing the status into <tt>RESUMED</tt>. */
public static final int PAUSED = 7; //云任务执行暂停
/** The Cloudlet has been resumed from <tt>PAUSED</tt> state. */
public static final int RESUMED = 8; //重启执行云任务
/** The cloudlet has failed due to a resource failure. */
public static final int FAILED_RESOURCE_UNAVAILABLE = 9;//由于资源失败 云任务失败
//公有访问控制符public Java的类是通过包的概念来组织的,包是类的一个松散的集合。具有了被其他包中的类访问的可能性,只要这些其他包中的类在程序中使用import语句引入public类,就可以访问和引用这个类
/** The vm id. */ //虚拟机编号
protected int vmId;
/** The cost per bw. */ //带宽费用
protected double costPerBw;
/** The accumulated bw cost. */ //累计的带宽费用
protected double accumulatedBwCost;
//protected 被三种类所引用:该类自身、与它在同一个包中的其它类、在其他包中的该类的子类。使用protected修饰符的主要作用是允许其他包中该类的子类来访问父类的特定属性。
// Utilization 【利用率】
/** The utilization of cpu model. */ //CPU利用模型
private UtilizationModel utilizationModelCpu;
/** The utilization of memory model. */ //memory利用模型
private UtilizationModel utilizationModelRam;
/** The utilization of bw model. */ //BW利用模型
private UtilizationModel utilizationModelBw;
// Data cloudlet 数据 云任务 ???
/** The required files. */ //请求的文件
private List<String> requiredFiles = null; // list of required filenames
/**【分配一个新的云任务对象】
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
* By default this constructor sets the history of this object.
*
* @param cloudletId the unique ID of this Cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId, //加了一个final表示不能修改传入的参数的值
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw) {
this(cloudletId, cloudletLength, pesNumber, cloudletFileSize, cloudletOutputSize, utilizationModelCpu, utilizationModelRam, utilizationModelBw, true);
this.vmId=-1;
this.accumulatedBwCost=0.0;
this.costPerBw=0.0;
this.requiredFiles = new LinkedList<String>();
}//this通常指当前对象 引用当前对象的某种东西,比如当前对象的某个方法,或当前对象的某个成员,你便可以利用this来实现这个目的,this的另一个用途是调用当前对象的另一个构造函数
/**
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
*
* @param cloudletId the unique ID of this cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param record record the history of this object or not
* @param fileList list of files required by this cloudlet
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId,
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw,
final boolean record,
final List<String> fileList) {
this(cloudletId, cloudletLength, pesNumber, cloudletFileSize, cloudletOutputSize, utilizationModelCpu, utilizationModelRam, utilizationModelBw, record);
this.vmId=-1;
this.accumulatedBwCost=0.0;//统计的带宽费用
this.costPerBw=0.0;
this.requiredFiles = fileList;
} //与上一个对象大部分一样!
/**
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
* By default this constructor sets the history of this object.
*
* @param cloudletId the unique ID of this Cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param fileList list of files required by this cloudlet
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId,
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw,
final List<String> fileList) {
this(cloudletId, cloudletLength, pesNumber, cloudletFileSize, cloudletOutputSize, utilizationModelCpu, utilizationModelRam, utilizationModelBw, true);
this.vmId=-1;
this.accumulatedBwCost=0.0;
this.costPerBw=0.0;
this.requiredFiles = fileList;
}
/**
* Allocates a new Cloudlet object. The Cloudlet length, input and output
* file sizes should be greater than or equal to 1.
*
* @param cloudletId the unique ID of this cloudlet
* @param cloudletLength the length or size (in MI) of this cloudlet
* to be executed in a PowerDatacenter
* @param cloudletFileSize the file size (in byte) of this cloudlet
* <tt>BEFORE</tt> submitting to a PowerDatacenter
* @param cloudletOutputSize the file size (in byte) of this cloudlet
* <tt>AFTER</tt> finish executing by
* a PowerDatacenter
* @param record record the history of this object or not
* @param pesNumber the pes number
* @param utilizationModelCpu the utilization model cpu
* @param utilizationModelRam the utilization model ram
* @param utilizationModelBw the utilization model bw
*
* @pre cloudletID >= 0
* @pre cloudletLength >= 0.0
* @pre cloudletFileSize >= 1
* @pre cloudletOutputSize >= 1
* @post $none
*/
public Cloudlet(
final int cloudletId,
final long cloudletLength,
final int pesNumber,
final long cloudletFileSize,
final long cloudletOutputSize,
final UtilizationModel utilizationModelCpu,
final UtilizationModel utilizationModelRam,
final UtilizationModel utilizationModelBw,
final boolean record) {
this.userId = -1; // 代理或用户设置 to be set by a Broker or user
this.status = CREATED; //任务状态
this.cloudletId = cloudletId;
this.pesNumber = pesNumber;
this.execStartTime = 0.0;
this.finishTime = -1.0; // 任务还没还没完成 meaning this Cloudlet hasn't finished yet
this.classType = 0; //类别
this.netToS = 0;
// Cloudlet length, Input and Output size should be at least 1 byte.至少一个字节
this.cloudletLength = Math.max(1, cloudletLength);
this.cloudletFileSize = Math.max(1, cloudletFileSize);
this.cloudletOutputSize = Math.max(1, cloudletOutputSize);
// Normally, a Cloudlet is only executed on a resource without being
// migrated to others. Hence, to reduce memory consumption, set the
// size of this ArrayList to be less than the default one.设置内存大小数组列表小于默认值
this.resList = new ArrayList<Resource>(2);
this.index = -1;
this.record = record;
this.vmId = -1;
this.accumulatedBwCost = 0.0;
this.costPerBw = 0.0;
this.requiredFiles = new LinkedList<String>();
setUtilizationModelCpu(utilizationModelCpu);
setUtilizationModelRam(utilizationModelRam);
setUtilizationModelBw(utilizationModelBw);
}
//【内部类】
//////////////////////// INTERNAL CLASS ///////////////////////////////////
/**
* Internal class that keeps track Cloudlet's movement in different
* CloudResources.(不同的资源中登记云任务动向)
*/
private static class Resource {
/** Cloudlet's submission time to a CloudResource. */
public double submissionTime = 0.0;//云任务的提交时间
/** The time of this Cloudlet resides in a CloudResource (from arrival time until departure time). */
public double wallClockTime = 0.0; //云任务从到达到撤离时间
/** The total execution time of this Cloudlet in a CloudResource. */
public double actualCPUTime = 0.0;//云任务执行时间
/** Cost per second a CloudResource charge to execute this Cloudlet. */
public double costPerSec = 0.0;//云资源每秒费用
/** Cloudlet's length finished so far. */
public long finishedSoFar = 0; //已完成的云任务长度
/** a CloudResource id. */
public int resourceId = -1;//云资源ID
/** a CloudResource name. */
public String resourceName = null;//云资源名
} // end of internal class
//【结束内部类】
//////////////////////// End of Internal Class //////////////////////////
/**设置预约ID
* Sets the id of the reservation made for this cloudlet.
*
* @param resId the reservation ID
*
* @return <tt>true</tt> if the ID has successfully been set or
* <tt>false</tt> otherwise.
*/
public boolean setReservationId(final int resId) {//预约的ID是资源的ID?
if(resId <= 0) {
return false;
}
reservationId = resId;
return true;
}
/**获取预约ID
* Gets the reservation ID that owns this Cloudlet.
*
* @return a reservation ID
*
* @pre $none
* @post $none
*/
public int getReservationId() {
return reservationId;
}
/**云任务是否通过预约来提交
* Checks whether this Cloudlet is submitted by reserving or not.
*
* @return <tt>true</tt> if this Cloudlet has reserved before,
* <tt>false</tt> otherwise
*/
public boolean hasReserved() {
if (reservationId == -1) {
return false;
}
return true;
}
/**设置云任务长度
* Sets the length or size (in MI) of this Cloudlet
* to be executed in a CloudResource.
* This Cloudlet length is calculated for 1 Pe only <tt>not</tt> the total
* length.
*
* @param cloudletLength the length or size (in MI) of this Cloudlet
* to be executed in a CloudResource
*
* @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise
*
* @pre cloudletLength > 0
* @post $none
*/
public boolean setCloudletLength(final long cloudletLength) {
if (cloudletLength <= 0) {
return false;
}
this.cloudletLength = cloudletLength;
return true;
}
/**设置服务等级【可以尝试】
* Sets the network service level for sending this cloudlet over a network.
*
* @param netServiceLevel determines the kind of service this cloudlet
* receives in the network (applicable to
* selected PacketScheduler class only)
*
* @return <code>true</code> if successful.
*
* @pre netServiceLevel >= 0
* @post $none
*/
public boolean setNetServiceLevel(final int netServiceLevel) {
boolean success = false;
if (netServiceLevel > 0) {
netToS = netServiceLevel;
success = true;
}
return success;
}
/**获取服务等级
* Gets the network service level for sending this cloudlet over a network.
*
* @return the network service level
*
* @pre $none
* @post $none
*/
public int getNetServiceLevel() {
return netToS;
}
/**获取云任务等待时间
* Gets the waiting time of this cloudlet executed on a resource.
*
* @return the waiting time
*
* @pre $none
* @post $none
*/
public double getWaitingTime() {
if (index == -1) {
return 0;
}
// use the latest resource submission time
final double subTime = resList.get(index).submissionTime;
return execStartTime - subTime;//执行开始时间-提交时间
}
/**任务类别(优先权)【尝试扩展使用】
* Sets the classType or priority of this Cloudlet for scheduling on a
* resource.
*
* @param classType classType of this Cloudlet
*
* @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise
*
* @pre classType > 0
* @post $none
*/
public boolean setClassType(final int classType) {
boolean success = false;
if (classType > 0) {
this.classType = classType;
success = true;
}
return success;
}
/**获取任务类别(优先权)
* Gets the classtype or priority of this Cloudlet for scheduling on a
* resource.
*
* @return classtype of this cloudlet
*
* @pre $none
* @post $none
*/
public int getClassType() {
return classType;
}
/**任务请求的PE
* Sets the number of PEs required to run this Cloudlet. <br>
* NOTE: The Cloudlet length is computed only for 1 Pe for simplicity. <br>
* For example, this Cloudlet has a length of 500 MI and requires 2 PEs.
* This means each Pe will execute 500 MI of this Cloudlet.每个PE都执行500 不是各分担250???
*
* @param pesNumber number of Pe
*
* @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise
*
* @pre numPE > 0
* @post $none
*/
public boolean setPesNumber(final int pesNumber) {
if (pesNumber > 0) {
this.pesNumber = pesNumber;
return true;
}
return false;
}
/**
* Gets the number of PEs required to run this Cloudlet.
*
* @return number of PEs
*
* @pre $none
* @post $none
*/
public int getPesNumber() {
return pesNumber;
}
/**云任务历史记录
* Gets the history of this Cloudlet. The layout of this history is in a
* readable table column with <tt>time</tt> and <tt>description</tt>
* as headers.
*
* @return a String containing the history of this Cloudlet object.
*
* @pre $none
* @post $result != null
*/
public String getCloudletHistory() {
String msg = null;
if (history == null) {
msg = "No history is recorded for Cloudlet #" + cloudletId;
} else {
msg = history.toString();
}
return msg;
}
/**任务已执行长度(适合 迁移任务或取消)
* Gets the length of this Cloudlet that has been executed so far
* from the latest CloudResource. This
* method is useful when trying to move this Cloudlet into different
* CloudResources or to cancel it.
*
* @return the length of a partially executed Cloudlet or the full Cloudlet
* length if it is completed
*
* @pre $none
* @post $result >= 0.0
*/
public long getCloudletFinishedSoFar() {
if (index == -1) {
return cloudletLength;
}
final long finish = resList.get(index).finishedSoFar;//resList 资源列表?
if (finish > cloudletLength) {
return cloudletLength;
}
return finish;
}
/**检验云任务是否执行完毕
* Checks whether this Cloudlet has finished execution or not.
*
* @return <tt>true</tt> if this Cloudlet has finished execution,
* <tt>false</tt> otherwise
*
* @pre $none
* @post $none
*/
public boolean isFinished() {
if (index == -1) {
return false;
}
boolean completed = false;
// if result is 0 or -ve then this Cloudlet has finished
final long finish = resList.get(index).finishedSoFar;
final long result = cloudletLength - finish;
if (result <= 0.0) {
completed = true;
}
return completed;
}
/**设置任务执行长度
* Sets the length of this Cloudlet that has been executed so far.
* This method is used by ResCloudlet class when an application
* is decided to cancel or to move this Cloudlet into different
* CloudResources.
*
* @param length length of this Cloudlet
*
* @see gridsim.AllocPolicy
* @see gridsim.ResCloudlet
* @pre length >= 0.0
* @post $none
*/
public void setCloudletFinishedSoFar(final long length) {
// if length is -ve then ignore
if (length < 0.0 || index < 0) {
return;
}
final Resource res = resList.get(index);
res.finishedSoFar = length;
if (record) {
write("Sets the length's finished so far to " + length);
}
}
/**设置用户ID
* Sets the user or owner ID of this Cloudlet. It is <tt>VERY</tt> important
* to set the user ID, otherwise this Cloudlet will not be executed in a
* CloudResource.
*
* @param id the user ID
*
* @pre id >= 0
* @post $none
*/
public void setUserId(final int id) {
userId = id;
if (record) {
write("Assigns the Cloudlet to " + CloudSim.getEntityName(id) + " (ID #" + id + ")");
}
}
/**
* Gets the user or owner ID of this Cloudlet.
*
* @return the user ID or <tt>-1</tt> if the user ID has not been set before
*
* @pre $none
* @post $result >= -1
*/
public int getUserId() {
return userId;
}
/**资源ID
* Gets the latest resource ID that processes this Cloudlet.
*
* @return the resource ID or <tt>-1</tt> if none
*
* @pre $none
* @post $result >= -1
*/
public int getResourceId() {
if (index == -1) {
return -1;
}
return resList.get(index).resourceId;
}
/**获取输入文件大小
* Gets the input file size of this Cloudlet <tt>BEFORE</tt>
* submitting to a CloudResource.
*
* @return the input file size of this Cloudlet
*
* @pre $none
* @post $result >= 1
*/
public long getCloudletFileSize() {
return cloudletFileSize;
}
/**获取输出文件大小
* Gets the output size of this Cloudlet <tt>AFTER</tt> submitting and
* executing to a CloudResource.
*
* @return the Cloudlet output file size
*
* @pre $none
* @post $result >= 1
*/
public long getCloudletOutputSize() {
return cloudletOutputSize;
}
/**通过资源实体设置资源参数
* Sets the resource parameters for which this Cloudlet is going to be
* executed. <br>
* NOTE: This method <tt>should</tt> be called only by a 【resource entity】,
* not the user or owner of this Cloudlet.
*
* @param resourceID the CloudResource ID
* @param cost the cost running this CloudResource per second
*
* @pre resourceID >= 0
* @pre cost > 0.0
* @post $none
*/
public void setResourceParameter(final int resourceID, final double cost) {
final Resource res = new Resource();
res.resourceId = resourceID;
res.costPerSec = cost;
res.resourceName = CloudSim.getEntityName(resourceID);
// add into a list if moving to a new grid resource
resList.add(res);
if (index == -1 && record) {
write("Allocates this Cloudlet to " + res.resourceName + " (ID #" + resourceID + ") with cost = $" + cost + "/sec");
} else if (record) {
final int id = resList.get(index).resourceId;
final String name = resList.get(index).resourceName;
write("Moves Cloudlet from " + name + " (ID #" + id + ") to " +
res.resourceName + " (ID #" + resourceID +
") with cost = $" + cost + "/sec");
}
index++; // initially, index = -1
}
/**设置 提交 到达 时间
* Sets the submission or arrival time of this Cloudlet into a CloudResource.
*
* @param clockTime 提交时间 the submission time
*
* @pre clockTime >= 0.0
* @post $none
*/
public void setSubmissionTime(final double clockTime) {
if (clockTime < 0.0 || index < 0) {
return;
}
final Resource res = resList.get(index);
res.submissionTime = clockTime;
if (record) {
write( "Sets the submission time to " + num.format(clockTime) );
}
}
/**
* Gets the submission or arrival time of this Cloudlet from
* the latest CloudResource.
*
* @return the submission time or <tt>0.0</tt> if none
*
* @pre $none
* @post $result >= 0.0
*/
public double getSubmissionTime() {
if (index == -1) {
return 0.0;
}
return resList.get(index).submissionTime;
}
/**设置执行开始时间 (取消 暂停 重启)
* Sets the execution start time of this Cloudlet inside a CloudResource.
* <b>NOTE:</b> With new functionalities, such as being able to cancel /
* to pause / to resume this Cloudlet, the execution start time only holds
* the latest one. Meaning, all previous execution start time are ignored.
*
* @param clockTime the latest execution start time
*
* @pre clockTime >= 0.0
* @post $none
*/
public void setExecStartTime(final double clockTime) {
execStartTime = clockTime;
if (record) {
write("Sets the execution start time to " + num.format(clockTime));
}
}
/**
* Gets the latest execution start time.
*
* @return the latest execution start time
*
* @pre $none
* @post $result >= 0.0
*/
public double getExecStartTime() {
return execStartTime;
}
/**云任务执行参数设置
* Sets this Cloudlet's execution parameters. These parameters are set by
* the CloudResource before departure or sending back to the original
* Cloudlet's owner.
*
* @param wallTime (云任务停留时间) the time of this Cloudlet resides in
* a CloudResource (from arrival time until
* departure time).
* @param actualTime (执行时间) the total execution time of this Cloudlet in a
* CloudResource.
*
* @pre wallTime >= 0.0
* @pre actualTime >= 0.0
* @post $none
*/
public void setExecParam(final double wallTime, final double actualTime) {
if (wallTime < 0.0 || actualTime < 0.0 || index < 0) {
return;
}
final Resource res = resList.get(index);
res.wallClockTime = wallTime;
res.actualCPUTime = actualTime;
if (record) {
write("Sets the wall clock time to "+ num.format(wallTime)+
" and the actual CPU time to " + num.format(actualTime));
}
}
/**云任务状态设置
* Sets the status code of this Cloudlet.
*
* @param newStatus the status code of this Cloudlet
*
* @throws Exception Invalid range of Cloudlet status
*
* @pre newStatus >= 0 && newStatus <= 8
* @post $none
*/
public void setCloudletStatus(final int newStatus) throws Exception {
// if the new status is same as current one, then ignore the rest
if (status == newStatus) {
return;
}
// throws an exception if the new status is outside the range
if (newStatus < Cloudlet.CREATED || newStatus > Cloudlet.FAILED_RESOURCE_UNAVAILABLE) {
throw new Exception("Cloudlet.setCloudletStatus() : Error - Invalid integer range for Cloudlet status.");
}//newStatus Cloudlet.CREATED 有可比性
if (newStatus == Cloudlet.SUCCESS) {
finishTime = CloudSim.clock();
}
if (record) {
write("Sets Cloudlet status from " + getCloudletStatusString() + " to " + Cloudlet.getStatusString(newStatus));
}
this.status = newStatus;
}
/**
* Gets the status code of this Cloudlet.
*
* @return the status code of this Cloudlet
*
* @pre $none
* @post $result >= 0
*/
public int getCloudletStatus() {
return status;
}
/**代表此刻云任务状态
* Gets the string representation of the current Cloudlet status code.
*
* @return the Cloudlet status code as a string or <tt>null</tt> if the
* status code is unknown
*
* @pre $none
* @post $none
*/
public String getCloudletStatusString() {
return Cloudlet.getStatusString(status);
}
/**
* Gets the string representation of the given Cloudlet status code.
*
* @param status the Cloudlet status code
*
* @return the Cloudlet status code as a string or <tt>null</tt> if the
* status code is unknown
*
* @pre $none
* @post $none
*/
public static String getStatusString(final int status) {
String statusString = null;
switch (status)
{
case Cloudlet.CREATED://创建
statusString = "Created";
break;
case Cloudlet.READY://准备
statusString = "Ready";
break;
case Cloudlet.INEXEC://执行
statusString = "InExec";
break;
case Cloudlet.SUCCESS://成功
statusString = "Success";
break;
case Cloudlet.QUEUED://排队等候
statusString = "Queued";
break;
case Cloudlet.FAILED://失败
statusString = "Failed";
break;
case Cloudlet.CANCELED://取消
statusString = "Canceled";
break;
case Cloudlet.PAUSED://暂停
statusString = "Paused";
break;
case Cloudlet.RESUMED://重启
statusString = "Resumed";
break;
case Cloudlet.FAILED_RESOURCE_UNAVAILABLE ://无可用资源
statusString = "Failed_resource_unavailable";
break;
default:
break;
}
return statusString;
}
/**获取云任务长度
* Gets the length of this Cloudlet.
*
* @return the length of this Cloudlet
*
* @pre $none
* @post $result >= 0.0
*/
public long getCloudletLength() {
return cloudletLength;
}
/**获取任务总长度
* Gets the total length (across all PEs) of this Cloudlet.
*
* @return the total length of this Cloudlet
*
* @pre $none
* @post $result >= 0.0
*/
public long getCloudletTotalLength() {
return getCloudletLength() * getPesNumber();//为什么乘以PE数
}
/**云资源上的执行费用
* Gets the cost running this Cloudlet in the latest CloudResource.
*
* @return the cost associated with running this Cloudlet
* or <tt>0.0</tt> if none
*
* @pre $none
* @post $result >= 0.0
*/
public double getCostPerSec() {
if (index == -1) {
return 0.0;
}
return resList.get(index).costPerSec;
}
/**云任务停留时间在最近的云资源上 (云资源 是指 虚拟机??)
* Gets the time of this Cloudlet resides in the latest CloudResource
* (from arrival time until departure time).
*
* @return the time of this Cloudlet resides in a CloudResource
*
* @pre $none
* @post $result >= 0.0
*/
public double getWallClockTime() {
if (index == -1) {
return 0.0;
}
return resList.get(index).wallClockTime;
}
/**执行云任务的所有资源名称
* Gets all the CloudResource names that executed this Cloudlet.
*
* @return an array of CloudResource names or <tt>null</tt> if it has none
*
* @pre $none
* @post $none
*/
public String[] getAllResourceName() {
final int size = resList.size();
String[] data = null;
if (size > 0) {
data = new String[size];
for (int i = 0; i < size; i++) {
data[i] = resList.get(i).resourceName;
}
}
return data;
}
/**执行云任务的所有资源ID号
* Gets all the CloudResource IDs that executed this Cloudlet.
*
* @return an array of CloudResource IDs or <tt>null</tt> if it has none
*
* @pre $none
* @post $none
*/
public int[] getAllResourceId() {
final int size = resList.size();
int[] data = null;
if (size > 0) {
data = new int[size];
for (int i = 0; i < size; i++) {
data[i] = resList.get(i).resourceId;
}
}
return data;
}
/**云任务执行的总时间
* Gets the total execution time of this Cloudlet in a given CloudResource ID.
*
* @param resId a CloudResource entity ID
*
* @return the total execution time of this Cloudlet in a CloudResource
* or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getActualCPUTime(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.actualCPUTime;
}
return 0.0;
}
/**指定的资源上运行云任务的费用
* Gets the cost running this Cloudlet in a given CloudResource ID.
*
* @param resId a CloudResource entity ID
*
* @return the cost associated with running this Cloudlet
* or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getCostPerSec(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.costPerSec;
}
return 0.0;
}
/**指定的资源上获取云任务已执行的长度 (取消 或 移动任务至不同的云资源)
* Gets the length of this Cloudlet that has been executed so far in a given
* CloudResource ID. This method is useful when trying to move this Cloudlet
* into different CloudResources or to cancel it.
*
* @param resId a CloudResource entity ID
*
* @return the length of a partially executed Cloudlet or the full Cloudlet
* length if it is completed or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public long getCloudletFinishedSoFar(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.finishedSoFar;
}
return 0;
}
/**指定的资源上获取云任务提交 到达时间
* Gets the submission or arrival time of this Cloudlet in the
* given CloudResource ID.
*
* @param resId a CloudResource entity ID
*
* @return the submission time or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getSubmissionTime(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.submissionTime;
}
return 0.0;
}
/**指定的资源上云任务停留的时间
* Gets the time of this Cloudlet resides in a given CloudResource ID
* (from arrival time until departure time).
*
* @param resId a CloudResource entity ID
*
* @return the time of this Cloudlet resides in the CloudResource
* or <tt>0.0</tt> if not found
*
* @pre resId >= 0
* @post $result >= 0.0
*/
public double getWallClockTime(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.wallClockTime;
}
return 0.0;
}
/**在ID基础上获取云资源名
* Gets the CloudResource name based on its ID.
*
* @param resId a CloudResource entity ID
*
* @return the CloudResource name or <tt>null</tt> if not found
*
* @pre resId >= 0
* @post $none
*/
public String getResourceName(final int resId) {
Resource resource = getResourceById(resId);
if (resource != null) {
return resource.resourceName;
}
return null;
}
/**由ID获取资源
* Gets the resource by id.
*
* @param resourceId the resource id
*
* @return the resource by id
*/
public Resource getResourceById(final int resourceId) {
for (Resource resource : resList) {
if (resource.resourceId == resourceId) {
return resource;
}
}
return null;
}
/**获取云任务在资源中完成时间
* Gets the finish time of this Cloudlet in a CloudResource.
*
* @return the finish or completion time of this Cloudlet or <tt>-1</tt> if
* not finished yet.
*
* @pre $none
* @post $result >= -1
*/
public double getFinishTime() {
return finishTime;
}
////////////////////////// PROTECTED METHODS //////////////////////////////
/**记录云任务特别交易历史
* Writes this particular history transaction of this Cloudlet into a log.
*
* @param str a history transaction of this Cloudlet
*
* @pre str != null
* @post $none
*/
protected void write(final String str) {
if (!record) {
return;
}
if (num == null || history == null) { // Creates the history or transactions of this Cloudlet
newline = System.getProperty("line.separator");
num = new DecimalFormat("#0.00#"); // with 3 decimal spaces
history = new StringBuffer(1000);
history.append("Time below denotes the simulation time.");
history.append( System.getProperty("line.separator") );
history.append("Time (sec) Description Cloudlet #"+cloudletId);
history.append( System.getProperty("line.separator") );
history.append("------------------------------------------");
history.append( System.getProperty("line.separator") );
history.append( num.format(CloudSim.clock()) );
history.append(" Creates Cloudlet ID #" + cloudletId);
history.append( System.getProperty("line.separator") );
}
history.append(num.format(CloudSim.clock()));
history.append(" " + str + newline);
}
/**云任务状态
* Get the status of the Cloudlet.
*
* @return status of the Cloudlet
*
* @pre $none
* @post $none
*/
public int getStatus(){
return getCloudletStatus();
}
/**获取任务ID
* Gets the ID of this Cloudlet.
*
* @return Cloudlet Id
*
* @pre $none
* @post $none
*/
public int getCloudletId() {
return this.cloudletId;
}
/**获取运行云任务的虚拟机ID
* Gets the ID of the VM that will run this Cloudlet.
*
* @return VM Id, -1 if the Cloudlet was not assigned to a VM
*
* @pre $none
* @post $none
*/
public int getVmId() {
return vmId;
}
/**设置虚拟机ID
* Sets the ID of the VM that will run this Cloudlet.
*
* @param vmId the vm id
*
* @pre id >= 0
* @post $none
*/
public void setVmId(final int vmId) {
this.vmId = vmId;
}
/**
* Returns the time the Cloudlet actually run.
*
* @return time in which the Cloudlet was running
*
* @pre $none
* @post $none
*/
public double getActualCPUTime(){
return getFinishTime() - getExecStartTime();
}
/**设置资源参数
* Sets the resource parameters for which this Cloudlet is going to be
* executed. <br>
* NOTE: This method <tt>should</tt> be called only by a resource entity,
* not the user or owner of this Cloudlet.
*
* @param resourceID the CloudResource ID
* @param costPerCPU the cost running this Cloudlet per second
* @param costPerBw the cost of data transfer to this PowerDatacenter
*
* @pre resourceID >= 0
* @pre cost > 0.0
* @post $none
*/
public void setResourceParameter(final int resourceID, final double costPerCPU, final double costPerBw) {
setResourceParameter(resourceID, costPerCPU);
this.costPerBw = costPerBw;
this.accumulatedBwCost = costPerBw * getCloudletFileSize();
}
/**执行云任务总费用
* Gets the total cost of processing or executing this Cloudlet
* <tt>Processing Cost = input data transfer + processing cost + output transfer cost</tt>.
*
* @return the total cost of processing Cloudlet
*
* @pre $none
* @post $result >= 0.0
*/
public double getProcessingCost() {
//cloudlet cost: execution cost...
//double cost = getProcessingCost();//处理费用
double cost = 0;
//...plus input data transfer cost...//数据传送费用
cost += this.accumulatedBwCost;
//...plus output cost输出费用
cost += this.costPerBw * getCloudletOutputSize();
return cost;
}
// Data cloudlet
/**
* Gets the required files.
*
* @return the required files
*/
public List<String> getRequiredFiles() {
return requiredFiles;
}
/**设置请求文件 (是指什么呢???)
* Sets the required files.
*
* @param requiredFiles the new required files
*/
protected void setRequiredFiles(final List<String> requiredFiles) {
this.requiredFiles = requiredFiles;
}
/**
* Adds the required filename to the list.
*
* @param fileName the required filename
*
* @return <tt>true</tt> if succesful, <tt>false</tt> otherwise
*/
public boolean addRequiredFile(final String fileName) {
// if the list is empty
if (getRequiredFiles() == null) {
setRequiredFiles(new LinkedList<String>());
}
// then check whether filename already exists or not
boolean result = false;
for (int i = 0; i < getRequiredFiles().size(); i++) {
final String temp = getRequiredFiles().get(i);
if (temp.equals(fileName)) {
result = true;
break;
}
}
if (!result) {
getRequiredFiles().add(fileName);
}
return result;
}
/**删除给出的问就爱你名
* Deletes the given filename from the list.
*
* @param filename the given filename to be deleted
*
* @return <tt>true</tt> if succesful, <tt>false</tt> otherwise
*/
public boolean deleteRequiredFile(final String filename) {
boolean result = false;
if (getRequiredFiles() == null) {
return result;
}
for (int i = 0; i < getRequiredFiles().size(); i++) {
final String temp = getRequiredFiles().get(i);
if (temp.equals(filename)) {
getRequiredFiles().remove(i);
result = true;
break;
}
}
return result;
}
/**
* Checks whether this cloudlet requires any files or not.
*
* @return <tt>true</tt> if required, <tt>false</tt> otherwise
*/
public boolean requiresFiles() {
boolean result = false;
if (getRequiredFiles() != null && getRequiredFiles().size() > 0) {
result = true;
}
return result;
}
//【设置 cpu ram bw 利用率】
/**
* Gets the utilization model cpu.
*
* @return the utilization model cpu
*/
public UtilizationModel getUtilizationModelCpu() {
return utilizationModelCpu;
}
/**
* Sets the utilization model cpu.
*
* @param utilizationModelCpu the new utilization model cpu
*/
public void setUtilizationModelCpu(final UtilizationModel utilizationModelCpu) {
this.utilizationModelCpu = utilizationModelCpu;
}
/**
* Gets the utilization model ram.
*
* @return the utilization model ram
*/
public UtilizationModel getUtilizationModelRam() {
return utilizationModelRam;
}
/**
* Sets the utilization model ram.
*
* @param utilizationModelRam the new utilization model ram
*/
public void setUtilizationModelRam(final UtilizationModel utilizationModelRam) {
this.utilizationModelRam = utilizationModelRam;
}
/**
* Gets the utilization model bw.
*
* @return the utilization model bw
*/
public UtilizationModel getUtilizationModelBw() {
return utilizationModelBw;
}
/**
* Sets the utilization model bw.
*
* @param utilizationModelBw the new utilization model bw
*/
public void setUtilizationModelBw(final UtilizationModel utilizationModelBw) {
this.utilizationModelBw = utilizationModelBw;
}
/**
* Gets the total utilization of cpu.
*
* @param time the time
*
* @return the utilization of cpu
*/
public double getUtilizationOfCpu(final double time) {
return getUtilizationModelCpu().getUtilization(time);
}
/**
* Gets the utilization of memory.
*
* @param time the time
*
* @return the utilization of memory
*/
public double getUtilizationOfRam(final double time) {
return getUtilizationModelRam().getUtilization(time);
}
/**
* Gets the utilization of bw.
*
* @param time the time
*
* @return the utilization of bw
*/
public double getUtilizationOfBw(final double time) {
return getUtilizationModelBw().getUtilization(time);
}
}
CloudSim源代码学习——任务单元(Cloudlet)的更多相关文章
- CloudSim源代码学习——服务代理商(DatacenterBroker)
DatacenterBroker.java文件如下: (其中,相关语句已经做好标注) /* * Title: CloudSim Toolkit * Description: CloudSim (Clo ...
- CloudSim源代码学习——虚拟机(VM)
package org.cloudbus.cloudsim; import java.util.ArrayList;//This class provides methods to manipulat ...
- CloudSim源代码学习——云数据中心(Datacenter)
package org.cloudbus.cloudsim; import java.text.DecimalFormat;//十进制 import java.util.ArrayList; impo ...
- struts2源代码学习之初始化(一)
看struts2源代码已有一段时日,从今天開始,就做一个总结吧. 首先,先看看怎么调试struts2源代码吧,主要是下面步骤: 使用Myeclipse创建一个webproject 导入struts2须 ...
- [Java] LinkedList / Queue - 源代码学习笔记
简单地画了下 LinkedList 的继承关系,如下图.只是画了关注的部分,并不是完整的关系图.本博文涉及的是 Queue, Deque, LinkedList 的源代码阅读笔记.关于 List 接口 ...
- 开源中国安卓client源代码学习(一) 渐变启动界面
开源中国安卓client源代码学习(一) 渐变启动界面 准备学习安卓开发, 看到网上有人推荐开源中国安卓client的源代码, 说里面包括了大部分技术, 于是准备好好研究研究. 特开通此系列博客来记录 ...
- 读Flask源代码学习Python--config原理
读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因 莫名其妙在第一份工作中使用了从来没有接 ...
- nginx源代码学习资源(不断更新)
nginx源代码学习是一个痛苦又快乐的过程,以下列出了一些nginx的学习资源. 首先要做的当然是下载一份nginx源代码,能够从nginx官方站点下载一份最新的. 看了nginx源代码,发现这是一份 ...
- JDK源代码学习系列07----Stack
JDK源代码学习系列07----Stack 1.Stack源代码很easy ...
随机推荐
- 设计模式总结(Java)—— 单例模式
1. 定义 为了确保一个类有且仅有一个实例,而且自行实例化并向整个系统提供这个实例. 2. 使用场景 确保某个类有且只有一个对象的场景,避免产生多个对象消耗过多的资源,或者某种类型的对象只应该有且只有 ...
- [Swift]正则表达式工具类
正则表达式工具类 import Foundation //基于NSRegularExpression api 的正则处理工具类 public struct Regex { private let re ...
- Swift5 语言指南(十五) 继承
类可以从另一个类继承方法,属性和其他特性.当一个类继承自另一个类时,继承类称为子类,它继承的类称为其超类.继承是一种基本行为,它将类与Swift中的其他类型区分开来. Swift中的类可以调用和访问属 ...
- PHP、JS、Python,数据库 获取今天是星期几了?[开发篇]
额,这个看起来是一个好简单的问题,但是真正到自己去一行行写的时候,又给忘了,妈蛋.有空就看看吧.今天是星期几?下面就来看看几种不同语言的实现吧! PHP语言 输出当前时间: echo date('Y- ...
- C语言中volatile的作用和使用方法
在程序设计中,尤其是在C语言.C++.C#和Java语言中,使用volatile关键字声明的变量或对象通常具有与优化.多线程相关的特殊属性. 通常,volatile关键字用来阻止(伪)编译器认为的无法 ...
- Python - 集成开发环境Pycharm的使用方法和技巧
PyCharm HomePage:PyCharm 我的Pycharm,我做主 Getting Started with PyCharm Pycharm使用技巧 Documentation & ...
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
要求:根据中序和后序遍历序列构建一棵二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int ...
- Django --ORM常用的字段和参数 多对多创建形式
1 ORM字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. IntegerField 一个整数类型 ...
- 单区域OSPF路由协议实现网络区域互通
1.什么是OSPF协议? OSPF协议的全程是开放式最短路径优先协议,协议采用链路状态协议算法(LS协议) 2.OSPF vs RIP RIP路由协议是距离矢量路由选择协议,收敛速度慢,如果在一些大型 ...
- Docker容器绑定外部IP和端口
Docker允许通过外部访问容器或者容器之间互联的方式来提供网络服务. 以下操作通过myfirstapp镜像模拟,如何制作myfirstapp镜像请点击此处. 1.外部访问容器容器启动之后,容器中可以 ...