-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessorHelper.py
executable file
·214 lines (181 loc) · 10.1 KB
/
ProcessorHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from Process import *
from output import *
class ProcessorHelper():
"""
Does arithmitic to calculate the current burst and add it to the average.
NOTE: YOU MUST TRACK START BURST AND LOG EVERY TIME A CONTEXT SWITCH OCCURS.
"""
processor = None
process = None
def __init__(self, processor):
self.processor = processor
self.process = self.processor.cProc
"""
Run the simulation through 'cycles' amount of iterations
NOTE: If cycles == -1, run till completion
"""
def run(self, cycles):
#If -1, run till completion
if(cycles == -1):
i = 0
writeOutput("time {0}ms: Simulator started for {1} {2}\n".format(int(self.processor.rTime), self.processor.algorithm, self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
while(not self.processor.procHelper.finished()):
#self.procPrinter.qPrint() #Print out processor queue's
self.processor.step() #Step the processor go to disc
i += 1
return 0
def finalize(self):
# CALULATE AVERAGES
wait = 0
turnAround = 0
for p in self.processor.donePool:
wait += p.waitTime #Increment wait time
turnAround += p.turnAround
# This is the final context switch
self.processor.cSwitchAmt += .5
# Account for last context switch and for the last step
self.processor.rTime += self.processor.cSwitchTime / 2
# Extract averages
self.processor.avgWait = float(wait) / self.processor.totalBurstCount
self.processor.avgTurnAround = float(turnAround) / self.processor.totalBurstCount
# simout
writeSimout("Algorithm {0}\n".format(self.processor.algorithm))
#writeSimout("-- Total runtime: {0}ms\n".format(self.processor.rTime))
#writeSimout("-- Total burst time: {0}ms\n".format(self.processor.totalBurst))
#writeSimout("-- Total wait time: {0}ms\n".format(wait))
writeSimout("-- average CPU burst time: {0} ms\n".format(round(self.processor.totalBurst / self.processor.totalBurstCount, 2)))
writeSimout("-- average wait time: {0} ms\n".format(round(self.processor.avgWait),2 ))
writeSimout("-- average turnaround time: {0} ms\n".format(round(self.processor.avgTurnAround, 2)))
writeSimout("-- total number of context switches: {0}\n".format(int(self.processor.cSwitchAmt)))
writeSimout("-- total number of preemptions: 0\n")
writeOutput("time {0}ms: Simulator ended for {1}\n".format(int(self.processor.rTime), self.processor.algorithm), self.processor.rTime, evenCPU())
writeOutput()
def logBurst(self):
# Calculate the current burst time
if (self.processor.rTime > self.processor.cProc.lastLogged):
bTime = self.processor.rTime - self.processor.startBurst
# Account for waitTime increasing an extra time
self.processor.cProc.waitTime -= 1
print("time {0}ms: Process {1} my wait time is {2}."
.format(self.processor.rTime, self.processor.cProc.label, self.processor.cProc.waitTime))
print("time {0}: Process {1} should have burst time {2}, and has actual burst time {3}\n".format(self.processor.rTime, self.processor.cProc.label, self.processor.cProc.burstTime, bTime))
self.processor.nBurst += 1
print("time {0}: I started bursting at {1}".format(self.processor.rTime, self.processor.startBurst))
print("time {0}:Total burst time = {1}, ".format(self.processor.rTime,self.processor.totalBurst))
self.processor.totalBurst = (self.processor.totalBurst + bTime)
print("is now = {0}\n".format(self.processor.totalBurst))
self.processor.cProc.lastLogged = self.processor.rTime
"""
Add a processor to the procPool
"""
def addProc(self, proc):
# Make sure proc is a process
assert (isinstance(proc, Process))
self.processor.procPool.append(proc)
self.processor.totalBurstCount += proc.burstCount
return
"""
Are we done yet? :Kappa:
"""
def finished(self):
if (self.processor.cProc is not None):
return ((self.processor.workQ.size() == 0) and (
len(self.processor.procPool) == 0) and self.processor.cProc.state == "FINISHED")
return False
"""
Do I need to Context switch
NOTE: if this returns none, simulation has ended
"""
def procReady(self):
self.processor.workQ.step(self.processor.algorithm)
# If cProc is None and:
# WorkQ empty -> False
# WorkQ !empty -> True
if (self.processor.cProc is None and not self.processor.workQ.isEmpty()):
return True
if (self.processor.cProc is None):
return False
############################################
# These should be the same no matter the algorithm
#
# if current proc is done or finished
if (self.processor.cProc.state == "FINISHED"):
# append to donePool
self.processor.donePool.append(self.processor.cProc)
writeOutput(
"time {0}ms: Process {1} terminated {2}\n".format(int(self.processor.rTime), self.processor.cProc.label, self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
print("FINISHED: time {0}ms: I'm about to start my burst bro".format(self.processor.rTime))
self.processor.procHelper.logBurst()
# Call step even though we're done so the process can calculate final
# average statistics
self.processor.cProc.step()
#print(self.processor.cProc.turnAround)
# Check to see if we are finished. If so, return None
if (self.processor.procHelper.finished()):
return None
# Not ended, return true
return True
# If its blocked we always context switch
elif (self.processor.cProc.state == "BLOCKED"):
if (self.processor.cProc.burstCount != 1):
# Next time to pre-empt is now + the time slice + context switch time
writeOutput(
"time {0}ms: Process {1} completed a CPU burst; {2} bursts to go {3}\n".format(
int(self.processor.rTime),
self.processor.cProc.label,
self.processor.cProc.burstCount,
self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
else:
# Next time to pre-empt is now + the time slice + context switch time
writeOutput(
"time {0}ms: Process {1} completed a CPU burst; {2} burst to go {3}\n".format(
int(self.processor.rTime),
self.processor.cProc.label,
self.processor.cProc.burstCount,
self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
print("BLOCKED: time {0}ms: I'm about to start my burst bro".format(self.processor.rTime))
self.processor.procHelper.logBurst()
return True
#
#
#
############################################
elif self.processor.algorithm == "FCFS":
# NEVER, only time is either finished or blocked cProc
return False
elif self.processor.algorithm == "SRT":
if (not self.processor.workQ.isEmpty()):
# If next proc.burst < this proc's burst
p = self.processor.workQ.peak()
if (p.burstTimeLeft < self.processor.cProc.burstTimeLeft):
if (self.processor.cProc.burstTimeLeft < self.processor.cProc.burstTime):
# For some reason the SRT algorithm is 1MS off on burstTime left... you know what to do
if (self.processor.algorithm == "SRT"):
writeOutput(
"time {0}ms: Process {1} started using the CPU with {2}ms remaining {3}\n".format(
int(self.processor.rTime), self.processor.cProc.label,
self.processor.cProc.burstTimeLeft,
self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
else:
writeOutput(
"time {0}ms: Process {1} started using the CPU with {2}ms remaining {3}\n".format(
int(self.processor.rTime), self.processor.cProc.label,
self.processor.cProc.burstTimeLeft,
self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
else:
# This is to ensure that the workQ is in its updated state before printing.
# it has no actual affect on the running since we will step it before
# any calculations are done
self.processor.workQ.step(self.processor.algorithm)
writeOutput(
"time {0}ms: Process {1} started using the CPU {2}\n".format(int(self.processor.rTime),
self.processor.cProc.label,
self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
return True
elif self.processor.algorithm == "RR":
if (self.processor.rTime >= self.processor.nxtSlice):
if (not self.processor.workQ.isEmpty()):
writeOutput("time {0}ms: Time slice expired; process {1} preempted with {2}ms to go {3}\n".format(
int(self.processor.rTime), self.processor.cProc.label, self.processor.cProc.burstTimeLeft, self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
return True
return False