ART: Remember whether the cache was pruned

Remember (and expose) whether the cache was pruned when initializing
the runtime.

Bug: 27618311

(cherry picked from commit 7ef1b4b20bd7cf485f5e443927c1e6fa797a0439)

Change-Id: I178d18d731a9dce5cb11ed0874c9c9c9d36d7d8e
diff --git a/runtime/gc/space/image_space_fs.h b/runtime/gc/space/image_space_fs.h
index ec4bf92..5237466 100644
--- a/runtime/gc/space/image_space_fs.h
+++ b/runtime/gc/space/image_space_fs.h
@@ -26,6 +26,7 @@
 #include "base/unix_file/fd_file.h"
 #include "globals.h"
 #include "os.h"
+#include "runtime.h"
 #include "utils.h"
 
 namespace art {
@@ -200,6 +201,11 @@
   impl::DeleteDirectoryContents(GetDalvikCacheOrDie(".", false), false);
   // Prune /data/dalvik-cache/<isa>.
   impl::DeleteDirectoryContents(GetDalvikCacheOrDie(GetInstructionSetString(isa), false), false);
+
+  // Be defensive. There should be a runtime created here, but this may be called in a test.
+  if (Runtime::Current() != nullptr) {
+    Runtime::Current()->SetPrunedDalvikCache(true);
+  }
 }
 
 // We write out an empty file to the zygote's ISA specific cache dir at the start of
diff --git a/runtime/native/dalvik_system_VMRuntime.cc b/runtime/native/dalvik_system_VMRuntime.cc
index f6b2f21..4ac28ae 100644
--- a/runtime/native/dalvik_system_VMRuntime.cc
+++ b/runtime/native/dalvik_system_VMRuntime.cc
@@ -622,6 +622,11 @@
   return env->NewStringUTF(GetInstructionSetString(kRuntimeISA));
 }
 
+static jboolean VMRuntime_didPruneDalvikCache(JNIEnv* env ATTRIBUTE_UNUSED,
+                                              jclass klass ATTRIBUTE_UNUSED) {
+  return Runtime::Current()->GetPrunedDalvikCache() ? JNI_TRUE : JNI_FALSE;
+}
+
 static JNINativeMethod gMethods[] = {
   NATIVE_METHOD(VMRuntime, addressOf, "!(Ljava/lang/Object;)J"),
   NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
@@ -657,6 +662,7 @@
                 "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V"),
   NATIVE_METHOD(VMRuntime, isBootClassPathOnDisk, "(Ljava/lang/String;)Z"),
   NATIVE_METHOD(VMRuntime, getCurrentInstructionSet, "()Ljava/lang/String;"),
+  NATIVE_METHOD(VMRuntime, didPruneDalvikCache, "()Z"),
 };
 
 void register_dalvik_system_VMRuntime(JNIEnv* env) {
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 0dfb0cf..66a3828 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -208,7 +208,8 @@
       experimental_flags_(ExperimentalFlags::kNone),
       oat_file_manager_(nullptr),
       is_low_memory_mode_(false),
-      safe_mode_(false) {
+      safe_mode_(false),
+      pruned_dalvik_cache_(false) {
   CheckAsmSupportOffsetsAndSizes();
   std::fill(callee_save_methods_, callee_save_methods_ + arraysize(callee_save_methods_), 0u);
   interpreter::CheckInterpreterAsmConstants();
diff --git a/runtime/runtime.h b/runtime/runtime.h
index 8e99f80..aebfad7 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -610,6 +610,14 @@
     return dump_native_stack_on_sig_quit_;
   }
 
+  bool GetPrunedDalvikCache() const {
+    return pruned_dalvik_cache_;
+  }
+
+  void SetPrunedDalvikCache(bool pruned) {
+    pruned_dalvik_cache_ = pruned;
+  }
+
  private:
   static void InitPlatformSignalHandlers();
 
@@ -822,6 +830,9 @@
   // Whether threads should dump their native stack on SIGQUIT.
   bool dump_native_stack_on_sig_quit_;
 
+  // Whether the dalvik cache was pruned when initializing the runtime.
+  bool pruned_dalvik_cache_;
+
   DISALLOW_COPY_AND_ASSIGN(Runtime);
 };
 std::ostream& operator<<(std::ostream& os, const Runtime::CalleeSaveType& rhs);