| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Logger\LoggerFactory; |
| 22 |
use Psr\Log\LoggerInterface; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class SectionProfiler { |
| 35 |
|
| 36 |
protected $start; |
| 37 |
|
| 38 |
protected $end; |
| 39 |
|
| 40 |
protected $stack = []; |
| 41 |
|
| 42 |
protected $workStack = []; |
| 43 |
|
| 44 |
protected $collated = []; |
| 45 |
|
| 46 |
protected $collateDone = false; |
| 47 |
|
| 48 |
|
| 49 |
protected $errorEntry; |
| 50 |
|
| 51 |
protected $logger; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function __construct( array $params = [] ) { |
| 57 |
$this->errorEntry = $this->getErrorEntry(); |
| 58 |
$this->logger = LoggerFactory::getInstance( 'profiler' ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function scopedProfileIn( $section ): ?SectionProfileCallback { |
| 65 |
$this->profileInInternal( $section ); |
| 66 |
|
| 67 |
return new SectionProfileCallback( $this, $section ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function scopedProfileOut( ?SectionProfileCallback &$section ) { |
| 74 |
$section = null; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function getFunctionStats() { |
| 99 |
$this->collateData(); |
| 100 |
|
| 101 |
if ( is_array( $this->start ) && is_array( $this->end ) ) { |
| 102 |
$totalCpu = max( $this->end['cpu'] - $this->start['cpu'], 0 ); |
| 103 |
$totalReal = max( $this->end['real'] - $this->start['real'], 0 ); |
| 104 |
$totalMem = max( $this->end['memory'] - $this->start['memory'], 0 ); |
| 105 |
} else { |
| 106 |
$totalCpu = 0; |
| 107 |
$totalReal = 0; |
| 108 |
$totalMem = 0; |
| 109 |
} |
| 110 |
|
| 111 |
$profile = []; |
| 112 |
foreach ( $this->collated as $fname => $data ) { |
| 113 |
$profile[] = [ |
| 114 |
'name' => $fname, |
| 115 |
'calls' => $data['count'], |
| 116 |
'real' => $data['real'] * 1000, |
| 117 |
'%real' => $totalReal ? 100 * $data['real'] / $totalReal : 0, |
| 118 |
'cpu' => $data['cpu'] * 1000, |
| 119 |
'%cpu' => $totalCpu ? 100 * $data['cpu'] / $totalCpu : 0, |
| 120 |
'memory' => $data['memory'], |
| 121 |
'%memory' => $totalMem ? 100 * $data['memory'] / $totalMem : 0, |
| 122 |
'min_real' => 1000 * $data['min_real'], |
| 123 |
'max_real' => 1000 * $data['max_real'] |
| 124 |
]; |
| 125 |
} |
| 126 |
|
| 127 |
$profile[] = [ |
| 128 |
'name' => '-total', |
| 129 |
'calls' => 1, |
| 130 |
'real' => 1000 * $totalReal, |
| 131 |
'%real' => 100, |
| 132 |
'cpu' => 1000 * $totalCpu, |
| 133 |
'%cpu' => 100, |
| 134 |
'memory' => $totalMem, |
| 135 |
'%memory' => 100, |
| 136 |
'min_real' => 1000 * $totalReal, |
| 137 |
'max_real' => 1000 * $totalReal |
| 138 |
]; |
| 139 |
|
| 140 |
return $profile; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function reset() { |
| 147 |
$this->start = null; |
| 148 |
$this->end = null; |
| 149 |
$this->stack = []; |
| 150 |
$this->workStack = []; |
| 151 |
$this->collated = []; |
| 152 |
$this->collateDone = false; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
protected function getZeroEntry() { |
| 159 |
return [ |
| 160 |
'cpu' => 0.0, |
| 161 |
'real' => 0.0, |
| 162 |
'memory' => 0, |
| 163 |
'count' => 0, |
| 164 |
'min_real' => 0.0, |
| 165 |
'max_real' => 0.0 |
| 166 |
]; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
protected function getErrorEntry() { |
| 173 |
$entry = $this->getZeroEntry(); |
| 174 |
$entry['count'] = 1; |
| 175 |
return $entry; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) { |
| 187 |
$entry =& $this->collated[$name]; |
| 188 |
if ( !is_array( $entry ) ) { |
| 189 |
$entry = $this->getZeroEntry(); |
| 190 |
$this->collated[$name] =& $entry; |
| 191 |
} |
| 192 |
$entry['cpu'] += $elapsedCpu; |
| 193 |
$entry['real'] += $elapsedReal; |
| 194 |
$entry['memory'] += $memChange > 0 ? $memChange : 0; |
| 195 |
$entry['count']++; |
| 196 |
$entry['min_real'] = min( $entry['min_real'], $elapsedReal ); |
| 197 |
$entry['max_real'] = max( $entry['max_real'], $elapsedReal ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function profileInInternal( $functionname ) { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
$this->collateDone = false; |
| 210 |
|
| 211 |
$cpu = $this->getTime( 'cpu' ); |
| 212 |
$real = $this->getTime( 'wall' ); |
| 213 |
$memory = memory_get_usage(); |
| 214 |
|
| 215 |
if ( $this->start === null ) { |
| 216 |
$this->start = [ 'cpu' => $cpu, 'real' => $real, 'memory' => $memory ]; |
| 217 |
} |
| 218 |
|
| 219 |
$this->workStack[] = [ |
| 220 |
$functionname, |
| 221 |
count( $this->workStack ), |
| 222 |
$real, |
| 223 |
$cpu, |
| 224 |
$memory |
| 225 |
]; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function profileOutInternal( $functionname ) { |
| 234 |
$item = array_pop( $this->workStack ); |
| 235 |
if ( $item === null ) { |
| 236 |
$this->logger->error( "Profiling error: $functionname" ); |
| 237 |
return; |
| 238 |
} |
| 239 |
[ $ofname, , $ortime, $octime, $omem ] = $item; |
| 240 |
|
| 241 |
if ( $functionname === 'close' ) { |
| 242 |
$message = "Profile section ended by close(): {$ofname}"; |
| 243 |
$this->logger->error( $message ); |
| 244 |
$this->collated[$message] = $this->errorEntry; |
| 245 |
$functionname = $ofname; |
| 246 |
} elseif ( $ofname !== $functionname ) { |
| 247 |
$message = "Profiling error: in({$ofname}), out($functionname)"; |
| 248 |
$this->logger->error( $message ); |
| 249 |
$this->collated[$message] = $this->errorEntry; |
| 250 |
} |
| 251 |
|
| 252 |
$realTime = $this->getTime( 'wall' ); |
| 253 |
$cpuTime = $this->getTime( 'cpu' ); |
| 254 |
$memUsage = memory_get_usage(); |
| 255 |
|
| 256 |
$elapsedcpu = $cpuTime - $octime; |
| 257 |
$elapsedreal = $realTime - $ortime; |
| 258 |
$memchange = $memUsage - $omem; |
| 259 |
$this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange ); |
| 260 |
|
| 261 |
$this->end = [ |
| 262 |
'cpu' => $cpuTime, |
| 263 |
'real' => $realTime, |
| 264 |
'memory' => $memUsage |
| 265 |
]; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
protected function collateData() { |
| 272 |
if ( $this->collateDone ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
$this->collateDone = true; |
| 276 |
|
| 277 |
while ( count( $this->workStack ) ) { |
| 278 |
$this->profileOutInternal( 'close' ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
protected function getTime( $metric = 'wall' ) { |
| 293 |
if ( $metric === 'cpu' || $metric === 'user' ) { |
| 294 |
$ru = getrusage( 0 ); |
| 295 |
$time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6; |
| 296 |
if ( $metric === 'cpu' ) { |
| 297 |
|
| 298 |
|
| 299 |
$time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6; |
| 300 |
} |
| 301 |
return $time; |
| 302 |
} else { |
| 303 |
return microtime( true ); |
| 304 |
} |
| 305 |
} |
| 306 |
} |