mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 06:21:12 +00:00
perf(native/process): avoid symbol BigInt/string allocs and trim process polls (#13720)
This commit is contained in:
1 parent
91b45c0aee
commit
f92a06226a
7 files changed
+58
-23
No files matched your search
@@ -73,6 +73,17 @@ export const readElfSymbolFieldsAt = (
|
||||
};
|
||||
};
|
||||
|
||||
/** Decode only the `st_value` word of a symbol already bounds-checked by `readElfSymbolFieldsAt`. */
|
||||
export const readElfSymbolValueAt = (
|
||||
bytes: Uint8Array,
|
||||
offset: number,
|
||||
bits: 32 | 64,
|
||||
littleEndian: boolean,
|
||||
): bigint =>
|
||||
bits === 64
|
||||
? readUint64At(bytes, offset + 8, littleEndian)
|
||||
: BigInt(readUint32At(bytes, offset + 4, littleEndian));
|
||||
|
||||
/** Decode one fixed ELF32/ELF64 symbol record without slicing its bytes. */
|
||||
export const readElfSymbolAt = (
|
||||
bytes: Uint8Array,
|
||||
|
||||
@@ -32,7 +32,7 @@ import {
|
||||
import {
|
||||
ELF_SYMBOL_TRUNCATED_MESSAGE,
|
||||
elfSymbolSize,
|
||||
readElfSymbolAt,
|
||||
readElfSymbolFieldsAt,
|
||||
} from "../../domain/elfSymbol.js";
|
||||
import {
|
||||
readElfGnuHashSymbolCount,
|
||||
@@ -1736,7 +1736,7 @@ const parseImports = (
|
||||
: new BinaryReader(versymBytes, reader.little, "elf");
|
||||
for (let index = 1; index < symbolCount; index += 1) {
|
||||
const at = dynsym.offset + index * entrySize;
|
||||
const parsed = readElfSymbolAt(bytes, at, bits, reader.little);
|
||||
const parsed = readElfSymbolFieldsAt(bytes, at, bits, reader.little);
|
||||
if (parsed === null) {
|
||||
issues.push({
|
||||
code: "malformed_entry",
|
||||
|
||||
@@ -12,7 +12,11 @@ import {
|
||||
} from "../../resources/index.js";
|
||||
import { decodeLimit } from "../../domain/parseFailure.js";
|
||||
import { readElfDynamicEntry } from "../../domain/elfDynamicEntry.js";
|
||||
import { elfSymbolSize, readElfSymbolAt } from "../../domain/elfSymbol.js";
|
||||
import {
|
||||
elfSymbolSize,
|
||||
readElfSymbolFieldsAt,
|
||||
readElfSymbolValueAt,
|
||||
} from "../../domain/elfSymbol.js";
|
||||
import {
|
||||
elfStringTableText,
|
||||
observeElfStringTableEntry,
|
||||
@@ -1890,7 +1894,7 @@ const scanSymbols = (
|
||||
for (let index = 0; index < count; index += 1) {
|
||||
assertArtifactReadActive(signal);
|
||||
const at = start + index * width;
|
||||
const symbol = readElfSymbolAt(
|
||||
const symbol = readElfSymbolFieldsAt(
|
||||
bytes,
|
||||
at,
|
||||
directory.bits,
|
||||
@@ -1913,7 +1917,12 @@ const scanSymbols = (
|
||||
continue;
|
||||
if (sectionIndex === SHN_UNDEF) continue;
|
||||
if (sectionIndex >= SHN_LORESERVE && sectionIndex !== SHN_ABS) continue;
|
||||
let address = symbol.value;
|
||||
let address = readElfSymbolValueAt(
|
||||
bytes,
|
||||
at,
|
||||
directory.bits,
|
||||
directory.little,
|
||||
);
|
||||
if (relocatable && sectionIndex !== SHN_ABS) {
|
||||
const owner = byIndex.get(sectionIndex);
|
||||
if (owner !== undefined) address += owner.address;
|
||||
|
||||
@@ -641,7 +641,8 @@ const stackRow = (
|
||||
continue;
|
||||
// A common definition shares those type bits but carries its size in
|
||||
// n_value, so only a zero n_value names an import.
|
||||
if (symbols.word(entry + 8, wide ? 64 : 32) !== 0n) continue;
|
||||
if (symbols.u32(entry + 8) !== 0 || (wide && symbols.u32(entry + 12) !== 0))
|
||||
continue;
|
||||
// After a match, later records are still admitted so symbol order cannot
|
||||
// hide a damaged table; their names need not be read.
|
||||
if (found !== null) {
|
||||
@@ -664,6 +665,12 @@ const stackRow = (
|
||||
unterminated = true;
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
(observed.contentBytes !== 16 && observed.contentBytes !== 17) ||
|
||||
strings[nameOffset] !== 0x5f ||
|
||||
strings[nameOffset + 1] !== 0x5f
|
||||
)
|
||||
continue;
|
||||
const name = elfStringTableText(observed) ?? "";
|
||||
if (name === "__stack_chk_fail" || name === "___stack_chk_fail")
|
||||
found = row(
|
||||
|
||||
@@ -1641,16 +1641,21 @@ const recheckOwnershipFailures = async (
|
||||
try {
|
||||
let processes = await host.listProcesses();
|
||||
const isDarwin = (host.platform ?? HOST_PLATFORM.platform) === "darwin";
|
||||
const retryableFailurePids = isDarwin
|
||||
? new Set(
|
||||
failures
|
||||
.filter((failure) => failure.reason !== "run-token-mismatch")
|
||||
.map((failure) => failure.pid),
|
||||
)
|
||||
: undefined;
|
||||
for (
|
||||
let attempt = 0;
|
||||
isDarwin &&
|
||||
retryableFailurePids !== undefined &&
|
||||
retryableFailurePids.size > 0 &&
|
||||
attempt < TOKEN_READ_RETRY_ATTEMPTS &&
|
||||
processes.some(
|
||||
({ pid, state }) =>
|
||||
failures.some(
|
||||
(failure) =>
|
||||
failure.pid === pid && failure.reason !== "run-token-mismatch",
|
||||
) && state.slice(1).includes("E"),
|
||||
retryableFailurePids.has(pid) && state.slice(1).includes("E"),
|
||||
);
|
||||
attempt += 1
|
||||
) {
|
||||
|
||||
@@ -1609,16 +1609,18 @@ export const createSystemProcessOwnershipHost = (
|
||||
const unresolved = stillLive.filter(
|
||||
({ pid }) => identities.get(pid)?.state !== "readable",
|
||||
);
|
||||
const liveAfterRetryPids = new Set(
|
||||
liveProcesses(await safeListProcesses(signal)).map(({ pid }) => pid),
|
||||
);
|
||||
const stillUnresolved = unresolved.filter(({ pid }) =>
|
||||
liveAfterRetryPids.has(pid),
|
||||
);
|
||||
if (stillUnresolved.length > 0)
|
||||
throw new ProcessAdmissionError("identity_snapshot_unavailable", {
|
||||
live_processes: stillUnresolved.length,
|
||||
});
|
||||
if (unresolved.length > 0) {
|
||||
const liveAfterRetryPids = new Set(
|
||||
liveProcesses(await safeListProcesses(signal)).map(({ pid }) => pid),
|
||||
);
|
||||
const stillUnresolved = unresolved.filter(({ pid }) =>
|
||||
liveAfterRetryPids.has(pid),
|
||||
);
|
||||
if (stillUnresolved.length > 0)
|
||||
throw new ProcessAdmissionError("identity_snapshot_unavailable", {
|
||||
live_processes: stillUnresolved.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
return processes.map(({ pid }) => {
|
||||
const observation = identities.get(pid);
|
||||
|
||||
@@ -810,9 +810,10 @@ export const observeSettlement = async ({
|
||||
consecutiveEmpty = 0;
|
||||
sawLive = true;
|
||||
}
|
||||
deadlineReached = monotonicProcessCaptureClock() - started >= settleMs;
|
||||
const elapsed = monotonicProcessCaptureClock() - started;
|
||||
deadlineReached = elapsed >= settleMs;
|
||||
if (deadlineReached) break;
|
||||
await delay(50, undefined, { signal });
|
||||
await delay(Math.min(50, settleMs - elapsed), undefined, { signal });
|
||||
}
|
||||
recordEvent("lifecycle", 1);
|
||||
// A lone empty observation proves neither liveness nor quiescence; only an
|
||||
|
||||
Reference in new issue
Block a user