porffor 0.49.6 → 0.49.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/compiler/2c.js +7 -4
- package/compiler/builtins/console.ts +8 -8
- package/compiler/builtins/date.ts +1 -1
- package/compiler/builtins/math.ts +7 -2
- package/compiler/builtins/string.ts +8 -8
- package/compiler/builtins_precompiled.js +58 -58
- package/compiler/codegen.js +168 -60
- package/compiler/cyclone.js +6 -2
- package/compiler/index.js +2 -0
- package/compiler/parse.js +1 -1
- package/compiler/precompile.js +4 -4
- package/package.json +1 -1
- package/runner/debug.js +0 -1
- package/runner/flamegraph.js +205 -0
- package/runner/{profile.js → hotlines.js} +0 -1
- package/runner/index.js +34 -34
- package/richards.js +0 -913
package/richards.js
DELETED
@@ -1,913 +0,0 @@
|
|
1
|
-
// Copyright 2013 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
|
-
// Simple framework for running the benchmark suites and
|
29
|
-
// computing a score based on the timing measurements.
|
30
|
-
|
31
|
-
// A benchmark has a name (string) and a function that will be run to
|
32
|
-
// do the performance measurement. The optional setup and tearDown
|
33
|
-
// arguments are functions that will be invoked before and after
|
34
|
-
// running the benchmark, but the running time of these functions will
|
35
|
-
// not be accounted for in the benchmark score.
|
36
|
-
function Benchmark(
|
37
|
-
name,
|
38
|
-
doWarmup,
|
39
|
-
doDeterministic,
|
40
|
-
deterministicIterations,
|
41
|
-
run,
|
42
|
-
setup,
|
43
|
-
tearDown,
|
44
|
-
rmsResult,
|
45
|
-
minIterations
|
46
|
-
) {
|
47
|
-
this.name = name;
|
48
|
-
this.doWarmup = doWarmup;
|
49
|
-
this.doDeterministic = doDeterministic;
|
50
|
-
this.deterministicIterations = deterministicIterations;
|
51
|
-
this.run = run;
|
52
|
-
this.Setup = setup ? setup : function () {};
|
53
|
-
this.TearDown = tearDown ? tearDown : function () {};
|
54
|
-
this.rmsResult = rmsResult ? rmsResult : null;
|
55
|
-
this.minIterations = minIterations ? minIterations : 32;
|
56
|
-
}
|
57
|
-
|
58
|
-
// Benchmark results hold the benchmark and the measured time used to
|
59
|
-
// run the benchmark. The benchmark score is computed later once a
|
60
|
-
// full benchmark suite has run to completion. If latency is set to 0
|
61
|
-
// then there is no latency score for this benchmark.
|
62
|
-
function BenchmarkResult(benchmark, time, latency) {
|
63
|
-
this.benchmark = benchmark;
|
64
|
-
this.time = time;
|
65
|
-
this.latency = latency;
|
66
|
-
}
|
67
|
-
|
68
|
-
// Automatically convert results to numbers. Used by the geometric
|
69
|
-
// mean computation.
|
70
|
-
BenchmarkResult.prototype.valueOf = function () {
|
71
|
-
return this.time;
|
72
|
-
};
|
73
|
-
|
74
|
-
// Suites of benchmarks consist of a name and the set of benchmarks in
|
75
|
-
// addition to the reference timing that the final score will be based
|
76
|
-
// on. This way, all scores are relative to a reference run and higher
|
77
|
-
// scores implies better performance.
|
78
|
-
function BenchmarkSuite(name, reference, benchmarks) {
|
79
|
-
this.name = name;
|
80
|
-
this.reference = reference;
|
81
|
-
this.benchmarks = benchmarks;
|
82
|
-
BenchmarkSuite.suites.push(this);
|
83
|
-
}
|
84
|
-
|
85
|
-
// Keep track of all declared benchmark suites.
|
86
|
-
BenchmarkSuite.suites = [];
|
87
|
-
|
88
|
-
// Scores are not comparable across versions. Bump the version if
|
89
|
-
// you're making changes that will affect that scores, e.g. if you add
|
90
|
-
// a new benchmark or change an existing one.
|
91
|
-
BenchmarkSuite.version = "9";
|
92
|
-
|
93
|
-
// Defines global benchsuite running mode that overrides benchmark suite
|
94
|
-
// behavior. Intended to be set by the benchmark driver. Undefined
|
95
|
-
// values here allow a benchmark to define behaviour itself.
|
96
|
-
BenchmarkSuite.config = {
|
97
|
-
doWarmup: undefined,
|
98
|
-
doDeterministic: undefined,
|
99
|
-
};
|
100
|
-
|
101
|
-
// To make the benchmark results predictable, we replace Math.random
|
102
|
-
// with a 100% deterministic alternative.
|
103
|
-
BenchmarkSuite.ResetRNG = function () {
|
104
|
-
Math.random = (function () {
|
105
|
-
var seed = 49734321;
|
106
|
-
return function () {
|
107
|
-
// Robert Jenkins' 32 bit integer hash function.
|
108
|
-
seed = (seed + 0x7ed55d16 + (seed << 12)) & 0xffffffff;
|
109
|
-
seed = (seed ^ 0xc761c23c ^ (seed >>> 19)) & 0xffffffff;
|
110
|
-
seed = (seed + 0x165667b1 + (seed << 5)) & 0xffffffff;
|
111
|
-
seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
|
112
|
-
seed = (seed + 0xfd7046c5 + (seed << 3)) & 0xffffffff;
|
113
|
-
seed = (seed ^ 0xb55a4f09 ^ (seed >>> 16)) & 0xffffffff;
|
114
|
-
return (seed & 0xfffffff) / 0x10000000;
|
115
|
-
};
|
116
|
-
})();
|
117
|
-
};
|
118
|
-
|
119
|
-
// Runs all registered benchmark suites and optionally yields between
|
120
|
-
// each individual benchmark to avoid running for too long in the
|
121
|
-
// context of browsers. Once done, the final score is reported to the
|
122
|
-
// runner.
|
123
|
-
BenchmarkSuite.RunSuites = function (runner, skipBenchmarks) {
|
124
|
-
skipBenchmarks = typeof skipBenchmarks === "undefined" ? [] : skipBenchmarks;
|
125
|
-
var continuation = null;
|
126
|
-
var suites = BenchmarkSuite.suites;
|
127
|
-
var length = suites.length;
|
128
|
-
BenchmarkSuite.scores = [];
|
129
|
-
var index = 0;
|
130
|
-
while (continuation || index < length) {
|
131
|
-
if (continuation) {
|
132
|
-
continuation = continuation();
|
133
|
-
} else {
|
134
|
-
var suite = suites[index++];
|
135
|
-
if (runner.NotifyStart) runner.NotifyStart(suite.name);
|
136
|
-
if (skipBenchmarks.indexOf(suite.name) > -1) {
|
137
|
-
suite.NotifySkipped(runner);
|
138
|
-
} else {
|
139
|
-
continuation = suite.RunStep(runner);
|
140
|
-
}
|
141
|
-
}
|
142
|
-
if (continuation && typeof window != "undefined" && window.setTimeout) {
|
143
|
-
window.setTimeout(RunStep, 25);
|
144
|
-
return;
|
145
|
-
}
|
146
|
-
}
|
147
|
-
|
148
|
-
// show final result
|
149
|
-
if (runner.NotifyScore) {
|
150
|
-
var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores);
|
151
|
-
var formatted = BenchmarkSuite.FormatScore(100 * score);
|
152
|
-
runner.NotifyScore(formatted);
|
153
|
-
}
|
154
|
-
};
|
155
|
-
|
156
|
-
// Counts the total number of registered benchmarks. Useful for
|
157
|
-
// showing progress as a percentage.
|
158
|
-
BenchmarkSuite.CountBenchmarks = function () {
|
159
|
-
var result = 0;
|
160
|
-
var suites = BenchmarkSuite.suites;
|
161
|
-
for (var i = 0; i < suites.length; i++) {
|
162
|
-
result += suites[i].benchmarks.length;
|
163
|
-
}
|
164
|
-
return result;
|
165
|
-
};
|
166
|
-
|
167
|
-
// Computes the geometric mean of a set of numbers.
|
168
|
-
BenchmarkSuite.GeometricMean = function (numbers) {
|
169
|
-
var log = 0;
|
170
|
-
for (var i = 0; i < numbers.length; i++) {
|
171
|
-
log += Math.log(numbers[i]);
|
172
|
-
}
|
173
|
-
return Math.pow(Math.E, log / numbers.length);
|
174
|
-
};
|
175
|
-
|
176
|
-
// Computes the geometric mean of a set of throughput time measurements.
|
177
|
-
BenchmarkSuite.GeometricMeanTime = function (measurements) {
|
178
|
-
var log = 0;
|
179
|
-
for (var i = 0; i < measurements.length; i++) {
|
180
|
-
log += Math.log(measurements[i].time);
|
181
|
-
}
|
182
|
-
return Math.pow(Math.E, log / measurements.length);
|
183
|
-
};
|
184
|
-
|
185
|
-
// Computes the geometric mean of a set of rms measurements.
|
186
|
-
BenchmarkSuite.GeometricMeanLatency = function (measurements) {
|
187
|
-
var log = 0;
|
188
|
-
var hasLatencyResult = false;
|
189
|
-
for (var i = 0; i < measurements.length; i++) {
|
190
|
-
if (measurements[i].latency != 0) {
|
191
|
-
log += Math.log(measurements[i].latency);
|
192
|
-
hasLatencyResult = true;
|
193
|
-
}
|
194
|
-
}
|
195
|
-
if (hasLatencyResult) {
|
196
|
-
return Math.pow(Math.E, log / measurements.length);
|
197
|
-
} else {
|
198
|
-
return 0;
|
199
|
-
}
|
200
|
-
};
|
201
|
-
|
202
|
-
// Converts a score value to a string with at least three significant
|
203
|
-
// digits.
|
204
|
-
BenchmarkSuite.FormatScore = function (value) {
|
205
|
-
return value.toFixed(0);
|
206
|
-
};
|
207
|
-
|
208
|
-
// Notifies the runner that we're done running a single benchmark in
|
209
|
-
// the benchmark suite. This can be useful to report progress.
|
210
|
-
BenchmarkSuite.prototype.NotifyStep = function (result) {
|
211
|
-
this.results.push(result);
|
212
|
-
if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name);
|
213
|
-
};
|
214
|
-
|
215
|
-
// Notifies the runner that we're done with running a suite and that
|
216
|
-
// we have a result which can be reported to the user if needed.
|
217
|
-
BenchmarkSuite.prototype.NotifyResult = function () {
|
218
|
-
var mean = BenchmarkSuite.GeometricMeanTime(this.results);
|
219
|
-
var score = this.reference[0] / mean;
|
220
|
-
BenchmarkSuite.scores.push(score);
|
221
|
-
if (this.runner.NotifyResult) {
|
222
|
-
var formatted = BenchmarkSuite.FormatScore(100 * score);
|
223
|
-
this.runner.NotifyResult(this.name, formatted);
|
224
|
-
}
|
225
|
-
if (this.reference.length == 2) {
|
226
|
-
var meanLatency = BenchmarkSuite.GeometricMeanLatency(this.results);
|
227
|
-
if (meanLatency != 0) {
|
228
|
-
var scoreLatency = this.reference[1] / meanLatency;
|
229
|
-
BenchmarkSuite.scores.push(scoreLatency);
|
230
|
-
if (this.runner.NotifyResult) {
|
231
|
-
var formattedLatency = BenchmarkSuite.FormatScore(100 * scoreLatency);
|
232
|
-
this.runner.NotifyResult(this.name + "Latency", formattedLatency);
|
233
|
-
}
|
234
|
-
}
|
235
|
-
}
|
236
|
-
};
|
237
|
-
|
238
|
-
BenchmarkSuite.prototype.NotifySkipped = function (runner) {
|
239
|
-
BenchmarkSuite.scores.push(1); // push default reference score.
|
240
|
-
if (runner.NotifyResult) {
|
241
|
-
runner.NotifyResult(this.name, "Skipped");
|
242
|
-
}
|
243
|
-
};
|
244
|
-
|
245
|
-
// Notifies the runner that running a benchmark resulted in an error.
|
246
|
-
BenchmarkSuite.prototype.NotifyError = function (error) {
|
247
|
-
if (this.runner.NotifyError) {
|
248
|
-
this.runner.NotifyError(this.name, error);
|
249
|
-
}
|
250
|
-
if (this.runner.NotifyStep) {
|
251
|
-
this.runner.NotifyStep(this.name);
|
252
|
-
}
|
253
|
-
};
|
254
|
-
|
255
|
-
// Runs a single benchmark for at least a second and computes the
|
256
|
-
// average time it takes to run a single iteration.
|
257
|
-
BenchmarkSuite.prototype.RunSingleBenchmark = function (benchmark, data) {
|
258
|
-
var config = BenchmarkSuite.config;
|
259
|
-
var doWarmup = config.doWarmup !== undefined ? config.doWarmup : benchmark.doWarmup;
|
260
|
-
var doDeterministic = config.doDeterministic !== undefined ? config.doDeterministic : benchmark.doDeterministic;
|
261
|
-
|
262
|
-
var elapsed = 0;
|
263
|
-
var runs = 0;
|
264
|
-
|
265
|
-
// Run either for 1 second or for the number of iterations specified
|
266
|
-
// by minIterations, depending on the config flag doDeterministic.
|
267
|
-
// for (var i = 0; doDeterministic ? i < benchmark.deterministicIterations : elapsed < 1000; i++) {
|
268
|
-
// benchmark.run();
|
269
|
-
// elapsed = performance.now() - start;
|
270
|
-
// }
|
271
|
-
|
272
|
-
// If we've run too few iterations, we continue for another second.
|
273
|
-
while (runs < benchmark.minIterations) {
|
274
|
-
// Run either for 1 second or for the number of iterations specified
|
275
|
-
// by minIterations, depending on the config flag doDeterministic.
|
276
|
-
var i = 0;
|
277
|
-
for (; doDeterministic ? i < benchmark.deterministicIterations : elapsed < 1000; i++) {
|
278
|
-
var start = performance.now();
|
279
|
-
benchmark.run();
|
280
|
-
elapsed += performance.now() - start;
|
281
|
-
}
|
282
|
-
|
283
|
-
runs += i;
|
284
|
-
}
|
285
|
-
|
286
|
-
console.log('did', runs, 'runs in', elapsed, 'ms');
|
287
|
-
var usec = (elapsed * 1000) / runs;
|
288
|
-
var rms = benchmark.rmsResult != null ? benchmark.rmsResult() : 0;
|
289
|
-
this.NotifyStep(new BenchmarkResult(benchmark, usec, rms));
|
290
|
-
|
291
|
-
return null;
|
292
|
-
};
|
293
|
-
|
294
|
-
// This function starts running a suite, but stops between each
|
295
|
-
// individual benchmark in the suite and returns a continuation
|
296
|
-
// function which can be invoked to run the next benchmark. Once the
|
297
|
-
// last benchmark has been executed, null is returned.
|
298
|
-
BenchmarkSuite.prototype.RunStep = function (runner) {
|
299
|
-
BenchmarkSuite.ResetRNG();
|
300
|
-
this.results = [];
|
301
|
-
this.runner = runner;
|
302
|
-
var length = this.benchmarks.length;
|
303
|
-
var index = 0;
|
304
|
-
var suite = this;
|
305
|
-
var data;
|
306
|
-
|
307
|
-
// Run the setup, the actual benchmark, and the tear down in three
|
308
|
-
// separate steps to allow the framework to yield between any of the
|
309
|
-
// steps.
|
310
|
-
|
311
|
-
try {
|
312
|
-
suite.benchmarks[index].Setup();
|
313
|
-
data = suite.RunSingleBenchmark(suite.benchmarks[index], data);
|
314
|
-
suite.benchmarks[index++].TearDown();
|
315
|
-
} catch (e) {
|
316
|
-
suite.NotifyError(e);
|
317
|
-
return null;
|
318
|
-
}
|
319
|
-
|
320
|
-
suite.NotifyResult();
|
321
|
-
return null;
|
322
|
-
};
|
323
|
-
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
324
|
-
// Redistribution and use in source and binary forms, with or without
|
325
|
-
// modification, are permitted provided that the following conditions are
|
326
|
-
// met:
|
327
|
-
//
|
328
|
-
// * Redistributions of source code must retain the above copyright
|
329
|
-
// notice, this list of conditions and the following disclaimer.
|
330
|
-
// * Redistributions in binary form must reproduce the above
|
331
|
-
// copyright notice, this list of conditions and the following
|
332
|
-
// disclaimer in the documentation and/or other materials provided
|
333
|
-
// with the distribution.
|
334
|
-
// * Neither the name of Google Inc. nor the names of its
|
335
|
-
// contributors may be used to endorse or promote products derived
|
336
|
-
// from this software without specific prior written permission.
|
337
|
-
//
|
338
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
339
|
-
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
340
|
-
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
341
|
-
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
342
|
-
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
343
|
-
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
344
|
-
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
345
|
-
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
346
|
-
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
347
|
-
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
348
|
-
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
349
|
-
|
350
|
-
// This is a JavaScript implementation of the Richards
|
351
|
-
// benchmark from:
|
352
|
-
//
|
353
|
-
// http://www.cl.cam.ac.uk/~mr10/Bench.html
|
354
|
-
//
|
355
|
-
// The benchmark was originally implemented in BCPL by
|
356
|
-
// Martin Richards.
|
357
|
-
|
358
|
-
var Richards = new BenchmarkSuite("Richards", [35302], [new Benchmark("Richards", true, false, 8200, runRichards)]);
|
359
|
-
|
360
|
-
var COUNT = 1000;
|
361
|
-
|
362
|
-
/**
|
363
|
-
* These two constants specify how many times a packet is queued and
|
364
|
-
* how many times a task is put on hold in a correct run of richards.
|
365
|
-
* They don't have any meaning a such but are characteristic of a
|
366
|
-
* correct run so if the actual queue or hold count is different from
|
367
|
-
* the expected there must be a bug in the implementation.
|
368
|
-
**/
|
369
|
-
var EXPECTED_QUEUE_COUNT = 2322;
|
370
|
-
var EXPECTED_HOLD_COUNT = 928;
|
371
|
-
|
372
|
-
/**
|
373
|
-
* A scheduler can be used to schedule a set of tasks based on their relative
|
374
|
-
* priorities. Scheduling is done by maintaining a list of task control blocks
|
375
|
-
* which holds tasks and the data queue they are processing.
|
376
|
-
* @constructor
|
377
|
-
*/
|
378
|
-
function Scheduler() {
|
379
|
-
this.queueCount = 0;
|
380
|
-
this.holdCount = 0;
|
381
|
-
this.blocks = new Array(NUMBER_OF_IDS);
|
382
|
-
this.list = null;
|
383
|
-
this.currentTcb = null;
|
384
|
-
this.currentId = null;
|
385
|
-
}
|
386
|
-
|
387
|
-
var ID_IDLE = 0;
|
388
|
-
var ID_WORKER = 1;
|
389
|
-
var ID_HANDLER_A = 2;
|
390
|
-
var ID_HANDLER_B = 3;
|
391
|
-
var ID_DEVICE_A = 4;
|
392
|
-
var ID_DEVICE_B = 5;
|
393
|
-
var NUMBER_OF_IDS = 6;
|
394
|
-
|
395
|
-
var KIND_DEVICE = 0;
|
396
|
-
var KIND_WORK = 1;
|
397
|
-
|
398
|
-
/**
|
399
|
-
* Add an idle task to this scheduler.
|
400
|
-
* @param {int} id the identity of the task
|
401
|
-
* @param {int} priority the task's priority
|
402
|
-
* @param {Packet} queue the queue of work to be processed by the task
|
403
|
-
* @param {int} count the number of times to schedule the task
|
404
|
-
*/
|
405
|
-
Scheduler.prototype.addIdleTask = function (id, priority, queue, count) {
|
406
|
-
this.addRunningTask(id, priority, queue, new IdleTask(this, 1, count));
|
407
|
-
};
|
408
|
-
|
409
|
-
/**
|
410
|
-
* Add a work task to this scheduler.
|
411
|
-
* @param {int} id the identity of the task
|
412
|
-
* @param {int} priority the task's priority
|
413
|
-
* @param {Packet} queue the queue of work to be processed by the task
|
414
|
-
*/
|
415
|
-
Scheduler.prototype.addWorkerTask = function (id, priority, queue) {
|
416
|
-
this.addTask(id, priority, queue, new WorkerTask(this, ID_HANDLER_A, 0));
|
417
|
-
};
|
418
|
-
|
419
|
-
/**
|
420
|
-
* Add a handler task to this scheduler.
|
421
|
-
* @param {int} id the identity of the task
|
422
|
-
* @param {int} priority the task's priority
|
423
|
-
* @param {Packet} queue the queue of work to be processed by the task
|
424
|
-
*/
|
425
|
-
Scheduler.prototype.addHandlerTask = function (id, priority, queue) {
|
426
|
-
this.addTask(id, priority, queue, new HandlerTask(this));
|
427
|
-
};
|
428
|
-
|
429
|
-
/**
|
430
|
-
* Add a handler task to this scheduler.
|
431
|
-
* @param {int} id the identity of the task
|
432
|
-
* @param {int} priority the task's priority
|
433
|
-
* @param {Packet} queue the queue of work to be processed by the task
|
434
|
-
*/
|
435
|
-
Scheduler.prototype.addDeviceTask = function (id, priority, queue) {
|
436
|
-
this.addTask(id, priority, queue, new DeviceTask(this));
|
437
|
-
};
|
438
|
-
|
439
|
-
/**
|
440
|
-
* Add the specified task and mark it as running.
|
441
|
-
* @param {int} id the identity of the task
|
442
|
-
* @param {int} priority the task's priority
|
443
|
-
* @param {Packet} queue the queue of work to be processed by the task
|
444
|
-
* @param {Task} task the task to add
|
445
|
-
*/
|
446
|
-
Scheduler.prototype.addRunningTask = function (id, priority, queue, task) {
|
447
|
-
this.addTask(id, priority, queue, task);
|
448
|
-
this.currentTcb.setRunning();
|
449
|
-
};
|
450
|
-
|
451
|
-
/**
|
452
|
-
* Add the specified task to this scheduler.
|
453
|
-
* @param {int} id the identity of the task
|
454
|
-
* @param {int} priority the task's priority
|
455
|
-
* @param {Packet} queue the queue of work to be processed by the task
|
456
|
-
* @param {Task} task the task to add
|
457
|
-
*/
|
458
|
-
Scheduler.prototype.addTask = function (id, priority, queue, task) {
|
459
|
-
// console.log('Scheduler.addTask', id);
|
460
|
-
this.currentTcb = new TaskControlBlock(this.list, id, priority, queue, task);
|
461
|
-
this.list = this.currentTcb;
|
462
|
-
this.blocks[id] = this.currentTcb;
|
463
|
-
};
|
464
|
-
|
465
|
-
/**
|
466
|
-
* Execute the tasks managed by this scheduler.
|
467
|
-
*/
|
468
|
-
Scheduler.prototype.schedule = function () {
|
469
|
-
this.currentTcb = this.list;
|
470
|
-
while (this.currentTcb != null) {
|
471
|
-
// console.log('Scheduler.schedule', this.currentTcb.task.__proto__.constructor.name, this.currentTcb.id, this.currentTcb.isHeldOrSuspended());
|
472
|
-
if (this.currentTcb.isHeldOrSuspended()) {
|
473
|
-
this.currentTcb = this.currentTcb.link;
|
474
|
-
} else {
|
475
|
-
this.currentId = this.currentTcb.id;
|
476
|
-
this.currentTcb = this.currentTcb.run();
|
477
|
-
}
|
478
|
-
}
|
479
|
-
};
|
480
|
-
|
481
|
-
/**
|
482
|
-
* Release a task that is currently blocked and return the next block to run.
|
483
|
-
* @param {int} id the id of the task to suspend
|
484
|
-
*/
|
485
|
-
Scheduler.prototype.release = function (id) {
|
486
|
-
var tcb = this.blocks[id];
|
487
|
-
if (tcb == null) return tcb;
|
488
|
-
tcb.markAsNotHeld();
|
489
|
-
if (tcb.priority > this.currentTcb.priority) {
|
490
|
-
return tcb;
|
491
|
-
} else {
|
492
|
-
return this.currentTcb;
|
493
|
-
}
|
494
|
-
};
|
495
|
-
|
496
|
-
/**
|
497
|
-
* Block the currently executing task and return the next task control block
|
498
|
-
* to run. The blocked task will not be made runnable until it is explicitly
|
499
|
-
* released, even if new work is added to it.
|
500
|
-
*/
|
501
|
-
Scheduler.prototype.holdCurrent = function () {
|
502
|
-
this.holdCount++;
|
503
|
-
this.currentTcb.markAsHeld();
|
504
|
-
return this.currentTcb.link;
|
505
|
-
};
|
506
|
-
|
507
|
-
/**
|
508
|
-
* Suspend the currently executing task and return the next task control block
|
509
|
-
* to run. If new work is added to the suspended task it will be made runnable.
|
510
|
-
*/
|
511
|
-
Scheduler.prototype.suspendCurrent = function () {
|
512
|
-
this.currentTcb.markAsSuspended();
|
513
|
-
return this.currentTcb;
|
514
|
-
};
|
515
|
-
|
516
|
-
/**
|
517
|
-
* Add the specified packet to the end of the worklist used by the task
|
518
|
-
* associated with the packet and make the task runnable if it is currently
|
519
|
-
* suspended.
|
520
|
-
* @param {Packet} packet the packet to add
|
521
|
-
*/
|
522
|
-
Scheduler.prototype.queue = function (packet) {
|
523
|
-
var t = this.blocks[packet.id];
|
524
|
-
// console.log('Scheduler.queue', packet.id, t == null);
|
525
|
-
if (t == null) return t;
|
526
|
-
this.queueCount++;
|
527
|
-
packet.link = null;
|
528
|
-
packet.id = this.currentId;
|
529
|
-
return t.checkPriorityAdd(this.currentTcb, packet);
|
530
|
-
};
|
531
|
-
|
532
|
-
/**
|
533
|
-
* A task control block manages a task and the queue of work packages associated
|
534
|
-
* with it.
|
535
|
-
* @param {TaskControlBlock} link the preceding block in the linked block list
|
536
|
-
* @param {int} id the id of this block
|
537
|
-
* @param {int} priority the priority of this block
|
538
|
-
* @param {Packet} queue the queue of packages to be processed by the task
|
539
|
-
* @param {Task} task the task
|
540
|
-
* @constructor
|
541
|
-
*/
|
542
|
-
function TaskControlBlock(link, id, priority, queue, task) {
|
543
|
-
this.link = link;
|
544
|
-
this.id = id;
|
545
|
-
this.priority = priority;
|
546
|
-
this.queue = queue;
|
547
|
-
this.task = task;
|
548
|
-
if (queue == null) {
|
549
|
-
this.state = STATE_SUSPENDED;
|
550
|
-
} else {
|
551
|
-
this.state = STATE_SUSPENDED_RUNNABLE;
|
552
|
-
}
|
553
|
-
}
|
554
|
-
|
555
|
-
/**
|
556
|
-
* The task is running and is currently scheduled.
|
557
|
-
*/
|
558
|
-
var STATE_RUNNING = 0;
|
559
|
-
|
560
|
-
/**
|
561
|
-
* The task has packets left to process.
|
562
|
-
*/
|
563
|
-
var STATE_RUNNABLE = 1;
|
564
|
-
|
565
|
-
/**
|
566
|
-
* The task is not currently running. The task is not blocked as such and may
|
567
|
-
* be started by the scheduler.
|
568
|
-
*/
|
569
|
-
var STATE_SUSPENDED = 2;
|
570
|
-
|
571
|
-
/**
|
572
|
-
* The task is blocked and cannot be run until it is explicitly released.
|
573
|
-
*/
|
574
|
-
var STATE_HELD = 4;
|
575
|
-
|
576
|
-
var STATE_SUSPENDED_RUNNABLE = STATE_SUSPENDED | STATE_RUNNABLE;
|
577
|
-
var STATE_NOT_HELD = ~STATE_HELD;
|
578
|
-
|
579
|
-
TaskControlBlock.prototype.setRunning = function () {
|
580
|
-
this.state = STATE_RUNNING;
|
581
|
-
};
|
582
|
-
|
583
|
-
TaskControlBlock.prototype.markAsNotHeld = function () {
|
584
|
-
this.state = this.state & STATE_NOT_HELD;
|
585
|
-
};
|
586
|
-
|
587
|
-
TaskControlBlock.prototype.markAsHeld = function () {
|
588
|
-
this.state = this.state | STATE_HELD;
|
589
|
-
};
|
590
|
-
|
591
|
-
TaskControlBlock.prototype.isHeldOrSuspended = function () {
|
592
|
-
return (this.state & STATE_HELD) != 0 || this.state == STATE_SUSPENDED;
|
593
|
-
};
|
594
|
-
|
595
|
-
TaskControlBlock.prototype.markAsSuspended = function () {
|
596
|
-
this.state = this.state | STATE_SUSPENDED;
|
597
|
-
};
|
598
|
-
|
599
|
-
TaskControlBlock.prototype.markAsRunnable = function () {
|
600
|
-
this.state = this.state | STATE_RUNNABLE;
|
601
|
-
};
|
602
|
-
|
603
|
-
/**
|
604
|
-
* Runs this task, if it is ready to be run, and returns the next task to run.
|
605
|
-
*/
|
606
|
-
TaskControlBlock.prototype.run = function () {
|
607
|
-
var packet;
|
608
|
-
if (this.state == STATE_SUSPENDED_RUNNABLE) {
|
609
|
-
packet = this.queue;
|
610
|
-
this.queue = packet.link;
|
611
|
-
if (this.queue == null) {
|
612
|
-
this.state = STATE_RUNNING;
|
613
|
-
} else {
|
614
|
-
this.state = STATE_RUNNABLE;
|
615
|
-
}
|
616
|
-
} else {
|
617
|
-
packet = null;
|
618
|
-
}
|
619
|
-
return this.task.run(packet);
|
620
|
-
};
|
621
|
-
|
622
|
-
/**
|
623
|
-
* Adds a packet to the worklist of this block's task, marks this as runnable if
|
624
|
-
* necessary, and returns the next runnable object to run (the one
|
625
|
-
* with the highest priority).
|
626
|
-
*/
|
627
|
-
TaskControlBlock.prototype.checkPriorityAdd = function (task, packet) {
|
628
|
-
if (this.queue == null) {
|
629
|
-
this.queue = packet;
|
630
|
-
this.markAsRunnable();
|
631
|
-
if (this.priority > task.priority) return this;
|
632
|
-
} else {
|
633
|
-
this.queue = packet.addTo(this.queue);
|
634
|
-
}
|
635
|
-
return task;
|
636
|
-
};
|
637
|
-
|
638
|
-
TaskControlBlock.prototype.toString = function () {
|
639
|
-
return "tcb { " + this.task + "@" + this.state + " }";
|
640
|
-
};
|
641
|
-
|
642
|
-
/**
|
643
|
-
* An idle task doesn't do any work itself but cycles control between the two
|
644
|
-
* device tasks.
|
645
|
-
* @param {Scheduler} scheduler the scheduler that manages this task
|
646
|
-
* @param {int} v1 a seed value that controls how the device tasks are scheduled
|
647
|
-
* @param {int} count the number of times this task should be scheduled
|
648
|
-
* @constructor
|
649
|
-
*/
|
650
|
-
function IdleTask(scheduler, v1, count) {
|
651
|
-
this.scheduler = scheduler;
|
652
|
-
this.v1 = v1;
|
653
|
-
this.count = count;
|
654
|
-
}
|
655
|
-
|
656
|
-
IdleTask.prototype.run = function (packet) {
|
657
|
-
this.count--;
|
658
|
-
if (this.count == 0) return this.scheduler.holdCurrent();
|
659
|
-
if ((this.v1 & 1) == 0) {
|
660
|
-
this.v1 = this.v1 >> 1;
|
661
|
-
return this.scheduler.release(ID_DEVICE_A);
|
662
|
-
} else {
|
663
|
-
this.v1 = (this.v1 >> 1) ^ 0xd008;
|
664
|
-
return this.scheduler.release(ID_DEVICE_B);
|
665
|
-
}
|
666
|
-
};
|
667
|
-
|
668
|
-
IdleTask.prototype.toString = function () {
|
669
|
-
return "IdleTask";
|
670
|
-
};
|
671
|
-
|
672
|
-
/**
|
673
|
-
* A task that suspends itself after each time it has been run to simulate
|
674
|
-
* waiting for data from an external device.
|
675
|
-
* @param {Scheduler} scheduler the scheduler that manages this task
|
676
|
-
* @constructor
|
677
|
-
*/
|
678
|
-
function DeviceTask(scheduler) {
|
679
|
-
this.scheduler = scheduler;
|
680
|
-
this.v1 = null;
|
681
|
-
}
|
682
|
-
|
683
|
-
DeviceTask.prototype.run = function (packet) {
|
684
|
-
if (packet == null) {
|
685
|
-
if (this.v1 == null) return this.scheduler.suspendCurrent();
|
686
|
-
var v = this.v1;
|
687
|
-
this.v1 = null;
|
688
|
-
return this.scheduler.queue(v);
|
689
|
-
} else {
|
690
|
-
this.v1 = packet;
|
691
|
-
return this.scheduler.holdCurrent();
|
692
|
-
}
|
693
|
-
};
|
694
|
-
|
695
|
-
DeviceTask.prototype.toString = function () {
|
696
|
-
return "DeviceTask";
|
697
|
-
};
|
698
|
-
|
699
|
-
/**
|
700
|
-
* A task that manipulates work packets.
|
701
|
-
* @param {Scheduler} scheduler the scheduler that manages this task
|
702
|
-
* @param {int} v1 a seed used to specify how work packets are manipulated
|
703
|
-
* @param {int} v2 another seed used to specify how work packets are manipulated
|
704
|
-
* @constructor
|
705
|
-
*/
|
706
|
-
function WorkerTask(scheduler, v1, v2) {
|
707
|
-
this.scheduler = scheduler;
|
708
|
-
this.v1 = v1;
|
709
|
-
this.v2 = v2;
|
710
|
-
}
|
711
|
-
|
712
|
-
WorkerTask.prototype.run = function (packet) {
|
713
|
-
// console.log('WorkerTask.run', packet == null);
|
714
|
-
if (packet == null) {
|
715
|
-
return this.scheduler.suspendCurrent();
|
716
|
-
} else {
|
717
|
-
if (this.v1 == ID_HANDLER_A) {
|
718
|
-
this.v1 = ID_HANDLER_B;
|
719
|
-
} else {
|
720
|
-
this.v1 = ID_HANDLER_A;
|
721
|
-
}
|
722
|
-
packet.id = this.v1;
|
723
|
-
packet.a1 = 0;
|
724
|
-
for (var i = 0; i < DATA_SIZE; i++) {
|
725
|
-
this.v2++;
|
726
|
-
if (this.v2 > 26) this.v2 = 1;
|
727
|
-
packet.a2[i] = this.v2;
|
728
|
-
}
|
729
|
-
return this.scheduler.queue(packet);
|
730
|
-
}
|
731
|
-
};
|
732
|
-
|
733
|
-
WorkerTask.prototype.toString = function () {
|
734
|
-
return "WorkerTask";
|
735
|
-
};
|
736
|
-
|
737
|
-
/**
|
738
|
-
* A task that manipulates work packets and then suspends itself.
|
739
|
-
* @param {Scheduler} scheduler the scheduler that manages this task
|
740
|
-
* @constructor
|
741
|
-
*/
|
742
|
-
function HandlerTask(scheduler) {
|
743
|
-
this.scheduler = scheduler;
|
744
|
-
this.v1 = null;
|
745
|
-
this.v2 = null;
|
746
|
-
}
|
747
|
-
|
748
|
-
HandlerTask.prototype.run = function (packet) {
|
749
|
-
if (packet != null) {
|
750
|
-
if (packet.kind == KIND_WORK) {
|
751
|
-
this.v1 = packet.addTo(this.v1);
|
752
|
-
} else {
|
753
|
-
this.v2 = packet.addTo(this.v2);
|
754
|
-
}
|
755
|
-
}
|
756
|
-
if (this.v1 != null) {
|
757
|
-
var count = this.v1.a1;
|
758
|
-
var v;
|
759
|
-
if (count < DATA_SIZE) {
|
760
|
-
if (this.v2 != null) {
|
761
|
-
v = this.v2;
|
762
|
-
this.v2 = this.v2.link;
|
763
|
-
v.a1 = this.v1.a2[count];
|
764
|
-
this.v1.a1 = count + 1;
|
765
|
-
return this.scheduler.queue(v);
|
766
|
-
}
|
767
|
-
} else {
|
768
|
-
v = this.v1;
|
769
|
-
this.v1 = this.v1.link;
|
770
|
-
return this.scheduler.queue(v);
|
771
|
-
}
|
772
|
-
}
|
773
|
-
return this.scheduler.suspendCurrent();
|
774
|
-
};
|
775
|
-
|
776
|
-
HandlerTask.prototype.toString = function () {
|
777
|
-
return "HandlerTask";
|
778
|
-
};
|
779
|
-
|
780
|
-
/* --- *
|
781
|
-
* P a c k e t
|
782
|
-
* --- */
|
783
|
-
|
784
|
-
var DATA_SIZE = 4;
|
785
|
-
|
786
|
-
/**
|
787
|
-
* A simple package of data that is manipulated by the tasks. The exact layout
|
788
|
-
* of the payload data carried by a packet is not importaint, and neither is the
|
789
|
-
* nature of the work performed on packets by the tasks.
|
790
|
-
*
|
791
|
-
* Besides carrying data, packets form linked lists and are hence used both as
|
792
|
-
* data and worklists.
|
793
|
-
* @param {Packet} link the tail of the linked list of packets
|
794
|
-
* @param {int} id an ID for this packet
|
795
|
-
* @param {int} kind the type of this packet
|
796
|
-
* @constructor
|
797
|
-
*/
|
798
|
-
function Packet(link, id, kind) {
|
799
|
-
this.link = link;
|
800
|
-
this.id = id;
|
801
|
-
this.kind = kind;
|
802
|
-
this.a1 = 0;
|
803
|
-
this.a2 = new Array(DATA_SIZE);
|
804
|
-
}
|
805
|
-
|
806
|
-
/**
|
807
|
-
* Add this packet to the end of a worklist, and return the worklist.
|
808
|
-
* @param {Packet} queue the worklist to add this packet to
|
809
|
-
*/
|
810
|
-
Packet.prototype.addTo = function (queue) {
|
811
|
-
this.link = null;
|
812
|
-
if (queue == null) return this;
|
813
|
-
var peek,
|
814
|
-
next = queue;
|
815
|
-
while ((peek = next.link) != null) next = peek;
|
816
|
-
next.link = this;
|
817
|
-
return queue;
|
818
|
-
};
|
819
|
-
|
820
|
-
Packet.prototype.toString = function () {
|
821
|
-
return "Packet";
|
822
|
-
};
|
823
|
-
|
824
|
-
/**
|
825
|
-
* The Richards benchmark simulates the task dispatcher of an
|
826
|
-
* operating system.
|
827
|
-
**/
|
828
|
-
function runRichards() {
|
829
|
-
var scheduler = new Scheduler();
|
830
|
-
scheduler.addIdleTask(ID_IDLE, 0, null, COUNT);
|
831
|
-
|
832
|
-
var queue = new Packet(null, ID_WORKER, KIND_WORK);
|
833
|
-
queue = new Packet(queue, ID_WORKER, KIND_WORK);
|
834
|
-
scheduler.addWorkerTask(ID_WORKER, 1000, queue);
|
835
|
-
|
836
|
-
queue = new Packet(null, ID_DEVICE_A, KIND_DEVICE);
|
837
|
-
queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE);
|
838
|
-
queue = new Packet(queue, ID_DEVICE_A, KIND_DEVICE);
|
839
|
-
scheduler.addHandlerTask(ID_HANDLER_A, 2000, queue);
|
840
|
-
|
841
|
-
queue = new Packet(null, ID_DEVICE_B, KIND_DEVICE);
|
842
|
-
queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE);
|
843
|
-
queue = new Packet(queue, ID_DEVICE_B, KIND_DEVICE);
|
844
|
-
scheduler.addHandlerTask(ID_HANDLER_B, 3000, queue);
|
845
|
-
|
846
|
-
scheduler.addDeviceTask(ID_DEVICE_A, 4000, null);
|
847
|
-
|
848
|
-
scheduler.addDeviceTask(ID_DEVICE_B, 5000, null);
|
849
|
-
|
850
|
-
scheduler.schedule();
|
851
|
-
|
852
|
-
if (scheduler.queueCount != EXPECTED_QUEUE_COUNT || scheduler.holdCount != EXPECTED_HOLD_COUNT) {
|
853
|
-
var msg =
|
854
|
-
"Error during execution: queueCount = " + scheduler.queueCount + ", holdCount = " + scheduler.holdCount + ".";
|
855
|
-
throw new Error(msg);
|
856
|
-
}
|
857
|
-
}
|
858
|
-
// Copyright 2014 the V8 project authors. All rights reserved.
|
859
|
-
// Redistribution and use in source and binary forms, with or without
|
860
|
-
// modification, are permitted provided that the following conditions are
|
861
|
-
// met:
|
862
|
-
//
|
863
|
-
// * Redistributions of source code must retain the above copyright
|
864
|
-
// notice, this list of conditions and the following disclaimer.
|
865
|
-
// * Redistributions in binary form must reproduce the above
|
866
|
-
// copyright notice, this list of conditions and the following
|
867
|
-
// disclaimer in the documentation and/or other materials provided
|
868
|
-
// with the distribution.
|
869
|
-
// * Neither the name of Google Inc. nor the names of its
|
870
|
-
// contributors may be used to endorse or promote products derived
|
871
|
-
// from this software without specific prior written permission.
|
872
|
-
//
|
873
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
874
|
-
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
875
|
-
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
876
|
-
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
877
|
-
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
878
|
-
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
879
|
-
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
880
|
-
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
881
|
-
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
882
|
-
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
883
|
-
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
884
|
-
|
885
|
-
var base_dir = "";
|
886
|
-
var success = true;
|
887
|
-
|
888
|
-
function PrintResult(name, result) {
|
889
|
-
console.log(name + ": " + result);
|
890
|
-
}
|
891
|
-
|
892
|
-
function PrintError(name, error) {
|
893
|
-
PrintResult(name, error);
|
894
|
-
success = false;
|
895
|
-
}
|
896
|
-
|
897
|
-
function PrintScore(score) {
|
898
|
-
return;
|
899
|
-
if (success) {
|
900
|
-
console.log("----");
|
901
|
-
console.log("Score (version " + BenchmarkSuite.version + "): " + score);
|
902
|
-
}
|
903
|
-
}
|
904
|
-
|
905
|
-
BenchmarkSuite.config.doWarmup = undefined;
|
906
|
-
BenchmarkSuite.config.doDeterministic = undefined;
|
907
|
-
|
908
|
-
function main() {
|
909
|
-
BenchmarkSuite.RunSuites({ NotifyResult: PrintResult, NotifyError: PrintError, NotifyScore: PrintScore });
|
910
|
-
// runRichards();
|
911
|
-
}
|
912
|
-
|
913
|
-
main();
|