📄 test.java
字号:
/************************************************************************
* Author: Nithiapidary Muthuvelu, Junyang Liu and Nay Lin Soe
* Date: 30 June 2004
*
* The objective of this program is to group a number of Gridlets with
* small MIPS ratings into a Gridlet group according to the MIPS
* rating of a specific Grid resource, and send the Gridlet group to that
* Grid resource for teh execution purpose.
*
* This program is a modified version of Example5.java of GridSim
* (Grid Simulation) Toolkit version 2.2.
*************************************************************************/
import java.util.*;
import java.text.*;
import gridsim.*;
/****************************************************************************
Test class creates Gridlets and sends them to three grid resource entities
****************************************************************************/
class Test extends GridSim
{
public Integer ID_;
private String name_;
private GridletList list_;
private GridletList receiveList_;
private int totalResource_;
private int numOfGridlets;
private double averageMI;
private int deviatePercentage;
private int granTime;
private int overhead;
private boolean grouping;
private String output;
private DecimalFormat df;
/*************************************************
Allocates a new Test object
**************************************************/
Test(String name, double baud_rate, int total_resource,
int numOfGridlets, double averageMI, int deviatePercentage,
int granTime, int overhead, boolean grouping) throws Exception
{
super(name, baud_rate);
this.name_ = name;
this.totalResource_ = total_resource;
this.receiveList_ = new GridletList();
// user input parameters
this.numOfGridlets = numOfGridlets;
this.averageMI = averageMI;
this.deviatePercentage = deviatePercentage;
this.granTime = granTime;
this.overhead = overhead;
this.grouping = grouping;
// get an ID for this entity
this.ID_ = new Integer(getEntityId(name));
df = new DecimalFormat("0.00");
output = "";
output += "\nCreating a grid user entity with name = " + name + ", and id = " + this.ID_ + "\n";
// creates a list of Gridlets for this grid user
this.list_ = createGridlet(this.ID_.intValue());
output +="\nCreating " + this.list_.size() + " Gridlets\n\n";
}
public String getOutput()
{
return output;
}
/********************************************************************
The core method that handles communications among GridSim entities
*******************************************************************/
public void body()
{
int i;
int resourceID[] = new int[this.totalResource_];
double resourceCost[] = new double[this.totalResource_];
String resourceName[] = new String[this.totalResource_];
double resourceMIPS[] = new double[this.totalResource_];
LinkedList resList;
ResourceCharacteristics resChar;
// Waiting to get list of Grid resources
while (true)
{
// pause for a while to wait for the Grid resources to finish registering to GIS
super.gridSimHold(1.0); // hold by 1 second
resList = getGridResourceList();
if (resList.size() == this.totalResource_)
break;
else
output += "\nWaiting to get list of resources ..... \n";
}
output += "\n================== Receiving Characteristics of the Selected Grid Resources ==================\n\n";
// a loop to get all the available Grid resources
for (i=0; i < this.totalResource_; i++)
{
// resource list contains list of resource IDs
resourceID[i] = ( (Integer)resList.get(i) ).intValue();
// requests to resource entity to send its characteristics
send(resourceID[i], GridSimTags.SCHEDULE_NOW,GridSimTags.RESOURCE_CHARACTERISTICS, this.ID_);
// waiting to get a resource characteristics
resChar = (ResourceCharacteristics) receiveEventObject();
resourceName[i] = resChar.getResourceName();
resourceCost[i] = resChar.getCostPerSec();
resourceMIPS[i] = resChar.getMIPSRating();
output += "Resource " + resourceName[i] + ", with id = " + resourceID[i]
+ ", cost = " + resourceCost[i] + ", MIPS = " + resourceMIPS[i] + "\n";
// record this event into "stat.txt" file
recordStatistics("\"Received ResourceCharacteristics from " + resourceName[i] + ", with id = " + resourceID[i]
+ ", cost = " + resourceCost[i] + ", MIPS = " + resourceMIPS[i] +"\"", "" );
}
output += "\n=====================================START SIMULATION===============================================\n";
// variable declarations
String info;
int m=0, n=0, p=0, q=0;
int id=0;
int checkPoint=0, processedGridlet=0;
double Time1, Time2, Total_Time;
double total_length, MIPS_Rate_Machine;
Gridlet gridlet, groupedGridlet;
GridletList list2 = new GridletList();
LinkedList TargetResource = new LinkedList();
LinkedList FGridlet = new LinkedList();
LinkedList LGridlet = new LinkedList();
// start timer
Time1 = clock();
// Gridlets are grouped together according to resource MIPS rating and sent to that resource
if(grouping == true)
{
// a loop to get a Grid resouce's MIPS rating and group the Gridlets according to that rating
for(i = 0; i < this.list_.size();) // loop to get the Gridlet
{
// check point to detect whether all the gridlets are processed within the given granularity time
checkPoint++;
if(checkPoint == 2)
{
processedGridlet = i-1;
}
for(n=0; n < (this.totalResource_) && (i < this.list_.size()); n++, i++) // loop to get the resource
{
p = i;
total_length = 0.0;
MIPS_Rate_Machine = 0.0;
id = n;
// get MIPS of a resource and multiply it with the granularity time
MIPS_Rate_Machine = resourceMIPS[id]*granTime;
output += "\nThe supported MI for granularity time " + granTime + " second(s), by Resource "
+ resourceName[id] + " (ID:" + resourceID[id] + ")" + " is " + MIPS_Rate_Machine;
// get one Gridlet
gridlet = (Gridlet) this.list_.get(i);
// if the resouce MIPS is less than Gridlet's MI, then assign the Gridlet to that resource
if( MIPS_Rate_Machine <= gridlet.getGridletLength())
{
// assign the indidual Gridlet to the resource without any grouping process
total_length = gridlet.getGridletLength();
}
// else, group the Gridlets based on resource MIPS
else
{
while((total_length <= MIPS_Rate_Machine) && (i< this.list_.size()))
{
gridlet = (Gridlet) this.list_.get(i);
total_length = total_length + gridlet.getGridletLength();
i++;
}
i=i-1;
// if the difference between total MI (total length) and resource MIPS is more than 50MI,
// then deduct the last Gridlet from the group
if((total_length - MIPS_Rate_Machine) > 50)
{
gridlet = (Gridlet) this.list_.get(i);
output += "Deducting the last Gridlet's length from the total length \n";
total_length = total_length - gridlet.getGridletLength();
i = i-1;
}
}
// create the GroupGridlets
groupedGridlet = new Gridlet(m,total_length,1000,1000);
// set Gridlet user for the GroupGridlets
groupedGridlet.setUserID(this.ID_.intValue());
// add the GroupGridlets into a list
list2.add(groupedGridlet);
// set the resource ID for each GroupGridlets
TargetResource.add(new Integer(id));
q=i-p+1;
output +="\nGroupGridlet " + m + "; composed of " + q + " Gridlets, (Gridlet " + p + " to Gridlet " + i + "); Total Length: "
+ total_length + "; Resource ID: " + resourceID[id] + "\n";
FGridlet.add(new Integer(p));
LGridlet.add(new Integer(i));
// record this event into "stat.txt" file
recordStatistics("\"GroupGridlet " + m + ", Total Gridlet (" + p + " to " + i + "): " + q + ", Total Length: "
+ total_length + ", Resource ID: " + resourceID[id] + "\"", "");
m++;
}
}
// send GroupGridlets to the Grid resources
output += "\n======================== Sending Gridlets to Grid Resources ========================\n";
i=0;
while(i < list2.size())
{
// get the assigned resource ID
id = ( (Integer)TargetResource.get(i) ).intValue();
// get the GroupGridlet
gridlet = (Gridlet) list2.get(i);
info = "groupedGridlet " + gridlet.getGridletID();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -