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
45 changes: 43 additions & 2 deletions packages/platform-apple/src/core/__tests__/perf-target.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { promises as fs } from 'node:fs';
import { IOS_DEVICE } from '../../__tests__/device-fixtures.ts';
import { parseApplePsOutput, resolveIosDevicePerfTarget } from '../perf-target.ts';
import { IOS_DEVICE, MACOS_DEVICE } from '../../__tests__/device-fixtures.ts';
import {
parseApplePsOutput,
readAppleProcessSamples,
resolveIosDevicePerfTarget,
} from '../perf-target.ts';
import { createLocalAppleToolProvider, withAppleToolProvider } from '../tool-provider.ts';

test('parseApplePsOutput reads pid cpu rss and command columns', () => {
Expand All @@ -28,6 +32,43 @@ test('parseApplePsOutput reads pid cpu rss and command columns', () => {
]);
});

test('parseApplePsOutput accepts a comma decimal separator', () => {
const rows = parseApplePsOutput('123 12,5 45678 /Applications/Test.app/Contents/MacOS/Test');

assert.deepEqual(rows, [
{
pid: 123,
cpuPercent: 12.5,
rssKb: 45678,
command: '/Applications/Test.app/Contents/MacOS/Test',
},
]);
});

test('readAppleProcessSamples pins the C locale for the macOS ps read', async () => {
let psEnv: NodeJS.ProcessEnv | undefined;
const provider = createLocalAppleToolProvider({
runCommand: async (cmd, _args, options) => {
if (cmd === 'ps') psEnv = options?.envOverrides;
return {
exitCode: 0,
stdout: '123 3.5 2048 /Applications/Test.app/Contents/MacOS/Test\n',
stderr: '',
};
},
});

const samples = await withAppleToolProvider(provider, () =>
readAppleProcessSamples(MACOS_DEVICE, {
executableName: 'Test',
executablePath: '/Applications/Test.app/Contents/MacOS/Test',
}),
);

assert.equal(psEnv?.LC_ALL, 'C.UTF-8');
assert.equal(samples.length, 1);
});

test('resolveIosDevicePerfTarget skips nested extensions and other installed copies', async () => {
const containers = 'file:///private/var/containers/Bundle/Application';
const appUrl = `${containers}/DEV/App.app/`;
Expand Down
10 changes: 8 additions & 2 deletions packages/platform-apple/src/core/perf-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { runAppleToolCommand, runXcrun, type ScopedSimctlCommand } from './tool-

const APPLE_PERF_TIMEOUT_MS = 15_000;

// BSD ps formats %cpu with the user's LC_NUMERIC (`12,5` under de_DE); pin a C locale that
// keeps argv as UTF-8 text, and still accept a comma from reads that cannot be pinned.
const PS_LOCALE_ENV: NodeJS.ProcessEnv = { LC_ALL: 'C.UTF-8' };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: C.UTF-8 is not a locale provided by macOS, so this override does not establish the UTF-8 character locale it claims. BSD ps can therefore render non-ASCII argv in C-locale escapes; matchesAppleExecutableProcess compares that output with the raw executable path, so perf can miss apps whose executable path contains non-ASCII characters. Remove LC_ALL and pin the valid macOS categories instead: LC_NUMERIC=C and LC_CTYPE=UTF-8.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/platform-apple/src/core/perf-target.ts, line 17:

<comment>`C.UTF-8` is not a locale provided by macOS, so this override does not establish the UTF-8 character locale it claims. BSD `ps` can therefore render non-ASCII argv in C-locale escapes; `matchesAppleExecutableProcess` compares that output with the raw executable path, so perf can miss apps whose executable path contains non-ASCII characters. Remove `LC_ALL` and pin the valid macOS categories instead: `LC_NUMERIC=C` and `LC_CTYPE=UTF-8`.</comment>

<file context>
@@ -12,6 +12,10 @@ import { runAppleToolCommand, runXcrun, type ScopedSimctlCommand } from './tool-
 
+// BSD ps formats %cpu with the user's LC_NUMERIC (`12,5` under de_DE); pin a C locale that
+// keeps argv as UTF-8 text, and still accept a comma from reads that cannot be pinned.
+const PS_LOCALE_ENV: NodeJS.ProcessEnv = { LC_ALL: 'C.UTF-8' };
+
 export type AppleProcessSample = {
</file context>
Suggested change
const PS_LOCALE_ENV: NodeJS.ProcessEnv = { LC_ALL: 'C.UTF-8' };
const PS_LOCALE_ENV: NodeJS.ProcessEnv = { LC_ALL: undefined, LC_NUMERIC: 'C', LC_CTYPE: 'UTF-8' };


export type AppleProcessSample = {
pid: number;
cpuPercent: number;
Expand All @@ -22,7 +26,7 @@ export type AppleProcessSample = {
export function parseApplePsOutput(stdout: string): AppleProcessSample[] {
const rows: AppleProcessSample[] = [];
for (const line of splitNonEmptyTrimmedLines(stdout)) {
const match = line.match(/^(\d+)\s+([0-9]+(?:\.[0-9]+)?)\s+(\d+)\s+(.+)$/);
const match = line.match(/^(\d+)\s+([0-9]+(?:[.,][0-9]+)?)\s+(\d+)\s+(.+)$/);
if (!match) continue;
const [pidText, cpuText, rssText, commandText] = match.slice(1);
if (
Expand All @@ -34,7 +38,7 @@ export function parseApplePsOutput(stdout: string): AppleProcessSample[] {
continue;
}
const pid = Number(pidText);
const cpuPercent = Number(cpuText);
const cpuPercent = Number(cpuText.replace(',', '.'));
const rssKb = Number(rssText);
const command = commandText.trim();
if (!Number.isFinite(pid) || !Number.isFinite(cpuPercent) || !Number.isFinite(rssKb)) {
Expand Down Expand Up @@ -155,6 +159,7 @@ export async function readAppleProcessSamples(
const result = isMacOs(device)
? await runAppleToolCommand('ps', ['-axo', 'pid=,%cpu=,rss=,command='], {
timeoutMs: APPLE_PERF_TIMEOUT_MS,
envOverrides: PS_LOCALE_ENV,
})
: await runAppleSimulatorProcessCommand(
buildSimctlArgsForDevice(device, [
Expand All @@ -179,5 +184,6 @@ async function runAppleSimulatorProcessCommand(args: ScopedSimctlCommand): Promi
if (result.exitCode === 0) return result;
return await runAppleToolCommand('ps', ['-axo', 'pid=,%cpu=,rss=,command='], {
timeoutMs: APPLE_PERF_TIMEOUT_MS,
envOverrides: PS_LOCALE_ENV,
});
}
Loading