package simulator.lists; import gridsim.gssim.Constants; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.SortedMap; import java.util.TreeMap; import schedframe.implementation.Reservation; import schedframe.implementation.TimeResourceAllocation; import schedframe.implementation.TimeSlot; import schedframe.resUnits.PEResourceUnit; import simulator.VirtualClock; public class ResourceUsageDynamic { private static final long TIME_BORDER=1000 * 60 * 60 * 24 * 7 * 2;//two weeks private ReservationList reservationList; private long timeInterval; private int cpuCount; public ResourceUsageDynamic(ReservationList reservationList, long timeInterval) { this.reservationList = reservationList; this.timeInterval = timeInterval; this.cpuCount = reservationList.cpuCount; } public TimeResourceAllocation[] getResourceUsage() { LinkedHashSet reservationsTimestampsSet = createReservationsTimestampsSet(); //System.out.println("reservationsTimestampsSet "+reservationsTimestampsSet); TreeMap reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet); //System.out.println("reservationsCalendar "+reservationsCalendar); LinkedHashSet timestampsSet = createTimestampsSet(); //System.out.println("timestampsSet "+timestampsSet); TreeMap calendar = createCalendar(timestampsSet, reservationsCalendar); TimeResourceAllocation[] slots; if(calendar.size()==0){ TreeMap emptyCalendar = createEmptyCalendar(); slots = new TimeResourceAllocation[emptyCalendar.size()+1]; Iterator it = emptyCalendar.keySet().iterator(); int counter = 0; Calendar endTime = Calendar.getInstance(); while (it.hasNext()) { Long time = (Long) it.next(); PEResourceUnit resUnit = new PEResourceUnit(cpuCount-emptyCalendar.get(time)); Calendar startTime = Calendar.getInstance(); startTime.setTimeInMillis(time - timeInterval); endTime = Calendar.getInstance(); endTime.setTimeInMillis(time); slots[counter] = new TimeResourceAllocation(resUnit, startTime, endTime); counter++; } PEResourceUnit resUnit= new PEResourceUnit(cpuCount); Calendar maxEndTime=Calendar.getInstance(); maxEndTime.setTimeInMillis(VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis()+TIME_BORDER); slots[counter] = new TimeResourceAllocation(resUnit, endTime, maxEndTime); return slots; /*slots = new TimeResourceAllocation[1]; PEResourceUnit resUnit = new PEResourceUnit(cpuCount); Calendar startTime=VirtualClock.getCurrentGlobalTimeCalendar(); Calendar endTime=Calendar.getInstance(); endTime.setTimeInMillis(VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis()+week); slots[0]=(new TimeResourceAllocation(resUnit, startTime, endTime)); return slots;*/ } else { slots = new TimeResourceAllocation[calendar .size()+1]; Iterator it = calendar.keySet().iterator(); int counter = 0; Calendar endTime = Calendar.getInstance(); while (it.hasNext()) { Long time = (Long) it.next(); PEResourceUnit resUnit = new PEResourceUnit(cpuCount-calendar.get(time)); Calendar startTime = Calendar.getInstance(); startTime.setTimeInMillis(time - timeInterval); endTime = Calendar.getInstance(); endTime.setTimeInMillis(time); slots[counter] = new TimeResourceAllocation(resUnit, startTime, endTime); counter++; } PEResourceUnit resUnit= new PEResourceUnit(cpuCount); Calendar maxEndTime=Calendar.getInstance(); maxEndTime.setTimeInMillis(VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis()+TIME_BORDER); slots[counter] = new TimeResourceAllocation(resUnit, endTime, maxEndTime); return slots; } } public boolean checkReservation(TimeSlot timeSlot, int size) { if (timeSlot.getDurationInMillis() <= 0) return false; LinkedHashSet reservationsTimestampsSet = createReservationsTimestampsSet(); TreeMap reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet); LinkedHashSet timestampsSet = createTimestampsSet(); TreeMap calendar = createCalendar(timestampsSet, reservationsCalendar); if(calendar.size()==0){ if (cpuCount >= size) return true; } int value = getMaxCalendarValue(calendar, timeSlot.getStartTime() .getTime(), timeSlot.getEndTime().getTime()); if (cpuCount - value >= size) return true; return false; } public TimeSlot findInterval(TimeSlot timeSlot, long interval, int size) { if (timeSlot.getDurationInMillis() <= 0 || interval == 0 || timeSlot.getDurationInMillis() < interval) return null; LinkedHashSet reservationsTimestampsSet = createReservationsTimestampsSet(); TreeMap reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet); LinkedHashSet timestampsSet = createTimestampsSet(); TreeMap calendar = createCalendar(timestampsSet, reservationsCalendar); if(calendar.size()==0){ return new TimeSlot(timeSlot.getStartTimeInMillis(), timeSlot.getStartTimeInMillis() + interval); } Date availableStartDate = getAvailableStartDate(calendar, timeSlot .getStartTime().getTime(), timeSlot.getEndTime().getTime(), interval, size); if (availableStartDate == null) return null; TimeSlot availableTimeSlot = new TimeSlot(availableStartDate.getTime(), availableStartDate.getTime() + interval); return availableTimeSlot; } private LinkedHashSet createTimestampsSet() { LinkedHashSet timestampsSet = new LinkedHashSet(); long time = VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis() + timeInterval; long lastReservationEndTime = 0; if(reservationList.size()==0) return timestampsSet; lastReservationEndTime = ((Reservation) reservationList.getLast()) .getSlot().getEndTime().getTime().getTime(); while (time < lastReservationEndTime) { timestampsSet.add(time); time = time + timeInterval; } timestampsSet.add(time); //timestampsSet.add(Long.MAX_VALUE); return timestampsSet; } private TreeMap createCalendar( LinkedHashSet timestampsSet, TreeMap reservationsCalendar) { TreeMap calendar = new TreeMap(); Iterator it = timestampsSet.iterator(); Integer value; while (it.hasNext()) { Long time = (Long) it.next(); TreeMap partOfCalendar = getContinousPartOfCalendar( reservationsCalendar, time - timeInterval, false, time, true); if (partOfCalendar.size() == 0) value = 0; else value = (Integer) Collections.max(partOfCalendar.values()); calendar.put(time, value); } return calendar; } private TreeMap createEmptyCalendar() { TreeMap emptyCalendar = new TreeMap(); long time=VirtualClock.getCurrentGlobalTimeCalendar().getTimeInMillis() + timeInterval; while (time createReservationsTimestampsSet() { LinkedHashSet reservationsTimestampsSet = new LinkedHashSet(); Iterator it = reservationList.iterator(); while (it.hasNext()) { Reservation reservation = (Reservation) it.next(); long time = reservation.getSlot().getStartTime().getTime() .getTime(); reservationsTimestampsSet.add(time); time = reservation.getSlot().getEndTime().getTime().getTime(); reservationsTimestampsSet.add(time); } return reservationsTimestampsSet; } private TreeMap createReservationsCalendar( LinkedHashSet reservationsTimestampsSet) { TreeMap reservationsCalendar = new TreeMap(); Iterator it = reservationsTimestampsSet.iterator(); while (it.hasNext()) { Long time = (Long) it.next(); reservationsCalendar.put(time, 0); } Iterator it2 = reservationList.iterator(); while (it2.hasNext()) { Reservation reservation = (Reservation) it2.next(); long startTime = reservation.getSlot().getStartTime().getTime() .getTime(); long endTime = reservation.getSlot().getEndTime().getTime() .getTime(); Iterator it3 = reservationsCalendar.keySet().iterator(); while (it3.hasNext()) { Long time = (Long) it3.next(); if (time > startTime && time <= endTime) { reservationsCalendar.put(time, (Integer) reservationsCalendar.get(time) + (int) reservation.getSlot() .getResourceUnit(Constants.PE) .getAmount()); } } } return reservationsCalendar; } private int getMaxCalendarValue(TreeMap calendar, Date startDate, Date endDate) { long endDateInCalendar = getCeilKey(calendar, endDate.getTime()); TreeMap partOfCalendar = getContinousPartOfCalendar( calendar, startDate.getTime(), false, endDateInCalendar, true); Integer value; if (partOfCalendar.size() == 0) value = 0; else value = (Integer) Collections.max(partOfCalendar.values()); return value; } private long getCeilKey(TreeMap calendar, Long key) { Iterator it = calendar.keySet().iterator(); while (it.hasNext()) { Long time = (Long) it.next(); if (time >= key) return time; } return key; } private TreeMap getContinousPartOfCalendar( TreeMap calendar, long startKey, boolean includeStartKey, long endKey, boolean includeEndKey) { TreeMap partOfCalendar = new TreeMap(); SortedMap tailCalendar = calendar.tailMap(startKey); Iterator it = tailCalendar.keySet().iterator(); while (it.hasNext()) { Long time = (Long) it.next(); if (time > startKey && time < endKey) { partOfCalendar.put(time, getCalendarValue(calendar, time)); } if (time == startKey && includeStartKey) { partOfCalendar.put(time, getCalendarValue(calendar, time)); } if (time == endKey && includeEndKey) { partOfCalendar.put(time, getCalendarValue(calendar, time)); } if (includeStartKey) partOfCalendar.put(startKey, getCalendarValue(calendar, startKey)); if (includeEndKey) partOfCalendar.put(endKey, getCalendarValue(calendar, endKey)); if (time >= endKey) { break; } } return partOfCalendar; } private Date getAvailableStartDate(TreeMap calendar, Date startDate, Date endDate, long interval, int size) { if (endDate.getTime() > calendar.lastKey()) calendar.put(endDate.getTime(), 0); long endDateInCalendar = getCeilKey(calendar, endDate.getTime()); TreeMap partOfCalendar = getContinousPartOfCalendar( calendar, startDate.getTime(), false, endDateInCalendar, true); if (partOfCalendar.size() == 0) return startDate; TreeMap partOfCalendar2; { endDateInCalendar = getCeilKey(partOfCalendar, startDate.getTime() + (long) interval); partOfCalendar2 = getContinousPartOfCalendar(partOfCalendar, partOfCalendar.firstKey(), true, endDateInCalendar, true); Integer value; if (partOfCalendar2.size() == 0) value = 0; else value = (Integer) Collections.max(partOfCalendar2.values()); if (cpuCount - value >= size) { return startDate; } } Iterator it = partOfCalendar.keySet().iterator(); while (it.hasNext()) { Long time = (Long) it.next(); if (time + interval > endDate.getTime()) { return null; } endDateInCalendar = getCeilKey(partOfCalendar, time + (long) interval); partOfCalendar2 = getContinousPartOfCalendar(partOfCalendar, time, false, endDateInCalendar, true); Integer value; if (partOfCalendar2.size() == 0) value = 0; else value = (Integer) Collections.max(partOfCalendar2.values()); if (cpuCount - value >= size) { return new Date(time); } } return null; } public int getCalendarValue(TreeMap calendar, long time) { SortedMap tailCalendar = calendar.tailMap(time); Iterator it = tailCalendar.keySet().iterator(); while (it.hasNext()) { Long keyTime = (Long) it.next(); if (keyTime >= time) return calendar.get(keyTime); } return 0; } }