From 2e613d2ded2c465bd06bd3cac30ffb4576bf72cc Mon Sep 17 00:00:00 2001 From: Serge Pavlov Date: Thu, 23 Apr 2020 13:04:52 +0700 Subject: [Support] Get process statistics in ExecuteAndWait and Wait The functions sys::ExcecuteAndWait and sys::Wait now have additional argument of type pointer to structure, which is filled with process execution statistics upon process termination. These are total and user execution times and peak memory consumption. By default this argument is nullptr so existing users of these function must not change behavior. Differential Revision: https://reviews.llvm.org/D78901 --- llvm/lib/Support/Unix/Program.inc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'llvm/lib/Support/Unix/Program.inc') diff --git a/llvm/lib/Support/Unix/Program.inc b/llvm/lib/Support/Unix/Program.inc index c1b5d0c..f834e95 100644 --- a/llvm/lib/Support/Unix/Program.inc +++ b/llvm/lib/Support/Unix/Program.inc @@ -332,7 +332,8 @@ static bool Execute(ProcessInfo &PI, StringRef Program, namespace llvm { ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait, - bool WaitUntilTerminates, std::string *ErrMsg) { + bool WaitUntilTerminates, std::string *ErrMsg, + Optional *ProcStat) { struct sigaction Act, Old; assert(PI.Pid && "invalid pid to wait on, process not started?"); @@ -355,9 +356,12 @@ ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait, // Parent process: Wait for the child process to terminate. int status; ProcessInfo WaitResult; + rusage Info; + if (ProcStat) + ProcStat->reset(); do { - WaitResult.Pid = waitpid(ChildPid, &status, WaitPidOptions); + WaitResult.Pid = wait4(ChildPid, &status, WaitPidOptions, &Info); } while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR); if (WaitResult.Pid != PI.Pid) { @@ -395,6 +399,13 @@ ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait, sigaction(SIGALRM, &Old, nullptr); } + if (ProcStat) { + std::chrono::microseconds UserT = toDuration(Info.ru_utime); + std::chrono::microseconds KernelT = toDuration(Info.ru_stime); + uint64_t PeakMemory = static_cast(Info.ru_maxrss); + *ProcStat = ProcessStatistics{UserT + KernelT, UserT, PeakMemory}; + } + // Return the proper exit status. Detect error conditions // so we can return -1 for them and set ErrMsg informatively. int result = 0; -- cgit v1.1