001: /**
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found at $PEGASUS_HOME/GTPL or
004: * http://www.globus.org/toolkit/download/license.html.
005: * This notice must appear in redistributions of this file
006: * with or without modification.
007: *
008: * Redistributions of this Software, with or without modification, must reproduce
009: * the GTPL in:
010: * (1) the Software, or
011: * (2) the Documentation or
012: * some other similar material which is provided with the Software (if any).
013: *
014: * Copyright 1999-2004
015: * University of Chicago and The University of Southern California.
016: * All rights reserved.
017: */package org.griphyn.cPlanner.selector.site.heft;
018:
019: import java.util.List;
020: import java.util.LinkedList;
021: import java.util.ListIterator;
022:
023: /**
024: * A data class that models a site as a collection of processors.
025: * The number of processors can only be specified in the constructor.
026: *
027: * @author Karan Vahi
028: * @version $Revision: 297 $
029: */
030: public class Site {
031:
032: /**
033: * The number of processors making up a site.
034: */
035: private int mNumProcessors;
036:
037: /**
038: * A list of processors making up the site.
039: */
040: private List mProcessors;
041:
042: /**
043: * The index to the processor that is to be used for scheduling a job.
044: */
045: private int mCurrentProcessorIndex;
046:
047: /**
048: * The logical name assigned to the site.
049: */
050: private String mName;
051:
052: /**
053: * The default constructor.
054: *
055: * @param name the name to be assigned to the site.
056: */
057: public Site(String name) {
058: mName = name;
059: mNumProcessors = 0;
060: mProcessors = new LinkedList();
061: mCurrentProcessorIndex = 0;
062: }
063:
064: /**
065: * The overloaded constructor.
066: *
067: * @param name the name to be assigned to the site.
068: * @param num the number of processors.
069: */
070: public Site(String name, int num) {
071: mName = name;
072: mNumProcessors = num;
073: mCurrentProcessorIndex = -1;
074: mProcessors = new LinkedList();
075: }
076:
077: /**
078: * Returns the earliest time the site is available for scheduling
079: * a job. It is non insertion based scheduling policy.
080: *
081: * @param start the time at which to start the search.
082: *
083: * @return long
084: */
085: public long getAvailableTime(long start) {
086: int num = 0;
087:
088: //each processor is checked for start of list
089: long result = Long.MAX_VALUE;
090: long current;
091: ListIterator it;
092: for (it = mProcessors.listIterator(); it.hasNext(); num++) {
093: Processor p = (Processor) it.next();
094: current = p.getAvailableTime(start);
095: if (current < result) {
096: //tentatively schedule a job on the processor
097: result = current;
098: mCurrentProcessorIndex = num;
099: }
100: }
101:
102: if (result > start && num < mNumProcessors) {
103: //tentatively schedule a job to an unused processor as yet.
104: result = start;
105: mCurrentProcessorIndex = num++;
106: //if using a normal iterator
107: //could use addLast() method
108: it.add(new Processor());
109:
110: }
111:
112: //sanity check
113: if (result == Long.MAX_VALUE) {
114: throw new RuntimeException("Unable to scheduled to site");
115: }
116:
117: return result;
118: }
119:
120: /**
121: * Schedules a job to the site.
122: *
123: * @param start the start time of the job.
124: * @param end the end time for the job
125: */
126: public void scheduleJob(long start, long end) {
127: //sanity check
128: if (mCurrentProcessorIndex == -1) {
129: throw new RuntimeException(
130: "Invalid State. The job needs to be tentatively scheduled first!");
131: }
132:
133: Processor p = (Processor) mProcessors
134: .get(mCurrentProcessorIndex);
135: p.scheduleJob(start, end);
136:
137: //reset the index
138: mCurrentProcessorIndex = -1;
139: }
140:
141: /**
142: * Returns the name of the site.
143: *
144: * @return name of the site.
145: */
146: public String getName() {
147: return mName;
148: }
149:
150: /**
151: * Returns the number of available processors.
152: *
153: * @return number of available processors.
154: */
155: public int getAvailableProcessors() {
156: return this.mNumProcessors;
157: }
158: }
|