10use Psr\Log\LoggerInterface;
11use Wikimedia\Timestamp\ConvertibleTimestamp;
47 $this->logger = LoggerFactory::getInstance(
'profiler' );
88 if ( is_array( $this->start ) && is_array( $this->end ) ) {
89 $totalCpu = max( $this->end[
'cpu'] - $this->start[
'cpu'], 0 );
90 $totalReal = max( $this->end[
'real'] - $this->start[
'real'], 0 );
91 $totalMem = max( $this->end[
'memory'] - $this->start[
'memory'], 0 );
99 foreach ( $this->collated as $fname => $data ) {
102 'calls' => $data[
'count'],
103 'real' => $data[
'real'] * 1000,
104 '%real' => $totalReal ? 100 * $data[
'real'] / $totalReal : 0,
105 'cpu' => $data[
'cpu'] * 1000,
106 '%cpu' => $totalCpu ? 100 * $data[
'cpu'] / $totalCpu : 0,
107 'memory' => $data[
'memory'],
108 '%memory' => $totalMem ? 100 * $data[
'memory'] / $totalMem : 0,
109 'min_real' => 1000 * $data[
'min_real'],
110 'max_real' => 1000 * $data[
'max_real']
117 'real' => 1000 * $totalReal,
119 'cpu' => 1000 * $totalCpu,
121 'memory' => $totalMem,
123 'min_real' => 1000 * $totalReal,
124 'max_real' => 1000 * $totalReal
137 $this->workStack = [];
138 $this->collated = [];
139 $this->collateDone =
false;
160 $entry = $this->getZeroEntry();
173 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
174 $entry =& $this->collated[$name];
175 if ( !is_array( $entry ) ) {
176 $entry = $this->getZeroEntry();
177 $this->collated[$name] =& $entry;
179 $entry[
'cpu'] += $elapsedCpu;
180 $entry[
'real'] += $elapsedReal;
181 $entry[
'memory'] += $memChange > 0 ? $memChange : 0;
183 $entry[
'min_real'] = min( $entry[
'min_real'], $elapsedReal );
184 $entry[
'max_real'] = max( $entry[
'max_real'], $elapsedReal );
196 $this->collateDone =
false;
198 $cpu = $this->getTime(
'cpu' );
199 $real = $this->getTime(
'wall' );
200 $memory = memory_get_usage();
202 if ( $this->start ===
null ) {
203 $this->start = [
'cpu' => $cpu,
'real' => $real,
'memory' => $memory ];
206 $this->workStack[] = [
208 count( $this->workStack ),
221 $item = array_pop( $this->workStack );
222 if ( $item ===
null ) {
223 $this->logger->error(
"Profiling error: $functionname" );
226 [ $ofname, , $ortime, $octime, $omem ] = $item;
228 if ( $functionname ===
'close' ) {
229 $message =
"Profile section ended by close(): {$ofname}";
230 $this->logger->error( $message );
231 $this->collated[$message] = $this->errorEntry;
232 $functionname = $ofname;
233 } elseif ( $ofname !== $functionname ) {
234 $message =
"Profiling error: in({$ofname}), out($functionname)";
235 $this->logger->error( $message );
236 $this->collated[$message] = $this->errorEntry;
239 $realTime = $this->getTime(
'wall' );
240 $cpuTime = $this->getTime(
'cpu' );
241 $memUsage = memory_get_usage();
243 $elapsedcpu = $cpuTime - $octime;
244 $elapsedreal = $realTime - $ortime;
245 $memchange = $memUsage - $omem;
246 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
251 'memory' => $memUsage
259 if ( $this->collateDone ) {
262 $this->collateDone =
true;
264 while ( count( $this->workStack ) ) {
265 $this->profileOutInternal(
'close' );
279 private function getTime( $metric =
'wall' ) {
280 if ( $metric ===
'cpu' || $metric ===
'user' ) {
281 $ru = getrusage( 0 );
282 $time = $ru[
'ru_utime.tv_sec'] + $ru[
'ru_utime.tv_usec'] / 1e6;
283 if ( $metric ===
'cpu' ) {
284 # This is the time of system calls, added to the user time
285 # it gives the total CPU time
286 $time += $ru[
'ru_stime.tv_sec'] + $ru[
'ru_stime.tv_usec'] / 1e6;
290 return ConvertibleTimestamp::hrtime() / 1e9;
296class_alias( SectionProfiler::class,
'SectionProfiler' );