Merge "Resolve Thread Visibility Issue in ProcessErrorStateRecord.appNotResponding" into udc-qpr-dev am: 52f45340a9

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/23766528

Change-Id: I4cc8efb4252354001abcfcbc25e0eea3bbf79bcc
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Mohamad Mahmoud
2023-06-27 13:54:40 +00:00
committed by Automerger Merge Worker
2 changed files with 30 additions and 18 deletions

View File

@@ -51,11 +51,11 @@ import android.util.SparseBooleanArray;
import com.android.internal.annotations.CompositeRWLock; import com.android.internal.annotations.CompositeRWLock;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.expresslog.Counter;
import com.android.internal.os.ProcessCpuTracker; import com.android.internal.os.ProcessCpuTracker;
import com.android.internal.os.TimeoutRecord; import com.android.internal.os.TimeoutRecord;
import com.android.internal.os.anr.AnrLatencyTracker; import com.android.internal.os.anr.AnrLatencyTracker;
import com.android.internal.util.FrameworkStatsLog; import com.android.internal.util.FrameworkStatsLog;
import com.android.modules.expresslog.Counter;
import com.android.server.ResourcePressureUtil; import com.android.server.ResourcePressureUtil;
import com.android.server.criticalevents.CriticalEventLog; import com.android.server.criticalevents.CriticalEventLog;
import com.android.server.stats.pull.ProcfsMemoryUtil.MemorySnapshot; import com.android.server.stats.pull.ProcfsMemoryUtil.MemorySnapshot;
@@ -456,6 +456,11 @@ class ProcessErrorStateRecord {
String currentPsiState = ResourcePressureUtil.currentPsiState(); String currentPsiState = ResourcePressureUtil.currentPsiState();
latencyTracker.currentPsiStateReturned(); latencyTracker.currentPsiStateReturned();
report.append(currentPsiState); report.append(currentPsiState);
// The 'processCpuTracker' variable is a shared resource that might be initialized and
// updated in a different thread. In order to prevent thread visibility issues, which
// can occur when one thread does not immediately see the changes made to
// 'processCpuTracker' by another thread, it is necessary to use synchronization whenever
// 'processCpuTracker' is accessed or modified.
ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true); ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true);
// We push the native pids collection task to the helper thread through // We push the native pids collection task to the helper thread through
@@ -517,12 +522,16 @@ class ProcessErrorStateRecord {
} }
mService.updateCpuStatsNow(); mService.updateCpuStatsNow();
mService.mAppProfiler.printCurrentCpuState(report, anrTime); mService.mAppProfiler.printCurrentCpuState(report, anrTime);
synchronized (processCpuTracker) {
info.append(processCpuTracker.printCurrentLoad()); info.append(processCpuTracker.printCurrentLoad());
}
info.append(report); info.append(report);
} }
report.append(tracesFileException.getBuffer()); report.append(tracesFileException.getBuffer());
synchronized (processCpuTracker) {
info.append(processCpuTracker.printCurrentState(anrTime)); info.append(processCpuTracker.printCurrentState(anrTime));
}
Slog.e(TAG, info.toString()); Slog.e(TAG, info.toString());
if (tracesFile == null) { if (tracesFile == null) {

View File

@@ -464,14 +464,16 @@ public class StackTracesDumpHelper {
latencyTracker.processCpuTrackerMethodsCalled(); latencyTracker.processCpuTrackerMethodsCalled();
} }
ArrayList<Integer> extraPids = new ArrayList<>(); ArrayList<Integer> extraPids = new ArrayList<>();
synchronized (processCpuTracker) {
processCpuTracker.init(); processCpuTracker.init();
}
try { try {
Thread.sleep(200); Thread.sleep(200);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
synchronized (processCpuTracker) {
processCpuTracker.update(); processCpuTracker.update();
// We'll take the stack crawls of just the top apps using CPU. // We'll take the stack crawls of just the top apps using CPU.
final int workingStatsNumber = processCpuTracker.countWorkingStats(); final int workingStatsNumber = processCpuTracker.countWorkingStats();
for (int i = 0; i < workingStatsNumber && extraPids.size() < 2; i++) { for (int i = 0; i < workingStatsNumber && extraPids.size() < 2; i++) {
@@ -488,6 +490,7 @@ public class StackTracesDumpHelper {
+ stats.pid); + stats.pid);
} }
} }
}
if (latencyTracker != null) { if (latencyTracker != null) {
latencyTracker.processCpuTrackerMethodsReturned(); latencyTracker.processCpuTrackerMethodsReturned();
} }