diff options
author | Serge Pavlov <sepavloff@gmail.com> | 2020-04-23 13:04:52 +0700 |
---|---|---|
committer | Serge Pavlov <sepavloff@gmail.com> | 2020-06-17 13:39:59 +0700 |
commit | 2e613d2ded2c465bd06bd3cac30ffb4576bf72cc (patch) | |
tree | 0d9a6548765470bdd8973de1b86426e9d9b92a5b /llvm/lib/Support/Unix | |
parent | e3fd9dc9734c5775dc6824d0a839702e8d43e7f6 (diff) | |
download | llvm-2e613d2ded2c465bd06bd3cac30ffb4576bf72cc.zip llvm-2e613d2ded2c465bd06bd3cac30ffb4576bf72cc.tar.gz llvm-2e613d2ded2c465bd06bd3cac30ffb4576bf72cc.tar.bz2 |
[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
Diffstat (limited to 'llvm/lib/Support/Unix')
-rw-r--r-- | llvm/lib/Support/Unix/Program.inc | 15 |
1 files changed, 13 insertions, 2 deletions
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<ProcessStatistics> *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<uint64_t>(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; |