/[dtapublic]/swprojs/trunk/libs/php/lib_dta/style/shared/cpuusagestatclock.inc
ViewVC logotype

Annotation of /swprojs/trunk/libs/php/lib_dta/style/shared/cpuusagestatclock.inc

Parent Directory Parent Directory | Revision Log Revision Log


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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25