22use Psr\Log\LoggerInterface;
23use Wikimedia\ScopedCallback;
59 $this->logger = LoggerFactory::getInstance(
'profiler' );
103 if ( is_array( $this->start ) && is_array( $this->end ) ) {
104 $totalCpu = max( $this->end[
'cpu'] - $this->start[
'cpu'], 0 );
105 $totalReal = max( $this->end[
'real'] - $this->start[
'real'], 0 );
106 $totalMem = max( $this->end[
'memory'] - $this->start[
'memory'], 0 );
114 foreach ( $this->collated as $fname => $data ) {
117 'calls' => $data[
'count'],
118 'real' => $data[
'real'] * 1000,
119 '%real' => $totalReal ? 100 * $data[
'real'] / $totalReal : 0,
120 'cpu' => $data[
'cpu'] * 1000,
121 '%cpu' => $totalCpu ? 100 * $data[
'cpu'] / $totalCpu : 0,
122 'memory' => $data[
'memory'],
123 '%memory' => $totalMem ? 100 * $data[
'memory'] / $totalMem : 0,
124 'min_real' => 1000 * $data[
'min_real'],
125 'max_real' => 1000 * $data[
'max_real']
132 'real' => 1000 * $totalReal,
134 'cpu' => 1000 * $totalCpu,
136 'memory' => $totalMem,
138 'min_real' => 1000 * $totalReal,
139 'max_real' => 1000 * $totalReal
152 $this->workStack = [];
153 $this->collated = [];
154 $this->collateDone =
false;
188 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
189 $entry =& $this->collated[$name];
190 if ( !is_array( $entry ) ) {
192 $this->collated[$name] =& $entry;
194 $entry[
'cpu'] += $elapsedCpu;
195 $entry[
'real'] += $elapsedReal;
196 $entry[
'memory'] += $memChange > 0 ? $memChange : 0;
198 $entry[
'min_real'] = min( $entry[
'min_real'], $elapsedReal );
199 $entry[
'max_real'] = max( $entry[
'max_real'], $elapsedReal );
211 $this->collateDone =
false;
213 $cpu = $this->
getTime(
'cpu' );
214 $real = $this->
getTime(
'wall' );
215 $memory = memory_get_usage();
217 if ( $this->start ===
null ) {
218 $this->start = [
'cpu' => $cpu,
'real' => $real,
'memory' => $memory ];
221 $this->workStack[] = [
223 count( $this->workStack ),
236 $item = array_pop( $this->workStack );
237 if ( $item ===
null ) {
238 $this->logger->error(
"Profiling error: $functionname" );
241 [ $ofname, , $ortime, $octime, $omem ] = $item;
243 if ( $functionname ===
'close' ) {
244 $message =
"Profile section ended by close(): {$ofname}";
245 $this->logger->error( $message );
247 $functionname = $ofname;
248 } elseif ( $ofname !== $functionname ) {
249 $message =
"Profiling error: in({$ofname}), out($functionname)";
250 $this->logger->error( $message );
254 $realTime = $this->
getTime(
'wall' );
255 $cpuTime = $this->
getTime(
'cpu' );
256 $memUsage = memory_get_usage();
258 $elapsedcpu = $cpuTime - $octime;
259 $elapsedreal = $realTime - $ortime;
260 $memchange = $memUsage - $omem;
261 $this->
updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
266 'memory' => $memUsage
274 if ( $this->collateDone ) {
277 $this->collateDone =
true;
279 while ( count( $this->workStack ) ) {
294 protected function getTime( $metric =
'wall' ) {
295 if ( $metric ===
'cpu' || $metric ===
'user' ) {
296 $ru = getrusage( 0 );
297 $time = $ru[
'ru_utime.tv_sec'] + $ru[
'ru_utime.tv_usec'] / 1e6;
298 if ( $metric ===
'cpu' ) {
299 # This is the time of system calls, added to the user time
300 # it gives the total CPU time
301 $time += $ru[
'ru_stime.tv_sec'] + $ru[
'ru_stime.tv_usec'] / 1e6;
305 return microtime(
true );
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
Arbitrary section name based PHP profiling.
array[] $collated
Map of (function name => aggregate data array)
profileInInternal( $functionname)
This method should not be called outside SectionProfiler.
array $errorEntry
Cache of a standard broken collation entry.
scopedProfileOut(ScopedCallback &$section)
getTime( $metric='wall')
Get the initial time of the request, based on getrusage()
array null $start
Map of (mem,real,cpu)
__construct(array $params=[])
array $workStack
Queue of open profile calls with start data.
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
array null $end
Map of (mem,real,cpu)
scopedProfileIn( $section)
profileOutInternal( $functionname)
This method should not be called outside SectionProfiler.
array[] $stack
List of resolved profile calls with start/end data.
reset()
Clear all of the profiling data for another run.
updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange)
Update the collation entry for a given method name.
collateData()
Populate collated data.