CC Cloudsim

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lab Program: 12

SIMULATE A CLOUD SCENARIO USING CLOUDSIM AND RUN A SCHEDULING ALGORITHM


THAT IS NOT PRESENT IN CLOUDSIM.

AIM:

To simulate cloud scenarios using cloudsim and run a scheduling algorithm that is not
present in cloudsim.

PROCEDURE:

Go to this website and download the cloudsim simulator


“http://www.cloudbus.org/cloudsim/”.

1) This link would open a github account and then open the required version.
2) Download the zip file and extract it.
3) Open the netbeans create a new project
4) Choose java -> choose java application
5) Give name and then click finish
6) Click on libraries and add a library of cloudsim to the application.
7) Type the program you want to run.
8) Run the application and see the output in the output area.

PROGRAM:

package fc;

import org.cloudbus.cloudsim.*;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.LinkedList;

public class fcfc_Scheduler {

private static List<Cloudlet> cloudletList;


private static List<Vm> vmList;
private static Datacenter[] datacenter;
private static double[][] commMatrix;
private static double[][] execMatrix;

private static List<Vm> createVM(int userId, int vms) {


LinkedList<Vm> list = new LinkedList<Vm>();

int mips = 250; long bw = 1000;


int pesNumber = 1; // number of cpus String vmm =
"Xen"; // VMM name
Vm[] vm = new Vm[vms]; for (int i = 0; i <
vms; i++) {
vm[i] = new Vm(datacenter[i].getId(), userId, mips, pesNumber, ram, bw, size, vmm, new
CloudletSchedulerSpaceShared());
list.add(vm[i]);
}

return list;
}

private static List<Cloudlet> createCloudlet(int userId, int cloudlets, int idShift) {


LinkedList<Cloudlet> list = new LinkedList<Cloudlet>();

long fileSize = 300;


long outputSize = 300;
int pesNumber = 1;
UtilizationModel utilizationModel = new UtilizationModelFull();

Cloudlet[] cloudlet = new Cloudlet[cloudlets];


for (int i = 0; i < cloudlets; i++) {
int dcId = (int) (Math.random() * Constants.NO_OF_DATA_CENTERS);
long length = (long) (1e3 * (commMatrix[i][dcId] + execMatrix[i][dcId]));

cloudlet[i] = new Cloudlet(idShift + i, length, pesNumber, fileSize, outputSize, utilizationModel,


utilizationModel, utilizationModel);

cloudlet[i].setUserId(userId); cloudlet[i].setVmId(dcId + 2);


list.add(cloudlet[i]);
}

return list;
}

public static void main(String[] args) { Log.printLine("Starting


FCFS Scheduler...");
new GenerateMatrices();
execMatrix = GenerateMatrices.getExecMatrix();
commMatrix = GenerateMatrices.getCommMatrix();

try {
int num_user = 1;
Calendar calendar = Calendar.getInstance(); boolean trace_flag =
false;
CloudSim.init(num_user, calendar, trace_flag);

datacenter = new Datacenter[Constants.NO_OF_DATA_CENTERS];


for (int i = 0; i < Constants.NO_OF_DATA_CENTERS; i++) {
datacenter[i] = DatacenterCreator.createDatacenter("Datacenter_" + i);
}

FCFSDatacenterBroker broker = createBroker("Broker_0");


int brokerId = broker.getId();
vmList = createVM(brokerId, Constants.NO_OF_DATA_CENTERS);
cloudletList = createCloudlet(brokerId, Constants.NO_OF_TASKS, 0);
broker.submitVmList(vmList);
broker.submitCloudletList(cloudletList);

CloudSim.startSimulation();

List<Cloudlet> newList = broker.getCloudletReceivedList();


newList.addAll(globalBroker.getBroker().getCloudletReceivedList());

CloudSim.stopSimulation();

printCloudletList(newList);

Log.printLine(FCFS_Scheduler.class.getName() + " finished!");


} catch (Exception e) { e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
}

private static FCFSDatacenterBroker createBroker(String name) throws Exception { return new


FCFSDatacenterBroker(name);
}

private static void printCloudletList(List<Cloudlet> list) { int size = list.size();


Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + "Data center ID" + indent + "VM ID" +
indent + "Time" + indent + "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
dft.setMinimumIntegerDigits(2);
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + dft.format(cloudlet.getCloudletId()) + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
Log.print("SUCCESS");
Log.printLine(indent + indent + dft.format(cloudlet.getResourceId()) + indent + indent
+ indent + dft.format(cloudlet.getVmId()) + indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent
+ indent + dft.format(cloudlet.getExecStartTime()) + indent + indent + indent +
dft.format(cloudlet.getFinishTime()));
}

double makespan = calcMakespan(list); Log.printLine("Makespan using FCFS:


" + makespan);
}

private static double calcMakespan(List<Cloudlet> list) { double makespan = 0;


double[] dcWorkingTime = new double[Constants.NO_OF_DATA_CENTERS];
for (int i = 0; i < Constants.NO_OF_TASKS; i++) {
int dcId = list.get(i).getVmId() % Constants.NO_OF_DATA_CENTERS;
if (dcWorkingTime[dcId] != 0)
dcWorkingTime[dcId]--;
dcWorkingTime[dcId] += execMatrix[i][dcId] + commMatrix[i][dcId];
makespan = Math.max(makespan, dcWorkingTime[dcId]);
}

return makespan;
}

FCFSDatacenterBroker.java

package fc;

import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.DatacenterBroker;

import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.core.CloudSim; import
org.cloudbus.cloudsim.core.SimEvent;

import java.util.ArrayList;

public class FCFSDatacenterBroker extends DatacenterBroker { public

FCFSDatacenterBroker(String name) throws Exception {


super(name);
}

public void scheduleTaskstoVms() { ArrayList<Cloudlet> clist = new ArrayList<>();


for (Cloudlet cloudlet : getCloudletSubmittedList()) {
clist.add(cloudlet);
}

setCloudletReceivedList(clist);
}

@Override
protected void processCloudletReturn(SimEvent ev) { Cloudlet cloudlet
= (Cloudlet) ev.getData(); getCloudletReceivedList().add(cloudlet);
Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloudlet " + cloudlet.getCloudletId()
+ " received");
cloudletsSubmitted--;

if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { scheduleTaskstoVms();


cloudletExecution(cloudlet);
}

protected void cloudletExecution(Cloudlet cloudlet) {

if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { Log.printLine(CloudSim.clock() + ": " +


getName() + ": All Cloudlets executed.
Finishing...");
clearDatacenters(); finishExecution();
} else {
if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { clearDatacenters();
createVmsInDatacenter(0);
}

}
OUTPUT:

RESULT:

Thus,simulated a cloud scenario using CloudSim and ran a scheduling algorithm that is not present in
CloudSim.

You might also like