]> sjero.net Git - iperf/blob - include/Timestamp.hpp
Timer Utilities
[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      * Return the number of microseconds from now to last time of setting.
156      * ------------------------------------------------------------------- */
157     long delta_usec(void) {
158         struct timeval previous = mTime;
159
160         setnow();
161         return subUsec(previous);
162     }
163
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);
171     }
172
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;
179
180         // watch for under- and overflow
181         if ( mTime.tv_usec < 0 ) {
182             mTime.tv_usec += kMillion;
183             mTime.tv_sec--;
184         }
185         if ( mTime.tv_usec >= kMillion ) {
186             mTime.tv_usec -= kMillion;
187             mTime.tv_sec++;
188         }
189
190         assert( mTime.tv_usec >= 0  &&
191                 mTime.tv_usec <  kMillion );
192     }
193
194     /* -------------------------------------------------------------------
195      * add the seconds to my timestamp.
196      * TODO optimize?
197      * ------------------------------------------------------------------- */
198     void add( double sec ) {
199         mTime.tv_sec  += (long) sec;
200         mTime.tv_usec += (long) ((sec - ((long) sec )) * kMillion);
201
202         // watch for overflow
203         if ( mTime.tv_usec >= kMillion ) {
204             mTime.tv_usec -= kMillion;
205             mTime.tv_sec++;
206         }
207
208         assert( mTime.tv_usec >= 0  &&
209                 mTime.tv_usec <  kMillion );
210     }
211
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);
219     }
220     bool before( Timestamp right ) { return before(right.mTime); }
221
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);
229     }
230     bool after( Timestamp right ) { return after(right.mTime); }
231
232     /**
233      * This function returns the fraction of time elapsed after the beginning 
234      * till the end
235      */
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)));
240         } else {
241             return -1.0;
242         }
243     }
244
245
246 protected:
247     enum {
248         kMillion = 1000000
249     };
250
251     struct timeval mTime;
252
253 }; // end class Timestamp
254
255 #endif // TIMESTAMP_H