OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <head> |
| 3 <script src="../htmlrunner.js"></script> |
| 4 <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 |
| 8 // met: |
| 9 // |
| 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. |
| 19 // |
| 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. |
| 31 |
| 32 |
| 33 // This is a JavaScript implementation of the Richards |
| 34 // benchmark from: |
| 35 // |
| 36 // http://www.cl.cam.ac.uk/~mr10/Bench.html |
| 37 // |
| 38 // The benchmark was originally implemented in BCPL by |
| 39 // Martin Richards. |
| 40 |
| 41 |
| 42 window.onload = function(){ startTest("v8-richards", ''); |
| 43 |
| 44 test("Richards", runRichards); |
| 45 |
| 46 endTest(); }; |
| 47 |
| 48 |
| 49 /** |
| 50 * The Richards benchmark simulates the task dispatcher of an |
| 51 * operating system. |
| 52 **/ |
| 53 function runRichards() { |
| 54 var scheduler = new Scheduler(); |
| 55 scheduler.addIdleTask(ID_IDLE, 0, null, COUNT); |
| 56 |
| 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); |
| 60 |
| 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); |
| 65 |
| 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); |
| 70 |
| 71 scheduler.addDeviceTask(ID_DEVICE_A, 4000, null); |
| 72 |
| 73 scheduler.addDeviceTask(ID_DEVICE_B, 5000, null); |
| 74 |
| 75 scheduler.schedule(); |
| 76 |
| 77 if (scheduler.queueCount != EXPECTED_QUEUE_COUNT || |
| 78 scheduler.holdCount != EXPECTED_HOLD_COUNT) { |
| 79 var msg = |
| 80 "Error during execution: queueCount = " + scheduler.queueCount + |
| 81 ", holdCount = " + scheduler.holdCount + "."; |
| 82 throw new Error(msg); |
| 83 } |
| 84 } |
| 85 |
| 86 var COUNT = 1000; |
| 87 |
| 88 /** |
| 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. |
| 94 **/ |
| 95 var EXPECTED_QUEUE_COUNT = 2322; |
| 96 var EXPECTED_HOLD_COUNT = 928; |
| 97 |
| 98 |
| 99 /** |
| 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. |
| 103 * @constructor |
| 104 */ |
| 105 function Scheduler() { |
| 106 this.queueCount = 0; |
| 107 this.holdCount = 0; |
| 108 this.blocks = new Array(NUMBER_OF_IDS); |
| 109 this.list = null; |
| 110 this.currentTcb = null; |
| 111 this.currentId = null; |
| 112 } |
| 113 |
| 114 var ID_IDLE = 0; |
| 115 var ID_WORKER = 1; |
| 116 var ID_HANDLER_A = 2; |
| 117 var ID_HANDLER_B = 3; |
| 118 var ID_DEVICE_A = 4; |
| 119 var ID_DEVICE_B = 5; |
| 120 var NUMBER_OF_IDS = 6; |
| 121 |
| 122 var KIND_DEVICE = 0; |
| 123 var KIND_WORK = 1; |
| 124 |
| 125 /** |
| 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 |
| 131 */ |
| 132 Scheduler.prototype.addIdleTask = function (id, priority, queue, count) { |
| 133 this.addRunningTask(id, priority, queue, new IdleTask(this, 1, count)); |
| 134 }; |
| 135 |
| 136 /** |
| 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 |
| 141 */ |
| 142 Scheduler.prototype.addWorkerTask = function (id, priority, queue) { |
| 143 this.addTask(id, priority, queue, new WorkerTask(this, ID_HANDLER_A, 0)); |
| 144 }; |
| 145 |
| 146 /** |
| 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 |
| 151 */ |
| 152 Scheduler.prototype.addHandlerTask = function (id, priority, queue) { |
| 153 this.addTask(id, priority, queue, new HandlerTask(this)); |
| 154 }; |
| 155 |
| 156 /** |
| 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 |
| 161 */ |
| 162 Scheduler.prototype.addDeviceTask = function (id, priority, queue) { |
| 163 this.addTask(id, priority, queue, new DeviceTask(this)) |
| 164 }; |
| 165 |
| 166 /** |
| 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 |
| 172 */ |
| 173 Scheduler.prototype.addRunningTask = function (id, priority, queue, task) { |
| 174 this.addTask(id, priority, queue, task); |
| 175 this.currentTcb.setRunning(); |
| 176 }; |
| 177 |
| 178 /** |
| 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 |
| 184 */ |
| 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; |
| 189 }; |
| 190 |
| 191 /** |
| 192 * Execute the tasks managed by this scheduler. |
| 193 */ |
| 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; |
| 199 } else { |
| 200 this.currentId = this.currentTcb.id; |
| 201 this.currentTcb = this.currentTcb.run(); |
| 202 } |
| 203 } |
| 204 }; |
| 205 |
| 206 /** |
| 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 |
| 209 */ |
| 210 Scheduler.prototype.release = function (id) { |
| 211 var tcb = this.blocks[id]; |
| 212 if (tcb == null) return tcb; |
| 213 tcb.markAsNotHeld(); |
| 214 if (tcb.priority > this.currentTcb.priority) { |
| 215 return tcb; |
| 216 } else { |
| 217 return this.currentTcb; |
| 218 } |
| 219 }; |
| 220 |
| 221 /** |
| 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. |
| 225 */ |
| 226 Scheduler.prototype.holdCurrent = function () { |
| 227 this.holdCount++; |
| 228 this.currentTcb.markAsHeld(); |
| 229 return this.currentTcb.link; |
| 230 }; |
| 231 |
| 232 /** |
| 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. |
| 235 */ |
| 236 Scheduler.prototype.suspendCurrent = function () { |
| 237 this.currentTcb.markAsSuspended(); |
| 238 return this.currentTcb; |
| 239 }; |
| 240 |
| 241 /** |
| 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 |
| 244 * suspended. |
| 245 * @param {Packet} packet the packet to add |
| 246 */ |
| 247 Scheduler.prototype.queue = function (packet) { |
| 248 var t = this.blocks[packet.id]; |
| 249 if (t == null) return t; |
| 250 this.queueCount++; |
| 251 packet.link = null; |
| 252 packet.id = this.currentId; |
| 253 return t.checkPriorityAdd(this.currentTcb, packet); |
| 254 }; |
| 255 |
| 256 /** |
| 257 * A task control block manages a task and the queue of work packages associated |
| 258 * with it. |
| 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 |
| 264 * @constructor |
| 265 */ |
| 266 function TaskControlBlock(link, id, priority, queue, task) { |
| 267 this.link = link; |
| 268 this.id = id; |
| 269 this.priority = priority; |
| 270 this.queue = queue; |
| 271 this.task = task; |
| 272 if (queue == null) { |
| 273 this.state = STATE_SUSPENDED; |
| 274 } else { |
| 275 this.state = STATE_SUSPENDED_RUNNABLE; |
| 276 } |
| 277 } |
| 278 |
| 279 /** |
| 280 * The task is running and is currently scheduled. |
| 281 */ |
| 282 var STATE_RUNNING = 0; |
| 283 |
| 284 /** |
| 285 * The task has packets left to process. |
| 286 */ |
| 287 var STATE_RUNNABLE = 1; |
| 288 |
| 289 /** |
| 290 * The task is not currently running. The task is not blocked as such and may |
| 291 * be started by the scheduler. |
| 292 */ |
| 293 var STATE_SUSPENDED = 2; |
| 294 |
| 295 /** |
| 296 * The task is blocked and cannot be run until it is explicitly released. |
| 297 */ |
| 298 var STATE_HELD = 4; |
| 299 |
| 300 var STATE_SUSPENDED_RUNNABLE = STATE_SUSPENDED | STATE_RUNNABLE; |
| 301 var STATE_NOT_HELD = ~STATE_HELD; |
| 302 |
| 303 TaskControlBlock.prototype.setRunning = function () { |
| 304 this.state = STATE_RUNNING; |
| 305 }; |
| 306 |
| 307 TaskControlBlock.prototype.markAsNotHeld = function () { |
| 308 this.state = this.state & STATE_NOT_HELD; |
| 309 }; |
| 310 |
| 311 TaskControlBlock.prototype.markAsHeld = function () { |
| 312 this.state = this.state | STATE_HELD; |
| 313 }; |
| 314 |
| 315 TaskControlBlock.prototype.isHeldOrSuspended = function () { |
| 316 return (this.state & STATE_HELD) != 0 || (this.state == STATE_SUSPENDED); |
| 317 }; |
| 318 |
| 319 TaskControlBlock.prototype.markAsSuspended = function () { |
| 320 this.state = this.state | STATE_SUSPENDED; |
| 321 }; |
| 322 |
| 323 TaskControlBlock.prototype.markAsRunnable = function () { |
| 324 this.state = this.state | STATE_RUNNABLE; |
| 325 }; |
| 326 |
| 327 /** |
| 328 * Runs this task, if it is ready to be run, and returns the next task to run. |
| 329 */ |
| 330 TaskControlBlock.prototype.run = function () { |
| 331 var packet; |
| 332 if (this.state == STATE_SUSPENDED_RUNNABLE) { |
| 333 packet = this.queue; |
| 334 this.queue = packet.link; |
| 335 if (this.queue == null) { |
| 336 this.state = STATE_RUNNING; |
| 337 } else { |
| 338 this.state = STATE_RUNNABLE; |
| 339 } |
| 340 } else { |
| 341 packet = null; |
| 342 } |
| 343 return this.task.run(packet); |
| 344 }; |
| 345 |
| 346 /** |
| 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). |
| 350 */ |
| 351 TaskControlBlock.prototype.checkPriorityAdd = function (task, packet) { |
| 352 if (this.queue == null) { |
| 353 this.queue = packet; |
| 354 this.markAsRunnable(); |
| 355 if (this.priority > task.priority) return this; |
| 356 } else { |
| 357 this.queue = packet.addTo(this.queue); |
| 358 } |
| 359 return task; |
| 360 }; |
| 361 |
| 362 TaskControlBlock.prototype.toString = function () { |
| 363 return "tcb { " + this.task + "@" + this.state + " }"; |
| 364 }; |
| 365 |
| 366 /** |
| 367 * An idle task doesn't do any work itself but cycles control between the two |
| 368 * device tasks. |
| 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 |
| 372 * @constructor |
| 373 */ |
| 374 function IdleTask(scheduler, v1, count) { |
| 375 this.scheduler = scheduler; |
| 376 this.v1 = v1; |
| 377 this.count = count; |
| 378 } |
| 379 |
| 380 IdleTask.prototype.run = function (packet) { |
| 381 this.count--; |
| 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); |
| 386 } else { |
| 387 this.v1 = (this.v1 >> 1) ^ 0xD008; |
| 388 return this.scheduler.release(ID_DEVICE_B); |
| 389 } |
| 390 }; |
| 391 |
| 392 IdleTask.prototype.toString = function () { |
| 393 return "IdleTask" |
| 394 }; |
| 395 |
| 396 /** |
| 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 |
| 400 * @constructor |
| 401 */ |
| 402 function DeviceTask(scheduler) { |
| 403 this.scheduler = scheduler; |
| 404 this.v1 = null; |
| 405 } |
| 406 |
| 407 DeviceTask.prototype.run = function (packet) { |
| 408 if (packet == null) { |
| 409 if (this.v1 == null) return this.scheduler.suspendCurrent(); |
| 410 var v = this.v1; |
| 411 this.v1 = null; |
| 412 return this.scheduler.queue(v); |
| 413 } else { |
| 414 this.v1 = packet; |
| 415 return this.scheduler.holdCurrent(); |
| 416 } |
| 417 }; |
| 418 |
| 419 DeviceTask.prototype.toString = function () { |
| 420 return "DeviceTask"; |
| 421 }; |
| 422 |
| 423 /** |
| 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 |
| 428 * @constructor |
| 429 */ |
| 430 function WorkerTask(scheduler, v1, v2) { |
| 431 this.scheduler = scheduler; |
| 432 this.v1 = v1; |
| 433 this.v2 = v2; |
| 434 } |
| 435 |
| 436 WorkerTask.prototype.run = function (packet) { |
| 437 if (packet == null) { |
| 438 return this.scheduler.suspendCurrent(); |
| 439 } else { |
| 440 if (this.v1 == ID_HANDLER_A) { |
| 441 this.v1 = ID_HANDLER_B; |
| 442 } else { |
| 443 this.v1 = ID_HANDLER_A; |
| 444 } |
| 445 packet.id = this.v1; |
| 446 packet.a1 = 0; |
| 447 for (var i = 0; i < DATA_SIZE; i++) { |
| 448 this.v2++; |
| 449 if (this.v2 > 26) this.v2 = 1; |
| 450 packet.a2[i] = this.v2; |
| 451 } |
| 452 return this.scheduler.queue(packet); |
| 453 } |
| 454 }; |
| 455 |
| 456 WorkerTask.prototype.toString = function () { |
| 457 return "WorkerTask"; |
| 458 }; |
| 459 |
| 460 /** |
| 461 * A task that manipulates work packets and then suspends itself. |
| 462 * @param {Scheduler} scheduler the scheduler that manages this task |
| 463 * @constructor |
| 464 */ |
| 465 function HandlerTask(scheduler) { |
| 466 this.scheduler = scheduler; |
| 467 this.v1 = null; |
| 468 this.v2 = null; |
| 469 } |
| 470 |
| 471 HandlerTask.prototype.run = function (packet) { |
| 472 if (packet != null) { |
| 473 if (packet.kind == KIND_WORK) { |
| 474 this.v1 = packet.addTo(this.v1); |
| 475 } else { |
| 476 this.v2 = packet.addTo(this.v2); |
| 477 } |
| 478 } |
| 479 if (this.v1 != null) { |
| 480 var count = this.v1.a1; |
| 481 var v; |
| 482 if (count < DATA_SIZE) { |
| 483 if (this.v2 != null) { |
| 484 v = this.v2; |
| 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); |
| 489 } |
| 490 } else { |
| 491 v = this.v1; |
| 492 this.v1 = this.v1.link; |
| 493 return this.scheduler.queue(v); |
| 494 } |
| 495 } |
| 496 return this.scheduler.suspendCurrent(); |
| 497 }; |
| 498 |
| 499 HandlerTask.prototype.toString = function () { |
| 500 return "HandlerTask"; |
| 501 }; |
| 502 |
| 503 /* --- * |
| 504 * P a c k e t |
| 505 * --- */ |
| 506 |
| 507 var DATA_SIZE = 4; |
| 508 |
| 509 /** |
| 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. |
| 513 * |
| 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 |
| 519 * @constructor |
| 520 */ |
| 521 function Packet(link, id, kind) { |
| 522 this.link = link; |
| 523 this.id = id; |
| 524 this.kind = kind; |
| 525 this.a1 = 0; |
| 526 this.a2 = new Array(DATA_SIZE); |
| 527 } |
| 528 |
| 529 /** |
| 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 |
| 532 */ |
| 533 Packet.prototype.addTo = function (queue) { |
| 534 this.link = null; |
| 535 if (queue == null) return this; |
| 536 var peek, next = queue; |
| 537 while ((peek = next.link) != null) |
| 538 next = peek; |
| 539 next.link = this; |
| 540 return queue; |
| 541 }; |
| 542 |
| 543 Packet.prototype.toString = function () { |
| 544 return "Packet"; |
| 545 }; |
| 546 </script> |
| 547 </head> |
| 548 <body></body> |
| 549 </html> |
OLD | NEW |