From 5cbce85fd3e4796674f5e7b75face9f9bd8b66b1 Mon Sep 17 00:00:00 2001 From: Vitalii Tomkiv Date: Wed, 18 May 2016 17:43:02 -0700 Subject: [PATCH 1/2] Make sure Zygote is running at process priority 0 after VM has started. Boosting up zygote priority before VM startup, saves ~450ms of boot time for N9, 180ms for Nexus 5X. (cherry picked from commit 5d551a5ac3d13706f62a86842ff6851e1d25213b) Bug: 28866384 Test: m Test: Device boots Change-Id: Ic85892b408e15bbc1de7ce706f113f23974fe478 --- core/jni/com_android_internal_os_Zygote.cpp | 22 ++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp index 894e1047e1aeb..39cd6e5fb951f 100644 --- a/core/jni/com_android_internal_os_Zygote.cpp +++ b/core/jni/com_android_internal_os_Zygote.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -153,6 +154,24 @@ static void SetSigChldHandler() { } } +// Resets nice priority for zygote process. Zygote priority can be set +// to high value during boot phase to speed it up. We want to ensure +// zygote is running at normal priority before childs are forked from it. +// +// This ends up being called repeatedly before each fork(), but there's +// no real harm in that. +static void ResetNicePriority(JNIEnv* env) { + errno = 0; + int prio = getpriority(PRIO_PROCESS, 0); + if (prio == -1 && errno != 0) { + ALOGW("getpriority failed: %s\n", strerror(errno)); + } + if (prio != 0 && setpriority(PRIO_PROCESS, 0, 0) != 0) { + ALOGE("setpriority(%d, 0, 0) failed: %s", PRIO_PROCESS, strerror(errno)); + RuntimeAbort(env, __LINE__, "setpriority failed"); + } +} + // Sets the SIGCHLD handler back to default behavior in zygote children. static void UnsetSigChldHandler() { struct sigaction sa; @@ -486,6 +505,8 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra RuntimeAbort(env, __LINE__, "Unable to restat file descriptor table."); } + ResetNicePriority(env); + pid_t pid = fork(); if (pid == 0) { @@ -774,4 +795,3 @@ int register_com_android_internal_os_Zygote(JNIEnv* env) { return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods)); } } // namespace android - From 80fb0b0a4466c7b5d7eacd337b99a5ca53a840ac Mon Sep 17 00:00:00 2001 From: Todd Kjos Date: Tue, 6 Sep 2016 18:25:46 -0700 Subject: [PATCH 2/2] Revert "Add boost to increase the load on newly-forked zygote processes." This reverts commit 6d43a861d01ef48f37c69f96346a13d23164a585. (cherry picked from commit 0f2ded62ae3d88ddb732df182c3bfee3e2729018) Test: m Test: Device boots Change-Id: I627e47c6ead4ce2d2bffa923a32a56d06c022df7 --- core/jni/Android.mk | 4 --- core/jni/com_android_internal_os_Zygote.cpp | 30 --------------------- 2 files changed, 34 deletions(-) diff --git a/core/jni/Android.mk b/core/jni/Android.mk index 5fcde8b5bc4e6..70e90044f3c74 100644 --- a/core/jni/Android.mk +++ b/core/jni/Android.mk @@ -20,10 +20,6 @@ ifneq ($(ENABLE_CPUSETS),) LOCAL_CFLAGS += -DENABLE_CPUSETS endif -ifneq ($(ENABLE_SCHED_BOOST),) - LOCAL_CFLAGS += -DENABLE_SCHED_BOOST -endif - LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES LOCAL_CFLAGS += -DU_USING_ICU_NAMESPACE=0 diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp index 39cd6e5fb951f..da059e3bfe679 100644 --- a/core/jni/com_android_internal_os_Zygote.cpp +++ b/core/jni/com_android_internal_os_Zygote.cpp @@ -437,27 +437,6 @@ void SetThreadName(const char* thread_name) { } } -#ifdef ENABLE_SCHED_BOOST -static void SetForkLoad(bool boost) { - // set scheduler knob to boost forked processes - pid_t currentPid = getpid(); - // fits at most "/proc/XXXXXXX/sched_init_task_load\0" - char schedPath[35]; - snprintf(schedPath, sizeof(schedPath), "/proc/%u/sched_init_task_load", currentPid); - int schedBoostFile = open(schedPath, O_WRONLY); - if (schedBoostFile < 0) { - ALOGW("Unable to set zygote scheduler boost"); - return; - } - if (boost) { - write(schedBoostFile, "100\0", 4); - } else { - write(schedBoostFile, "0\0", 2); - } - close(schedBoostFile); -} -#endif - // The list of open zygote file descriptors. static FileDescriptorTable* gOpenFdTable = NULL; @@ -471,10 +450,6 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra jstring instructionSet, jstring dataDir) { SetSigChldHandler(); -#ifdef ENABLE_SCHED_BOOST - SetForkLoad(true); -#endif - sigset_t sigchld; sigemptyset(&sigchld); sigaddset(&sigchld, SIGCHLD); @@ -655,11 +630,6 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra } else if (pid > 0) { // the parent process -#ifdef ENABLE_SCHED_BOOST - // unset scheduler knob - SetForkLoad(false); -#endif - // We blocked SIGCHLD prior to a fork, we unblock it here. if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) { ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));