Debian Codegraph @

docs/examples/centralserver.py

raw symbols SCIP
1 """ centralserver.py
2
3 A time-shared computer consists of a single
4 central processing unit (CPU) and a number of
5 terminals. The operator of each terminal `thinks'
6 for a time (exponential, mean 100.0 sec) and then
7 submits a task to the computer with a service time
8 (exponential, mean 1.0 sec). The operator then
9 remains idle until the task completes service and
10 returns to him or her. The arriving tasks form a
11 single FCFS queue in front of the CPU.
12
13 Upon leaving the CPU a task is either finished
14 (probability 0.20) and returns to its operator
15 to begin another `think' time, or requires data
16 from a disk drive (probability 0.8). If a task
17 requires access to the disk, it joins a FCFS queue
18 before service (service time at the disk,
19 exponential, mean 1.39 sec). When finished with
20 the disk, a task returns to the CPU queue again
21 for another compute time (exp, mean 1.$ sec).
22
23 the objective is to measure the throughput of
24 the CPU (tasks per second)
25 """
26 from SimPy.Simulation import *
27 ## from SimPy.SimulationTrace import *
28 import random as ran
29
30 ## Model components ------------------------
31
32 class Task(Process):
33 """ A computer task requires at least
34 one use of the CPU and possibly accesses to a
35 disk drive."""
36 completed = 0
37 rate = 0.0
38 def execute(self,maxCompletions):
39 while Task.completed < maxCompletions:
40 self.debug(" starts thinking")
41 thinktime = ran.expovariate(1.0/MeanThinkTime)
42 yield hold,self,thinktime
43 self.debug(" request cpu")
44 yield request,self,cpu
45 self.debug(" got cpu")
46 CPUtime=ran.expovariate(1.0/MeanCPUTime)
47 yield hold,self,CPUtime
48 yield release,self,cpu
49 self.debug(" finish cpu")
50 while ran.random() < pDisk:
51 self.debug(" request disk")
52 yield request,self,disk
53 self.debug(" got disk")
54 disktime=ran.expovariate(1.0/MeanDiskTime)
55 yield hold,self,disktime
56 self.debug(" finish disk")
57 yield release,self,disk
58 self.debug(" request cpu")
59 yield request,self,cpu
60 self.debug(" got cpu")
61 CPUtime=ran.expovariate(1.0/MeanCPUTime)
62 yield hold,self,CPUtime
63 yield release,self,cpu
64 Task.completed += 1
65 self.debug(" completed %d tasks"%(Task.completed,))
66 Task.rate = Task.completed/float(now())
67
68 def debug(self,message):
69 FMT="%9.3f %s %s"
70 if DEBUG:
71 print FMT%(now(),self.name,message)
72
73
74 ## Model ------------------------------
75 def main():
76 initialize()
77 for i in range(Nterminals):
78 t = Task(name="task"+`i`)
79 activate(t,t.execute(MaxCompletions))
80 simulate(until = MaxrunTime)
81 return (now(),Task.rate)
82
83 ## Experiment data -------------------------
84
85 cpu = Resource(name='cpu')
86 disk = Resource(name='disk')
87 Nterminals = 3 ## Number of terminals = Tasks
88 pDisk = 0.8 ## prob. of going to disk
89 MeanThinkTime = 10.0 ## seconds
90 MeanCPUTime = 1.0 ## seconds
91 MeanDiskTime = 1.39 ## seconds
92
93 ran.seed(111113333)
94 MaxrunTime = 20000.0
95 MaxCompletions = 100
96 DEBUG = False
97
98
99 ## Experiment
100
101 result=main()
102
103 ## Analysis/output -------------------------
104
105 print 'centralserver'
106 print '%7.4f: CPU rate = %7.4f tasks per second'%result