forked from calculatortamer/pluotsorbet-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.ts
214 lines (183 loc) · 6.52 KB
/
scheduler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
module J2ME {
/** @const */ export var MAX_PRIORITY: number = 10;
/** @const */ export var MIN_PRIORITY: number = 1;
/** @const */ export var NORMAL_PRIORITY: number = 5;
/** @const */ export var ISOLATE_MIN_PRIORITY: number = 1;
/** @const */ export var ISOLATE_NORM_PRIORITY: number = 2;
/** @const */ export var ISOLATE_MAX_PRIORITY: number = 3;
/**
* Maximum time in ms that all of the threads have to run before the event
* loop is run.
* NOTE: this number is somewhat arbitrarily chosen, but the thought is
* we'd like the system to respond in under 100ms, so by using 80ms we then
* have 20ms to get an event, start processing it, and render an update.
* @const
*/
var MAX_WINDOW_EXECUTION_TIME: number = 80;
/**
* Number of ms between preemption checks, chosen arbitrarily.
* @const
*/
var PREEMPTION_INTERVAL: number = 5;
/**
* Time when the last preemption check was allowed.
*/
var lastPreemptionCheck: number = 0;
/**
* Number of preemption checks thus far.
*/
export var preemptionCount: number = 0;
/**
* Time when the window began execution.
* @type {number}
*/
var windowStartTime: number = 0;
/**
* Time used to track thread execution time. This is updated when the thread starts
* in the execution window and when `updateCurrentRuntime` is called.
*/
var threadTrackingTime: number = 0;
/**
* Used to block preemptions from happening during code that can't handle them.
*/
export var preemptionLockLevel: number = 0;
/**
* All of the currently runnable threads. Sorted in ascending order by virtualRuntime.
* @type {Array}
*/
var runningQueue: Context [] = [];
/**
* The smallest virtual runtime of all the currently executing threads. This number is
* monotonically increasing.
*/
var minVirtualRuntime: number = 0;
/**
* True when a setTimeout has been scheduled to run the threads.
*/
var processQueueScheduled: boolean = false;
/**
* The currently executing context.
*/
var current: Context = null;
/**
* Rate the virtual runtime increases.
*/
var currentTimeScale: number = 1;
/**
* The scheduler tracks the amount of time(virtualRuntime) that each thread has had to execute
* and tries to always execute the thread that has had least amount of time to run next.
* For higher priority threads the virtual runtime is increased at a slower rate to give them
* more time to be the the front of the queue and vice versa for low priority threads. To allow
* the event loop a turn there is an overall MAX_WINDOW_EXECUTION_TIME that if reached will yield
* all the threads and schedule them to resume on a setTimeout. This allows us to run up to
* MAX_WINDOW_EXECUTION_TIME/PREEMPTION_INTERVAL threads per execution window.
*/
export class Scheduler {
static enqueue(ctx: Context, directExecution?: boolean) {
if (ctx.virtualRuntime === 0) {
// Ensure the new thread doesn't dominate.
ctx.virtualRuntime = minVirtualRuntime;
}
runningQueue.unshift(ctx);
runningQueue.sort(function(a: Context, b: Context) {
return a.virtualRuntime - b.virtualRuntime;
});
Scheduler.updateMinVirtualRuntime();
Scheduler.processRunningQueue(directExecution);
}
private static processRunningQueue(directExecution?: boolean) {
function run() {
processQueueScheduled = false;
try {
windowStartTime = performance.now();
while (runningQueue.length) {
var now = performance.now();
if (now - windowStartTime >= MAX_WINDOW_EXECUTION_TIME) {
break;
}
var ctx = runningQueue.shift();
threadTrackingTime = lastPreemptionCheck = now;
current = ctx;
/*
* The current scaling is a simple linear function where the scale goes from 1x to .1x for lowest
* priority to highest priority.
* NOTE: this should be tuned.
* RUNTIME THREAD SCALE
* low low 1x
* norm norm 0.72x
* high high .1x
*/
currentTimeScale = -0.03103448276 * (ctx.priority * ctx.runtime.priority) + 1.031034483;
ctx.execute();
Scheduler.updateCurrentRuntime();
current = null;
}
} finally {
if (runningQueue.length) {
Scheduler.processRunningQueue();
}
}
}
if (directExecution) {
run();
return;
}
if (processQueueScheduled) {
return;
}
processQueueScheduled = true;
(<any>window).nextTickDuringEvents(run);
}
private static updateMinVirtualRuntime() {
var virtualRuntime = minVirtualRuntime;
if (current) {
virtualRuntime = current.virtualRuntime;
}
if (runningQueue.length) {
var nextContext = runningQueue[0];
if (!current) {
virtualRuntime = nextContext.virtualRuntime;
} else {
virtualRuntime = Math.min(virtualRuntime, nextContext.virtualRuntime);
}
}
minVirtualRuntime = Math.max(minVirtualRuntime, virtualRuntime);
}
private static updateCurrentRuntime() {
var now = performance.now();
var ctx = current;
var executionTime = now - threadTrackingTime;
var weightedExecutionTime = executionTime * currentTimeScale;
ctx.virtualRuntime += weightedExecutionTime;
threadTrackingTime = now;
Scheduler.updateMinVirtualRuntime()
}
static shouldPreempt(): boolean {
if (preemptionLockLevel > 0) {
return false;
}
var now = performance.now();
var totalElapsed = now - windowStartTime;
if (totalElapsed > MAX_WINDOW_EXECUTION_TIME) {
preemptionCount++;
threadWriter && threadWriter.writeLn("Execution window timeout: " + totalElapsed.toFixed(2) + " ms, samples: " + PS + ", count: " + preemptionCount);
return true;
}
Scheduler.updateCurrentRuntime();
var elapsed = now - lastPreemptionCheck;
if (elapsed < PREEMPTION_INTERVAL) {
return false;
}
lastPreemptionCheck = now;
if (runningQueue.length === 0) {
return false;
}
if ($.ctx.virtualRuntime > runningQueue[0].virtualRuntime) {
preemptionCount++;
threadWriter && threadWriter.writeLn("Preemption: " + elapsed.toFixed(2) + " ms, samples: " + PS + ", count: " + preemptionCount);
return true;
}
return false;
}
}
}