seyed8453 / cloudsim

Automatically exported from code.google.com/p/cloudsim
0 stars 0 forks source link

Addition of simulation termination feature to CloudSim core #8

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The following modifications to the CloudSim class allows for a simulation to be 
terminated at a given point in time (similar to the pause feature).

First, add class variable:

    /** The termination time. */
    private static long terminateAt = -1;

Second, add corresponding methods:

    /**
     * This method is called if one wants to terminate the simulation.
     *
     * @return true, if successful; false otherwise.
     */
    public static boolean terminateSimulation() {
        running = false;
        printMessage("Simulation: Reached termination time.");
        return true;
    }

    /**
     * This method is called if one wants to terminate the simulation at a 
     * given time.
     *
     * @param time the time at which the simulation has to be terminated
     * @return true, if successful
     * otherwise.
     */
    public static boolean terminateSimulation(long time) {
        if (time <= clock) {
            return false;
        } else {
            terminateAt = time;
        }
        return true;
    }

Third, modify run() method as follows:

    public static double run() {
        if (!running) {
            runStart();
        }
        while (true) {
            if (runClockTick() || abruptTerminate) {
                break;
            }

            // This IF statement is the only modification required 
            // to the run() method.
            if (terminateAt != -1 && clock >= terminateAt) {
                terminateSimulation();
                clock = terminateAt;
                break;
            }

            if (pauseAt != -1 && ((future.size() > 0 && clock <= pauseAt && pauseAt <= future.iterator().next().eventTime()) || future.size() == 0 && pauseAt <= clock)) {
                pauseSimulation();
                clock = pauseAt;
            }

            while (paused) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        double clock = clock();

        finishSimulation();
        runStop();

        return clock;
    }

Original issue reported on code.google.com by gastonke...@gmail.com on 29 Aug 2011 at 8:30

GoogleCodeExporter commented 8 years ago
The issue is not a defect, but a proposed enhancement.

Regards,
Gaston

Original comment by gastonke...@gmail.com on 29 Aug 2011 at 8:32

GoogleCodeExporter commented 8 years ago
The above code has been added to the CloudSim class.

Original comment by rodrigo.calheiros on 15 Dec 2011 at 3:41

GoogleCodeExporter commented 8 years ago

Original comment by rodrigo.calheiros on 15 Dec 2011 at 6:29