Follow up r47852. Unlike forkWorkers(), the start() method doesn't get the $procs...
[lhc/web/wiklou.git] / includes / ProfilerSimple.php
1 <?php
2 /**
3 * @file
4 * @ingroup Profiler
5 */
6
7 if ( !class_exists( 'Profiler' ) ) {
8 require_once(dirname(__FILE__).'/Profiler.php');
9 }
10
11 /**
12 * Simple profiler base class.
13 * @todo document methods (?)
14 * @ingroup Profiler
15 */
16 class ProfilerSimple extends Profiler {
17 var $mMinimumTime = 0;
18 var $mProfileID = false;
19
20 function __construct() {
21 global $wgRequestTime, $wgRUstart;
22 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
23 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
24
25 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
26 $elapsedreal = microtime(true) - $wgRequestTime;
27
28 $entry =& $this->mCollated["-setup"];
29 if (!is_array($entry)) {
30 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
31 $this->mCollated["-setup"] =& $entry;
32 }
33 $entry['cpu'] += $elapsedcpu;
34 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
35 $entry['real'] += $elapsedreal;
36 $entry['real_sq'] += $elapsedreal*$elapsedreal;
37 $entry['count']++;
38 }
39 }
40
41 function setMinimum( $min ) {
42 $this->mMinimumTime = $min;
43 }
44
45 function setProfileID( $id ) {
46 $this->mProfileID = $id;
47 }
48
49 function getProfileID() {
50 if ( $this->mProfileID === false ) {
51 return wfWikiID();
52 } else {
53 return $this->mProfileID;
54 }
55 }
56
57 function profileIn($functionname) {
58 global $wgDebugFunctionEntry;
59 if ($wgDebugFunctionEntry) {
60 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
61 }
62 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
63 }
64
65 function profileOut($functionname) {
66 global $wgDebugFunctionEntry;
67
68 if ($wgDebugFunctionEntry) {
69 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
70 }
71
72 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
73
74 if (!$ofname) {
75 $this->debug("Profiling error: $functionname\n");
76 } else {
77 if ($functionname == 'close') {
78 $message = "Profile section ended by close(): {$ofname}";
79 $functionname = $ofname;
80 $this->debug( "$message\n" );
81 $this->mCollated[$message] = array(
82 'real' => 0.0, 'count' => 1);
83 }
84 elseif ($ofname != $functionname) {
85 $message = "Profiling error: in({$ofname}), out($functionname)";
86 $this->debug( "$message\n" );
87 $this->mCollated[$message] = array(
88 'real' => 0.0, 'count' => 1);
89 }
90 $entry =& $this->mCollated[$functionname];
91 $elapsedcpu = $this->getCpuTime() - $octime;
92 $elapsedreal = microtime(true) - $ortime;
93 if (!is_array($entry)) {
94 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
95 $this->mCollated[$functionname] =& $entry;
96 }
97 $entry['cpu'] += $elapsedcpu;
98 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
99 $entry['real'] += $elapsedreal;
100 $entry['real_sq'] += $elapsedreal*$elapsedreal;
101 $entry['count']++;
102
103 }
104 }
105
106 function getFunctionReport() {
107 /* Implement in output subclasses */
108 }
109
110 function getCpuTime($ru=null) {
111 if ( function_exists( 'getrusage' ) ) {
112 if ( $ru == null ) {
113 $ru = getrusage();
114 }
115 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
116 $ru['ru_stime.tv_usec']) * 1e-6);
117 } else {
118 return 0;
119 }
120 }
121
122 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
123 function getTime($time=null) {
124 if ($time==null) {
125 return microtime(true);
126 }
127 list($a,$b)=explode(" ",$time);
128 return (float)($a+$b);
129 }
130 }