michael@0: // Copyright 2006-2008 the V8 project authors. All rights reserved. michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following michael@0: // disclaimer in the documentation and/or other materials provided michael@0: // with the distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived michael@0: // from this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: michael@0: // This is a JavaScript implementation of the Richards michael@0: // benchmark from: michael@0: // michael@0: // http://www.cl.cam.ac.uk/~mr10/Bench.html michael@0: // michael@0: // The benchmark was originally implemented in BCPL by michael@0: // Martin Richards. michael@0: michael@0: michael@0: var Richards = new BenchmarkSuite('Richards', 35302, [ michael@0: new Benchmark("Richards", runRichards) michael@0: ]); michael@0: michael@0: michael@0: /** michael@0: * The Richards benchmark simulates the task dispatcher of an michael@0: * operating system. michael@0: **/ michael@0: function runRichards() { michael@0: var scheduler = new Scheduler(); michael@0: scheduler.addIdleTask(ID_IDLE, 0, null, COUNT); michael@0: michael@0: var queue = new Packet(null, ID_WORKER, KIND_WORK); michael@0: queue = new Packet(queue, ID_WORKER, KIND_WORK); michael@0: scheduler.addWorkerTask(ID_WORKER, 1000, queue); michael@0: michael@0: queue = new Packet(null, ID_DEVICE_A, KIND_DEVICE); michael@0: queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE); michael@0: queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE); michael@0: scheduler.addHandlerTask(ID_HANDLER_A, 2000, queue); michael@0: michael@0: queue = new Packet(null, ID_DEVICE_B, KIND_DEVICE); michael@0: queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE); michael@0: queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE); michael@0: scheduler.addHandlerTask(ID_HANDLER_B, 3000, queue); michael@0: michael@0: scheduler.addDeviceTask(ID_DEVICE_A, 4000, null); michael@0: michael@0: scheduler.addDeviceTask(ID_DEVICE_B, 5000, null); michael@0: michael@0: scheduler.schedule(); michael@0: michael@0: if (scheduler.queueCount != EXPECTED_QUEUE_COUNT || michael@0: scheduler.holdCount != EXPECTED_HOLD_COUNT) { michael@0: var msg = michael@0: "Error during execution: queueCount = " + scheduler.queueCount + michael@0: ", holdCount = " + scheduler.holdCount + "."; michael@0: throw new Error(msg); michael@0: } michael@0: } michael@0: michael@0: var COUNT = 1000; michael@0: michael@0: /** michael@0: * These two constants specify how many times a packet is queued and michael@0: * how many times a task is put on hold in a correct run of richards. michael@0: * They don't have any meaning a such but are characteristic of a michael@0: * correct run so if the actual queue or hold count is different from michael@0: * the expected there must be a bug in the implementation. michael@0: **/ michael@0: var EXPECTED_QUEUE_COUNT = 2322; michael@0: var EXPECTED_HOLD_COUNT = 928; michael@0: michael@0: michael@0: /** michael@0: * A scheduler can be used to schedule a set of tasks based on their relative michael@0: * priorities. Scheduling is done by maintaining a list of task control blocks michael@0: * which holds tasks and the data queue they are processing. michael@0: * @constructor michael@0: */ michael@0: function Scheduler() { michael@0: this.queueCount = 0; michael@0: this.holdCount = 0; michael@0: this.blocks = new Array(NUMBER_OF_IDS); michael@0: this.list = null; michael@0: this.currentTcb = null; michael@0: this.currentId = null; michael@0: } michael@0: michael@0: var ID_IDLE = 0; michael@0: var ID_WORKER = 1; michael@0: var ID_HANDLER_A = 2; michael@0: var ID_HANDLER_B = 3; michael@0: var ID_DEVICE_A = 4; michael@0: var ID_DEVICE_B = 5; michael@0: var NUMBER_OF_IDS = 6; michael@0: michael@0: var KIND_DEVICE = 0; michael@0: var KIND_WORK = 1; michael@0: michael@0: /** michael@0: * Add an idle task to this scheduler. michael@0: * @param {int} id the identity of the task michael@0: * @param {int} priority the task's priority michael@0: * @param {Packet} queue the queue of work to be processed by the task michael@0: * @param {int} count the number of times to schedule the task michael@0: */ michael@0: Scheduler.prototype.addIdleTask = function (id, priority, queue, count) { michael@0: this.addRunningTask(id, priority, queue, new IdleTask(this, 1, count)); michael@0: }; michael@0: michael@0: /** michael@0: * Add a work task to this scheduler. michael@0: * @param {int} id the identity of the task michael@0: * @param {int} priority the task's priority michael@0: * @param {Packet} queue the queue of work to be processed by the task michael@0: */ michael@0: Scheduler.prototype.addWorkerTask = function (id, priority, queue) { michael@0: this.addTask(id, priority, queue, new WorkerTask(this, ID_HANDLER_A, 0)); michael@0: }; michael@0: michael@0: /** michael@0: * Add a handler task to this scheduler. michael@0: * @param {int} id the identity of the task michael@0: * @param {int} priority the task's priority michael@0: * @param {Packet} queue the queue of work to be processed by the task michael@0: */ michael@0: Scheduler.prototype.addHandlerTask = function (id, priority, queue) { michael@0: this.addTask(id, priority, queue, new HandlerTask(this)); michael@0: }; michael@0: michael@0: /** michael@0: * Add a handler task to this scheduler. michael@0: * @param {int} id the identity of the task michael@0: * @param {int} priority the task's priority michael@0: * @param {Packet} queue the queue of work to be processed by the task michael@0: */ michael@0: Scheduler.prototype.addDeviceTask = function (id, priority, queue) { michael@0: this.addTask(id, priority, queue, new DeviceTask(this)) michael@0: }; michael@0: michael@0: /** michael@0: * Add the specified task and mark it as running. michael@0: * @param {int} id the identity of the task michael@0: * @param {int} priority the task's priority michael@0: * @param {Packet} queue the queue of work to be processed by the task michael@0: * @param {Task} task the task to add michael@0: */ michael@0: Scheduler.prototype.addRunningTask = function (id, priority, queue, task) { michael@0: this.addTask(id, priority, queue, task); michael@0: this.currentTcb.setRunning(); michael@0: }; michael@0: michael@0: /** michael@0: * Add the specified task to this scheduler. michael@0: * @param {int} id the identity of the task michael@0: * @param {int} priority the task's priority michael@0: * @param {Packet} queue the queue of work to be processed by the task michael@0: * @param {Task} task the task to add michael@0: */ michael@0: Scheduler.prototype.addTask = function (id, priority, queue, task) { michael@0: this.currentTcb = new TaskControlBlock(this.list, id, priority, queue, task); michael@0: this.list = this.currentTcb; michael@0: this.blocks[id] = this.currentTcb; michael@0: }; michael@0: michael@0: /** michael@0: * Execute the tasks managed by this scheduler. michael@0: */ michael@0: Scheduler.prototype.schedule = function () { michael@0: this.currentTcb = this.list; michael@0: while (this.currentTcb != null) { michael@0: if (this.currentTcb.isHeldOrSuspended()) { michael@0: this.currentTcb = this.currentTcb.link; michael@0: } else { michael@0: this.currentId = this.currentTcb.id; michael@0: this.currentTcb = this.currentTcb.run(); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Release a task that is currently blocked and return the next block to run. michael@0: * @param {int} id the id of the task to suspend michael@0: */ michael@0: Scheduler.prototype.release = function (id) { michael@0: var tcb = this.blocks[id]; michael@0: if (tcb == null) return tcb; michael@0: tcb.markAsNotHeld(); michael@0: if (tcb.priority > this.currentTcb.priority) { michael@0: return tcb; michael@0: } else { michael@0: return this.currentTcb; michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Block the currently executing task and return the next task control block michael@0: * to run. The blocked task will not be made runnable until it is explicitly michael@0: * released, even if new work is added to it. michael@0: */ michael@0: Scheduler.prototype.holdCurrent = function () { michael@0: this.holdCount++; michael@0: this.currentTcb.markAsHeld(); michael@0: return this.currentTcb.link; michael@0: }; michael@0: michael@0: /** michael@0: * Suspend the currently executing task and return the next task control block michael@0: * to run. If new work is added to the suspended task it will be made runnable. michael@0: */ michael@0: Scheduler.prototype.suspendCurrent = function () { michael@0: this.currentTcb.markAsSuspended(); michael@0: return this.currentTcb; michael@0: }; michael@0: michael@0: /** michael@0: * Add the specified packet to the end of the worklist used by the task michael@0: * associated with the packet and make the task runnable if it is currently michael@0: * suspended. michael@0: * @param {Packet} packet the packet to add michael@0: */ michael@0: Scheduler.prototype.queue = function (packet) { michael@0: var t = this.blocks[packet.id]; michael@0: if (t == null) return t; michael@0: this.queueCount++; michael@0: packet.link = null; michael@0: packet.id = this.currentId; michael@0: return t.checkPriorityAdd(this.currentTcb, packet); michael@0: }; michael@0: michael@0: /** michael@0: * A task control block manages a task and the queue of work packages associated michael@0: * with it. michael@0: * @param {TaskControlBlock} link the preceding block in the linked block list michael@0: * @param {int} id the id of this block michael@0: * @param {int} priority the priority of this block michael@0: * @param {Packet} queue the queue of packages to be processed by the task michael@0: * @param {Task} task the task michael@0: * @constructor michael@0: */ michael@0: function TaskControlBlock(link, id, priority, queue, task) { michael@0: this.link = link; michael@0: this.id = id; michael@0: this.priority = priority; michael@0: this.queue = queue; michael@0: this.task = task; michael@0: if (queue == null) { michael@0: this.state = STATE_SUSPENDED; michael@0: } else { michael@0: this.state = STATE_SUSPENDED_RUNNABLE; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * The task is running and is currently scheduled. michael@0: */ michael@0: var STATE_RUNNING = 0; michael@0: michael@0: /** michael@0: * The task has packets left to process. michael@0: */ michael@0: var STATE_RUNNABLE = 1; michael@0: michael@0: /** michael@0: * The task is not currently running. The task is not blocked as such and may michael@0: * be started by the scheduler. michael@0: */ michael@0: var STATE_SUSPENDED = 2; michael@0: michael@0: /** michael@0: * The task is blocked and cannot be run until it is explicitly released. michael@0: */ michael@0: var STATE_HELD = 4; michael@0: michael@0: var STATE_SUSPENDED_RUNNABLE = STATE_SUSPENDED | STATE_RUNNABLE; michael@0: var STATE_NOT_HELD = ~STATE_HELD; michael@0: michael@0: TaskControlBlock.prototype.setRunning = function () { michael@0: this.state = STATE_RUNNING; michael@0: }; michael@0: michael@0: TaskControlBlock.prototype.markAsNotHeld = function () { michael@0: this.state = this.state & STATE_NOT_HELD; michael@0: }; michael@0: michael@0: TaskControlBlock.prototype.markAsHeld = function () { michael@0: this.state = this.state | STATE_HELD; michael@0: }; michael@0: michael@0: TaskControlBlock.prototype.isHeldOrSuspended = function () { michael@0: return (this.state & STATE_HELD) != 0 || (this.state == STATE_SUSPENDED); michael@0: }; michael@0: michael@0: TaskControlBlock.prototype.markAsSuspended = function () { michael@0: this.state = this.state | STATE_SUSPENDED; michael@0: }; michael@0: michael@0: TaskControlBlock.prototype.markAsRunnable = function () { michael@0: this.state = this.state | STATE_RUNNABLE; michael@0: }; michael@0: michael@0: /** michael@0: * Runs this task, if it is ready to be run, and returns the next task to run. michael@0: */ michael@0: TaskControlBlock.prototype.run = function () { michael@0: var packet; michael@0: if (this.state == STATE_SUSPENDED_RUNNABLE) { michael@0: packet = this.queue; michael@0: this.queue = packet.link; michael@0: if (this.queue == null) { michael@0: this.state = STATE_RUNNING; michael@0: } else { michael@0: this.state = STATE_RUNNABLE; michael@0: } michael@0: } else { michael@0: packet = null; michael@0: } michael@0: return this.task.run(packet); michael@0: }; michael@0: michael@0: /** michael@0: * Adds a packet to the worklist of this block's task, marks this as runnable if michael@0: * necessary, and returns the next runnable object to run (the one michael@0: * with the highest priority). michael@0: */ michael@0: TaskControlBlock.prototype.checkPriorityAdd = function (task, packet) { michael@0: if (this.queue == null) { michael@0: this.queue = packet; michael@0: this.markAsRunnable(); michael@0: if (this.priority > task.priority) return this; michael@0: } else { michael@0: this.queue = packet.addTo(this.queue); michael@0: } michael@0: return task; michael@0: }; michael@0: michael@0: TaskControlBlock.prototype.toString = function () { michael@0: return "tcb { " + this.task + "@" + this.state + " }"; michael@0: }; michael@0: michael@0: /** michael@0: * An idle task doesn't do any work itself but cycles control between the two michael@0: * device tasks. michael@0: * @param {Scheduler} scheduler the scheduler that manages this task michael@0: * @param {int} v1 a seed value that controls how the device tasks are scheduled michael@0: * @param {int} count the number of times this task should be scheduled michael@0: * @constructor michael@0: */ michael@0: function IdleTask(scheduler, v1, count) { michael@0: this.scheduler = scheduler; michael@0: this.v1 = v1; michael@0: this.count = count; michael@0: } michael@0: michael@0: IdleTask.prototype.run = function (packet) { michael@0: this.count--; michael@0: if (this.count == 0) return this.scheduler.holdCurrent(); michael@0: if ((this.v1 & 1) == 0) { michael@0: this.v1 = this.v1 >> 1; michael@0: return this.scheduler.release(ID_DEVICE_A); michael@0: } else { michael@0: this.v1 = (this.v1 >> 1) ^ 0xD008; michael@0: return this.scheduler.release(ID_DEVICE_B); michael@0: } michael@0: }; michael@0: michael@0: IdleTask.prototype.toString = function () { michael@0: return "IdleTask" michael@0: }; michael@0: michael@0: /** michael@0: * A task that suspends itself after each time it has been run to simulate michael@0: * waiting for data from an external device. michael@0: * @param {Scheduler} scheduler the scheduler that manages this task michael@0: * @constructor michael@0: */ michael@0: function DeviceTask(scheduler) { michael@0: this.scheduler = scheduler; michael@0: this.v1 = null; michael@0: } michael@0: michael@0: DeviceTask.prototype.run = function (packet) { michael@0: if (packet == null) { michael@0: if (this.v1 == null) return this.scheduler.suspendCurrent(); michael@0: var v = this.v1; michael@0: this.v1 = null; michael@0: return this.scheduler.queue(v); michael@0: } else { michael@0: this.v1 = packet; michael@0: return this.scheduler.holdCurrent(); michael@0: } michael@0: }; michael@0: michael@0: DeviceTask.prototype.toString = function () { michael@0: return "DeviceTask"; michael@0: }; michael@0: michael@0: /** michael@0: * A task that manipulates work packets. michael@0: * @param {Scheduler} scheduler the scheduler that manages this task michael@0: * @param {int} v1 a seed used to specify how work packets are manipulated michael@0: * @param {int} v2 another seed used to specify how work packets are manipulated michael@0: * @constructor michael@0: */ michael@0: function WorkerTask(scheduler, v1, v2) { michael@0: this.scheduler = scheduler; michael@0: this.v1 = v1; michael@0: this.v2 = v2; michael@0: } michael@0: michael@0: WorkerTask.prototype.run = function (packet) { michael@0: if (packet == null) { michael@0: return this.scheduler.suspendCurrent(); michael@0: } else { michael@0: if (this.v1 == ID_HANDLER_A) { michael@0: this.v1 = ID_HANDLER_B; michael@0: } else { michael@0: this.v1 = ID_HANDLER_A; michael@0: } michael@0: packet.id = this.v1; michael@0: packet.a1 = 0; michael@0: for (var i = 0; i < DATA_SIZE; i++) { michael@0: this.v2++; michael@0: if (this.v2 > 26) this.v2 = 1; michael@0: packet.a2[i] = this.v2; michael@0: } michael@0: return this.scheduler.queue(packet); michael@0: } michael@0: }; michael@0: michael@0: WorkerTask.prototype.toString = function () { michael@0: return "WorkerTask"; michael@0: }; michael@0: michael@0: /** michael@0: * A task that manipulates work packets and then suspends itself. michael@0: * @param {Scheduler} scheduler the scheduler that manages this task michael@0: * @constructor michael@0: */ michael@0: function HandlerTask(scheduler) { michael@0: this.scheduler = scheduler; michael@0: this.v1 = null; michael@0: this.v2 = null; michael@0: } michael@0: michael@0: HandlerTask.prototype.run = function (packet) { michael@0: if (packet != null) { michael@0: if (packet.kind == KIND_WORK) { michael@0: this.v1 = packet.addTo(this.v1); michael@0: } else { michael@0: this.v2 = packet.addTo(this.v2); michael@0: } michael@0: } michael@0: if (this.v1 != null) { michael@0: var count = this.v1.a1; michael@0: var v; michael@0: if (count < DATA_SIZE) { michael@0: if (this.v2 != null) { michael@0: v = this.v2; michael@0: this.v2 = this.v2.link; michael@0: v.a1 = this.v1.a2[count]; michael@0: this.v1.a1 = count + 1; michael@0: return this.scheduler.queue(v); michael@0: } michael@0: } else { michael@0: v = this.v1; michael@0: this.v1 = this.v1.link; michael@0: return this.scheduler.queue(v); michael@0: } michael@0: } michael@0: return this.scheduler.suspendCurrent(); michael@0: }; michael@0: michael@0: HandlerTask.prototype.toString = function () { michael@0: return "HandlerTask"; michael@0: }; michael@0: michael@0: /* --- * michael@0: * P a c k e t michael@0: * --- */ michael@0: michael@0: var DATA_SIZE = 4; michael@0: michael@0: /** michael@0: * A simple package of data that is manipulated by the tasks. The exact layout michael@0: * of the payload data carried by a packet is not importaint, and neither is the michael@0: * nature of the work performed on packets by the tasks. michael@0: * michael@0: * Besides carrying data, packets form linked lists and are hence used both as michael@0: * data and worklists. michael@0: * @param {Packet} link the tail of the linked list of packets michael@0: * @param {int} id an ID for this packet michael@0: * @param {int} kind the type of this packet michael@0: * @constructor michael@0: */ michael@0: function Packet(link, id, kind) { michael@0: this.link = link; michael@0: this.id = id; michael@0: this.kind = kind; michael@0: this.a1 = 0; michael@0: this.a2 = new Array(DATA_SIZE); michael@0: } michael@0: michael@0: /** michael@0: * Add this packet to the end of a worklist, and return the worklist. michael@0: * @param {Packet} queue the worklist to add this packet to michael@0: */ michael@0: Packet.prototype.addTo = function (queue) { michael@0: this.link = null; michael@0: if (queue == null) return this; michael@0: var peek, next = queue; michael@0: while ((peek = next.link) != null) michael@0: next = peek; michael@0: next.link = this; michael@0: return queue; michael@0: }; michael@0: michael@0: Packet.prototype.toString = function () { michael@0: return "Packet"; michael@0: };