Search code examples
timemigrationvirtual-machinecloudsim

Print migration time in CloudSim


I want to implement the virtual machine migration time formula in cloud sim, and i made a simple method but it gave me wrong results, the problem is that i need the list of migrated virtual machines in the simulation so i can get to calculate and print the total migration time in the end, please if you have any idea help me !

public static double getTotalMigrationTime(List<Vm> vms){           
  double Tmigr = 0;
  for(Vm vm:vms){
    if(vm.isInMigration()){
      double Cj = vm.getRam();
      double BWj = vm.getBw();
      Tmigr += Cj/BWj;
    }
  }
  return Tmigr;
}

Solution

  • you have to add the migration and energy formula in the PowerDatacenter class, more precisely in the updateCloudletProcessing method, and also add a set and get method without forgetting a local variable in the class to save the migration time and energy in it.

            // Migration Time
        public double Tmigr = 0;
    
        // Migration Energy
        public double Emigr;
    
    @Override
    protected void updateCloudletProcessing() {
        if (getCloudletSubmitted() == -1 || getCloudletSubmitted() == CloudSim.clock()) {
            CloudSim.cancelAll(getId(), new PredicateType(CloudSimTags.VM_DATACENTER_EVENT));
            schedule(getId(), getSchedulingInterval(), CloudSimTags.VM_DATACENTER_EVENT);
            return;
        }
        double currentTime = CloudSim.clock();
                double Pm = 4.5;
                double sum = 0;
    
        // if some time passed since last processing
        if (currentTime > getLastProcessTime()) {
            System.out.print(currentTime + " ");
    
            double minTime = updateCloudetProcessingWithoutSchedulingFutureEventsForce();
    
            if (!isDisableMigrations()) {
                List<Map<String, Object>> migrationMap = getVmAllocationPolicy().optimizeAllocation(
                        getVmList());
    
                if (migrationMap != null) {
                    for (Map<String, Object> migrate : migrationMap) {
                        Vm vm = (Vm) migrate.get("vm");
                        PowerHost targetHost = (PowerHost) migrate.get("host");
                        PowerHost oldHost = (PowerHost) vm.getHost();
    
                                                // Calculates migration time and energy
                                                double Cj = vm.getRam();
                                                double BWj = (double)targetHost.getBw()/(2*8000);
                                                Tmigr = Tmigr + Cj/BWj; // Time
                                                sum = sum + Pm*(Cj/BWj);
                                                Emigr = 4*sum; //energy
    
                        if (oldHost == null) {
                            Log.formatLine(
                                    "%.2f: Migration of VM #%d to Host #%d is started",
                                    currentTime,
                                    vm.getId(),
                                    targetHost.getId());
                        } else {
                            Log.formatLine(
                                    "%.2f: Migration of VM #%d from Host #%d to Host #%d is started",
                                    currentTime,
                                    vm.getId(),
                                    oldHost.getId(),
                                    targetHost.getId());
                        }
    
                        targetHost.addMigratingInVm(vm);
                        incrementMigrationCount();
    
                        /** VM migration delay = RAM / bandwidth **/
                        // we use BW / 2 to model BW available for migration purposes, the other
                        // half of BW is for VM communication
                        // around 16 seconds for 1024 MB using 1 Gbit/s network
                        send(
                                getId(),
                                vm.getRam() / ((double) targetHost.getBw() / (2 * 8000)),
                                CloudSimTags.VM_MIGRATE,
                                migrate);
                    }
                }
            }
    
            // schedules an event to the next time
            if (minTime != Double.MAX_VALUE) {
                CloudSim.cancelAll(getId(), new PredicateType(CloudSimTags.VM_DATACENTER_EVENT));
                send(getId(), getSchedulingInterval(), CloudSimTags.VM_DATACENTER_EVENT);
            }
    
            setLastProcessTime(currentTime);
        }
                setMigrationTime(Tmigr); // Update Total Migration Time
                setMigrationEnergy(Emigr); // Update Total Migration Energy
    }
    
    // sets migration time
     public void setMigrationTime(double Tm){
         Tmigr = Tm;
     }
     //gets migration time
     public double getMigrationTime(){
         return Tmigr;
     }
     //sets migration Energy
     public void setMigrationEnergy(double Em){
         Emigr = Em;
     }
     //gets total migration energy
     public double getMigrationEnergy(){
         return Emigr;
     }
    

    After that you can get the migration time and energy using the get methods and print them in the Helper Class, without forgetting to transform the energy to KWh.

    //get Migration Time and energy from PowerDatacenter class
        double Tmigr = datacenter.getMigrationTime();
        double Emigr = datacenter.getMigrationEnergy()/(3600*1000);
    
    Log.printLine(String.format("Migration Time : %.2f sec",Tmigr));        
            Log.printLine(String.format("Migration Energy : %.5f kWh",Emigr));