/[dtapublic]/to_be_filed/webprojs/php_libraries/php_library/general/cpuusagestatclock.inc
ViewVC logotype

Annotation of /to_be_filed/webprojs/php_libraries/php_library/general/cpuusagestatclock.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (hide annotations) (download)
Sat Oct 8 23:35:33 2016 UTC (8 years, 2 months ago) by dashley
File size: 6737 byte(s)
Initial commit.
1 dashley 35 <?php
2     //
3     //********************************************************************************
4     //Copyright (C) 2003 David T. Ashley
5     //********************************************************************************
6     //This program or source file is free software; you can redistribute it and/or
7     //modify it under the terms of the GNU General Public License as published by
8     //the Free Software Foundation; either version 2 of the License, or (at your
9     //option) any later version.
10     //
11     //This program or source file is distributed in the hope that it will
12     //be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13     //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     //GNU General Public License for more details.
15     //
16     //You may have received a copy of the GNU General Public License
17     //along with this program; if not, write to the Free Software
18     //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     //********************************************************************************
20     //
21     //Dave Ashley, 01/03
22     //
23     //This defines a simple PHP class to make it easier to implement a page statistic
24     //that indicates how much CPU time was used by a page or a portion of a page.
25     //The statistics are not always straightforward because each load of a page
26     //may just add more used time to the same process. Stats must be obtained
27     //by subtraction.
28     //
29     //The basic strategy is that a clock is constructed then may be alternatively
30     //started, stopped, and polled. It is necessary to stop a clock before
31     //polling it.
32     //
33     class Cpuusagestatclock
34     {
35     //The fields below are updated each time the clock is stopped.
36     var $utime_s;
37     //The seconds portion of the amount of user time consumed.
38     var $utime_m;
39     //The microseconds portion of the amount of user time consumed.
40     var $stime_s;
41     //The seconds portion of the amount of system time consumed.
42     var $stime_m;
43     //The microseconds portion of the amount of system time consumed.
44    
45     //The following fields are analogous to the ones above, except they
46     //hold time snapshot values from when the clock was most recently
47     //started.
48     var $ss_utime_s;
49     var $ss_utime_m;
50     var $ss_stime_s;
51     var $ss_stime_m;
52    
53     //This is the constructor function.
54     function Cpuusagestatclock()
55     {
56     //All that is required at construction is to set the cumulative
57     //values to zero.
58     $this->utime_s = 0;
59     $this->utime_m = 0;
60     $this->stime_s = 0;
61     $this->stime_m = 0;
62     }
63    
64     //This starts the clock. This just records a static time usage mark.
65     function start()
66     {
67     //Get the snapshot from the system.
68     $snapshot = getrusage();
69    
70     //Record the values of interest.
71     $this->ss_utime_s = $snapshot["ru_utime.tv_sec"];
72     $this->ss_utime_m = $snapshot["ru_utime.tv_usec"];
73     $this->ss_stime_s = $snapshot["ru_stime.tv_sec"];
74     $this->ss_stime_m = $snapshot["ru_stime.tv_usec"];
75     }
76    
77    
78     //This stops the clock. This calculates the used time and adds it in to
79     //the totals.
80     function stop()
81     {
82     //Get the snapshot from the system.
83     $snapshot = getrusage();
84    
85     //Calculate and add in the elapsed user time usage since the snapshot.
86     $this->utime_s += ($snapshot["ru_utime.tv_sec"] - $this->ss_utime_s);
87     $this->utime_m += ($snapshot["ru_utime.tv_usec"] - $this->ss_utime_m);
88     while ($this->utime_m < 0)
89     {
90     $this->utime_m += 1000000;
91     $this->utime_s--;
92     }
93    
94     //Calculate and add in the elapsed system time usage since the snapshot.
95     $this->stime_s += ($snapshot["ru_stime.tv_sec"] - $this->ss_stime_s);
96     $this->stime_m += ($snapshot["ru_stime.tv_usec"] - $this->ss_stime_m);
97     while ($this->stime_m < 0)
98     {
99     $this->stime_m += 1000000;
100     $this->stime_s--;
101     }
102     }
103    
104     //This returns the user time microseconds elapsed of a stopped clock, with no padding.
105     function utime_raw_us()
106     {
107     return($this->utime_m);
108     }
109    
110     //This returns the user time microseconds elapsed of a stopped clock, padded
111     //with leading zeros to be six digits long.
112     function utime_formatted_us()
113     {
114     $rv = $this->utime_raw_us();
115    
116     while(strlen($rv) < 6)
117     $rv = "0" . $rv;
118    
119     return($rv);
120     }
121    
122     //This returns the system time microseconds elapsed of a stopped clock, with no padding.
123     function stime_raw_us()
124     {
125     return($this->stime_m);
126     }
127    
128     //This returns the system time microseconds elapsed of a stopped clock, padded
129     //with leading zeros to be six digits long.
130     function stime_formatted_us()
131     {
132     $rv = $this->stime_raw_us();
133    
134     while(strlen($rv) < 6)
135     $rv = "0" . $rv;
136    
137     return($rv);
138     }
139    
140     //This returns the total time microseconds elapsed of a stopped clock, with no padding.
141     function ttime_raw_us()
142     {
143     $rv = $this->stime_m + $this->utime_m;
144    
145     if ($rv >= 1000000)
146     $rv -= 1000000;
147    
148     return($rv);
149     }
150    
151     //This returns the total time microseconds elapsed of a stopped clock, padded
152     //with leading zeros to be six digits long.
153     function ttime_formatted_us()
154     {
155     $rv = $this->ttime_raw_us();
156    
157     while(strlen($rv) < 6)
158     $rv = "0" . $rv;
159    
160     return($rv);
161     }
162    
163     //This returns the user time seconds elapsed of a stopped clock.
164     function utime_s()
165     {
166     return($this->utime_s);
167     }
168    
169     //This returns the system time seconds elapsed of a stopped clock.
170     function stime_s()
171     {
172     return($this->stime_s);
173     }
174    
175     //This returns the total time seconds elapsed of a stopped clock.
176     function ttime_s()
177     {
178     $rv = $this->stime_s + $this->utime_s;
179    
180     $us_total = $this->stime_m + $this->utime_m;
181    
182     if ($us_total >= 1000000)
183     $rv++;
184    
185     return($rv);
186     }
187    
188     //This returns the standard string for a footer of a web page. No HTML formatting tags
189     //are included.
190     function std_web_page_cpu_time_usage_footer()
191     {
192     return
193     (
194     "This page required approximately "
195     .
196     $this->ttime_s()
197     .
198     "."
199     .
200     Substr($this->ttime_formatted_us(), 0, 2)
201     .
202     "s ("
203     .
204     $this->stime_s()
205     .
206     "."
207     .
208     Substr($this->stime_formatted_us(), 0, 2)
209     .
210     "s system, "
211     .
212     $this->utime_s()
213     .
214     "."
215     .
216     Substr($this->utime_formatted_us(), 0, 2)
217     .
218     "s user) of CPU time on the server to generate."
219     );
220     }
221     }
222     ?>

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25