Propagate background scheduling class across processes.
This is a very simply implementation: upon receiving an IPC, if the handling thread is at a background priority (the driver will have taken care of propagating this from the calling thread), then stick it in to the background scheduling group. Plus an API to turn this off for the process, which is used by the system process. This also pulls some of the code for managing scheduling classes out of the Process JNI wrappers and in to some convenience methods in thread.h.
This commit is contained in:
@@ -76,6 +76,13 @@ public class BinderInternal {
|
||||
*/
|
||||
public static final native IBinder getContextObject();
|
||||
|
||||
/**
|
||||
* Special for system process to not allow incoming calls to run at
|
||||
* background scheduling priority.
|
||||
* @hide
|
||||
*/
|
||||
public static final native void disableBackgroundScheduling(boolean disable);
|
||||
|
||||
static native final void handleGc();
|
||||
|
||||
public static void forceGc(String reason) {
|
||||
|
||||
@@ -670,6 +670,12 @@ static void android_os_BinderInternal_joinThreadPool(JNIEnv* env, jobject clazz)
|
||||
android::IPCThreadState::self()->joinThreadPool();
|
||||
}
|
||||
|
||||
static void android_os_BinderInternal_disableBackgroundScheduling(JNIEnv* env,
|
||||
jobject clazz, jboolean disable)
|
||||
{
|
||||
IPCThreadState::disableBackgroundScheduling(disable ? true : false);
|
||||
}
|
||||
|
||||
static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz)
|
||||
{
|
||||
LOGV("Gc has executed, clearing binder ops");
|
||||
@@ -682,6 +688,7 @@ static const JNINativeMethod gBinderInternalMethods[] = {
|
||||
/* name, signature, funcPtr */
|
||||
{ "getContextObject", "()Landroid/os/IBinder;", (void*)android_os_BinderInternal_getContextObject },
|
||||
{ "joinThreadPool", "()V", (void*)android_os_BinderInternal_joinThreadPool },
|
||||
{ "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling },
|
||||
{ "handleGc", "()V", (void*)android_os_BinderInternal_handleGc }
|
||||
};
|
||||
|
||||
|
||||
@@ -120,11 +120,7 @@ jint android_os_Process_myUid(JNIEnv* env, jobject clazz)
|
||||
|
||||
jint android_os_Process_myTid(JNIEnv* env, jobject clazz)
|
||||
{
|
||||
#ifdef HAVE_GETTID
|
||||
return gettid();
|
||||
#else
|
||||
return getpid();
|
||||
#endif
|
||||
return androidGetTid();
|
||||
}
|
||||
|
||||
jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name)
|
||||
@@ -191,15 +187,11 @@ jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name)
|
||||
|
||||
void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
|
||||
{
|
||||
if (grp > ANDROID_TGROUP_MAX || grp < 0) {
|
||||
signalExceptionForGroupError(env, clazz, EINVAL);
|
||||
int res = androidSetThreadSchedulingGroup(pid, grp);
|
||||
if (res != NO_ERROR) {
|
||||
signalExceptionForGroupError(env, clazz, res == BAD_VALUE ? EINVAL : errno);
|
||||
return;
|
||||
}
|
||||
|
||||
if (set_sched_policy(pid, (grp == ANDROID_TGROUP_BG_NONINTERACT) ?
|
||||
SP_BACKGROUND : SP_FOREGROUND)) {
|
||||
signalExceptionForGroupError(env, clazz, errno);
|
||||
}
|
||||
}
|
||||
|
||||
void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
|
||||
@@ -275,22 +267,15 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin
|
||||
void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
|
||||
jint pid, jint pri)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (pri >= ANDROID_PRIORITY_BACKGROUND) {
|
||||
rc = set_sched_policy(pid, SP_BACKGROUND);
|
||||
} else if (getpriority(PRIO_PROCESS, pid) >= ANDROID_PRIORITY_BACKGROUND) {
|
||||
rc = set_sched_policy(pid, SP_FOREGROUND);
|
||||
}
|
||||
|
||||
if (rc) {
|
||||
signalExceptionForGroupError(env, clazz, errno);
|
||||
return;
|
||||
}
|
||||
|
||||
if (setpriority(PRIO_PROCESS, pid, pri) < 0) {
|
||||
signalExceptionForPriorityError(env, clazz, errno);
|
||||
int rc = androidSetThreadPriority(pid, pri);
|
||||
if (rc != 0) {
|
||||
if (rc == INVALID_OPERATION) {
|
||||
signalExceptionForPriorityError(env, clazz, errno);
|
||||
} else {
|
||||
signalExceptionForGroupError(env, clazz, errno);
|
||||
}
|
||||
}
|
||||
|
||||
//LOGI("Setting priority of %d: %d, getpriority returns %d\n",
|
||||
// pid, pri, getpriority(PRIO_PROCESS, pid));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user