Commit 31751bbf authored by Victor Loh's avatar Victor Loh Committed by Facebook Github Bot

Add pollWithRusage to Subprocess

Summary:
I was looking for a way to get rusage of a particular pid because
getrusage isn't good enough since it records the rusage of all the children
that has been terminated (and waited for). Even though wait4 is marked as
deprecated, wait3 (the cousin of wait4) is still being used in places like
time. Furthermore, there is no suitable replacement to get rusage with wait*.

Reviewed By: yfeldblum

Differential Revision: D5008084

fbshipit-source-id: 6e511ebec7464d21309e5112aca95083e9307ea1
parent 646be295
...@@ -22,8 +22,6 @@ ...@@ -22,8 +22,6 @@
#if __linux__ #if __linux__
#include <sys/prctl.h> #include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#endif #endif
#include <fcntl.h> #include <fcntl.h>
...@@ -45,6 +43,7 @@ ...@@ -45,6 +43,7 @@
#include <folly/io/Cursor.h> #include <folly/io/Cursor.h>
#include <folly/portability/Sockets.h> #include <folly/portability/Sockets.h>
#include <folly/portability/Stdlib.h> #include <folly/portability/Stdlib.h>
#include <folly/portability/SysSyscall.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
constexpr int kExecFailure = 127; constexpr int kExecFailure = 127;
...@@ -546,11 +545,11 @@ void Subprocess::readChildErrorPipe(int pfd, const char* executable) { ...@@ -546,11 +545,11 @@ void Subprocess::readChildErrorPipe(int pfd, const char* executable) {
throw SubprocessSpawnError(executable, info.errCode, info.errnoValue); throw SubprocessSpawnError(executable, info.errCode, info.errnoValue);
} }
ProcessReturnCode Subprocess::poll() { ProcessReturnCode Subprocess::poll(struct rusage* ru) {
returnCode_.enforce(ProcessReturnCode::RUNNING); returnCode_.enforce(ProcessReturnCode::RUNNING);
DCHECK_GT(pid_, 0); DCHECK_GT(pid_, 0);
int status; int status;
pid_t found = ::waitpid(pid_, &status, WNOHANG); pid_t found = ::wait4(pid_, &status, WNOHANG, ru);
// The spec guarantees that EINTR does not occur with WNOHANG, so the only // The spec guarantees that EINTR does not occur with WNOHANG, so the only
// two remaining errors are ECHILD (other code reaped the child?), or // two remaining errors are ECHILD (other code reaped the child?), or
// EINVAL (cosmic rays?), both of which merit an abort: // EINVAL (cosmic rays?), both of which merit an abort:
......
...@@ -117,6 +117,7 @@ ...@@ -117,6 +117,7 @@
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/gen/String.h> #include <folly/gen/String.h>
#include <folly/io/IOBufQueue.h> #include <folly/io/IOBufQueue.h>
#include <folly/portability/SysResource.h>
namespace folly { namespace folly {
...@@ -509,7 +510,7 @@ class Subprocess { ...@@ -509,7 +510,7 @@ class Subprocess {
* e.g. if you wait for the underlying process without going through this * e.g. if you wait for the underlying process without going through this
* Subprocess instance. * Subprocess instance.
*/ */
ProcessReturnCode poll(); ProcessReturnCode poll(struct rusage* ru = nullptr);
/** /**
* Poll the child's status. If the process is still running, return false. * Poll the child's status. If the process is still running, return false.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment