MediaWiki master
SectionProfiler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Profiler;
8
10use Psr\Log\LoggerInterface;
11
24 protected $start;
26 protected $end;
28 protected $stack = [];
30 protected $workStack = [];
32 protected $collated = [];
34 protected $collateDone = false;
35
37 protected $errorEntry;
39 protected $logger;
40
44 public function __construct( array $params = [] ) {
45 $this->errorEntry = $this->getErrorEntry();
46 $this->logger = LoggerFactory::getInstance( 'profiler' );
47 }
48
52 #[\NoDiscard]
53 public function scopedProfileIn( $section ): ?SectionProfileCallback {
54 $this->profileInInternal( $section );
55
56 return new SectionProfileCallback( $this, $section );
57 }
58
59 public function scopedProfileOut( ?SectionProfileCallback &$section ) {
60 $section = null;
61 }
62
84 public function getFunctionStats() {
85 $this->collateData();
86
87 if ( is_array( $this->start ) && is_array( $this->end ) ) {
88 $totalCpu = max( $this->end['cpu'] - $this->start['cpu'], 0 );
89 $totalReal = max( $this->end['real'] - $this->start['real'], 0 );
90 $totalMem = max( $this->end['memory'] - $this->start['memory'], 0 );
91 } else {
92 $totalCpu = 0;
93 $totalReal = 0;
94 $totalMem = 0;
95 }
96
97 $profile = [];
98 foreach ( $this->collated as $fname => $data ) {
99 $profile[] = [
100 'name' => $fname,
101 'calls' => $data['count'],
102 'real' => $data['real'] * 1000,
103 '%real' => $totalReal ? 100 * $data['real'] / $totalReal : 0,
104 'cpu' => $data['cpu'] * 1000,
105 '%cpu' => $totalCpu ? 100 * $data['cpu'] / $totalCpu : 0,
106 'memory' => $data['memory'],
107 '%memory' => $totalMem ? 100 * $data['memory'] / $totalMem : 0,
108 'min_real' => 1000 * $data['min_real'],
109 'max_real' => 1000 * $data['max_real']
110 ];
111 }
112
113 $profile[] = [
114 'name' => '-total',
115 'calls' => 1,
116 'real' => 1000 * $totalReal,
117 '%real' => 100,
118 'cpu' => 1000 * $totalCpu,
119 '%cpu' => 100,
120 'memory' => $totalMem,
121 '%memory' => 100,
122 'min_real' => 1000 * $totalReal,
123 'max_real' => 1000 * $totalReal
124 ];
125
126 return $profile;
127 }
128
132 public function reset() {
133 $this->start = null;
134 $this->end = null;
135 $this->stack = [];
136 $this->workStack = [];
137 $this->collated = [];
138 $this->collateDone = false;
139 }
140
144 protected function getZeroEntry() {
145 return [
146 'cpu' => 0.0,
147 'real' => 0.0,
148 'memory' => 0,
149 'count' => 0,
150 'min_real' => 0.0,
151 'max_real' => 0.0
152 ];
153 }
154
158 protected function getErrorEntry() {
159 $entry = $this->getZeroEntry();
160 $entry['count'] = 1;
161 return $entry;
162 }
163
172 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
173 $entry =& $this->collated[$name];
174 if ( !is_array( $entry ) ) {
175 $entry = $this->getZeroEntry();
176 $this->collated[$name] =& $entry;
177 }
178 $entry['cpu'] += $elapsedCpu;
179 $entry['real'] += $elapsedReal;
180 $entry['memory'] += $memChange > 0 ? $memChange : 0;
181 $entry['count']++;
182 $entry['min_real'] = min( $entry['min_real'], $elapsedReal );
183 $entry['max_real'] = max( $entry['max_real'], $elapsedReal );
184 }
185
191 public function profileInInternal( $functionname ) {
192 // Once the data is collated for reports, any future calls
193 // should clear the collation cache so the next report will
194 // reflect them. This matters when trace mode is used.
195 $this->collateDone = false;
196
197 $cpu = $this->getTime( 'cpu' );
198 $real = $this->getTime( 'wall' );
199 $memory = memory_get_usage();
200
201 if ( $this->start === null ) {
202 $this->start = [ 'cpu' => $cpu, 'real' => $real, 'memory' => $memory ];
203 }
204
205 $this->workStack[] = [
206 $functionname,
207 count( $this->workStack ),
208 $real,
209 $cpu,
210 $memory
211 ];
212 }
213
219 public function profileOutInternal( $functionname ) {
220 $item = array_pop( $this->workStack );
221 if ( $item === null ) {
222 $this->logger->error( "Profiling error: $functionname" );
223 return;
224 }
225 [ $ofname, /* $ocount */, $ortime, $octime, $omem ] = $item;
226
227 if ( $functionname === 'close' ) {
228 $message = "Profile section ended by close(): {$ofname}";
229 $this->logger->error( $message );
230 $this->collated[$message] = $this->errorEntry;
231 $functionname = $ofname;
232 } elseif ( $ofname !== $functionname ) {
233 $message = "Profiling error: in({$ofname}), out($functionname)";
234 $this->logger->error( $message );
235 $this->collated[$message] = $this->errorEntry;
236 }
237
238 $realTime = $this->getTime( 'wall' );
239 $cpuTime = $this->getTime( 'cpu' );
240 $memUsage = memory_get_usage();
241
242 $elapsedcpu = $cpuTime - $octime;
243 $elapsedreal = $realTime - $ortime;
244 $memchange = $memUsage - $omem;
245 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
246
247 $this->end = [
248 'cpu' => $cpuTime,
249 'real' => $realTime,
250 'memory' => $memUsage
251 ];
252 }
253
257 protected function collateData() {
258 if ( $this->collateDone ) {
259 return;
260 }
261 $this->collateDone = true;
262 // Close opened profiling sections
263 while ( count( $this->workStack ) ) {
264 $this->profileOutInternal( 'close' );
265 }
266 }
267
278 protected function getTime( $metric = 'wall' ) {
279 if ( $metric === 'cpu' || $metric === 'user' ) {
280 $ru = getrusage( 0 /* RUSAGE_SELF */ );
281 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
282 if ( $metric === 'cpu' ) {
283 # This is the time of system calls, added to the user time
284 # it gives the total CPU time
285 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
286 }
287 return $time;
288 } else {
289 return microtime( true );
290 }
291 }
292}
293
295class_alias( SectionProfiler::class, 'SectionProfiler' );
Create PSR-3 logger objects.
Subclass ScopedCallback to avoid call_user_func_array(), which is slow.
Arbitrary section name based PHP profiling.
reset()
Clear all of the profiling data for another run.
collateData()
Populate collated data.
profileInInternal( $functionname)
This method should not be called outside SectionProfiler.
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
array[] $collated
Map of (function name => aggregate data array)
array null $start
Map of (mem,real,cpu)
array null $end
Map of (mem,real,cpu)
array $workStack
Queue of open profile calls with start data.
array $errorEntry
Cache of a standard broken collation entry.
array[] $stack
List of resolved profile calls with start/end data.
updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange)
Update the collation entry for a given method name.
profileOutInternal( $functionname)
This method should not be called outside SectionProfiler.
getTime( $metric='wall')
Get the initial time of the request, based on getrusage()
scopedProfileOut(?SectionProfileCallback &$section)