MediaWiki 1.44.0
SectionProfiler.php
Go to the documentation of this file.
1<?php
22use Psr\Log\LoggerInterface;
23use Wikimedia\ScopedCallback;
24
37 protected $start;
39 protected $end;
41 protected $stack = [];
43 protected $workStack = [];
45 protected $collated = [];
47 protected $collateDone = false;
48
50 protected $errorEntry;
52 protected $logger;
53
57 public function __construct( array $params = [] ) {
58 $this->errorEntry = $this->getErrorEntry();
59 $this->logger = LoggerFactory::getInstance( 'profiler' );
60 }
61
66 public function scopedProfileIn( $section ) {
67 $this->profileInInternal( $section );
68
69 return new SectionProfileCallback( $this, $section );
70 }
71
72 public function scopedProfileOut( ScopedCallback &$section ) {
73 $section = null;
74 }
75
97 public function getFunctionStats() {
98 $this->collateData();
99
100 if ( is_array( $this->start ) && is_array( $this->end ) ) {
101 $totalCpu = max( $this->end['cpu'] - $this->start['cpu'], 0 );
102 $totalReal = max( $this->end['real'] - $this->start['real'], 0 );
103 $totalMem = max( $this->end['memory'] - $this->start['memory'], 0 );
104 } else {
105 $totalCpu = 0;
106 $totalReal = 0;
107 $totalMem = 0;
108 }
109
110 $profile = [];
111 foreach ( $this->collated as $fname => $data ) {
112 $profile[] = [
113 'name' => $fname,
114 'calls' => $data['count'],
115 'real' => $data['real'] * 1000,
116 '%real' => $totalReal ? 100 * $data['real'] / $totalReal : 0,
117 'cpu' => $data['cpu'] * 1000,
118 '%cpu' => $totalCpu ? 100 * $data['cpu'] / $totalCpu : 0,
119 'memory' => $data['memory'],
120 '%memory' => $totalMem ? 100 * $data['memory'] / $totalMem : 0,
121 'min_real' => 1000 * $data['min_real'],
122 'max_real' => 1000 * $data['max_real']
123 ];
124 }
125
126 $profile[] = [
127 'name' => '-total',
128 'calls' => 1,
129 'real' => 1000 * $totalReal,
130 '%real' => 100,
131 'cpu' => 1000 * $totalCpu,
132 '%cpu' => 100,
133 'memory' => $totalMem,
134 '%memory' => 100,
135 'min_real' => 1000 * $totalReal,
136 'max_real' => 1000 * $totalReal
137 ];
138
139 return $profile;
140 }
141
145 public function reset() {
146 $this->start = null;
147 $this->end = null;
148 $this->stack = [];
149 $this->workStack = [];
150 $this->collated = [];
151 $this->collateDone = false;
152 }
153
157 protected function getZeroEntry() {
158 return [
159 'cpu' => 0.0,
160 'real' => 0.0,
161 'memory' => 0,
162 'count' => 0,
163 'min_real' => 0.0,
164 'max_real' => 0.0
165 ];
166 }
167
171 protected function getErrorEntry() {
172 $entry = $this->getZeroEntry();
173 $entry['count'] = 1;
174 return $entry;
175 }
176
185 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
186 $entry =& $this->collated[$name];
187 if ( !is_array( $entry ) ) {
188 $entry = $this->getZeroEntry();
189 $this->collated[$name] =& $entry;
190 }
191 $entry['cpu'] += $elapsedCpu;
192 $entry['real'] += $elapsedReal;
193 $entry['memory'] += $memChange > 0 ? $memChange : 0;
194 $entry['count']++;
195 $entry['min_real'] = min( $entry['min_real'], $elapsedReal );
196 $entry['max_real'] = max( $entry['max_real'], $elapsedReal );
197 }
198
204 public function profileInInternal( $functionname ) {
205 // Once the data is collated for reports, any future calls
206 // should clear the collation cache so the next report will
207 // reflect them. This matters when trace mode is used.
208 $this->collateDone = false;
209
210 $cpu = $this->getTime( 'cpu' );
211 $real = $this->getTime( 'wall' );
212 $memory = memory_get_usage();
213
214 if ( $this->start === null ) {
215 $this->start = [ 'cpu' => $cpu, 'real' => $real, 'memory' => $memory ];
216 }
217
218 $this->workStack[] = [
219 $functionname,
220 count( $this->workStack ),
221 $real,
222 $cpu,
223 $memory
224 ];
225 }
226
232 public function profileOutInternal( $functionname ) {
233 $item = array_pop( $this->workStack );
234 if ( $item === null ) {
235 $this->logger->error( "Profiling error: $functionname" );
236 return;
237 }
238 [ $ofname, /* $ocount */, $ortime, $octime, $omem ] = $item;
239
240 if ( $functionname === 'close' ) {
241 $message = "Profile section ended by close(): {$ofname}";
242 $this->logger->error( $message );
243 $this->collated[$message] = $this->errorEntry;
244 $functionname = $ofname;
245 } elseif ( $ofname !== $functionname ) {
246 $message = "Profiling error: in({$ofname}), out($functionname)";
247 $this->logger->error( $message );
248 $this->collated[$message] = $this->errorEntry;
249 }
250
251 $realTime = $this->getTime( 'wall' );
252 $cpuTime = $this->getTime( 'cpu' );
253 $memUsage = memory_get_usage();
254
255 $elapsedcpu = $cpuTime - $octime;
256 $elapsedreal = $realTime - $ortime;
257 $memchange = $memUsage - $omem;
258 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
259
260 $this->end = [
261 'cpu' => $cpuTime,
262 'real' => $realTime,
263 'memory' => $memUsage
264 ];
265 }
266
270 protected function collateData() {
271 if ( $this->collateDone ) {
272 return;
273 }
274 $this->collateDone = true;
275 // Close opened profiling sections
276 while ( count( $this->workStack ) ) {
277 $this->profileOutInternal( 'close' );
278 }
279 }
280
291 protected function getTime( $metric = 'wall' ) {
292 if ( $metric === 'cpu' || $metric === 'user' ) {
293 $ru = getrusage( 0 /* RUSAGE_SELF */ );
294 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
295 if ( $metric === 'cpu' ) {
296 # This is the time of system calls, added to the user time
297 # it gives the total CPU time
298 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
299 }
300 return $time;
301 } else {
302 return microtime( true );
303 }
304 }
305}
Create PSR-3 logger objects.
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.
LoggerInterface $logger
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.