2006-01-07 13:09:30 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2014-11-04 17:40:40 +00:00
|
|
|
* Base class for profiling.
|
2012-04-28 18:41:55 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Profiler
|
2012-04-28 18:41:55 +00:00
|
|
|
* @defgroup Profiler Profiler
|
|
|
|
|
*/
|
|
|
|
|
|
2014-09-04 23:56:20 +00:00
|
|
|
/**
|
2014-11-04 17:40:40 +00:00
|
|
|
* Profiler base class that defines the interface and some trivial
|
|
|
|
|
* functionality
|
2014-04-14 20:30:58 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Profiler
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2014-04-14 20:30:58 +00:00
|
|
|
abstract class Profiler {
|
2014-04-09 22:43:12 +00:00
|
|
|
/** @var string|bool Profiler ID for bucketing data */
|
2014-11-05 19:03:20 +00:00
|
|
|
protected $profileID = false;
|
2014-04-09 22:43:12 +00:00
|
|
|
/** @var bool Whether MediaWiki is in a SkinTemplate output context */
|
2014-11-05 19:03:20 +00:00
|
|
|
protected $templated = false;
|
2014-11-14 18:58:07 +00:00
|
|
|
/** @var array All of the params passed from $wgProfiler */
|
|
|
|
|
protected $params = array();
|
2015-04-01 23:30:16 +00:00
|
|
|
/** @var IContextSource Current request context */
|
|
|
|
|
protected $context = null;
|
2014-04-14 20:30:58 +00:00
|
|
|
/** @var TransactionProfiler */
|
|
|
|
|
protected $trxProfiler;
|
2015-02-12 20:53:39 +00:00
|
|
|
/** @var Profiler */
|
|
|
|
|
private static $instance = null;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2014-04-09 22:43:12 +00:00
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( array $params ) {
|
2011-08-10 15:35:03 +00:00
|
|
|
if ( isset( $params['profileID'] ) ) {
|
2014-11-05 19:03:20 +00:00
|
|
|
$this->profileID = $params['profileID'];
|
2011-08-10 15:35:03 +00:00
|
|
|
}
|
2014-11-14 18:58:07 +00:00
|
|
|
$this->params = $params;
|
2014-04-14 20:30:58 +00:00
|
|
|
$this->trxProfiler = new TransactionProfiler();
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
2005-01-28 00:50:58 +00:00
|
|
|
|
2011-04-16 19:00:54 +00:00
|
|
|
/**
|
|
|
|
|
* Singleton
|
|
|
|
|
* @return Profiler
|
|
|
|
|
*/
|
2014-04-14 20:30:58 +00:00
|
|
|
final public static function instance() {
|
2015-02-12 20:53:39 +00:00
|
|
|
if ( self::$instance === null ) {
|
2015-04-01 23:30:16 +00:00
|
|
|
global $wgProfiler, $wgProfileLimit;
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'class' => 'ProfilerStub',
|
|
|
|
|
'sampling' => 1,
|
|
|
|
|
'threshold' => $wgProfileLimit,
|
|
|
|
|
'output' => array(),
|
|
|
|
|
);
|
2013-04-20 15:38:24 +00:00
|
|
|
if ( is_array( $wgProfiler ) ) {
|
2015-04-01 23:30:16 +00:00
|
|
|
$params = array_merge( $params, $wgProfiler );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
|
|
|
|
|
if ( PHP_SAPI === 'cli' || !$inSample ) {
|
|
|
|
|
$params['class'] = 'ProfilerStub';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_array( $params['output'] ) ) {
|
|
|
|
|
$params['output'] = array( $params['output'] );
|
2011-04-16 19:00:54 +00:00
|
|
|
}
|
2015-04-01 23:30:16 +00:00
|
|
|
|
|
|
|
|
self::$instance = new $params['class']( $params );
|
2011-04-16 19:00:54 +00:00
|
|
|
}
|
2015-02-12 20:53:39 +00:00
|
|
|
return self::$instance;
|
2011-04-16 19:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-21 22:54:57 +00:00
|
|
|
/**
|
|
|
|
|
* Replace the current profiler with $profiler if no non-stub profiler is set
|
|
|
|
|
*
|
|
|
|
|
* @param Profiler $profiler
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
final public static function replaceStubInstance( Profiler $profiler ) {
|
2015-02-12 20:53:39 +00:00
|
|
|
if ( self::$instance && !( self::$instance instanceof ProfilerStub ) ) {
|
2014-11-21 22:54:57 +00:00
|
|
|
throw new MWException( 'Could not replace non-stub profiler instance.' );
|
|
|
|
|
} else {
|
2015-02-12 20:53:39 +00:00
|
|
|
self::$instance = $profiler;
|
2014-11-21 22:54:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-09 22:43:12 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $id
|
|
|
|
|
*/
|
2011-04-16 19:30:50 +00:00
|
|
|
public function setProfileID( $id ) {
|
2014-11-05 19:03:20 +00:00
|
|
|
$this->profileID = $id;
|
2011-04-16 19:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-09 22:43:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-04-16 19:30:50 +00:00
|
|
|
public function getProfileID() {
|
2014-11-05 19:03:20 +00:00
|
|
|
if ( $this->profileID === false ) {
|
2011-04-16 19:30:50 +00:00
|
|
|
return wfWikiID();
|
|
|
|
|
} else {
|
2014-11-05 19:03:20 +00:00
|
|
|
return $this->profileID;
|
2011-04-16 19:30:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-01 23:30:16 +00:00
|
|
|
/**
|
|
|
|
|
* Sets the context for this Profiler
|
|
|
|
|
*
|
|
|
|
|
* @param IContextSource $context
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
public function setContext( $context ) {
|
|
|
|
|
$this->context = $context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the context for this Profiler
|
|
|
|
|
*
|
|
|
|
|
* @return IContextSource
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
public function getContext() {
|
|
|
|
|
if ( $this->context ) {
|
|
|
|
|
return $this->context;
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( __METHOD__ . " called and \$context is null. " .
|
|
|
|
|
"Return RequestContext::getMain(); for sanity\n" );
|
|
|
|
|
return RequestContext::getMain();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 09:15:30 +00:00
|
|
|
// Kept BC for now, remove when possible
|
2015-06-17 13:28:51 +00:00
|
|
|
public function profileIn( $functionname ) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function profileOut( $functionname ) {
|
|
|
|
|
}
|
2015-01-09 09:15:30 +00:00
|
|
|
|
2014-11-22 01:13:27 +00:00
|
|
|
/**
|
|
|
|
|
* Mark the start of a custom profiling frame (e.g. DB queries).
|
|
|
|
|
* The frame ends when the result of this method falls out of scope.
|
|
|
|
|
*
|
|
|
|
|
* @param string $section
|
|
|
|
|
* @return ScopedCallback|null
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
abstract public function scopedProfileIn( $section );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ScopedCallback $section
|
|
|
|
|
*/
|
2015-03-26 03:50:41 +00:00
|
|
|
public function scopedProfileOut( ScopedCallback &$section = null ) {
|
2014-11-22 01:13:27 +00:00
|
|
|
$section = null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:11:19 +00:00
|
|
|
/**
|
2014-11-05 23:05:22 +00:00
|
|
|
* @return TransactionProfiler
|
|
|
|
|
* @since 1.25
|
2013-06-27 19:11:19 +00:00
|
|
|
*/
|
2014-11-05 23:05:22 +00:00
|
|
|
public function getTransactionProfiler() {
|
|
|
|
|
return $this->trxProfiler;
|
2013-06-27 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-14 20:30:58 +00:00
|
|
|
/**
|
|
|
|
|
* Close opened profiling sections
|
|
|
|
|
*/
|
|
|
|
|
abstract public function close();
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-01 23:30:16 +00:00
|
|
|
* Get all usable outputs.
|
2014-11-14 18:58:07 +00:00
|
|
|
*
|
2014-11-18 19:12:07 +00:00
|
|
|
* @throws MWException
|
2015-04-01 23:30:16 +00:00
|
|
|
* @return array Array of ProfilerOutput instances.
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
private function getOutputs() {
|
|
|
|
|
$outputs = array();
|
|
|
|
|
foreach ( $this->params['output'] as $outputType ) {
|
2015-04-02 17:46:57 +00:00
|
|
|
// The class may be specified as either the full class name (for
|
|
|
|
|
// example, 'ProfilerOutputUdp') or (for backward compatibility)
|
|
|
|
|
// the trailing portion of the class name (for example, 'udp').
|
|
|
|
|
$outputClass = strpos( $outputType, 'ProfilerOutput' ) === false
|
|
|
|
|
? 'ProfilerOutput' . ucfirst( $outputType )
|
|
|
|
|
: $outputType;
|
|
|
|
|
if ( !class_exists( $outputClass ) ) {
|
2015-04-01 23:30:16 +00:00
|
|
|
throw new MWException( "'$outputType' is an invalid output type" );
|
|
|
|
|
}
|
|
|
|
|
$outputInstance = new $outputClass( $this, $this->params );
|
|
|
|
|
if ( $outputInstance->canUse() ) {
|
|
|
|
|
$outputs[] = $outputInstance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $outputs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log the data to some store or even the page output
|
|
|
|
|
*
|
2014-11-14 18:58:07 +00:00
|
|
|
* @since 1.25
|
2014-04-14 20:30:58 +00:00
|
|
|
*/
|
2014-11-14 18:58:07 +00:00
|
|
|
public function logData() {
|
2015-04-01 23:30:16 +00:00
|
|
|
$request = $this->getContext()->getRequest();
|
2014-11-14 18:58:07 +00:00
|
|
|
|
2015-04-01 23:30:16 +00:00
|
|
|
$timeElapsed = $request->getElapsedTime();
|
|
|
|
|
$timeElapsedThreshold = $this->params['threshold'];
|
|
|
|
|
if ( $timeElapsed <= $timeElapsedThreshold ) {
|
2014-11-14 18:58:07 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-01 23:30:16 +00:00
|
|
|
$outputs = $this->getOutputs();
|
|
|
|
|
if ( !$outputs ) {
|
|
|
|
|
return;
|
2014-11-14 18:58:07 +00:00
|
|
|
}
|
2015-04-01 23:30:16 +00:00
|
|
|
|
|
|
|
|
$stats = $this->getFunctionStats();
|
|
|
|
|
foreach ( $outputs as $output ) {
|
|
|
|
|
$output->log( $stats );
|
2014-11-14 18:58:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 23:32:20 +00:00
|
|
|
/**
|
|
|
|
|
* Output current data to the page output if configured to do so
|
|
|
|
|
*
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
public function logDataPageOutputOnly() {
|
|
|
|
|
foreach ( $this->getOutputs() as $output ) {
|
|
|
|
|
if ( $output instanceof ProfilerOutputText ) {
|
|
|
|
|
$stats = $this->getFunctionStats();
|
|
|
|
|
$output->log( $stats );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 18:58:07 +00:00
|
|
|
/**
|
|
|
|
|
* Get the content type sent out to the client.
|
|
|
|
|
* Used for profilers that output instead of store data.
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
public function getContentType() {
|
|
|
|
|
foreach ( headers_list() as $header ) {
|
|
|
|
|
if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
|
|
|
|
|
return $m[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-04-14 20:30:58 +00:00
|
|
|
|
2010-02-26 08:53:06 +00:00
|
|
|
/**
|
|
|
|
|
* Mark this call as templated or not
|
2010-07-01 20:45:21 +00:00
|
|
|
*
|
2014-04-19 20:22:20 +00:00
|
|
|
* @param bool $t
|
2010-02-26 08:53:06 +00:00
|
|
|
*/
|
2014-04-09 22:43:12 +00:00
|
|
|
public function setTemplated( $t ) {
|
2014-11-05 19:03:20 +00:00
|
|
|
$this->templated = $t;
|
2010-02-26 08:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-06 16:03:03 +00:00
|
|
|
/**
|
2014-11-14 18:58:07 +00:00
|
|
|
* Was this call as templated or not
|
2011-04-21 16:31:02 +00:00
|
|
|
*
|
2014-11-14 18:58:07 +00:00
|
|
|
* @return bool
|
2008-05-06 16:03:03 +00:00
|
|
|
*/
|
2014-11-14 18:58:07 +00:00
|
|
|
public function getTemplated() {
|
|
|
|
|
return $this->templated;
|
|
|
|
|
}
|
2005-01-28 00:50:58 +00:00
|
|
|
|
2008-05-06 16:03:03 +00:00
|
|
|
/**
|
2014-11-14 18:58:07 +00:00
|
|
|
* Get the aggregated inclusive profiling data for each method
|
2014-10-27 04:27:59 +00:00
|
|
|
*
|
2014-11-14 18:58:07 +00:00
|
|
|
* The percent time for each time is based on the current "total" time
|
|
|
|
|
* used is based on all methods so far. This method can therefore be
|
|
|
|
|
* called several times in between several profiling calls without the
|
|
|
|
|
* delays in usage of the profiler skewing the results. A "-total" entry
|
|
|
|
|
* is always included in the results.
|
2012-03-28 18:42:03 +00:00
|
|
|
*
|
2014-11-14 18:58:07 +00:00
|
|
|
* When a call chain involves a method invoked within itself, any
|
|
|
|
|
* entries for the cyclic invocation should be be demarked with "@".
|
|
|
|
|
* This makes filtering them out easier and follows the xhprof style.
|
|
|
|
|
*
|
|
|
|
|
* @return array List of method entries arrays, each having:
|
2014-12-08 21:36:19 +00:00
|
|
|
* - name : method name
|
|
|
|
|
* - calls : the number of invoking calls
|
2015-06-23 03:31:22 +00:00
|
|
|
* - real : real time elapsed (ms)
|
2014-12-08 21:36:19 +00:00
|
|
|
* - %real : percent real time
|
2015-06-23 03:31:22 +00:00
|
|
|
* - cpu : CPU time elapsed (ms)
|
2014-12-08 21:36:19 +00:00
|
|
|
* - %cpu : percent CPU time
|
|
|
|
|
* - memory : memory used (bytes)
|
|
|
|
|
* - %memory : percent memory used
|
|
|
|
|
* - min_real : min real time in a call (ms)
|
|
|
|
|
* - max_real : max real time in a call (ms)
|
2014-11-14 18:58:07 +00:00
|
|
|
* @since 1.25
|
2012-03-28 18:42:03 +00:00
|
|
|
*/
|
2014-11-14 18:58:07 +00:00
|
|
|
abstract public function getFunctionStats();
|
2005-07-25 07:00:20 +00:00
|
|
|
|
2012-03-28 18:42:03 +00:00
|
|
|
/**
|
2014-11-14 18:58:07 +00:00
|
|
|
* Returns a profiling output to be stored in debug file
|
2012-03-28 18:42:03 +00:00
|
|
|
*
|
2014-11-14 18:58:07 +00:00
|
|
|
* @return string
|
2012-03-28 18:42:03 +00:00
|
|
|
*/
|
2014-11-14 18:58:07 +00:00
|
|
|
abstract public function getOutput();
|
2014-04-14 20:30:58 +00:00
|
|
|
}
|