Skip to content

Commit

Permalink
fix(profiler): handle multiple processes with same name
Browse files Browse the repository at this point in the history
This is similar to what Android Studio displays

Say you have an app loading several images, chances are the app will create a different process to load each images but with the same name
For instance, let's say this launches 5 different processes each accounting for 20% CPU usage in the same 500ms window

Before this fix, the stats would be additionned, meaning we would see one single thread with that name with 100% CPU usage. It would thus hit the high CPU usage threshold, even though, nothing is actually saturating

After this fix you would see
process 20%
process #2 20%
...
process #5 20%

Worth noting that a more ideal fix would have been to store processes by ids, but that actually makes the list of processes less readable and more difficult to compare
Say every 500ms you load 5 requests, your 5 request processes would get recreated with same name but different ids, so every 500ms, 5 new processes would get added to the list. By still grouping by name, but allowing for multiple occurrences in a 500ms window, we keep only a smaller amount of processes, like Android Studio does for instance.
It's also easier to compare and track with multiple iterations
  • Loading branch information
Almouro committed Jul 27, 2023
1 parent 08fcc0b commit fe47958
Showing 1 changed file with 11 additions and 2 deletions.
Expand Up @@ -7,8 +7,10 @@ export interface ProcessStat {
cpuNumber: string;
}

export const processOutput = (output: string, pid: string): ProcessStat[] =>
output
export const processOutput = (output: string, pid: string): ProcessStat[] => {
const processNames: { [name: string]: number } = {};

return output
.split(/\r\n|\n|\r/)
.filter(Boolean)
.map((stats) => {
Expand All @@ -31,6 +33,12 @@ export const processOutput = (output: string, pid: string): ProcessStat[] =>
processName = processName.replace(`Binder:${pid}_`, "Binder #");
}

processNames[processName] = (processNames[processName] || 0) + 1;

if (processNames[processName] > 1) {
processName = `${processName} #${processNames[processName]}`;
}

const utime = parseInt(subProcessStats[13], 10);
const stime = parseInt(subProcessStats[14], 10);

Expand All @@ -40,3 +48,4 @@ export const processOutput = (output: string, pid: string): ProcessStat[] =>

return { processId, processName, totalCpuTime, cpuNumber };
});
};

0 comments on commit fe47958

Please sign in to comment.