From 5494695148b63ab33e6e5e178dcef5fa6380116c Mon Sep 17 00:00:00 2001 From: Jiakai Zhang Date: Tue, 6 Sep 2022 21:02:09 +0100 Subject: Add a function in ExecUtils to return wall time and CPU time. Bug: 245380798 Test: m test-art-host-gtest-art_runtime_tests Change-Id: I25d0d4c3dfaf24490c96ca650f076b2214597d2a Merged-In: I25d0d4c3dfaf24490c96ca650f076b2214597d2a (cherry picked from commit ebbd20c55adbdde7922c2ca402e1a04ed6ecee4c) --- runtime/exec_utils.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'runtime/exec_utils.h') diff --git a/runtime/exec_utils.h b/runtime/exec_utils.h index 79a12d770a..751c64d9c8 100644 --- a/runtime/exec_utils.h +++ b/runtime/exec_utils.h @@ -26,6 +26,14 @@ namespace art { +struct ProcessStat { + // The total wall time, in milliseconds, that the process spent, or 0 if failed to get the value. + int wall_time_ms = 0; + // The total CPU time, in milliseconds, that the process and any waited-for children spent, or 0 + // if failed to get the value. + int cpu_time_ms = 0; +}; + // Wrapper on fork/execv to run a command in a subprocess. // These spawn child processes using the environment as it was set when the single instance // of the runtime (Runtime::Current()) was started. If no instance of the runtime was started, it @@ -49,8 +57,26 @@ class ExecUtils { /*out*/ bool* timed_out, /*out*/ std::string* error_msg) const; + // Same as above, but also collects stat of the process. The stat is collected no matter the child + // process succeeds or not. + virtual int ExecAndReturnCode(const std::vector& arg_vector, + int timeout_sec, + /*out*/ bool* timed_out, + /*out*/ ProcessStat* stat, + /*out*/ std::string* error_msg) const; + protected: virtual android::base::unique_fd PidfdOpen(pid_t pid) const; + + // Returns the content of `/proc//stat`, or an empty string if failed. + virtual std::string GetProcStat(pid_t pid) const; + + virtual int64_t GetUptimeMs() const; + + virtual int64_t GetTicksPerSec() const; + + private: + bool GetStat(pid_t pid, /*out*/ ProcessStat* stat, /*out*/ std::string* error_msg) const; }; inline bool Exec(const std::vector& arg_vector, /*out*/ std::string* error_msg) { -- cgit v1.2.3-59-g8ed1b