File: centralserver_OO.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (106 lines) | stat: -rw-r--r-- 3,646 bytes parent folder | download | duplicates (4)
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
""" centralserver.py

A time-shared computer consists of a single
central processing unit (CPU) and a number of
terminals. The operator of each terminal `thinks'
for a time (exponential, mean 100.0 sec) and then
submits a task to the computer with a service time
(exponential, mean 1.0 sec). The operator then
remains idle until the task completes service and
returns to him or her. The arriving tasks form a
single FCFS queue in front of the CPU.

Upon leaving the CPU a task is either finished
(probability 0.20) and returns to its operator
to begin another `think' time, or requires data
from a disk drive (probability 0.8). If a task
requires access to the disk, it joins a FCFS queue
before service (service time at the disk,
exponential, mean 1.39 sec). When finished with
the disk, a task returns to the CPU queue again
for another compute time (exp, mean 1.$ sec).

the objective is to measure the throughput of
the CPU (tasks per second)
"""
from SimPy.Simulation import *
## from SimPy.SimulationTrace import *
import random as ran

## Model components ------------------------

class Task(Process):
    """ A computer  task  requires at least
    one use of the CPU and possibly accesses to a
    disk drive."""
    completed = 0
    rate = 0.0
    def execute(self,maxCompletions):
        while Task.completed < MaxCompletions:
            self.debug(" starts thinking")
            thinktime = ran.expovariate(1.0/MeanThinkTime)
            yield hold,self,thinktime
            self.debug(" request cpu")
            yield request,self,self.sim.cpu
            self.debug(" got cpu")
            CPUtime=ran.expovariate(1.0/MeanCPUTime)
            yield hold,self,CPUtime
            yield release,self,self.sim.cpu
            self.debug(" finish cpu")
            while ran.random() < pDisk:
                self.debug(" request disk")
                yield request,self,self.sim.disk
                self.debug(" got disk")
                disktime=ran.expovariate(1.0/MeanDiskTime)
                yield hold,self,disktime
                self.debug(" finish disk")
                yield release,self,self.sim.disk
                self.debug(" request cpu")
                yield request,self,self.sim.cpu
                self.debug(" got cpu")
                CPUtime=ran.expovariate(1.0/MeanCPUTime)
                yield hold,self,CPUtime
                yield release,self,self.sim.cpu
            Task.completed += 1
        self.debug(" completed %d tasks"%(Task.completed,))
        Task.rate = Task.completed/float(self.sim.now())

    def debug(self,message):
        FMT="%9.3f %s %s"
        if DEBUG:
            print FMT%(self.sim.now(),self.name,message)
            
    
## Model ------------------------------
class CentralServerModel(Simulation):
    def run(self):
        self.initialize()
        self.cpu  = Resource(name='cpu',sim=self)
        self.disk = Resource(name='disk',sim=self)
        for i in range(Nterminals):
            t = Task(name="task"+`i`,sim=self)
            self.activate(t,t.execute(MaxCompletions))
        self.simulate(until = MaxrunTime)
        return (self.now(),Task.rate)

## Experiment data -------------------------
Nterminals = 3       ## Number of terminals = Tasks
pDisk    = 0.8       ## prob. of going to disk
MeanThinkTime = 10.0 ## seconds
MeanCPUTime = 1.0    ## seconds
MeanDiskTime = 1.39  ## seconds

ran.seed(111113333)
MaxrunTime = 20000.0
MaxCompletions = 100
DEBUG = False


## Experiment

result = CentralServerModel().run()

## Analysis/output -------------------------

print 'centralserver'
print '%7.4f: CPU rate = %7.4f tasks per second'%result