[12] | 1 | /**CFile*********************************************************************** |
---|
| 2 | |
---|
| 3 | FileName [ cpu_time.c ] |
---|
| 4 | |
---|
| 5 | PackageName [ util ] |
---|
| 6 | |
---|
| 7 | Synopsis [ System time calls ] |
---|
| 8 | |
---|
| 9 | Description [ The problem is that all unix systems have a different notion |
---|
| 10 | of how fast time goes (i.e., the units returned by). This |
---|
| 11 | returns a consistent result. ] |
---|
| 12 | |
---|
| 13 | Author [ Stephen Edwards <sedwards@eecs.berkeley.edu> and others ] |
---|
| 14 | |
---|
| 15 | Copyright [Copyright (c) 1994-1996 The Regents of the Univ. of California. |
---|
| 16 | All rights reserved. |
---|
| 17 | |
---|
| 18 | Permission is hereby granted, without written agreement and without license |
---|
| 19 | or royalty fees, to use, copy, modify, and distribute this software and its |
---|
| 20 | documentation for any purpose, provided that the above copyright notice and |
---|
| 21 | the following two paragraphs appear in all copies of this software. |
---|
| 22 | |
---|
| 23 | IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR |
---|
| 24 | DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT |
---|
| 25 | OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF |
---|
| 26 | CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 27 | |
---|
| 28 | THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
---|
| 29 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
---|
| 30 | FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN |
---|
| 31 | "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE |
---|
| 32 | MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.] |
---|
| 33 | |
---|
| 34 | ******************************************************************************/ |
---|
| 35 | |
---|
| 36 | #include "util.h" |
---|
| 37 | |
---|
| 38 | //#if HAVE_SYS_TYPES_H |
---|
| 39 | # include<sys/types.h> |
---|
| 40 | //#endif |
---|
| 41 | |
---|
| 42 | //#if HAVE_SYS_TIMES_H |
---|
| 43 | # include<sys/times.h> |
---|
| 44 | //#endif |
---|
| 45 | |
---|
| 46 | /**AutomaticStart*************************************************************/ |
---|
| 47 | |
---|
| 48 | /*---------------------------------------------------------------------------*/ |
---|
| 49 | /* Static function prototypes */ |
---|
| 50 | /*---------------------------------------------------------------------------*/ |
---|
| 51 | |
---|
| 52 | /**AutomaticEnd***************************************************************/ |
---|
| 53 | |
---|
| 54 | /**Function******************************************************************** |
---|
| 55 | |
---|
| 56 | Synopsis [ Return elapsed time in milliseconds ] |
---|
| 57 | |
---|
| 58 | Description [ Return a long which represents the elapsed time in |
---|
| 59 | milliseconds since some constant reference. <P> |
---|
| 60 | |
---|
| 61 | There are two possibilities: |
---|
| 62 | <OL> |
---|
| 63 | <LI> The system is non-POSIX compliant, so unistd.h |
---|
| 64 | and hence sysconf() can't tell us the clock tick |
---|
| 65 | speed. At this point, we have to resort to |
---|
| 66 | using the user-settable CLOCK_RESOLUTION definition |
---|
| 67 | to get the right speed |
---|
| 68 | <LI> The system is POSIX-compliant. unistd.h gives |
---|
| 69 | us sysconf(), which tells us the clock rate. |
---|
| 70 | </OL> |
---|
| 71 | ] |
---|
| 72 | |
---|
| 73 | SideEffects [ none ] |
---|
| 74 | |
---|
| 75 | ******************************************************************************/ |
---|
| 76 | long |
---|
| 77 | util_cpu_time(void) |
---|
| 78 | { |
---|
| 79 | long t = 0; |
---|
| 80 | |
---|
| 81 | #if HAVE_SYSCONF == 1 |
---|
| 82 | |
---|
| 83 | /* Code for POSIX systems */ |
---|
| 84 | |
---|
| 85 | struct tms buffer; |
---|
| 86 | long nticks; /* number of clock ticks per second */ |
---|
| 87 | |
---|
| 88 | nticks = sysconf(_SC_CLK_TCK); |
---|
| 89 | times(&buffer); |
---|
| 90 | t = (long) (buffer.tms_utime * (1000.0/nticks)); |
---|
| 91 | |
---|
| 92 | #else |
---|
| 93 | # ifndef vms |
---|
| 94 | |
---|
| 95 | /* Code for non-POSIX systems */ |
---|
| 96 | |
---|
| 97 | struct tms buffer; |
---|
| 98 | |
---|
| 99 | time(&buffer); |
---|
| 100 | t = buffer.tms_utime * 1000.0 / CLOCK_RESOLUTION; |
---|
| 101 | |
---|
| 102 | # else |
---|
| 103 | |
---|
| 104 | /* Code for VMS (?) */ |
---|
| 105 | |
---|
| 106 | struct {int p1, p2, p3, p4;} buffer; |
---|
| 107 | static long ref_time; |
---|
| 108 | times(&buffer); |
---|
| 109 | t = buffer.p1 * 10; |
---|
| 110 | if (ref_time == 0) |
---|
| 111 | ref_time = t; |
---|
| 112 | t = t - ref_time; |
---|
| 113 | |
---|
| 114 | # endif /* vms */ |
---|
| 115 | #endif /* _POSIX_VERSION */ |
---|
| 116 | |
---|
| 117 | return t; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /**Function******************************************************************** |
---|
| 121 | |
---|
| 122 | Synopsis [ Return elapsed time in milliseconds. It includes waited- |
---|
| 123 | for terminated children. ] |
---|
| 124 | |
---|
| 125 | Description [ Return a long which represents the elapsed time in |
---|
| 126 | milliseconds since some constant reference. This time |
---|
| 127 | includes the CPU time spent executing instructions of |
---|
| 128 | the calling process and the time this process waited |
---|
| 129 | for its children to be terminated<P> |
---|
| 130 | |
---|
| 131 | There are two possibilities: |
---|
| 132 | <OL> |
---|
| 133 | <LI> The system is non-POSIX compliant, so unistd.h |
---|
| 134 | and hence sysconf() can't tell us the clock tick |
---|
| 135 | speed. At this point, we have to resort to |
---|
| 136 | using the user-settable CLOCK_RESOLUTION definition |
---|
| 137 | to get the right speed |
---|
| 138 | <LI> The system is POSIX-compliant. unistd.h gives |
---|
| 139 | us sysconf(), which tells us the clock rate. |
---|
| 140 | </OL> |
---|
| 141 | ] |
---|
| 142 | |
---|
| 143 | SideEffects [ none ] |
---|
| 144 | |
---|
| 145 | ******************************************************************************/ |
---|
| 146 | long |
---|
| 147 | util_cpu_ctime(void) |
---|
| 148 | { |
---|
| 149 | long t = 0; |
---|
| 150 | |
---|
| 151 | #if HAVE_SYSCONF == 1 |
---|
| 152 | |
---|
| 153 | /* Code for POSIX systems */ |
---|
| 154 | |
---|
| 155 | struct tms buffer; |
---|
| 156 | long nticks; /* number of clock ticks per second */ |
---|
| 157 | |
---|
| 158 | nticks = sysconf(_SC_CLK_TCK); |
---|
| 159 | times(&buffer); |
---|
| 160 | t = (long) ((buffer.tms_utime + buffer.tms_cutime) * (1000.0/nticks)); |
---|
| 161 | |
---|
| 162 | #else |
---|
| 163 | # ifndef vms |
---|
| 164 | |
---|
| 165 | /* Code for non-POSIX systems */ |
---|
| 166 | |
---|
| 167 | struct tms buffer; |
---|
| 168 | |
---|
| 169 | time(&buffer); |
---|
| 170 | t = (buffer.tms_utime + buffer.tms_cutime) * 1000.0 / CLOCK_RESOLUTION; |
---|
| 171 | |
---|
| 172 | # else |
---|
| 173 | |
---|
| 174 | /* Code for VMS (?) */ |
---|
| 175 | |
---|
| 176 | struct {int p1, p2, p3, p4;} buffer; |
---|
| 177 | static long ref_time; |
---|
| 178 | times(&buffer); |
---|
| 179 | t = (buffer.p1 + buffer.p3) * 10; |
---|
| 180 | if (ref_time == 0) |
---|
| 181 | ref_time = t; |
---|
| 182 | t = t - ref_time; |
---|
| 183 | |
---|
| 184 | # endif /* vms */ |
---|
| 185 | #endif /* _POSIX_VERSION */ |
---|
| 186 | |
---|
| 187 | return t; |
---|
| 188 | } |
---|
| 189 | |
---|