2019-03-26 15:33:47 +01:00
|
|
|
/* Calc jitter, mean and variance of GPS vs NTP TS.
|
|
|
|
*
|
|
|
|
* Author: Umar Farooq <umar.farooq@rwth-aachen.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2019-03-26 15:33:47 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstring>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <vector>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/timing.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
#define GPS_NTP_DELAY_WIN_SIZE 16
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
class JitterCalcHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
std::vector<int64_t> jitter_val;
|
|
|
|
std::vector<int64_t> delay_series;
|
|
|
|
std::vector<int64_t> moving_avg;
|
|
|
|
std::vector<int64_t> moving_var;
|
2020-09-11 14:57:05 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int64_t delay_mov_sum;
|
|
|
|
int64_t delay_mov_sum_sqrd;
|
|
|
|
int curr_count;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
JitterCalcHook(Path *p, Node *n, int fl, int prio, bool en = true)
|
|
|
|
: Hook(p, n, fl, prio, en), jitter_val(GPS_NTP_DELAY_WIN_SIZE),
|
|
|
|
delay_series(GPS_NTP_DELAY_WIN_SIZE),
|
|
|
|
moving_avg(GPS_NTP_DELAY_WIN_SIZE), moving_var(GPS_NTP_DELAY_WIN_SIZE),
|
|
|
|
delay_mov_sum(0), delay_mov_sum_sqrd(0), curr_count(0) {}
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Hook to calculate jitter between GTNET-SKT GPS timestamp and Villas node NTP timestamp.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* Drawbacks: No protection for out of order packets. Default positive delay assumed,
|
|
|
|
* so GPS timestamp should be earlier than NTP timestamp. If difference b/w NTP and GPS ts
|
|
|
|
* is high (i.e. several mins depending on GPS_NTP_DELAY_WIN_SIZE),
|
|
|
|
* the variance value will overrun the 64bit value.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual Hook::Reason process(struct Sample *smp) {
|
|
|
|
assert(state == State::STARTED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
timespec now = time_now();
|
|
|
|
int64_t delay_sec, delay_nsec, curr_delay_us;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
delay_sec = now.tv_sec - smp->ts.origin.tv_sec;
|
|
|
|
delay_nsec = now.tv_nsec - smp->ts.origin.tv_nsec;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Calc on microsec instead of nenosec delay as variance formula overflows otherwise.*/
|
|
|
|
curr_delay_us = delay_sec * 1000000 + delay_nsec / 1000;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
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
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
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);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
delay_series[curr_count] = curr_delay_us; // Update the last delay value
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Jitter calc formula as used in Wireshark according to RFC3550 (RTP)
|
2019-03-26 15:33:47 +01:00
|
|
|
D(i,j) = (Rj-Ri)-(Sj-Si) = (Rj-Sj)-(Ri-Si)
|
|
|
|
J(i) = J(i-1)+(|D(i-1,i)|-J(i-1))/16
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
jitter_val[(curr_count + 1) % GPS_NTP_DELAY_WIN_SIZE] =
|
|
|
|
jitter_val[curr_count] +
|
|
|
|
(labs(curr_delay_us) - jitter_val[curr_count]) / 16;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->info(
|
|
|
|
"{}: jitter={} usec, moving average={} usec, moving variance={} usec",
|
|
|
|
__FUNCTION__, jitter_val[(curr_count + 1) % GPS_NTP_DELAY_WIN_SIZE],
|
|
|
|
moving_avg[curr_count], moving_var[curr_count]);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
curr_count++;
|
|
|
|
if (curr_count >= GPS_NTP_DELAY_WIN_SIZE)
|
|
|
|
curr_count = 0;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return Reason::OK;
|
|
|
|
}
|
2019-03-26 15:33:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Register hook
|
2020-06-14 15:00:02 +02:00
|
|
|
static char n[] = "jitter_calc";
|
|
|
|
static char d[] = "Calc jitter, mean and variance of GPS vs NTP TS";
|
2023-09-07 11:46:39 +02:00
|
|
|
static HookPlugin<JitterCalcHook, n, d,
|
|
|
|
(int)Hook::Flags::NODE_READ | (int)Hook::Flags::PATH, 0>
|
|
|
|
p;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|