git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 579adcf1a43c078d9d69454167290ad6d0b0665f

use sorted map

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 9/23/2017, 12:19:03 AM
Parent: a2acc6de9878b25ba7ae028fcf33ca086d042fae

Files changed

kernel.jschanged
package-lock.jsonchanged
package.jsonchanged
scheduler.jschanged
tests/index.jschanged
kernel.jsView
@@ -47,9 +47,8 @@
4747 async _startMessageLoop () {
4848 // this ensure we only every have one loop running at a time
4949 if (this.containerState !== 'running') {
5050 this.containerState = 'running'
51-
5251 while (1) {
5352 const message = await this.ports.getNextMessage()
5453 if (!message) break
5554
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 495562 bytes
New file size: 495974 bytes
package.jsonView
@@ -32,11 +32,12 @@
3232 "dependencies": {
3333 "binary-search-insert": "^1.0.3",
3434 "chunk": "0.0.2",
3535 "ipld-graph-builder": "1.3.0",
36- "lockmap": "0.0.0",
36+ "lockmap": "0.0.1",
3737 "merkle-radix-tree": "0.0.4",
3838 "primea-message": "0.0.2",
39+ "sortedmap": "0.0.0",
3940 "typedarray-addition": "0.0.0"
4041 },
4142 "devDependencies": {
4243 "primea-abstract-container": "0.0.4",
scheduler.jsView
@@ -1,5 +1,6 @@
11 const binarySearchInsert = require('binary-search-insert')
2+const SortedMap = require('sortedmap')
23 const LockMap = require('lockmap')
34
45 module.exports = class Scheduler {
56 /**
@@ -9,10 +10,14 @@
910 constructor () {
1011 this._waits = []
1112 this._running = new Set()
1213 this._loadingInstances = new LockMap()
13- this.instances = new Map()
14+ this.instances = new SortedMap(comparator)
1415 this.systemServices = new Map()
16+
17+ function comparator (a, b) {
18+ return a.ticks - b.ticks
19+ }
1520 }
1621
1722 /**
1823 * locks the scheduler from clearing waits untill the lock is resolved
@@ -34,17 +39,10 @@
3439 this._checkWaits()
3540 }
3641
3742 _update (instance) {
38- // sorts the container instance map by tick count
3943 this.instances.delete(instance.id)
40- const instanceArray = [...this.instances]
41- binarySearchInsert(instanceArray, comparator, [instance.id, instance])
42- this.instances = new Map(instanceArray)
43-
44- function comparator (a, b) {
45- return a[1].ticks - b[1].ticks
46- }
44+ this.instances.set(instance.id, instance)
4745 }
4846
4947 /**
5048 * returns a container
tests/index.jsView
@@ -24,56 +24,55 @@
2424 node.on('ready', () => {
2525 tape('basic', async t => {
2626 t.plan(3)
2727 let message
28- const expectedState = { '/': 'zdpuAyCWhqq3v5DWhJCJPBgFkFbMxmD4do98TusonJmw7TyEG' }
28+ const expectedState = {
29+ '/': 'zdpuAyCWhqq3v5DWhJCJPBgFkFbMxmD4do98TusonJmw7TyEG'
30+ }
2931
3032 class testVMContainer extends BaseContainer {
3133 onMessage (m) {
3234 t.true(m === message, 'should recive a message')
3335 }
3436 }
3537
36- try {
37- const hypervisor = new Hypervisor(node.dag)
38- hypervisor.registerContainer(testVMContainer)
38+ const hypervisor = new Hypervisor(node.dag)
39+ hypervisor.registerContainer(testVMContainer)
3940
40- const port = hypervisor.creationService.getPort()
41+ const port = hypervisor.creationService.getPort()
4142
42- let rootContainer = await hypervisor.send(port, new Message({
43- data: {
44- type: testVMContainer.typeId
45- }
46- }))
43+ let rootContainer = await hypervisor.send(port, new Message({
44+ data: {
45+ type: testVMContainer.typeId
46+ }
47+ }))
4748
48- rootContainer = await hypervisor.getInstance(rootContainer.id)
49+ rootContainer = await hypervisor.getInstance(rootContainer.id)
4950
50- hypervisor.pin(rootContainer)
51+ hypervisor.pin(rootContainer)
5152
52- const [portRef1, portRef2] = rootContainer.ports.createChannel()
53- const initMessage = rootContainer.createMessage({
54- data: {
55- code: Buffer.from('test code'),
56- type: testVMContainer.typeId
57- },
58- ports: [portRef2]
59- })
53+ const [portRef1, portRef2] = rootContainer.ports.createChannel()
54+ const initMessage = rootContainer.createMessage({
55+ data: {
56+ code: Buffer.from('test code'),
57+ type: testVMContainer.typeId
58+ },
59+ ports: [portRef2]
60+ })
6061
61- await rootContainer.send(port, initMessage)
62- await rootContainer.ports.bind('first', portRef1)
62+ message = rootContainer.createMessage()
63+ await Promise.all([
64+ rootContainer.send(port, initMessage),
65+ rootContainer.ports.bind('first', portRef1),
66+ rootContainer.send(portRef1, message)
67+ ])
68+ rootContainer.shutdown()
6369
64- message = rootContainer.createMessage()
65- await rootContainer.send(portRef1, message)
66- rootContainer.shutdown()
70+ // console.log(JSON.stringify(hypervisor.state, null, 2))
71+ const stateRoot = await hypervisor.createStateRoot(Infinity)
72+ t.deepEquals(stateRoot, expectedState, 'expected root!')
6773
68- // console.log(JSON.stringify(hypervisor.state, null, 2))
69- const stateRoot = await hypervisor.createStateRoot(Infinity)
70- t.deepEquals(stateRoot, expectedState, 'expected root!')
71-
72- t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
73- } catch (e) {
74- console.log(e)
75- }
74+ t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
7675 })
7776
7877 tape('basic - do not store containers with no ports bound', async t => {
7978 t.plan(1)

Built with git-ssb-web