-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimers.c
185 lines (162 loc) · 3.86 KB
/
timers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Part of `snmp-query-engine`.
*
* Copyright 2012-2013, Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include "sqe.h"
struct timeval prog_start;
static JudyL timers = NULL;
struct timer *
new_timer(struct timeval *when)
{
void **sec_slot;
struct timer **usec_slot, *t;
JLI(sec_slot, timers, when->tv_sec);
if (sec_slot == PJERR)
croak(2, "new_timer: JLI(timers) failed");
if (!*sec_slot) {
PS.active_timers_sec++;
PS.total_timers_sec++;
}
JLI(usec_slot, *sec_slot, when->tv_usec);
if (usec_slot == PJERR)
croak(2, "new_timer: JLI(*sec_slot) failed");
if (!*usec_slot) {
PS.active_timers_usec++;
PS.total_timers_usec++;
t = malloc(sizeof(*t));
if (!t)
croak(2, "new_timer: malloc(timer)");
bzero(t, sizeof(*t));
t->when = *when;
TAILQ_INIT(&t->timed_out_sids);
TAILQ_INIT(&t->throttled_destinations);
*usec_slot = t;
}
return *usec_slot;
}
struct timer *
find_timer(struct timeval *when)
{
void **sec_slot;
struct timer **usec_slot;
JLG(sec_slot, timers, when->tv_sec);
if (sec_slot == PJERR)
croak(2, "find_timer: JLG(timers) failed");
if (!sec_slot) return NULL;
JLG(usec_slot, *sec_slot, when->tv_usec);
if (usec_slot == PJERR)
croak(2, "find_timer: JLG(*sec_slot) failed");
if (!usec_slot) return NULL;
return *usec_slot;
}
int
cleanup_timer(struct timer *t)
{
Word_t rc;
void **sec_slot;
struct timeval tv;
if (!t) return 1;
if (!TAILQ_EMPTY(&t->timed_out_sids)) return 0;
if (!TAILQ_EMPTY(&t->throttled_destinations)) return 0;
tv = t->when;
free(t);
JLG(sec_slot, timers, tv.tv_sec);
if (sec_slot == PJERR)
croak(2, "cleanup_timer: JLG(timers)#1 failed");
if (!sec_slot) return 1;
JLD(rc, *sec_slot, tv.tv_usec);
PS.active_timers_usec--;
JLG(sec_slot, timers, tv.tv_sec);
if (sec_slot == PJERR)
croak(2, "cleanup_timer: JLG(timers)#2 failed");
if (!sec_slot) return 1;
if (!*sec_slot) {
JLD(rc, timers, tv.tv_sec);
PS.active_timers_sec--;
}
return 1;
}
int
ms_to_next_timer(void)
{
struct timer *t;
struct timeval now;
t = next_timer();
if (!t) return 1000;
gettimeofday(&now, NULL);
if (now.tv_sec > t->when.tv_sec) return 0;
if (now.tv_sec == t->when.tv_sec) {
if (now.tv_usec > t->when.tv_usec) return 0;
return (t->when.tv_usec - now.tv_usec)/1000;
}
if (t->when.tv_sec - now.tv_sec > 5) return 1000;
return 1000*(t->when.tv_sec - now.tv_sec) + ((int)t->when.tv_usec - (int)now.tv_usec)/1000;
}
struct timer *
next_timer(void)
{
Word_t sec, usec;
void **sec_slot;
struct timer **usec_slot;
again:
sec = 0;
JLF(sec_slot, timers, sec);
if (!sec_slot) return NULL;
usec = 0;
JLF(usec_slot, *sec_slot, usec);
if (!usec_slot) return NULL;
if (cleanup_timer(*usec_slot)) goto again;
return *usec_slot;
}
struct timeval last_unclog;
void
trigger_timers(void)
{
struct timer *t;
struct timeval now;
gettimeofday(&now, NULL);
if (last_unclog.tv_sec < now.tv_sec) {
last_unclog = now;
unclog_all_destinations();
}
again:
t = next_timer();
if (!t) return;
if (t->when.tv_sec > now.tv_sec) return;
if (t->when.tv_sec == now.tv_sec && t->when.tv_usec > now.tv_usec) return;
if (!TAILQ_EMPTY(&t->throttled_destinations)) {
destination_timer(TAILQ_FIRST(&t->throttled_destinations));
goto again;
}
if (!TAILQ_EMPTY(&t->timed_out_sids)) {
sid_timer(TAILQ_FIRST(&t->timed_out_sids));
goto again;
}
cleanup_timer(t);
goto again;
}
void
set_timeout(struct timeval *tv, int timeout)
{
struct timeval to;
gettimeofday(tv, NULL);
to.tv_sec = timeout / 1000;
to.tv_usec = 1000 * (timeout % 1000);
to.tv_usec += tv->tv_usec;
to.tv_sec += tv->tv_sec;
to.tv_sec += to.tv_usec / 1000000;
to.tv_usec %= 1000000;
*tv = to;
}
int64_t
ms_passed_since(struct timeval *tv)
{
struct timeval now;
if (!tv->tv_sec) return INT_MAX;
gettimeofday(&now, NULL);
return (now.tv_sec - tv->tv_sec) * 1000 + (now.tv_usec - tv->tv_usec) / 1000;
}