git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 06318e4ba3c91b1e19512d30a17d3cfe981d06af

only check the saturation once

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 9/8/2017, 6:15:46 PM
Parent: 67f17984adbe450a9a06aa0987f1b232af5f3120

Files changed

creationService.jschanged
index.jschanged
kernel.jschanged
portManager.jschanged
creationService.jsView
@@ -25,8 +25,16 @@
2525 destId: 0
2626 }
2727 }
2828
29+ // send (port, message) {
30+ // message._hops++
31+ // message._fromTicks = this.ticks
32+ // message.fromId = this.id
33+
34+ // return this.hypervisor.send(port, message)
35+ // }
36+
2937 /**
3038 * creates an new container instances and save it in the state
3139 * @returns {Promise}
3240 */
index.jsView
@@ -5,9 +5,8 @@
55 const DFSchecker = require('./dfsChecker.js')
66 const CreationService = require('./creationService.js')
77
88 const CREATION_ID = 0
9-// const ROUTING_ID = 1
109
1110 module.exports = class Hypervisor {
1211 /**
1312 * The Hypervisor manages the container instances by instantiating them and
kernel.jsView
@@ -169,9 +169,6 @@
169169 message.fromId = this.id
170170 this.ports.removeSentPorts(message)
171171
172172 return this.hypervisor.send(port, message)
173- // if (this.currentMessage !== message && !message.responsePort) {
174- // this.currentMessage._addSubMessage(message)
175- // }
176173 }
177174 }
portManager.jsView
@@ -1,23 +1,23 @@
11 const DeleteMessage = require('./deleteMessage')
22
33 // decides which message to go first
4-function messageArbiter (nameA, nameB) {
5- const a = this.ports[nameA].messages[0]
6- const b = this.ports[nameB].messages[0]
4+function messageArbiter (portA, portB) {
5+ const a = portA.messages[0]
6+ const b = portB.messages[0]
77
88 if (!a) {
9- return nameB
9+ return portB
1010 } else if (!b) {
11- return nameA
11+ return portA
1212 }
1313
1414 // order by number of ticks if messages have different number of ticks
1515 if (a._fromTicks !== b._fromTicks) {
16- return a._fromTicks < b._fromTicks ? nameA : nameB
16+ return a._fromTicks < b._fromTicks ? portA : portB
1717 } else {
1818 // insertion order
19- return nameA
19+ return portA
2020 }
2121 }
2222
2323 module.exports = class PortManager {
@@ -142,9 +142,9 @@
142142
143143 const numOfMsg = port.messages.push(message)
144144
145145 if (numOfMsg === 1) {
146- if (this._isSaturated()) {
146+ if (this._isSaturated(this.ports)) {
147147 this._saturationResolve()
148148 this._saturationPromise = new Promise((resolve, reject) => {
149149 this._saturationResolve = resolve
150150 })
@@ -177,62 +177,67 @@
177177 this._unboundPorts.add(port2)
178178 return [port1, port2]
179179 }
180180
181- // find and returns the next message
182- _peekNextMessage () {
183- const names = Object.keys(this.ports)
184- if (names.length) {
185- const portName = names.reduce(messageArbiter.bind(this))
186- const port = this.ports[portName]
181+ // find and returns the next message that this instance currently knows about
182+ _peekNextMessage (ports) {
183+ ports = Object.values(ports)
184+ if (ports.length) {
185+ const port = ports.reduce(messageArbiter)
187186 return port.messages[0]
188187 }
189188 }
190189
191190 /**
192191 * Waits for the the next message if any
193192 * @returns {Promise}
194193 */
195- async getNextMessage () {
196- let message = this._peekNextMessage()
197- let saturated = this._isSaturated()
194+ async getNextMessage (ports = this.ports, timeout = Infinity) {
195+ let message = this._peekNextMessage(ports)
198196 let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
199197
200- while (!saturated && // end if there are messages on all the ports
201- // end if we have a message older then slowest containers
202- !((message && oldestTime >= message._fromTicks) ||
203- // end if there are no messages and this container is the oldest contaner
204- (!message && oldestTime === this.kernel.ticks))) {
205- const ticksToWait = message ? message._fromTicks : this.kernel.ticks
198+ await Promise.race([
199+ this._whenSaturated().then(() => {
200+ message = this._peekNextMessage(ports)
201+ }),
202+ new Promise(async (resolve, reject) => {
203+ while (// end if we have a message older then slowest containers
204+ !((message && oldestTime >= message._fromTicks) ||
205+ // end if there are no messages and this container is the oldest contaner
206+ (!message && oldestTime === this.kernel.ticks))) {
207+ let ticksToWait = message ? message._fromTicks : this.kernel.ticks
208+ // ticksToWait = ticksToWait > timeout ? timeout : ticksToWait
206209
207- await Promise.race([
208- this.hypervisor.scheduler.wait(ticksToWait, this.id).then(() => {
209- message = this._peekNextMessage()
210- }),
211- this._olderMessage(message).then(m => {
212- message = m
213- }),
214- this._whenSaturated().then(() => {
215- saturated = true
216- message = this._peekNextMessage()
217- })
218- ])
210+ await Promise.race([
211+ this.hypervisor.scheduler.wait(ticksToWait, this.id).then(() => {
212+ message = this._peekNextMessage(ports)
213+ }),
214+ this._olderMessage(message).then(m => {
215+ message = m
216+ })
217+ ])
219218
220- oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
221- }
222-
219+ oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
220+ }
221+ resolve()
222+ })
223+ ])
223224 return message
224225 }
225226
226227 // tests wether or not all the ports have a message
227- _isSaturated () {
228- const keys = Object.keys(this.ports)
229- return keys.length ? keys.every(name => this.ports[name].messages.length) : 0
228+ _isSaturated (ports) {
229+ const values = Object.values(ports)
230+ return values.length ? values.every(port => port.messages.length) : false
230231 }
231232
232233 // returns a promise that resolve when the ports are saturated
233234 _whenSaturated () {
234- return this._saturationPromise
235+ if (this._isSaturated(this.ports)) {
236+ return Promise.resolve()
237+ } else {
238+ return this._saturationPromise
239+ }
235240 }
236241
237242 // returns a promise that resolve when a message older then the given message
238243 // is recived

Built with git-ssb-web