| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Wikimedia\RunningStat; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
s |
| 28 |
|
| 29 |
|
| 30 |
class XhprofData { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected $config; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
protected $hieraData; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected $inclusive; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected $complete; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( array $data, array $config = [] ) { |
| 64 |
$this->config = array_merge( [ |
| 65 |
'include' => null, |
| 66 |
'sort' => 'wt', |
| 67 |
], $config ); |
| 68 |
|
| 69 |
$this->hieraData = $this->pruneData( $data ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 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 getRawData() { |
| 99 |
return $this->hieraData; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public static function splitKey( $key ) { |
| 113 |
return array_pad( explode( '==>', $key, 2 ), -2, null ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected function pruneData( $data ) { |
| 124 |
if ( !$this->config['include'] ) { |
| 125 |
return $data; |
| 126 |
} |
| 127 |
|
| 128 |
$want = array_fill_keys( $this->config['include'], true ); |
| 129 |
$want['main()'] = true; |
| 130 |
|
| 131 |
$keep = []; |
| 132 |
foreach ( $data as $key => $stats ) { |
| 133 |
[ $parent, $child ] = self::splitKey( $key ); |
| 134 |
if ( ( $parent !== null && isset( $want[$parent] ) ) || isset( $want[$child] ) ) { |
| 135 |
$keep[$key] = $stats; |
| 136 |
} |
| 137 |
} |
| 138 |
return $keep; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function getInclusiveMetrics() { |
| 160 |
if ( $this->inclusive === null ) { |
| 161 |
$main = $this->hieraData['main()']; |
| 162 |
$hasCpu = isset( $main['cpu'] ); |
| 163 |
$hasMu = isset( $main['mu'] ); |
| 164 |
$hasAlloc = isset( $main['alloc'] ); |
| 165 |
|
| 166 |
$inclusive = []; |
| 167 |
foreach ( $this->hieraData as $key => $stats ) { |
| 168 |
[ , $child ] = self::splitKey( $key ); |
| 169 |
if ( !isset( $inclusive[$child] ) ) { |
| 170 |
$inclusive[$child] = [ |
| 171 |
'ct' => 0, |
| 172 |
'wt' => new RunningStat(), |
| 173 |
]; |
| 174 |
if ( $hasCpu ) { |
| 175 |
$inclusive[$child]['cpu'] = new RunningStat(); |
| 176 |
} |
| 177 |
if ( $hasMu ) { |
| 178 |
$inclusive[$child]['mu'] = new RunningStat(); |
| 179 |
$inclusive[$child]['pmu'] = new RunningStat(); |
| 180 |
} |
| 181 |
if ( $hasAlloc ) { |
| 182 |
$inclusive[$child]['alloc'] = new RunningStat(); |
| 183 |
$inclusive[$child]['free'] = new RunningStat(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
$inclusive[$child]['ct'] += $stats['ct']; |
| 188 |
foreach ( $stats as $stat => $value ) { |
| 189 |
if ( $stat === 'ct' ) { |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
if ( !isset( $inclusive[$child][$stat] ) ) { |
| 194 |
|
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
for ( $i = 0; $i < $stats['ct']; $i++ ) { |
| 199 |
$inclusive[$child][$stat]->addObservation( |
| 200 |
$value / $stats['ct'] |
| 201 |
); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
foreach ( $inclusive as $func => $stats ) { |
| 209 |
foreach ( $stats as $name => $value ) { |
| 210 |
if ( $value instanceof RunningStat ) { |
| 211 |
$total = $value->getMean() * $value->getCount(); |
| 212 |
$percent = ( isset( $main[$name] ) && $main[$name] ) |
| 213 |
? 100 * $total / $main[$name] |
| 214 |
: 0; |
| 215 |
$inclusive[$func][$name] = [ |
| 216 |
'total' => $total, |
| 217 |
'min' => $value->min, |
| 218 |
'mean' => $value->getMean(), |
| 219 |
'max' => $value->max, |
| 220 |
'variance' => $value->m2, |
| 221 |
'percent' => $percent, |
| 222 |
]; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
uasort( $inclusive, self::makeSortFunction( |
| 228 |
$this->config['sort'], 'total' |
| 229 |
) ); |
| 230 |
$this->inclusive = $inclusive; |
| 231 |
} |
| 232 |
return $this->inclusive; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function getCompleteMetrics() { |
| 247 |
if ( $this->complete === null ) { |
| 248 |
|
| 249 |
$this->complete = $this->getInclusiveMetrics(); |
| 250 |
|
| 251 |
foreach ( $this->complete as $func => $stats ) { |
| 252 |
foreach ( $stats as $stat => $value ) { |
| 253 |
if ( $stat === 'ct' ) { |
| 254 |
continue; |
| 255 |
} |
| 256 |
|
| 257 |
$this->complete[$func][$stat]['exclusive'] = $value['total']; |
| 258 |
} |
| 259 |
|
| 260 |
$this->complete[$func]['calls'] = []; |
| 261 |
$this->complete[$func]['subcalls'] = []; |
| 262 |
} |
| 263 |
|
| 264 |
foreach ( $this->hieraData as $key => $stats ) { |
| 265 |
[ $parent, $child ] = self::splitKey( $key ); |
| 266 |
if ( $parent !== null ) { |
| 267 |
|
| 268 |
$this->complete[$child]['calls'][$parent] = $stats; |
| 269 |
$this->complete[$parent]['subcalls'][$child] = $stats; |
| 270 |
} |
| 271 |
|
| 272 |
if ( $parent !== null && isset( $this->complete[$parent] ) ) { |
| 273 |
|
| 274 |
foreach ( $stats as $stat => $value ) { |
| 275 |
if ( $stat === 'ct' ) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
|
| 279 |
if ( !isset( $this->complete[$parent][$stat] ) ) { |
| 280 |
|
| 281 |
continue; |
| 282 |
} |
| 283 |
|
| 284 |
$this->complete[$parent][$stat]['exclusive'] -= $value; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
uasort( $this->complete, self::makeSortFunction( |
| 290 |
$this->config['sort'], 'exclusive' |
| 291 |
) ); |
| 292 |
} |
| 293 |
return $this->complete; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function getCallers( $function ) { |
| 304 |
$edges = $this->getCompleteMetrics(); |
| 305 |
if ( isset( $edges[$function]['calls'] ) ) { |
| 306 |
return array_keys( $edges[$function]['calls'] ); |
| 307 |
} else { |
| 308 |
return []; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function getCallees( $function ) { |
| 320 |
$edges = $this->getCompleteMetrics(); |
| 321 |
if ( isset( $edges[$function]['subcalls'] ) ) { |
| 322 |
return array_keys( $edges[$function]['subcalls'] ); |
| 323 |
} else { |
| 324 |
return []; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function getCriticalPath( $metric = 'wt' ) { |
| 335 |
$func = 'main()'; |
| 336 |
$path = [ |
| 337 |
$func => $this->hieraData[$func], |
| 338 |
]; |
| 339 |
while ( $func ) { |
| 340 |
$callees = $this->getCallees( $func ); |
| 341 |
$maxCallee = null; |
| 342 |
$maxCall = null; |
| 343 |
foreach ( $callees as $callee ) { |
| 344 |
$call = "{$func}==>{$callee}"; |
| 345 |
if ( $maxCall === null || |
| 346 |
$this->hieraData[$call][$metric] > |
| 347 |
$this->hieraData[$maxCall][$metric] |
| 348 |
) { |
| 349 |
$maxCallee = $callee; |
| 350 |
$maxCall = $call; |
| 351 |
} |
| 352 |
} |
| 353 |
if ( $maxCall !== null ) { |
| 354 |
$path[$maxCall] = $this->hieraData[$maxCall]; |
| 355 |
} |
| 356 |
$func = $maxCallee; |
| 357 |
} |
| 358 |
return $path; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public static function makeSortFunction( $key, $sub ) { |
| 370 |
return static function ( $a, $b ) use ( $key, $sub ) { |
| 371 |
if ( isset( $a[$key] ) && isset( $b[$key] ) ) { |
| 372 |
|
| 373 |
|
| 374 |
$valA = is_array( $a[$key] ) ? $a[$key][$sub] : $a[$key]; |
| 375 |
$valB = is_array( $b[$key] ) ? $b[$key][$sub] : $b[$key]; |
| 376 |
return $valB <=> $valA; |
| 377 |
} else { |
| 378 |
|
| 379 |
return isset( $a[$key] ) ? -1 : 1; |
| 380 |
} |
| 381 |
}; |
| 382 |
} |
| 383 |
} |