Add callback for notifying that startup is completed

Add a callback that can be called to denote that application startup
is completed. This may affect how the profile is collected and how
startup related caches are managed.

Bug: 123377072
Bug: 120671223
Test: test-art-host
Change-Id: If7eb8909cc5e99082a2243b5029380244b46174d
diff --git a/runtime/jit/profile_saver.cc b/runtime/jit/profile_saver.cc
index f908c62..7346a2c 100644
--- a/runtime/jit/profile_saver.cc
+++ b/runtime/jit/profile_saver.cc
@@ -109,6 +109,16 @@
   }
 }
 
+void ProfileSaver::NotifyStartupCompleted() {
+  Thread* self = Thread::Current();
+  MutexLock mu(self, *Locks::profiler_lock_);
+  if (instance_ == nullptr || instance_->shutting_down_) {
+    return;
+  }
+  MutexLock mu2(self, instance_->wait_lock_);
+  instance_->period_condition_.Signal(self);
+}
+
 void ProfileSaver::Run() {
   Thread* self = Thread::Current();
 
@@ -120,7 +130,7 @@
   {
     MutexLock mu(self, wait_lock_);
     const uint64_t end_time = NanoTime() + MsToNs(options_.GetSaveResolvedClassesDelayMs());
-    while (true) {
+    while (!Runtime::Current()->GetStartupCompleted()) {
       const uint64_t current_time = NanoTime();
       if (current_time >= end_time) {
         break;
@@ -129,8 +139,11 @@
     }
     total_ms_of_sleep_ += options_.GetSaveResolvedClassesDelayMs();
   }
-  FetchAndCacheResolvedClassesAndMethods(/*startup=*/ true);
+  // Tell the runtime that startup is completed if it has not already been notified.
+  // TODO: We should use another thread to do this in case the profile saver is not running.
+  Runtime::Current()->NotifyStartupCompleted();
 
+  FetchAndCacheResolvedClassesAndMethods(/*startup=*/ true);
 
   // When we save without waiting for JIT notifications we use a simple
   // exponential back off policy bounded by max_wait_without_jit.
diff --git a/runtime/jit/profile_saver.h b/runtime/jit/profile_saver.h
index 02c8cd1..97271c9 100644
--- a/runtime/jit/profile_saver.h
+++ b/runtime/jit/profile_saver.h
@@ -59,6 +59,9 @@
   // Just for testing purposes.
   static bool HasSeenMethod(const std::string& profile, bool hot, MethodReference ref);
 
+  // Notify that startup has completed.
+  static void NotifyStartupCompleted();
+
  private:
   ProfileSaver(const ProfileSaverOptions& options,
                const std::string& output_filename,
diff --git a/runtime/native/dalvik_system_VMRuntime.cc b/runtime/native/dalvik_system_VMRuntime.cc
index d705d5f..8115b6b 100644
--- a/runtime/native/dalvik_system_VMRuntime.cc
+++ b/runtime/native/dalvik_system_VMRuntime.cc
@@ -306,6 +306,10 @@
   runtime->UpdateProcessState(static_cast<ProcessState>(process_state));
 }
 
+static void VMRuntime_notifyStartupCompleted(JNIEnv*, jobject) {
+  Runtime::Current()->NotifyStartupCompleted();
+}
+
 static void VMRuntime_trimHeap(JNIEnv* env, jobject) {
   Runtime::Current()->GetHeap()->Trim(ThreadForEnv(env));
 }
@@ -722,6 +726,7 @@
   NATIVE_METHOD(VMRuntime, registerNativeFreeInternal, "(I)V"),
   NATIVE_METHOD(VMRuntime, getNotifyNativeInterval, "()I"),
   NATIVE_METHOD(VMRuntime, notifyNativeAllocationsInternal, "()V"),
+  NATIVE_METHOD(VMRuntime, notifyStartupCompleted, "()V"),
   NATIVE_METHOD(VMRuntime, registerSensitiveThread, "()V"),
   NATIVE_METHOD(VMRuntime, requestConcurrentGC, "()V"),
   NATIVE_METHOD(VMRuntime, requestHeapTrim, "()V"),
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index aa35780..6ab5d98 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -2773,4 +2773,21 @@
   }
 }
 
+void Runtime::NotifyStartupCompleted() {
+  bool expected = false;
+  if (!startup_completed_.compare_exchange_strong(expected, true, std::memory_order_seq_cst)) {
+    // Right now NotifyStartupCompleted will be called up to twice, once from profiler and up to
+    // once externally. For this reason there are no asserts.
+    return;
+  }
+  VLOG(startup) << "Startup completed notified";
+
+  // Notify the profiler saver that startup is now completed.
+  ProfileSaver::NotifyStartupCompleted();
+}
+
+bool Runtime::GetStartupCompleted() const {
+  return startup_completed_.load(std::memory_order_seq_cst);
+}
+
 }  // namespace art
diff --git a/runtime/runtime.h b/runtime/runtime.h
index ace0eea..564a1b9 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -836,6 +836,13 @@
     load_app_image_startup_cache_ = enabled;
   }
 
+  // Notify the runtime that application startup is considered completed. Only has effect for the
+  // first call.
+  void NotifyStartupCompleted();
+
+  // Return true if startup is already completed.
+  bool GetStartupCompleted() const;
+
  private:
   static void InitPlatformSignalHandlers();
 
@@ -1161,6 +1168,9 @@
 
   bool load_app_image_startup_cache_ = false;
 
+  // If startup has completed, must happen at most once.
+  std::atomic<bool> startup_completed_ = false;
+
   // Note: See comments on GetFaultMessage.
   friend std::string GetFaultMessageForAbortLogging();
   friend class ScopedThreadPoolUsage;
diff --git a/test/1002-notify-startup/expected.txt b/test/1002-notify-startup/expected.txt
new file mode 100644
index 0000000..a86b9aa
--- /dev/null
+++ b/test/1002-notify-startup/expected.txt
@@ -0,0 +1,3 @@
+JNI_OnLoad called
+Startup completed: false
+Startup completed: true
diff --git a/test/1002-notify-startup/info.txt b/test/1002-notify-startup/info.txt
new file mode 100644
index 0000000..1ebca87
--- /dev/null
+++ b/test/1002-notify-startup/info.txt
@@ -0,0 +1 @@
+Test that the startup completed callback works.
diff --git a/test/1002-notify-startup/src-art/Main.java b/test/1002-notify-startup/src-art/Main.java
new file mode 100644
index 0000000..9a1e442
--- /dev/null
+++ b/test/1002-notify-startup/src-art/Main.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import dalvik.system.VMRuntime;
+
+public class Main {
+  public static void main(String[] args) {
+    System.loadLibrary(args[0]);
+    System.out.println("Startup completed: " + hasStartupCompleted());
+    VMRuntime.getRuntime().notifyStartupCompleted();
+    System.out.println("Startup completed: " + hasStartupCompleted());
+  }
+
+  private static native boolean hasStartupCompleted();
+}
diff --git a/test/1002-notify-startup/startup_interface.cc b/test/1002-notify-startup/startup_interface.cc
new file mode 100644
index 0000000..8705bb2
--- /dev/null
+++ b/test/1002-notify-startup/startup_interface.cc
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "gc/heap.h"
+#include "runtime.h"
+
+namespace art {
+namespace {
+
+extern "C" bool JNICALL Java_Main_hasStartupCompleted(JNIEnv*, jclass) {
+  return Runtime::Current()->GetStartupCompleted();
+}
+
+}  // namespace
+}  // namespace art
diff --git a/test/Android.bp b/test/Android.bp
index a11d5d9..a5d63c2 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -496,6 +496,7 @@
         "800-smali/jni.cc",
         "909-attach-agent/disallow_debugging.cc",
         "1001-app-image-regions/app_image_regions.cc",
+        "1002-notify-startup/startup_interface.cc",
         "1947-breakpoint-redefine-deopt/check_deopt.cc",
         "common/runtime_state.cc",
         "common/stack_inspect.cc",
diff --git a/test/knownfailures.json b/test/knownfailures.json
index 927817a..6a41daf 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -1062,6 +1062,7 @@
           "988-method-trace",
           "989-method-trace-throw",
           "993-breakpoints",
+          "1002-notify-startup",
           "1900-track-alloc",
           "1906-suspend-list-me-first",
           "1914-get-local-instance",