Merge "AppProfiler: don't hold lock for CPU times" into udc-dev

This commit is contained in:
TreeHugger Robot
2023-03-02 17:53:05 +00:00
committed by Android (Google) Code Review
2 changed files with 15 additions and 27 deletions

View File

@@ -80,10 +80,6 @@ public class ProcessCpuTracker {
/** Stores user time and system time in jiffies. */ /** Stores user time and system time in jiffies. */
private final long[] mProcessStatsData = new long[4]; private final long[] mProcessStatsData = new long[4];
/** Stores user time and system time in jiffies. Used for
* public API to retrieve CPU use for a process. Must lock while in use. */
private final long[] mSinglePidStatsData = new long[4];
private static final int[] PROCESS_FULL_STATS_FORMAT = new int[] { private static final int[] PROCESS_FULL_STATS_FORMAT = new int[] {
PROC_SPACE_TERM, PROC_SPACE_TERM,
PROC_SPACE_TERM|PROC_PARENS|PROC_OUT_STRING, // 2: name PROC_SPACE_TERM|PROC_PARENS|PROC_OUT_STRING, // 2: name
@@ -629,9 +625,8 @@ public class ProcessCpuTracker {
* executing in both user and system code. Safe to call without lock held. * executing in both user and system code. Safe to call without lock held.
*/ */
public long getCpuTimeForPid(int pid) { public long getCpuTimeForPid(int pid) {
synchronized (mSinglePidStatsData) {
final String statFile = "/proc/" + pid + "/stat"; final String statFile = "/proc/" + pid + "/stat";
final long[] statsData = mSinglePidStatsData; final long[] statsData = new long[4];
if (Process.readProcFile(statFile, PROCESS_STATS_FORMAT, if (Process.readProcFile(statFile, PROCESS_STATS_FORMAT,
null, statsData, null)) { null, statsData, null)) {
long time = statsData[PROCESS_STAT_UTIME] long time = statsData[PROCESS_STAT_UTIME]
@@ -640,23 +635,20 @@ public class ProcessCpuTracker {
} }
return 0; return 0;
} }
}
/** /**
* Returns the total time (in milliseconds) the given PID has spent waiting * Returns the total time (in milliseconds) the given PID has spent waiting
* in the runqueue. Safe to call without lock held. * in the runqueue. Safe to call without lock held.
*/ */
public long getCpuDelayTimeForPid(int pid) { public long getCpuDelayTimeForPid(int pid) {
synchronized (mSinglePidStatsData) {
final String statFile = "/proc/" + pid + "/schedstat"; final String statFile = "/proc/" + pid + "/schedstat";
final long[] statsData = mSinglePidStatsData; final long[] statsData = new long[4];
if (Process.readProcFile(statFile, PROCESS_SCHEDSTATS_FORMAT, if (Process.readProcFile(statFile, PROCESS_SCHEDSTATS_FORMAT,
null, statsData, null)) { null, statsData, null)) {
return statsData[PROCESS_SCHEDSTAT_CPU_DELAY_TIME] / 1_000_000; return statsData[PROCESS_SCHEDSTAT_CPU_DELAY_TIME] / 1_000_000;
} }
return 0; return 0;
} }
}
/** /**
* @return time in milliseconds. * @return time in milliseconds.

View File

@@ -1892,16 +1892,12 @@ public class AppProfiler {
} }
long getCpuTimeForPid(int pid) { long getCpuTimeForPid(int pid) {
synchronized (mProcessCpuTracker) {
return mProcessCpuTracker.getCpuTimeForPid(pid); return mProcessCpuTracker.getCpuTimeForPid(pid);
} }
}
long getCpuDelayTimeForPid(int pid) { long getCpuDelayTimeForPid(int pid) {
synchronized (mProcessCpuTracker) {
return mProcessCpuTracker.getCpuDelayTimeForPid(pid); return mProcessCpuTracker.getCpuDelayTimeForPid(pid);
} }
}
List<ProcessCpuTracker.Stats> getCpuStats(Predicate<ProcessCpuTracker.Stats> predicate) { List<ProcessCpuTracker.Stats> getCpuStats(Predicate<ProcessCpuTracker.Stats> predicate) {
synchronized (mProcessCpuTracker) { synchronized (mProcessCpuTracker) {