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 * subtract the right timestamp from my timestamp.
156 * return the difference in seconds as a floating point.
157 * ------------------------------------------------------------------- */
158 double subSec( Timestamp right ) {
159 return(mTime.tv_sec - right.mTime.tv_sec) +
160 (mTime.tv_usec - right.mTime.tv_usec) / ((double) kMillion);
163 /* -------------------------------------------------------------------
164 * add the right timestamp to my timestamp.
165 * ------------------------------------------------------------------- */
166 void add( Timestamp right ) {
167 mTime.tv_sec += right.mTime.tv_sec;
168 mTime.tv_usec += right.mTime.tv_usec;
170 // watch for under- and overflow
171 if ( mTime.tv_usec < 0 ) {
172 mTime.tv_usec += kMillion;
175 if ( mTime.tv_usec >= kMillion ) {
176 mTime.tv_usec -= kMillion;
180 assert( mTime.tv_usec >= 0 &&
181 mTime.tv_usec < kMillion );
184 /* -------------------------------------------------------------------
185 * add the seconds to my timestamp.
187 * ------------------------------------------------------------------- */
188 void add( double sec ) {
189 mTime.tv_sec += (long) sec;
190 mTime.tv_usec += (long) ((sec - ((long) sec )) * kMillion);
192 // watch for overflow
193 if ( mTime.tv_usec >= kMillion ) {
194 mTime.tv_usec -= kMillion;
198 assert( mTime.tv_usec >= 0 &&
199 mTime.tv_usec < kMillion );
202 /* -------------------------------------------------------------------
203 * return true if my timestamp is before the right timestamp.
204 * ------------------------------------------------------------------- */
205 bool before( Timestamp right ) {
206 return mTime.tv_sec < right.mTime.tv_sec ||
207 (mTime.tv_sec == right.mTime.tv_sec &&
208 mTime.tv_usec < right.mTime.tv_usec);
211 /* -------------------------------------------------------------------
212 * return true if my timestamp is before the right timestamp.
213 * ------------------------------------------------------------------- */
214 bool before( timeval right ) {
215 return mTime.tv_sec < right.tv_sec ||
216 (mTime.tv_sec == right.tv_sec &&
217 mTime.tv_usec < right.tv_usec);
220 /* -------------------------------------------------------------------
221 * return true if my timestamp is after the right timestamp.
222 * ------------------------------------------------------------------- */
223 bool after( Timestamp right ) {
224 return mTime.tv_sec > right.mTime.tv_sec ||
225 (mTime.tv_sec == right.mTime.tv_sec &&
226 mTime.tv_usec > right.mTime.tv_usec);
230 * This function returns the fraction of time elapsed after the beginning
233 double fraction(Timestamp currentTime, Timestamp endTime) {
234 if ( (currentTime.after(*this)) && (endTime.after(currentTime)) ) {
235 return(((double)currentTime.subUsec(*this)) /
236 ((double)endTime.subUsec(*this)));
248 struct timeval mTime;
250 }; // end class Timestamp
252 #endif // TIMESTAMP_H