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