23use Wikimedia\ScopedCallback;
55 $this->collateOnly = empty(
$params[
'trace'] );
99 if ( is_array( $this->start ) ) {
100 $totalCpu = max( $this->end[
'cpu'] - $this->start[
'cpu'], 0 );
101 $totalReal = max( $this->end[
'real'] - $this->start[
'real'], 0 );
102 $totalMem = max( $this->end[
'memory'] - $this->start[
'memory'], 0 );
110 foreach ( $this->collated as
$fname => $data ) {
113 'calls' => $data[
'count'],
114 'real' => $data[
'real'] * 1000,
115 '%real' => $totalReal ? 100 * $data[
'real'] / $totalReal : 0,
116 'cpu' => $data[
'cpu'] * 1000,
117 '%cpu' => $totalCpu ? 100 * $data[
'cpu'] / $totalCpu : 0,
118 'memory' => $data[
'memory'],
119 '%memory' => $totalMem ? 100 * $data[
'memory'] / $totalMem : 0,
120 'min_real' => 1000 * $data[
'min_real'],
121 'max_real' => 1000 * $data[
'max_real']
128 'real' => 1000 * $totalReal,
130 'cpu' => 1000 * $totalCpu,
132 'memory' => $totalMem,
134 'min_real' => 1000 * $totalReal,
135 'max_real' => 1000 * $totalReal
148 $this->workStack = [];
149 $this->collated = [];
150 $this->collateDone =
false;
184 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
185 $entry =& $this->collated[
$name];
186 if ( !is_array( $entry ) ) {
188 $this->collated[
$name] =& $entry;
190 $entry[
'cpu'] += $elapsedCpu;
191 $entry[
'real'] += $elapsedReal;
192 $entry[
'memory'] += $memChange > 0 ? $memChange : 0;
194 $entry[
'min_real'] = min( $entry[
'min_real'], $elapsedReal );
195 $entry[
'max_real'] = max( $entry[
'max_real'], $elapsedReal );
207 $this->collateDone =
false;
209 $cpu = $this->
getTime(
'cpu' );
210 $real = $this->
getTime(
'wall' );
211 $memory = memory_get_usage();
213 if ( $this->start ===
null ) {
214 $this->start = [
'cpu' => $cpu,
'real' => $real,
'memory' => $memory ];
217 $this->workStack[] = [
219 count( $this->workStack ),
232 $item = array_pop( $this->workStack );
233 if ( $item ===
null ) {
234 $this->
debugGroup(
'profileerror',
"Profiling error: $functionname" );
237 list( $ofname, , $ortime, $octime, $omem ) = $item;
239 if ( $functionname ===
'close' ) {
240 $message =
"Profile section ended by close(): {$ofname}";
241 $this->
debugGroup(
'profileerror', $message );
242 if ( $this->collateOnly ) {
245 $this->stack[] = [ $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 ];
247 $functionname = $ofname;
248 } elseif ( $ofname !== $functionname ) {
249 $message =
"Profiling error: in({$ofname}), out($functionname)";
250 $this->
debugGroup(
'profileerror', $message );
251 if ( $this->collateOnly ) {
254 $this->stack[] = [ $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 ];
258 $realTime = $this->
getTime(
'wall' );
259 $cpuTime = $this->
getTime(
'cpu' );
260 $memUsage = memory_get_usage();
262 if ( $this->collateOnly ) {
263 $elapsedcpu = $cpuTime - $octime;
264 $elapsedreal = $realTime - $ortime;
265 $memchange = $memUsage - $omem;
266 $this->
updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
268 $this->stack[] = array_merge( $item, [ $realTime, $cpuTime, $memUsage ] );
274 'memory' => $memUsage
284 if ( $this->collateOnly ) {
285 throw new Exception(
"Tree is only available for trace profiling." );
287 return implode(
'', array_map(
288 [ $this,
'getCallTreeLine' ], $this->
remapCallTree( $this->stack )
299 if ( count(
$stack ) < 2 ) {
303 for ( $max = count(
$stack ) - 1; $max > 0; ) {
307 for ( $i = $max - 1; $i >= 0; $i-- ) {
308 if (
$stack[$i][1] > $level ) {
314 $working = $this->
remapCallTree( array_reverse( $working ) );
316 foreach ( $working as $item ) {
322 array_unshift( $outputs,
$output );
325 foreach ( $outputs as
$output ) {
340 list(
$fname, $level, $startreal, , , $endreal ) = $entry;
341 $delta = $endreal - $startreal;
342 $space = str_repeat(
' ', $level );
343 # The ugly double sprintf is to work around a PHP bug,
344 # which has been fixed in recent releases.
345 return sprintf(
"%10s %s %s\n",
346 trim( sprintf(
"%7.3f", $delta * 1000.0 ) ), $space,
$fname );
353 if ( $this->collateDone ) {
356 $this->collateDone =
true;
358 while ( count( $this->workStack ) ) {
362 if ( $this->collateOnly ) {
366 $this->collated = [];
368 # Estimate profiling overhead
370 $profileCount = count( $this->stack );
373 # First, subtract the overhead!
374 $overheadTotal = $overheadMemory = $overheadInternal = [];
375 foreach ( $this->stack as $entry ) {
378 $elapsed = $entry[5] - $entry[2];
379 $memchange = $entry[7] - $entry[4];
381 if (
$fname ===
'-overhead-total' ) {
382 $overheadTotal[] = $elapsed;
383 $overheadMemory[] = max( 0, $memchange );
384 } elseif (
$fname ===
'-overhead-internal' ) {
385 $overheadInternal[] = $elapsed;
388 $overheadTotal = $overheadTotal ?
389 array_sum( $overheadTotal ) / count( $overheadInternal ) : 0;
390 $overheadMemory = $overheadMemory ?
391 array_sum( $overheadMemory ) / count( $overheadInternal ) : 0;
392 $overheadInternal = $overheadInternal ?
393 array_sum( $overheadInternal ) / count( $overheadInternal ) : 0;
396 foreach ( $this->stack as $index => $entry ) {
399 $elapsedCpu = $entry[6] - $entry[3];
400 $elapsedReal = $entry[5] - $entry[2];
401 $memchange = $entry[7] - $entry[4];
404 if ( substr(
$fname, 0, 9 ) !==
'-overhead' ) {
405 # Adjust for profiling overhead (except special values with elapsed=0)
407 $elapsed -= $overheadInternal;
408 $elapsed -= ( $subcalls * $overheadTotal );
409 $memchange -= ( $subcalls * $overheadMemory );
416 $this->collated[
'-overhead-total'][
'count'] = $profileCount;
417 arsort( $this->collated, SORT_NUMERIC );
420 $this->end = $oldEnd;
430 for ( $i = 0; $i < $profileCount; $i++ ) {
448 for ( $i =
$start - 1; $i >= 0 &&
$stack[$i][1] > $level; $i-- ) {
464 protected function getTime( $metric =
'wall' ) {
465 if ( $metric ===
'cpu' || $metric ===
'user' ) {
470 $time = $ru[
'ru_utime.tv_sec'] + $ru[
'ru_utime.tv_usec'] / 1e6;
471 if ( $metric ===
'cpu' ) {
472 # This is the time of system calls, added to the user time
473 # it gives the total CPU time
474 $time += $ru[
'ru_stime.tv_sec'] + $ru[
'ru_stime.tv_usec'] / 1e6;
478 return microtime(
true );
488 if ( function_exists(
'wfDebug' ) ) {
500 if ( function_exists(
'wfDebugLog' ) ) {
522 parent::__construct(
null );
528 $this->profiler->profileOutInternal( $this->section );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetRusage()
Get system resource usage of current request context.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
__construct(SectionProfiler $profiler, $section)
SectionProfiler $profiler
Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle.
array $stack
List of resolved profile calls with start/end data.
remapCallTree(array $stack)
Recursive function the format the current profiling array into a tree.
getCallTreeLine( $entry)
Callback to get a formatted line for the call tree.
profileInInternal( $functionname)
This method should not be called outside SectionProfiler.
array $errorEntry
Cache of a standard broken collation entry.
scopedProfileOut(ScopedCallback &$section)
array $collated
Map of (function name => aggregate data array)
debug( $s)
Add an entry in the debug log file.
getTime( $metric='wall')
Get the initial time of the request, based on getrusage()
bool $collateOnly
Whether to collect the full stack trace or just aggregates.
__construct(array $params=[])
array $workStack
Queue of open profile calls with start data.
array $start
Map of (mem,real,cpu)
debugGroup( $group, $s)
Add an entry in the debug log group.
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getCallTreeReport()
Returns a tree of function calls with their real times.
scopedProfileIn( $section)
calculateOverhead( $profileCount)
Dummy calls to calculate profiling overhead.
profileOutInternal( $functionname)
This method should not be called outside SectionProfiler.
reset()
Clear all of the profiling data for another run.
updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange)
Update the collation entry for a given method name.
array $end
Map of (mem,real,cpu)
calltreeCount( $stack, $start)
Counts the number of profiled function calls sitting under the given point in the call graph.
collateData()
Populate collated data.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
see documentation in includes Linker php for Linker::makeImageLink & $time
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Allows to change the fields on the form that will be generated $name
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section