Merge main into perf/exception-checks-main

This commit is contained in:
Chris Tate
2026-10-11 13:38:06 -05:00
28 changed files with 318 additions and 20 deletions
+3
View File
@@ -108,6 +108,9 @@ Verifies and reads the installed precompiled runtime objects and vendor archives
<dt><code>--windows-subsystem &lt;console|gui&gt;</code></dt>
<dd>Select the subsystem of a Windows executable. <code>console</code> is the default; <code>gui</code> prevents Windows from opening a console window for the app. Valid only for executable builds targeting Windows.</dd>
<dt><code>--detached-exit</code></dt>
<dd>On Linux, when the program calls <code>process.exit()</code>, hand the release of its memory to a helper process so the parent sees the exit sooner. The kernel otherwise frees the whole address space before the exit is reported, which takes tens of milliseconds per gigabyte. Opt in only where the trade-offs are acceptable: for up to about 100 ms after the exit, the helper keeps the executable mapped (overwriting the binary in place fails with "text file busy"), its memory stays charged to the cgroup, and the teardown CPU time no longer appears in the parent's resource usage. Valid only for executable builds; it has no effect on other platforms.</dd>
<dt><code>--npm-static &lt;pkg[,pkg…]|auto&gt;</code></dt>
<dd>EXPERIMENTAL. Compile the named npm packages' shipped JS statically as program modules instead of embedding them for the engine (repeatable; <code>auto</code> opts in every eligible direct import). A package the preflight refuses falls back to the island with a coverage-report note. See <a href="/dependencies">npm Dependencies</a> for maturity notes.</dd>
@@ -2,6 +2,6 @@
"schemaVersion": 3,
"nodeVersion": "24.15.0",
"nodeCommit": "848430679556aed0bd073f2bc263331ad84fa119",
"artifactVersion": "ae934a592466ef1e67c1",
"artifactVersion": "cb524f2eb043e40eb9f4",
"rowCount": 3662
}
@@ -746,9 +746,9 @@
"apiEntries": 24,
"static": {
"supported": 0,
"partial": 10,
"partial": 11,
"refused": 0,
"not-implemented": 14,
"not-implemented": 13,
"by-design": 0,
"unreviewed": 0,
"not-applicable": 0
@@ -61038,9 +61038,9 @@
"inherited": true
},
"static": {
"status": "not-implemented",
"detail": "No static compiler lowering is registered for this API yet.",
"verification": "registry-gap"
"status": "partial",
"detail": "Implemented for the call shapes accepted by the compiler lowering.",
"verification": "test-backed"
},
"dynamic": {
"status": "partial",
@@ -8,8 +8,8 @@
"tiers": {
"static": {
"replace-refusal": 42,
"verify-gap": 2418,
"audit-partial": 746,
"verify-gap": 2417,
"audit-partial": 747,
"classify": 2,
"implement": 80
},
@@ -66294,12 +66294,14 @@
"priority": "high",
"tiers": {
"static": {
"status": "not-implemented",
"action": "verify-gap",
"verification": "registry-gap",
"confidence": "low",
"source": "compiler-unmatched:os",
"tests": []
"status": "partial",
"action": "audit-partial",
"verification": "test-backed",
"confidence": "high",
"source": "surface-manifest:node-builtin.os.availableParallelism",
"tests": [
"tests/corpus/4598-os-available-parallelism.ts"
]
},
"dynamic": {
"status": "partial",
@@ -819,9 +819,9 @@
"apiEntries": 24,
"static": {
"supported": 0,
"partial": 10,
"partial": 11,
"refused": 0,
"not-implemented": 14,
"not-implemented": 13,
"by-design": 0,
"unreviewed": 0,
"not-applicable": 0
@@ -83033,7 +83033,8 @@
"evidence": "derived:descendants",
"tests": [
"tests/corpus/1480-os-network-interfaces.ts",
"tests/corpus/2917-builtin-callable-widths.ts"
"tests/corpus/2917-builtin-callable-widths.ts",
"tests/corpus/4598-os-available-parallelism.ts"
]
},
"dynamic": {
@@ -83279,8 +83280,11 @@
"inherited": true
},
"static": {
"status": "not-implemented",
"evidence": "compiler-unmatched:os"
"status": "partial",
"evidence": "surface-manifest:node-builtin.os.availableParallelism",
"tests": [
"tests/corpus/4598-os-available-parallelism.ts"
]
},
"dynamic": {
"status": "partial",
@@ -80,6 +80,7 @@
"node-builtin.module": ["tests/corpus/1598-cjs-builtin-require/main.js"],
"node-builtin.net": ["tests/corpus/2500-net-autosel-timeout.ts", "tests/corpus/2596-net-arg-ladders.cjs"],
"node-builtin.os": ["tests/corpus/1480-os-network-interfaces.ts", "tests/corpus/2917-builtin-callable-widths.ts"],
"node-builtin.os.availableParallelism": ["tests/corpus/4598-os-available-parallelism.ts"],
"node-builtin.path": ["tests/corpus/957-builtins-namespace.ts", "tests/corpus/2915-path-function-values.ts", "tests/corpus/2916-path-function-values-cjs.ts", "tests/corpus/2917-builtin-callable-widths.ts", "tests/corpus/2918-builtin-callable-widths-cjs.ts"],
"node-builtin.perf_hooks": ["tests/corpus/957-builtins-namespace.ts"],
"node-builtin.process": ["tests/corpus/990-process-basics.ts", "tests/corpus/2314-process-introspection.ts"],
+2
View File
@@ -126,6 +126,7 @@ async function tryFastPath(): Promise<number | null> {
backend: "llvm",
...(optimization === "dev" || optimization === "speed" ? { optimization } : {}),
...(values.strip ? { strip: true as const } : {}),
...(values["detached-exit"] ? { detachedExit: true as const } : {}),
npmStatic,
ffiProfile: ffiPath === null ? null : { path: ffiPath, bytes: ffiBytes! },
target: `${process.env["SCRIPTC_TARGET"] ?? "native"}:${buildPlatform}:${process.arch}:${
@@ -183,6 +184,7 @@ async function coverageFastPath(
(failOn !== undefined && failOn !== "blockers" && failOn !== "divergences") ||
values.emit !== undefined ||
values.strip ||
values["detached-exit"] ||
values.lib ||
values.profile !== undefined ||
values.ffi !== undefined ||
+2
View File
@@ -46,6 +46,8 @@ describe("output option compatibility", () => {
[{ emit: "llvm", windowsSubsystem: "gui" }, /windows-subsystem/],
[{ emit: "obj", strip: true }, /strip/],
[{ emit: "llvm", strip: true }, /strip/],
[{ emit: "obj", detachedExit: true }, /detached-exit/],
[{ emit: "llvm", detachedExit: true }, /detached-exit/],
] as const)("rejects %j", (override, message) => {
const result = resolveOutputOptions("build", { ...BASE, ...override });
expect(result).toEqual({ ok: false, message: expect.stringMatching(message) });
+1
View File
@@ -1688,6 +1688,7 @@ declare module "os" {
export function type(): string;
/* Total system memory in bytes (sysctl hw.memsize / sysconf). */
export function totalmem(): number;
export function availableParallelism(): number;
/* The passwd-entry snapshot (uv_os_get_passwd): shell is `string |
* null` to match @types/node (POSIX always answers the string arm);
* homedir is pw_dir — NOT os.homedir()'s $HOME-first cascade. The type
@@ -109,6 +109,16 @@ test("throwing-call count does not multiply identical scope cleanup", () => {
}
});
test("detached exit defines the runtime opt-in symbol only when requested", () => {
const module = moduleFor([call()]);
const plain = emitLlvmModule(module);
expect(emitLlvmModule(module, { detachedExit: false })).toBe(plain);
expect(plain).not.toContain("@scr_detached_exit");
const detached = emitLlvmModule(module, { detachedExit: true });
expect(detached.match(/^@scr_detached_exit = constant i8 1$/gm)).toHaveLength(1);
expect(detached.replace("@scr_detached_exit = constant i8 1\n", "")).toBe(plain);
});
test("inline RC is a speed-only emission mode and leaves release IR unchanged", () => {
for (const boxed of [false, true]) {
const module = moduleFor(Array.from({ length: 4 }, call), { boxed });
@@ -401,6 +401,10 @@ export interface LlvmTargetOptions {
/** Emit retain/release fast paths inline (the `speed` posture). Off, every
* retain and release is a runtime call. */
inlineRc?: boolean;
/** Define `scr_detached_exit` so the Linux runtime hands the address-space
* teardown after `process.exit` to a helper process (`--detached-exit`).
* Executables only; the runtime's weak default keeps it off. */
detachedExit?: boolean;
}
export function emitLlvmModule(mod: IrModule, options: LlvmTargetOptions = {}): string {
@@ -528,6 +532,7 @@ export class LlEmitter {
private readonly executableTls: boolean;
private readonly emitLibraryIdentity: boolean;
private readonly runtimeAbiMarker: boolean;
private readonly detachedExit: boolean;
/** Interned string literals: UTF-8 text → { symbol, byte length } —
* first-use order, the runtime ABI’s determinism discipline. */
private readonly literals = new Map<string, { sym: string; len: number }>();
@@ -836,6 +841,7 @@ export class LlEmitter {
!/apple|darwin|windows|mingw|cygwin|win32/.test(options.targetTriple || process.platform);
this.emitLibraryIdentity = options.emitLibraryIdentity !== false;
this.runtimeAbiMarker = options.runtimeAbiMarker === true;
this.detachedExit = options.detachedExit === true;
this.rcHelpers = options.inlineRc === true ? new Set() : null;
// ScrCycHdr.color stays 12 bytes behind a wasm32 object and 16 bytes
// behind a 64-bit object. The wasm32 header pads before color so its
@@ -1922,6 +1928,10 @@ export class LlEmitter {
...(this.runtimeAbiMarker && this.mod.lib === undefined
? [`declare void @${RUNTIME_ABI_MARKER}()`]
: []),
// Overrides the runtime's weak default (scr_lib.c).
...(this.detachedExit && this.mod.lib === undefined
? [`@scr_detached_exit = constant i8 1`]
: []),
);
for (const d of this.decls) out.push(d);
out.push(``);
@@ -151,6 +151,7 @@ export const LIB_FN_SYMS: Record<string, string> = {
"os.homedir": "scr_os_homedir",
"os.type": "scr_os_type",
"os.totalmem": "scr_os_totalmem",
"os.availableParallelism": "scr_os_available_parallelism",
"os.release": "scr_os_release",
"os.userName": "scr_os_user_name",
"os.userShell": "scr_os_user_shell",
+8 -1
View File
@@ -86,6 +86,7 @@ async function main(args: string[], host: CliHost): Promise<number> {
values.profile !== undefined ||
values.strip ||
values["windows-subsystem"] !== undefined ||
values["detached-exit"] ||
(values["npm-static"] ?? []).length > 0 ||
values["provenance-sources"] ||
externalTypeArgs.length > 0 ||
@@ -151,11 +152,12 @@ async function main(args: string[], host: CliHost): Promise<number> {
values.strip ||
values.ffi !== undefined ||
values["windows-subsystem"] !== undefined ||
values["detached-exit"] ||
(values["npm-static"] ?? []).length > 0 ||
externalTypeArgs.length > 0
) {
fail(
"scriptc build --lib takes no --dynamic/--backend/--emit/--print/--optimization/--strip/--windows-subsystem/--npm-static/--ffi/--external-types: the profile pins the emission and optimization, npm imports are judged automatically, outbound FFI belongs to executable builds, and external type mappings belong to coverage",
"scriptc build --lib takes no --dynamic/--backend/--emit/--print/--optimization/--strip/--windows-subsystem/--detached-exit/--npm-static/--ffi/--external-types: the profile pins the emission and optimization, npm imports are judged automatically, outbound FFI belongs to executable builds, and external type mappings belong to coverage",
);
}
const profilePath = resolve(profileArg);
@@ -198,6 +200,9 @@ async function main(args: string[], host: CliHost): Promise<number> {
if (command === "coverage" && values.strip) {
fail(`--strip is a build/run option\n\n${USAGE}`);
}
if (command === "coverage" && values["detached-exit"]) {
fail(`--detached-exit is a build/run option\n\n${USAGE}`);
}
if (
values.print !== undefined &&
values.print !== "native-link-info" &&
@@ -292,6 +297,7 @@ async function main(args: string[], host: CliHost): Promise<number> {
sanitize: values.sanitize,
...(values.optimization === undefined ? {} : { optimization: values.optimization }),
strip: values.strip,
detachedExit: values["detached-exit"],
...(windowsSubsystem === undefined ? {} : { windowsSubsystem }),
...(values.ffi === undefined ? {} : { ffi: values.ffi }),
});
@@ -380,6 +386,7 @@ async function main(args: string[], host: CliHost): Promise<number> {
...(optimization !== undefined ? { optimization } : {}),
...(values.strip ? { strip: true } : {}),
...(windowsSubsystem !== undefined ? { windowsSubsystem } : {}),
...(values["detached-exit"] ? { detachedExit: true } : {}),
...(npmStatic !== undefined ? { npmStatic } : {}),
...(ffiProfilePath !== undefined ? { ffiProfilePath } : {}),
...(printNativeLinkInfo ? { nativeLinkInfo: true } : {}),
@@ -10,6 +10,7 @@ export interface OutputOptionValues {
optimization?: string;
strip?: boolean;
windowsSubsystem?: string;
detachedExit?: boolean;
ffi?: string;
}
@@ -69,6 +70,9 @@ export function resolveOutputOptions(
if (values.strip && emit !== "exe") {
return { ok: false, message: `--strip is only supported with --emit=exe` };
}
if (values.detachedExit && emit !== "exe") {
return { ok: false, message: `--detached-exit is only supported with --emit=exe` };
}
if (emit === "ir" && backend !== undefined) {
return {
ok: false,
+6
View File
@@ -42,6 +42,11 @@ Options:
--windows-subsystem <console|gui>
Windows executable subsystem (default: console). gui
prevents Windows from opening a console window
--detached-exit
Linux: after process.exit, free the program's memory in
a helper process so the parent sees the exit sooner. The
helper keeps the executable mapped and its memory charged
for up to ~100 ms after exit (opt in; --emit=exe only)
--keep-llvm keep generated LLVM IR beside the executable (default)
--no-keep-llvm delete generated LLVM IR after compiling
--emit-ir also write IR beside an executable or library archive;
@@ -81,6 +86,7 @@ export const CLI_OPTIONS = {
optimization: { type: "string" },
strip: { type: "boolean", default: false },
"windows-subsystem": { type: "string" },
"detached-exit": { type: "boolean", default: false },
"keep-llvm": { type: "boolean", default: true },
"emit-ir": { type: "boolean", default: false },
sanitize: { type: "boolean", default: false },
+5
View File
@@ -34,6 +34,11 @@ export interface CompileBaseOptions {
/** Windows PE executable subsystem. Console is the default; GUI suppresses
* automatic console-window creation. Only valid for Windows executables. */
windowsSubsystem?: WindowsSubsystem;
/** On Linux, hand the address-space teardown after `process.exit` to a
* helper process so the parent sees the exit sooner. The helper keeps the
* executable mapped and the memory charged for up to ~100 ms after exit.
* Executables only; no effect on other platforms. */
detachedExit?: boolean;
/** --npm-static: package names whose shipped, unminified JS compiles
* STATICALLY as program modules (inference types the bodies; statements
* the lowering cannot prove become runtime fences). "auto" opts in every
@@ -217,6 +217,7 @@ test("early executable keys isolate compile modes, paths, implementation, and FF
{ ...f.options, dynamic: true },
{ ...f.options, optimization: "dev" },
{ ...f.options, optimization: "speed" },
{ ...f.options, detachedExit: true },
{ ...f.options, npmStatic: "auto" },
{ ...f.options, npmStatic: ["commander"] },
{ ...f.options, outPath: join(f.options.outDir, "other") },
@@ -110,6 +110,8 @@ export interface EarlyExecutableCacheOptions {
strip?: true;
/** Omitted for the default Windows console subsystem. */
windowsSubsystem?: "gui";
/** Omitted unless --detached-exit is set. */
detachedExit?: true;
npmStatic: readonly string[] | "auto" | null;
/** Raw manifest identity: path and bytes. Native archives remain under the
* stricter native cache's independent dependency validation. */
@@ -220,6 +222,7 @@ function cacheKey(options: EarlyExecutableCacheOptions): string {
...optimizationKeyParts(options.optimization),
...(options.strip ? ["strip"] : []),
...(options.windowsSubsystem === "gui" ? ["windows-subsystem-gui"] : []),
...(options.detachedExit ? ["detached-exit"] : []),
options.npmStatic === null
? "<npm-static-off>"
: options.npmStatic === "auto"
@@ -258,6 +261,7 @@ function routeKey(options: Omit<EarlyExecutableRouteOptions, "nativeEnvironment"
for (const part of optimizationKeyParts(options.optimization)) hash.update(`${part}\0`);
if (options.strip) hash.update("strip\0");
if (options.windowsSubsystem === "gui") hash.update("windows-subsystem-gui\0");
if (options.detachedExit) hash.update("detached-exit\0");
hash
.update(
options.npmStatic === null
@@ -1311,6 +1311,14 @@ export const BUILTIN_MODULE_FNS: Record<
type: { fn: "os.type", params: [], result: STRING, valueParams: [] },
// Total physical memory in bytes — same predating-pair story.
totalmem: { fn: "os.totalmem", params: [], result: F64, valueParams: [] },
// uv_available_parallelism: the CPUs this process may use (affinity,
// then cgroup quota on Linux), at least 1.
availableParallelism: {
fn: "os.availableParallelism",
params: [],
result: F64,
valueParams: [],
},
// Entirely special-cased (lowerOsNetworkInterfacesCall): the result is
// the call site's mapped Dict<NetworkInterfaceInfo[]> shape, verified
// structurally there — this entry only routes the dispatch.
+15
View File
@@ -762,6 +762,7 @@ async function prepareExecutableInput(
runtimeAbiMarker: useRuntimePack,
objectAudit: opts.sanitize === true,
...(opts.optimization === "speed" ? { inlineRc: true } : {}),
...(opts.detachedExit === true ? { detachedExit: true } : {}),
});
} catch (err) {
if (!(err instanceof LlvmUnsupportedError)) throw err;
@@ -842,6 +843,19 @@ async function compileTracked(
sourceTexts: new Map(),
};
}
if (opts.detachedExit === true && outputKind !== "exe") {
return {
ok: false,
diagnostics: [
nativeCodegenDiag(
"SC3002",
"--detached-exit is only supported for executable output",
entryPath,
),
],
sourceTexts: new Map(),
};
}
if (opts.nativeLinkInfo === true && outputKind !== "obj") {
return {
ok: false,
@@ -1017,6 +1031,7 @@ async function compileTracked(
...optimizationField(opts.optimization),
...(opts.strip ? { strip: true as const } : {}),
...(opts.windowsSubsystem === "gui" ? { windowsSubsystem: "gui" as const } : {}),
...(opts.detachedExit === true ? { detachedExit: true as const } : {}),
npmStatic: opts.npmStatic ?? null,
ffiProfile:
opts.ffiProfilePath === undefined || ffiProfileBytes === null
@@ -432,6 +432,8 @@ export const LIB_FN_SIGS = defineLibFnSignatures({
"os.type": { argTypes: [], result: STRING },
/** os.totalmem(): total physical memory in bytes. Never throws. */
"os.totalmem": { argTypes: [], result: F64 },
/** os.availableParallelism(): CPUs the process may use (libuv's answer), >= 1. Never throws. */
"os.availableParallelism": { argTypes: [], result: F64 },
/** umask(2): arg < 0 reads without setting (umask has no read-only form
* — set 0, restore); otherwise sets and answers the PREVIOUS mask.
* Never throws. */
+1
View File
@@ -284,6 +284,7 @@ export function prepareNativeExecutable(
runtimeAbiMarker: outputKind === "obj" || outputKind === "exe",
objectAudit: options.sanitize === true,
...(options.optimization === "speed" ? { inlineRc: true } : {}),
...(options.detachedExit === true && outputKind === "exe" ? { detachedExit: true } : {}),
...(options.optimization === "dev" && !options.strip
? { debugSources: prepared.sourceTexts }
: {}),
+6
View File
@@ -1334,6 +1334,12 @@
"status": "static",
"note": "constant value read"
},
{
"id": "node-builtin.os.availableParallelism",
"kind": "node-builtin",
"name": "os.availableParallelism",
"status": "static"
},
{
"id": "node-builtin.os.homedir",
"kind": "node-builtin",
@@ -46,6 +46,19 @@ test("Windows subsystem selection refuses non-executable compiler output", async
await expect(readFile(outPath)).rejects.toMatchObject({ code: "ENOENT" });
});
test("detached exit refuses non-executable compiler output", async () => {
const { entry, outDir } = await fixture();
const outPath = join(outDir, "main.ll");
const result = await compile(entry, { outDir, outPath, outputKind: "llvm", detachedExit: true });
expect(result).toMatchObject({
ok: false,
diagnostics: [
{ code: "SC3002", message: expect.stringContaining("only supported for executable output") },
],
});
await expect(readFile(outPath)).rejects.toMatchObject({ code: "ENOENT" });
});
test("LLVM is the exact primary artifact and never creates an executable", async () => {
const { entry, outDir } = await fixture();
const llvmPath = join(outDir, "exact.llvm-output");
@@ -15834,6 +15834,19 @@
"<repo>/tests/corpus/final-field-borrows.ts"
],
"diags": []
},
"<repo>/tests/corpus/4598-os-available-parallelism.ts": {
"order": [
"<repo>/tests/corpus/4598-os-available-parallelism.ts"
],
"diags": []
},
"<repo>/tests/corpus/constant-module-globals/main.ts": {
"order": [
"<repo>/tests/corpus/constant-module-globals/consts.ts",
"<repo>/tests/corpus/constant-module-globals/main.ts"
],
"diags": []
}
}
}
+159
View File
@@ -123,6 +123,11 @@ static ScrStr *scr_fs_win_utf8(const WCHAR *text, size_t length) {
#elif defined(__linux__)
#include <netpacket/packet.h>
#endif
#if defined(__linux__)
#include <sched.h> /* sched_getaffinity, CPU_COUNT (os.availableParallelism) */
#elif defined(__APPLE__)
#include <sys/sysctl.h> /* sysctlbyname (os.availableParallelism) */
#endif
#define scr_sys_mkdir(p, m) mkdir((p), (m))
@@ -1376,6 +1381,13 @@ ScrStr *scr_os_type(void) {
return scr_str_new("Windows_NT", 10);
}
double scr_os_available_parallelism(void) {
/* uv_available_parallelism on Windows: GetSystemInfo's processor count. */
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors < 1 ? 1 : (double)info.dwNumberOfProcessors;
}
double scr_os_totalmem(void) {
MEMORYSTATUSEX ms;
memset(&ms, 0, sizeof ms);
@@ -1413,6 +1425,8 @@ ScrStr *scr_os_user_homedir(void) { return scr_os_homedir(); }
ScrStr *scr_os_release(void) { return scr_str_new("", 0); }
ScrStr *scr_os_type(void) { return scr_str_new("WASI", 4); }
double scr_os_totalmem(void) { return 0; }
/* One thread of execution, as the dynamic island's os answers. */
double scr_os_available_parallelism(void) { return 1; }
/* The guest temp namespace is stable across hosts. `scriptc run` preopens
* the host's /tmp at this path; other WASI hosts can provide the same
* capability without leaking a host-specific TMPDIR into the module. */
@@ -1486,6 +1500,98 @@ ScrStr *scr_os_type(void) {
return scr_str_new(u.sysname, strlen(u.sysname));
}
#ifdef __linux__
/* libuv's uv__slurp: one read of at most cap - 1 bytes of path into buf
* (NUL-terminated); false when the file cannot be opened or read. A single
* read, like libuv, so a long /proc/self/cgroup is cut where Node cuts it. */
static bool scr_os_slurp(const char *path, char *buf, size_t cap) {
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return false;
ssize_t n;
do
n = read(fd, buf, cap - 1);
while (n < 0 && errno == EINTR);
close(fd);
if (n < 0) return false;
buf[n] = '\0';
return true;
}
/* libuv's uv__get_constrained_cpu (1.51): the CPU quota of the process's
* cgroup in whole CPUs. For cgroup v2 it walks cpu.max from the process's
* cgroup up to the root; like libuv, the last level with a numeric limit
* (the outermost) decides. Returns false when there is no answer. */
static bool scr_os_cgroup_cpu_quota(long long *quota) {
char cgroup[1024];
if (!scr_os_slurp("/proc/self/cgroup", cgroup, sizeof cgroup)) return false;
if (strncmp(cgroup, "0::/", 4) == 0) {
static const char mount[] = "/sys/fs/cgroup";
const char *trimmed = cgroup + 4;
int size = (int)strcspn(trimmed, "\n");
char path[256], file[256], buf[1024], value[16];
snprintf(path, sizeof path, "%s/%.*s/cgroup.controllers", mount, size, trimmed);
if (!scr_os_slurp(path, buf, sizeof buf)) return false;
snprintf(path, sizeof path, "%s/%.*s", mount, size, trimmed);
while (strncmp(path, mount, sizeof mount - 1) == 0) {
long long limit, period;
snprintf(file, sizeof file, "%s/cpu.max", path);
if (scr_os_slurp(file, value, sizeof value) && strncmp(value, "max", 3) != 0 &&
sscanf(value, "%lld %lld", &limit, &period) == 2 && period != 0)
*quota = limit / period;
char *slash = strrchr(path, '/');
if (slash == NULL || strcmp(path, mount) == 0) break;
*slash = '\0';
}
return true;
}
char *cpu = strstr(cgroup, ":cpu,");
if (cpu == NULL) return false;
cpu += sizeof ":cpu," - 1;
int size = (int)strcspn(cpu, "\n");
char path[256], buf[64];
long long per_period, period;
snprintf(path, sizeof path, "/sys/fs/cgroup/%.*s/cpu.cfs_quota_us", size, cpu);
if (!scr_os_slurp(path, buf, sizeof buf) || sscanf(buf, "%lld", &per_period) != 1) return false;
snprintf(path, sizeof path, "/sys/fs/cgroup/%.*s/cpu.cfs_period_us", size, cpu);
if (!scr_os_slurp(path, buf, sizeof buf) || sscanf(buf, "%lld", &period) != 1 || period == 0)
return false;
*quota = per_period / period;
return true;
}
#endif
double scr_os_available_parallelism(void) {
/* uv_available_parallelism (libuv 1.51, Node's os.availableParallelism):
* the CPUs this process may run on (Linux: its affinity mask, then any
* smaller cgroup CPU quota; Darwin: hw.activecpu, hw.logicalcpu, hw.ncpu),
* else the online processors; at least 1. */
long rc = -1;
#if defined(__linux__)
cpu_set_t set;
memset(&set, 0, sizeof set);
if (sched_getaffinity(0, sizeof set, &set) == 0) rc = CPU_COUNT(&set);
#elif defined(__APPLE__)
static const char *const mib[] = {"hw.activecpu", "hw.logicalcpu", "hw.ncpu"};
for (size_t i = 0; i < sizeof mib / sizeof mib[0]; i++) {
int nprocs = 0;
size_t len = sizeof nprocs;
if (sysctlbyname(mib[i], &nprocs, &len, NULL, 0) == 0 && len == sizeof nprocs && nprocs > 0) {
rc = nprocs;
break;
}
}
#endif
if (rc < 0) rc = sysconf(_SC_NPROCESSORS_ONLN);
#if defined(__linux__)
/* A quota below one CPU truncates to 0 and is ignored, not clamped to 1:
* libuv (1.51 through 1.52) applies only `quota > 0 && quota < rc`, so
* Node then answers the affinity count. */
long long quota = 0;
if (scr_os_cgroup_cpu_quota(&quota) && quota > 0 && quota < rc) rc = (long)quota;
#endif
return rc < 1 ? 1 : (double)rc;
}
double scr_os_totalmem(void) {
/* Total physical memory in bytes (sysconf pages × page size — Darwin
* and Linux both answer _SC_PHYS_PAGES). */
@@ -1806,6 +1912,56 @@ double scr_process_exit_code_or_zero(void) {
return (double)scr_exit_code_hint_get();
}
#if defined(__linux__)
#include <sched.h>
#include <signal.h>
#include <sys/syscall.h>
#ifndef SYS_close_range
#define SYS_close_range 436 /* the generic syscall table: x86_64 and aarch64 */
#endif
/* Detached teardown (`scriptc build --detached-exit`): the kernel frees an
* exiting process's memory on its critical path, 30-40 ms per GB of 4 KiB
* pages, before its parent sees it exit. A helper process that shares the
* address space (CLONE_VM) and holds no descriptor but the read end of a
* pipe keeps the memory alive past the exit, then exits itself and frees it
* off the parent's critical path (the latency mold's --fork hides). The
* kernel releases an exiting process's memory (exit_mm) before its files
* (exit_files), so EOF on the pipe means the helper holds the last
* reference. The helper keeps the executable mapped and the memory charged
* to the cgroup until then, so a program opts in at build time: the program
* object then defines scr_detached_exit = 1, overriding this weak default. */
__attribute__((weak)) const unsigned char scr_detached_exit = 0;
static char scr_teardown_stack[16384] __attribute__((aligned(64)));
static int scr_teardown_main(void *arg) {
int fd = (int)(intptr_t)arg;
/* Raw syscalls only: the helper runs on the exiting thread's TLS. */
if (fd > 0) (void)syscall(SYS_close_range, 0u, (unsigned)fd - 1u, 0u);
(void)syscall(SYS_close_range, (unsigned)fd + 1u, ~0u, 0u);
char c;
while (syscall(SYS_read, fd, &c, 1) != 0) {
}
syscall(SYS_exit, 0);
return 0;
}
static void scr_detach_teardown(void) {
if (!scr_detached_exit) return;
int fds[2];
if (pipe2(fds, O_CLOEXEC) != 0) return;
sigset_t all, old;
sigfillset(&all);
pthread_sigmask(SIG_SETMASK, &all, &old);
pid_t pid = clone(scr_teardown_main, scr_teardown_stack + sizeof scr_teardown_stack, CLONE_VM | SIGCHLD,
(void *)(intptr_t)fds[0]);
pthread_sigmask(SIG_SETMASK, &old, NULL);
(void)close(fds[0]);
if (pid < 0) (void)close(fds[1]);
/* fds[1] stays open until the process's files are released. */
}
#endif
void scr_process_exit(double code) {
/* Node runs 'exit' listeners on explicit process.exit() too — they run
* HERE, synchronously, before the teardown-free exit below. The hook is
@@ -1823,6 +1979,9 @@ void scr_process_exit(double code) {
* Node), and the RC audit is meaningless mid-program (live values are
* expected). scr_init's flush-at-exit is also skipped — flush here. */
fflush(stdout);
#if defined(__linux__)
scr_detach_teardown();
#endif
_Exit((int)code);
}
+5
View File
@@ -3154,6 +3154,10 @@ bool scr_process_kill_named(double pid, const ScrStr *signal);
/* fflush stdout, then _Exit((int)code): no atexit handlers run — the RC
* audit is deliberately skipped (exiting mid-program leaves live values). */
void scr_process_exit(double code);
/* Nonzero hands the address-space teardown after scr_process_exit to a
* helper process (Linux, `--detached-exit`). scr_lib.c defines a weak 0;
* the program object of such an executable defines it as 1. */
extern const unsigned char scr_detached_exit;
void scr_process_set_exit_code(double code);
double scr_process_exit_code_or_zero(void);
/* process._exiting: true once the exit sequence began (process.exit or
@@ -4085,6 +4089,7 @@ ScrStr *scr_os_tmpdir(void);
ScrStr *scr_os_release(void); /* uname(2) release — +1 fresh */
ScrStr *scr_os_type(void); /* uname(2) sysname — +1 fresh */
double scr_os_totalmem(void); /* total physical memory, bytes */
double scr_os_available_parallelism(void); /* uv_available_parallelism: CPUs this process may use, >= 1 */
/* os.userInfo()'s field trio (pw_name / pw_shell / pw_dir — the passwd
* home, not the $HOME cascade). +1 fresh; abort on lookup failure. */
ScrStr *scr_os_user_name(void);
@@ -0,0 +1,13 @@
// os.availableParallelism() — differential against the host's own Node:
// libuv's uv_available_parallelism (the CPU affinity mask, then a smaller
// cgroup CPU quota on Linux; hw.activecpu on Darwin), so both processes
// see the same count.
import * as os from "node:os";
import { availableParallelism } from "node:os";
const n = os.availableParallelism();
console.log(typeof n, Number.isInteger(n), n >= 1);
console.log(n === availableParallelism());
console.log(n);
// A thread count derived from it, like a parallel tool's default.
console.log(Math.max(1, Math.min(n, 64)) === n || n > 64);