Avoid setting alarms with deadlines that have already elapsed

This commit is contained in:
Jeremy Lakeman 2013-06-19 15:27:17 +09:30
parent 297a30e3c9
commit 61a958fd34
5 changed files with 19 additions and 11 deletions

View File

@ -104,16 +104,14 @@ int overlayServerMode()
of wifi latency anyway, so we'll live with it. Larger values will affect voice transport, of wifi latency anyway, so we'll live with it. Larger values will affect voice transport,
and smaller values would affect CPU and energy use, and make the simulation less realistic. */ and smaller values would affect CPU and energy use, and make the simulation less realistic. */
time_ms_t now = gettime_ms();
#define SCHEDULE(X, Y, D) { \ #define SCHEDULE(X, Y, D) { \
static struct profile_total _stats_##X={.name="" #X "",}; \ static struct profile_total _stats_##X={.name="" #X "",}; \
static struct sched_ent _sched_##X={\ static struct sched_ent _sched_##X={\
.stats = &_stats_##X, \ .stats = &_stats_##X, \
.function=X,\ .function=X,\
}; \ }; \
_sched_##X.alarm=(now+Y);\ _sched_##X.alarm=(gettime_ms()+Y);\
_sched_##X.deadline=(now+Y+D);\ _sched_##X.deadline=(gettime_ms()+Y+D);\
schedule(&_sched_##X); } schedule(&_sched_##X); }
/* Periodically check for server shut down */ /* Periodically check for server shut down */

View File

@ -236,6 +236,12 @@ int overlay_address_append(struct decode_context *context, struct overlay_buffer
static int add_explain_response(struct subscriber *subscriber, void *context){ static int add_explain_response(struct subscriber *subscriber, void *context){
struct decode_context *response = context; struct decode_context *response = context;
// only explain a SID once every half second.
time_ms_t now = gettime_ms();
if (now - subscriber->last_explained < 500)
return 0;
subscriber->last_explained = now;
if (!response->please_explain){ if (!response->please_explain){
response->please_explain = calloc(sizeof(struct overlay_frame),1); response->please_explain = calloc(sizeof(struct overlay_frame),1);
response->please_explain->payload=ob_new(); response->please_explain->payload=ob_new();

View File

@ -85,6 +85,7 @@ struct subscriber{
time_ms_t last_probe; time_ms_t last_probe;
time_ms_t last_probe_response; time_ms_t last_probe_response;
time_ms_t last_tx; time_ms_t last_tx;
time_ms_t last_explained;
// public signing key details for remote peers // public signing key details for remote peers
unsigned char sas_public[SAS_SIZE]; unsigned char sas_public[SAS_SIZE];

View File

@ -611,7 +611,6 @@ static void interface_read_file(struct overlay_interface *interface)
IN(); IN();
/* Grab packets, unpackage and dispatch frames to consumers */ /* Grab packets, unpackage and dispatch frames to consumers */
struct file_packet packet; struct file_packet packet;
time_ms_t now = gettime_ms();
/* Read from interface file */ /* Read from interface file */
long long length=lseek(interface->alarm.poll.fd,0,SEEK_END); long long length=lseek(interface->alarm.poll.fd,0,SEEK_END);
@ -661,6 +660,7 @@ static void interface_read_file(struct overlay_interface *interface)
/* if there's no input, while we want to check for more soon, /* if there's no input, while we want to check for more soon,
we need to allow all other low priority alarms to fire first, we need to allow all other low priority alarms to fire first,
otherwise we'll dominate the scheduler without accomplishing anything */ otherwise we'll dominate the scheduler without accomplishing anything */
time_ms_t now = gettime_ms();
if (interface->recv_offset>=length){ if (interface->recv_offset>=length){
if (interface->alarm.alarm == -1 || now + 5 < interface->alarm.alarm){ if (interface->alarm.alarm == -1 || now + 5 < interface->alarm.alarm){
interface->alarm.alarm = now + 5; interface->alarm.alarm = now + 5;
@ -748,26 +748,29 @@ static void overlay_interface_poll(struct sched_ent *alarm)
struct overlay_interface *interface = (overlay_interface *)alarm; struct overlay_interface *interface = (overlay_interface *)alarm;
if (alarm->poll.revents==0){ if (alarm->poll.revents==0){
time_ms_t now = gettime_ms(); alarm->alarm=-1;
time_ms_t now = gettime_ms();
if (interface->state==INTERFACE_STATE_UP && interface->tick_ms>0){ if (interface->state==INTERFACE_STATE_UP && interface->tick_ms>0){
if (now >= interface->last_tx+interface->tick_ms) if (now >= interface->last_tx+interface->tick_ms)
overlay_send_tick_packet(interface); overlay_send_tick_packet(interface);
alarm->alarm=interface->last_tx+interface->tick_ms; alarm->alarm=interface->last_tx+interface->tick_ms;
}else{ alarm->deadline=alarm->alarm+interface->tick_ms/2;
alarm->alarm=-1;
} }
alarm->deadline=alarm->alarm+interface->tick_ms/2;
switch(interface->socket_type){ switch(interface->socket_type){
case SOCK_DGRAM: case SOCK_DGRAM:
case SOCK_STREAM: case SOCK_STREAM:
break; break;
case SOCK_FILE: case SOCK_FILE:
interface_read_file(interface); interface_read_file(interface);
now = gettime_ms();
break; break;
} }
if (alarm->alarm!=-1) { if (alarm->alarm!=-1) {
if (alarm->alarm < now)
alarm->alarm = now;
schedule(alarm); schedule(alarm);
} }
} }

View File

@ -343,7 +343,7 @@ struct overlay_buffer;
struct overlay_frame; struct overlay_frame;
struct broadcast; struct broadcast;
#define STRUCT_SCHED_ENT_UNUSED ((struct sched_ent){NULL, NULL, NULL, NULL, {-1, 0, 0}, 0LL, 0LL, NULL, -1}) #define STRUCT_SCHED_ENT_UNUSED {.poll.fd=-1, ._poll_index=-1,}
extern int overlayMode; extern int overlayMode;