Use nativePostForkSystemServer for system server profiling

Need to use nativePostForkSystemServer instead of nativePostForkChild
since nativePostForkChild is called after the class loader is created.

(cherry-picked from commit 0712f2969c0f19fa2cc41f99751d39ee1852ebbc)
Bug: 139883463
Bug: 144383344
Test: showmap `pid system_server` and verify
Test: atest BootImageProfileTest

Merged-In: I7c4f61b08d286ef6056931231f6b835749092720
Change-Id: I7c4f61b08d286ef6056931231f6b835749092720
diff --git a/runtime/native/dalvik_system_ZygoteHooks.cc b/runtime/native/dalvik_system_ZygoteHooks.cc
index ce942c8..8e832b8 100644
--- a/runtime/native/dalvik_system_ZygoteHooks.cc
+++ b/runtime/native/dalvik_system_ZygoteHooks.cc
@@ -264,7 +264,8 @@
 }
 
 static void ZygoteHooks_nativePostForkSystemServer(JNIEnv* env ATTRIBUTE_UNUSED,
-                                                   jclass klass ATTRIBUTE_UNUSED) {
+                                                   jclass klass ATTRIBUTE_UNUSED,
+                                                   jint runtime_flags) {
   // Set the runtime state as the first thing, in case JIT and other services
   // start querying it.
   Runtime::Current()->SetAsSystemServer();
@@ -281,6 +282,12 @@
   // be closed in the common nativePostForkChild below.
   Runtime::Current()->GetOatFileManager().SetOnlyUseSystemOatFiles(
       /*enforce=*/false, /*assert_no_files_loaded=*/false);
+
+  // Enable profiling if required based on the flags. This is done here instead of in
+  // nativePostForkChild since nativePostForkChild is called after loading the system server oat
+  // files.
+  bool profile_system_server = (runtime_flags & PROFILE_SYSTEM_SERVER) == PROFILE_SYSTEM_SERVER;
+  Runtime::Current()->GetJITOptions()->SetSaveProfilingInfo(profile_system_server);
 }
 
 static void ZygoteHooks_nativePostForkChild(JNIEnv* env,
@@ -444,7 +451,7 @@
 static JNINativeMethod gMethods[] = {
   NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
   NATIVE_METHOD(ZygoteHooks, nativePostZygoteFork, "()V"),
-  NATIVE_METHOD(ZygoteHooks, nativePostForkSystemServer, "()V"),
+  NATIVE_METHOD(ZygoteHooks, nativePostForkSystemServer, "(I)V"),
   NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZZLjava/lang/String;)V"),
   NATIVE_METHOD(ZygoteHooks, startZygoteNoThreadCreation, "()V"),
   NATIVE_METHOD(ZygoteHooks, stopZygoteNoThreadCreation, "()V"),
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 3d4116b..135ee63 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -994,16 +994,13 @@
     }
   }
 
-  if (is_system_server) {
-    jit_options_->SetSaveProfilingInfo(profile_system_server);
-    if (profile_system_server) {
-      // Set the system server package name to "android".
-      // This is used to tell the difference between samples provided by system server
-      // and samples generated by other apps when processing boot image profiles.
-      SetProcessPackageName("android");
-      jit_options_->SetWaitForJitNotificationsToSaveProfile(false);
-      VLOG(profiler) << "Enabling system server profiles";
-    }
+  if (is_system_server && profile_system_server) {
+    // Set the system server package name to "android".
+    // This is used to tell the difference between samples provided by system server
+    // and samples generated by other apps when processing boot image profiles.
+    SetProcessPackageName("android");
+    jit_options_->SetWaitForJitNotificationsToSaveProfile(false);
+    VLOG(profiler) << "Enabling system server profiles";
   }
 
   // Create the thread pools.