Browse Source

Make sample robust for clock changes.

controller_bus^2
root 8 years ago
parent
commit
6606002f4a
2 changed files with 63 additions and 6 deletions
  1. +56
    -5
      controller/controller/controller_sample.c
  2. +7
    -1
      controller/dt_ctrl.c

+ 56
- 5
controller/controller/controller_sample.c View File

@ -29,6 +29,7 @@
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <signal.h>
#include "controller_block.h"
#include "controller_sample.h"
@ -125,6 +126,34 @@ static ssize_t sample_timing_snprintf(struct sample_timing *st,
}
static int sample_timing_sane(struct timespec *t_exp, struct timespec *t_act)
{
long s_diff;
long n_diff;
s_diff = t_act->tv_sec - t_exp->tv_sec;
if (s_diff < 0) {
return -1;
}
if (s_diff > 1) {
return 1;
}
n_diff = s_diff * 1000 * 1000 * 1000;
n_diff += t_act->tv_nsec - t_exp->tv_nsec;
if (n_diff > nsec_interval * 10) {
return 1;
}
return 0;
}
static void alarm_handler(int d)
{
log_send(LOG_T_ERROR, "Sample has been inactive to long!");
}
/*
This thread should be realtime....
*/
@ -142,6 +171,16 @@ static void *sample_thread(void *arg)
pthread_mutex_t lock;
pthread_cond_t cv;
#endif
sigset_t sigset;
struct sigaction sigact;
sigemptyset (&sigset);
sigaddset(&sigset, SIGALRM);
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
sigact.sa_handler = alarm_handler;
sigemptyset (&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGALRM, &sigact, NULL);
log_send(LOG_T_DEBUG, "Starting sample thread");
controller_sample_thread_running = true;
@ -206,14 +245,26 @@ static void *sample_thread(void *arg)
clock_gettime(clock, &t_end);
sample_timing_add(&st_start, &t, &t_start);
sample_timing_add(&st_io, &t_start, &t_io);
sample_timing_add(&st_end, &t, &t_end);
sample_timing_add(&st_sample, &t_start, &t_end);
if (sample_timing_diff_nsec(&t, &t_end) > nsec_interval) {
controller_sampleoverruns++;
}
if (sample_timing_sane(&t, &t_start)) {
log_send(LOG_T_ERROR,
"Time sanity check triggered: %lld.%09ld %lld.%09ld",
(long long)t.tv_sec, t.tv_nsec,
(long long)t_start.tv_sec, t_start.tv_nsec);
t.tv_sec = t_start.tv_sec;
t.tv_nsec = (t_start.tv_nsec / nsec_interval) * nsec_interval;
log_send(LOG_T_WARNING,
"Using current time for next sampe: %lld.%09ld",
(long long)t.tv_sec, t.tv_nsec);
} else {
sample_timing_add(&st_start, &t, &t_start);
sample_timing_add(&st_io, &t_start, &t_io);
sample_timing_add(&st_end, &t, &t_end);
sample_timing_add(&st_sample, &t_start, &t_end);
}
alarm(1);
}
return NULL;
}


+ 7
- 1
controller/dt_ctrl.c View File

@ -33,6 +33,7 @@
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <signal.h>
#include "dynarg.h"
@ -53,7 +54,12 @@ int main(int argc, char **argv)
{
char *ctrl_filename;
char *dot_filename;
sigset_t sigset;
sigemptyset (&sigset);
sigaddset(&sigset, SIGALRM);
sigprocmask(SIG_BLOCK, &sigset, NULL);
log_server_start(CTRL_LOG_PORT, LOG_T_DEBUG, LOG_T_INFO);
controller_block_param_init();


Loading…
Cancel
Save