From 6131a6d11b814eed96a42888d68efff5ff0d44cd Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 12 Sep 2016 02:47:06 -0700 Subject: [PATCH] Improve compatibility checks in WaitConditionLoop * Check if getrusage() is not defined. * Assume worse-case CPU usage in such cases to avoid spin loops. * Move these checks to the constructor so they only happen once. Change-Id: I180629c17b3323e8841a25c2d920390116ae6b74 --- includes/libs/WaitConditionLoop.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/includes/libs/WaitConditionLoop.php b/includes/libs/WaitConditionLoop.php index 3339eb3108..969e86e89c 100644 --- a/includes/libs/WaitConditionLoop.php +++ b/includes/libs/WaitConditionLoop.php @@ -34,6 +34,8 @@ class WaitConditionLoop { private $timeout; /** @var float Seconds */ private $lastWaitTime; + /** @var integer|null */ + private $rusageMode; const CONDITION_REACHED = 1; const CONDITION_CONTINUE = 0; // evaluates as falsey @@ -50,6 +52,12 @@ class WaitConditionLoop { $this->condition = $condition; $this->timeout = $timeout; $this->busyCallbacks =& $busyCallbacks; + + if ( defined( 'HHVM_VERSION' ) && PHP_OS === 'Linux' ) { + $this->rusageMode = 2; // RUSAGE_THREAD + } elseif ( function_exists( 'getrusage' ) ) { + $this->rusageMode = 0; // RUSAGE_SELF + } } /** @@ -139,18 +147,14 @@ class WaitConditionLoop { * @return float Returns 0.0 if not supported (Windows on PHP < 7) */ protected function getCpuTime() { - $time = 0.0; - - if ( defined( 'HHVM_VERSION' ) && PHP_OS === 'Linux' ) { - $ru = getrusage( 2 /* RUSAGE_THREAD */ ); - } else { - $ru = getrusage( 0 /* RUSAGE_SELF */ ); - } - if ( $ru ) { - $time += $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6; - $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6; + if ( $this->rusageMode === null ) { + return microtime( true ); // assume worst case (all time is CPU) } + $ru = getrusage( $this->rusageMode ); + $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6; + $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6; + return $time; } -- 2.20.1