xref: /iperf/src/timer.c (revision fda84f3f)
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 
12 
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     return ((tv1->tv_sec - tv0->tv_sec) + (abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0));
28 }
29 
30 int
31 timer_expired(struct timer * tp)
32 {
33     /* for timer with zero time */
34     if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec)
35     {
36 	//printf(" timer_expired: begining and end times are equal \n");
37 	return 0;
38     }
39 
40     struct timeval now;
41     int64_t   end = 0, current = 0, diff = 0;
42 
43     //printf("checking if timer has expired \n");
44     if (gettimeofday(&now, NULL) < 0)
45     {
46 	perror("gettimeofday");
47 	return -1;
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     diff = end - current;
56     return diff <= 0;
57 
58 }
59 
60 void
61 update_timer(struct timer * tp, time_t sec, suseconds_t usec)
62 {
63     if (gettimeofday(&tp->begin, NULL) < 0)
64     {
65 	perror("gettimeofday");
66     }
67     tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
68     tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
69 
70     tp->expired = timer_expired;
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     {
80 	perror("malloc");
81 	return NULL;
82     }
83 
84     if (gettimeofday(&tp->begin, NULL) < 0)
85     {
86 	perror("gettimeofday");
87 	return NULL;
88     }
89 
90     tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
91     tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
92 
93     tp->expired = timer_expired;
94 
95     return tp;
96 }
97 
98 void
99 free_timer(struct timer * tp)
100 {
101     free(tp);
102 }
103 
104 int
105 delay(int64_t ns)
106 {
107     struct timespec req, rem;
108 
109     req.tv_sec = 0;
110 
111     while (ns >= 1000000000L)
112     {
113 	ns -= 1000000000L;
114 	req.tv_sec += 1;
115     }
116 
117     req.tv_nsec = ns;
118 
119     while (nanosleep(&req, &rem) == -1)
120 	if (EINTR == errno)
121 	    memcpy(&req, &rem, sizeof rem);
122 	else
123 	    return -1;
124     return 0;
125 }
126 
127 # ifdef DELAY_SELECT_METHOD
128 int
129 delay(int us)
130 {
131     struct timeval tv;
132 
133     tv.tv_sec = 0;
134     tv.tv_usec = us;
135     (void) select(1, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv);
136     return (1);
137 }
138 #endif
139 
140 int64_t
141 timer_remaining(struct timer * tp)
142 {
143     struct timeval now;
144     long int  end_time = 0, current_time = 0, diff = 0;
145     if (gettimeofday(&now, NULL) < 0)
146     {
147 	perror("gettimeofday");
148 	return -1;
149     }
150     end_time += tp->end.tv_sec * 1000000;
151     end_time += tp->end.tv_usec;
152 
153     current_time += now.tv_sec * 1000000;
154     current_time += now.tv_usec;
155 
156     diff = end_time - current_time;
157     if (diff > 0)
158 	return diff;
159     else
160 	return 0;
161 }
162