Reduce total profiling overhead by about 6%, and internal overhead by about 40%
[lhc/web/wiklou.git] / includes / Profiling.php
1 <?php
2 /**
3 * This file is only included if profiling is enabled
4 * @package MediaWiki
5 */
6
7 /**
8 * @param $functioname name of the function we will profile
9 */
10 function wfProfileIn( $functionname ) {
11 global $wgProfiler;
12 $wgProfiler->profileIn( $functionname );
13 }
14
15 /**
16 * @param $functioname name of the function we have profiled
17 */
18 function wfProfileOut( $functionname = 'missing' ) {
19 global $wgProfiler;
20 $wgProfiler->profileOut( $functionname );
21 }
22
23 function wfGetProfilingOutput( $start, $elapsed ) {
24 global $wgProfiler;
25 return $wgProfiler->getOutput( $start, $elapsed );
26 }
27
28 function wfProfileClose() {
29 global $wgProfiler;
30 $wgProfiler->close();
31 }
32
33 if( !function_exists( 'memory_get_usage' ) ) {
34 # Old PHP or --enable-memory-limit not compiled in
35 function memory_get_usage() {
36 return 0;
37 }
38 }
39
40 /**
41 * @todo document
42 * @package MediaWiki
43 */
44 class Profiler
45 {
46 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
47 var $mCalls = array(), $mTotals = array();
48 /*
49 function Profiler()
50 {
51 $this->mProfileStack = array();
52 $this->mWorkStack = array();
53 $this->mCollated = array();
54 }
55 */
56
57 function profileIn( $functionname ) {
58 global $wgDebugFunctionEntry;
59 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
60 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering '.$functionname."\n" );
61 }
62 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(), memory_get_usage() );
63 }
64
65 function profileOut( $functionname ) {
66 $time = microtime();
67 $memory = memory_get_usage();
68 global $wgDebugProfiling, $wgDebugFunctionEntry;
69
70 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
71 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Exiting '.$functionname."\n" );
72 }
73
74 $bit = array_pop( $this->mWorkStack );
75
76 if ( !$bit ) {
77 wfDebug( "Profiling error, !\$bit: $functionname\n" );
78 } else {
79 if ( $wgDebugProfiling ) {
80 if ( $functionname == 'close' ) {
81 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
82 } elseif ( $bit[0] != $functionname ) {
83 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
84 }
85 }
86 $bit[] = $time;
87 $bit[] = $memory;
88 $this->mStack[] = $bit;
89 }
90 }
91
92 function close() {
93 while ( count( $this->mWorkStack ) ) {
94 $this->profileOut( 'close' );
95 }
96 }
97
98 function getOutput() {
99 global $wgDebugFunctionEntry;
100 $wgDebugFunctionEntry = false;
101
102 if( !count( $this->mStack ) ) {
103 return "No profiling output\n";
104 }
105 $this->close();
106 $width = 125;
107 $format = "%-" . ($width - 34) . "s %6d %6.3f %6.3f %7.3f%% %6d (%6.3f-%6.3f)\n";
108 $titleFormat = "%-" . ($width - 34) . "s %9s %9s %9s %9s %6s\n";
109 $prof = "\nProfiling data\n";
110 $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
111 $this->mCollated = array();
112 $this->mCalls = array();
113 $this->mMemory = array();
114
115 # Estimate profiling overhead
116 $profileCount = count( $this->mStack );
117 wfProfileIn( '-overhead-total' );
118 for ($i=0; $i<$profileCount ; $i++) {
119 wfProfileIn( '-overhead-internal' );
120 wfProfileOut( '-overhead-internal' );
121 }
122 wfProfileOut( '-overhead-total' );
123
124 # Collate
125 foreach ( $this->mStack as $entry ) {
126 $fname = $entry[0];
127 $thislevel = $entry[1];
128 $start = explode( ' ', $entry[2]);
129 $start = (float)$start[0] + (float)$start[1];
130 $end = explode( ' ', $entry[4]);
131 $end = (float)$end[0] + (float)$end[1];
132 $elapsed = $end - $start;
133
134 $memory = $entry[5] - $entry[3];
135
136 if ( !array_key_exists( $fname, $this->mCollated ) ) {
137 $this->mCollated[$fname] = 0;
138 $this->mCalls[$fname] = 0;
139 $this->mMemory[$fname] = 0;
140 $this->mMin[$fname] = 1 << 24;
141 $this->mMax[$fname] = 0;
142 }
143
144 $this->mCollated[$fname] += $elapsed;
145 $this->mCalls[$fname] ++;
146 $this->mMemory[$fname] += $memory;
147 $this->mMin[$fname] = min( $this->mMin[$fname], $elapsed );
148 $this->mMax[$fname] = max( $this->mMax[$fname], $elapsed );
149 }
150
151 $total = @$this->mCollated['-total'];
152 $overhead = $this->mCollated['-overhead-internal'] / $profileCount;
153 $this->mCalls['-overhead-total'] = $profileCount;
154
155 # Output
156 asort( $this->mCollated, SORT_NUMERIC );
157 foreach ( $this->mCollated as $fname => $elapsed ) {
158 $calls = $this->mCalls[$fname];
159 # Adjust for overhead
160 if ( $fname[0] != '-' ) {
161 $elapsed -= $overhead * $calls;
162 }
163
164 $percent = $total ? 100. * $elapsed / $total : 0;
165 $memory = $this->mMemory[$fname];
166 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
167 (float)($elapsed * 1000) / $calls, $percent, $memory,
168 ($this->mMin[$fname] * 1000.0),
169 ($this->mMax[$fname] * 1000.0));
170
171 global $wgProfileToDatabase;
172 if( $wgProfileToDatabase ) {
173 Profiler::logToDB( $fname, (float)($elapsed * 1000), $calls );
174 }
175 }
176 $prof .= "\nTotal: $total\n\n";
177
178 return $prof;
179 }
180
181
182 /**
183 * @static
184 */
185 function logToDB($name, $timeSum, $eventCount) {
186 $dbw =& wfGetDB( DB_MASTER );
187 $profiling = $dbw->tableName( 'profiling' );
188
189 $name = substr($name,0,255);
190 $encname = $dbw->strencode($name);
191 $sql = "UPDATE $profiling ".
192 "SET pf_count=pf_count+{$eventCount}, ".
193 "pf_time=pf_time + {$timeSum} ".
194 "WHERE pf_name='{$encname}'";
195 $dbw->query($sql);
196
197 $rc = $dbw->affectedRows();
198 if( $rc == 0) {
199 $dbw->insert('profiling',array(
200 'pf_name'=>$name,
201 'pf_count'=>$eventCount,
202 'pf_time'=>$timeSum),
203 $fname,array('IGNORE'));
204 }
205 // When we upgrade to mysql 4.1, the insert+update
206 // can be merged into just a insert with this construct added:
207 // "ON DUPLICATE KEY UPDATE ".
208 // "pf_count=pf_count + VALUES(pf_count), ".
209 // "pf_time=pf_time + VALUES(pf_time)";
210 }
211
212 }
213
214
215 $wgProfiler = new Profiler();
216 $wgProfiler->profileIn( '-total' );
217 ?>