summaryrefslogtreecommitdiff
path: root/runtime/exec_utils.cc
AgeCommit message (Collapse)Author
2024-08-12Fix big negative dex2oatWallTimeMillis. Jiakai Zhang
We calculate dex2oatWallTimeMillis by (uptime - starttime). To get a reasonable result, both times need to be from the same clock. However, it's unclear which clock the kernel uses for the latter. proc_pid_stat(5) does not have anything helpful on this matter: (22) starttime %llu The time the process started after system boot. Before Linux 2.6, this value was expressed in jiffies. Since Linux 2.6, the value is expressed in clock ticks (divide by sysconf(_SC_CLK_TCK)). The format for this field was %lu before Linux 2.6. To make sure we use the same clock, we obtain the uptime on process start as the start time. Bug: 315061143 Test: Presubmit Change-Id: I5f344e953a945656474b3f625368c1ccb5aa52d8
2024-06-11Use process groups to manage artd's subprocesses. Jiakai Zhang
This change adds a new option `new_process_group` to ExecUtils, and this option is only used by artd to manage its subprocesses. artd creates a process group for each child process and its descendants, and it uses `kill(-pid, SIGKILL)` to kill the whole process group upon job cancellation. Before this change, ExecUtils always creates a new process group for a child process. This behavior can be dated back to https://android.googlesource.com/platform/art/+/5643b786dd48df1f4b89a39e6024a22d032c79d4%5E%21/. A comment on the `setpgid` call says: "change process groups, so we don't get reaped by ProcessManager". We cannot find out why changing the process group was needed or how process reaping was related to process groups at that time because there isn't such a thing called "ProcessManager" in Android anymore. However, we don't need this behavior today because the usage of the code that does fork and exec has completely changed. At the time where the original CL was submitted, the code was for the runtime to generate a persistent boot image on /data. Today, the runtime never generates a persistent boot image because it's considered insecure. Today, ExecUtils is mostly used in tests. Other than tests, it's only used in four places: Zygote, odrefresh, artd, and dexopt_chroot_setup. - Zygote uses it to call dex2oat to create an in-memory boot image. It doesn't make sense to keep dex2oat running when Zygote (the memfd owner) is killed. - odrefresh uses it to call dex2oat to create persistent boot images. We do want to kill dex2oat when killing odrefresh. - artd uses it to call dex2oat, profman, derive_classpath, and odrefresh. It sets `new_process_group` to true, so it's behavior regarding process groups is unchanged. It's a service managed by the service manager. The service manager uses cgroups, not process grups, to manage and kill processes, so creating new process groups for artd's children is fine. - dexopt_chroot_setup uses it to call apexd and linkerconfig. We do want to kill apexd and linkerconfig when dexopt_chroot_setup is killed. Therefore, changing this behavior is fine. After this change, ExecUtils no longer creates a new process group for a child process unless `new_process_group` is set to true. Bug: 345723405 Bug: 311377497 Test: m test-art-host-gtest-art_runtime_tests Test: m test-art-host-gtest-art_artd_tests Test: - 1. adb shell pm compile -m speed -f -v com.android.settings 2. adb shell pm art cancel <job-id> 3. No change to the existing behavior: dex2oat is still killed. Test: - 1. adb shell pm art pr-dexopt-job --run 2. Press Ctrl+C. 3. Both odrefresh and dex2oat are killed. Change-Id: I3058fc68a6bd5f98617422c1eb0861648b144a51
2024-06-07Revert "Kill the child process when the parent process dies." Jiakai Zhang
This reverts commit 7afcee1180986d8b87773d173af7ecd63859ffe3. Reason for revert: It doesn't work. See details in b/345723405#comment2 We'll find an alternative. Bug: 345723405 Bug: 311377497 Change-Id: I70152124bb2f996c5211b4f4bdf9519e80bdf6ca Test: Presubmit
2024-06-06Make teardown faster and more reliable. Jiakai Zhang
Before this change, we only trigger GC and sleep 5 seconds, hoping that processes in chroot exit within that 5 seconds. After this change, we use pidfd to wait for processes to exit, so teardown can be performed as soon as all processes have exited, making teardown considerably faster. Also, if the processes don't exit within 5 seconds, we kill them, making teardown more reliable. Only one more SELinux permission is needed, which is the permission to send `sigkill` to artd and its subprocesses. system_server already has all the other permissions to perform the operations used in this CL. 1. system_server has supplementary group "readproc" (3009), to list all pids in "/proc", bypassing "hidepid=2" (see proc(5)). - https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/com/android/internal/os/ZygoteInit.java;l=658;drc=7d3ffbae618e9e728644a96647ed709bf39ae759 2. system_server can read /proc/<pid>/* for all domains. - https://cs.android.com/android/platform/superproject/main/+/main:system/sepolicy/private/system_server.te;l=213;drc=ca2f3851afaee866d37caae16598b3d5c20844d4 3. system_server has CAP_SYS_PTRACE, to read the "/proc/<pid>/exe" link of anyprocess (see proc(5)). - https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/com/android/internal/os/ZygoteInit.java;l=635;drc=7d3ffbae618e9e728644a96647ed709bf39ae759 - https://cs.android.com/android/platform/superproject/main/+/main:system/sepolicy/private/system_server.te;l=145;drc=ca2f3851afaee866d37caae16598b3d5c20844d4 4. system_server has CAP_KILL, to kill processes that belong to other users (see kill(2)). - https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/com/android/internal/os/ZygoteInit.java;l=628;drc=7d3ffbae618e9e728644a96647ed709bf39ae759 - https://cs.android.com/android/platform/superproject/main/+/main:system/sepolicy/private/system_server.te;l=138;drc=ca2f3851afaee866d37caae16598b3d5c20844d4 Bug: 311377497 Test: m test-art-host-gtest-art_libarttools_tests Test: atest art_standalone_libarttools_tests --iterations 10 Test: adb shell pm art pr-dexopt-job --test Test: Run and cancel Pre-reboot Dexopt multiple times. Change-Id: I1e41cd71402944e31b33e410ac1635766afe55c4
2024-05-30Kill the child process when the parent process dies. Jiakai Zhang
When we have a process hierarchy, such as artd->odrefresh->dex2oat, we want to be able to kill a subtree. For example, if we kill odrefresh, we want dex2oat to be killed as well; if we kill artd, we want both odrefresh and dex2oat to be killed as well. This can be achieved by PR_SET_PDEATHSIG. Bug: 311377497 Test: Kill odrefresh and see dex2oat killed. Change-Id: I28cc271f5c37b9ad50bee179eb9b211cb3545a2f
2024-01-18Add visibility attributes in runtime/e* Dmitrii Ishcheikin
Bug: 260881207 Test: presubmit Test: abtd app_compat_drm Test: abtd app_compat_top_100 Test: abtd app_compat_banking Change-Id: I0eca5c4fd64ec61388eded9b9e066091581e0c7e
2023-12-07Make ProcessStat collection more robust. Jiakai Zhang
- Handle the case where start time is 0. - Handle the case where getting uptime failed. - Use 64bit int. (32bit is enough to record a time less than 24 days, but just in case.) Bug: 315061143 Test: m test-art-host-gtest-art_runtime_tests Change-Id: I3ac546f6921b8d6add2491170b5d8f50cc7169c1
2023-02-03Fix performance-inefficient-vector-operation clang-tidy issues Stefano Cianciulli
Test: m tidy-art Bug: 264654008 Merged-In: I6b0b495cbb934f9b8caedb389a643428d3cffc47 (cherry picked from commit d59d4d116f9d40113c32c8b1225388308830b099) Change-Id: Ic0a52fb2c6842d18b292088135a39d6886b69d46
2022-09-16odrefresh: Report dex2oat result for each stage. Jiakai Zhang
This is the first step to improve odrefresh metrics collection. More CLs will follow. After the change, the metrics should tell: - whether dex2oat is invoked or not - whether dex2oat crashed or exited with a code Partially cherry-picked from commit fa152ba922a6301a10671d4684b75fe8cb2a2330 Bug: 246534524 Test: m test-art-host-gtest-art_runtime_tests Test: atest ArtGtestsTargetChroot:OdRefreshTest Merged-In: Id962a6e6a765371d290a836c51059d725c9250f3 Change-Id: I69ddf64552d4f2e51e111a40871890d41c88721d
2022-09-16Change ExecUtils to support callbacks. Jiakai Zhang
artd needs the callbacks to get the pid in order to kill the subprocess. Partially cherry-picked from commit 659f49bdb1263ceb26f666052f3ef7e4732b4eb2 Bug: 244412198 Test: m test-art-host-gtest-art_runtime_tests Merged-In: I8c40949b5ed88ff85ddedad9d86f0b9bbfddb98d Change-Id: Ia28410bdf6314178756ee5e3a94000dc6f3ecc97
2022-09-16Add a function in ExecUtils to return wall time and CPU time. Jiakai Zhang
Bug: 245380798 Test: m test-art-host-gtest-art_runtime_tests Change-Id: I25d0d4c3dfaf24490c96ca650f076b2214597d2a Merged-In: I25d0d4c3dfaf24490c96ca650f076b2214597d2a (cherry picked from commit ebbd20c55adbdde7922c2ca402e1a04ed6ecee4c)
2022-07-11Fix ExecUtils.WaitChildWithTimeoutFallback by adding a fallback. Jiakai Zhang
`WaitChildWithTimeout` didn't work with old Linux kernels because it uses a syscall `pidfd_open`, which was added in Linux 5.4. This change adds a fallback implementation of `WaitChildWithTimeout` that creates a thread to wait instead of relying on `pidfd_open`. Also: - Use android::base::unique_fd to hold pidfd so that the fd is guaranteed to be closed. - Allow timeout_sec to be negative, in which case it blocks until the subprocess exits. Bug: 229268202 Test: m test-art-host-gtest-art_runtime_tests Change-Id: Iec3f21db135d9a8a3a915372253f1ff3e285e5cf Merged-In: Iec3f21db135d9a8a3a915372253f1ff3e285e5cf (cherry picked from commit 99bd8dd2bf8eac1a60bf65e442462753ed7f2147)
2022-07-11Revert^2 "Update ExecUtils to support artd use cases." Jiakai Zhang
This reverts commit ff95ad517297384d4a2ea589490019f90d637e17. Reason for revert: Fix available (aosp/2148047) Test: Presubmit Change-Id: I952960949267a7b6e63cd7966a347cb1f3374b85 Merged-In: I375034162cded6fdf09ee4d4cd783a0d3987af49
2022-07-05Revert "Update ExecUtils to support artd use cases." Jiakai Zhang
This reverts commit f0061f62fbd5e4766220c154d914e7b9d07a86eb. Reason for revert: Broke LUCI. Change-Id: Iec793a667545a8469f2a712135353e816c623262
2022-07-04Update ExecUtils to support artd use cases. Jiakai Zhang
- Use pidfd_open/poll instaed of sigprocmask/sigtimedwait to implement executing a command with a timeout because sigprocmask/sigtimedwait does not support multithreaded program, while artd is multithreaded. The same approach is used in installd in https://r.android.com/1993670. - Add "const" to the `std::vector<std::string>& arg_vector` parameter. - Use "int" instead of "time_t" for the "timeout_sec" parameter because "time_t" represents the time since epoch rather than a duration. - Set an error message with the signal number when the subprocess is terminated by a signal. - Crash the subprocess instead of exiting it with an exit code when execv fails. The behavior does not change. ExecUtilsTest is still passing. Bug: 229268202 Test: m test-art-host-gtest-art_runtime_tests Test: atest ArtGtestsTargetChroot:ExecUtilsTest Change-Id: I375034162cded6fdf09ee4d4cd783a0d3987af49 Merged-In: I375034162cded6fdf09ee4d4cd783a0d3987af49 (cherry picked from commit b48b732c535894bbae8f3c19eceeda67c40e9d61)
2021-01-18Add an overload of ExecAndReturnCode taking a timeout Orion Hodson
Enables callers to timeout waiting on a subprocess. Bug: 177432913 Test: test-art-host-gtest-art_runtime_tests32 Change-Id: I7a27e0ca4679f45daf815d7e2563ca9b723bd701
2017-06-08ART: Fix or disable some tidy warnings. Andreas Gampe
Add a strlcpy shim for the host, so we can use strlcpy instead of strcpy everywhere. Fixed warnings include unused-decls, (some) unreachable code, use after std::move, string char append, leaks, (some) excessive padding. Disable some warnings we cannot or do not want to avoid. Bug: 32619234 Test: m Test: m test-art-host Change-Id: Ie191985eebb160d94b988b41735d4f0a1fa1b54e
2017-02-01Separate art::Exec from utils David Sehr
The rest of utils.cc does not depend on art::Runtime. This separates the part dependent on that class, so that including utils.cc in the build does not require the entire Runtime. Another preparatory cleanup to getting tools to build on Windows. Bug: 22322814 Test: test-art Change-Id: I194ff363fc2ab87e5311ecea6973a2d0fad2621d