camoucore 0.3.0 → 0.6.0
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/LICENSE +4 -3
- package/dist/camoufox/__main__.d.ts +10 -0
- package/dist/camoufox/__main__.js +1175 -0
- package/dist/camoufox/__version__.d.ts +31 -0
- package/dist/camoufox/__version__.js +32 -0
- package/dist/camoufox/_humanize_playwright.d.ts +54 -0
- package/dist/camoufox/_humanize_playwright.js +429 -0
- package/dist/camoufox/addons.d.ts +21 -0
- package/dist/camoufox/addons.js +56 -0
- package/dist/camoufox/async_api.d.ts +9 -0
- package/dist/camoufox/async_api.js +9 -0
- package/dist/camoufox/browser-pin.d.ts +46 -0
- package/dist/camoufox/browser-pin.js +96 -0
- package/dist/camoufox/cli.d.ts +7 -0
- package/dist/camoufox/cli.js +20 -0
- package/dist/camoufox/coherence.d.ts +141 -0
- package/dist/camoufox/coherence.js +708 -0
- package/dist/camoufox/data-files/audio-output-latency.json +15 -0
- package/dist/camoufox/data-files/browser-pin.json +7 -0
- package/dist/camoufox/data-files/display-profiles.json +6 -0
- package/dist/camoufox/data-files/essential-fonts.json +5 -0
- package/dist/camoufox/data-files/fingerprint-presets-v150.json +14544 -0
- package/dist/camoufox/data-files/fingerprint-presets.json +5192 -0
- package/dist/camoufox/data-files/font-bases.json +1888 -0
- package/dist/camoufox/data-files/font-groups.json +716 -0
- package/dist/camoufox/data-files/font-prefs.json +1046 -0
- package/dist/camoufox/data-files/fonts.json +240 -0
- package/dist/camoufox/data-files/fpgen.yml +44 -0
- package/dist/camoufox/data-files/launcher-constants.json +136 -0
- package/dist/camoufox/data-files/media-devices.json +180 -0
- package/dist/camoufox/data-files/repos.yml +40 -0
- package/dist/camoufox/data-files/voice-manifests.json +1864 -0
- package/dist/camoufox/data-files/voice-uris.json +188 -0
- package/dist/camoufox/data-files/warnings.yml +146 -0
- package/dist/camoufox/display_profile.d.ts +21 -0
- package/dist/camoufox/display_profile.js +129 -0
- package/dist/camoufox/driver_defaults.d.ts +27 -0
- package/dist/camoufox/driver_defaults.js +119 -0
- package/dist/camoufox/exceptions.d.ts +68 -0
- package/dist/camoufox/exceptions.js +122 -0
- package/dist/camoufox/fingerprints.d.ts +434 -0
- package/dist/camoufox/fingerprints.js +1936 -0
- package/dist/camoufox/gpudisplay.d.ts +158 -0
- package/dist/camoufox/gpudisplay.js +695 -0
- package/dist/camoufox/host_rendering.d.ts +38 -0
- package/dist/camoufox/host_rendering.js +180 -0
- package/dist/camoufox/humanize.d.ts +89 -0
- package/dist/camoufox/humanize.js +143 -0
- package/dist/camoufox/index.d.ts +21 -0
- package/dist/camoufox/index.js +21 -0
- package/dist/camoufox/ip.d.ts +45 -0
- package/dist/camoufox/ip.js +114 -0
- package/dist/camoufox/multiversion.d.ts +131 -0
- package/dist/camoufox/multiversion.js +490 -0
- package/dist/camoufox/node-floor.d.ts +5 -0
- package/dist/camoufox/node-floor.js +17 -0
- package/dist/camoufox/paths.d.ts +45 -0
- package/dist/camoufox/paths.js +62 -0
- package/dist/camoufox/pkgman.d.ts +361 -0
- package/dist/camoufox/pkgman.js +1066 -0
- package/dist/camoufox/product.d.ts +8 -0
- package/dist/camoufox/product.js +8 -0
- package/dist/camoufox/pycompat.d.ts +97 -0
- package/dist/camoufox/pycompat.js +458 -0
- package/dist/camoufox/server.d.ts +36 -0
- package/dist/camoufox/server.js +82 -0
- package/dist/camoufox/sync_api.d.ts +77 -0
- package/dist/camoufox/sync_api.js +207 -0
- package/dist/camoufox/utils.d.ts +638 -0
- package/dist/camoufox/utils.js +2507 -0
- package/dist/camoufox/warnings.d.ts +16 -0
- package/dist/camoufox/warnings.js +65 -0
- package/dist/camoufox/webgl.d.ts +52 -0
- package/dist/camoufox/webgl.js +264 -0
- package/dist/camouwolf/__main__.d.ts +10 -0
- package/dist/camouwolf/__main__.js +1180 -0
- package/dist/camouwolf/__version__.d.ts +31 -0
- package/dist/camouwolf/__version__.js +32 -0
- package/dist/camouwolf/_humanize_playwright.d.ts +53 -0
- package/dist/camouwolf/_humanize_playwright.js +427 -0
- package/dist/camouwolf/addons.d.ts +42 -0
- package/dist/camouwolf/addons.js +113 -0
- package/dist/camouwolf/async_api.d.ts +9 -0
- package/dist/camouwolf/async_api.js +9 -0
- package/dist/camouwolf/browser-pin.d.ts +46 -0
- package/dist/camouwolf/browser-pin.js +96 -0
- package/dist/camouwolf/cli.d.ts +7 -0
- package/dist/camouwolf/cli.js +20 -0
- package/dist/camouwolf/clienthints.d.ts +61 -0
- package/dist/camouwolf/clienthints.js +176 -0
- package/dist/camouwolf/coherence.d.ts +152 -0
- package/dist/camouwolf/coherence.js +554 -0
- package/dist/camouwolf/data-files/browser-pin.json +7 -0
- package/dist/camouwolf/data-files/camouwolf.defaults.json +118 -0
- package/dist/camouwolf/data-files/data/bundle-fonts.json +1983 -0
- package/dist/camouwolf/data-files/data/chrome-languages.json +517 -0
- package/dist/camouwolf/data-files/data/properties.json +321 -0
- package/dist/camouwolf/data-files/data/system-colors.json +35 -0
- package/dist/camouwolf/data-files/data/ubo.json +6 -0
- package/dist/camouwolf/data-files/fpgen.yml +41 -0
- package/dist/camouwolf/data-files/launcher-constants.json +26 -0
- package/dist/camouwolf/data-files/repos.yml +23 -0
- package/dist/camouwolf/data-files/warnings.yml +96 -0
- package/dist/camouwolf/driver_defaults.d.ts +25 -0
- package/dist/camouwolf/driver_defaults.js +114 -0
- package/dist/camouwolf/exceptions.d.ts +95 -0
- package/dist/camouwolf/exceptions.js +150 -0
- package/dist/camouwolf/fingerprints.d.ts +227 -0
- package/dist/camouwolf/fingerprints.js +753 -0
- package/dist/camouwolf/gpudisplay.d.ts +171 -0
- package/dist/camouwolf/gpudisplay.js +711 -0
- package/dist/camouwolf/host_rendering.d.ts +18 -0
- package/dist/camouwolf/host_rendering.js +114 -0
- package/dist/camouwolf/humanize.d.ts +84 -0
- package/dist/camouwolf/humanize.js +128 -0
- package/dist/camouwolf/index.d.ts +22 -0
- package/dist/camouwolf/index.js +21 -0
- package/dist/camouwolf/multiversion.d.ts +131 -0
- package/dist/camouwolf/multiversion.js +491 -0
- package/dist/camouwolf/node-floor.d.ts +5 -0
- package/dist/camouwolf/node-floor.js +17 -0
- package/dist/camouwolf/paths.d.ts +34 -0
- package/dist/camouwolf/paths.js +58 -0
- package/dist/camouwolf/pkgman.d.ts +331 -0
- package/dist/camouwolf/pkgman.js +972 -0
- package/dist/camouwolf/product.d.ts +3 -0
- package/dist/camouwolf/product.js +3 -0
- package/dist/camouwolf/pycompat.d.ts +92 -0
- package/dist/camouwolf/pycompat.js +461 -0
- package/dist/camouwolf/server.d.ts +36 -0
- package/dist/camouwolf/server.js +81 -0
- package/dist/camouwolf/switches.d.ts +77 -0
- package/dist/camouwolf/switches.js +222 -0
- package/dist/camouwolf/sync_api.d.ts +84 -0
- package/dist/camouwolf/sync_api.js +173 -0
- package/dist/camouwolf/utils.d.ts +644 -0
- package/dist/camouwolf/utils.js +2057 -0
- package/dist/camouwolf/warnings.d.ts +14 -0
- package/dist/camouwolf/warnings.js +59 -0
- package/dist/camouwolf/webgl.d.ts +41 -0
- package/dist/camouwolf/webgl.js +198 -0
- package/dist/data.d.ts +1 -1
- package/dist/data.js +3 -3
- package/dist/download.js +1 -1
- package/dist/exceptions.d.ts +1 -1
- package/dist/exceptions.js +1 -1
- package/dist/geolocation.js +1 -1
- package/dist/host.js +1 -1
- package/dist/ip.js +1 -1
- package/dist/locales.js +1 -1
- package/dist/mcp/cli.d.ts +15 -0
- package/dist/mcp/cli.js +108 -0
- package/dist/mcp/index.d.ts +8 -0
- package/dist/mcp/index.js +8 -0
- package/dist/mcp/main.d.ts +2 -0
- package/dist/mcp/main.js +8 -0
- package/dist/mcp/secrets.d.ts +50 -0
- package/dist/mcp/secrets.js +193 -0
- package/dist/mcp/server.d.ts +48 -0
- package/dist/mcp/server.js +653 -0
- package/dist/mcp/tree.d.ts +10 -0
- package/dist/mcp/tree.js +36 -0
- package/dist/passthrough.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/settings.js +1 -1
- package/dist/telemetry.d.ts +0 -1
- package/dist/telemetry.js +5 -4
- package/dist/virtdisplay.d.ts +1 -1
- package/dist/virtdisplay.js +1 -1
- package/dist/warnings.js +1 -1
- package/package.json +57 -13
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* headless="virtual-gpu": a headless Xorg server pinned to one GPU.
|
|
3
|
+
*
|
|
4
|
+
* TypeScript twin of python/camoucore/src/camoucore/camouwolf/gpudisplay.py.
|
|
5
|
+
*
|
|
6
|
+
* Xvfb has no GPU path, so a browser on it renders WebGL and canvas with
|
|
7
|
+
* Mesa's software rasterizer. This display is a real Xorg server bound to one
|
|
8
|
+
* card by its PCI address, drawing a virtual screen with no monitor attached,
|
|
9
|
+
* so the browser renders on that card. The card is always named, never
|
|
10
|
+
* guessed: the identity claims the GPU that renders, and two cards render
|
|
11
|
+
* differently.
|
|
12
|
+
*/
|
|
13
|
+
import { execFileSync } from "node:child_process";
|
|
14
|
+
import * as fs from "node:fs";
|
|
15
|
+
import * as net from "node:net";
|
|
16
|
+
import * as os from "node:os";
|
|
17
|
+
import * as path from "node:path";
|
|
18
|
+
import { VirtualDisplay } from "../virtdisplay.js";
|
|
19
|
+
import { AmbiguousGpuSelection, CannotExecuteXorg, CannotFindXorg, GpuNotFound, GpuSoftwareFallback, GpuVtTakeover, NoGpuFound, } from "./exceptions.js";
|
|
20
|
+
import { PRODUCT } from "./product.js";
|
|
21
|
+
import { LeakWarning, loadWarnings } from "./warnings.js";
|
|
22
|
+
/** Names the card when the `gpu` launch option is not given. */
|
|
23
|
+
export const GPU_ENV_VAR = "CAMOUWOLF_GPU";
|
|
24
|
+
/**
|
|
25
|
+
* How long the browser has to render on the GPU after the probe page starts
|
|
26
|
+
* drawing. A card busy with other work has taken 5-10s.
|
|
27
|
+
*/
|
|
28
|
+
export const GPU_WAIT_S = 30;
|
|
29
|
+
export const GPU_WAIT_ENV_VAR = "CAMOUWOLF_GPU_WAIT_S";
|
|
30
|
+
/**
|
|
31
|
+
* The root window is not observable (screen.* is spoofed per context), but
|
|
32
|
+
* the GPU compositor needs a real surface, so it is not 1x1 like Xvfb's. Like
|
|
33
|
+
* the Xvfb one, launchOptions fits it to the identity before it starts.
|
|
34
|
+
*/
|
|
35
|
+
export const GPU_SCREEN = "1920x1080x24";
|
|
36
|
+
/** Where Xorg looks up a relative -config when the setuid wrapper elevates it. */
|
|
37
|
+
export const SYSTEM_CONFIG_DIR = "/etc/X11";
|
|
38
|
+
// Without root, Xorg.wrap must let any user start Xorg (a launch from ssh,
|
|
39
|
+
// systemd or a container is not a console login) and keep it root, which a
|
|
40
|
+
// server with no VT of its own needs to open the card. Stock Debian and Ubuntu
|
|
41
|
+
// ship allowed_users=console and needs_root_rights=auto.
|
|
42
|
+
export const XWRAPPER_CONFIG = "/etc/X11/Xwrapper.config";
|
|
43
|
+
export const XWRAPPER_SETTINGS = {
|
|
44
|
+
allowed_users: "anybody",
|
|
45
|
+
needs_root_rights: "yes",
|
|
46
|
+
};
|
|
47
|
+
export const XORG_WRAPPERS = [
|
|
48
|
+
"/usr/lib/xorg/Xorg.wrap",
|
|
49
|
+
"/usr/libexec/Xorg.wrap",
|
|
50
|
+
];
|
|
51
|
+
/**
|
|
52
|
+
* -terminate: the server exits this many seconds after its last client leaves.
|
|
53
|
+
* -sharevts: never take a VT of its own, which blanks a live desktop.
|
|
54
|
+
*/
|
|
55
|
+
export const SERVER_FLAGS = [
|
|
56
|
+
"-terminate",
|
|
57
|
+
"2",
|
|
58
|
+
"-nolisten",
|
|
59
|
+
"tcp",
|
|
60
|
+
"-sharevts",
|
|
61
|
+
"-novtswitch",
|
|
62
|
+
];
|
|
63
|
+
/**
|
|
64
|
+
* Switches that put Chromium on the real GPU behind this display; the caller's
|
|
65
|
+
* own switches win.
|
|
66
|
+
*
|
|
67
|
+
* Chromium blocklists aggressively, and on a headless Xorg with no display
|
|
68
|
+
* manager it often decides the driver is untrusted and falls back to
|
|
69
|
+
* SwiftShader. The launch still succeeds and WebGL still works, which is why
|
|
70
|
+
* require_gpu checks the DRM usage instead of trusting the launch.
|
|
71
|
+
*
|
|
72
|
+
* --use-gl=angle --use-angle=gl pins ANGLE to the desktop GL backend, which is
|
|
73
|
+
* what Chrome on Linux uses; left to choose, it can land on the Vulkan backend.
|
|
74
|
+
*
|
|
75
|
+
* 2D canvas stays on the CPU. Accelerated 2D canvas antialiases through
|
|
76
|
+
* whichever ANGLE backend the host has, which is visible in the pixels and
|
|
77
|
+
* cannot agree with a Windows identity (Direct3D 11); the CPU rasterizer is
|
|
78
|
+
* the same on every backend. WebGL, WebGPU and compositing keep the GPU.
|
|
79
|
+
*/
|
|
80
|
+
export const GPU_SWITCHES = [
|
|
81
|
+
"--ignore-gpu-blocklist",
|
|
82
|
+
"--enable-gpu-rasterization",
|
|
83
|
+
"--enable-zero-copy",
|
|
84
|
+
"--use-gl=angle",
|
|
85
|
+
"--use-angle=gl",
|
|
86
|
+
"--disable-accelerated-2d-canvas",
|
|
87
|
+
];
|
|
88
|
+
/**
|
|
89
|
+
* Draws one WebGL frame per animation frame: the browser opens the GPU only
|
|
90
|
+
* when something renders.
|
|
91
|
+
*/
|
|
92
|
+
export const PROBE_PAGE = "data:text/html,<canvas id=c width=32 height=32></canvas><script>" +
|
|
93
|
+
"const gl=c.getContext('webgl2')||c.getContext('webgl');" +
|
|
94
|
+
"function f(){if(gl){gl.clear(gl.COLOR_BUFFER_BIT);gl.flush();}requestAnimationFrame(f);}f();" +
|
|
95
|
+
"</script>";
|
|
96
|
+
const ACTIVE_VT = "/sys/class/tty/tty0/active";
|
|
97
|
+
/**
|
|
98
|
+
* A crash right after startup (a driver fault under VRAM pressure) and a VT
|
|
99
|
+
* switch both land just after the display is reported.
|
|
100
|
+
*/
|
|
101
|
+
const SETTLE_MS = 1000;
|
|
102
|
+
/** An X11 connection setup request: little-endian, protocol 11.0, no auth. */
|
|
103
|
+
const X11_SETUP = Buffer.from([0x6c, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
104
|
+
const VENDORS = {
|
|
105
|
+
"0x10de": "NVIDIA",
|
|
106
|
+
"0x1002": "AMD",
|
|
107
|
+
"0x8086": "Intel",
|
|
108
|
+
};
|
|
109
|
+
const PCI_ADDRESS = /^[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\.[0-9a-f]$/;
|
|
110
|
+
export function gpuVendor(gpu) {
|
|
111
|
+
return VENDORS[gpu.vendorId] ?? gpu.vendorId;
|
|
112
|
+
}
|
|
113
|
+
/** Xorg's BusID: decimal bus:device:function, without the domain. */
|
|
114
|
+
export function busId(gpu) {
|
|
115
|
+
const [, bus, devFn] = gpu.pciAddress.split(":");
|
|
116
|
+
const [dev, fn] = devFn.split(".");
|
|
117
|
+
return `PCI:${Number.parseInt(bus, 16)}:${Number.parseInt(dev, 16)}:${Number.parseInt(fn, 16)}`;
|
|
118
|
+
}
|
|
119
|
+
/** modesetting drives any KMS device with glamor; NVIDIA needs its own. */
|
|
120
|
+
export function xorgDriver(gpu) {
|
|
121
|
+
return gpu.kernelDriver === "nvidia" ? "nvidia" : "modesetting";
|
|
122
|
+
}
|
|
123
|
+
/** Whether a DRM file's usage stats (its fdinfo) show time on a GPU engine. */
|
|
124
|
+
/**
|
|
125
|
+
* Whether a DRM file's usage stats (its fdinfo) show work on a GPU engine.
|
|
126
|
+
*
|
|
127
|
+
* Drivers report busy time (drm-engine-<engine>: <n> ns; amdgpu, i915) or busy
|
|
128
|
+
* cycles (drm-cycles-<engine>: <n>; xe reports only these). drm-total-cycles
|
|
129
|
+
* counts the GPU's clock whether this client used it or not, so it is not work.
|
|
130
|
+
*/
|
|
131
|
+
export function drewOnGpu(fdinfo) {
|
|
132
|
+
return [
|
|
133
|
+
...fdinfo.matchAll(/^drm-(?:engine-[\w-]+:\s*(\d+) ns|cycles-[\w-]+:\s*(\d+))$/gm),
|
|
134
|
+
].some((match) => Number(match[1] ?? match[2]) > 0);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The card's render node and its primary node. The browser's GPU work is accounted
|
|
138
|
+
* to whichever it was handed: a second launch from one process on an RX 7900
|
|
139
|
+
* XTX drew through /dev/dri/card0, not renderD128.
|
|
140
|
+
*/
|
|
141
|
+
export function drmNodes(gpu) {
|
|
142
|
+
return [gpu.renderNode, gpu.cardNode].filter((node) => Boolean(node));
|
|
143
|
+
}
|
|
144
|
+
export function describeGpu(gpu) {
|
|
145
|
+
return `${gpu.pciAddress} ${gpuVendor(gpu)} [${gpu.vendorId}:${gpu.deviceId}] driver=${gpu.kernelDriver} ${gpu.renderNode}`;
|
|
146
|
+
}
|
|
147
|
+
function read(file) {
|
|
148
|
+
try {
|
|
149
|
+
return fs.readFileSync(file, "utf-8").trim();
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return "";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function realpath(file) {
|
|
156
|
+
try {
|
|
157
|
+
return fs.realpathSync(file);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function list(dir) {
|
|
164
|
+
try {
|
|
165
|
+
return fs.readdirSync(dir);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function nvidiaNode(root, pciAddress) {
|
|
172
|
+
const info = read(path.join(root, "proc/driver/nvidia/gpus", pciAddress, "information"));
|
|
173
|
+
const match = /^Device Minor:\s*(\d+)/m.exec(info);
|
|
174
|
+
return match ? `/dev/nvidia${match[1]}` : null;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The GPUs with a render node and a PCI address, in PCI address order.
|
|
178
|
+
*
|
|
179
|
+
* Ordered by PCI address, not render node number: render nodes are numbered in
|
|
180
|
+
* the order the drivers loaded, which can change from one boot to the next.
|
|
181
|
+
*/
|
|
182
|
+
export function enumerateGpus(root = "/") {
|
|
183
|
+
const drm = path.join(root, "sys/class/drm");
|
|
184
|
+
const cards = new Map();
|
|
185
|
+
for (const card of list(drm)) {
|
|
186
|
+
if (!/^card\d+$/.test(card))
|
|
187
|
+
continue;
|
|
188
|
+
const real = realpath(path.join(drm, card, "device"));
|
|
189
|
+
if (real)
|
|
190
|
+
cards.set(real, `/dev/dri/${card}`);
|
|
191
|
+
}
|
|
192
|
+
const gpus = [];
|
|
193
|
+
for (const node of list(path.join(root, "dev/dri"))) {
|
|
194
|
+
if (!node.startsWith("renderD"))
|
|
195
|
+
continue;
|
|
196
|
+
const device = path.join(drm, node, "device");
|
|
197
|
+
const real = realpath(device);
|
|
198
|
+
if (!real || !PCI_ADDRESS.test(path.basename(real)))
|
|
199
|
+
continue;
|
|
200
|
+
const pciAddress = path.basename(real);
|
|
201
|
+
const uevent = Object.fromEntries(read(path.join(device, "uevent"))
|
|
202
|
+
.split("\n")
|
|
203
|
+
.filter((line) => line.includes("="))
|
|
204
|
+
.map((line) => [
|
|
205
|
+
line.slice(0, line.indexOf("=")),
|
|
206
|
+
line.slice(line.indexOf("=") + 1),
|
|
207
|
+
]));
|
|
208
|
+
const kernelDriver = uevent.DRIVER ?? "";
|
|
209
|
+
gpus.push({
|
|
210
|
+
pciAddress,
|
|
211
|
+
renderNode: `/dev/dri/${node}`,
|
|
212
|
+
cardNode: cards.get(real) ?? null,
|
|
213
|
+
vendorId: read(path.join(device, "vendor")).toLowerCase(),
|
|
214
|
+
deviceId: read(path.join(device, "device")).toLowerCase(),
|
|
215
|
+
kernelDriver,
|
|
216
|
+
nvidiaNode: kernelDriver === "nvidia" ? nvidiaNode(root, pciAddress) : null,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
return gpus.sort((a, b) => a.pciAddress < b.pciAddress ? -1 : a.pciAddress > b.pciAddress ? 1 : 0);
|
|
220
|
+
}
|
|
221
|
+
function matches(index, gpu, selector) {
|
|
222
|
+
if (/^\d+$/.test(selector))
|
|
223
|
+
return Number(selector) === index;
|
|
224
|
+
const ids = `${gpu.vendorId}:${gpu.deviceId}`.replaceAll("0x", "");
|
|
225
|
+
const names = [gpu.renderNode, gpuVendor(gpu), ids];
|
|
226
|
+
if (gpu.cardNode)
|
|
227
|
+
names.push(gpu.cardNode);
|
|
228
|
+
const wanted = selector.replaceAll("0x", "");
|
|
229
|
+
return (names.some((name) => name.toLowerCase() === wanted ||
|
|
230
|
+
path.basename(name).toLowerCase() === wanted) || gpu.pciAddress.includes(selector));
|
|
231
|
+
}
|
|
232
|
+
function listing(gpus, shown) {
|
|
233
|
+
return gpus
|
|
234
|
+
.map((gpu, i) => !shown || shown.includes(gpu) ? ` [${i}] ${describeGpu(gpu)}` : null)
|
|
235
|
+
.filter((line) => line !== null)
|
|
236
|
+
.join("\n");
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The one GPU `selector` (or CAMOUWOLF_GPU) names.
|
|
240
|
+
*
|
|
241
|
+
* A selector is an index into enumerateGpus(), a render or card node (name or
|
|
242
|
+
* /dev/dri path), a PCI address or part of one, vendor:device ids, or a vendor
|
|
243
|
+
* name. Without one, the host must have exactly one GPU.
|
|
244
|
+
*/
|
|
245
|
+
export function selectGpu(selector, gpus = enumerateGpus()) {
|
|
246
|
+
if (!gpus.length) {
|
|
247
|
+
throw new NoGpuFound("virtual-gpu: no GPU render node with a PCI address. Is a GPU present, is its driver " +
|
|
248
|
+
"loaded, and are /dev/dri and /sys visible (--device /dev/dri in a container)?");
|
|
249
|
+
}
|
|
250
|
+
const wanted = String(selector ?? process.env[GPU_ENV_VAR] ?? "")
|
|
251
|
+
.trim()
|
|
252
|
+
.toLowerCase();
|
|
253
|
+
if (!wanted) {
|
|
254
|
+
if (gpus.length === 1)
|
|
255
|
+
return gpus[0];
|
|
256
|
+
throw new AmbiguousGpuSelection(`virtual-gpu: ${gpus.length} GPUs found. Name one with gpu= or ${GPU_ENV_VAR}, ` +
|
|
257
|
+
`preferably by PCI address:\n${listing(gpus)}`);
|
|
258
|
+
}
|
|
259
|
+
const matched = gpus.filter((gpu, i) => matches(i, gpu, wanted));
|
|
260
|
+
if (!matched.length) {
|
|
261
|
+
throw new GpuNotFound(`virtual-gpu: no GPU matches '${wanted}'. Available:\n${listing(gpus)}`);
|
|
262
|
+
}
|
|
263
|
+
if (matched.length > 1) {
|
|
264
|
+
throw new AmbiguousGpuSelection(`virtual-gpu: '${wanted}' matches ${matched.length} GPUs:\n${listing(gpus, matched)}`);
|
|
265
|
+
}
|
|
266
|
+
return matched[0];
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* An xorg.conf that drives `gpu` with a virtual screen and no outputs.
|
|
270
|
+
*
|
|
271
|
+
* `UseDisplayDevice "None"` keeps the NVIDIA driver off every connector, so it
|
|
272
|
+
* needs no DRM master and coexists with a desktop on the same card.
|
|
273
|
+
*/
|
|
274
|
+
export function xorgConfig(gpu, screen = GPU_SCREEN) {
|
|
275
|
+
const [width, height, depth] = screen.split("x");
|
|
276
|
+
const driver = xorgDriver(gpu);
|
|
277
|
+
const offscreen = driver === "nvidia" ? '\tOption "UseDisplayDevice" "None"\n' : "";
|
|
278
|
+
return `# Generated by camouwolf for headless="virtual-gpu".
|
|
279
|
+
# GPU: ${gpu.pciAddress} ${gpuVendor(gpu)} [${gpu.vendorId}:${gpu.deviceId}] driver=${gpu.kernelDriver}
|
|
280
|
+
Section "ServerFlags"
|
|
281
|
+
\tOption "AutoAddDevices" "false"
|
|
282
|
+
\tOption "AutoAddGPU" "false"
|
|
283
|
+
\tOption "DontVTSwitch" "true"
|
|
284
|
+
\tOption "AllowMouseOpenFail" "true"
|
|
285
|
+
EndSection
|
|
286
|
+
|
|
287
|
+
Section "Device"
|
|
288
|
+
\tIdentifier "camouwolf-gpu"
|
|
289
|
+
\tDriver "${driver}"
|
|
290
|
+
\tBusID "${busId(gpu)}"
|
|
291
|
+
${offscreen}\tOption "AllowEmptyInitialConfiguration" "true"
|
|
292
|
+
EndSection
|
|
293
|
+
|
|
294
|
+
Section "Monitor"
|
|
295
|
+
\tIdentifier "camouwolf-monitor"
|
|
296
|
+
\tHorizSync 5.0 - 1000.0
|
|
297
|
+
\tVertRefresh 5.0 - 200.0
|
|
298
|
+
EndSection
|
|
299
|
+
|
|
300
|
+
Section "Screen"
|
|
301
|
+
\tIdentifier "camouwolf-screen"
|
|
302
|
+
\tDevice "camouwolf-gpu"
|
|
303
|
+
\tMonitor "camouwolf-monitor"
|
|
304
|
+
\tDefaultDepth ${depth}
|
|
305
|
+
\tSubSection "Display"
|
|
306
|
+
\t\tDepth ${depth}
|
|
307
|
+
\t\tVirtual ${width} ${height}
|
|
308
|
+
\tEndSubSection
|
|
309
|
+
EndSection
|
|
310
|
+
|
|
311
|
+
Section "ServerLayout"
|
|
312
|
+
\tIdentifier "camouwolf-layout"
|
|
313
|
+
\tScreen "camouwolf-screen"
|
|
314
|
+
EndSection
|
|
315
|
+
`;
|
|
316
|
+
}
|
|
317
|
+
/** The config's file name in /etc/X11, keyed by PCI address. */
|
|
318
|
+
export function configName(gpu) {
|
|
319
|
+
return `camouwolf-gpu-${gpu.pciAddress.replace(/[:.]/g, "_")}.conf`;
|
|
320
|
+
}
|
|
321
|
+
/** The one command that sets this machine up for `gpu`. */
|
|
322
|
+
export function setupCommand(gpu) {
|
|
323
|
+
const sudo = process.getuid?.() === 0 ? "" : "sudo ";
|
|
324
|
+
return `npx camouwolf gpu-setup ${gpu.pciAddress} | ${sudo}sh`;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* A root shell script that sets this machine up for headless="virtual-gpu" on
|
|
328
|
+
* `gpu`: Xorg and its setuid wrapper, the wrapper's settings, and the card's
|
|
329
|
+
* config. Safe to run again.
|
|
330
|
+
*/
|
|
331
|
+
export function setupScript(gpu) {
|
|
332
|
+
const config = path.join(GpuDisplay.configDir, configName(gpu));
|
|
333
|
+
const xwrapper = GpuDisplay.xwrapperConfig;
|
|
334
|
+
const settings = Object.entries(XWRAPPER_SETTINGS)
|
|
335
|
+
.map(([key, value]) => `${key}=${value}\\n`)
|
|
336
|
+
.join("");
|
|
337
|
+
const deletes = Object.keys(XWRAPPER_SETTINGS)
|
|
338
|
+
.map((key) => `-e '/^[[:space:]]*${key}[[:space:]]*=/d'`)
|
|
339
|
+
.join(" ");
|
|
340
|
+
const nvidia = xorgDriver(gpu) === "nvidia"
|
|
341
|
+
? "\n" +
|
|
342
|
+
"if ! find /usr/lib /usr/lib64 -name nvidia_drv.so 2>/dev/null | grep -q .; then\n" +
|
|
343
|
+
"\techo 'camouwolf gpu-setup: no nvidia_drv.so found. Install the NVIDIA driver'\\''s Xorg " +
|
|
344
|
+
"module, matching the kernel driver (on Ubuntu, xserver-xorg-video-nvidia-<version>).' >&2\n" +
|
|
345
|
+
"fi\n"
|
|
346
|
+
: "";
|
|
347
|
+
return `#!/bin/sh
|
|
348
|
+
# Generated by camouwolf gpu-setup for headless="virtual-gpu".
|
|
349
|
+
# GPU: ${describeGpu(gpu)}
|
|
350
|
+
# Run once as root: ${setupCommand(gpu)}
|
|
351
|
+
set -e
|
|
352
|
+
if [ "$(id -u)" -ne 0 ]; then
|
|
353
|
+
\techo 'camouwolf gpu-setup: run this as root (pipe it to sudo sh).' >&2
|
|
354
|
+
\texit 1
|
|
355
|
+
fi
|
|
356
|
+
|
|
357
|
+
# 1. The Xorg server and its setuid wrapper, Xorg.wrap.
|
|
358
|
+
if command -v apt-get >/dev/null 2>&1; then
|
|
359
|
+
\tdpkg -s xserver-xorg-core xserver-xorg-legacy >/dev/null 2>&1 \\
|
|
360
|
+
\t\t|| DEBIAN_FRONTEND=noninteractive apt-get install -y xserver-xorg-core xserver-xorg-legacy
|
|
361
|
+
elif ! command -v Xorg >/dev/null 2>&1; then
|
|
362
|
+
\techo 'camouwolf gpu-setup: install the Xorg server and its setuid wrapper (Xorg.wrap), then run this again.' >&2
|
|
363
|
+
\texit 1
|
|
364
|
+
fi
|
|
365
|
+
${nvidia}
|
|
366
|
+
# 2. Let Xorg.wrap start Xorg as root for any user. A launch from ssh, systemd
|
|
367
|
+
# or a container is not a console login. Any local user can then start a
|
|
368
|
+
# root X server.
|
|
369
|
+
touch ${xwrapper}
|
|
370
|
+
sed -i ${deletes} ${xwrapper}
|
|
371
|
+
printf '${settings}' >> ${xwrapper}
|
|
372
|
+
|
|
373
|
+
# 3. The card's config, bound by PCI address.
|
|
374
|
+
cat > ${config} <<'CAMOUWOLF_XORG_CONF'
|
|
375
|
+
${xorgConfig(gpu)}CAMOUWOLF_XORG_CONF
|
|
376
|
+
|
|
377
|
+
echo 'headless="virtual-gpu" is set up for ${gpu.pciAddress}.'
|
|
378
|
+
`;
|
|
379
|
+
}
|
|
380
|
+
/** What keeps Xorg.wrap from starting Xorg for this user, if anything. */
|
|
381
|
+
export function setupProblems() {
|
|
382
|
+
const problems = [];
|
|
383
|
+
const wrappers = GpuDisplay.xorgWrappers;
|
|
384
|
+
if (!wrappers.some((wrapper) => fs.existsSync(wrapper))) {
|
|
385
|
+
problems.push(`no setuid Xorg.wrap (${wrappers.join(" or ")})`);
|
|
386
|
+
}
|
|
387
|
+
const xwrapper = GpuDisplay.xwrapperConfig;
|
|
388
|
+
const settings = new Map();
|
|
389
|
+
for (const line of read(xwrapper).split("\n")) {
|
|
390
|
+
const at = line.indexOf("=");
|
|
391
|
+
if (at >= 0)
|
|
392
|
+
settings.set(line.slice(0, at).trim(), line.slice(at + 1).trim());
|
|
393
|
+
}
|
|
394
|
+
for (const [key, wanted] of Object.entries(XWRAPPER_SETTINGS)) {
|
|
395
|
+
const have = settings.get(key) || "unset";
|
|
396
|
+
if (have !== wanted)
|
|
397
|
+
problems.push(`${xwrapper} has ${key}=${have}, needs ${key}=${wanted}`);
|
|
398
|
+
}
|
|
399
|
+
return problems;
|
|
400
|
+
}
|
|
401
|
+
/** The logind session on VT `vt`, which its user may activate without root. */
|
|
402
|
+
function sessionOnVt(vt) {
|
|
403
|
+
let sessions;
|
|
404
|
+
try {
|
|
405
|
+
sessions = execFileSync("loginctl", ["list-sessions", "--no-legend"], {
|
|
406
|
+
encoding: "utf-8",
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
catch {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
for (const line of sessions.split("\n")) {
|
|
413
|
+
const session = line.trim().split(/\s+/)[0];
|
|
414
|
+
if (!session)
|
|
415
|
+
continue;
|
|
416
|
+
try {
|
|
417
|
+
const number = execFileSync("loginctl", ["show-session", session, "-p", "VTNr", "--value"], { encoding: "utf-8" }).trim();
|
|
418
|
+
if (number === vt)
|
|
419
|
+
return session;
|
|
420
|
+
}
|
|
421
|
+
catch { }
|
|
422
|
+
}
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
function run(command, args) {
|
|
426
|
+
try {
|
|
427
|
+
execFileSync(command, args, { stdio: "ignore" });
|
|
428
|
+
}
|
|
429
|
+
catch { }
|
|
430
|
+
}
|
|
431
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
432
|
+
/**
|
|
433
|
+
* A headless Xorg server on one GPU, for headless="virtual-gpu".
|
|
434
|
+
*
|
|
435
|
+
* As root, Xorg takes a temporary config and log. Otherwise it runs through
|
|
436
|
+
* the setuid Xorg.wrap, which accepts only a config name relative to /etc/X11
|
|
437
|
+
* and no -logfile, so the config is installed there once.
|
|
438
|
+
*/
|
|
439
|
+
export class GpuDisplay extends VirtualDisplay {
|
|
440
|
+
/** Where the config is installed for the setuid wrapper. */
|
|
441
|
+
static configDir = SYSTEM_CONFIG_DIR;
|
|
442
|
+
/** Xorg.wrap's settings. */
|
|
443
|
+
static xwrapperConfig = XWRAPPER_CONFIG;
|
|
444
|
+
/** Where the setuid wrapper may be installed. */
|
|
445
|
+
static xorgWrappers = XORG_WRAPPERS;
|
|
446
|
+
gpu;
|
|
447
|
+
requireGpu;
|
|
448
|
+
client = null;
|
|
449
|
+
tempFiles = [];
|
|
450
|
+
vt = null;
|
|
451
|
+
vtTaken = false;
|
|
452
|
+
constructor(gpu, debug = false, requireGpu = true) {
|
|
453
|
+
super(PRODUCT, debug);
|
|
454
|
+
if (!this.fixed)
|
|
455
|
+
this.screen = GPU_SCREEN;
|
|
456
|
+
this.serverName = "Xorg";
|
|
457
|
+
this.gpu = gpu;
|
|
458
|
+
this.requireGpu = requireGpu;
|
|
459
|
+
}
|
|
460
|
+
get xorgPath() {
|
|
461
|
+
try {
|
|
462
|
+
return execFileSync("which", ["Xorg"], {
|
|
463
|
+
encoding: "utf-8",
|
|
464
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
465
|
+
}).trim();
|
|
466
|
+
}
|
|
467
|
+
catch {
|
|
468
|
+
throw new CannotFindXorg('headless="virtual-gpu" needs the Xorg server. Set this machine up with:\n' +
|
|
469
|
+
` ${setupCommand(this.gpu)}`);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/** A failed start, with what the machine is missing when that is why. */
|
|
473
|
+
error(message) {
|
|
474
|
+
if (process.getuid?.() !== 0) {
|
|
475
|
+
const problems = setupProblems();
|
|
476
|
+
if (problems.length) {
|
|
477
|
+
message +=
|
|
478
|
+
'\nheadless="virtual-gpu" without root needs Xorg.wrap to start Xorg as root:\n' +
|
|
479
|
+
problems.map((problem) => ` - ${problem}\n`).join("") +
|
|
480
|
+
`Set this machine up with:\n ${setupCommand(this.gpu)}`;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return new CannotExecuteXorg(message);
|
|
484
|
+
}
|
|
485
|
+
// No parent-death signal: the setuid Xorg.wrap drops it on exec. The held
|
|
486
|
+
// client and -terminate end the server instead (ready()).
|
|
487
|
+
wrapper() {
|
|
488
|
+
return [];
|
|
489
|
+
}
|
|
490
|
+
command() {
|
|
491
|
+
let files;
|
|
492
|
+
if (process.getuid?.() === 0) {
|
|
493
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "camouwolf-xorg-"));
|
|
494
|
+
const config = path.join(dir, "xorg.conf");
|
|
495
|
+
fs.writeFileSync(config, xorgConfig(this.gpu, this.screen), {
|
|
496
|
+
mode: 0o600,
|
|
497
|
+
});
|
|
498
|
+
const log = path.join(dir, "xorg.log");
|
|
499
|
+
this.tempFiles = [dir, log];
|
|
500
|
+
files = ["-config", config, "-logfile", log];
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
const name = configName(this.gpu);
|
|
504
|
+
const installed = path.join(GpuDisplay.configDir, name);
|
|
505
|
+
if (!fs.existsSync(installed)) {
|
|
506
|
+
throw new CannotExecuteXorg(`headless="virtual-gpu" without root needs ${installed}. ` +
|
|
507
|
+
`Set this machine up with:\n ${setupCommand(this.gpu)}`);
|
|
508
|
+
}
|
|
509
|
+
files = ["-config", name];
|
|
510
|
+
}
|
|
511
|
+
return [this.xorgPath, ...files, ...SERVER_FLAGS];
|
|
512
|
+
}
|
|
513
|
+
serverEnv() {
|
|
514
|
+
return process.env;
|
|
515
|
+
}
|
|
516
|
+
get log() {
|
|
517
|
+
return this.tempFiles[1] ?? `/var/log/Xorg.${this._display}.log`;
|
|
518
|
+
}
|
|
519
|
+
async get() {
|
|
520
|
+
this.vt = read(ACTIVE_VT) || null;
|
|
521
|
+
this.vtTaken = false;
|
|
522
|
+
const vt = this.vt;
|
|
523
|
+
const guard = vt
|
|
524
|
+
? setInterval(() => {
|
|
525
|
+
if (read(ACTIVE_VT) !== vt)
|
|
526
|
+
this.vtTaken = true;
|
|
527
|
+
}, 100)
|
|
528
|
+
: null;
|
|
529
|
+
try {
|
|
530
|
+
const display = await super.get();
|
|
531
|
+
if (this.vtTaken)
|
|
532
|
+
this.failVt();
|
|
533
|
+
return display;
|
|
534
|
+
}
|
|
535
|
+
finally {
|
|
536
|
+
if (guard)
|
|
537
|
+
clearInterval(guard);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
async ready() {
|
|
541
|
+
const deadline = Date.now() + SETTLE_MS;
|
|
542
|
+
while (Date.now() < deadline) {
|
|
543
|
+
if (this.vtTaken)
|
|
544
|
+
this.failVt();
|
|
545
|
+
const proc = this.proc;
|
|
546
|
+
if (proc && (proc.exitCode !== null || proc.signalCode !== null)) {
|
|
547
|
+
const error = this.error(`Xorg exited (${proc.exitCode ?? proc.signalCode}) right after starting. See ${this.log}.`);
|
|
548
|
+
this.kill();
|
|
549
|
+
throw error;
|
|
550
|
+
}
|
|
551
|
+
await sleep(100);
|
|
552
|
+
}
|
|
553
|
+
// The server exits at its last client's disconnect (-terminate), and a
|
|
554
|
+
// server no client ever reached never does. Holding one client from here
|
|
555
|
+
// on means it always has a last client: this one, closed by kill() or by
|
|
556
|
+
// the kernel when this process dies. An unprivileged launcher may not be
|
|
557
|
+
// able to signal a root Xorg, so this is the teardown that always works.
|
|
558
|
+
const client = net.connect({ path: `/tmp/.X11-unix/X${this._display}` });
|
|
559
|
+
try {
|
|
560
|
+
await new Promise((resolve, reject) => {
|
|
561
|
+
client.once("connect", resolve);
|
|
562
|
+
client.once("error", reject);
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
catch (error) {
|
|
566
|
+
this.kill();
|
|
567
|
+
throw this.error(`Could not connect to the Xorg display it reported: ${error}`);
|
|
568
|
+
}
|
|
569
|
+
client.on("error", () => { });
|
|
570
|
+
client.write(X11_SETUP);
|
|
571
|
+
// Never keeps the launcher's event loop alive.
|
|
572
|
+
client.unref();
|
|
573
|
+
this.client = client;
|
|
574
|
+
}
|
|
575
|
+
failVt() {
|
|
576
|
+
const vt = this.vt ?? "";
|
|
577
|
+
const number = vt.replace(/^tty/, "");
|
|
578
|
+
const session = sessionOnVt(number);
|
|
579
|
+
if (session)
|
|
580
|
+
run("loginctl", ["activate", session]);
|
|
581
|
+
if (read(ACTIVE_VT) !== vt)
|
|
582
|
+
run("chvt", [number]);
|
|
583
|
+
const restored = read(ACTIVE_VT) === vt;
|
|
584
|
+
this.kill();
|
|
585
|
+
throw new GpuVtTakeover(`Xorg on ${describeGpu(this.gpu)} switched the active VT away from ${vt}. ` +
|
|
586
|
+
(restored
|
|
587
|
+
? "It was switched back."
|
|
588
|
+
: `Switch back with \`sudo chvt ${number}\`.`));
|
|
589
|
+
}
|
|
590
|
+
/** Every process started with DISPLAY set to this display, except Xorg and us. */
|
|
591
|
+
displayProcesses() {
|
|
592
|
+
const needle = `\0DISPLAY=:${this._display}\0`;
|
|
593
|
+
const skip = new Set([process.pid, this.proc?.pid]);
|
|
594
|
+
const pids = [];
|
|
595
|
+
for (const entry of list("/proc")) {
|
|
596
|
+
if (!/^\d+$/.test(entry) || skip.has(Number(entry)))
|
|
597
|
+
continue;
|
|
598
|
+
try {
|
|
599
|
+
const environ = fs.readFileSync(`/proc/${entry}/environ`, "latin1");
|
|
600
|
+
if (`\0${environ}`.includes(needle))
|
|
601
|
+
pids.push(Number(entry));
|
|
602
|
+
}
|
|
603
|
+
catch { }
|
|
604
|
+
}
|
|
605
|
+
return pids;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Whether a process on this display renders on the GPU.
|
|
609
|
+
*
|
|
610
|
+
* The browser opens the card's DRM nodes for DMABuf even when it renders in
|
|
611
|
+
* software, so an open render node proves nothing. Rendering is the NVIDIA
|
|
612
|
+
* driver's /dev/nvidia<minor> held open, or work the kernel accounted to a
|
|
613
|
+
* GPU engine on one of the card's DRM nodes.
|
|
614
|
+
*/
|
|
615
|
+
usesGpu() {
|
|
616
|
+
const nodes = drmNodes(this.gpu);
|
|
617
|
+
for (const pid of this.displayProcesses()) {
|
|
618
|
+
for (const fd of list(`/proc/${pid}/fd`)) {
|
|
619
|
+
try {
|
|
620
|
+
const target = fs.readlinkSync(`/proc/${pid}/fd/${fd}`);
|
|
621
|
+
if (target === this.gpu.nvidiaNode ||
|
|
622
|
+
(nodes.includes(target) &&
|
|
623
|
+
drewOnGpu(fs.readFileSync(`/proc/${pid}/fdinfo/${fd}`, "utf-8"))))
|
|
624
|
+
return true;
|
|
625
|
+
}
|
|
626
|
+
catch { }
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
async waitForGpu() {
|
|
632
|
+
const waitS = Number(process.env[GPU_WAIT_ENV_VAR]) || GPU_WAIT_S;
|
|
633
|
+
const deadline = Date.now() + waitS * 1000;
|
|
634
|
+
while (Date.now() < deadline) {
|
|
635
|
+
if (this.usesGpu())
|
|
636
|
+
return true;
|
|
637
|
+
await sleep(250);
|
|
638
|
+
}
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
/** Throw, or warn with require_gpu: false, when the browser did not use the GPU. */
|
|
642
|
+
checkGpu(active) {
|
|
643
|
+
if (active)
|
|
644
|
+
return;
|
|
645
|
+
if (!this.requireGpu) {
|
|
646
|
+
LeakWarning.warn(loadWarnings().gpu_software_rendering);
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
throw new GpuSoftwareFallback(`The browser on :${this._display} never rendered on ${describeGpu(this.gpu)}, so it renders ` +
|
|
650
|
+
"in software. Chromium may have blocklisted the driver or failed to create a GPU " +
|
|
651
|
+
`context. See ${this.log}.`);
|
|
652
|
+
}
|
|
653
|
+
kill() {
|
|
654
|
+
this.client?.destroy();
|
|
655
|
+
this.client = null;
|
|
656
|
+
if (this._display !== null) {
|
|
657
|
+
// The browser's helpers (dbus, at-spi) are not in its process group.
|
|
658
|
+
for (const signal of ["SIGTERM", "SIGKILL"]) {
|
|
659
|
+
for (const pid of this.displayProcesses()) {
|
|
660
|
+
try {
|
|
661
|
+
process.kill(pid, signal);
|
|
662
|
+
}
|
|
663
|
+
catch { }
|
|
664
|
+
}
|
|
665
|
+
const deadline = Date.now() + 1500;
|
|
666
|
+
while (this.displayProcesses().length && Date.now() < deadline) {
|
|
667
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
// SIGTERM lets Xorg remove its root-owned socket, which this process
|
|
672
|
+
// cannot; the base kill() SIGKILLs only a server still running after that.
|
|
673
|
+
const proc = this.proc;
|
|
674
|
+
if (proc && proc.exitCode === null && proc.signalCode === null) {
|
|
675
|
+
try {
|
|
676
|
+
proc.kill("SIGTERM");
|
|
677
|
+
}
|
|
678
|
+
catch { }
|
|
679
|
+
const deadline = Date.now() + 10_000;
|
|
680
|
+
while (!exited(proc.pid) && Date.now() < deadline) {
|
|
681
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
super.kill();
|
|
685
|
+
if (this.tempFiles.length) {
|
|
686
|
+
fs.rmSync(this.tempFiles[0], { recursive: true, force: true });
|
|
687
|
+
this.tempFiles = [];
|
|
688
|
+
}
|
|
689
|
+
this._display = null;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
/** Whether `pid` has exited (a zombie counts: it renders nothing). */
|
|
693
|
+
function exited(pid) {
|
|
694
|
+
if (!pid)
|
|
695
|
+
return true;
|
|
696
|
+
const stat = read(`/proc/${pid}/stat`);
|
|
697
|
+
return !stat || / Z /.test(stat.slice(stat.lastIndexOf(")")));
|
|
698
|
+
}
|
|
699
|
+
/** Draw in a page of `target` and check the browser rendered on the GPU. */
|
|
700
|
+
export async function proveGpu(target, display) {
|
|
701
|
+
const page = await target.newPage();
|
|
702
|
+
let active;
|
|
703
|
+
try {
|
|
704
|
+
await page.goto(PROBE_PAGE);
|
|
705
|
+
active = await display.waitForGpu();
|
|
706
|
+
}
|
|
707
|
+
finally {
|
|
708
|
+
await page.close();
|
|
709
|
+
}
|
|
710
|
+
display.checkGpu(active);
|
|
711
|
+
}
|