/[dtapublic]/to_be_filed/webprojs/php_libraries/php_library/pamc_a/time/cpuuse.inc
ViewVC logotype

Contents of /to_be_filed/webprojs/php_libraries/php_library/pamc_a/time/cpuuse.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (show annotations) (download)
Sat Oct 8 23:35:33 2016 UTC (8 years, 2 months ago) by dashley
File size: 4316 byte(s)
Initial commit.
1 <?php
2 //
3 //********************************************************************************
4 //Copyright (C) 2006 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/06
22 //
23 //CPU time measurement and display on a page.
24 //
25 //
26 //Returns a string containing four integers separated by colons,
27 //which are, in order, system time seconds, system time microseconds,
28 //user time seconds, and user time microseconds. No left-zero padding
29 //is performed.
30 //
31 function CPUUSE_snapshot()
32 {
33 $snapshot = getrusage(); //Get the resource usage in a single structure.
34
35 //Prep and return the concatenated string.
36 return(
37 $snapshot["ru_stime.tv_sec"]
38 .
39 ":"
40 .
41 $snapshot["ru_stime.tv_usec"]
42 .
43 ":"
44 .
45 $snapshot["ru_utime.tv_sec"]
46 .
47 ":"
48 .
49 $snapshot["ru_utime.tv_usec"]
50 );
51 }
52 //
53 //Returns the difference in two timestamps. Each timestamp is in the
54 //format of colon-separated integers returned by CPUUSE_snapshot().
55 //
56 //This function won't behave properly on a negative difference (it is
57 //designed for $t1 >= $t0 only.
58 //
59 function CPUUSE_diff($t0, $t1)
60 {
61 //Split the strings at the colons.
62 //
63 $t0_exploded = explode(":", $t0);
64 $t1_exploded = explode(":", $t1);
65 //
66 //Calculate the differences. There is a little bit of trickiness in
67 //processing the microseconds, in case the seconds has rolled over.
68 //
69 $rv[0] = $t1_exploded[0] - $t0_exploded[0];
70 $rv[1] = $t1_exploded[1] - $t0_exploded[1];
71 $rv[2] = $t1_exploded[2] - $t0_exploded[2];
72 $rv[3] = $t1_exploded[3] - $t0_exploded[3];
73 if ($rv[1] < 0)
74 {
75 $rv[1] += 1000000;
76 $rv[0] -= 1;
77 }
78 if ($rv[3] < 0)
79 {
80 $rv[3] += 1000000;
81 $rv[2] -= 1;
82 }
83 //
84 //Return the result to the caller.
85 return($rv[0] . ":" . $rv[1] . ":" . $rv[2] . ":" . $rv[3]);
86 }
87 //
88 //Supplied a time difference in the format returned by CPUUSE_diff() (four colon-separated
89 //integers, which are respectively system seconds, system microseconds, user seconds, and
90 //user microseconds), returns two strings that can be used on a web page (seconds plus
91 //two decimal points). The first string is system consumption, the second is user.
92 //
93 function CPUUSE_format_display_web($input_diff, &$systime, &$usertime)
94 {
95 //Split apart the passed integers.
96 //
97 $diff_exploded = explode(":", $input_diff);
98
99 //Increase the time by 5000 microseconds to round in the second
100 //decimal place.
101 $diff_exploded[1] += 5000;
102 if ($diff_exploded[1] > 999999)
103 {
104 $diff_exploded[1] -= 1000000;
105 $diff_exploded[0] += 1;
106 }
107 $diff_exploded[3] += 5000;
108 if ($diff_exploded[3] > 999999)
109 {
110 $diff_exploded[3] -= 1000000;
111 $diff_exploded[2] += 1;
112 }
113
114 //Zero-pad system microseconds.
115 while(strlen($diff_exploded[1]) < 6)
116 {
117 $diff_exploded[1] = "0" . $diff_exploded[1];
118 }
119
120 //Zero-pad user microseconds.
121 while(strlen($diff_exploded[3]) < 6)
122 {
123 $diff_exploded[3] = "0" . $diff_exploded[3];
124 }
125
126 $systime = $diff_exploded[0] . "." . Substr($diff_exploded[1], 0, 2);
127 $usertime = $diff_exploded[2] . "." . Substr($diff_exploded[3], 0, 2);
128 }
129 //
130 ?>

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25