source: xssim/trunk/src/simulator/lists/ResourceUsageDynamic.java @ 104

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