1 | package simulator.lists; |
---|
2 | |
---|
3 | import gridsim.gssim.Constants; |
---|
4 | |
---|
5 | import java.util.Calendar; |
---|
6 | import java.util.Collections; |
---|
7 | import java.util.Date; |
---|
8 | import java.util.Iterator; |
---|
9 | import java.util.LinkedHashSet; |
---|
10 | import java.util.SortedMap; |
---|
11 | import java.util.TreeMap; |
---|
12 | |
---|
13 | import schedframe.implementation.Reservation; |
---|
14 | import schedframe.implementation.TimeResourceAllocation; |
---|
15 | import schedframe.implementation.TimeSlot; |
---|
16 | import schedframe.resUnits.PEResourceUnit; |
---|
17 | import simulator.VirtualClock; |
---|
18 | |
---|
19 | public class ResourceUsage { |
---|
20 | |
---|
21 | private static final long TIME_BORDER=1000 * 60 * 60 * 24 * 7;//week |
---|
22 | private ReservationList<Reservation> reservationList; |
---|
23 | private long timeInterval; |
---|
24 | private int cpuCount; |
---|
25 | private ReservationList<Reservation> oldReservationList; |
---|
26 | private LinkedHashSet<Long> reservationsTimestampsSet; |
---|
27 | private TreeMap<Long, Integer> reservationsCalendar; |
---|
28 | private LinkedHashSet<Long> timestampsSet; |
---|
29 | private TreeMap<Long, Integer> calendar; |
---|
30 | |
---|
31 | public ResourceUsage(ReservationList<Reservation> reservationList, |
---|
32 | long timeInterval) { |
---|
33 | this.reservationList = reservationList; |
---|
34 | this.timeInterval = timeInterval; |
---|
35 | this.cpuCount = reservationList.cpuCount; |
---|
36 | reservationsTimestampsSet = createReservationsTimestampsSet(); |
---|
37 | reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet); |
---|
38 | timestampsSet = createTimestampsSet(); |
---|
39 | calendar = createCalendar(timestampsSet, reservationsCalendar); |
---|
40 | oldReservationList = new ReservationList<Reservation>(reservationList); |
---|
41 | } |
---|
42 | |
---|
43 | public TimeResourceAllocation[] getResourceUsage() { |
---|
44 | |
---|
45 | if (!oldReservationList.equals(reservationList)) { |
---|
46 | reservationsTimestampsSet = createReservationsTimestampsSet(); |
---|
47 | reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet); |
---|
48 | calendar = createCalendar(timestampsSet, reservationsCalendar); |
---|
49 | oldReservationList = new ReservationList<Reservation>( |
---|
50 | reservationList); |
---|
51 | } |
---|
52 | |
---|
53 | TimeResourceAllocation[] slots; |
---|
54 | |
---|
55 | if(calendar.size()==0){ |
---|
56 | |
---|
57 | TreeMap<Long, Integer> emptyCalendar = createEmptyCalendar(); |
---|
58 | slots = new TimeResourceAllocation[emptyCalendar.size()+1]; |
---|
59 | Iterator<Long> it = emptyCalendar.keySet().iterator(); |
---|
60 | int counter = 0; |
---|
61 | Calendar endTime = Calendar.getInstance(); |
---|
62 | while (it.hasNext()) { |
---|
63 | Long time = (Long) it.next(); |
---|
64 | PEResourceUnit resUnit = new PEResourceUnit(cpuCount-emptyCalendar.get(time)); |
---|
65 | Calendar startTime = Calendar.getInstance(); |
---|
66 | startTime.setTimeInMillis(time - timeInterval); |
---|
67 | endTime = Calendar.getInstance(); |
---|
68 | endTime.setTimeInMillis(time); |
---|
69 | slots[counter] = new TimeResourceAllocation(resUnit, startTime, |
---|
70 | endTime); |
---|
71 | counter++; |
---|
72 | |
---|
73 | } |
---|
74 | PEResourceUnit resUnit= new PEResourceUnit(cpuCount); |
---|
75 | Calendar maxEndTime=Calendar.getInstance(); |
---|
76 | maxEndTime.setTimeInMillis(VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis()+TIME_BORDER); |
---|
77 | slots[counter] = new TimeResourceAllocation(resUnit, endTime, |
---|
78 | maxEndTime); |
---|
79 | return slots; |
---|
80 | /*slots = new TimeResourceAllocation[1]; |
---|
81 | PEResourceUnit resUnit = new PEResourceUnit(cpuCount); |
---|
82 | Calendar startTime=VirtualClock.getCurrentGlobalTimeCalendar(); |
---|
83 | Calendar endTime=Calendar.getInstance(); |
---|
84 | endTime.setTimeInMillis(VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis()+week); |
---|
85 | slots[0]=(new TimeResourceAllocation(resUnit, startTime, endTime)); |
---|
86 | return slots;*/ |
---|
87 | } else { |
---|
88 | |
---|
89 | slots = new TimeResourceAllocation[calendar |
---|
90 | .size()+1]; |
---|
91 | Iterator<Long> it = calendar.keySet().iterator(); |
---|
92 | int counter = 0; |
---|
93 | Calendar endTime = Calendar.getInstance(); |
---|
94 | while (it.hasNext()) { |
---|
95 | Long time = (Long) it.next(); |
---|
96 | PEResourceUnit resUnit = new PEResourceUnit(cpuCount-calendar.get(time)); |
---|
97 | Calendar startTime = Calendar.getInstance(); |
---|
98 | startTime.setTimeInMillis(time - timeInterval); |
---|
99 | endTime = Calendar.getInstance(); |
---|
100 | endTime.setTimeInMillis(time); |
---|
101 | slots[counter] = new TimeResourceAllocation(resUnit, startTime, |
---|
102 | endTime); |
---|
103 | counter++; |
---|
104 | |
---|
105 | } |
---|
106 | PEResourceUnit resUnit= new PEResourceUnit(cpuCount); |
---|
107 | //resUnit.setResUnitStatus(Avl.NOT_USED); |
---|
108 | //resUnit.setAmountPerspective(Avl.USED); |
---|
109 | Calendar maxEndTime=Calendar.getInstance(); |
---|
110 | maxEndTime.setTimeInMillis(VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis()+TIME_BORDER); |
---|
111 | slots[counter] = new TimeResourceAllocation(resUnit, endTime, |
---|
112 | maxEndTime); |
---|
113 | return slots; |
---|
114 | } |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | public boolean checkReservation(TimeSlot timeSlot, int size) { |
---|
119 | if (timeSlot.getDurationInMillis() <= 0) |
---|
120 | return false; |
---|
121 | if (!oldReservationList.equals(reservationList)) { |
---|
122 | reservationsTimestampsSet = createReservationsTimestampsSet(); |
---|
123 | reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet); |
---|
124 | calendar = createCalendar(timestampsSet, reservationsCalendar); |
---|
125 | oldReservationList = new ReservationList<Reservation>( |
---|
126 | reservationList); |
---|
127 | } |
---|
128 | if(calendar.size()==0){ |
---|
129 | if (cpuCount >= size) |
---|
130 | return true; |
---|
131 | } |
---|
132 | int value = getMaxCalendarValue(calendar, timeSlot.getStartTime() |
---|
133 | .getTime(), timeSlot.getEndTime().getTime()); |
---|
134 | if (cpuCount - value >= size) |
---|
135 | return true; |
---|
136 | return false; |
---|
137 | } |
---|
138 | |
---|
139 | public TimeSlot findInterval(TimeSlot timeSlot, long interval, int size) { |
---|
140 | if (timeSlot.getDurationInMillis() <= 0 || interval == 0 |
---|
141 | || timeSlot.getDurationInMillis() < interval) |
---|
142 | return null; |
---|
143 | if (!oldReservationList.equals(reservationList)) { |
---|
144 | reservationsTimestampsSet = createReservationsTimestampsSet(); |
---|
145 | reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet); |
---|
146 | calendar = createCalendar(timestampsSet, reservationsCalendar); |
---|
147 | oldReservationList = new ReservationList<Reservation>( |
---|
148 | reservationList); |
---|
149 | } |
---|
150 | if(calendar.size()==0){ |
---|
151 | return new TimeSlot(timeSlot.getStartTimeInMillis(), timeSlot.getStartTimeInMillis() + interval); |
---|
152 | } |
---|
153 | Date availableStartDate = getAvailableStartDate(calendar, timeSlot |
---|
154 | .getStartTime().getTime(), timeSlot.getEndTime().getTime(), |
---|
155 | interval, size); |
---|
156 | if (availableStartDate == null) |
---|
157 | return null; |
---|
158 | TimeSlot availableTimeSlot = new TimeSlot(availableStartDate.getTime(), |
---|
159 | availableStartDate.getTime() + interval); |
---|
160 | return availableTimeSlot; |
---|
161 | } |
---|
162 | |
---|
163 | private LinkedHashSet<Long> createTimestampsSet() { |
---|
164 | LinkedHashSet<Long> timestampsSet = new LinkedHashSet<Long>(); |
---|
165 | long time = timeInterval; |
---|
166 | long lastReservationEndTime = 0; |
---|
167 | if (reservationList.size() == 0) |
---|
168 | return timestampsSet; |
---|
169 | lastReservationEndTime = ((Reservation) reservationList.getLast()) |
---|
170 | .getSlot().getEndTime().getTime().getTime(); |
---|
171 | while (time < lastReservationEndTime) { |
---|
172 | timestampsSet.add(time); |
---|
173 | time = time + timeInterval; |
---|
174 | |
---|
175 | } |
---|
176 | timestampsSet.add(time); |
---|
177 | return timestampsSet; |
---|
178 | } |
---|
179 | |
---|
180 | private TreeMap<Long, Integer> createCalendar( |
---|
181 | LinkedHashSet<Long> timestampsSet, |
---|
182 | TreeMap<Long, Integer> reservationsCalendar) { |
---|
183 | TreeMap<Long, Integer> calendar = new TreeMap<Long, Integer>(); |
---|
184 | |
---|
185 | Iterator<Long> it = timestampsSet.iterator(); |
---|
186 | Integer value; |
---|
187 | while (it.hasNext()) { |
---|
188 | Long time = (Long) it.next(); |
---|
189 | TreeMap<Long, Integer> partOfCalendar = getContinousPartOfCalendar( |
---|
190 | reservationsCalendar, time - timeInterval, false, time, |
---|
191 | true); |
---|
192 | if (partOfCalendar.size() == 0) |
---|
193 | value = 0; |
---|
194 | else |
---|
195 | value = (Integer) Collections.max(partOfCalendar.values()); |
---|
196 | calendar.put(time, value); |
---|
197 | |
---|
198 | } |
---|
199 | |
---|
200 | return calendar; |
---|
201 | } |
---|
202 | |
---|
203 | private TreeMap<Long, Integer> createEmptyCalendar() { |
---|
204 | TreeMap<Long, Integer> emptyCalendar = new TreeMap<Long, Integer>(); |
---|
205 | |
---|
206 | long time=VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis() + timeInterval; |
---|
207 | while (time<VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis() + TIME_BORDER) { |
---|
208 | |
---|
209 | emptyCalendar.put(time, 0); |
---|
210 | time = time + timeInterval; |
---|
211 | |
---|
212 | } |
---|
213 | return emptyCalendar; |
---|
214 | } |
---|
215 | |
---|
216 | private LinkedHashSet<Long> createReservationsTimestampsSet() { |
---|
217 | LinkedHashSet<Long> reservationsTimestampsSet = new LinkedHashSet<Long>(); |
---|
218 | Iterator it = reservationList.iterator(); |
---|
219 | while (it.hasNext()) { |
---|
220 | Reservation reservation = (Reservation) it.next(); |
---|
221 | long time = reservation.getSlot().getStartTime().getTime() |
---|
222 | .getTime(); |
---|
223 | reservationsTimestampsSet.add(time); |
---|
224 | time = reservation.getSlot().getEndTime().getTime().getTime(); |
---|
225 | reservationsTimestampsSet.add(time); |
---|
226 | |
---|
227 | } |
---|
228 | return reservationsTimestampsSet; |
---|
229 | } |
---|
230 | |
---|
231 | private TreeMap<Long, Integer> createReservationsCalendar( |
---|
232 | LinkedHashSet<Long> reservationsTimestampsSet) { |
---|
233 | TreeMap<Long, Integer> reservationsCalendar = new TreeMap<Long, Integer>(); |
---|
234 | |
---|
235 | Iterator<Long> it = reservationsTimestampsSet.iterator(); |
---|
236 | while (it.hasNext()) { |
---|
237 | Long time = (Long) it.next(); |
---|
238 | reservationsCalendar.put(time, 0); |
---|
239 | } |
---|
240 | Iterator it2 = reservationList.iterator(); |
---|
241 | while (it2.hasNext()) { |
---|
242 | Reservation reservation = (Reservation) it2.next(); |
---|
243 | long startTime = reservation.getSlot().getStartTime().getTime() |
---|
244 | .getTime(); |
---|
245 | long endTime = reservation.getSlot().getEndTime().getTime() |
---|
246 | .getTime(); |
---|
247 | Iterator<Long> it3 = reservationsCalendar.keySet().iterator(); |
---|
248 | while (it3.hasNext()) { |
---|
249 | Long time = (Long) it3.next(); |
---|
250 | if (time > startTime && time <= endTime) { |
---|
251 | reservationsCalendar.put(time, |
---|
252 | (Integer) reservationsCalendar.get(time) |
---|
253 | + (int) reservation.getSlot() |
---|
254 | .getResourceUnit(Constants.PE) |
---|
255 | .getAmount()); |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | } |
---|
260 | return reservationsCalendar; |
---|
261 | } |
---|
262 | |
---|
263 | private int getMaxCalendarValue(TreeMap<Long, Integer> calendar, |
---|
264 | Date startDate, Date endDate) { |
---|
265 | long endDateInCalendar = getCeilKey(calendar, endDate.getTime()); |
---|
266 | TreeMap<Long, Integer> partOfCalendar = getContinousPartOfCalendar( |
---|
267 | calendar, startDate.getTime(), false, endDateInCalendar, true); |
---|
268 | Integer value; |
---|
269 | if (partOfCalendar.size() == 0) |
---|
270 | value = 0; |
---|
271 | else |
---|
272 | value = (Integer) Collections.max(partOfCalendar.values()); |
---|
273 | return value; |
---|
274 | } |
---|
275 | |
---|
276 | private long getCeilKey(TreeMap<Long, Integer> calendar, Long key) { |
---|
277 | Iterator<Long> it = calendar.keySet().iterator(); |
---|
278 | while (it.hasNext()) { |
---|
279 | Long time = (Long) it.next(); |
---|
280 | if (time >= key) |
---|
281 | return time; |
---|
282 | } |
---|
283 | return key; |
---|
284 | } |
---|
285 | |
---|
286 | private TreeMap<Long, Integer> getContinousPartOfCalendar( |
---|
287 | TreeMap<Long, Integer> calendar, long startKey, |
---|
288 | boolean includeStartKey, long endKey, boolean includeEndKey) { |
---|
289 | |
---|
290 | TreeMap<Long, Integer> partOfCalendar = new TreeMap<Long, Integer>(); |
---|
291 | SortedMap<Long, Integer> tailCalendar = calendar.tailMap(startKey); |
---|
292 | Iterator<Long> it = tailCalendar.keySet().iterator(); |
---|
293 | while (it.hasNext()) { |
---|
294 | Long time = (Long) it.next(); |
---|
295 | if (time > startKey && time < endKey) { |
---|
296 | partOfCalendar.put(time, getCalendarValue(calendar, time)); |
---|
297 | } |
---|
298 | if (time == startKey && includeStartKey) { |
---|
299 | partOfCalendar.put(time, getCalendarValue(calendar, time)); |
---|
300 | } |
---|
301 | if (time == endKey && includeEndKey) { |
---|
302 | partOfCalendar.put(time, getCalendarValue(calendar, time)); |
---|
303 | } |
---|
304 | if (includeStartKey) |
---|
305 | partOfCalendar.put(startKey, getCalendarValue(calendar, |
---|
306 | startKey)); |
---|
307 | if (includeEndKey) |
---|
308 | partOfCalendar.put(endKey, getCalendarValue(calendar, endKey)); |
---|
309 | if (time >= endKey) { |
---|
310 | break; |
---|
311 | } |
---|
312 | |
---|
313 | } |
---|
314 | return partOfCalendar; |
---|
315 | } |
---|
316 | |
---|
317 | private Date getAvailableStartDate(TreeMap<Long, Integer> calendar, |
---|
318 | Date startDate, Date endDate, long interval, int size) { |
---|
319 | if (endDate.getTime() > calendar.lastKey()) |
---|
320 | calendar.put(endDate.getTime(), 0); |
---|
321 | long endDateInCalendar = getCeilKey(calendar, endDate.getTime()); |
---|
322 | TreeMap<Long, Integer> partOfCalendar = getContinousPartOfCalendar( |
---|
323 | calendar, startDate.getTime(), false, endDateInCalendar, true); |
---|
324 | if (partOfCalendar.size() == 0) |
---|
325 | return startDate; |
---|
326 | TreeMap<Long, Integer> partOfCalendar2; |
---|
327 | { |
---|
328 | endDateInCalendar = getCeilKey(partOfCalendar, startDate.getTime() |
---|
329 | + (long) interval); |
---|
330 | partOfCalendar2 = getContinousPartOfCalendar(partOfCalendar, |
---|
331 | partOfCalendar.firstKey(), true, endDateInCalendar, true); |
---|
332 | Integer value; |
---|
333 | if (partOfCalendar2.size() == 0) |
---|
334 | value = 0; |
---|
335 | else |
---|
336 | value = (Integer) Collections.max(partOfCalendar2.values()); |
---|
337 | if (cpuCount - value >= size) { |
---|
338 | return startDate; |
---|
339 | } |
---|
340 | } |
---|
341 | Iterator<Long> it = partOfCalendar.keySet().iterator(); |
---|
342 | while (it.hasNext()) { |
---|
343 | |
---|
344 | Long time = (Long) it.next(); |
---|
345 | if (time + interval > endDate.getTime()) { |
---|
346 | return null; |
---|
347 | } |
---|
348 | endDateInCalendar = getCeilKey(partOfCalendar, time |
---|
349 | + (long) interval); |
---|
350 | |
---|
351 | partOfCalendar2 = getContinousPartOfCalendar(partOfCalendar, time, |
---|
352 | false, endDateInCalendar, true); |
---|
353 | |
---|
354 | Integer value; |
---|
355 | if (partOfCalendar2.size() == 0) |
---|
356 | value = 0; |
---|
357 | else |
---|
358 | value = (Integer) Collections.max(partOfCalendar2.values()); |
---|
359 | if (cpuCount - value >= size) { |
---|
360 | return new Date(time); |
---|
361 | } |
---|
362 | |
---|
363 | } |
---|
364 | return null; |
---|
365 | } |
---|
366 | |
---|
367 | public int getCalendarValue(TreeMap<Long, Integer> calendar, long time) { |
---|
368 | |
---|
369 | SortedMap<Long, Integer> tailCalendar = calendar.tailMap(time); |
---|
370 | Iterator<Long> it = tailCalendar.keySet().iterator(); |
---|
371 | while (it.hasNext()) { |
---|
372 | Long keyTime = (Long) it.next(); |
---|
373 | if (keyTime >= time) |
---|
374 | return calendar.get(keyTime); |
---|
375 | } |
---|
376 | return 0; |
---|
377 | |
---|
378 | } |
---|
379 | |
---|
380 | } |
---|