Add interface for updating the heap's process state

In the future, the heap's process state will be used
to determine GC behavior.

Change-Id: Iba4f038d28dbf483b6573d8feb25e4246ead1d50
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index deb1b8c..0de5a71 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -57,7 +57,7 @@
 
 // When to create a log message about a slow GC, 100ms.
 static const uint64_t kSlowGcThreshold = MsToNs(100);
-// When to create a log message about a slow pause, 5ms.
+// When to create a log message about a long pause, 5ms.
 static const uint64_t kLongGcPauseThreshold = MsToNs(5);
 static const bool kDumpGcPerformanceOnShutdown = false;
 // Minimum amount of remaining bytes before a concurrent GC is triggered.
@@ -84,6 +84,7 @@
       total_objects_freed_ever_(0),
       large_object_threshold_(3 * kPageSize),
       num_bytes_allocated_(0),
+      process_state_(PROCESS_STATE_TOP),
       verify_missing_card_marks_(false),
       verify_system_weaks_(false),
       verify_pre_gc_heap_(false),
@@ -233,6 +234,10 @@
   }
 };
 
+void Heap::UpdateProcessState(ProcessState process_state) {
+  process_state_ = process_state;
+}
+
 void Heap::AddContinuousSpace(space::ContinuousSpace* space) {
   WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
   DCHECK(space != NULL);
diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h
index 630d063..9e9c2b3 100644
--- a/runtime/gc/heap.h
+++ b/runtime/gc/heap.h
@@ -100,6 +100,24 @@
 };
 const HeapVerificationMode kDesiredHeapVerification = kNoHeapVerification;
 
+// This comes from ActivityManager and needs to be kept in sync.
+enum ProcessState {
+  PROCESS_STATE_PERSISTENT = 0,
+  PROCESS_STATE_PERSISTENT_UI = 1,
+  PROCESS_STATE_TOP = 2,
+  PROCESS_STATE_IMPORTANT_FOREGROUND = 3,
+  PROCESS_STATE_IMPORTANT_BACKGROUND = 4,
+  PROCESS_STATE_BACKUP = 5,
+  PROCESS_STATE_HEAVY_WEIGHT = 6,
+  PROCESS_STATE_SERVICE = 7,
+  PROCESS_STATE_RECEIVER = 8,
+  PROCESS_STATE_HOME = 9,
+  PROCESS_STATE_LAST_ACTIVITY = 10,
+  PROCESS_STATE_CACHED_ACTIVITY = 11,
+  PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12,
+  PROCESS_STATE_CACHED_EMPTY = 13,
+};
+
 class Heap {
  public:
   static const size_t kDefaultInitialSize = 2 * MB;
@@ -359,6 +377,9 @@
                              collector::GcType gc_type)
       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
 
+  // Update process state to let the heap know which type of GC to do.
+  void UpdateProcessState(ProcessState process_state);
+
   // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
   // Assumes there is only one image space.
   space::ImageSpace* GetImageSpace() const;
@@ -512,6 +533,9 @@
   // Number of bytes allocated.  Adjusted after each allocation and free.
   AtomicInteger num_bytes_allocated_;
 
+  // Current process state, updated by activity manager.
+  ProcessState process_state_;
+
   // Heap verification flags.
   const bool verify_missing_card_marks_;
   const bool verify_system_weaks_;
diff --git a/runtime/native/dalvik_system_VMRuntime.cc b/runtime/native/dalvik_system_VMRuntime.cc
index ce3cc93..ce9dd4f 100644
--- a/runtime/native/dalvik_system_VMRuntime.cc
+++ b/runtime/native/dalvik_system_VMRuntime.cc
@@ -21,6 +21,7 @@
 #include "debugger.h"
 #include "dex_file-inl.h"
 #include "gc/allocator/dlmalloc.h"
+#include "gc/heap.h"
 #include "gc/space/dlmalloc_space.h"
 #include "jni_internal.h"
 #include "mirror/class-inl.h"
@@ -197,6 +198,10 @@
   Runtime::Current()->GetHeap()->ConcurrentGC(self);
 }
 
+static void VMRuntime_updateProcessState(JNIEnv* env, jobject, jint processState) {
+  Runtime::Current()->GetHeap()->UpdateProcessState(static_cast<gc::ProcessState>(processState));
+}
+
 static JNINativeMethod gMethods[] = {
   NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
   NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
@@ -214,6 +219,7 @@
   NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
   NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
   NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
+  NATIVE_METHOD(VMRuntime, updateProcessState, "(I)V"),
 };
 
 void register_dalvik_system_VMRuntime(JNIEnv* env) {