Under HHVM, measure resources for the thread, not calling process
authorOri Livneh <ori@wikimedia.org>
Thu, 4 Sep 2014 23:56:20 +0000 (16:56 -0700)
committerOri Livneh <ori@wikimedia.org>
Wed, 10 Sep 2014 19:03:32 +0000 (12:03 -0700)
As of <https://github.com/facebook/hhvm/commit/0f98cab>, it is possible to
call getrusage( [ int $who = 0 ] ) with $who = 2 to request resource usage
info for the current thread (RUSAGE_THERAD), rather than the calling process
(RUSAGE_SELF). (Earlier versions of HHVM return RUSAGE_SELF data unless $who
is 1.)

PHP5 code can assume that each request is handled in a dedicated subprocess,
but the same is not true of HHVM, which is multi-threaded. Therefore, to get
resource usage data for the current request context, it is necessary to ask
for RUSAGE_THREAD rather than RUSAGE_SELF.

To do this, introduce a new global function in Profiler.php: wfGetRusage().
It is defined there and not in GlobalFunctions.php so that it can be used early
in WebStart.php.

Bug: 70227
Change-Id: Ibe9598ecdfc0f6c434f8b3c7a94f06a7b2fcca23

includes/WebStart.php
includes/parser/ParserOutput.php
includes/profiler/Profiler.php

index e137628..2ae72dc 100644 (file)
@@ -40,8 +40,6 @@ if ( ini_get( 'register_globals' ) ) {
 header( 'X-Content-Type-Options: nosniff' );
 
 $wgRequestTime = microtime( true );
-# getrusage() does not exist on the Microsoft Windows platforms, catching this
-$wgRUstart = function_exists( 'getrusage' ) ? getrusage() : array();
 unset( $IP );
 
 # Valid web server entry point, enable includes.
@@ -60,11 +58,12 @@ if ( $IP === false ) {
        $IP = realpath( '.' ) ?: dirname( __DIR__ );
 }
 
-# Start the autoloader, so that extensions can derive classes from core files
-require_once "$IP/includes/AutoLoader.php";
-
 # Load the profiler
 require_once "$IP/includes/profiler/Profiler.php";
+$wgRUstart = wfGetRusage() ?: array();
+
+# Start the autoloader, so that extensions can derive classes from core files
+require_once "$IP/includes/AutoLoader.php";
 
 # Load up some global defines.
 require_once "$IP/includes/Defines.php";
index f939de3..cca6048 100644 (file)
@@ -733,10 +733,12 @@ class ParserOutput extends CacheTime {
                if ( !$clock || $clock === 'wall' ) {
                        $ret['wall'] = microtime( true );
                }
-               if ( ( !$clock || $clock === 'cpu' ) && function_exists( 'getrusage' ) ) {
-                       $ru = getrusage();
-                       $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
-                       $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
+               if ( !$clock || $clock === 'cpu' ) {
+                       $ru = wfGetRusage();
+                       if ( $ru ) {
+                               $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
+                               $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
+                       }
                }
                return $ret;
        }
index 7b8f340..418b5d4 100644 (file)
  * @file
  * @ingroup Profiler
  * @defgroup Profiler Profiler
- * This file is only included if profiling is enabled
  */
 
+/**
+ * Get system resource usage of current request context.
+ * Invokes the getrusage(2) system call, requesting RUSAGE_SELF if on PHP5
+ * or RUSAGE_THREAD if on HHVM. Returns false if getrusage is not available.
+ *
+ * @since 1.24
+ * @return array|bool Resource usage data or false if no data available.
+ */
+function wfGetRusage() {
+       if ( !function_exists( 'getrusage' ) ) {
+               return false;
+       } elseif ( defined ( 'HHVM_VERSION' ) ) {
+               return getrusage( 2 /* RUSAGE_THREAD */ );
+       } else {
+               return getrusage( 0 /* RUSAGE_SELF */ );
+       }
+}
+
 /**
  * Begin profiling of a function
  * @param string $functionname Name of the function we will profile
@@ -272,10 +289,10 @@ abstract class Profiler {
         */
        protected function getTime( $metric = 'wall' ) {
                if ( $metric === 'cpu' || $metric === 'user' ) {
-                       if ( !function_exists( 'getrusage' ) ) {
+                       $ru = wfGetRusage();
+                       if ( !$ru ) {
                                return 0;
                        }
-                       $ru = getrusage();
                        $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
                        if ( $metric === 'cpu' ) {
                                # This is the time of system calls, added to the user time