init
This commit is contained in:
33
src/org/cloudbus/cloudsim/util/Check.java
Normal file
33
src/org/cloudbus/cloudsim/util/Check.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package org.cloudbus.cloudsim.util;
|
||||
|
||||
public class Check {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
// Test2 t2 = new Test2(10);
|
||||
// Test2 t1 = new Test2(10);
|
||||
// Test2 t3 = new Test2(40);
|
||||
//
|
||||
// if(t1.equals(t2)){
|
||||
// System.out.println("Success1");
|
||||
// }if(t3==t2){
|
||||
// System.out.println("Success2");
|
||||
// }if(t1==t3){
|
||||
// System.out.println("Success3");
|
||||
// }
|
||||
// System.out.println("DONE");
|
||||
Test t2 = new Test(10);
|
||||
Test t1 = new Test(10);
|
||||
Test t3 = new Test(40);
|
||||
|
||||
if(t1.equals(t2)){
|
||||
System.out.println("Success1");
|
||||
}if(t3==t2){
|
||||
System.out.println("Success2");
|
||||
}if(t1==t3){
|
||||
System.out.println("Success3");
|
||||
}
|
||||
System.out.println("DONE");
|
||||
}
|
||||
|
||||
}
|
||||
55
src/org/cloudbus/cloudsim/util/ExecutionTimeMeasurer.java
Normal file
55
src/org/cloudbus/cloudsim/util/ExecutionTimeMeasurer.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Title: CloudSim Toolkit
|
||||
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
|
||||
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* Copyright (c) 2009-2012, The University of Melbourne, Australia
|
||||
*/
|
||||
|
||||
package org.cloudbus.cloudsim.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The class for measuring the execution time.
|
||||
*
|
||||
* @author Anton Beloglazov
|
||||
* @since CloudSim Toolkit 3.0
|
||||
*/
|
||||
public class ExecutionTimeMeasurer {
|
||||
|
||||
/** The execution times. */
|
||||
private final static Map<String, Long> executionTimes = new HashMap<String, Long>();
|
||||
|
||||
/**
|
||||
* Start.
|
||||
*
|
||||
* @param name the name
|
||||
*/
|
||||
public static void start(String name) {
|
||||
getExecutionTimes().put(name, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* End.
|
||||
*
|
||||
* @param name the name
|
||||
* @return the double
|
||||
*/
|
||||
public static double end(String name) {
|
||||
double time = (System.currentTimeMillis() - getExecutionTimes().get(name)) / 1000.0;
|
||||
getExecutionTimes().remove(name);
|
||||
return time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the execution times.
|
||||
*
|
||||
* @return the execution times
|
||||
*/
|
||||
public static Map<String, Long> getExecutionTimes() {
|
||||
return executionTimes;
|
||||
}
|
||||
|
||||
}
|
||||
379
src/org/cloudbus/cloudsim/util/MathUtil.java
Normal file
379
src/org/cloudbus/cloudsim/util/MathUtil.java
Normal file
@@ -0,0 +1,379 @@
|
||||
/*
|
||||
* Title: CloudSim Toolkit
|
||||
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
|
||||
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* Copyright (c) 2009-2012, The University of Melbourne, Australia
|
||||
*/
|
||||
|
||||
package org.cloudbus.cloudsim.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
|
||||
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
|
||||
import org.apache.commons.math3.stat.regression.SimpleRegression;
|
||||
|
||||
/**
|
||||
* A class containing multiple convenient math functions.
|
||||
*
|
||||
* @author Anton Beloglazov
|
||||
* @since CloudSim Toolkit 3.0
|
||||
*/
|
||||
public class MathUtil {
|
||||
|
||||
/**
|
||||
* Sums a list of numbers.
|
||||
*
|
||||
* @param list the list
|
||||
* @return the double
|
||||
*/
|
||||
public static double sum(final List<? extends Number> list) {
|
||||
double sum = 0;
|
||||
for (Number number : list) {
|
||||
sum += number.doubleValue();
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* List to array.
|
||||
*
|
||||
* @param list the list
|
||||
* @return the double[]
|
||||
*/
|
||||
public static double[] listToArray(final List<? extends Number> list) {
|
||||
double[] array = new double[list.size()];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = list.get(i).doubleValue();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the median.
|
||||
*
|
||||
* @param list the list
|
||||
* @return the median
|
||||
*/
|
||||
public static double median(final List<Double> list) {
|
||||
return getStatistics(list).getPercentile(50);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the median.
|
||||
*
|
||||
* @param list the list
|
||||
*
|
||||
* @return the median
|
||||
*/
|
||||
public static double median(final double[] list) {
|
||||
return getStatistics(list).getPercentile(50);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns descriptive statistics for the list of numbers.
|
||||
*
|
||||
* @param list
|
||||
* - the list of numbers. Must not be null.
|
||||
* @return - descriptive statistics for the list of numbers.
|
||||
*/
|
||||
public static DescriptiveStatistics getStatistics(final List<Double> list) {
|
||||
// Get a DescriptiveStatistics instance
|
||||
DescriptiveStatistics stats = new DescriptiveStatistics();
|
||||
|
||||
// Add the data from the array
|
||||
for (Double d : list) {
|
||||
stats.addValue(d);
|
||||
}
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns descriptive statistics for the array of numbers.
|
||||
*
|
||||
* @param list - the array of numbers. Must not be null.
|
||||
* @return - descriptive statistics for the array of numbers.
|
||||
*/
|
||||
public static DescriptiveStatistics getStatistics(final double[] list) {
|
||||
// Get a DescriptiveStatistics instance
|
||||
DescriptiveStatistics stats = new DescriptiveStatistics();
|
||||
|
||||
// Add the data from the array
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
stats.addValue(list[i]);
|
||||
}
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the average.
|
||||
*
|
||||
* @param list the list
|
||||
*
|
||||
* @return the average
|
||||
*/
|
||||
public static double mean(final List<Double> list) {
|
||||
double sum = 0;
|
||||
for (Double number : list) {
|
||||
sum += number;
|
||||
}
|
||||
return sum / list.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Variance.
|
||||
*
|
||||
* @param list the list
|
||||
* @return the double
|
||||
*/
|
||||
public static double variance(final List<Double> list) {
|
||||
long n = 0;
|
||||
double mean = mean(list);
|
||||
double s = 0.0;
|
||||
|
||||
for (double x : list) {
|
||||
n++;
|
||||
double delta = x - mean;
|
||||
mean += delta / n;
|
||||
s += delta * (x - mean);
|
||||
}
|
||||
// if you want to calculate std deviation
|
||||
// of a sample change this to (s/(n-1))
|
||||
return s / (n - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the standard deviation.
|
||||
*
|
||||
* @param list the list
|
||||
* @return the double
|
||||
*/
|
||||
public static double stDev(final List<Double> list) {
|
||||
return Math.sqrt(variance(list));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the mad.
|
||||
*
|
||||
* @param data the data
|
||||
* @return the mad
|
||||
*/
|
||||
public static double mad(final double[] data) {
|
||||
double mad = 0;
|
||||
if (data.length > 0) {
|
||||
double median = median(data);
|
||||
double[] deviationSum = new double[data.length];
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
deviationSum[i] = Math.abs(median - data[i]);
|
||||
}
|
||||
mad = median(deviationSum);
|
||||
}
|
||||
return mad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IQR.
|
||||
*
|
||||
* @param data the data
|
||||
* @return the IQR
|
||||
*/
|
||||
public static double iqr(final double[] data) {
|
||||
Arrays.sort(data);
|
||||
int q1 = (int) Math.round(0.25 * (data.length + 1)) - 1;
|
||||
int q3 = (int) Math.round(0.75 * (data.length + 1)) - 1;
|
||||
return data[q3] - data[q1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Count non zero beginning of the data.
|
||||
*
|
||||
* @param data the data
|
||||
* @return the int
|
||||
*/
|
||||
public static int countNonZeroBeginning(final double[] data) {
|
||||
int i = data.length - 1;
|
||||
while (i >= 0) {
|
||||
if (data[i--] != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return i + 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count shortest row.
|
||||
*
|
||||
* @param data the data
|
||||
* @return the int
|
||||
*/
|
||||
public static int countShortestRow(final double[][] data) {
|
||||
int minLength = 0;
|
||||
for (double[] row : data) {
|
||||
if (row.length < minLength) {
|
||||
minLength = row.length;
|
||||
}
|
||||
}
|
||||
return minLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim zero tail.
|
||||
*
|
||||
* @param data the data
|
||||
* @return the double[]
|
||||
*/
|
||||
public static double[] trimZeroTail(final double[] data) {
|
||||
return Arrays.copyOfRange(data, 0, countNonZeroBeginning(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the loess parameter estimates.
|
||||
*
|
||||
* @param y the y
|
||||
* @return the loess parameter estimates
|
||||
*/
|
||||
public static double[] getLoessParameterEstimates(final double[] y) {
|
||||
int n = y.length;
|
||||
double[] x = new double[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
x[i] = i + 1;
|
||||
}
|
||||
return createWeigthedLinearRegression(x, y, getTricubeWeigts(n))
|
||||
.regress().getParameterEstimates();
|
||||
}
|
||||
|
||||
public static SimpleRegression createLinearRegression(final double[] x,
|
||||
final double[] y) {
|
||||
SimpleRegression regression = new SimpleRegression();
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
regression.addData(x[i], y[i]);
|
||||
}
|
||||
return regression;
|
||||
}
|
||||
|
||||
public static OLSMultipleLinearRegression createLinearRegression(
|
||||
final double[][] x, final double[] y) {
|
||||
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
|
||||
regression.newSampleData(y, x);
|
||||
return regression;
|
||||
}
|
||||
|
||||
public static SimpleRegression createWeigthedLinearRegression(
|
||||
final double[] x, final double[] y, final double[] weigths) {
|
||||
double[] xW = new double[x.length];
|
||||
double[] yW = new double[y.length];
|
||||
|
||||
// As to Flanagan's documentation they perform weigthed regression if the
|
||||
// number or non-zero weigths is more than 40%
|
||||
int numZeroWeigths = 0;
|
||||
for (int i = 0; i < weigths.length; i++) {
|
||||
if (weigths[i] <= 0) {
|
||||
numZeroWeigths++;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
if (numZeroWeigths >= 0.4 * weigths.length) {
|
||||
// See: http://www.ncsu.edu/crsc/events/ugw07/Presentations/Crooks_Qiao/Crooks_Qiao_Alt_Presentation.pdf
|
||||
xW[i] = Math.sqrt(weigths[i]) * x[i];
|
||||
yW[i] = Math.sqrt(weigths[i]) * y[i];
|
||||
} else {
|
||||
xW[i] = x[i];
|
||||
yW[i] = y[i];
|
||||
}
|
||||
}
|
||||
|
||||
return createLinearRegression(xW, yW);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the robust loess parameter estimates.
|
||||
*
|
||||
* @param y the y
|
||||
* @return the robust loess parameter estimates
|
||||
*/
|
||||
public static double[] getRobustLoessParameterEstimates(final double[] y) {
|
||||
int n = y.length;
|
||||
double[] x = new double[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
x[i] = i + 1;
|
||||
}
|
||||
SimpleRegression tricubeRegression = createWeigthedLinearRegression(x,
|
||||
y, getTricubeWeigts(n));
|
||||
double[] residuals = new double[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
residuals[i] = y[i] - tricubeRegression.predict(x[i]);
|
||||
}
|
||||
SimpleRegression tricubeBySquareRegression = createWeigthedLinearRegression(
|
||||
x, y, getTricubeBisquareWeigts(residuals));
|
||||
|
||||
double[] estimates = tricubeBySquareRegression.regress()
|
||||
.getParameterEstimates();
|
||||
if (estimates[0] == Double.NaN || estimates[1] == Double.NaN) {
|
||||
return tricubeRegression.regress().getParameterEstimates();
|
||||
}
|
||||
return estimates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tricube weigts.
|
||||
*
|
||||
* @param n the n
|
||||
* @return the tricube weigts
|
||||
*/
|
||||
public static double[] getTricubeWeigts(final int n) {
|
||||
double[] weights = new double[n];
|
||||
double top = n - 1;
|
||||
double spread = top;
|
||||
for (int i = 2; i < n; i++) {
|
||||
double k = Math.pow(1 - Math.pow((top - i) / spread, 3), 3);
|
||||
if (k > 0) {
|
||||
weights[i] = 1 / k;
|
||||
} else {
|
||||
weights[i] = Double.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
weights[0] = weights[1] = weights[2];
|
||||
return weights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tricube bisquare weigts.
|
||||
*
|
||||
* @param residuals the residuals
|
||||
* @return the tricube bisquare weigts
|
||||
*/
|
||||
public static double[] getTricubeBisquareWeigts(final double[] residuals) {
|
||||
int n = residuals.length;
|
||||
double[] weights = getTricubeWeigts(n);
|
||||
double[] weights2 = new double[n];
|
||||
double s6 = median(abs(residuals)) * 6;
|
||||
for (int i = 2; i < n; i++) {
|
||||
double k = Math.pow(1 - Math.pow(residuals[i] / s6, 2), 2);
|
||||
if (k > 0) {
|
||||
weights2[i] = (1 / k) * weights[i];
|
||||
} else {
|
||||
weights2[i] = Double.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
weights2[0] = weights2[1] = weights2[2];
|
||||
return weights2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abs.
|
||||
*
|
||||
* @param data the data
|
||||
* @return the double[]
|
||||
*/
|
||||
public static double[] abs(final double[] data) {
|
||||
double[] result = new double[data.length];
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
result[i] = Math.abs(data[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
18
src/org/cloudbus/cloudsim/util/Test.java
Normal file
18
src/org/cloudbus/cloudsim/util/Test.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.cloudbus.cloudsim.util;
|
||||
|
||||
public class Test {
|
||||
public String s1;
|
||||
public int i1;
|
||||
|
||||
public Test(int i){
|
||||
s1 = new String("chas");
|
||||
i1=i;
|
||||
}
|
||||
|
||||
public boolean equals(Test t){
|
||||
if(t.s1.equals(s1) && t.i1==(i1) )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
11
src/org/cloudbus/cloudsim/util/Test2.java
Normal file
11
src/org/cloudbus/cloudsim/util/Test2.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package org.cloudbus.cloudsim.util;
|
||||
|
||||
public class Test2 {
|
||||
public Test t;
|
||||
public String st;
|
||||
|
||||
public Test2(int i){
|
||||
t = new Test(i);
|
||||
st = new String("PP");
|
||||
}
|
||||
}
|
||||
495
src/org/cloudbus/cloudsim/util/WorkloadFileReader.java
Normal file
495
src/org/cloudbus/cloudsim/util/WorkloadFileReader.java
Normal file
@@ -0,0 +1,495 @@
|
||||
/*
|
||||
* Title: CloudSim Toolkit
|
||||
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
|
||||
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* Copyright (c) 2009-2012, The University of Melbourne, Australia
|
||||
*/
|
||||
|
||||
package org.cloudbus.cloudsim.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import org.cloudbus.cloudsim.Cloudlet;
|
||||
import org.cloudbus.cloudsim.UtilizationModel;
|
||||
import org.cloudbus.cloudsim.UtilizationModelFull;
|
||||
|
||||
/**
|
||||
* This class is responsible for reading resource traces from a file and creating a list of jobs.
|
||||
* <p>
|
||||
* <b>NOTE:</b>
|
||||
* <ul>
|
||||
* <li>This class can only take <tt>one</tt> trace file of the following format: <i>ASCII text, zip,
|
||||
* gz.</i>
|
||||
* <li>If you need to load multiple trace files, then you need to create multiple instances of this
|
||||
* class <tt>each with a unique
|
||||
* entity name</tt>.
|
||||
* <li>If size of the trace file is huge or contains lots of traces please increase the JVM heap
|
||||
* size accordingly by using <tt>java -Xmx</tt> option when running the simulation.
|
||||
* <li>The default job file size for sending to and receiving from a resource is
|
||||
* {@link gridsim.net.Link#DEFAULT_MTU}. However, you can specify the file size by using
|
||||
* {@link #setGridletFileSize(int)}.
|
||||
* <li>A job run time is only for 1 PE <tt>not</tt> the total number of allocated PEs. Therefore, a
|
||||
* Gridlet length is also calculated for 1 PE.<br>
|
||||
* For example, job #1 in the trace has a run time of 100 seconds for 2 processors. This means each
|
||||
* processor runs job #1 for 100 seconds, if the processors have the same specification.
|
||||
* </ul>
|
||||
* <p>
|
||||
* By default, this class follows the standard workload format as specified in <a
|
||||
* href="http://www.cs.huji.ac.il/labs/parallel/workload/">
|
||||
* http://www.cs.huji.ac.il/labs/parallel/workload/</a> <br>
|
||||
* However, you can use other format by calling the below methods before running the simulation:
|
||||
* <ul>
|
||||
* <li> {@link #setComment(String)}
|
||||
* <li> {@link #setField(int, int, int, int, int)}
|
||||
* </ul>
|
||||
*
|
||||
* @author Anthony Sulistio and Marcos Dias de Assuncao
|
||||
* @since 5.0
|
||||
*
|
||||
* @see Workload
|
||||
*/
|
||||
public class WorkloadFileReader implements WorkloadModel {
|
||||
|
||||
private final File file; // file name
|
||||
|
||||
private final int rating; // a PE rating
|
||||
|
||||
private ArrayList<Cloudlet> jobs = null; // a list for getting all the
|
||||
|
||||
// Gridlets
|
||||
|
||||
// using Standard Workload Format
|
||||
private int JOB_NUM = 1 - 1; // job number
|
||||
|
||||
private int SUBMIT_TIME = 2 - 1; // submit time of a Gridlet
|
||||
|
||||
private final int RUN_TIME = 4 - 1; // running time of a Gridlet
|
||||
|
||||
private final int NUM_PROC = 5 - 1; // number of processors needed for a
|
||||
|
||||
// Gridlet
|
||||
private int REQ_NUM_PROC = 8 - 1; // required number of processors
|
||||
|
||||
private int REQ_RUN_TIME = 9 - 1; // required running time
|
||||
|
||||
private final int USER_ID = 12 - 1; // if of user who submitted the job
|
||||
|
||||
private final int GROUP_ID = 13 - 1; // if of group of the user who
|
||||
|
||||
// submitted the
|
||||
// job
|
||||
private int MAX_FIELD = 18; // max number of field in the trace file
|
||||
|
||||
private String COMMENT = ";"; // a string that denotes the start of a
|
||||
|
||||
// comment
|
||||
private static final int IRRELEVANT = -1; // irrelevant number
|
||||
|
||||
private String[] fieldArray = null; // a temp array storing all the fields
|
||||
|
||||
/**
|
||||
* Create a new {@link WorkloadFileReader} object.
|
||||
*
|
||||
* @param fileName the workload trace filename in one of the following format: <i>ASCII text,
|
||||
* zip, gz.</i>
|
||||
* @param rating the resource's PE rating
|
||||
* @throws FileNotFoundException
|
||||
* @throws IllegalArgumentException This happens for the following conditions:
|
||||
* <ul>
|
||||
* <li>the workload trace file name is null or empty
|
||||
* <li>the resource PE rating <= 0
|
||||
* </ul>
|
||||
* @pre fileName != null
|
||||
* @pre rating > 0
|
||||
* @post $none
|
||||
*/
|
||||
public WorkloadFileReader(final String fileName, final int rating) throws FileNotFoundException {
|
||||
if (fileName == null || fileName.length() == 0) {
|
||||
throw new IllegalArgumentException("Invalid trace file name.");
|
||||
} else if (rating <= 0) {
|
||||
throw new IllegalArgumentException("Resource PE rating must be > 0.");
|
||||
}
|
||||
|
||||
file = new File(fileName);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("Workload trace " + fileName + " does not exist");
|
||||
}
|
||||
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads job information from a given file.
|
||||
*
|
||||
* @return the list of gridlets read from the file; <code>null</code> in case of failure.
|
||||
*/
|
||||
@Override
|
||||
public ArrayList<Cloudlet> generateWorkload() {
|
||||
if (jobs == null) {
|
||||
jobs = new ArrayList<Cloudlet>();
|
||||
|
||||
// create a temp array
|
||||
fieldArray = new String[MAX_FIELD];
|
||||
|
||||
try {
|
||||
if (file.getName().endsWith(".gz")) {
|
||||
readGZIPFile(file);
|
||||
} else if (file.getName().endsWith(".zip")) {
|
||||
readZipFile(file);
|
||||
} else {
|
||||
readFile(file);
|
||||
}
|
||||
} catch (final FileNotFoundException e) {
|
||||
} catch (final IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
return jobs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies the start of a comment line.
|
||||
*
|
||||
* @param cmt a character that denotes the start of a comment, e.g. ";" or "#"
|
||||
* @return <code>true</code> if it is successful, <code>false</code> otherwise
|
||||
* @pre comment != null
|
||||
* @post $none
|
||||
*/
|
||||
public boolean setComment(final String cmt) {
|
||||
boolean success = false;
|
||||
if (cmt != null && cmt.length() > 0) {
|
||||
COMMENT = cmt;
|
||||
success = true;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells this class what to look in the trace file. This method should be called before the
|
||||
* start of the simulation.
|
||||
* <p>
|
||||
* By default, this class follows the standard workload format as specified in <a
|
||||
* href="http://www.cs.huji.ac.il/labs/parallel/workload/">
|
||||
* http://www.cs.huji.ac.il/labs/parallel/workload/</a> <br>
|
||||
* However, you can use other format by calling this method.
|
||||
* <p>
|
||||
* The parameters must be a positive integer number starting from 1. A special case is where
|
||||
* <tt>jobNum == -1</tt>, meaning the job or gridlet ID starts at 1.
|
||||
*
|
||||
* @param maxField max. number of field/column in one row
|
||||
* @param jobNum field/column number for locating the job ID
|
||||
* @param submitTime field/column number for locating the job submit time
|
||||
* @param runTime field/column number for locating the job run time
|
||||
* @param numProc field/column number for locating the number of PEs required to run a job
|
||||
* @return <code>true</code> if successful, <code>false</code> otherwise
|
||||
* @throws IllegalArgumentException if any of the arguments are not within the acceptable ranges
|
||||
* @pre maxField > 0
|
||||
* @pre submitTime > 0
|
||||
* @pre runTime > 0
|
||||
* @pre numProc > 0
|
||||
* @post $none
|
||||
*/
|
||||
public boolean setField(
|
||||
final int maxField,
|
||||
final int jobNum,
|
||||
final int submitTime,
|
||||
final int runTime,
|
||||
final int numProc) {
|
||||
// need to subtract by 1 since array starts at 0.
|
||||
if (jobNum > 0) {
|
||||
JOB_NUM = jobNum - 1;
|
||||
} else if (jobNum == 0) {
|
||||
throw new IllegalArgumentException("Invalid job number field.");
|
||||
} else {
|
||||
JOB_NUM = -1;
|
||||
}
|
||||
|
||||
// get the max. number of field
|
||||
if (maxField > 0) {
|
||||
MAX_FIELD = maxField;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid max. number of field.");
|
||||
}
|
||||
|
||||
// get the submit time field
|
||||
if (submitTime > 0) {
|
||||
SUBMIT_TIME = submitTime - 1;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid submit time field.");
|
||||
}
|
||||
|
||||
// get the run time field
|
||||
if (runTime > 0) {
|
||||
REQ_RUN_TIME = runTime - 1;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid run time field.");
|
||||
}
|
||||
|
||||
// get the number of processors field
|
||||
if (numProc > 0) {
|
||||
REQ_NUM_PROC = numProc - 1;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid number of processors field.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------- PRIVATE METHODS -------------------
|
||||
|
||||
/**
|
||||
* Creates a Gridlet with the given information and adds to the list
|
||||
*
|
||||
* @param id a Gridlet ID
|
||||
* @param submitTime Gridlet's submit time
|
||||
* @param runTime Gridlet's run time
|
||||
* @param numProc number of processors
|
||||
* @param reqRunTime user estimated run time
|
||||
* @param userID user id
|
||||
* @param groupID user's group id
|
||||
* @pre id >= 0
|
||||
* @pre submitTime >= 0
|
||||
* @pre runTime >= 0
|
||||
* @pre numProc > 0
|
||||
* @post $none
|
||||
*/
|
||||
private void createJob(
|
||||
final int id,
|
||||
final long submitTime,
|
||||
final int runTime,
|
||||
final int numProc,
|
||||
final int reqRunTime,
|
||||
final int userID,
|
||||
final int groupID) {
|
||||
// create the cloudlet
|
||||
final int len = runTime * rating;
|
||||
UtilizationModel utilizationModel = new UtilizationModelFull();
|
||||
final Cloudlet wgl = new Cloudlet(
|
||||
id,
|
||||
len,
|
||||
numProc,
|
||||
0,
|
||||
0,
|
||||
utilizationModel,
|
||||
utilizationModel,
|
||||
utilizationModel);
|
||||
jobs.add(wgl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts relevant information from a given array
|
||||
*
|
||||
* @param array an array of String
|
||||
* @param line a line number
|
||||
* @pre array != null
|
||||
* @pre line > 0
|
||||
*/
|
||||
private void extractField(final String[] array, final int line) {
|
||||
try {
|
||||
Integer obj = null;
|
||||
|
||||
// get the job number
|
||||
int id = 0;
|
||||
if (JOB_NUM == IRRELEVANT) {
|
||||
id = jobs.size() + 1;
|
||||
} else {
|
||||
obj = new Integer(array[JOB_NUM].trim());
|
||||
id = obj.intValue();
|
||||
}
|
||||
|
||||
// get the submit time
|
||||
final Long l = new Long(array[SUBMIT_TIME].trim());
|
||||
final long submitTime = l.intValue();
|
||||
|
||||
// get the user estimated run time
|
||||
obj = new Integer(array[REQ_RUN_TIME].trim());
|
||||
final int reqRunTime = obj.intValue();
|
||||
|
||||
// if the required run time field is ignored, then use
|
||||
// the actual run time
|
||||
obj = new Integer(array[RUN_TIME].trim());
|
||||
int runTime = obj.intValue();
|
||||
|
||||
final int userID = new Integer(array[USER_ID].trim()).intValue();
|
||||
final int groupID = new Integer(array[GROUP_ID].trim()).intValue();
|
||||
|
||||
// according to the SWF manual, runtime of 0 is possible due
|
||||
// to rounding down. E.g. runtime is 0.4 seconds -> runtime = 0
|
||||
if (runTime <= 0) {
|
||||
runTime = 1; // change to 1 second
|
||||
}
|
||||
|
||||
// get the number of allocated processors
|
||||
obj = new Integer(array[REQ_NUM_PROC].trim());
|
||||
int numProc = obj.intValue();
|
||||
|
||||
// if the required num of allocated processors field is ignored
|
||||
// or zero, then use the actual field
|
||||
if (numProc == IRRELEVANT || numProc == 0) {
|
||||
obj = new Integer(array[NUM_PROC].trim());
|
||||
numProc = obj.intValue();
|
||||
}
|
||||
|
||||
// finally, check if the num of PEs required is valid or not
|
||||
if (numProc <= 0) {
|
||||
numProc = 1;
|
||||
}
|
||||
createJob(id, submitTime, runTime, numProc, reqRunTime, userID, groupID);
|
||||
} catch (final Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Breaks a line of string into many fields.
|
||||
*
|
||||
* @param line a line of string
|
||||
* @param lineNum a line number
|
||||
* @pre line != null
|
||||
* @pre lineNum > 0
|
||||
* @post $none
|
||||
*/
|
||||
private void parseValue(final String line, final int lineNum) {
|
||||
// skip a comment line
|
||||
if (line.startsWith(COMMENT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String[] sp = line.split("\\s+"); // split the fields based on a
|
||||
// space
|
||||
int len = 0; // length of a string
|
||||
int index = 0; // the index of an array
|
||||
|
||||
// check for each field in the array
|
||||
for (final String elem : sp) {
|
||||
len = elem.length(); // get the length of a string
|
||||
|
||||
// if it is empty then ignore
|
||||
if (len == 0) {
|
||||
continue;
|
||||
}
|
||||
fieldArray[index] = elem;
|
||||
index++;
|
||||
}
|
||||
|
||||
if (index == MAX_FIELD) {
|
||||
extractField(fieldArray, lineNum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a text file one line at the time
|
||||
*
|
||||
* @param fl a file name
|
||||
* @return <code>true</code> if successful, <code>false</code> otherwise.
|
||||
* @throws IOException if the there was any error reading the file
|
||||
* @throws FileNotFoundException if the file was not found
|
||||
*/
|
||||
private boolean readFile(final File fl) throws IOException, FileNotFoundException {
|
||||
boolean success = false;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fl)));
|
||||
|
||||
// read one line at the time
|
||||
int line = 1;
|
||||
while (reader.ready()) {
|
||||
parseValue(reader.readLine(), line);
|
||||
line++;
|
||||
}
|
||||
|
||||
reader.close();
|
||||
success = true;
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a gzip file one line at the time
|
||||
*
|
||||
* @param fl a gzip file name
|
||||
* @return <code>true</code> if successful; <code>false</code> otherwise.
|
||||
* @throws IOException if the there was any error reading the file
|
||||
* @throws FileNotFoundException if the file was not found
|
||||
*/
|
||||
private boolean readGZIPFile(final File fl) throws IOException, FileNotFoundException {
|
||||
boolean success = false;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(fl))));
|
||||
|
||||
// read one line at the time
|
||||
int line = 1;
|
||||
while (reader.ready()) {
|
||||
parseValue(reader.readLine(), line);
|
||||
line++;
|
||||
}
|
||||
|
||||
reader.close();
|
||||
success = true;
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a Zip file.
|
||||
*
|
||||
* @param fl a zip file name
|
||||
* @return <code>true</code> if reading a file is successful; <code>false</code> otherwise.
|
||||
* @throws IOException if the there was any error reading the file
|
||||
*/
|
||||
private boolean readZipFile(final File fl) throws IOException {
|
||||
boolean success = false;
|
||||
ZipFile zipFile = null;
|
||||
try {
|
||||
BufferedReader reader = null;
|
||||
|
||||
// ZipFile offers an Enumeration of all the files in the file
|
||||
zipFile = new ZipFile(fl);
|
||||
final Enumeration<? extends ZipEntry> e = zipFile.entries();
|
||||
while (e.hasMoreElements()) {
|
||||
success = false; // reset the value again
|
||||
final ZipEntry zipEntry = e.nextElement();
|
||||
|
||||
reader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
|
||||
|
||||
// read one line at the time
|
||||
int line = 1;
|
||||
while (reader.ready()) {
|
||||
parseValue(reader.readLine(), line);
|
||||
line++;
|
||||
}
|
||||
|
||||
reader.close();
|
||||
success = true;
|
||||
}
|
||||
} finally {
|
||||
if (zipFile != null) {
|
||||
zipFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
}
|
||||
34
src/org/cloudbus/cloudsim/util/WorkloadModel.java
Normal file
34
src/org/cloudbus/cloudsim/util/WorkloadModel.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Title: CloudSim Toolkit
|
||||
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
|
||||
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* Copyright (c) 2009-2012, The University of Melbourne, Australia
|
||||
*/
|
||||
|
||||
package org.cloudbus.cloudsim.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.cloudbus.cloudsim.Cloudlet;
|
||||
|
||||
/**
|
||||
* This interface defines what a workload model should provide. A workload model generates a list of
|
||||
* jobs that can be dispatched to a resource by {@link Workload}.
|
||||
*
|
||||
* @author Marcos Dias de Assuncao
|
||||
* @since 5.0
|
||||
*
|
||||
* @see Workload
|
||||
* @see WorkloadFileReader
|
||||
*/
|
||||
public interface WorkloadModel {
|
||||
|
||||
/**
|
||||
* Returns a list with the jobs generated by the workload.
|
||||
*
|
||||
* @return a list with the jobs generated by the workload.
|
||||
*/
|
||||
List<Cloudlet> generateWorkload();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user