summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Sadrul Chowdhury <sadrul@google.com> 2024-02-07 03:34:31 +0000
committer Sadrul Chowdhury <sadrul@google.com> 2024-02-07 18:46:03 +0000
commita517e9e34b94ece9ae22e50ffbed0e1cb832a110 (patch)
tree060f9043453a5051b75654888ea2758c132d8df0
parenta0ce6b06e48bd282aa675575149b9228f59cb4df (diff)
Boost process priority during fork.
The zygote process sets MAX priority for the child process after a fork if it is a 'high priority fork' (e.g. for top-app). However, the zygote does a fair amount of work before the fork, and can take up some amount of time due to getting descheduled. This introduces latency in app-launch, especially on low-power devices. Fix this by setting the zygote process priority to MAX for a high-priority fork early on, and reset it back to DEFAULT afterwards. Bug: 323891634 Test: manually Change-Id: Iac1ea81531f59ee6245c73530f2dd639209421f7
-rw-r--r--core/jni/com_android_internal_os_Zygote.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp
index 9c40a28dfd81..791014305c7a 100644
--- a/core/jni/com_android_internal_os_Zygote.cpp
+++ b/core/jni/com_android_internal_os_Zygote.cpp
@@ -2256,6 +2256,11 @@ pid_t zygote::ForkCommon(JNIEnv* env, bool is_system_server,
const std::vector<int>& fds_to_ignore,
bool is_priority_fork,
bool purge) {
+ ATRACE_CALL();
+ if (is_priority_fork) {
+ setpriority(PRIO_PROCESS, 0, PROCESS_PRIORITY_MAX);
+ }
+
SetSignalHandlers();
// Curry a failure function.
@@ -2341,6 +2346,10 @@ pid_t zygote::ForkCommon(JNIEnv* env, bool is_system_server,
// We blocked SIGCHLD prior to a fork, we unblock it here.
UnblockSignal(SIGCHLD, fail_fn);
+ if (is_priority_fork && pid != 0) {
+ setpriority(PRIO_PROCESS, 0, PROCESS_PRIORITY_DEFAULT);
+ }
+
return pid;
}
@@ -2408,6 +2417,7 @@ static jint com_android_internal_os_Zygote_nativeForkSystemServer(
JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities,
jlong effective_capabilities) {
+ ATRACE_CALL();
std::vector<int> fds_to_close(MakeUsapPipeReadFDVector()),
fds_to_ignore(fds_to_close);
@@ -2483,6 +2493,7 @@ static jint com_android_internal_os_Zygote_nativeForkApp(JNIEnv* env,
jintArray managed_session_socket_fds,
jboolean args_known,
jboolean is_priority_fork) {
+ ATRACE_CALL();
std::vector<int> session_socket_fds =
ExtractJIntArray(env, "USAP", nullptr, managed_session_socket_fds)
.value_or(std::vector<int>());
@@ -2498,6 +2509,7 @@ int zygote::forkApp(JNIEnv* env,
bool args_known,
bool is_priority_fork,
bool purge) {
+ ATRACE_CALL();
std::vector<int> fds_to_close(MakeUsapPipeReadFDVector()),
fds_to_ignore(fds_to_close);