package org.cloudbus.cloudsim.power;

import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.core.CloudSim;

import java.util.List;

/**
 * ClassName PowerVmSelectionPolicyMyNewSelectionSecond
 * Description
 * Create by HW
 * Date 2021/4/24 10:48
 */
public class PowerVmSelectionPolicyMyNewSelectionSecond extends PowerVmSelectionPolicy {
    @Override
    public Vm getVmToMigrate(PowerHost host) {
       /* List<PowerVm> migratableVms = getMigratableVms(host);
        if (migratableVms.isEmpty()){
            return null;
        }
        Vm vmTomigrate=null;
        double minMetrix= Double.MAX_VALUE,value1=0.3,value2=0.7;
        for (Vm vm:migratableVms) {
            //获得相关的vm请求的MIPS
            *//*double sum=0;
            for(Double currentAllocateMIPS:vm.getCurrentAllocatedMips()){
                sum+=currentAllocateMIPS;
            }*//*//vm.getCurrentRequestedTotalMips();
            double perfomance=value1*(vm.getCurrentRequestedTotalMips()*vm.getCurrentUtilizationOfCpu()/vm.getMips()*1)
                    +value2*(vm.getCurrentRequestedRam()/vm.getRam());
            //double Metrix=vm.getSize()/host.getBw()*((1-host.getUtilizationOfCpu())*(vm.getMips()/host.getTotalMips()));
            if (perfomance<minMetrix){
                minMetrix=perfomance;
                vmTomigrate=vm;
            }
        }
        return vmTomigrate;
    }*/
        List<PowerVm> migratableVms = getMigratableVms(host);
        if (migratableVms.isEmpty()) {
            return null;
        }
        Vm vmToMigrate = null;
        double minMetric = Double.MAX_VALUE;
        for (Vm vm : migratableVms) {
            if (vm.isInMigration()) {
                continue;
            }
            //设置相应的权重，此处是选择在主机中，虚拟机所在主机空间资源最大的虚拟机
            double w1=0.6,w2=0.3,w3=0.1;

            double metrix1=(w2*vm.getRam()/host.getRam()+w1*(vm.getCurrentUtilizationOfCpu()*vm.getMips()
                    / vm.getMips())/host.getUtilizationOfCpuMips()+w3*(vm.getCurrentRequestedBw()/host.getUsedBw()));
            if (metrix1 < minMetric) {
                minMetric = metrix1;
                vmToMigrate = vm;
            }
        }
        return vmToMigrate;
    }
}
