1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

do not use global variables for hook jitter_calc (closes #114)

This commit is contained in:
Steffen Vogel 2017-08-03 00:20:14 +02:00
parent 434fe6dbd6
commit dcbaf5d037

View file

@ -31,15 +31,52 @@
#include "timing.h"
#include "sample.h"
#define CALC_GPS_NTP_DELAY 0 /* @todo move to global config file */
#define GPS_NTP_DELAY_WIN_SIZE 16
static int64_t jitter_val[GPS_NTP_DELAY_WIN_SIZE] = {0};
static int64_t delay_series[GPS_NTP_DELAY_WIN_SIZE] = {0};
static int64_t moving_avg[GPS_NTP_DELAY_WIN_SIZE] = {0};
static int64_t moving_var[GPS_NTP_DELAY_WIN_SIZE] = {0};
static int64_t delay_mov_sum = 0, delay_mov_sum_sqrd = 0;
static int curr_count = 0;
struct jitter_calc {
int64_t *jitter_val;
int64_t *delay_series;
int64_t *moving_avg;
int64_t *moving_var;
int64_t delay_mov_sum;
int64_t delay_mov_sum_sqrd;
int curr_count;
};
int jitter_calc_init(struct hook *h)
{
struct jitter_calc *j = h->_vd;
size_t sz = GPS_NTP_DELAY_WIN_SIZE * sizeof(int64_t);
j->jitter_val = alloc(sz);
j->delay_series = alloc(sz);
j->moving_avg = alloc(sz);
j->moving_var = alloc(sz);
memset(j->jitter_val, 0, sz);
memset(j->delay_series, 0, sz);
memset(j->moving_avg, 0, sz);
memset(j->moving_var, 0, sz);
j->delay_mov_sum = 0;
j->delay_mov_sum_sqrd = 0;
j->curr_count = 0;
return 0;
}
int jitter_calc_deinit(struct hook *h)
{
struct jitter_calc *j = h->_vd;
free(j->jitter_val);
free(j->delay_series);
free(j->moving_avg);
free(j->moving_var);
return 0;
}
/**
* Hook to calculate jitter between GTNET-SKT GPS timestamp and Villas node NTP timestamp.
@ -49,9 +86,10 @@ static int curr_count = 0;
* is high (i.e. several mins depending on GPS_NTP_DELAY_WIN_SIZE),
* the variance value will overrun the 64bit value.
*/
int hook_jitter_ts(struct hook *h, struct sample *smps[], size_t *cnt)
int jitter_calc_read(struct hook *h, struct sample *smps[], size_t *cnt)
{
/* @todo save data for each node, not just displaying on the screen, doesn't work for more than one node!!! */
struct jitter_calc *j = h->_vd;
struct timespec now = time_now();
int64_t delay_sec, delay_nsec, curr_delay_us;
@ -60,44 +98,45 @@ int hook_jitter_ts(struct hook *h, struct sample *smps[], size_t *cnt)
delay_nsec = now.tv_nsec - smps[i]->ts.origin.tv_nsec;
/* Calc on microsec instead of nenosec delay as variance formula overflows otherwise.*/
curr_delay_us = delay_sec*1000000 + delay_nsec/1000;
curr_delay_us = delay_sec * 1000000 + delay_nsec / 1000;
delay_mov_sum = delay_mov_sum + curr_delay_us - delay_series[curr_count];
moving_avg[curr_count] = delay_mov_sum/(GPS_NTP_DELAY_WIN_SIZE); /* Will be valid after GPS_NTP_DELAY_WIN_SIZE initial values */
j->delay_mov_sum = j->delay_mov_sum + curr_delay_us - j->delay_series[j->curr_count];
j->moving_avg[j->curr_count] = j->delay_mov_sum / GPS_NTP_DELAY_WIN_SIZE; /* Will be valid after GPS_NTP_DELAY_WIN_SIZE initial values */
delay_mov_sum_sqrd = delay_mov_sum_sqrd + (curr_delay_us*curr_delay_us) - (delay_series[curr_count]*delay_series[curr_count]);
moving_var[curr_count] = (delay_mov_sum_sqrd - (delay_mov_sum*delay_mov_sum)/GPS_NTP_DELAY_WIN_SIZE)/(GPS_NTP_DELAY_WIN_SIZE-1);
j->delay_mov_sum_sqrd = j->delay_mov_sum_sqrd + (curr_delay_us * curr_delay_us) - (j->delay_series[j->curr_count] * j->delay_series[j->curr_count]);
j->moving_var[j->curr_count] = (j->delay_mov_sum_sqrd - (j->delay_mov_sum * j->delay_mov_sum) / GPS_NTP_DELAY_WIN_SIZE) / (GPS_NTP_DELAY_WIN_SIZE - 1);
delay_series[curr_count] = curr_delay_us; /* Update the last delay value */
j->delay_series[j->curr_count] = curr_delay_us; /* Update the last delay value */
/* Jitter calc formula as used in Wireshark according to RFC3550 (RTP)
D(i,j) = (Rj-Ri)-(Sj-Si) = (Rj-Sj)-(Ri-Si)
J(i) = J(i-1)+(|D(i-1,i)|-J(i-1))/16
*/
jitter_val[(curr_count+1)%GPS_NTP_DELAY_WIN_SIZE] = jitter_val[curr_count] + (labs(curr_delay_us) - jitter_val[curr_count])/16;
j->jitter_val[(j->curr_count + 1) % GPS_NTP_DELAY_WIN_SIZE] = j->jitter_val[j->curr_count] + (labs(curr_delay_us) - j->jitter_val[j->curr_count]) / 16;
stats("%s: jitter=%" PRId64 " usec, moving average=%" PRId64 " usec, moving variance=%" PRId64 " usec", __FUNCTION__, jitter_val[(curr_count+1)%GPS_NTP_DELAY_WIN_SIZE], moving_avg[curr_count], moving_var[curr_count]);
stats("%s: jitter=%" PRId64 " usec, moving average=%" PRId64 " usec, moving variance=%" PRId64 " usec", __FUNCTION__, j->jitter_val[(j->curr_count + 1) % GPS_NTP_DELAY_WIN_SIZE], j->moving_avg[j->curr_count], j->moving_var[j->curr_count]);
curr_count++;
if(curr_count >= GPS_NTP_DELAY_WIN_SIZE)
curr_count = 0;
j->curr_count++;
if(j->curr_count >= GPS_NTP_DELAY_WIN_SIZE)
j->curr_count = 0;
}
return 0;
}
#if CALC_GPS_NTP_DELAY == 1
static struct plugin p = {
.name = "jitter_calc",
.description = "Calc jitter, mean and variance of GPS vs NTP TS",
.type = PLUGIN_TYPE_HOOK,
.hook = {
.priority = 0,
.builtin = true,
.read = hook_jitter_ts,
.init = jitter_calc_init,
.destroy= jitter_calc_deinit,
.read = jitter_calc_read,
.size = sizeof(struct jitter_calc)
}
};
REGISTER_PLUGIN(&p)
#endif
/** @} */