-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.c
626 lines (527 loc) · 17.8 KB
/
timer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/* timer.c - schedule periodic events based on timer interrupts
*
* Copyright (C) 2012, Greg Johnson
* Released under the terms of the GNU GPL v2.0.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* $Id: timer.c,v 1.16 2012-02-22 19:27:23 greg Exp $
*/
static const char Version[] = "Version "
"$Id: timer.c,v 1.16 2012-02-22 19:27:23 greg Exp $";
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "util.h"
#include "cloud.h"
#include "timer.h"
#include "print.h"
#include "random.h"
#include "sequence.h"
#include "stp_beacon.h"
#include <sys/time.h>
int intervals[TIMER_COUNT] = {
0,
WRT_UPDATE_INTERVAL,
ETH_UPDATE_INTERVAL,
0,
0,
0,
0,
0,
};
struct timeval times[TIMER_COUNT] = {
{0, 0},
{0, 0},
{0, 0},
{-1, 0},
{-1, 0},
{0, 0},
{0, 0},
{0, 0},
};
struct timeval now;
struct timeval start;
char got_interrupt[TIMER_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0};
int interrupt_pipe[2];
/* this routine is called periodically based on timed interrupts.
* it sends a character into a pipe we use to talk to ourselves.
* the arrival of a character causes the main routine to break out
* of its "select()" statement and do periodic activities that we
* are supposed to initiate, such as sending out stp beacons.
*/
void send_interrupt_pipe_char()
{
char c = 'x';
int result = write(interrupt_pipe[1], &c, 1);
if (result == -1) {
ddprintf("send_interrupt_pipe_char; write failed: %s\n",
strerror(errno));
return;
}
}
/* this is the routine that gets called when a timer interrupt takes place.
* to avoid the complications of having to worry about messing with data
* structures at the interrupt level when the mainline code might be
* manipulating them, we simply notify that mainline code that a timer
* interrupt took place, and let it handle it. we do this by putting a
* character into a pipe that is one of the things the mainline routine
* selects on when it is looking for input.
*
* also, set the next timer alarm.
*/
void repeated(int arg)
{
send_interrupt_pipe_char();
set_next_alarm();
}
/* create the pipe we use to send interrupts to ourselves. */
void timer_init()
{
if (pipe(interrupt_pipe) == -1) {
ddprintf("main; pipe creation failed: %s\n", strerror(errno));
exit(1);
}
}
/* block or unblock timer interrupts based on "todo" */
void block_timer_interrupts(int todo)
{
sigset_t blockum;
int result;
while (true) {
if ((result = sigemptyset(&blockum)) == -1) { continue; }
if ((result = sigaddset(&blockum, SIGALRM)) == -1) { continue; }
if ((result = sigprocmask(todo, &blockum, NULL)) == -1) {
ddprintf("block_timer_interrupts problem: %s\n",
strerror(errno));
}
break;
}
}
void stop_alarm(void)
{
int result;
struct itimerval timer = {{0, 0}, {0, 0}};
result = setitimer(ITIMER_REAL, &timer, 0);
}
/* set an alarm to go off msec milliseconds in the future. */
void set_alarm(int msec)
{
int result;
struct itimerval timer;
if (msec == 0) { msec = 1; }
timer.it_value.tv_sec = msec / 1000;
timer.it_value.tv_usec = (msec % 1000) * 1000;
/* make sure that we get an interrupt at least once per second. */
timer.it_interval.tv_sec = SAFETY_INTERVAL;
timer.it_interval.tv_usec = 0;
result = setitimer(ITIMER_REAL, &timer, 0);
}
/* debug-print the amount of time that has transpired since the box was
* turned on in seconds, with fraction showing hundreth's of seconds.
*/
void ptime(char *title, struct timeval time)
{
ddprintf("%s: %.2f; ",
title,
(time.tv_sec == -1)
? -1
: ((double) usec_diff(time.tv_sec, time.tv_usec,
start.tv_sec, start.tv_usec) / 1000000.));
}
/* debug-print the amount of time that has transpired since the box was
* turen on in seconds, with fraction showing hundreth's of seconds.
*/
void dptime(char *title, long sec, long usec)
{
struct timeval time;
time.tv_sec = sec;
time.tv_usec = usec;
ptime(title, time);
}
/* debug print current timer values */
void timer_print()
{
ptime("now ", now); ddprintf("\n");
ptime("send_stp ", times[0]); ddprintf("\n");
ptime("process_beacon ", times[1]); ddprintf("\n");
ptime("process_eth_beacon ", times[2]); ddprintf("\n");
ptime("noncloud_message ", times[3]); ddprintf("\n");
ptime("lockable_timeout ", times[4]); ddprintf("\n");
ptime("print_cloud ", times[5]); ddprintf("\n");
ptime("ping_neighbors ", times[6]); ddprintf("\n");
ptime("disable_print_cloud", times[7]); ddprintf("\n");
ptime("wifi_scan ", times[8]); ddprintf("\n");
}
/* of the various timers (currently 8), figure out which one is due to
* happen soonest, and set a timer interrupt to go off to wake us up then.
*/
void set_next_alarm()
{
long long next_interrupt, maybe_next;
while (!checked_gettimeofday(&now));
if (db[2].d) {
ptime("now", now);
ptime("t0", times[0]);
ptime("t1", times[1]);
ptime("t2", times[2]);
ptime("t3", times[3]);
ptime("t4", times[4]);
ptime("t5", times[5]);
ptime("t6", times[6]);
ptime("t7", times[7]);
ddprintf("\n");
}
/* see if we are past time of next expected interrupt here, and if so
* note it and reset timer for the future.
*/
/* this is the exponential timer for stp_beacons */
while ((next_interrupt = usec_diff(times[0].tv_sec, times[0].tv_usec,
now.tv_sec, now.tv_usec)) < 0)
{
double wait_time;
int iwait_time;
if (db[39].d) {
int mean_wait_time = MEAN_WAKEUP_TIME;
if (stp_recv_beacon_count > 0) {
mean_wait_time *= stp_recv_beacon_count;
}
wait_time = neg_exp(mean_wait_time);
} else {
wait_time = neg_exp(MEAN_WAKEUP_TIME);
}
iwait_time = (int) wait_time;
if (db[40].d) {
iwait_time *= 20;
ddprintf("set stp_send timeout to %d\n", iwait_time);
}
usec_add_msecs(×[0].tv_sec, ×[0].tv_usec, iwait_time);
if (!db[54].d) {
got_interrupt[send_stp] = 1;
}
}
/* same as above */
/* this is the wrt_beacon processing interval */
while ((maybe_next = usec_diff(times[1].tv_sec, times[1].tv_usec,
now.tv_sec, now.tv_usec)) < 0)
{
usec_add_msecs(×[1].tv_sec, ×[1].tv_usec, intervals[1]);
got_interrupt[process_beacon] = 1;
}
if (maybe_next < next_interrupt) { next_interrupt = maybe_next; }
/* same as above */
/* this is the eth_beacon processing interval */
while ((maybe_next = usec_diff(times[2].tv_sec, times[2].tv_usec,
now.tv_sec, now.tv_usec)) < 0)
{
usec_add_msecs(×[2].tv_sec, ×[2].tv_usec, intervals[2]);
got_interrupt[process_eth_beacon] = 1;
}
if (maybe_next < next_interrupt) { next_interrupt = maybe_next; }
/* non-cloud message timeout */
if (times[3].tv_sec != -1) {
maybe_next = usec_diff(times[3].tv_sec, times[3].tv_usec,
now.tv_sec, now.tv_usec);
if (maybe_next < 0) {
times[3].tv_sec = -1;
got_interrupt[noncloud_message] = 1;
} else if (maybe_next < next_interrupt) {
next_interrupt = maybe_next;
}
}
/* lockable resource timeout */
if (times[4].tv_sec != -1) {
maybe_next = usec_diff(times[4].tv_sec, times[4].tv_usec,
now.tv_sec, now.tv_usec);
if (maybe_next < 0) {
times[4].tv_sec = -1;
got_interrupt[lockable_timeout] = 1;
if (db[28].d) {
ddprintf("\nset_next_alarm; "
"detected lockable resource timeout..\n");
}
} else if (maybe_next < next_interrupt) {
next_interrupt = maybe_next;
}
}
/* print to cloud.asp timeout */
if (times[5].tv_sec != -1) {
maybe_next = usec_diff(times[5].tv_sec, times[5].tv_usec,
now.tv_sec, now.tv_usec);
if (maybe_next < 0) {
times[5].tv_sec = -1;
got_interrupt[print_cloud] = 1;
if (db[28].d) {
ddprintf("\nset_next_alarm; "
"detected print_cloud timeout..\n");
}
} else if (maybe_next < next_interrupt) {
next_interrupt = maybe_next;
}
}
/* ping neighbor timeout */
if (times[6].tv_sec != -1) {
maybe_next = usec_diff(times[6].tv_sec, times[6].tv_usec,
now.tv_sec, now.tv_usec);
if (maybe_next < 0) {
times[6].tv_sec = -1;
got_interrupt[ping_neighbors] = 1;
if (db[28].d) {
ddprintf("\nset_next_alarm; "
"detected ping_neighbors timeout..\n");
}
} else if (maybe_next < next_interrupt) {
next_interrupt = maybe_next;
}
}
/* disable cloud.asp printing timeout */
if (times[7].tv_sec != -1) {
maybe_next = usec_diff(times[7].tv_sec, times[7].tv_usec,
now.tv_sec, now.tv_usec);
if (maybe_next < 0) {
times[7].tv_sec = -1;
got_interrupt[disable_print_cloud] = 1;
if (db[28].d) {
ddprintf("\nset_next_alarm; "
"detected disable print cloud timeout..\n");
}
} else if (maybe_next < next_interrupt) {
next_interrupt = maybe_next;
}
}
/* wifi scan timeout */
if (times[8].tv_sec != -1) {
maybe_next = usec_diff(times[8].tv_sec, times[8].tv_usec,
now.tv_sec, now.tv_usec);
if (maybe_next < 0) {
times[8].tv_sec = -1;
got_interrupt[wifi_scan] = 1;
if (db[28].d) {
ddprintf("\nset_next_alarm; "
"detected wifi_scan timeout..\n");
}
} else if (maybe_next < next_interrupt) {
next_interrupt = maybe_next;
}
}
set_alarm((int) (next_interrupt / 1000));
if (db[2].d) {
int i;
ptime("end", now);
ptime("t0", times[0]);
ptime("t1", times[1]);
ptime("t2", times[2]);
ptime("t3", times[3]);
ptime("t4", times[4]);
ptime("t5", times[5]);
ptime("t6", times[6]);
ptime("t7", times[7]);
ptime("t8", times[8]);
ddprintf("\ninterrupts pending: ");
for (i = 0; i < TIMER_COUNT; i++) {
if (got_interrupt[i]) {
ddprintf(" %d", i);
}
}
ddprintf("\nwaiting %d msec\n", (int) (next_interrupt / 1000));
}
} /* set_next_alarm */
/* figure out when in the future to send our next ping broadcast message.
* we do this based on a uniform[PING_INTERVAL_MIN .. PING_INTERVAL_MAX]
* probability distribution, where the high end of the range is shorter
* than the stp timeout window. so, we have a fighting chance to have
* other cloud boxes hear something from us before they time us out and
* assume we are dead.
*/
void set_next_ping_alarm(void)
{
int msec;
if (times[6].tv_sec != -1) {
if (db[28].d) { ddprintf("already set; returning.\n"); }
return;
}
while (!checked_gettimeofday(×[6]));
msec = discrete_unif(PING_INTERVAL_MAX - PING_INTERVAL_MIN);
msec += PING_INTERVAL_MIN;
if (db[40].d) { msec *= 20; }
if (db[7].d) {
ddprintf("next time: %d\n", msec);
}
usec_add_msecs(×[6].tv_sec, ×[6].tv_usec, msec);
if (db[7].d) {
ptime("\nping_neighbor timer now", now);
ptime("ping_neighbor interrupt", times[6]);
ddprintf("\n");
}
block_timer_interrupts(SIG_BLOCK);
set_next_alarm();
block_timer_interrupts(SIG_UNBLOCK);
}
/* figure out when in the future to send our next ping broadcast message.
* we do this based on a uniform[PING_INTERVAL_MIN .. PING_INTERVAL_MAX]
* probability distribution, where the high end of the range is shorter
* than the stp timeout window. so, we have a fighting chance to have
* other cloud boxes hear something from us before they time us out and
* assume we are dead.
*/
void set_next_scan_alarm(void)
{
int msec;
if (times[8].tv_sec != -1) {
if (db[28].d) { ddprintf("already set; returning.\n"); }
return;
}
while (!checked_gettimeofday(×[8]));
msec = discrete_unif(SCAN_INTERVAL_MAX - SCAN_INTERVAL_MIN);
msec += PING_INTERVAL_MIN;
if (db[7].d) {
ddprintf("next time: %d\n", msec);
}
usec_add_msecs(×[8].tv_sec, ×[8].tv_usec, msec);
block_timer_interrupts(SIG_BLOCK);
set_next_alarm();
block_timer_interrupts(SIG_UNBLOCK);
}
/* if the 'display the cloud' option has been set for this box,
* every CLOUD_PRINT_INTERVAL seconds (usually 5 seconds), we re-build an
* ascii version of our model of the current cloud, viewable from our web
* page.
*
* (if that option is set for at least one box in the cloud, all cloud boxes
* are instructed to include their local cloud topology connectivity with
* every stp beacon they send out, somewhat increasing protocol overhead.
* but not too badly, since these messages only go out on average every
* second or so.)
*/
void set_next_cloud_print_alarm(void)
{
struct timeval tv;
if (!db[38].d && !db[30].d) {
times[5].tv_sec = -1;
times[7].tv_sec = -1;
return;
}
if (times[5].tv_sec != -1 && times[7].tv_sec != -1) {
if (db[28].d) { ddprintf("already set; returning.\n"); }
return;
}
while (!checked_gettimeofday(&tv));
if (times[5].tv_sec == -1) {
times[5] = tv;
usec_add_msecs(×[5].tv_sec, ×[5].tv_usec,
CLOUD_PRINT_INTERVAL);
if (db[28].d) {
ptime("\nprint_cloud timer now", now);
ptime("print_cloud interrupt", times[5]);
ddprintf("\n");
}
}
if (times[7].tv_sec == -1) {
int disable_time;
times[7] = tv;
disable_time = CLOUD_PRINT_DISABLE;
if (stp_recv_beacon_count > 1) {
disable_time *= stp_recv_beacon_count;
}
usec_add_msecs(×[7].tv_sec, ×[7].tv_usec, disable_time);
if (db[28].d) {
ptime("\ndisable_print_cloud timer now", now);
ptime("disable_print_cloud interrupt", times[7]);
ddprintf("\n");
}
}
block_timer_interrupts(SIG_BLOCK);
set_next_alarm();
block_timer_interrupts(SIG_UNBLOCK);
}
/* we've been told that printing of the cloud should continue.
* kick the disable_print_cloud timer farther down the road.
*/
void restart_disable_print_cloud()
{
times[7].tv_sec = -1;
set_next_cloud_print_alarm();
}
/* make sure that the disable_cloud_print alarm is set. if it is set,
* let it run. if it is not set, set it.
*/
void ensure_disable_print_cloud()
{
if (times[7].tv_sec == -1) {
set_next_cloud_print_alarm();
}
}
/* set timer to generate alarm if we haven't heard an ack back to our
* sequence message.
*/
void update_ack_timer()
{
if (!db[22].d) { return; }
if (times[3].tv_sec != -1) { return; }
while (!checked_gettimeofday(×[3]));
usec_add_msecs(×[3].tv_sec, ×[3].tv_usec, ACK_TIMEOUT_MSEC);
block_timer_interrupts(SIG_BLOCK);
set_next_alarm();
block_timer_interrupts(SIG_UNBLOCK);
}
/* turn off ack timer */
void turn_off_ack_timer()
{
if (awaiting_ack()) { return; }
times[3].tv_sec = -1;
block_timer_interrupts(SIG_BLOCK);
set_next_alarm();
block_timer_interrupts(SIG_UNBLOCK);
}
/* we have added a granted lock, or a lock request, or an owned lock.
* all locks time out by themselves if they aren't explicitly updated
* as part of the cloud protocol. for the given lock, set its timeout
* time, and schedule a timer interrupt when that time is reached.
*/
void set_lock_timer(lockable_resource_t *l)
{
struct timeval now;
/* don't know why we commented this out; probably a mistake */
// if (!db[22].d) { return; }
if (db[28].d) { ddprintf("set_lock_timer..\n"); }
while (!checked_gettimeofday(&now));
l->sec = now.tv_sec;
l->usec = now.tv_usec;
usec_add_msecs(&l->sec, &l->usec, RECV_TIMEOUT_USEC / 1000);
reset_lock_timer();
if (db[28].d) {
ptime("\nset_lock_timer now", now);
ptime("set_lock_timer interrupt", times[4]);
ddprintf("\n");
}
} /* set_lock_timer */
/* note next time that a lockable resource will time out, and make sure
* we get an interrupt for it.
*/
void reset_lock_timer()
{
int i;
long earliest_sec = -1, earliest_usec;
for (i = 0; i < pending_request_count; i++) {
lockable_resource_t *l = &pending_requests[i];
update_time(&earliest_sec, &earliest_usec, l->sec, l->usec);
}
for (i = 0; i < locks_owned_count; i++) {
lockable_resource_t *l = &locks_owned[i];
update_time(&earliest_sec, &earliest_usec, l->sec, l->usec);
}
for (i = 0; i < locks_granted_count; i++) {
lockable_resource_t *l = &locks_granted[i];
update_time(&earliest_sec, &earliest_usec, l->sec, l->usec);
}
times[4].tv_sec = earliest_sec;
times[4].tv_usec = earliest_usec;
block_timer_interrupts(SIG_BLOCK);
set_next_alarm();
block_timer_interrupts(SIG_UNBLOCK);
} /* reset_lock_timer */