From 2c5edd8a4cb8b598c5cbd8a959f2d6870d5922cc Mon Sep 17 00:00:00 2001 From: Alexandre Moureaux Date: Fri, 16 Dec 2022 16:54:00 +0100 Subject: [PATCH] fix(profiler): make FPS polling safer (#62) Should fix https://github.com/bamlab/android-performance-profiler/issues/48 --- .../commands/atrace/__tests__/pollFpsUsage.ts | 1 + .../src/commands/atrace/pollFpsUsage.ts | 52 +++++++++++++------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/packages/android-performance-profiler/src/commands/atrace/__tests__/pollFpsUsage.ts b/packages/android-performance-profiler/src/commands/atrace/__tests__/pollFpsUsage.ts index 5cda6223..d011cd91 100644 --- a/packages/android-performance-profiler/src/commands/atrace/__tests__/pollFpsUsage.ts +++ b/packages/android-performance-profiler/src/commands/atrace/__tests__/pollFpsUsage.ts @@ -31,6 +31,7 @@ com.example-4808 (-----) [006] .... 176340.064914: tracing_mark_write: B|4808|d com.example-4808 (-----) [006] .... 176340.064942: tracing_mark_write: B|4808|Record View#draw() com.example-4808 (-----) [006] .... 176340.065726: tracing_mark_write: E|4808 com.example-4808 (-----) [006] .... 176340.067496: tracing_mark_write: E|4808 +com.example-4808 (-----) [005] ..s. 1799.488062: run_rebalance_domains: CPU5 FAILED TO GET (this is one line coming from Samsung A40) com.example-4808 (-----) [006] .... 176340.067503: tracing_mark_write: E|4808 com.example-4808 (-----) [006] .... 176340.067509: tracing_mark_write: E|4808 com.example-4808 (-----) [006] .... 176340.147257: tracing_mark_write: B|4808|Choreographer#onVsync 4573951 diff --git a/packages/android-performance-profiler/src/commands/atrace/pollFpsUsage.ts b/packages/android-performance-profiler/src/commands/atrace/pollFpsUsage.ts index 92a530ca..2554d8d5 100644 --- a/packages/android-performance-profiler/src/commands/atrace/pollFpsUsage.ts +++ b/packages/android-performance-profiler/src/commands/atrace/pollFpsUsage.ts @@ -1,4 +1,12 @@ -export const parseLine = (line: string) => { +import { Logger } from "@perf-profiler/logger"; + +export const parseLine = ( + line: string +): { + timestamp: number; + ending: boolean; + methodName: string | undefined; +} => { let regexMatching = line.match( / (\d+\.\d+): tracing_mark_write: ([A-Z])(.*)/ ); @@ -46,27 +54,37 @@ export class FrameTimeParser { const frameTimes: number[] = []; lines.forEach((line) => { - if (!line.includes("-" + pid + " ")) return; + try { + if (!line.includes("-" + pid + " ")) return; - const { timestamp, ending, methodName } = parseLine(line); + const { timestamp, ending, methodName } = parseLine(line); - if (ending) { - this.methodStartedCount--; - if (this.methodStartedCount <= 0) { - if (this.doFrameStartedTimeStamp) { - frameTimes.push(timestamp - this.doFrameStartedTimeStamp); - this.doFrameStartedTimeStamp = null; - } + if (ending) { + this.methodStartedCount--; + if (this.methodStartedCount <= 0) { + if (this.doFrameStartedTimeStamp) { + frameTimes.push(timestamp - this.doFrameStartedTimeStamp); + this.doFrameStartedTimeStamp = null; + } - this.methodStartedCount = 0; - } - } else { - if (methodName.includes("Choreographer#doFrame")) { - this.methodStartedCount = 1; - this.doFrameStartedTimeStamp = timestamp; + this.methodStartedCount = 0; + } } else { - this.methodStartedCount++; + if (methodName) { + if (methodName.includes("Choreographer#doFrame")) { + this.methodStartedCount = 1; + this.doFrameStartedTimeStamp = timestamp; + } else { + this.methodStartedCount++; + } + } } + } catch (error) { + Logger.error(`Failed to parse Atrace line: +${line} + +Error: +${error instanceof Error ? error.message : error}`); } });