This repository was archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFCFS_Scheduler.java
More file actions
141 lines (108 loc) · 4.91 KB
/
FCFS_Scheduler.java
File metadata and controls
141 lines (108 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package FCFS;
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import utils.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FCFS_Scheduler {
private static double[][] lengthMatrix;
public static double main(String[] args) {
double finishtime = 0.0;
Log.printLine("Starting FCFS Scheduler...");
try {
Commons.set_cloudsim_parameters();
CloudSim.init(Commons.num_user, Commons.calendar, Commons.trace_flag);
Commons.createDatacenters();
FCFS_DatacenterBroker broker = createBroker("Broker_0");
int brokerId = broker.getId();
Commons.create_vms_and_cloudlets(brokerId, 1);
broker.submitVmList(Commons.vmList);
broker.submitCloudletList(Commons.cloudletList);
CloudSim.startSimulation();
CloudSim.stopSimulation();
List<Cloudlet> newList = broker.getCloudletReceivedList();
finishtime = Commons.printCloudletList(newList, 1, null);
/***AddED EXTRA */
printCloudletList(newList);
Log.printLine("FCFS Scheduler finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
}
return finishtime;
}
private static FCFS_DatacenterBroker createBroker(String name) throws Exception {
return new FCFS_DatacenterBroker(name);
}
/******** ADDED EXTRA PART */
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("VM ID" + indent + "Load Quality"+ indent + "Time");
DecimalFormat dft = new DecimalFormat("###.##");
dft.setMinimumIntegerDigits(2);
lengthMatrix = GenerateLengthMatrix.getlengthMatrix();
// for (int i = 0; i < size; i++) {
// cloudlet = list.get(i);
// int taskId = i; // Assuming task ID starts from 0
// int dataCenterId = cloudlet.getVmId() % Constants.NO_OF_DATACENTERS;
// double length = lengthMatrix[taskId][dataCenterId];
// Log.printLine(indent + dft.format(taskId) + indent + indent + dft.format(dataCenterId) + indent + indent + dft.format(length));
// }
// CODE TO CHECK UNDERLOADED/OVERLOADED SYSTEM
double tot_summation_time=0;
for(int i=0;i<Constants.NO_OF_TASKS;i++)
{
tot_summation_time+= lengthMatrix[i][0];
}
double avg_sum_time=tot_summation_time/Constants.NO_OF_DATACENTERS;
// System.out.println("avg_sum_time "+ avg_sum_time);
double a = (avg_sum_time*25)/100;
double b = (avg_sum_time*60)/100;
double x = avg_sum_time-a;
double y = avg_sum_time+b;
// System.out.printf(" \n Below are a, b, x, y ");
// System.out.println(a + " "+ b + " " + x + " " + y);
Map<Integer, Double> totalLengths = new HashMap<>();
// First loop to calculate total lengths
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
int taskId = i; // Assuming task ID starts from 0
int dataCenterId = cloudlet.getVmId() % Constants.NO_OF_DATACENTERS;
double length = lengthMatrix[taskId][dataCenterId];
// Update total length for the datacenter
totalLengths.put(dataCenterId, totalLengths.getOrDefault(dataCenterId, 0.0) + length);
}
// Second loop to print load quality using total lengths
for (Map.Entry<Integer, Double> entry : totalLengths.entrySet()) {
int dataCenterId = entry.getKey();
double totalLengthForDatacenter = entry.getValue();
String loadAns = ((totalLengthForDatacenter > y) ? "Overloaded" : (totalLengthForDatacenter < x) ? "Underloaded" : "Ok");
System.out.println(dataCenterId + indent + loadAns + indent + totalLengthForDatacenter);
}
System.out.println("avg_sum_time "+ avg_sum_time);
System.out.printf(" \n Below are a, b, x, y ");
System.out.println(a + " "+ b + " " + x + " " + y);
// CODE FOR LOAD ENDS HERE
double makespan = calcMakespan(list);
Log.printLine("Makespan using FCFS: " + makespan);
}
private static double calcMakespan(List<Cloudlet> list) {
lengthMatrix = GenerateLengthMatrix.getlengthMatrix();
double makespan = 0;
double[] dcWorkingTime = new double[Constants.NO_OF_DATACENTERS];
for (int i = 0; i < list.size(); i++) {
int dcId = list.get(i).getVmId() % Constants.NO_OF_DATACENTERS;
if (dcWorkingTime[dcId] != 0) --dcWorkingTime[dcId];
dcWorkingTime[dcId] += lengthMatrix[i][dcId];
makespan = Math.max(makespan, dcWorkingTime[dcId]);
}
return makespan;
}
/**** */
}