Fix jiffy hz to milliseconds calculation in CarServiceHelperService. am: cabdc9559d

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/opt/car/services/+/18090204

Change-Id: I42109e47cfbf5503cd14d1540d2cb6a7bfcc6413
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/builtInServices/src/com/android/internal/car/CarServiceHelperService.java b/builtInServices/src/com/android/internal/car/CarServiceHelperService.java
index ddc4250..4e2333c 100644
--- a/builtInServices/src/com/android/internal/car/CarServiceHelperService.java
+++ b/builtInServices/src/com/android/internal/car/CarServiceHelperService.java
@@ -80,6 +80,7 @@
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.regex.Matcher;
@@ -115,8 +116,8 @@
     private static final String CSHS_UPDATABLE_CLASSNAME_STRING =
             "com.android.internal.car.updatable.CarServiceHelperServiceUpdatableImpl";
     private static final String PROC_PID_STAT_PATTERN =
-            "([0-9]*)\\s\\((\\S+)\\)\\s\\S\\s(?:-?[0-9]*\\s){18}([0-9]*)\\s(?:-?[0-9]*\\s)*"
-                    + "-?[0-9]*";
+            "(?<pid>[0-9]*)\\s\\((?<name>\\S+)\\)\\s\\S\\s(?:-?[0-9]*\\s){18}"
+                    + "(?<startClockTicks>[0-9]*)\\s(?:-?[0-9]*\\s)*-?[0-9]*";
 
     private final Context mContext;
     private final Object mLock = new Object();
@@ -541,7 +542,7 @@
                 line = line.substring(0, index);
             }
             return Paths.get(line).getFileName().toString();
-        } catch (IOException e) {
+        } catch (IOException | RuntimeException e) {
             Slogf.w(TAG, "Cannot read %s", filename);
             return ProcessInfo.UNKNOWN_PROCESS;
         }
@@ -553,11 +554,14 @@
             String line = reader.readLine().replace('\0', ' ').trim();
             Matcher m = mProcPidStatPattern.matcher(line);
             if (m.find()) {
-                return new ProcessInfo(Integer.parseInt(m.group(1)), m.group(2),
-                        Long.parseLong(m.group(3)));
+                int readPid = Integer.parseInt(Objects.requireNonNull(m.group("pid")));
+                if (readPid == pid) {
+                    return new ProcessInfo(pid, m.group("name"),
+                            Long.parseLong(Objects.requireNonNull(m.group("startClockTicks"))));
+                }
             }
-        } catch (IOException e) {
-            Slogf.w(TAG, "Cannot read %s", filename);
+        } catch (IOException | RuntimeException e) {
+            Slogf.w(TAG, e, "Cannot read %s", filename);
         }
         return new ProcessInfo(pid, ProcessInfo.UNKNOWN_PROCESS, ProcessInfo.INVALID_START_TIME);
     }
@@ -704,7 +708,8 @@
     private static final class ProcessInfo {
         public static final String UNKNOWN_PROCESS = "unknown process";
         public static final int INVALID_START_TIME = -1;
-        public static final long JIFFY_HZ = Os.sysconf(OsConstants._SC_CLK_TCK);
+
+        private static final long MILLIS_PER_JIFFY = 1000L / Os.sysconf(OsConstants._SC_CLK_TCK);
 
         public final int pid;
         public final String name;
@@ -714,10 +719,14 @@
             this.pid = pid;
             this.name = name;
             this.startTimeMillis = startClockTicks != INVALID_START_TIME
-                    ? startClockTicks/JIFFY_HZ : INVALID_START_TIME;
+                    ? startClockTicks * MILLIS_PER_JIFFY : INVALID_START_TIME;
         }
 
         boolean doMatch(int pid, long startTimeMillis) {
+            // Start time reported by the services that monitor the process health will be either
+            // the actual start time of the pid or the elapsed real time when the pid was last seen
+            // alive. Thus, verify whether the given start time is at least the actual start time of
+            // the pid.
             return this.pid == pid && (this.startTimeMillis == INVALID_START_TIME
                     || this.startTimeMillis <= startTimeMillis);
         }