Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,12 @@ export function formatStatus(status: StatusGetOutput, now: number, cliVersion?:
].join("\n");
}
const globalLine = `Running global: ${capacity.global.running} + ${capacity.global.reserved} reserved/${capacity.global.maxRunning}, warm ${capacity.global.warm}${capacity.global.overLimit ? " (over limit)" : ""}`;
const capacityLines = (["ios", "android"] as const).map((platform) => {
const usage = capacity[platform];
return `Capacity ${platform}: managed ${usage.used}/${usage.limit}, running ${usage.running} + ${usage.reserved} reserved/${usage.maxRunning}, warm ${usage.warm}${usage.overLimit ? " (over limit)" : ""}${usage.atRamBudget ? " (at RAM budget)" : ""}`;
});
const capacityLines = PLATFORMS.filter((platform) => !absentPlatform(capacity[platform])).map(
(platform) => {
const usage = capacity[platform];
return `Capacity ${platform}: managed ${usage.used}/${usage.limit}, running ${usage.running} + ${usage.reserved} reserved/${usage.maxRunning}, warm ${usage.warm}${usage.overLimit ? " (over limit)" : ""}${usage.atRamBudget ? " (at RAM budget)" : ""}`;
},
);
const ramBudget = capacity.ramBudget;
const ramLines =
ramBudget === undefined
Expand All @@ -299,7 +301,7 @@ export function formatStatus(status: StatusGetOutput, now: number, cliVersion?:
const warmPoolLines =
status.warmPool === undefined || daemon.mode === "gateway"
? []
: formatWarmPool(status.warmPool);
: formatWarmPool(status.warmPool, capacity);
const deviceLines = devices.map((device) => {
const markers = [
device.foreignStateDetectedAt === undefined ? undefined : "foreign state change",
Expand Down Expand Up @@ -351,10 +353,23 @@ function deviceKind(device: NonNullable<StatusGetOutput["devices"]>[number]): st
return `mode ${esc(String(device.mode))}, serves default mode: ${device.servesDefaultMode ? "yes" : "no"}${baseline}`;
}

function formatWarmPool(warmPool: NonNullable<StatusGetOutput["warmPool"]>): string[] {
const PLATFORMS = ["ios", "android"] as const;

/** why: a worker with no driver for a platform reports 0 devices and 0 running slots for it (#1689); it has no line. */
function absentPlatform(usage: { readonly limit: number; readonly maxRunning: number }): boolean {
return usage.limit === 0 && usage.maxRunning === 0;
}

function formatWarmPool(
warmPool: NonNullable<StatusGetOutput["warmPool"]>,
capacity: NonNullable<StatusGetOutput["capacity"]>,
): string[] {
const { enabled, reserveRunning, targets } = warmPool;
const reserve = PLATFORMS.filter((platform) => !absentPlatform(capacity[platform]))
.map((platform) => `${platform} ${reserveRunning[platform]}`)
.join(" ");
return [
`warm pool: ${enabled ? "enabled" : "disabled"}, reserve ios ${reserveRunning.ios} android ${reserveRunning.android}`,
`warm pool: ${enabled ? "enabled" : "disabled"}${reserve === "" ? "" : `, reserve ${reserve}`}`,
...targets.map((target) => {
const name = esc(
[
Expand Down
52 changes: 52 additions & 0 deletions src/cli/index-status-warm-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,55 @@ describe("CLI text output escapes control characters in remote text", () => {
},
);
});

describe("CLI: status of a worker with no iOS slots (#1689)", () => {
it.each([
[
"iOS",
{ ios: { limit: 0, maxRunning: 0 } },
"Capacity android: managed 0/1, running 0 + 0 reserved/1, warm 0\n" +
"warm pool: enabled, reserve android 1\n",
],
[
"both platforms",
{ android: { limit: 0, maxRunning: 0 }, ios: { limit: 0, maxRunning: 0 } },
"warm pool: enabled\n",
],
])(
"simlock status prints no capacity line and no warm reserve for a platform a worker reports 0 devices and 0 running slots for: %s",
async (_name, absent, lines) => {
const output = outputCapture();
const status = {
...EMPTY_STATUS,
capacity: {
...EMPTY_STATUS.capacity,
global: { ...EMPTY_STATUS.capacity.global, maxRunning: 1 },
ios: { ...EMPTY_STATUS.capacity.ios, ...absent.ios },
android: {
...EMPTY_STATUS.capacity.android,
...("android" in absent ? absent.android : {}),
},
},
host: { arch: "x64", os: "Windows", osVersion: "11", tools: [] },
warmPool: { enabled: true, reserveRunning: { android: 1, ios: 0 }, targets: [] },
} satisfies StatusGetOutput;

await runCli(
["status"],
output.environmentWith({
connectAdmin: async () => fakeClient({ getStatus: () => Promise.resolve(status) }),
}),
);

expect(output.stdout).toBe(
"Daemon: running (worker)\n" +
"Simlock: 0.0.0-test\n" +
`CLI: ${PACKAGE_VERSION}\n` +
"Host: Windows 11 x64\n" +
"Running global: 0 + 0 reserved/1, warm 0\n" +
lines +
"Queue depth: 0\n",
);
},
);
});
1 change: 1 addition & 0 deletions src/core/capacity/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { CapacityCoordinator } from "./coordinator.js";
export type { CapacityReservation } from "./coordinator.js";
export { buildCapacityFigures } from "./figures.js";
export { servingOnly } from "./serving-only.js";
export { CapacityObserver, capacityChangedPayload } from "./observer.js";
export { capacityDevice, capacityDevices, plannedCapacityDevice } from "./devices.js";
export type { CapacityLimits } from "./limits.js";
Expand Down
73 changes: 73 additions & 0 deletions src/core/capacity/serving-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type {
CapacityDecision,
CapacityDevice,
CapacityPlatform,
CapacityStrategy,
PlannedCapacityDevice,
RunningCapacity,
} from "./strategy.js";

const PLATFORMS: readonly CapacityPlatform[] = ["ios", "android"];

/**
* why: a platform with no driver on this host (iOS on Windows or Linux) can never run a device, so
* it gets no device or running slot and the global running limit counts only the platforms that can
* (#1689). A platform whose driver was refused still serves: its devices hold slots.
*/
export function servingOnly(
strategy: CapacityStrategy,
serves: (platform: CapacityPlatform) => boolean,
): CapacityStrategy {
const unserved = PLATFORMS.filter((platform) => !serves(platform));
if (unserved.length === 0) return strategy;
const refused = (platform: CapacityPlatform): boolean => unserved.includes(platform);

const runningCapacity = (
devices: readonly CapacityDevice[],
reservations: readonly CapacityPlatform[],
): RunningCapacity => {
const inner = strategy.runningCapacity(devices, reservations);
const entry = (platform: CapacityPlatform) =>
refused(platform)
? {
...inner[platform],
maxRunning: 0,
overLimit: inner[platform].running + inner[platform].reserved > 0,
}
: inner[platform];
const ios = entry("ios");
const android = entry("android");
const maxRunning = Math.min(inner.global.maxRunning, ios.maxRunning + android.maxRunning);
return {
android,
global: {
...inner.global,
maxRunning,
overLimit: inner.global.running + inner.global.reserved > maxRunning,
},
ios,
};
};

return {
canBoot: (device, devices) =>
refused(device.platform)
? { ok: false, reason: "device-limit" }
: strategy.canBoot(device, devices),
canProvision: (device: PlannedCapacityDevice, devices) =>
refused(device.platform)
? { ok: false, reason: "device-limit" }
: strategy.canProvision(device, devices),
canReserveRunning: (platform, devices, reservations): CapacityDecision => {
if (refused(platform)) return { ok: false, reason: "platform-running-limit" };
const { global } = runningCapacity(devices, reservations);
if (global.running + global.reserved >= global.maxRunning) {
return { ok: false, reason: "global-running-limit" };
}
return strategy.canReserveRunning(platform, devices, reservations);
},
deviceLimit: (platform) => (refused(platform) ? 0 : strategy.deviceLimit(platform)),
ramBudget: (devices) => strategy.ramBudget(devices),
runningCapacity,
};
}
34 changes: 33 additions & 1 deletion src/core/create-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MemoryLogSink,
} from "../ports/index.js";
import {
buildCapacityFigures,
type Config,
type ConfigOverrides,
type Core,
Expand All @@ -35,6 +36,8 @@ async function build(
readonly overrides?: ConfigOverrides;
readonly sink?: MemoryLogSink;
readonly withAndroid?: boolean;
/** why: a host with no iOS driver at all, as a Windows or Linux worker (#1689). */
readonly androidOnly?: boolean;
} = {},
) {
const clock = new FakeClock(1_000);
Expand All @@ -49,7 +52,12 @@ async function build(
platform: "ios",
});
const androidDriver = new FakeDriver({ clock, platform: "android" });
const drivers = options.withAndroid === true ? [driver, androidDriver] : [driver];
const drivers =
options.androidOnly === true
? [androidDriver]
: options.withAndroid === true
? [driver, androidDriver]
: [driver];
const filesystem = new MemoryFilesystem();
const systemStats = new FakeSystemStats({ cpuCount: 8, totalRamBytes: 32 * 1024 ** 3 });
let next = 1;
Expand Down Expand Up @@ -622,3 +630,27 @@ describe("createCore", () => {
});
});
});

describe("createCore on a host with no iOS driver (#1689)", () => {
it("reports no iOS device or running slot, no iOS warm reserve, and a global running limit of the Android slots alone", async () => {
// why: the default resource limits on 8 CPUs and 32 GiB are 4 iOS and 2 Android, 6 in all.
const { core, registry } = await build({
androidOnly: true,
overrides: { warmPool: { reserveRunning: { android: 1, ios: 1 } } },
});

const figures = buildCapacityFigures(registry.snapshot.virtualDevices, core.capacityReader);

expect({
ios: { limit: figures.ios.limit, maxRunning: figures.ios.maxRunning },
android: { limit: figures.android.limit, maxRunning: figures.android.maxRunning },
global: { maxRunning: figures.global.maxRunning, overLimit: figures.global.overLimit },
reserveRunning: core.warmPoolReader.figures().reserveRunning,
}).toStrictEqual({
ios: { limit: 0, maxRunning: 0 },
android: { limit: 2, maxRunning: 2 },
global: { maxRunning: 2, overLimit: false },
reserveRunning: { android: 1, ios: 0 },
});
});
});
8 changes: 7 additions & 1 deletion src/core/create-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { StateDatabase } from "../state/index.js";
import {
buildCapacityFigures,
CapacityCoordinator,
servingOnly,
capacityDevices,
CapacityObserver,
createCapacityStrategy,
Expand Down Expand Up @@ -194,7 +195,12 @@ export function createCore(options: CoreOptions): Core {
};

const capacity = new CapacityCoordinator(
createCapacityStrategy(options.config.capacity, options.systemStats),
servingOnly(
createCapacityStrategy(options.config.capacity, options.systemStats),
(platform) =>
options.drivers.some((driver) => driver.platform === platform) ||
(options.driverRejections ?? []).some((rejection) => rejection.platform === platform),
),
() => capacityObserver.changed(),
memoryPressure,
);
Expand Down
20 changes: 19 additions & 1 deletion src/core/doctor-warm-pool-targets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe("Doctor: warm pool targets", () => {
readonly figures?: ReturnType<typeof figuresOf>;
readonly driver?: Omit<ConstructorParameters<typeof FakeDriver>[0], "clock">;
readonly virtualDevices?: Partial<Record<"ios" | "android", boolean>>;
readonly platforms?: readonly ("ios" | "android")[];
}): Promise<DoctorFinding[]> {
const clock = new FakeClock(10_000);
const eventBus = new EventBus(clock);
Expand All @@ -70,7 +71,9 @@ describe("Doctor: warm pool targets", () => {
targets: options.targets ?? [],
},
},
drivers: [new FakeDriver({ clock, platform: "ios", ...options.driver })],
drivers: (options.platforms ?? ["ios", "android"]).map(
(platform) => new FakeDriver({ clock, platform, ...options.driver }),
),
eventBus,
registry,
warmPool: {
Expand Down Expand Up @@ -206,6 +209,21 @@ describe("Doctor: warm pool targets", () => {
expect(await ask(false)).toStrictEqual([]);
});

it("leaves the reserveRunning of a platform with no driver on this host out of the room the targets are checked against", async () => {
const ask = (platforms: readonly ("ios" | "android")[]) =>
findings({
limit: { global: 2 },
platforms,
reserve: { android: 2, ios: 0 },
targets: [{ count: 2, model: "iPhone 17", platform: "ios" }],
});

expect(await ask(["ios", "android"])).toStrictEqual([
expect.objectContaining({ reason: "over-limit", target: "all targets" }),
]);
expect(await ask(["ios"])).toStrictEqual([]);
});

it("reports a target short with unknown-model as unreachable", async () => {
const found = await findings({
figures: figuresOf([{ model: "iPhone 99", osVersion: "26.0", short: "unknown-model" }]),
Expand Down
4 changes: 3 additions & 1 deletion src/core/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,9 @@ export class Doctor {
{
reserveRunning: reserveOnVirtualPlatforms(
this.options.config.warmPool.reserveRunning,
(platform) => (platform === "ios" ? ios : android).virtualDevices,
(platform) =>
(platform === "ios" ? ios : android).virtualDevices &&
this.options.drivers.some((driver) => driver.platform === platform),
),
},
limits,
Expand Down
16 changes: 12 additions & 4 deletions src/core/warm-pool/converger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,23 @@ export class WarmPool {
return this.#reports;
}

/** why: a platform with virtual devices off or no driver on this host holds no running slot for idle devices (ADR 0084 §2, #1689). */
#reserveOnServedPlatforms(): { readonly ios: number; readonly android: number } {
return reserveOnVirtualPlatforms(
this.options.config.reserveRunning,
(platform) =>
this.options.catalog.virtualDevices(platform) && this.options.hasDriver(platform),
);
}

/**
* What `status`, `doctor` and the console read: the pool as the last pass left it. With the
* pool off, or before a pass has run, every configured target as the operator wrote it: short
* `disabled` with the pool off, and with no reason before the first pass.
*/
figures(): WarmPoolFigures {
const { enabled, reserveRunning, targets } = this.options.config;
const { enabled, targets } = this.options.config;
const reserveRunning = this.#reserveOnServedPlatforms();
const reports =
enabled && this.#passed
? this.#figured
Expand Down Expand Up @@ -663,9 +673,7 @@ export class WarmPool {
config: {
enabled: this.options.config.enabled,
maxConcurrentBoots: this.options.config.maxConcurrentBoots,
reserveRunning: reserveOnVirtualPlatforms(this.options.config.reserveRunning, (platform) =>
this.options.catalog.virtualDevices(platform),
),
reserveRunning: this.#reserveOnServedPlatforms(),
shutdownAfterMs: this.options.idle.shutdownAfterMs,
},
devices,
Expand Down
8 changes: 7 additions & 1 deletion src/daemon/dispatcher-leases-capacity.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, expect, it } from "vitest";
import { type Config } from "../core/index.js";
import { FakeDriver } from "../core/testing.js";
import { FakeClock } from "../ports/index.js";
import { classifyError } from "./error-code.js";
import { HOST_SYSTEM, buildDispatcher, flush, gibibyte, session } from "./test-dispatcher.js";

Expand Down Expand Up @@ -47,7 +49,11 @@ describe("Dispatcher: a noWait lease.request refused for capacity names the chec
holds: true,
},
] as const)("rejects NO_CAPACITY with details.capacityReason $reason at $name", async (row) => {
const { dispatcher } = await buildDispatcher({ capacity: row.capacity });
// why: a host serves a platform only with a driver for it (#1689), so the global limit above the iOS one needs an Android driver.
const { dispatcher } = await buildDispatcher({
capacity: row.capacity,
extraDrivers: [new FakeDriver({ clock: new FakeClock(1_000), platform: "android" })],
});
if (row.holds) await dispatcher.dispatch("lease.request", ios, holder());

await expect(
Expand Down
2 changes: 2 additions & 0 deletions src/daemon/dispatcher-leases-device-mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ describe("Dispatcher: device mode on every surface", () => {

it("status.get on a daemon whose pool is off carries enabled false, the reserve and every configured target with ready 0, booting 0 and short disabled", async () => {
const { dispatcher } = await buildDispatcher({
// why: a platform without a driver on the host reserves nothing (#1689), and the reserve below names Android.
extraDrivers: [new FakeDriver({ clock: new FakeClock(1_000), platform: "android" })],
warmPool: {
enabled: false,
reserveRunning: { android: 1, ios: 0 },
Expand Down
1 change: 1 addition & 0 deletions src/daemon/dispatcher-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ describe("Dispatcher: status.get RAM budget", () => {
describe("atRamBudget", () => {
async function withIosDevices(count: number) {
const built = await buildDispatcher({
extraDrivers: [new FakeDriver({ clock: new FakeClock(1_000), platform: "android" })],
capacity: {
strategy: "resource",
config: {
Expand Down
Loading
Loading