Profiler.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * @defgroup Profiler Profiler
  4. *
  5. * @file
  6. * @ingroup Profiler
  7. * This file is only included if profiling is enabled
  8. */
  9. /** backward compatibility */
  10. $wgProfiling = true;
  11. /**
  12. * Begin profiling of a function
  13. * @param $functioname name of the function we will profile
  14. */
  15. function wfProfileIn( $functionname ) {
  16. global $wgProfiler;
  17. $wgProfiler->profileIn( $functionname );
  18. }
  19. /**
  20. * Stop profiling of a function
  21. * @param $functioname name of the function we have profiled
  22. */
  23. function wfProfileOut( $functionname = 'missing' ) {
  24. global $wgProfiler;
  25. $wgProfiler->profileOut( $functionname );
  26. }
  27. /**
  28. * Returns a profiling output to be stored in debug file
  29. *
  30. * @param float $start
  31. * @param float $elapsed time elapsed since the beginning of the request
  32. */
  33. function wfGetProfilingOutput( $start, $elapsed ) {
  34. global $wgProfiler;
  35. return $wgProfiler->getOutput( $start, $elapsed );
  36. }
  37. /**
  38. * Close opened profiling sections
  39. */
  40. function wfProfileClose() {
  41. global $wgProfiler;
  42. $wgProfiler->close();
  43. }
  44. if (!function_exists('memory_get_usage')) {
  45. # Old PHP or --enable-memory-limit not compiled in
  46. function memory_get_usage() {
  47. return 0;
  48. }
  49. }
  50. /**
  51. * @ingroup Profiler
  52. * @todo document
  53. */
  54. class Profiler {
  55. var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
  56. var $mCalls = array (), $mTotals = array ();
  57. function __construct() {
  58. // Push an entry for the pre-profile setup time onto the stack
  59. global $wgRequestTime;
  60. if ( !empty( $wgRequestTime ) ) {
  61. $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, 0 );
  62. $this->mStack[] = array( '-setup', 1, $wgRequestTime, 0, microtime(true), 0 );
  63. } else {
  64. $this->profileIn( '-total' );
  65. }
  66. }
  67. /**
  68. * Called by wfProfieIn()
  69. * @param $functionname string
  70. */
  71. function profileIn( $functionname ) {
  72. global $wgDebugFunctionEntry, $wgProfiling;
  73. if( !$wgProfiling ) return;
  74. if( $wgDebugFunctionEntry ){
  75. $this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" );
  76. }
  77. $this->mWorkStack[] = array( $functionname, count( $this->mWorkStack ), $this->getTime(), memory_get_usage() );
  78. }
  79. /**
  80. * Called by wfProfieOut()
  81. * @param $functionname string
  82. */
  83. function profileOut($functionname) {
  84. global $wgDebugFunctionEntry, $wgProfiling;
  85. if( !$wgProfiling ) return;
  86. $memory = memory_get_usage();
  87. $time = $this->getTime();
  88. if( $wgDebugFunctionEntry ){
  89. $this->debug( str_repeat( ' ', count( $this->mWorkStack ) - 1 ) . 'Exiting ' . $functionname . "\n" );
  90. }
  91. $bit = array_pop($this->mWorkStack);
  92. if (!$bit) {
  93. $this->debug("Profiling error, !\$bit: $functionname\n");
  94. } else {
  95. //if( $wgDebugProfiling ){
  96. if( $functionname == 'close' ){
  97. $message = "Profile section ended by close(): {$bit[0]}";
  98. $this->debug( "$message\n" );
  99. $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
  100. }
  101. elseif( $bit[0] != $functionname ){
  102. $message = "Profiling error: in({$bit[0]}), out($functionname)";
  103. $this->debug( "$message\n" );
  104. $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
  105. }
  106. //}
  107. $bit[] = $time;
  108. $bit[] = $memory;
  109. $this->mStack[] = $bit;
  110. }
  111. }
  112. /**
  113. * called by wfProfileClose()
  114. */
  115. function close() {
  116. while( count( $this->mWorkStack ) ){
  117. $this->profileOut( 'close' );
  118. }
  119. }
  120. /**
  121. * called by wfGetProfilingOutput()
  122. */
  123. function getOutput() {
  124. global $wgDebugFunctionEntry, $wgProfileCallTree;
  125. $wgDebugFunctionEntry = false;
  126. if( !count( $this->mStack ) && !count( $this->mCollated ) ){
  127. return "No profiling output\n";
  128. }
  129. $this->close();
  130. if( $wgProfileCallTree ) {
  131. global $wgProfileToDatabase;
  132. # XXX: We must call $this->getFunctionReport() to log to the DB
  133. if( $wgProfileToDatabase ) {
  134. $this->getFunctionReport();
  135. }
  136. return $this->getCallTree();
  137. } else {
  138. return $this->getFunctionReport();
  139. }
  140. }
  141. /**
  142. * returns a tree of function call instead of a list of functions
  143. */
  144. function getCallTree() {
  145. return implode( '', array_map( array( &$this, 'getCallTreeLine' ), $this->remapCallTree( $this->mStack ) ) );
  146. }
  147. /**
  148. * Recursive function the format the current profiling array into a tree
  149. *
  150. * @param $stack profiling array
  151. */
  152. function remapCallTree( $stack ) {
  153. if( count( $stack ) < 2 ){
  154. return $stack;
  155. }
  156. $outputs = array ();
  157. for( $max = count( $stack ) - 1; $max > 0; ){
  158. /* Find all items under this entry */
  159. $level = $stack[$max][1];
  160. $working = array ();
  161. for( $i = $max -1; $i >= 0; $i-- ){
  162. if( $stack[$i][1] > $level ){
  163. $working[] = $stack[$i];
  164. } else {
  165. break;
  166. }
  167. }
  168. $working = $this->remapCallTree( array_reverse( $working ) );
  169. $output = array();
  170. foreach( $working as $item ){
  171. array_push( $output, $item );
  172. }
  173. array_unshift( $output, $stack[$max] );
  174. $max = $i;
  175. array_unshift( $outputs, $output );
  176. }
  177. $final = array();
  178. foreach( $outputs as $output ){
  179. foreach( $output as $item ){
  180. $final[] = $item;
  181. }
  182. }
  183. return $final;
  184. }
  185. /**
  186. * Callback to get a formatted line for the call tree
  187. */
  188. function getCallTreeLine( $entry ) {
  189. list( $fname, $level, $start, /* $x */, $end) = $entry;
  190. $delta = $end - $start;
  191. $space = str_repeat(' ', $level);
  192. # The ugly double sprintf is to work around a PHP bug,
  193. # which has been fixed in recent releases.
  194. return sprintf( "%10s %s %s\n", trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
  195. }
  196. function getTime() {
  197. return microtime(true);
  198. #return $this->getUserTime();
  199. }
  200. function getUserTime() {
  201. $ru = getrusage();
  202. return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6;
  203. }
  204. /**
  205. * Returns a list of profiled functions.
  206. * Also log it into the database if $wgProfileToDatabase is set to true.
  207. */
  208. function getFunctionReport() {
  209. global $wgProfileToDatabase;
  210. $width = 140;
  211. $nameWidth = $width - 65;
  212. $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n";
  213. $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
  214. $prof = "\nProfiling data\n";
  215. $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
  216. $this->mCollated = array ();
  217. $this->mCalls = array ();
  218. $this->mMemory = array ();
  219. # Estimate profiling overhead
  220. $profileCount = count($this->mStack);
  221. wfProfileIn( '-overhead-total' );
  222. for( $i = 0; $i < $profileCount; $i ++ ){
  223. wfProfileIn( '-overhead-internal' );
  224. wfProfileOut( '-overhead-internal' );
  225. }
  226. wfProfileOut( '-overhead-total' );
  227. # First, subtract the overhead!
  228. foreach( $this->mStack as $entry ){
  229. $fname = $entry[0];
  230. $start = $entry[2];
  231. $end = $entry[4];
  232. $elapsed = $end - $start;
  233. $memory = $entry[5] - $entry[3];
  234. if( $fname == '-overhead-total' ){
  235. $overheadTotal[] = $elapsed;
  236. $overheadMemory[] = $memory;
  237. }
  238. elseif( $fname == '-overhead-internal' ){
  239. $overheadInternal[] = $elapsed;
  240. }
  241. }
  242. $overheadTotal = array_sum( $overheadTotal ) / count( $overheadInternal );
  243. $overheadMemory = array_sum( $overheadMemory ) / count( $overheadInternal );
  244. $overheadInternal = array_sum( $overheadInternal ) / count( $overheadInternal );
  245. # Collate
  246. foreach( $this->mStack as $index => $entry ){
  247. $fname = $entry[0];
  248. $start = $entry[2];
  249. $end = $entry[4];
  250. $elapsed = $end - $start;
  251. $memory = $entry[5] - $entry[3];
  252. $subcalls = $this->calltreeCount( $this->mStack, $index );
  253. if( !preg_match( '/^-overhead/', $fname ) ){
  254. # Adjust for profiling overhead (except special values with elapsed=0
  255. if( $elapsed ) {
  256. $elapsed -= $overheadInternal;
  257. $elapsed -= ($subcalls * $overheadTotal);
  258. $memory -= ($subcalls * $overheadMemory);
  259. }
  260. }
  261. if( !array_key_exists( $fname, $this->mCollated ) ){
  262. $this->mCollated[$fname] = 0;
  263. $this->mCalls[$fname] = 0;
  264. $this->mMemory[$fname] = 0;
  265. $this->mMin[$fname] = 1 << 24;
  266. $this->mMax[$fname] = 0;
  267. $this->mOverhead[$fname] = 0;
  268. }
  269. $this->mCollated[$fname] += $elapsed;
  270. $this->mCalls[$fname]++;
  271. $this->mMemory[$fname] += $memory;
  272. $this->mMin[$fname] = min($this->mMin[$fname], $elapsed);
  273. $this->mMax[$fname] = max($this->mMax[$fname], $elapsed);
  274. $this->mOverhead[$fname] += $subcalls;
  275. }
  276. $total = @$this->mCollated['-total'];
  277. $this->mCalls['-overhead-total'] = $profileCount;
  278. # Output
  279. arsort( $this->mCollated, SORT_NUMERIC );
  280. foreach( $this->mCollated as $fname => $elapsed ){
  281. $calls = $this->mCalls[$fname];
  282. $percent = $total ? 100. * $elapsed / $total : 0;
  283. $memory = $this->mMemory[$fname];
  284. $prof .= sprintf($format, substr($fname, 0, $nameWidth), $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin[$fname] * 1000.0), ($this->mMax[$fname] * 1000.0), $this->mOverhead[$fname]);
  285. # Log to the DB
  286. if( $wgProfileToDatabase ) {
  287. self::logToDB($fname, (float) ($elapsed * 1000), $calls, (float) ($memory) );
  288. }
  289. }
  290. $prof .= "\nTotal: $total\n\n";
  291. return $prof;
  292. }
  293. /**
  294. * Counts the number of profiled function calls sitting under
  295. * the given point in the call graph. Not the most efficient algo.
  296. *
  297. * @param $stack Array:
  298. * @param $start Integer:
  299. * @return Integer
  300. * @private
  301. */
  302. function calltreeCount($stack, $start) {
  303. $level = $stack[$start][1];
  304. $count = 0;
  305. for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
  306. $count ++;
  307. }
  308. return $count;
  309. }
  310. /**
  311. * Log a function into the database.
  312. *
  313. * @param $name string: function name
  314. * @param $timeSum float
  315. * @param $eventCount int: number of times that function was called
  316. */
  317. static function logToDB( $name, $timeSum, $eventCount, $memorySum ){
  318. # Do not log anything if database is readonly (bug 5375)
  319. if( wfReadOnly() ) { return; }
  320. global $wgProfilePerHost;
  321. $dbw = wfGetDB( DB_MASTER );
  322. if( !is_object( $dbw ) )
  323. return false;
  324. $errorState = $dbw->ignoreErrors( true );
  325. $name = substr($name, 0, 255);
  326. if( $wgProfilePerHost ){
  327. $pfhost = wfHostname();
  328. } else {
  329. $pfhost = '';
  330. }
  331. // Kludge
  332. $timeSum = ($timeSum >= 0) ? $timeSum : 0;
  333. $memorySum = ($memorySum >= 0) ? $memorySum : 0;
  334. $dbw->update( 'profiling',
  335. array(
  336. "pf_count=pf_count+{$eventCount}",
  337. "pf_time=pf_time+{$timeSum}",
  338. "pf_memory=pf_memory+{$memorySum}",
  339. ),
  340. array(
  341. 'pf_name' => $name,
  342. 'pf_server' => $pfhost,
  343. ),
  344. __METHOD__ );
  345. $rc = $dbw->affectedRows();
  346. if ($rc == 0) {
  347. $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount,
  348. 'pf_time' => $timeSum, 'pf_memory' => $memorySum, 'pf_server' => $pfhost ),
  349. __METHOD__, array ('IGNORE'));
  350. }
  351. // When we upgrade to mysql 4.1, the insert+update
  352. // can be merged into just a insert with this construct added:
  353. // "ON DUPLICATE KEY UPDATE ".
  354. // "pf_count=pf_count + VALUES(pf_count), ".
  355. // "pf_time=pf_time + VALUES(pf_time)";
  356. $dbw->ignoreErrors( $errorState );
  357. }
  358. /**
  359. * Get the function name of the current profiling section
  360. */
  361. function getCurrentSection() {
  362. $elt = end( $this->mWorkStack );
  363. return $elt[0];
  364. }
  365. /**
  366. * Get function caller
  367. * @param $level int
  368. */
  369. static function getCaller( $level ) {
  370. $backtrace = wfDebugBacktrace();
  371. if ( isset( $backtrace[$level] ) ) {
  372. if ( isset( $backtrace[$level]['class'] ) ) {
  373. $caller = $backtrace[$level]['class'] . '::' . $backtrace[$level]['function'];
  374. } else {
  375. $caller = $backtrace[$level]['function'];
  376. }
  377. } else {
  378. $caller = 'unknown';
  379. }
  380. return $caller;
  381. }
  382. /**
  383. * Add an entry in the debug log file
  384. * @param $s string to output
  385. */
  386. function debug( $s ) {
  387. if( function_exists( 'wfDebug' ) ) {
  388. wfDebug( $s );
  389. }
  390. }
  391. }