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.js | changed |
package-lock.json | changed |
package.json | changed |
scheduler.js | changed |
tests/index.js | changed |
kernel.js | ||
---|---|---|
@@ -47,9 +47,8 @@ | ||
47 | 47 | async _startMessageLoop () { |
48 | 48 | // this ensure we only every have one loop running at a time |
49 | 49 | if (this.containerState !== 'running') { |
50 | 50 | this.containerState = 'running' |
51 | - | |
52 | 51 | while (1) { |
53 | 52 | const message = await this.ports.getNextMessage() |
54 | 53 | if (!message) break |
55 | 54 |
package-lock.json | ||
---|---|---|
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.json | ||
---|---|---|
@@ -32,11 +32,12 @@ | ||
32 | 32 | "dependencies": { |
33 | 33 | "binary-search-insert": "^1.0.3", |
34 | 34 | "chunk": "0.0.2", |
35 | 35 | "ipld-graph-builder": "1.3.0", |
36 | - "lockmap": "0.0.0", | |
36 | + "lockmap": "0.0.1", | |
37 | 37 | "merkle-radix-tree": "0.0.4", |
38 | 38 | "primea-message": "0.0.2", |
39 | + "sortedmap": "0.0.0", | |
39 | 40 | "typedarray-addition": "0.0.0" |
40 | 41 | }, |
41 | 42 | "devDependencies": { |
42 | 43 | "primea-abstract-container": "0.0.4", |
scheduler.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const binarySearchInsert = require('binary-search-insert') |
2 | +const SortedMap = require('sortedmap') | |
2 | 3 | const LockMap = require('lockmap') |
3 | 4 | |
4 | 5 | module.exports = class Scheduler { |
5 | 6 | /** |
@@ -9,10 +10,14 @@ | ||
9 | 10 | constructor () { |
10 | 11 | this._waits = [] |
11 | 12 | this._running = new Set() |
12 | 13 | this._loadingInstances = new LockMap() |
13 | - this.instances = new Map() | |
14 | + this.instances = new SortedMap(comparator) | |
14 | 15 | this.systemServices = new Map() |
16 | + | |
17 | + function comparator (a, b) { | |
18 | + return a.ticks - b.ticks | |
19 | + } | |
15 | 20 | } |
16 | 21 | |
17 | 22 | /** |
18 | 23 | * locks the scheduler from clearing waits untill the lock is resolved |
@@ -34,17 +39,10 @@ | ||
34 | 39 | this._checkWaits() |
35 | 40 | } |
36 | 41 | |
37 | 42 | _update (instance) { |
38 | - // sorts the container instance map by tick count | |
39 | 43 | 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) | |
47 | 45 | } |
48 | 46 | |
49 | 47 | /** |
50 | 48 | * returns a container |
tests/index.js | ||
---|---|---|
@@ -24,56 +24,55 @@ | ||
24 | 24 | node.on('ready', () => { |
25 | 25 | tape('basic', async t => { |
26 | 26 | t.plan(3) |
27 | 27 | let message |
28 | - const expectedState = { '/': 'zdpuAyCWhqq3v5DWhJCJPBgFkFbMxmD4do98TusonJmw7TyEG' } | |
28 | + const expectedState = { | |
29 | + '/': 'zdpuAyCWhqq3v5DWhJCJPBgFkFbMxmD4do98TusonJmw7TyEG' | |
30 | + } | |
29 | 31 | |
30 | 32 | class testVMContainer extends BaseContainer { |
31 | 33 | onMessage (m) { |
32 | 34 | t.true(m === message, 'should recive a message') |
33 | 35 | } |
34 | 36 | } |
35 | 37 | |
36 | - try { | |
37 | - const hypervisor = new Hypervisor(node.dag) | |
38 | - hypervisor.registerContainer(testVMContainer) | |
38 | + const hypervisor = new Hypervisor(node.dag) | |
39 | + hypervisor.registerContainer(testVMContainer) | |
39 | 40 | |
40 | - const port = hypervisor.creationService.getPort() | |
41 | + const port = hypervisor.creationService.getPort() | |
41 | 42 | |
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 | + })) | |
47 | 48 | |
48 | - rootContainer = await hypervisor.getInstance(rootContainer.id) | |
49 | + rootContainer = await hypervisor.getInstance(rootContainer.id) | |
49 | 50 | |
50 | - hypervisor.pin(rootContainer) | |
51 | + hypervisor.pin(rootContainer) | |
51 | 52 | |
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 | + }) | |
60 | 61 | |
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() | |
63 | 69 | |
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!') | |
67 | 73 | |
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) | |
76 | 75 | }) |
77 | 76 | |
78 | 77 | tape('basic - do not store containers with no ports bound', async t => { |
79 | 78 | t.plan(1) |
Built with git-ssb-web