Bug 1835710 - Cancel off-thread JIT compilation before changing nursery allocation...
[gecko.git] / dom / base / ProcessSelector.sys.mjs
blob59319f7bf691a50beefef25eaa8c4f5bee01e9dd
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Fills up aProcesses until max and then selects randomly from the available
6 // ones.
7 export function RandomSelector() {}
9 RandomSelector.prototype = {
10   classID: Components.ID("{c616fcfd-9737-41f1-aa74-cee72a38f91b}"),
11   QueryInterface: ChromeUtils.generateQI(["nsIContentProcessProvider"]),
13   provideProcess(aType, aProcesses, aMaxCount) {
14     if (aProcesses.length < aMaxCount) {
15       return Ci.nsIContentProcessProvider.NEW_PROCESS;
16     }
18     return Math.floor(Math.random() * aMaxCount);
19   },
22 // Fills up aProcesses until max and then selects one from the available
23 // ones that host the least number of tabs.
24 export function MinTabSelector() {}
26 MinTabSelector.prototype = {
27   classID: Components.ID("{2dc08eaf-6eef-4394-b1df-a3a927c1290b}"),
28   QueryInterface: ChromeUtils.generateQI(["nsIContentProcessProvider"]),
30   provideProcess(aType, aProcesses, aMaxCount) {
31     let min = Number.MAX_VALUE;
32     let candidate = Ci.nsIContentProcessProvider.NEW_PROCESS;
34     // The reason for not directly using aProcesses.length here is because if
35     // we keep processes alive for testing but want a test to use only single
36     // content process we can just keep relying on dom.ipc.processCount = 1
37     // this way.
38     let numIters = Math.min(aProcesses.length, aMaxCount);
40     for (let i = 0; i < numIters; i++) {
41       let process = aProcesses[i];
42       let tabCount = process.tabCount;
43       if (tabCount < min) {
44         min = tabCount;
45         candidate = i;
46       }
47     }
49     // If all current processes have at least one tab and we have not yet
50     // reached the maximum, spawn a new process.
51     if (min > 0 && aProcesses.length < aMaxCount) {
52       return Ci.nsIContentProcessProvider.NEW_PROCESS;
53     }
55     // Otherwise we use candidate.
56     return candidate;
57   },