am 95934be3: am 03c7547c: Merge change 24425 into eclair

Merge commit '95934be333d74ac48c19ae40a8207361a913516a'

* commit '95934be333d74ac48c19ae40a8207361a913516a':
  vm: Add support for using scheduler policies instead of cgroups
diff --git a/vm/Globals.h b/vm/Globals.h
index 3141701..c94a578 100644
--- a/vm/Globals.h
+++ b/vm/Globals.h
@@ -633,6 +633,8 @@
      */
     pid_t systemServerPid;
 
+    int kernelGroupScheduling;
+
 //#define COUNT_PRECISE_METHODS
 #ifdef COUNT_PRECISE_METHODS
     PointerSet* preciseMethods;
diff --git a/vm/Init.c b/vm/Init.c
index 046db67..dcbdfb7 100644
--- a/vm/Init.c
+++ b/vm/Init.c
@@ -1120,6 +1120,14 @@
     }
 #endif
 
+    /* Configure group scheduling capabilities */
+    if (!access("/dev/cpuctl/tasks", F_OK)) {
+        LOGV("Using kernel group scheduling");
+        gDvm.kernelGroupScheduling = 1;
+    } else {
+        LOGV("Using kernel scheduler policies");
+    }
+
     /* configure signal handling */
     if (!gDvm.reduceSignals)
         blockSignals();
diff --git a/vm/Thread.c b/vm/Thread.c
index c9db9bc..7ce2c5f 100644
--- a/vm/Thread.c
+++ b/vm/Thread.c
@@ -3086,7 +3086,7 @@
  *
  * Returns 0 on success.
  */
-int dvmChangeThreadSchedulerGroup(const char *cgroup)
+static int dvmChangeThreadSchedulerGroup(const char *cgroup)
 {
 #ifdef HAVE_ANDROID_OS
     int fd;
@@ -3122,6 +3122,29 @@
 }
 
 /*
+ * Change the scheduling policy of the current thread
+ */
+void dvmChangeThreadSchedulerPolicy(SchedPolicy policy)
+{
+    if (gDvm.kernelGroupScheduling) {
+        const char *grp = NULL;
+
+        if (policy == SCHED_BACKGROUND) {
+            grp = "bg_non_interactive";
+        }
+
+        dvmChangeThreadSchedulerGroup(grp);
+    } else {
+        struct sched_param param;
+
+        param.sched_priority = 0;
+        sched_setscheduler(getpid(),
+                           (policy == SCHED_BACKGROUND) ? 5 : 0,
+                            &param);
+    }
+}
+
+/*
  * Change the priority of a system thread to match that of the Thread object.
  *
  * We map a priority value from 1-10 to Linux "nice" values, where lower
@@ -3139,9 +3162,9 @@
     newNice = kNiceValues[newPriority-1];
 
     if (newNice >= ANDROID_PRIORITY_BACKGROUND) {
-        dvmChangeThreadSchedulerGroup("bg_non_interactive");
+        dvmChangeThreadSchedulerPolicy(SCHED_BACKGROUND);
     } else if (getpriority(PRIO_PROCESS, pid) >= ANDROID_PRIORITY_BACKGROUND) {
-        dvmChangeThreadSchedulerGroup(NULL);
+        dvmChangeThreadSchedulerPolicy(SCHED_FOREGROUND);
     }
 
     if (setpriority(PRIO_PROCESS, pid, newNice) != 0) {
diff --git a/vm/Thread.h b/vm/Thread.h
index 402d4c8..020c4b0 100644
--- a/vm/Thread.h
+++ b/vm/Thread.h
@@ -421,10 +421,17 @@
 INLINE JNIEnv* dvmGetThreadJNIEnv(Thread* self) { return self->jniEnv; }
 INLINE void dvmSetThreadJNIEnv(Thread* self, JNIEnv* env) { self->jniEnv = env;}
 
+typedef enum SchedPolicy {
+    SCHED_BACKGROUND = 0,
+    SCHED_FOREGROUND = 1,
+} SchedPolicy;
+
 /*
- * Change the scheduler group of the current process
+ * Change the scheduling policy of the current process.
+ * This is mapped onto whatever underlying scheme the kernel
+ * supports (cgroups/scheduler policies/wombats/etc)
  */
-int dvmChangeThreadSchedulerGroup(const char *group);
+void dvmChangeThreadSchedulerPolicy(SchedPolicy policy);
 
 /*
  * Update the priority value of the underlying pthread.
diff --git a/vm/alloc/Heap.c b/vm/alloc/Heap.c
index c548190..cdb94c7 100644
--- a/vm/alloc/Heap.c
+++ b/vm/alloc/Heap.c
@@ -783,7 +783,7 @@
          */
 
         if (priorityResult >= ANDROID_PRIORITY_BACKGROUND) {
-            dvmChangeThreadSchedulerGroup(NULL);
+            dvmChangeThreadSchedulerPolicy(SCHED_FOREGROUND);
         }
 
         if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL) != 0) {
@@ -1036,7 +1036,7 @@
         }
 
         if (oldThreadPriority >= ANDROID_PRIORITY_BACKGROUND) {
-            dvmChangeThreadSchedulerGroup("bg_non_interactive");
+            dvmChangeThreadSchedulerPolicy(SCHED_BACKGROUND);
         }
     }
     gcElapsedTime = (dvmGetRelativeTimeUsec() - gcHeap->gcStartTime) / 1000;