| 1 |
.. highlight:: python |
| 2 |
:linenothreshold: 5 |
| 3 |
|
| 4 |
============================= |
| 5 |
Simulation with Tracing |
| 6 |
============================= |
| 7 |
|
| 8 |
:Authors: - Klaus Muller <[email protected]> |
| 9 |
:SimPy release: |release| |
| 10 |
:Web-site: http://simpy.sourceforge.net/ |
| 11 |
:Python-Version: 2.6 and later |
| 12 |
:Date: December 2011 |
| 13 |
|
| 14 |
.. contents:: Contents |
| 15 |
:depth: 2 |
| 16 |
|
| 17 |
Introduction |
| 18 |
------------------- |
| 19 |
The tracing utility has been developed to give users insight into the |
| 20 |
dynamics of the execution of SimPy simulation programs. It can help |
| 21 |
developers with testing and users with explaining SimPy models to themselves |
| 22 |
and others (e.g. for documentation or teaching purposes). |
| 23 |
|
| 24 |
Tracing *SimPy* programs |
| 25 |
------------------------- |
| 26 |
|
| 27 |
Tracing any *SimPy* program is as simple as replacing:: |
| 28 |
|
| 29 |
from SimPy.Simulation import * |
| 30 |
|
| 31 |
with:: |
| 32 |
|
| 33 |
from SimPy.SimulationTrace import * |
| 34 |
|
| 35 |
This will give a complete trace of all the scheduling statements |
| 36 |
executed during the program's execution. |
| 37 |
|
| 38 |
An even nicer way is to replace this import by:: |
| 39 |
|
| 40 |
if __debug__: |
| 41 |
from SimPy.SimulationTrace import * |
| 42 |
else: |
| 43 |
from SimPy.Simulation import * |
| 44 |
|
| 45 |
|
| 46 |
This gives a trace during the development and debugging. If one then |
| 47 |
executes the program with |
| 48 |
*python -O myprog.py*, tracing is switched off, and no run-time |
| 49 |
overhead is incurred. (*__debug__* is a |
| 50 |
global Python constant which is set to False by commandline options -O |
| 51 |
and -OO.) |
| 52 |
|
| 53 |
For the same reason, any user call to *trace* methods should be written |
| 54 |
as:: |
| 55 |
|
| 56 |
if __debug__: |
| 57 |
trace.ttext("This will only show during debugging") |
| 58 |
|
| 59 |
Here is an example (bank02.py from the Bank Tutorial):: |
| 60 |
|
| 61 |
from SimPy.SimulationTrace import * # <== changed for tracing |
| 62 |
##from SimPy.Simulation import * |
| 63 |
|
| 64 |
""" Simulate a single customer """ |
| 65 |
class Customer(Process): |
| 66 |
""" Customer arrives, looks around and leaves """ |
| 67 |
def __init__(self,name): |
| 68 |
Process.__init__(self) |
| 69 |
self.name = name |
| 70 |
|
| 71 |
def visit(self,timeInBank=0): |
| 72 |
print "%7.4f %s: Here I am"%(now(),self.name) |
| 73 |
yield hold,self,timeInBank |
| 74 |
print "%7.4f %s: I must leave"%(now(),self.name) |
| 75 |
|
| 76 |
def model(): |
| 77 |
initialize() |
| 78 |
c1=Customer(name="Klaus") |
| 79 |
activate(c1,c1.visit(timeInBank=10.0),delay=5.0) |
| 80 |
c2=Customer(name="Tony") |
| 81 |
activate(c2,c2.visit(timeInBank=8.0),delay=2.0) |
| 82 |
c3=Customer(name="Evelyn") |
| 83 |
activate(c3,c3.visit(timeInBank=20.0),delay=12.0) |
| 84 |
simulate(until=400.0) |
| 85 |
|
| 86 |
model() |
| 87 |
|
| 88 |
This program produces the following output:: |
| 89 |
|
| 90 |
0 activate <Klaus> at time: 5.0 prior: 0 |
| 91 |
0 activate <Tony> at time: 2.0 prior: 0 |
| 92 |
0 activate <Evelyn> at time: 12.0 prior: 0 |
| 93 |
2.0000 Tony: Here I am |
| 94 |
2.0 hold <Tony> delay: 8.0 |
| 95 |
5.0000 Klaus: Here I am |
| 96 |
5.0 hold <Klaus> delay: 10.0 |
| 97 |
10.0000 Tony: I must leave |
| 98 |
10.0 <Tony> terminated |
| 99 |
12.0000 Evelyn: Here I am |
| 100 |
12.0 hold <Evelyn> delay: 20.0 |
| 101 |
15.0000 Klaus: I must leave |
| 102 |
15.0 <Klaus> terminated |
| 103 |
32.0000 Evelyn: I must leave |
| 104 |
32.0 <Evelyn> terminated |
| 105 |
|
| 106 |
|
| 107 |
Another example:: |
| 108 |
|
| 109 |
""" bank09.py: Simulate customers arriving |
| 110 |
at random, using a Source requesting service |
| 111 |
from several clerks but a single queue |
| 112 |
with a random servicetime |
| 113 |
""" |
| 114 |
from __future__ import generators |
| 115 |
from SimPy.SimulationTrace import * |
| 116 |
from random import Random |
| 117 |
|
| 118 |
class Source(Process): |
| 119 |
""" Source generates customers randomly""" |
| 120 |
def __init__(self,seed=333): |
| 121 |
Process.__init__(self) |
| 122 |
self.SEED = seed |
| 123 |
|
| 124 |
def generate(self,number,interval): |
| 125 |
rv = Random(self.SEED) |
| 126 |
for i in range(number): |
| 127 |
c = Customer(name = "Customer%02d"%(i,)) |
| 128 |
activate(c,c.visit(timeInBank=12.0)) |
| 129 |
t = rv.expovariate(1.0/interval) |
| 130 |
yield hold,self,t |
| 131 |
|
| 132 |
class Customer(Process): |
| 133 |
""" Customer arrives, is served and leaves """ |
| 134 |
def __init__(self,name): |
| 135 |
Process.__init__(self) |
| 136 |
self.name = name |
| 137 |
|
| 138 |
def visit(self,timeInBank=0): |
| 139 |
arrive=now() |
| 140 |
print "%7.4f %s: Here I am "%(now(),self.name) |
| 141 |
yield request,self,counter |
| 142 |
wait=now()-arrive |
| 143 |
print "%7.4f %s: Waited %6.3f"%(now(),self.name,wait) |
| 144 |
tib = counterRV.expovariate(1.0/timeInBank) |
| 145 |
yield hold,self,tib |
| 146 |
yield release,self,counter |
| 147 |
print "%7.4f %s: Finished"%(now(),self.name) |
| 148 |
|
| 149 |
def model(counterseed=3939393): |
| 150 |
global counter,counterRV |
| 151 |
counter = Resource(name="Clerk",capacity = 2) #Lcapacity |
| 152 |
counterRV = Random(counterseed) |
| 153 |
initialize() |
| 154 |
sourceseed = 1133 |
| 155 |
source = Source(seed = sourceseed) |
| 156 |
activate(source,source.generate(5,10.0),0.0) |
| 157 |
simulate(until=400.0) |
| 158 |
|
| 159 |
model() |
| 160 |
|
| 161 |
This produces:: |
| 162 |
|
| 163 |
0 activate <a_process> at time: 0 prior: 0 |
| 164 |
0 activate <Customer00> at time: 0 prior: 0 |
| 165 |
0 hold <a_process> delay: 8.73140489458 |
| 166 |
0.0000 Customer00: Here I am |
| 167 |
0 request <Customer00> <Clerk> priority: default |
| 168 |
. . .waitQ: [] |
| 169 |
. . .activeQ: ['Customer00'] |
| 170 |
0.0000 Customer00: Waited 0.000 |
| 171 |
0 hold <Customer00> delay: 8.90355092634 |
| 172 |
8.73140489458 activate <Customer01> at time: 8.73140489458 prior: 0 |
| 173 |
8.73140489458 hold <a_process> delay: 8.76709801376 |
| 174 |
8.7314 Customer01: Here I am |
| 175 |
8.73140489458 request <Customer01> <Clerk> priority: default |
| 176 |
. . .waitQ: [] |
| 177 |
. . .activeQ: ['Customer00', 'Customer01'] |
| 178 |
8.7314 Customer01: Waited 0.000 |
| 179 |
8.73140489458 hold <Customer01> delay: 21.6676883425 |
| 180 |
8.90355092634 release <Customer00> <Clerk> |
| 181 |
. . .waitQ: [] |
| 182 |
. . .activeQ: ['Customer01'] |
| 183 |
8.9036 Customer00: Finished |
| 184 |
8.90355092634 <Customer00> terminated |
| 185 |
17.4985029083 activate <Customer02> at time: 17.4985029083 prior: 0 |
| 186 |
|
| 187 |
. . . . . |
| 188 |
|
| 189 |
And here is an example showing the trace output for compound yield statements:: |
| 190 |
|
| 191 |
from SimPy.SimulationTrace import * |
| 192 |
class Client(Process): |
| 193 |
def __init__(self,name): |
| 194 |
Process.__init__(self,name) |
| 195 |
def getServed(self,tank): |
| 196 |
yield (get,self,tank,10),(hold,self,1.5) |
| 197 |
if self.acquired(tank): |
| 198 |
print "%s got 10 %s"%(self.name,tank.unitName) |
| 199 |
else: |
| 200 |
print "%s reneged"%self.name |
| 201 |
class Filler(Process): |
| 202 |
def __init__(self,name): |
| 203 |
Process.__init__(self,name) |
| 204 |
def fill(self,tank): |
| 205 |
for i in range(3): |
| 206 |
yield hold,self,1 |
| 207 |
yield put,self,tank,10 |
| 208 |
initialize() |
| 209 |
tank=Level(name="Tank",unitName="gallons") |
| 210 |
for i in range(2): |
| 211 |
c=Client("Client %s"%i) |
| 212 |
activate(c,c.getServed(tank)) |
| 213 |
f=Filler("Tanker") |
| 214 |
activate(f,f.fill(tank)) |
| 215 |
simulate(until=10) |
| 216 |
|
| 217 |
It produces this output:: |
| 218 |
|
| 219 |
0 get <Client 0>to get: 10 gallons from <Tank> priority: default |
| 220 |
. . .getQ: ['Client 0'] |
| 221 |
. . .putQ: [] |
| 222 |
. . .in buffer: 0 |
| 223 |
|| RENEGE COMMAND: |
| 224 |
|| hold <Client 0> delay: 1.5 |
| 225 |
0 get <Client 1>to get: 10 gallons from <Tank> priority: default |
| 226 |
. . .getQ: ['Client 0', 'Client 1'] |
| 227 |
. . .putQ: [] |
| 228 |
. . .in buffer: 0 |
| 229 |
|| RENEGE COMMAND: |
| 230 |
|| hold <Client 1> delay: 1.5 |
| 231 |
0 hold <Tanker> delay: 1 |
| 232 |
0 hold <RENEGE-hold for Client 0> delay: 1.5 |
| 233 |
0 hold <RENEGE-hold for Client 1> delay: 1.5 |
| 234 |
1 put <Tanker> to put: 10 gallons into <Tank> priority: default |
| 235 |
. . .getQ: ['Client 1'] |
| 236 |
. . .putQ: [] |
| 237 |
. . .in buffer: 0 |
| 238 |
1 hold <Tanker> delay: 1 |
| 239 |
Client 0 got 10 gallons |
| 240 |
1 <Client 0> terminated |
| 241 |
1.5 <RENEGE-hold for Client 1> terminated |
| 242 |
Client 1 reneged |
| 243 |
1.5 <Client 1> terminated |
| 244 |
2 put <Tanker> to put: 10 gallons into <Tank> priority: default |
| 245 |
. . .getQ: [] |
| 246 |
. . .putQ: [] |
| 247 |
. . .in buffer: 10 |
| 248 |
2 hold <Tanker> delay: 1 |
| 249 |
3 put <Tanker> to put: 10 gallons into <Tank> priority: default |
| 250 |
. . .getQ: [] |
| 251 |
. . .putQ: [] |
| 252 |
. . .in buffer: 20 |
| 253 |
3 <Tanker> terminated |
| 254 |
|
| 255 |
In this example, the Client entities are requesting 10 gallons from the *tank* (a Level object). |
| 256 |
If they can't get them within 1.5 time units, they renege (give up waiting). |
| 257 |
The renege command parts of the compound statements (*hold,self,1.5*)are shown |
| 258 |
in the trace output with a prefix of || to indicate that they are being executed |
| 259 |
in parallel with the primary command part (*get,self,tank,10*). They are being |
| 260 |
executed by behind-the-scenes processes (e.g. *RENEGE-hold for Client 0*). |
| 261 |
|
| 262 |
The trace contains all calls of scheduling statements (**yield . . .**, |
| 263 |
**activate()**, **reactivate()**, **cancel()** and also the termination |
| 264 |
of processes (at completion of all their scheduling statements). For |
| 265 |
**yield request** and **yield release** calls, it provides also the queue |
| 266 |
status (waiting customers in *waitQ* and customers being served in *activeQ*. |
| 267 |
|
| 268 |
**trace.tchange()**: Changing the tracing |
| 269 |
------------------------------------------ |
| 270 |
|
| 271 |
**trace** is an instance of the **Trace** class defined in *SimulationTrace.py*. |
| 272 |
This gets automatically initialized upon importing *SimulationTrace*.. |
| 273 |
|
| 274 |
The tracing can be changed at runtime by calling **trace.tchange()** with one or |
| 275 |
more of the following named parameters: |
| 276 |
|
| 277 |
*start*: |
| 278 |
|
| 279 |
changes the tracing start time. Default is 0. Example: **trace.tchange(start=222.2)** |
| 280 |
to start tracing at simulation time 222.2. |
| 281 |
|
| 282 |
*end* : |
| 283 |
|
| 284 |
changes the tracing end time. Default is a very large number (hopefully past |
| 285 |
any simulation endtime you will ever use). |
| 286 |
Example: **trace.tchange(end=33)** to stop tracing at time 33. |
| 287 |
|
| 288 |
*toTrace*: |
| 289 |
|
| 290 |
changes the commands to be traced. Default is |
| 291 |
*["hold","activate","cancel","reactivate","passivate","request", |
| 292 |
"release","interrupt","waitevent","queueevent", |
| 293 |
"signal","waituntil","put","get","terminated"]*. |
| 294 |
Value must be a list containing |
| 295 |
one or more of those values in the default. Note: "terminated" causes |
| 296 |
tracing of all process terminations. |
| 297 |
Example: **trace.tchange(toTrace=["hold","activate"])** traces only the |
| 298 |
*yield hold* and *activate()* statements. |
| 299 |
|
| 300 |
*outfile*: |
| 301 |
|
| 302 |
redirects the trace out put to a file (default is *sys.stdout*). Value |
| 303 |
must be a file object open for writing. |
| 304 |
Example: **trace.tchange(outfile=open(r"c:\\python25\\bank02trace.txt","w"))** |
| 305 |
|
| 306 |
All these parameters can be combined. |
| 307 |
Example: **trace.tchange(start=45.0,toTrace=["terminated"])** will trace all |
| 308 |
process terminations from time 45.0 till the end of the simulation. |
| 309 |
|
| 310 |
The changes become effective at the time **trace.tchange()** is called. This |
| 311 |
implies for example that, if the call **trace.tchange(start=50)** is made at time |
| 312 |
100, it has no effect before *now()==100*. |
| 313 |
|
| 314 |
**treset()**: Resetting the trace to default values |
| 315 |
--------------------------------------------------- |
| 316 |
|
| 317 |
The trace parameters can be reset to their default values by calling **trace.treset()**. |
| 318 |
|
| 319 |
**trace.tstart()**, **trace.tstop()**: Enabling/disabling the trace |
| 320 |
--------------------------------------------------------------------- |
| 321 |
|
| 322 |
Calling **trace.tstart()** enables the tracing, and **trace.tstop()** |
| 323 |
disables it. Neither call changes any tracing parameters. |
| 324 |
|
| 325 |
|
| 326 |
**trace.ttext()**: Annotating the trace |
| 327 |
--------------------------------------- |
| 328 |
|
| 329 |
The event-by-event trace output is already very useful in showing the sequence |
| 330 |
in which SimPy's quasi-parallel processes are executed. |
| 331 |
|
| 332 |
For documentation, publishing or teaching purposes, it is even more useful |
| 333 |
if the trace output can be intermingled with output which not only |
| 334 |
shows the command executed, but also contextual information such as |
| 335 |
the values of state variables. If one outputs the reason *why* a specific |
| 336 |
scheduling command is executed, the trace can give a natural language description |
| 337 |
of the simulation scenario. |
| 338 |
|
| 339 |
For such in-line annotation, the **trace.ttext(<string>)** method is |
| 340 |
available. It provides a string which is output together with the trace of |
| 341 |
the next scheduling statement. This string is valid *only* for the scheduling |
| 342 |
statement following it. |
| 343 |
|
| 344 |
Example:: |
| 345 |
|
| 346 |
class Bus(Process): |
| 347 |
def __init__(self,name): |
| 348 |
Process.__init__(self,name) |
| 349 |
|
| 350 |
def operate(self,repairduration=0): |
| 351 |
tripleft = 1000 |
| 352 |
while tripleft > 0: |
| 353 |
trace.ttext("Try to go for %s"%tripleft) |
| 354 |
yield hold,self,tripleft |
| 355 |
if self.interrupted(): |
| 356 |
tripleft=self.interruptLeft |
| 357 |
self.interruptReset() |
| 358 |
trace.ttext("Start repair taking %s time units"%repairduration) |
| 359 |
yield hold,self,repairduration |
| 360 |
else: |
| 361 |
break # no breakdown, ergo bus arrived |
| 362 |
trace.ttext("<%s> has arrived"%self.name) |
| 363 |
|
| 364 |
class Breakdown(Process): |
| 365 |
def __init__(self,myBus): |
| 366 |
Process.__init__(self,name="Breakdown "+myBus.name) |
| 367 |
self.bus=myBus |
| 368 |
|
| 369 |
def breakBus(self,interval): |
| 370 |
|
| 371 |
while True: |
| 372 |
trace.ttext("Breakdown process waiting for %s"%interval) |
| 373 |
yield hold,self,interval |
| 374 |
if self.bus.terminated(): break |
| 375 |
trace.ttext("Breakdown of %s"%self.bus.name) |
| 376 |
self.interrupt(self.bus) |
| 377 |
|
| 378 |
print"\n\n+++test_interrupt" |
| 379 |
initialize() |
| 380 |
b=Bus("Bus 1") |
| 381 |
trace.ttext("Start %s"%b.name) |
| 382 |
activate(b,b.operate(repairduration=20)) |
| 383 |
br=Breakdown(b) |
| 384 |
trace.ttext("Start the Breakdown process for %s"%b.name) |
| 385 |
activate(br,br.breakBus(200)) |
| 386 |
trace.start=100 |
| 387 |
print simulate(until=4000) |
| 388 |
|
| 389 |
This produces: |
| 390 |
|
| 391 |
0 activate <Bus 1> at time: 0 prior: 0 |
| 392 |
---- Start Bus 1 |
| 393 |
0 activate <Breakdown Bus 1> at time: 0 prior: 0 |
| 394 |
---- Start the Breakdown process for Bus 1 |
| 395 |
200 reactivate <Bus 1> time: 200 prior: 0 |
| 396 |
200 interrupt by: <Breakdown Bus 1> of: <Bus 1> |
| 397 |
---- Breakdown of Bus 1 |
| 398 |
200 hold <Breakdown Bus 1> delay: 200 |
| 399 |
---- Breakdown process waiting for 200 |
| 400 |
200 hold <Bus 1> delay: 20 |
| 401 |
---- Start repair taking 20 time units |
| 402 |
220 hold <Bus 1> delay: 800 |
| 403 |
---- Try to go for 800 |
| 404 |
400 reactivate <Bus 1> time: 400 prior: 0 |
| 405 |
400 interrupt by: <Breakdown Bus 1> of: <Bus 1> |
| 406 |
---- Breakdown of Bus 1 |
| 407 |
400 hold <Breakdown Bus 1> delay: 200 |
| 408 |
---- Breakdown process waiting for 200 |
| 409 |
400 hold <Bus 1> delay: 20 |
| 410 |
---- Start repair taking 20 time units |
| 411 |
420 hold <Bus 1> delay: 620 |
| 412 |
|
| 413 |
. . . . . |
| 414 |
|
| 415 |
The line starting with "----" is the comment related to the command traced |
| 416 |
in the preceding output line. |
| 417 |
|
| 418 |
Nice output of class instances |
| 419 |
------------------------------ |
| 420 |
|
| 421 |
After the import of *SimPy.SimulationTrace*, all instances of classes |
| 422 |
*Process* and *Resource* (and all their subclasses) have a nice string |
| 423 |
representation like so:: |
| 424 |
|
| 425 |
>>> class Bus(Process): |
| 426 |
... def __init__(self,id): |
| 427 |
... Process.__init__(self,name=id) |
| 428 |
... self.typ="Bus" |
| 429 |
... |
| 430 |
>>> b=Bus("Line 15") |
| 431 |
>>> b |
| 432 |
<Instance of Bus, id 21860960: |
| 433 |
.name=Line 15 |
| 434 |
.typ=Bus |
| 435 |
> |
| 436 |
>>> |
| 437 |
|
| 438 |
This can be handy in statements like **trace.ttext("Status of %s"%b)**. |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
.. |
| 443 |
Local Variables: |
| 444 |
mode: rst |
| 445 |
indent-tabs-mode: nil |
| 446 |
sentence-end-double-space: t |
| 447 |
fill-column: 70 |
| 448 |
End: |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|