xref: /iperf/src/timer.c (revision c5038eea)
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 timeval_to_double(tv1) - timeval_to_double(tv0);
28    return  (tv1->tv_sec - tv0->tv_sec) + abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0;
29 }
30 
31 int
32 timer_expired(struct timer *tp)
33 {
34     // for timer with zero time
35     if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec)
36         return 0;
37 
38     struct timeval now;
39     int64_t  end = 0, current= 0, diff= 0;
40     if(gettimeofday(&now, NULL) < 0) {
41         perror("gettimeofday");
42         return -1;
43     }
44 
45     end+= tp->end.tv_sec * 1000000 ;
46     end+= tp->end.tv_usec;
47 
48     current+= now.tv_sec * 1000000 ;
49     current+= now.tv_usec;
50 
51     diff = end - current;
52 
53    return diff <= 0;
54 
55     // currently using microsecond limit. Else we need to introduce timespec instread of timeval
56 }
57 
58 void
59 update_timer(struct timer *tp, time_t sec, suseconds_t usec)
60 {
61     if(gettimeofday(&tp->begin, NULL) < 0) {
62         perror("gettimeofday");
63     }
64 
65     memcpy(&tp->end, &tp->begin, sizeof(struct timer));
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 }
71 
72 struct timer *
73 new_timer(time_t sec, suseconds_t usec)
74 {
75     struct timer *tp;
76     tp = (struct timer *) malloc(sizeof(struct timer));
77 
78     if(gettimeofday(&tp->begin, NULL) < 0) {
79         perror("gettimeofday");
80         return NULL;
81     }
82 
83     memcpy(&tp->end, &tp->begin, sizeof(struct timer));
84     tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
85     tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
86 
87     tp->expired = timer_expired;
88 
89     return tp;
90 }
91 
92 void
93 free_timer(struct timer *tp)
94 {
95     free(tp);
96 }
97 
98 int
99 delay(int64_t ns)
100 {
101     struct timespec req, rem;
102 
103     req.tv_sec = 0;
104 
105     while(ns >= 1000000000L)
106     {
107         ns -= 1000000000L;
108         req.tv_sec += 1;
109     }
110 
111     req.tv_nsec = ns;
112 
113     while(nanosleep(&req, &rem) == -1 )
114         if (EINTR == errno)
115             memcpy(&req, &rem, sizeof rem);
116         else
117             return -1;
118 
119     return 0;
120 }
121 
122 int64_t
123 timer_remaining(struct timer *tp)
124 {
125     struct timeval now;
126     long int end_time = 0, current_time= 0, diff= 0;
127     if(gettimeofday(&now, NULL) < 0) {
128         perror("gettimeofday");
129         return -1;
130     }
131 
132     end_time+= tp->end.tv_sec * 1000000 ;
133     end_time+= tp->end.tv_usec;
134 
135     current_time+= now.tv_sec * 1000000 ;
136     current_time+= now.tv_usec;
137 
138     diff = end_time - current_time;
139     if(diff > 0)
140         return diff;
141     else
142         return 0;
143 }
144 
145 
146