1 | package de.uka.ipd.sdq.scheduler.resources.active; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import de.uka.ipd.sdq.scheduler.IActiveResource; |
7 | import de.uka.ipd.sdq.scheduler.IRunningProcess; |
8 | import de.uka.ipd.sdq.scheduler.SchedulerModel; |
9 | import de.uka.ipd.sdq.scheduler.entities.SchedulerEntity; |
10 | import de.uka.ipd.sdq.scheduler.events.SchedulingEvent; |
11 | import de.uka.ipd.sdq.scheduler.events.SchedulingInterruptEvent; |
12 | import de.uka.ipd.sdq.scheduler.resources.IResourceInstance; |
13 | import de.uka.ipd.sdq.scheduler.sensors.IActiveResourceStateSensor; |
14 | |
15 | public class SimResourceInstance extends SchedulerEntity implements IResourceInstance { |
16 | |
17 | private int number; |
18 | private IActiveResource containing_resource; |
19 | private IRunningProcess running_process; |
20 | |
21 | /** |
22 | * The variable last_running_process is necessary to fake (!!!) changes in |
23 | * the association of light weight processes (LWPs) and threads, since the |
24 | * current scheduler simulator does not reflect this association (it just |
25 | * models 'processes'). For a solid model, this distinction is mandatory. |
26 | * |
27 | * |
28 | * The last_running_process is used to fake the different treatment of |
29 | * threads in load balancing. |
30 | * |
31 | * So, what's the difficulty there? |
32 | * |
33 | * As soon as a thread is put to sleep, its LWP looks for a new thread (of |
34 | * the same heavyweight process) to execute for its remaining timeslice. It |
35 | * prefers the last thread in the busiest run queue. This leads to a new |
36 | * LWP-to-thread mapping. Basically, the LWPs of both threads are switched. |
37 | * |
38 | * This behavior differs significantly from process load balancing, where |
39 | * the LWP is moved to a new processor! |
40 | * |
41 | */ |
42 | private IRunningProcess last_running_process; |
43 | |
44 | private SchedulingEvent scheduling_event; |
45 | private boolean isScheduling; |
46 | private List<IActiveResourceStateSensor> resourceObserverList = new ArrayList<IActiveResourceStateSensor>(); |
47 | |
48 | public SimResourceInstance(SchedulerModel model, int number, IActiveResource containing_resource) { |
49 | super(model, SimResourceInstance.class.getName()); |
50 | this.number = number; |
51 | this.containing_resource = containing_resource; |
52 | // Initialise this at start instead of container for multiple Simulation |
53 | // runs with different simulator instances... |
54 | // this.scheduling_event = new |
55 | // SchedulingEvent((SimActiveResource)containing_resource,this); |
56 | this.running_process = null; |
57 | this.isScheduling = false; |
58 | } |
59 | |
60 | public IRunningProcess getRunningProcess() { |
61 | return running_process; |
62 | } |
63 | |
64 | public void release() { |
65 | this.last_running_process = running_process; |
66 | this.running_process = null; |
67 | updateObservers(); |
68 | } |
69 | |
70 | private void updateObservers() { |
71 | for (IActiveResourceStateSensor observer : resourceObserverList) { |
72 | observer.update(getQueueLength(), getId()); |
73 | } |
74 | } |
75 | |
76 | public void addObserver(IActiveResourceStateSensor observer) { |
77 | resourceObserverList.add(observer); |
78 | } |
79 | |
80 | public void removeObserver(IActiveResourceStateSensor observer) { |
81 | resourceObserverList.remove(observer); |
82 | } |
83 | |
84 | public boolean processAssigned() { |
85 | return running_process != null; |
86 | } |
87 | |
88 | public void assign(IRunningProcess process) { |
89 | assert !this.processAssigned() : "There is already a process executing on resource instance " |
90 | + this; |
91 | running_process = process; |
92 | updateObservers(); |
93 | } |
94 | |
95 | public String getName() { |
96 | return containing_resource.getName() + "_" + number; |
97 | } |
98 | |
99 | public void scheduleSchedulingEvent(double time) { |
100 | cancelSchedulingEvent(); |
101 | scheduling_event.schedule(this, time); |
102 | } |
103 | |
104 | public void schedulingInterrupt(double time) { |
105 | new SchedulingInterruptEvent(getModel(), (SimActiveResource) containing_resource).schedule(this, time); |
106 | } |
107 | |
108 | public void cancelSchedulingEvent() { |
109 | scheduling_event.removeEvent(); |
110 | } |
111 | |
112 | @Override |
113 | public String toString() { |
114 | return getName(); |
115 | } |
116 | |
117 | @Override |
118 | public boolean equals(Object obj) { |
119 | if (obj instanceof SimResourceInstance) { |
120 | SimResourceInstance instance = (SimResourceInstance) obj; |
121 | return this.getId() == instance.getId(); |
122 | } |
123 | return false; |
124 | } |
125 | |
126 | public int getId() { |
127 | return number; |
128 | } |
129 | |
130 | @Override |
131 | public int hashCode() { |
132 | return (getName() + getId()).hashCode(); |
133 | } |
134 | |
135 | public double getNextSchedEventTime() { |
136 | double simTime = getModel().getSimulationControl().getCurrentSimulationTime(); |
137 | double eventTime = scheduling_event.scheduledAtTime(); |
138 | return eventTime - simTime; |
139 | } |
140 | |
141 | public void start() { |
142 | this.scheduling_event = new SchedulingEvent(getModel(), |
143 | (SimActiveResource) containing_resource); |
144 | scheduling_event.schedule(this, 0); |
145 | } |
146 | |
147 | public void stop() { |
148 | scheduling_event.removeEvent(); |
149 | } |
150 | |
151 | public void setIsScheduling(boolean b) { |
152 | isScheduling = b; |
153 | } |
154 | |
155 | public boolean isScheduling() { |
156 | return isScheduling; |
157 | } |
158 | |
159 | public boolean isIdle() { |
160 | return running_process == null; |
161 | } |
162 | |
163 | public IRunningProcess getLastRunningProcess() { |
164 | return last_running_process; |
165 | } |
166 | |
167 | public int getQueueLength() { |
168 | return containing_resource.getQueueLengthFor(this); |
169 | } |
170 | |
171 | } |