xref: /iperf/src/timer.c (revision 0bd8d9da)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <sys/errno.h>
6 #include <sys/types.h>
7 #include <stdint.h>
8 #include <time.h>
9 
10 #include "timer.h"
11 #include "iperf_error.h"
12 
13 double
14 timeval_to_double(struct timeval * tv)
15 {
16     double d;
17 
18     d = tv->tv_sec + tv->tv_usec / 1000000;
19 
20     return d;
21 }
22 
23 double
24 timeval_diff(struct timeval * tv0, struct timeval * tv1)
25 {
26     double time1, time2;
27 
28     time1 = tv0->tv_sec + (tv0->tv_usec / 1000000.0);
29     time2 = tv1->tv_sec + (tv1->tv_usec / 1000000.0);
30 
31     time1 = time1 - time2;
32     if (time1 < 0)
33         time1 = -time1;
34     return (time1);
35 }
36 
37 int
38 timer_expired(struct timer * tp)
39 {
40     if (tp == NULL)
41         return 0;
42 
43     struct timeval now;
44     int64_t end = 0, current = 0;
45 
46     gettimeofday(&now, NULL);
47 
48     end += tp->end.tv_sec * 1000000;
49     end += tp->end.tv_usec;
50 
51     current += now.tv_sec * 1000000;
52     current += now.tv_usec;
53 
54     return current > end;
55 }
56 
57 int
58 update_timer(struct timer * tp, time_t sec, suseconds_t usec)
59 {
60     if (gettimeofday(&tp->begin, NULL) < 0) {
61         i_errno = IEUPDATETIMER;
62         return (-1);
63     }
64 
65     tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
66     tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
67 
68     tp->expired = timer_expired;
69     return (0);
70 }
71 
72 struct timer *
73 new_timer(time_t sec, suseconds_t usec)
74 {
75     struct timer *tp = NULL;
76     tp = (struct timer *) calloc(1, sizeof(struct timer));
77     if (tp == NULL) {
78         i_errno = IENEWTIMER;
79         return (NULL);
80     }
81 
82     if (gettimeofday(&tp->begin, NULL) < 0) {
83         i_errno = IENEWTIMER;
84         return (NULL);
85     }
86 
87     tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
88     tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
89 
90     tp->expired = timer_expired;
91 
92     return tp;
93 }
94 
95 void
96 free_timer(struct timer * tp)
97 {
98     free(tp);
99 }
100 
101 int
102 delay(int64_t ns)
103 {
104     struct timespec req, rem;
105 
106     req.tv_sec = 0;
107 
108     while (ns >= 1000000000L) {
109         ns -= 1000000000L;
110         req.tv_sec += 1;
111     }
112 
113     req.tv_nsec = ns;
114 
115     while (nanosleep(&req, &rem) == -1)
116         if (EINTR == errno)
117             memcpy(&req, &rem, sizeof rem);
118         else
119             return -1;
120     return 0;
121 }
122 
123 # ifdef DELAY_SELECT_METHOD
124 int
125 delay(int us)
126 {
127     struct timeval tv;
128 
129     tv.tv_sec = 0;
130     tv.tv_usec = us;
131     (void) select(1, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv);
132     return (1);
133 }
134 #endif
135 
136 int64_t
137 timer_remaining(struct timer * tp)
138 {
139     struct timeval now;
140     long int  end_time = 0, current_time = 0, diff = 0;
141 
142     gettimeofday(&now, NULL);
143 
144     end_time += tp->end.tv_sec * 1000000;
145     end_time += tp->end.tv_usec;
146 
147     current_time += now.tv_sec * 1000000;
148     current_time += now.tv_usec;
149 
150     diff = end_time - current_time;
151     if (diff > 0)
152         return diff;
153     else
154         return 0;
155 }
156