3 <script src=
"../htmlrunner.js"></script>
5 // Copyright 2006-2008 the V8 project authors. All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // This is a JavaScript implementation of the Richards
36 // http://www.cl.cam.ac.uk/~mr10/Bench.html
38 // The benchmark was originally implemented in BCPL by
42 window
.onload = function(){ startTest("v8-richards", 'ca0410e0');
44 test("Richards", runRichards
);
50 * The Richards benchmark simulates the task dispatcher of an
53 function runRichards() {
54 var scheduler
= new Scheduler();
55 scheduler
.addIdleTask(ID_IDLE
, 0, null, COUNT
);
57 var queue
= new Packet(null, ID_WORKER
, KIND_WORK
);
58 queue
= new Packet(queue
, ID_WORKER
, KIND_WORK
);
59 scheduler
.addWorkerTask(ID_WORKER
, 1000, queue
);
61 queue
= new Packet(null, ID_DEVICE_A
, KIND_DEVICE
);
62 queue
= new Packet(queue
, ID_DEVICE_A
, KIND_DEVICE
);
63 queue
= new Packet(queue
, ID_DEVICE_A
, KIND_DEVICE
);
64 scheduler
.addHandlerTask(ID_HANDLER_A
, 2000, queue
);
66 queue
= new Packet(null, ID_DEVICE_B
, KIND_DEVICE
);
67 queue
= new Packet(queue
, ID_DEVICE_B
, KIND_DEVICE
);
68 queue
= new Packet(queue
, ID_DEVICE_B
, KIND_DEVICE
);
69 scheduler
.addHandlerTask(ID_HANDLER_B
, 3000, queue
);
71 scheduler
.addDeviceTask(ID_DEVICE_A
, 4000, null);
73 scheduler
.addDeviceTask(ID_DEVICE_B
, 5000, null);
77 if (scheduler
.queueCount
!= EXPECTED_QUEUE_COUNT
||
78 scheduler
.holdCount
!= EXPECTED_HOLD_COUNT
) {
80 "Error during execution: queueCount = " + scheduler
.queueCount
+
81 ", holdCount = " + scheduler
.holdCount
+ ".";
89 * These two constants specify how many times a packet is queued and
90 * how many times a task is put on hold in a correct run of richards.
91 * They don't have any meaning a such but are characteristic of a
92 * correct run so if the actual queue or hold count is different from
93 * the expected there must be a bug in the implementation.
95 var EXPECTED_QUEUE_COUNT
= 2322;
96 var EXPECTED_HOLD_COUNT
= 928;
100 * A scheduler can be used to schedule a set of tasks based on their relative
101 * priorities. Scheduling is done by maintaining a list of task control blocks
102 * which holds tasks and the data queue they are processing.
105 function Scheduler() {
108 this.blocks
= new Array(NUMBER_OF_IDS
);
110 this.currentTcb
= null;
111 this.currentId
= null;
116 var ID_HANDLER_A
= 2;
117 var ID_HANDLER_B
= 3;
120 var NUMBER_OF_IDS
= 6;
126 * Add an idle task to this scheduler.
127 * @param {int} id the identity of the task
128 * @param {int} priority the task's priority
129 * @param {Packet} queue the queue of work to be processed by the task
130 * @param {int} count the number of times to schedule the task
132 Scheduler
.prototype.addIdleTask = function (id
, priority
, queue
, count
) {
133 this.addRunningTask(id
, priority
, queue
, new IdleTask(this, 1, count
));
137 * Add a work task to this scheduler.
138 * @param {int} id the identity of the task
139 * @param {int} priority the task's priority
140 * @param {Packet} queue the queue of work to be processed by the task
142 Scheduler
.prototype.addWorkerTask = function (id
, priority
, queue
) {
143 this.addTask(id
, priority
, queue
, new WorkerTask(this, ID_HANDLER_A
, 0));
147 * Add a handler task to this scheduler.
148 * @param {int} id the identity of the task
149 * @param {int} priority the task's priority
150 * @param {Packet} queue the queue of work to be processed by the task
152 Scheduler
.prototype.addHandlerTask = function (id
, priority
, queue
) {
153 this.addTask(id
, priority
, queue
, new HandlerTask(this));
157 * Add a handler task to this scheduler.
158 * @param {int} id the identity of the task
159 * @param {int} priority the task's priority
160 * @param {Packet} queue the queue of work to be processed by the task
162 Scheduler
.prototype.addDeviceTask = function (id
, priority
, queue
) {
163 this.addTask(id
, priority
, queue
, new DeviceTask(this))
167 * Add the specified task and mark it as running.
168 * @param {int} id the identity of the task
169 * @param {int} priority the task's priority
170 * @param {Packet} queue the queue of work to be processed by the task
171 * @param {Task} task the task to add
173 Scheduler
.prototype.addRunningTask = function (id
, priority
, queue
, task
) {
174 this.addTask(id
, priority
, queue
, task
);
175 this.currentTcb
.setRunning();
179 * Add the specified task to this scheduler.
180 * @param {int} id the identity of the task
181 * @param {int} priority the task's priority
182 * @param {Packet} queue the queue of work to be processed by the task
183 * @param {Task} task the task to add
185 Scheduler
.prototype.addTask = function (id
, priority
, queue
, task
) {
186 this.currentTcb
= new TaskControlBlock(this.list
, id
, priority
, queue
, task
);
187 this.list
= this.currentTcb
;
188 this.blocks
[id
] = this.currentTcb
;
192 * Execute the tasks managed by this scheduler.
194 Scheduler
.prototype.schedule = function () {
195 this.currentTcb
= this.list
;
196 while (this.currentTcb
!= null) {
197 if (this.currentTcb
.isHeldOrSuspended()) {
198 this.currentTcb
= this.currentTcb
.link
;
200 this.currentId
= this.currentTcb
.id
;
201 this.currentTcb
= this.currentTcb
.run();
207 * Release a task that is currently blocked and return the next block to run.
208 * @param {int} id the id of the task to suspend
210 Scheduler
.prototype.release = function (id
) {
211 var tcb
= this.blocks
[id
];
212 if (tcb
== null) return tcb
;
214 if (tcb
.priority
> this.currentTcb
.priority
) {
217 return this.currentTcb
;
222 * Block the currently executing task and return the next task control block
223 * to run. The blocked task will not be made runnable until it is explicitly
224 * released, even if new work is added to it.
226 Scheduler
.prototype.holdCurrent = function () {
228 this.currentTcb
.markAsHeld();
229 return this.currentTcb
.link
;
233 * Suspend the currently executing task and return the next task control block
234 * to run. If new work is added to the suspended task it will be made runnable.
236 Scheduler
.prototype.suspendCurrent = function () {
237 this.currentTcb
.markAsSuspended();
238 return this.currentTcb
;
242 * Add the specified packet to the end of the worklist used by the task
243 * associated with the packet and make the task runnable if it is currently
245 * @param {Packet} packet the packet to add
247 Scheduler
.prototype.queue = function (packet
) {
248 var t
= this.blocks
[packet
.id
];
249 if (t
== null) return t
;
252 packet
.id
= this.currentId
;
253 return t
.checkPriorityAdd(this.currentTcb
, packet
);
257 * A task control block manages a task and the queue of work packages associated
259 * @param {TaskControlBlock} link the preceding block in the linked block list
260 * @param {int} id the id of this block
261 * @param {int} priority the priority of this block
262 * @param {Packet} queue the queue of packages to be processed by the task
263 * @param {Task} task the task
266 function TaskControlBlock(link
, id
, priority
, queue
, task
) {
269 this.priority
= priority
;
273 this.state
= STATE_SUSPENDED
;
275 this.state
= STATE_SUSPENDED_RUNNABLE
;
280 * The task is running and is currently scheduled.
282 var STATE_RUNNING
= 0;
285 * The task has packets left to process.
287 var STATE_RUNNABLE
= 1;
290 * The task is not currently running. The task is not blocked as such and may
291 * be started by the scheduler.
293 var STATE_SUSPENDED
= 2;
296 * The task is blocked and cannot be run until it is explicitly released.
300 var STATE_SUSPENDED_RUNNABLE
= STATE_SUSPENDED
| STATE_RUNNABLE
;
301 var STATE_NOT_HELD
= ~STATE_HELD
;
303 TaskControlBlock
.prototype.setRunning = function () {
304 this.state
= STATE_RUNNING
;
307 TaskControlBlock
.prototype.markAsNotHeld = function () {
308 this.state
= this.state
& STATE_NOT_HELD
;
311 TaskControlBlock
.prototype.markAsHeld = function () {
312 this.state
= this.state
| STATE_HELD
;
315 TaskControlBlock
.prototype.isHeldOrSuspended = function () {
316 return (this.state
& STATE_HELD
) != 0 || (this.state
== STATE_SUSPENDED
);
319 TaskControlBlock
.prototype.markAsSuspended = function () {
320 this.state
= this.state
| STATE_SUSPENDED
;
323 TaskControlBlock
.prototype.markAsRunnable = function () {
324 this.state
= this.state
| STATE_RUNNABLE
;
328 * Runs this task, if it is ready to be run, and returns the next task to run.
330 TaskControlBlock
.prototype.run = function () {
332 if (this.state
== STATE_SUSPENDED_RUNNABLE
) {
334 this.queue
= packet
.link
;
335 if (this.queue
== null) {
336 this.state
= STATE_RUNNING
;
338 this.state
= STATE_RUNNABLE
;
343 return this.task
.run(packet
);
347 * Adds a packet to the worklist of this block's task, marks this as runnable if
348 * necessary, and returns the next runnable object to run (the one
349 * with the highest priority).
351 TaskControlBlock
.prototype.checkPriorityAdd = function (task
, packet
) {
352 if (this.queue
== null) {
354 this.markAsRunnable();
355 if (this.priority
> task
.priority
) return this;
357 this.queue
= packet
.addTo(this.queue
);
362 TaskControlBlock
.prototype.toString = function () {
363 return "tcb { " + this.task
+ "@" + this.state
+ " }";
367 * An idle task doesn't do any work itself but cycles control between the two
369 * @param {Scheduler} scheduler the scheduler that manages this task
370 * @param {int} v1 a seed value that controls how the device tasks are scheduled
371 * @param {int} count the number of times this task should be scheduled
374 function IdleTask(scheduler
, v1
, count
) {
375 this.scheduler
= scheduler
;
380 IdleTask
.prototype.run = function (packet
) {
382 if (this.count
== 0) return this.scheduler
.holdCurrent();
383 if ((this.v1
& 1) == 0) {
384 this.v1
= this.v1
>> 1;
385 return this.scheduler
.release(ID_DEVICE_A
);
387 this.v1
= (this.v1
>> 1) ^ 0xD008;
388 return this.scheduler
.release(ID_DEVICE_B
);
392 IdleTask
.prototype.toString = function () {
397 * A task that suspends itself after each time it has been run to simulate
398 * waiting for data from an external device.
399 * @param {Scheduler} scheduler the scheduler that manages this task
402 function DeviceTask(scheduler
) {
403 this.scheduler
= scheduler
;
407 DeviceTask
.prototype.run = function (packet
) {
408 if (packet
== null) {
409 if (this.v1
== null) return this.scheduler
.suspendCurrent();
412 return this.scheduler
.queue(v
);
415 return this.scheduler
.holdCurrent();
419 DeviceTask
.prototype.toString = function () {
424 * A task that manipulates work packets.
425 * @param {Scheduler} scheduler the scheduler that manages this task
426 * @param {int} v1 a seed used to specify how work packets are manipulated
427 * @param {int} v2 another seed used to specify how work packets are manipulated
430 function WorkerTask(scheduler
, v1
, v2
) {
431 this.scheduler
= scheduler
;
436 WorkerTask
.prototype.run = function (packet
) {
437 if (packet
== null) {
438 return this.scheduler
.suspendCurrent();
440 if (this.v1
== ID_HANDLER_A
) {
441 this.v1
= ID_HANDLER_B
;
443 this.v1
= ID_HANDLER_A
;
447 for (var i
= 0; i
< DATA_SIZE
; i
++) {
449 if (this.v2
> 26) this.v2
= 1;
450 packet
.a2
[i
] = this.v2
;
452 return this.scheduler
.queue(packet
);
456 WorkerTask
.prototype.toString = function () {
461 * A task that manipulates work packets and then suspends itself.
462 * @param {Scheduler} scheduler the scheduler that manages this task
465 function HandlerTask(scheduler
) {
466 this.scheduler
= scheduler
;
471 HandlerTask
.prototype.run = function (packet
) {
472 if (packet
!= null) {
473 if (packet
.kind
== KIND_WORK
) {
474 this.v1
= packet
.addTo(this.v1
);
476 this.v2
= packet
.addTo(this.v2
);
479 if (this.v1
!= null) {
480 var count
= this.v1
.a1
;
482 if (count
< DATA_SIZE
) {
483 if (this.v2
!= null) {
485 this.v2
= this.v2
.link
;
486 v
.a1
= this.v1
.a2
[count
];
487 this.v1
.a1
= count
+ 1;
488 return this.scheduler
.queue(v
);
492 this.v1
= this.v1
.link
;
493 return this.scheduler
.queue(v
);
496 return this.scheduler
.suspendCurrent();
499 HandlerTask
.prototype.toString = function () {
500 return "HandlerTask";
510 * A simple package of data that is manipulated by the tasks. The exact layout
511 * of the payload data carried by a packet is not importaint, and neither is the
512 * nature of the work performed on packets by the tasks.
514 * Besides carrying data, packets form linked lists and are hence used both as
515 * data and worklists.
516 * @param {Packet} link the tail of the linked list of packets
517 * @param {int} id an ID for this packet
518 * @param {int} kind the type of this packet
521 function Packet(link
, id
, kind
) {
526 this.a2
= new Array(DATA_SIZE
);
530 * Add this packet to the end of a worklist, and return the worklist.
531 * @param {Packet} queue the worklist to add this packet to
533 Packet
.prototype.addTo = function (queue
) {
535 if (queue
== null) return this;
536 var peek
, next
= queue
;
537 while ((peek
= next
.link
) != null)
543 Packet
.prototype.toString = function () {