Skip to content

Commit 709524a

Browse files
thymikeeclaude
andcommitted
refactor(platform-apple): tighten the xctrace record retry loop and shared process-name reader
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 7877b40 commit 709524a

3 files changed

Lines changed: 43 additions & 44 deletions

File tree

‎packages/platform-apple/src/core/perf-xctrace.ts‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export type AppleXctracePerfMode = 'cpu-profile' | 'trace';
5454

5555
type AppleXctraceRecordTarget = number[] | 'all-processes';
5656

57-
type AppleXctraceRecordAttempt<T> = { started: T } | { failure: ExecResult };
57+
type AppleXctraceRecordAttempt<T> = { recorded: T } | { failure: ExecResult };
5858

5959
export type AppleXctraceTimedRecord = {
6060
startedAt: string;
@@ -135,9 +135,9 @@ export async function startAppleXctracePerfCapture(params: {
135135
failureMessage: `Failed to start Apple xctrace ${params.mode} capture for ${params.appBundleId}`,
136136
},
137137
async (): Promise<AppleXctraceRecordAttempt<ExecBackgroundResult>> => {
138-
const started = runCmdBackground('xcrun', args, { allowFailure: true });
139-
const immediate = await waitForImmediateAppleXctraceExit(started.wait);
140-
return immediate ? { failure: immediate } : { started };
138+
const recorded = runCmdBackground('xcrun', args, { allowFailure: true });
139+
const immediate = await waitForImmediateAppleXctraceExit(recorded.wait);
140+
return immediate ? { failure: immediate } : { recorded };
141141
},
142142
);
143143
return {
@@ -282,7 +282,12 @@ export async function recordAppleXctraceTimedTrace(params: {
282282
});
283283
if (result.exitCode !== 0) return { failure: result };
284284
return {
285-
started: { result, startedAt, endedAt: new Date().toISOString(), capturedAtMs: Date.now() },
285+
recorded: {
286+
result,
287+
startedAt,
288+
endedAt: new Date().toISOString(),
289+
capturedAtMs: Date.now(),
290+
},
286291
};
287292
},
288293
);
@@ -402,25 +407,29 @@ async function recordAppleXctraceWithRetry<T>(
402407
},
403408
attemptRecord: () => Promise<AppleXctraceRecordAttempt<T>>,
404409
): Promise<T> {
405-
let failure: ExecResult = { stdout: '', stderr: '', exitCode: 1 };
406-
for (let attempt = 1; attempt <= IOS_DEVICE_TRACE_RECORD_MAX_ATTEMPTS; attempt += 1) {
410+
for (let attempt = 1; ; attempt += 1) {
407411
await prepareAppleTraceRecordRetry(tracePath, attempt);
408412
const outcome = await attemptRecord();
409-
if ('started' in outcome) return outcome.started;
410-
failure = outcome.failure;
411-
if (!isRetryableIosDeviceTraceRecordFailure(failure)) break;
413+
if ('recorded' in outcome) return outcome.recorded;
414+
if (
415+
attempt < IOS_DEVICE_TRACE_RECORD_MAX_ATTEMPTS &&
416+
isRetryableIosDeviceTraceRecordFailure(outcome.failure)
417+
) {
418+
continue;
419+
}
420+
const { failure } = outcome;
421+
throw new AppError(
422+
'COMMAND_FAILED',
423+
context.failureMessage,
424+
execFailureDetails(failure, {
425+
cmd: 'xcrun',
426+
args,
427+
appBundleId: context.appBundleId,
428+
deviceId: context.device.id,
429+
hint: resolveIosDevicePerfHint(failure.stdout, failure.stderr),
430+
}),
431+
);
412432
}
413-
throw new AppError(
414-
'COMMAND_FAILED',
415-
context.failureMessage,
416-
execFailureDetails(failure, {
417-
cmd: 'xcrun',
418-
args,
419-
appBundleId: context.appBundleId,
420-
deviceId: context.device.id,
421-
hint: resolveIosDevicePerfHint(failure.stdout, failure.stderr),
422-
}),
423-
);
424433
}
425434

426435
export function isRetryableIosDeviceTraceRecordFailure(result: {

‎packages/platform-apple/src/core/perf-xml.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ function readDirectXmlProcess(element: XmlNode | undefined): XmlProcess | null {
9090
if (!element || element.children.some((child) => child.name === 'sentinel')) return null;
9191
const pidNode = findFirstXmlNode(element.children, (child) => child.name === 'pid');
9292
const pid = parseDirectXmlNumber(pidNode);
93-
const name = (element.attributes.fmt ?? '').replace(/\s+\(\d+\)$/, '').trim();
93+
const name = (element.attributes.fmt ?? '')
94+
.trim()
95+
.replace(/\s+\(\d+\)$/, '')
96+
.trim();
9497
if (pid === null && name.length === 0) return null;
9598
return {
9699
pid: pid ?? undefined,

‎packages/platform-apple/src/core/perf.ts‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -413,20 +413,13 @@ async function captureIosDeviceFramePerf(
413413
requireTraceData: true,
414414
failureMessage: `Failed to record iOS frame-health sample for ${appBundleId}`,
415415
});
416-
const exportTable = async (schema: string, fileName: string) =>
417-
await exportIosDevicePerfTable(
418-
device,
419-
appBundleId,
420-
tracePath,
421-
schema,
422-
path.join(tempDir, fileName),
423-
);
416+
const context = { device, appBundleId, tracePath, tempDir };
424417
return {
425418
windowStartedAt: record.startedAt,
426419
windowEndedAt: record.endedAt,
427-
hitchesXml: await exportTable('hitches', 'hitches.xml'),
428-
frameLifetimesXml: await exportTable('hitches-frame-lifetimes', 'frame-lifetimes.xml'),
429-
displayInfoXml: await exportTable('device-display-info', 'display-info.xml').catch(
420+
hitchesXml: await exportIosDevicePerfTable(context, 'hitches'),
421+
frameLifetimesXml: await exportIosDevicePerfTable(context, 'hitches-frame-lifetimes'),
422+
displayInfoXml: await exportIosDevicePerfTable(context, 'device-display-info').catch(
430423
() => undefined,
431424
),
432425
};
@@ -436,18 +429,15 @@ async function captureIosDeviceFramePerf(
436429
}
437430

438431
async function exportIosDevicePerfTable(
439-
device: DeviceInfo,
440-
appBundleId: string,
441-
tracePath: string,
432+
context: { device: DeviceInfo; appBundleId: string; tracePath: string; tempDir: string },
442433
schema: string,
443-
outPath: string,
444434
): Promise<string> {
445435
return await exportAppleXctraceData({
446-
tracePath,
447-
outPath,
436+
tracePath: context.tracePath,
437+
outPath: path.join(context.tempDir, `${schema}.xml`),
448438
query: { schema },
449439
failureMessage: `Failed to export iOS device ${schema} data`,
450-
failureDetails: { appBundleId, deviceId: device.id },
440+
failureDetails: { appBundleId: context.appBundleId, deviceId: context.device.id },
451441
});
452442
}
453443

@@ -537,11 +527,8 @@ async function captureIosDevicePerfTable(
537527
return {
538528
capturedAtMs: record.capturedAtMs,
539529
xml: await exportIosDevicePerfTable(
540-
device,
541-
appBundleId,
542-
tracePath,
530+
{ device, appBundleId, tracePath, tempDir },
543531
'activity-monitor-process-live',
544-
path.join(tempDir, 'activity-monitor-process-live.xml'),
545532
),
546533
};
547534
} finally {

0 commit comments

Comments
 (0)