if (vm.getId() == id) { return vm;
/** * Gets a {@link Vm} with a given id and owned by a given user. * * @param vmList list of existing VMs * @param id ID of required VM * @param userId the user ID of the VM's owner * @return Vm with the given ID, $null if not found * @pre $none * @post $none */ public static <T extends Vm> T getByIdAndUserId(List<T> vmList, int id, int userId) { for (T vm : vmList) { if (vm.getId() == id && vm.getUserId() == userId) { return vm; } } return null; }
/** * Gets a VM by its id and user. * * @param vmId the vm id * @param userId ID of VM's owner * @return the virtual machine object, $null if not found * @pre $none * @post $none */ public Vm getVm(int vmId, int userId) { for (Vm vm : getVmList()) { if (vm.getId() == vmId && vm.getUserId() == userId) { return vm; } } return null; }
/** * Destroy all virtual machines running in datacenters. * * @pre $none * @post $none */ protected void clearDatacenters() { for (Vm vm : getVmsCreatedList()) { Log.printConcatLine(CloudSim.clock(), ": " + getName(), ": Destroying VM #", vm.getId()); sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.VM_DESTROY, vm); } getVmsCreatedList().clear(); }
/** * Sends request to destroy all VMs running on the datacenter. * * @pre $none * @post $none /** Destroy the virtual machines running in datacenters. * * @pre $none * @post $none */ protected void clearDatacenters() { for (Vm vm : getVmsCreatedList()) { Log.printConcatLine(CloudSim.clock(), ": ", getName(), ": Destroying VM #", vm.getId()); sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.VM_DESTROY, vm); } getVmsCreatedList().clear(); }
/** * Gets a new vm placement considering the list of VM to migrate. * * @param vmsToMigrate the list of VMs to migrate * @param excludedHosts the list of hosts that aren't selected as destination hosts * @return the new vm placement map */ protected List<Map<String, Object>> getNewVmPlacement( List<? extends Vm> vmsToMigrate, Set<? extends Host> excludedHosts) { List<Map<String, Object>> migrationMap = new LinkedList<Map<String, Object>>(); PowerVmList.sortByCpuUtilization(vmsToMigrate); for (Vm vm : vmsToMigrate) { PowerHost allocatedHost = findHostForVm(vm, excludedHosts); if (allocatedHost != null) { allocatedHost.vmCreate(vm); Log.printConcatLine("VM #", vm.getId(), " allocated to host #", allocatedHost.getId()); Map<String, Object> migrate = new HashMap<String, Object>(); migrate.put("vm", vm); migrate.put("host", allocatedHost); migrationMap.add(migrate); } } return migrationMap; }
if (allocatedHost != null) { allocatedHost.vmCreate(vm); Log.printConcatLine("VM #", vm.getId(), " allocated to host #", allocatedHost.getId());
@Override public boolean allocateHostForVm(Vm vm, Host host) { if (host == null) { Log.formatLine("%.2f: No suitable host found for VM #" + vm.getId() + "\n", CloudSim.clock()); return false; } if (host.vmCreate(vm)) { // if vm has been succesfully created in the host getVmTable().put(vm.getUid(), host); Log.formatLine( "%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(), CloudSim.clock()); return true; } Log.formatLine( "%.2f: Creation of VM #" + vm.getId() + " on the host #" + host.getId() + " failed\n", CloudSim.clock()); return false; }
/** * Creates the given VM within the NetworkDatacenter. * It can be directly accessed by Datacenter Broker which manages allocation of Cloudlets. * * @param vm * @return true if the VW was created successfully, false otherwise */ public boolean processVmCreateNetwork(Vm vm) { boolean result = getVmAllocationPolicy().allocateHostForVm(vm); if (result) { VmToSwitchid.put(vm.getId(), ((NetworkHost) vm.getHost()).sw.getId()); VmtoHostlist.put(vm.getId(), vm.getHost().getId()); System.out.println(vm.getId() + " VM is created on " + vm.getHost().getId()); getVmList().add(vm); vm.updateVmProcessing(CloudSim.clock(), getVmAllocationPolicy().getHost(vm).getVmScheduler() .getAllocatedMipsForVm(vm)); } return result; }
/** * Creates virtual machines in the datacenters managed by this broker. * * @param datacenterIdList the list of datacenters managed by this broker. * @since 1.0 */ protected void createVmsInDatacenter(List<Integer> datacenterIdList) { List<Vm> auxList = new ArrayList<Vm>(); for(Vm vm : getVmList()) { auxList.add(vm); } int requestedVms = 0; for (Integer datacenterId : datacenterIdList) { String datacenterName = CloudSim.getEntityName(datacenterId); for (Vm vm : auxList) { if (!getVmsToDatacentersMap().containsKey(vm.getId())) { Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() + " in " + datacenterName); sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); requestedVms++; auxList.remove(vm); break; } } getDatacenterRequestedIdsList().add(datacenterId); setVmsRequested(requestedVms); } setVmsAcks(0); }
/** * Restore VM allocation from the allocation history. * @see #savedAllocation */ protected void restoreAllocation() { for (Host host : getHostList()) { host.vmDestroyAll(); host.reallocateMigratingInVms(); } for (Map<String, Object> map : getSavedAllocation()) { Vm vm = (Vm) map.get("vm"); PowerHost host = (PowerHost) map.get("host"); if (!host.vmCreate(vm)) { Log.printConcatLine("Couldn't restore VM #", vm.getId(), " on host #", host.getId()); System.exit(0); } getVmTable().put(vm.getUid(), host); } }
@Override public boolean allocateHostForVm(Vm vm, Host host) { if (host.vmCreate(vm)) { // if vm has been succesfully created in the host getVmTable().put(vm.getUid(), host); int requiredPes = vm.getNumberOfPes(); int idx = getHostList().indexOf(host); getUsedPes().put(vm.getUid(), requiredPes); getFreePes().set(idx, getFreePes().get(idx) - requiredPes); Log.formatLine( "%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(), CloudSim.clock()); return true; } return false; } }
@Override public boolean allocateHostForVm(Vm vm, Host host) { if (host.vmCreate(vm)) { // if vm has been succesfully created in the host getVmTable().put(vm.getUid(), host); int requiredPes = vm.getNumberOfPes(); int idx = getHostList().indexOf(host); getUsedPes().put(vm.getUid(), requiredPes); getFreePes().set(idx, getFreePes().get(idx) - requiredPes); Log.formatLine( "%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(), CloudSim.clock()); return true; } return false; } }
/** * Create the submitted virtual machines in a datacenter. * * @param datacenterId Id of the chosen Datacenter * @pre $none * @post $none * @see #submitVmList(java.util.List) */ protected void createVmsInDatacenter(int datacenterId) { // send as much vms as possible for this datacenter before trying the next one int requestedVms = 0; String datacenterName = CloudSim.getEntityName(datacenterId); for (Vm vm : getVmList()) { if (!getVmsToDatacentersMap().containsKey(vm.getId())) { Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() + " in " + datacenterName); sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); requestedVms++; } } getDatacenterRequestedIdsList().add(datacenterId); setVmsRequested(requestedVms); setVmsAcks(0); }
int vmId = vm.getId();
/** * Allocates a host for a given VM. * * @param vm a virtual machine. * * @return <code>true</code> if the host could be allocated; * <code>false</code> otherwise. */ @Override public boolean allocateHostForVm(Vm vm) { PowerHost allocatedHost = findHostForVm(vm); if (allocatedHost != null && allocatedHost.vmCreate(vm)) { //if vm has been succesfully created in the host getVmTable().put(vm.getUid(), allocatedHost); if (!Log.isDisabled()) { Log.print(String.format("%.2f: VM #" + vm.getId() + " has been allocated to the host #" + allocatedHost.getId() + "\n", CloudSim.clock())); } return true; } return false; }
/** * Process the event for an User/Broker who wants to destroy a VM previously created in this * Datacenter. This Datacenter may send, upon request, the status back to the * User/Broker. * * @param ev information about the event just happened * @param ack indicates if the event's sender expects to receive * an acknowledge message when the event finishes to be processed * * @pre ev != null * @post $none */ protected void processVmDestroy(SimEvent ev, boolean ack) { Vm vm = (Vm) ev.getData(); getVmAllocationPolicy().deallocateHostForVm(vm); if (ack) { int[] data = new int[3]; data[0] = getId(); data[1] = vm.getId(); data[2] = CloudSimTags.TRUE; sendNow(vm.getUserId(), CloudSimTags.VM_DESTROY_ACK, data); } getVmList().remove(vm); }
Log.printConcatLine("[VmScheduler.vmCreate] Allocation of VM #", vm.getId(), " to Host #", getId(), " failed by storage"); return false; Log.printConcatLine("[VmScheduler.vmCreate] Allocation of VM #", vm.getId(), " to Host #", getId(), " failed by RAM"); return false; Log.printConcatLine("[VmScheduler.vmCreate] Allocation of VM #", vm.getId(), " to Host #", getId(), " failed by BW"); getRamProvisioner().deallocateRamForVm(vm); Log.printConcatLine("[VmScheduler.vmCreate] Allocation of VM #", vm.getId(), " to Host #", getId(), " failed by MIPS"); getRamProvisioner().deallocateRamForVm(vm);
int[] data = new int[3]; data[0] = getId(); data[1] = vm.getId();