-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_buf_proc.cr
187 lines (145 loc) · 3.51 KB
/
gen_buf_proc.cr
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
require "../src/quartz"
require "csv"
alias Duration = Quartz::Duration
class Generator < Quartz::AtomicModel
output :out
precision micro
state do
var n = 0
var max = 0
var sigma : Duration do
Duration.new(RND.rand(1i64..1000i64), Quartz::Scale::MICRO)
end
end
def time_advance : Quartz::Duration
self.sigma
end
def internal_transition
self.sigma = if n < max
Duration.new(RND.rand(1i64..1000i64), model_precision).tap { self.n += 1 }
else
Duration.infinity(model_precision)
end
end
def output
post nil, on: :out
end
def external_transition(bag)
end
end
class Buffer < Quartz::AtomicModel
input :in, :ready
output :out
precision micro
state do
var nb_job : Int32 = 0
var waiting : Bool = false
var sigma : Duration = Duration::INFINITY
end
def external_transition(bag)
if bag.has_key?(input_port(:in))
self.nb_job += 1
end
if bag.has_key?(input_port(:ready))
self.waiting = false
end
if !waiting && nb_job > 0
self.sigma = Duration.new(5, model_precision)
end
end
def output
post nil, on: :out
end
def time_advance : Quartz::Duration
self.sigma
end
def internal_transition
self.sigma = Duration::INFINITY
self.waiting = true
self.nb_job -= 1
end
def confluent_transition(bag)
internal_transition
external_transition(bag)
end
end
class CPU < Quartz::AtomicModel
input :task
output :done
precision nano
state do
var sigma : Duration = Duration::INFINITY
end
def external_transition(bag)
self.sigma = Duration.new(RND.rand(3i64..1000i64**4), model_precision)
end
def output
post nil, on: :done
end
def internal_transition
self.sigma = Duration.infinity(model_precision)
end
def time_advance : Quartz::Duration
self.sigma
end
end
class GenBufProc < Quartz::CoupledModel
def initialize(name, max)
super(name)
gen = Generator.new(:gen, Generator::State.new(max: max))
buf = Buffer.new(:buf)
proc = CPU.new(:proc)
self << gen
self << buf
self << proc
attach :out, to: :in, between: gen, and: buf
attach :out, to: :task, between: buf, and: proc
attach :done, to: :ready, between: proc, and: buf
end
end
class Tracer
include Quartz::Hooks::Notifiable
include Quartz::Observer
@file : File?
property filename : String
def initialize(@filename, notifier)
notifier.subscribe(Quartz::Hooks::PRE_INIT, self)
notifier.subscribe(Quartz::Hooks::POST_SIMULATION, self)
notifier.subscribe(Quartz::Hooks::POST_ABORT, self)
end
def close
@file.try { |f| f.flush; f.close }
end
def notify(hook)
case hook
when Quartz::Hooks::PRE_INIT
@file = File.new(@filename, "w+")
@file.try &.puts "time,n"
when Quartz::Hooks::POST_SIMULATION, Quartz::Hooks::POST_ABORT
close
@file = nil
else
# no-op
end
end
def update(model, info)
return unless model.is_a?(Buffer)
buf = model.as(Buffer)
if info
return if info.not_nil![:transition].as(Quartz::Any).as_sym == :init
end
@file.try &.puts "#{info[:time]},#{buf.nb_job}"
end
end
RND = Random.new(1324)
max = 10000
model = GenBufProc.new(:genbufproc, max)
sim = Quartz::Simulation.new(
model,
maintain_hierarchy: false,
scheduler: :binary_heap
)
model[:buf].add_observer(Tracer.new("genbufproc_trace.csv", sim.notifier))
sim.simulate
puts sim.transition_stats[:TOTAL]
puts sim.elapsed_secs