]> sjero.net Git - iperf/blob - include/Timestamp.hpp
ae05f859a1cadaf4c51a82898873625831a9f314
[iperf] / include / Timestamp.hpp
1 /*--------------------------------------------------------------- 
2  * Copyright (c) 1999,2000,2001,2002,2003                       
3  * The Board of Trustees of the University of Illinois            
4  * All Rights Reserved.                                           
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: 
14  *
15  *     
16  * Redistributions of source code must retain the above 
17  * copyright notice, this list of conditions and 
18  * the following disclaimers. 
19  *
20  *     
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. 
25  * 
26  *     
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. 
31  * 
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  * ________________________________________________________________ 
46  *
47  * Timestamp.hpp
48  * by Mark Gates <mgates@nlanr.net>
49  * -------------------------------------------------------------------
50  * A generic interface to a timestamp.
51  * This implementation uses the unix gettimeofday().
52  * -------------------------------------------------------------------
53  * headers
54  * uses
55  *   <sys/types.h>
56  *   <sys/time.h>
57  *   <unistd.h>
58  * ------------------------------------------------------------------- */
59
60 #ifndef TIMESTAMP_H
61 #define TIMESTAMP_H
62
63 #include "headers.h"
64
65 /* ------------------------------------------------------------------- */
66 class Timestamp {
67 public:
68     /* -------------------------------------------------------------------
69      * Create a timestamp, with the current time in it.
70      * ------------------------------------------------------------------- */
71     Timestamp( void ) {
72         setnow();
73     }
74
75     /* -------------------------------------------------------------------
76      * Create a timestamp, with the given seconds/microseconds
77      * ------------------------------------------------------------------- */
78     Timestamp( long sec, long usec ) {
79         set( sec, usec );
80     }
81
82     /* -------------------------------------------------------------------
83      * Create a timestamp, with the given seconds
84      * ------------------------------------------------------------------- */
85     Timestamp( double sec ) {
86         set( sec );
87     }
88
89     /* -------------------------------------------------------------------
90      * Set timestamp to current time.
91      * ------------------------------------------------------------------- */
92     void setnow( void ) {
93         gettimeofday( &mTime, NULL );
94     }
95
96     /* -------------------------------------------------------------------
97      * Set timestamp to the given seconds/microseconds
98      * ------------------------------------------------------------------- */
99     void set( long sec, long usec ) {
100         assert( sec  >= 0 );
101         assert( usec >= 0  &&  usec < kMillion );
102
103         mTime.tv_sec  = sec;
104         mTime.tv_usec = usec;      
105     }
106
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);
113     }
114
115     /* -------------------------------------------------------------------
116      * return seconds portion of timestamp
117      * ------------------------------------------------------------------- */
118     long getSecs( void ) {
119         return mTime.tv_sec;
120     }
121
122     /* -------------------------------------------------------------------
123      * return microseconds portion of timestamp
124      * ------------------------------------------------------------------- */
125     long getUsecs( void ) {
126         return mTime.tv_usec;
127     }
128
129     /* -------------------------------------------------------------------
130      * return timestamp as a floating point seconds
131      * ------------------------------------------------------------------- */
132     double get( void ) {
133         return mTime.tv_sec + mTime.tv_usec / ((double) kMillion);
134     }
135
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);
143     }
144
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);
152     }
153
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);
161     }
162
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;
169
170         // watch for under- and overflow
171         if ( mTime.tv_usec < 0 ) {
172             mTime.tv_usec += kMillion;
173             mTime.tv_sec--;
174         }
175         if ( mTime.tv_usec >= kMillion ) {
176             mTime.tv_usec -= kMillion;
177             mTime.tv_sec++;
178         }
179
180         assert( mTime.tv_usec >= 0  &&
181                 mTime.tv_usec <  kMillion );
182     }
183
184     /* -------------------------------------------------------------------
185      * add the seconds to my timestamp.
186      * TODO optimize?
187      * ------------------------------------------------------------------- */
188     void add( double sec ) {
189         mTime.tv_sec  += (long) sec;
190         mTime.tv_usec += (long) ((sec - ((long) sec )) * kMillion);
191
192         // watch for overflow
193         if ( mTime.tv_usec >= kMillion ) {
194             mTime.tv_usec -= kMillion;
195             mTime.tv_sec++;
196         }
197
198         assert( mTime.tv_usec >= 0  &&
199                 mTime.tv_usec <  kMillion );
200     }
201
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);
209     }
210     
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);
218     }
219
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);
227     }
228
229     /**
230      * This function returns the fraction of time elapsed after the beginning 
231      * till the end
232      */
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)));
237         } else {
238             return -1.0;
239         }
240     }
241
242
243 protected:
244     enum {
245         kMillion = 1000000
246     };
247
248     struct timeval mTime;
249
250 }; // end class Timestamp
251
252 #endif // TIMESTAMP_H