org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicyRandom Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudsim-plus Show documentation
Show all versions of cloudsim-plus Show documentation
CloudSim Plus: A modern, highly extensible and easier-to-use Java 8 Framework for Modeling and Simulation of Cloud Computing Infrastructures and Services
/*
* CloudSim Plus: A modern, highly-extensible and easier-to-use Framework for
* Modeling and Simulation of Cloud Computing Infrastructures and Services.
* http://cloudsimplus.org
*
* Copyright (C) 2015-2021 Universidade da Beira Interior (UBI, Portugal) and
* the Instituto Federal de Educação Ciência e Tecnologia do Tocantins (IFTO, Brazil).
*
* This file is part of CloudSim Plus.
*
* CloudSim Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CloudSim Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CloudSim Plus. If not, see .
*/
package org.cloudbus.cloudsim.allocationpolicies;
import org.cloudbus.cloudsim.distributions.ContinuousDistribution;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.vms.Vm;
import java.util.Objects;
import java.util.Optional;
/**
* A VM allocation policy
* which finds a random Host having suitable resources to place a given VM.
* This is a high time-efficient policy with a best-case complexity O(1)
* and a worst-case complexity O(N), where N is the number of Hosts.
*
*
* NOTES:
*
* - This policy doesn't perform optimization of VM allocation by means of VM migration.
* - It has a low computational complexity (high time-efficient) but may return
* and inactive Host that will be activated, while there may be active Hosts
* suitable for the VM.
* - Despite the low computational complexity, such a policy may increase the number of active Hosts,
* that increases power consumption.
*
*
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 4.4.2
*/
public class VmAllocationPolicyRandom extends VmAllocationPolicyAbstract implements VmAllocationPolicy {
/**
* A Pseudo-Random Number Generator (PRNG) used to select a Host.
*/
private final ContinuousDistribution random;
/**
* Instantiates a VmAllocationPolicyRandom.
*
* @param random a Pseudo-Random Number Generator (PRNG) used to select a Host.
* The PRNG must return values between 0 and 1.
*/
public VmAllocationPolicyRandom(final ContinuousDistribution random){
super();
this.random = Objects.requireNonNull(random);
}
@Override
protected Optional defaultFindHostForVm(final Vm vm) {
final var hostList = getHostList();
/* The for loop just defines the maximum number of Hosts to try.
* When a suitable Host is found, the method returns immediately. */
final int maxTries = hostList.size();
for (int i = 0; i < maxTries; i++) {
final int hostIndex = (int)(random.sample() * hostList.size());
final var host = hostList.get(hostIndex);
if (host.isSuitableForVm(vm)) {
return Optional.of(host);
}
}
return Optional.empty();
}
}