OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 |
| 29 // This is a JavaScript implementation of the Richards |
| 30 // benchmark from: |
| 31 // |
| 32 // http://www.cl.cam.ac.uk/~mr10/Bench.html |
| 33 // |
| 34 // The benchmark was originally implemented in BCPL by |
| 35 // Martin Richards. |
| 36 |
| 37 |
| 38 function RichardsBenchmark(param) { |
| 39 COUNT = param; |
| 40 switch(param) { |
| 41 case 1000: |
| 42 EXPECTED_QUEUE_COUNT = 2322; |
| 43 EXPECTED_HOLD_COUNT = 928; |
| 44 break; |
| 45 case 10000: |
| 46 EXPECTED_QUEUE_COUNT = 23246; |
| 47 EXPECTED_HOLD_COUNT = 9297; |
| 48 break; |
| 49 case 1000000: |
| 50 EXPECTED_QUEUE_COUNT = 2326410; |
| 51 EXPECTED_HOLD_COUNT = 930563; |
| 52 break; |
| 53 } |
| 54 runRichards(); |
| 55 } |
| 56 |
| 57 /** |
| 58 * The Richards benchmark simulates the task dispatcher of an |
| 59 * operating system. |
| 60 **/ |
| 61 function runRichards() { |
| 62 var scheduler = new Scheduler(); |
| 63 scheduler.addIdleTask(ID_IDLE, 0, null, COUNT); |
| 64 |
| 65 var queue = new Packet(null, ID_WORKER, KIND_WORK); |
| 66 queue = new Packet(queue, ID_WORKER, KIND_WORK); |
| 67 scheduler.addWorkerTask(ID_WORKER, 1000, queue); |
| 68 |
| 69 queue = new Packet(null, ID_DEVICE_A, KIND_DEVICE); |
| 70 queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE); |
| 71 queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE); |
| 72 scheduler.addHandlerTask(ID_HANDLER_A, 2000, queue); |
| 73 |
| 74 queue = new Packet(null, ID_DEVICE_B, KIND_DEVICE); |
| 75 queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE); |
| 76 queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE); |
| 77 scheduler.addHandlerTask(ID_HANDLER_B, 3000, queue); |
| 78 |
| 79 scheduler.addDeviceTask(ID_DEVICE_A, 4000, null); |
| 80 |
| 81 scheduler.addDeviceTask(ID_DEVICE_B, 5000, null); |
| 82 |
| 83 scheduler.schedule(); |
| 84 |
| 85 if (scheduler.queueCount != EXPECTED_QUEUE_COUNT || |
| 86 scheduler.holdCount != EXPECTED_HOLD_COUNT) { |
| 87 var msg = |
| 88 "Error during execution: queueCount = " + scheduler.queueCount + |
| 89 ", holdCount = " + scheduler.holdCount + "."; |
| 90 throw new Error(msg); |
| 91 } |
| 92 } |
| 93 |
| 94 var COUNT = 1000; |
| 95 |
| 96 /** |
| 97 v * These two constants specify how many times a packet is queued and |
| 98 * how many times a task is put on hold in a correct run of richards. |
| 99 * They don't have any meaning a such but are characteristic of a |
| 100 * correct run so if the actual queue or hold count is different from |
| 101 * the expected there must be a bug in the implementation. |
| 102 **/ |
| 103 var EXPECTED_QUEUE_COUNT = 2322; |
| 104 var EXPECTED_HOLD_COUNT = 928; |
| 105 |
| 106 |
| 107 /** |
| 108 * A scheduler can be used to schedule a set of tasks based on their relative |
| 109 * priorities. Scheduling is done by maintaining a list of task control blocks |
| 110 * which holds tasks and the data queue they are processing. |
| 111 * @constructor |
| 112 */ |
| 113 function Scheduler() { |
| 114 this.queueCount = 0; |
| 115 this.holdCount = 0; |
| 116 this.blocks = new Array(NUMBER_OF_IDS); |
| 117 this.list = null; |
| 118 this.currentTcb = null; |
| 119 this.currentId = null; |
| 120 } |
| 121 |
| 122 var ID_IDLE = 0; |
| 123 var ID_WORKER = 1; |
| 124 var ID_HANDLER_A = 2; |
| 125 var ID_HANDLER_B = 3; |
| 126 var ID_DEVICE_A = 4; |
| 127 var ID_DEVICE_B = 5; |
| 128 var NUMBER_OF_IDS = 6; |
| 129 |
| 130 var KIND_DEVICE = 0; |
| 131 var KIND_WORK = 1; |
| 132 |
| 133 /** |
| 134 * Add an idle task to this scheduler. |
| 135 * @param {int} id the identity of the task |
| 136 * @param {int} priority the task's priority |
| 137 * @param {Packet} queue the queue of work to be processed by the task |
| 138 * @param {int} count the number of times to schedule the task |
| 139 */ |
| 140 Scheduler.prototype.addIdleTask = function (id, priority, queue, count) { |
| 141 this.addRunningTask(id, priority, queue, new IdleTask(this, 1, count)); |
| 142 }; |
| 143 |
| 144 /** |
| 145 * Add a work task to this scheduler. |
| 146 * @param {int} id the identity of the task |
| 147 * @param {int} priority the task's priority |
| 148 * @param {Packet} queue the queue of work to be processed by the task |
| 149 */ |
| 150 Scheduler.prototype.addWorkerTask = function (id, priority, queue) { |
| 151 this.addTask(id, priority, queue, new WorkerTask(this, ID_HANDLER_A, 0)); |
| 152 }; |
| 153 |
| 154 /** |
| 155 * Add a handler task to this scheduler. |
| 156 * @param {int} id the identity of the task |
| 157 * @param {int} priority the task's priority |
| 158 * @param {Packet} queue the queue of work to be processed by the task |
| 159 */ |
| 160 Scheduler.prototype.addHandlerTask = function (id, priority, queue) { |
| 161 this.addTask(id, priority, queue, new HandlerTask(this)); |
| 162 }; |
| 163 |
| 164 /** |
| 165 * Add a handler task to this scheduler. |
| 166 * @param {int} id the identity of the task |
| 167 * @param {int} priority the task's priority |
| 168 * @param {Packet} queue the queue of work to be processed by the task |
| 169 */ |
| 170 Scheduler.prototype.addDeviceTask = function (id, priority, queue) { |
| 171 this.addTask(id, priority, queue, new DeviceTask(this)) |
| 172 }; |
| 173 |
| 174 /** |
| 175 * Add the specified task and mark it as running. |
| 176 * @param {int} id the identity of the task |
| 177 * @param {int} priority the task's priority |
| 178 * @param {Packet} queue the queue of work to be processed by the task |
| 179 * @param {Task} task the task to add |
| 180 */ |
| 181 Scheduler.prototype.addRunningTask = function (id, priority, queue, task) { |
| 182 this.addTask(id, priority, queue, task); |
| 183 this.currentTcb.setRunning(); |
| 184 }; |
| 185 |
| 186 /** |
| 187 * Add the specified task to this scheduler. |
| 188 * @param {int} id the identity of the task |
| 189 * @param {int} priority the task's priority |
| 190 * @param {Packet} queue the queue of work to be processed by the task |
| 191 * @param {Task} task the task to add |
| 192 */ |
| 193 Scheduler.prototype.addTask = function (id, priority, queue, task) { |
| 194 this.currentTcb = new TaskControlBlock(this.list, id, priority, queue, task); |
| 195 this.list = this.currentTcb; |
| 196 this.blocks[id] = this.currentTcb; |
| 197 }; |
| 198 |
| 199 /** |
| 200 * Execute the tasks managed by this scheduler. |
| 201 */ |
| 202 Scheduler.prototype.schedule = function () { |
| 203 this.currentTcb = this.list; |
| 204 while (this.currentTcb != null) { |
| 205 if (this.currentTcb.isHeldOrSuspended()) { |
| 206 this.currentTcb = this.currentTcb.link; |
| 207 } else { |
| 208 this.currentId = this.currentTcb.id; |
| 209 this.currentTcb = this.currentTcb.run(); |
| 210 } |
| 211 } |
| 212 }; |
| 213 |
| 214 /** |
| 215 * Release a task that is currently blocked and return the next block to run. |
| 216 * @param {int} id the id of the task to suspend |
| 217 */ |
| 218 Scheduler.prototype.release = function (id) { |
| 219 var tcb = this.blocks[id]; |
| 220 if (tcb == null) return tcb; |
| 221 tcb.markAsNotHeld(); |
| 222 if (tcb.priority > this.currentTcb.priority) { |
| 223 return tcb; |
| 224 } else { |
| 225 return this.currentTcb; |
| 226 } |
| 227 }; |
| 228 |
| 229 /** |
| 230 * Block the currently executing task and return the next task control block |
| 231 * to run. The blocked task will not be made runnable until it is explicitly |
| 232 * released, even if new work is added to it. |
| 233 */ |
| 234 Scheduler.prototype.holdCurrent = function () { |
| 235 this.holdCount++; |
| 236 this.currentTcb.markAsHeld(); |
| 237 return this.currentTcb.link; |
| 238 }; |
| 239 |
| 240 /** |
| 241 * Suspend the currently executing task and return the next task control block |
| 242 * to run. If new work is added to the suspended task it will be made runnable. |
| 243 */ |
| 244 Scheduler.prototype.suspendCurrent = function () { |
| 245 this.currentTcb.markAsSuspended(); |
| 246 return this.currentTcb; |
| 247 }; |
| 248 |
| 249 /** |
| 250 * Add the specified packet to the end of the worklist used by the task |
| 251 * associated with the packet and make the task runnable if it is currently |
| 252 * suspended. |
| 253 * @param {Packet} packet the packet to add |
| 254 */ |
| 255 Scheduler.prototype.queue = function (packet) { |
| 256 var t = this.blocks[packet.id]; |
| 257 if (t == null) return t; |
| 258 this.queueCount++; |
| 259 packet.link = null; |
| 260 packet.id = this.currentId; |
| 261 return t.checkPriorityAdd(this.currentTcb, packet); |
| 262 }; |
| 263 |
| 264 /** |
| 265 * A task control block manages a task and the queue of work packages associated |
| 266 * with it. |
| 267 * @param {TaskControlBlock} link the preceding block in the linked block list |
| 268 * @param {int} id the id of this block |
| 269 * @param {int} priority the priority of this block |
| 270 * @param {Packet} queue the queue of packages to be processed by the task |
| 271 * @param {Task} task the task |
| 272 * @constructor |
| 273 */ |
| 274 function TaskControlBlock(link, id, priority, queue, task) { |
| 275 this.link = link; |
| 276 this.id = id; |
| 277 this.priority = priority; |
| 278 this.queue = queue; |
| 279 this.task = task; |
| 280 if (queue == null) { |
| 281 this.state = STATE_SUSPENDED; |
| 282 } else { |
| 283 this.state = STATE_SUSPENDED_RUNNABLE; |
| 284 } |
| 285 } |
| 286 |
| 287 /** |
| 288 * The task is running and is currently scheduled. |
| 289 */ |
| 290 var STATE_RUNNING = 0; |
| 291 |
| 292 /** |
| 293 * The task has packets left to process. |
| 294 */ |
| 295 var STATE_RUNNABLE = 1; |
| 296 |
| 297 /** |
| 298 * The task is not currently running. The task is not blocked as such and may |
| 299 * be started by the scheduler. |
| 300 */ |
| 301 var STATE_SUSPENDED = 2; |
| 302 |
| 303 /** |
| 304 * The task is blocked and cannot be run until it is explicitly released. |
| 305 */ |
| 306 var STATE_HELD = 4; |
| 307 |
| 308 var STATE_SUSPENDED_RUNNABLE = STATE_SUSPENDED | STATE_RUNNABLE; |
| 309 var STATE_NOT_HELD = ~STATE_HELD; |
| 310 |
| 311 TaskControlBlock.prototype.setRunning = function () { |
| 312 this.state = STATE_RUNNING; |
| 313 }; |
| 314 |
| 315 TaskControlBlock.prototype.markAsNotHeld = function () { |
| 316 this.state = this.state & STATE_NOT_HELD; |
| 317 }; |
| 318 |
| 319 TaskControlBlock.prototype.markAsHeld = function () { |
| 320 this.state = this.state | STATE_HELD; |
| 321 }; |
| 322 |
| 323 TaskControlBlock.prototype.isHeldOrSuspended = function () { |
| 324 return (this.state & STATE_HELD) != 0 || (this.state == STATE_SUSPENDED); |
| 325 }; |
| 326 |
| 327 TaskControlBlock.prototype.markAsSuspended = function () { |
| 328 this.state = this.state | STATE_SUSPENDED; |
| 329 }; |
| 330 |
| 331 TaskControlBlock.prototype.markAsRunnable = function () { |
| 332 this.state = this.state | STATE_RUNNABLE; |
| 333 }; |
| 334 |
| 335 /** |
| 336 * Runs this task, if it is ready to be run, and returns the next task to run. |
| 337 */ |
| 338 TaskControlBlock.prototype.run = function () { |
| 339 var packet; |
| 340 if (this.state == STATE_SUSPENDED_RUNNABLE) { |
| 341 packet = this.queue; |
| 342 this.queue = packet.link; |
| 343 if (this.queue == null) { |
| 344 this.state = STATE_RUNNING; |
| 345 } else { |
| 346 this.state = STATE_RUNNABLE; |
| 347 } |
| 348 } else { |
| 349 packet = null; |
| 350 } |
| 351 return this.task.run(packet); |
| 352 }; |
| 353 |
| 354 /** |
| 355 * Adds a packet to the worklist of this block's task, marks this as runnable if |
| 356 * necessary, and returns the next runnable object to run (the one |
| 357 * with the highest priority). |
| 358 */ |
| 359 TaskControlBlock.prototype.checkPriorityAdd = function (task, packet) { |
| 360 if (this.queue == null) { |
| 361 this.queue = packet; |
| 362 this.markAsRunnable(); |
| 363 if (this.priority > task.priority) return this; |
| 364 } else { |
| 365 this.queue = packet.addTo(this.queue); |
| 366 } |
| 367 return task; |
| 368 }; |
| 369 |
| 370 TaskControlBlock.prototype.toString = function () { |
| 371 return "tcb { " + this.task + "@" + this.state + " }"; |
| 372 }; |
| 373 |
| 374 /** |
| 375 * An idle task doesn't do any work itself but cycles control between the two |
| 376 * device tasks. |
| 377 * @param {Scheduler} scheduler the scheduler that manages this task |
| 378 * @param {int} v1 a seed value that controls how the device tasks are scheduled |
| 379 * @param {int} count the number of times this task should be scheduled |
| 380 * @constructor |
| 381 */ |
| 382 function IdleTask(scheduler, v1, count) { |
| 383 this.scheduler = scheduler; |
| 384 this.v1 = v1; |
| 385 this.count = count; |
| 386 } |
| 387 |
| 388 IdleTask.prototype.run = function (packet) { |
| 389 this.count--; |
| 390 if (this.count == 0) return this.scheduler.holdCurrent(); |
| 391 if ((this.v1 & 1) == 0) { |
| 392 this.v1 = this.v1 >> 1; |
| 393 return this.scheduler.release(ID_DEVICE_A); |
| 394 } else { |
| 395 this.v1 = (this.v1 >> 1) ^ 0xD008; |
| 396 return this.scheduler.release(ID_DEVICE_B); |
| 397 } |
| 398 }; |
| 399 |
| 400 IdleTask.prototype.toString = function () { |
| 401 return "IdleTask" |
| 402 }; |
| 403 |
| 404 /** |
| 405 * A task that suspends itself after each time it has been run to simulate |
| 406 * waiting for data from an external device. |
| 407 * @param {Scheduler} scheduler the scheduler that manages this task |
| 408 * @constructor |
| 409 */ |
| 410 function DeviceTask(scheduler) { |
| 411 this.scheduler = scheduler; |
| 412 this.v1 = null; |
| 413 } |
| 414 |
| 415 DeviceTask.prototype.run = function (packet) { |
| 416 if (packet == null) { |
| 417 if (this.v1 == null) return this.scheduler.suspendCurrent(); |
| 418 var v = this.v1; |
| 419 this.v1 = null; |
| 420 return this.scheduler.queue(v); |
| 421 } else { |
| 422 this.v1 = packet; |
| 423 return this.scheduler.holdCurrent(); |
| 424 } |
| 425 }; |
| 426 |
| 427 DeviceTask.prototype.toString = function () { |
| 428 return "DeviceTask"; |
| 429 }; |
| 430 |
| 431 /** |
| 432 * A task that manipulates work packets. |
| 433 * @param {Scheduler} scheduler the scheduler that manages this task |
| 434 * @param {int} v1 a seed used to specify how work packets are manipulated |
| 435 * @param {int} v2 another seed used to specify how work packets are manipulated |
| 436 * @constructor |
| 437 */ |
| 438 function WorkerTask(scheduler, v1, v2) { |
| 439 this.scheduler = scheduler; |
| 440 this.v1 = v1; |
| 441 this.v2 = v2; |
| 442 } |
| 443 |
| 444 WorkerTask.prototype.run = function (packet) { |
| 445 if (packet == null) { |
| 446 return this.scheduler.suspendCurrent(); |
| 447 } else { |
| 448 if (this.v1 == ID_HANDLER_A) { |
| 449 this.v1 = ID_HANDLER_B; |
| 450 } else { |
| 451 this.v1 = ID_HANDLER_A; |
| 452 } |
| 453 packet.id = this.v1; |
| 454 packet.a1 = 0; |
| 455 for (var i = 0; i < DATA_SIZE; i++) { |
| 456 this.v2++; |
| 457 if (this.v2 > 26) this.v2 = 1; |
| 458 packet.a2[i] = this.v2; |
| 459 } |
| 460 return this.scheduler.queue(packet); |
| 461 } |
| 462 }; |
| 463 |
| 464 WorkerTask.prototype.toString = function () { |
| 465 return "WorkerTask"; |
| 466 }; |
| 467 |
| 468 /** |
| 469 * A task that manipulates work packets and then suspends itself. |
| 470 * @param {Scheduler} scheduler the scheduler that manages this task |
| 471 * @constructor |
| 472 */ |
| 473 function HandlerTask(scheduler) { |
| 474 this.scheduler = scheduler; |
| 475 this.v1 = null; |
| 476 this.v2 = null; |
| 477 } |
| 478 |
| 479 HandlerTask.prototype.run = function (packet) { |
| 480 if (packet != null) { |
| 481 if (packet.kind == KIND_WORK) { |
| 482 this.v1 = packet.addTo(this.v1); |
| 483 } else { |
| 484 this.v2 = packet.addTo(this.v2); |
| 485 } |
| 486 } |
| 487 if (this.v1 != null) { |
| 488 var count = this.v1.a1; |
| 489 var v; |
| 490 if (count < DATA_SIZE) { |
| 491 if (this.v2 != null) { |
| 492 v = this.v2; |
| 493 this.v2 = this.v2.link; |
| 494 v.a1 = this.v1.a2[count]; |
| 495 this.v1.a1 = count + 1; |
| 496 return this.scheduler.queue(v); |
| 497 } |
| 498 } else { |
| 499 v = this.v1; |
| 500 this.v1 = this.v1.link; |
| 501 return this.scheduler.queue(v); |
| 502 } |
| 503 } |
| 504 return this.scheduler.suspendCurrent(); |
| 505 }; |
| 506 |
| 507 HandlerTask.prototype.toString = function () { |
| 508 return "HandlerTask"; |
| 509 }; |
| 510 |
| 511 /* --- * |
| 512 * P a c k e t |
| 513 * --- */ |
| 514 |
| 515 var DATA_SIZE = 4; |
| 516 |
| 517 /** |
| 518 * A simple package of data that is manipulated by the tasks. The exact layout |
| 519 * of the payload data carried by a packet is not importaint, and neither is the |
| 520 * nature of the work performed on packets by the tasks. |
| 521 * |
| 522 * Besides carrying data, packets form linked lists and are hence used both as |
| 523 * data and worklists. |
| 524 * @param {Packet} link the tail of the linked list of packets |
| 525 * @param {int} id an ID for this packet |
| 526 * @param {int} kind the type of this packet |
| 527 * @constructor |
| 528 */ |
| 529 function Packet(link, id, kind) { |
| 530 this.link = link; |
| 531 this.id = id; |
| 532 this.kind = kind; |
| 533 this.a1 = 0; |
| 534 this.a2 = new Array(DATA_SIZE); |
| 535 } |
| 536 |
| 537 /** |
| 538 * Add this packet to the end of a worklist, and return the worklist. |
| 539 * @param {Packet} queue the worklist to add this packet to |
| 540 */ |
| 541 Packet.prototype.addTo = function (queue) { |
| 542 this.link = null; |
| 543 if (queue == null) return this; |
| 544 var peek, next = queue; |
| 545 while ((peek = next.link) != null) |
| 546 next = peek; |
| 547 next.link = this; |
| 548 return queue; |
| 549 }; |
| 550 |
| 551 Packet.prototype.toString = function () { |
| 552 return "Packet"; |
| 553 }; |
OLD | NEW |