1 /*---------------------------------------------------------------
2 * Copyright (c) 1999,2000,2001,2002,2003
3 * The Board of Trustees of the University of Illinois
5 *---------------------------------------------------------------
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software (Iperf) and associated
8 * documentation files (the "Software"), to deal in the Software
9 * without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute,
11 * sublicense, and/or sell copies of the Software, and to permit
12 * persons to whom the Software is furnished to do
13 * so, subject to the following conditions:
16 * Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and
18 * the following disclaimers.
21 * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimers in the documentation and/or other materials
24 * provided with the distribution.
27 * Neither the names of the University of Illinois, NCSA,
28 * nor the names of its contributors may be used to endorse
29 * or promote products derived from this Software without
30 * specific prior written permission.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
34 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
36 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
37 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
38 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 * ________________________________________________________________
41 * National Laboratory for Applied Network Research
42 * National Center for Supercomputing Applications
43 * University of Illinois at Urbana-Champaign
44 * http://www.ncsa.uiuc.edu
45 * ________________________________________________________________
48 * by Mark Gates <mgates@nlanr.net>
49 * -------------------------------------------------------------------
50 * A generic interface to a timestamp.
51 * This implementation uses the unix gettimeofday().
52 * -------------------------------------------------------------------
58 * ------------------------------------------------------------------- */
65 /* ------------------------------------------------------------------- */
68 /* -------------------------------------------------------------------
69 * Create a timestamp, with the current time in it.
70 * ------------------------------------------------------------------- */
75 /* -------------------------------------------------------------------
76 * Create a timestamp, with the given seconds/microseconds
77 * ------------------------------------------------------------------- */
78 Timestamp( long sec, long usec ) {
82 /* -------------------------------------------------------------------
83 * Create a timestamp, with the given seconds
84 * ------------------------------------------------------------------- */
85 Timestamp( double sec ) {
89 /* -------------------------------------------------------------------
90 * Set timestamp to current time.
91 * ------------------------------------------------------------------- */
93 gettimeofday( &mTime, NULL );
96 /* -------------------------------------------------------------------
97 * Set timestamp to the given seconds/microseconds
98 * ------------------------------------------------------------------- */
99 void set( long sec, long usec ) {
101 assert( usec >= 0 && usec < kMillion );
104 mTime.tv_usec = usec;
107 /* -------------------------------------------------------------------
108 * Set timestamp to the given seconds
109 * ------------------------------------------------------------------- */
110 void set( double sec ) {
111 mTime.tv_sec = (long) sec;
112 mTime.tv_usec = (long) ((sec - mTime.tv_sec) * kMillion);
115 /* -------------------------------------------------------------------
116 * return seconds portion of timestamp
117 * ------------------------------------------------------------------- */
118 long getSecs( void ) {
122 /* -------------------------------------------------------------------
123 * return microseconds portion of timestamp
124 * ------------------------------------------------------------------- */
125 long getUsecs( void ) {
126 return mTime.tv_usec;
129 /* -------------------------------------------------------------------
130 * return timestamp as a floating point seconds
131 * ------------------------------------------------------------------- */
133 return mTime.tv_sec + mTime.tv_usec / ((double) kMillion);
136 /* -------------------------------------------------------------------
137 * subtract the right timestamp from my timestamp.
138 * return the difference in microseconds.
139 * ------------------------------------------------------------------- */
140 long subUsec( Timestamp right ) {
141 return(mTime.tv_sec - right.mTime.tv_sec) * kMillion +
142 (mTime.tv_usec - right.mTime.tv_usec);
145 /* -------------------------------------------------------------------
146 * subtract the right timestamp from my timestamp.
147 * return the difference in microseconds.
148 * ------------------------------------------------------------------- */
149 long subUsec( timeval right ) {
150 return(mTime.tv_sec - right.tv_sec) * kMillion +
151 (mTime.tv_usec - right.tv_usec);
154 /* -------------------------------------------------------------------
155 * Return the number of microseconds from now to last time of setting.
156 * ------------------------------------------------------------------- */
157 long delta_usec(void) {
158 struct timeval previous = mTime;
161 return subUsec(previous);
164 /* -------------------------------------------------------------------
165 * subtract the right timestamp from my timestamp.
166 * return the difference in seconds as a floating point.
167 * ------------------------------------------------------------------- */
168 double subSec( Timestamp right ) {
169 return(mTime.tv_sec - right.mTime.tv_sec) +
170 (mTime.tv_usec - right.mTime.tv_usec) / ((double) kMillion);
173 /* -------------------------------------------------------------------
174 * add the right timestamp to my timestamp.
175 * ------------------------------------------------------------------- */
176 void add( Timestamp right ) {
177 mTime.tv_sec += right.mTime.tv_sec;
178 mTime.tv_usec += right.mTime.tv_usec;
180 // watch for under- and overflow
181 if ( mTime.tv_usec < 0 ) {
182 mTime.tv_usec += kMillion;
185 if ( mTime.tv_usec >= kMillion ) {
186 mTime.tv_usec -= kMillion;
190 assert( mTime.tv_usec >= 0 &&
191 mTime.tv_usec < kMillion );
194 /* -------------------------------------------------------------------
195 * add the seconds to my timestamp.
197 * ------------------------------------------------------------------- */
198 void add( double sec ) {
199 mTime.tv_sec += (long) sec;
200 mTime.tv_usec += (long) ((sec - ((long) sec )) * kMillion);
202 // watch for overflow
203 if ( mTime.tv_usec >= kMillion ) {
204 mTime.tv_usec -= kMillion;
208 assert( mTime.tv_usec >= 0 &&
209 mTime.tv_usec < kMillion );
212 /* -------------------------------------------------------------------
213 * return true if my timestamp is before the right timestamp.
214 * ------------------------------------------------------------------- */
215 bool before( timeval right ) {
216 return mTime.tv_sec < right.tv_sec ||
217 (mTime.tv_sec == right.tv_sec &&
218 mTime.tv_usec < right.tv_usec);
220 bool before( Timestamp right ) { return before(right.mTime); }
222 /* -------------------------------------------------------------------
223 * return true if my timestamp is after the right timestamp.
224 * ------------------------------------------------------------------- */
225 bool after( timeval right ) {
226 return mTime.tv_sec > right.tv_sec ||
227 (mTime.tv_sec == right.tv_sec &&
228 mTime.tv_usec > right.tv_usec);
230 bool after( Timestamp right ) { return after(right.mTime); }
233 * This function returns the fraction of time elapsed after the beginning
236 double fraction(Timestamp currentTime, Timestamp endTime) {
237 if ( (currentTime.after(*this)) && (endTime.after(currentTime)) ) {
238 return(((double)currentTime.subUsec(*this)) /
239 ((double)endTime.subUsec(*this)));
251 struct timeval mTime;
253 }; // end class Timestamp
255 #endif // TIMESTAMP_H