Fix UID tracking in ProcessCpuTracker.
Many processes are incorrectly listed under UID 0 (e.g. surfaceflinger)
because some "/proc/$PID/stat" files are with user "root".
The user of the folder "/proc/$PID/" is the correct one to use.
Bug: 119218763
Test: adb shell dumpsys batterystats -c | grep surfaceflinger
Change-Id: Ibfbe9b04c884bc9bde383629bcbba73f48ed34ee
(cherry picked from commit 7ead3ee46fa208df469e53055e7fd8d6b442b949)
diff --git a/core/java/com/android/internal/os/ProcessCpuTracker.java b/core/java/com/android/internal/os/ProcessCpuTracker.java
index 6179918..dcf8d28 100644
--- a/core/java/com/android/internal/os/ProcessCpuTracker.java
+++ b/core/java/com/android/internal/os/ProcessCpuTracker.java
@@ -19,10 +19,10 @@
import static android.os.Process.*;
import android.annotation.UnsupportedAppUsage;
-import android.os.FileUtils;
import android.os.Process;
import android.os.StrictMode;
import android.os.SystemClock;
+import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.util.Slog;
@@ -247,6 +247,7 @@
pid = _pid;
if (parentPid < 0) {
final File procDir = new File("/proc", Integer.toString(pid));
+ uid = getUid(procDir.toString());
statFile = new File(procDir, "stat").toString();
cmdlineFile = new File(procDir, "cmdline").toString();
threadsDir = (new File(procDir, "task")).toString();
@@ -262,13 +263,22 @@
parentPid));
final File taskDir = new File(
new File(procDir, "task"), Integer.toString(pid));
+ uid = getUid(taskDir.toString());
statFile = new File(taskDir, "stat").toString();
cmdlineFile = null;
threadsDir = null;
threadStats = null;
workingThreads = null;
}
- uid = FileUtils.getUid(statFile.toString());
+ }
+
+ private static int getUid(String path) {
+ try {
+ return Os.stat(path).st_uid;
+ } catch (ErrnoException e) {
+ Slog.w(TAG, "Failed to stat(" + path + "): " + e);
+ return -1;
+ }
}
}