Track memory RSS increases in JobRunner
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 11 Jan 2016 21:13:21 +0000 (13:13 -0800)
committerOri.livneh <ori@wikimedia.org>
Tue, 12 Jan 2016 20:41:45 +0000 (20:41 +0000)
Bug: T123284
Change-Id: Ic6b76a722cc8d1e18a42b9779f776850ae4700f4

includes/jobqueue/JobRunner.php

index a6d088f..4ab9f5a 100644 (file)
@@ -251,6 +251,7 @@ class JobRunner implements LoggerAwareInterface {
                $this->debugCallback( $msg );
 
                // Run the job...
+               $rssStart = $this->getMaxRssKb();
                $jobStartTime = microtime( true );
                try {
                        $status = $job->run();
@@ -272,6 +273,7 @@ class JobRunner implements LoggerAwareInterface {
                // Clear out title cache data from prior snapshots
                LinkCache::singleton()->clear();
                $timeMs = intval( ( microtime( true ) - $jobStartTime ) * 1000 );
+               $rssEnd = $this->getMaxRssKb();
 
                // Record how long jobs wait before getting popped
                $readyTs = $job->getReadyTimestamp();
@@ -288,6 +290,10 @@ class JobRunner implements LoggerAwareInterface {
                }
                // Track the execution time for jobs
                $stats->timing( "jobqueue.run.$jType", $timeMs );
+               // Track RSS increases for jobs (in case of memory leaks)
+               if ( $rssStart && $rssEnd ) {
+                       $stats->increment( "jobqueue.rss_delta.$jType", $rssEnd - $rssStart );
+               }
 
                if ( $status === false ) {
                        $msg = $job->toString() . " t=$timeMs error={$error}";
@@ -302,6 +308,15 @@ class JobRunner implements LoggerAwareInterface {
                return array( 'status' => $status, 'error' => $error, 'timeMs' => $timeMs );
        }
 
+       /**
+        * @return int|null Max memory RSS in kilobytes
+        */
+       private function getMaxRssKb() {
+               $info = wfGetRusage() ?: array();
+               // see http://linux.die.net/man/2/getrusage
+               return isset( $info['ru_maxrss'] ) ? (int)$info['ru_maxrss'] : null;
+       }
+
        /**
         * @param Job $job
         * @return int Seconds for this runner to avoid doing more jobs of this type