Condition for Allocate a Task Group to a Single VM
Description: To allocate a task group to a single VM in CloudSim, assign the same VM ID to all tasks using the setVmId method of the Cloudlet class. Add the VM to the broker's VM list, iterate over the tasks to set the VM ID, and submit the task list to the broker. This ensures all tasks in the group are executed on the specified VM during the simulation.
Sample Code
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.*;
import java.text.DecimalFormat;
import java.util.*;
/**
* Task Group Allocation to a Single VM Example
*/
public class TaskGroupAllocationSingleVM {
private static List cloudletList;
private static List vmlist;
public static void main(String[] args) {
Log.printLine("Starting Task Group Allocation Example...");
try {
// Initialize CloudSim
int numUser = 1; // number of cloud users
Calendar calendar = Calendar.getInstance(); // Current date and time
boolean traceFlag = false; // trace events
CloudSim.init(numUser, calendar, traceFlag);
// Create Datacenter
Datacenter datacenter0 = createDatacenter("Datacenter_0");
// Create Broker
DatacenterBroker broker = createBroker();
int brokerId = broker.getId();
// Create Virtual Machines (VMs)
vmlist = new ArrayList<>();
int vmId = 0;
int mips = 1000;
int ram = 2048; // VM memory (MB)
long bw = 10000; // Bandwidth (MBps)
long size = 10000; // VM storage (MB)
int pesNumber = 1; // Number of CPUs
String vmm = "Xen"; // VM Monitor
// Create a single VM
Vm vm1 = new Vm(vmId, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
vmlist.add(vm1);
// Submit the VM list to the broker
broker.submitVmList(vmlist);
// Create Cloudlets (Tasks)
cloudletList = new ArrayList<>();
int cloudletId = 0;
long length = 400000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
// Create a group of 5 cloudlets
for (int i = 0; i < 5; i++) {
Cloudlet cloudlet = new Cloudlet(cloudletId++, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet.setUserId(brokerId);
cloudlet.setVmId(vm1.getId()); // Assign all cloudlets to VM1
cloudletList.add(cloudlet);
}
// Submit the cloudlet list to the broker
broker.submitCloudletList(cloudletList);
// Start the simulation
CloudSim.startSimulation();
// Retrieve results
List newList = broker.getCloudletReceivedList();
printCloudletList(newList);
CloudSim.stopSimulation();
Log.printLine("Task Group Allocation Example finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("An error occurred.");
}
}
/**
* Creates a Datacenter with a single host.
*/
private static Datacenter createDatacenter(String name) {
List hostList = new ArrayList<>();
List peList = new ArrayList<>();
int mips = 1000;
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // Processing Element (PE)
int hostId = 0;
int ram = 20480; // Host memory (MB)
long storage = 1000000; // Host storage (MB)
int bw = 100000; // Bandwidth (MBps)
hostList.add(new Host(hostId, new RamProvisionerSimple(ram), new BwProvisionerSimple(bw),
storage, peList, new VmSchedulerTimeShared(peList)));
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
double timeZone = 10.0; // GMT+10
double cost = 3.0; // Cost per second
double costPerMem = 0.05; // Cost per MB of memory
double costPerStorage = 0.001; // Cost per MB of storage
double costPerBw = 0.0; // Cost per MB of bandwidth
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, timeZone, cost, costPerMem, costPerStorage, costPerBw);
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), new LinkedList<>(), 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
/**
* Creates a broker.
*/
private static DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
}
return broker;
}
/**
* Prints the cloudlet list.
*/
private static void printCloudletList(List list) {
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (Cloudlet cloudlet : list) {
Log.printLine(cloudlet.getCloudletId() + indent
+ (cloudlet.getStatus() == Cloudlet.SUCCESS ? "SUCCESS" : "FAILED") + indent
+ cloudlet.getVmId() + indent
+ dft.format(cloudlet.getActualCPUTime()) + indent
+ dft.format(cloudlet.getExecStartTime()) + indent
+ dft.format(cloudlet.getFinishTime()));
}
}
}