1 /*
2  * iperf, Copyright (c) 2014-2018, The Regents of the University of
3  * California, through Lawrence Berkeley National Laboratory (subject
4  * to receipt of any required approvals from the U.S. Dept. of
5  * Energy).  All rights reserved.
6  *
7  * If you have questions about your rights to use or distribute this
8  * software, please contact Berkeley Lab's Technology Transfer
9  * Department at TTD@lbl.gov.
10  *
11  * NOTICE.  This software is owned by the U.S. Department of Energy.
12  * As such, the U.S. Government has been granted for itself and others
13  * acting on its behalf a paid-up, nonexclusive, irrevocable,
14  * worldwide license in the Software to reproduce, prepare derivative
15  * works, and perform publicly and display publicly.  Beginning five
16  * (5) years after the date permission to assert copyright is obtained
17  * from the U.S. Department of Energy, and subject to any subsequent
18  * five (5) year renewals, the U.S. Government is granted for itself
19  * and others acting on its behalf a paid-up, nonexclusive,
20  * irrevocable, worldwide license in the Software to reproduce,
21  * prepare derivative works, distribute copies to the public, perform
22  * publicly and display publicly, and to permit others to do so.
23  *
24  * This code is distributed under a BSD style license, see the LICENSE
25  * file for complete information.
26  */
27 
28 
29 #include <stddef.h>
30 
31 #include "iperf_config.h"
32 #include "iperf_time.h"
33 
34 #ifdef HAVE_CLOCK_GETTIME
35 
36 #include <time.h>
37 
38 int
iperf_time_now(struct iperf_time * time1)39 iperf_time_now(struct iperf_time *time1)
40 {
41     struct timespec ts;
42     int result;
43     result = clock_gettime(CLOCK_MONOTONIC, &ts);
44     if (result == 0) {
45         time1->secs = (uint32_t) ts.tv_sec;
46         time1->usecs = (uint32_t) ts.tv_nsec / 1000;
47     }
48     return result;
49 }
50 
51 #else
52 
53 #include <sys/time.h>
54 
55 int
iperf_time_now(struct iperf_time * time1)56 iperf_time_now(struct iperf_time *time1)
57 {
58     struct timeval tv;
59     int result;
60     result = gettimeofday(&tv, NULL);
61     time1->secs = tv.tv_sec;
62     time1->usecs = tv.tv_usec;
63     return result;
64 }
65 
66 #endif
67 
68 /* iperf_time_add_usecs
69  *
70  * Add a number of microseconds to a iperf_time.
71  */
72 void
iperf_time_add_usecs(struct iperf_time * time1,uint64_t usecs)73 iperf_time_add_usecs(struct iperf_time *time1, uint64_t usecs)
74 {
75     time1->secs += usecs / 1000000L;
76     time1->usecs += usecs % 1000000L;
77     if ( time1->usecs >= 1000000L ) {
78         time1->secs += time1->usecs / 1000000L;
79         time1->usecs %= 1000000L;
80     }
81 }
82 
83 uint64_t
iperf_time_in_usecs(struct iperf_time * time)84 iperf_time_in_usecs(struct iperf_time *time)
85 {
86     return time->secs * 1000000LL + time->usecs;
87 }
88 
89 double
iperf_time_in_secs(struct iperf_time * time)90 iperf_time_in_secs(struct iperf_time *time)
91 {
92     return time->secs + time->usecs / 1000000.0;
93 }
94 
95 /* iperf_time_compare
96  *
97  * Compare two timestamps
98  *
99  * Returns -1 if time1 is earlier, 1 if time1 is later,
100  * or 0 if the timestamps are equal.
101  */
102 int
iperf_time_compare(struct iperf_time * time1,struct iperf_time * time2)103 iperf_time_compare(struct iperf_time *time1, struct iperf_time *time2)
104 {
105     if (time1->secs < time2->secs)
106         return -1;
107     if (time1->secs > time2->secs)
108         return 1;
109     if (time1->usecs < time2->usecs)
110         return -1;
111     if (time1->usecs > time2->usecs)
112         return 1;
113     return 0;
114 }
115 
116 /* iperf_time_diff
117  *
118  * Calculates the time from time2 to time1, assuming time1 is later than time2.
119  * The diff will always be positive, so the return value should be checked
120  * to determine if time1 was earlier than time2.
121  *
122  * Returns 1 if the time1 is less than or equal to time2, otherwise 0.
123  */
124 int
iperf_time_diff(struct iperf_time * time1,struct iperf_time * time2,struct iperf_time * diff)125 iperf_time_diff(struct iperf_time *time1, struct iperf_time *time2, struct iperf_time *diff)
126 {
127     int past = 0;
128     int cmp = 0;
129 
130     cmp = iperf_time_compare(time1, time2);
131     if (cmp == 0) {
132         diff->secs = 0;
133         diff->usecs = 0;
134         past = 1;
135     }
136     else if (cmp == 1) {
137         diff->secs = time1->secs - time2->secs;
138         diff->usecs = time1->usecs;
139         if (diff->usecs < time2->usecs) {
140             diff->secs -= 1;
141             diff->usecs += 1000000;
142         }
143         diff->usecs = diff->usecs - time2->usecs;
144     } else {
145         diff->secs = time2->secs - time1->secs;
146         diff->usecs = time2->usecs;
147         if (diff->usecs < time1->usecs) {
148             diff->secs -= 1;
149             diff->usecs += 1000000;
150         }
151         diff->usecs = diff->usecs - time1->usecs;
152         past = 1;
153     }
154 
155     return past;
156 }
157