Framework: Add API to get zygote PID

Add a zygote call that returns the PID of the zygote. Used for
profiling.

Bug: 79266002
Test: m
Change-Id: Idf9ba5ec314eb4cb72921000ad6cb34d1035a92a
diff --git a/core/java/android/os/ZygoteProcess.java b/core/java/android/os/ZygoteProcess.java
index 5718ef9..bb77a93 100644
--- a/core/java/android/os/ZygoteProcess.java
+++ b/core/java/android/os/ZygoteProcess.java
@@ -462,6 +462,35 @@
     }
 
     /**
+     * Attempt to retrieve the PID of the zygote serving the given abi.
+     */
+    public int getZygotePid(String abi) {
+        try {
+            synchronized (mLock) {
+                ZygoteState state = openZygoteSocketIfNeeded(abi);
+
+                // Each query starts with the argument count (1 in this case)
+                state.writer.write("1");
+                // ... followed by a new-line.
+                state.writer.newLine();
+                // ... followed by our only argument.
+                state.writer.write("--get-pid");
+                state.writer.newLine();
+                state.writer.flush();
+
+                // The response is a length prefixed stream of ASCII bytes.
+                int numBytes = state.inputStream.readInt();
+                byte[] bytes = new byte[numBytes];
+                state.inputStream.readFully(bytes);
+
+                return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII));
+            }
+        } catch (Exception ex) {
+            throw new RuntimeException("Failure retrieving pid", ex);
+        }
+    }
+
+    /**
      * Push hidden API blacklisting exemptions into the zygote process(es).
      *
      * <p>The list of exemptions will take affect for all new processes forked from the zygote after