Skip to content

Commit 1098637

Browse files
davidv1992rnijveld
authored andcommitted
Implemented missing fields in the current dataset.
1 parent 8dc64d8 commit 1098637

File tree

12 files changed

+121
-19
lines changed

12 files changed

+121
-19
lines changed

statime-linux/src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ async fn actual_main() {
294294
let (instance_state_sender, instance_state_receiver) =
295295
tokio::sync::watch::channel(ObservableInstanceState {
296296
default_ds: instance.default_ds(),
297-
current_ds: instance.current_ds(),
297+
current_ds: instance.current_ds(None),
298298
parent_ds: instance.parent_ds(),
299299
time_properties_ds: instance.time_properties_ds(),
300300
path_trace_ds: instance.path_trace_ds(),
@@ -502,7 +502,12 @@ async fn run(
502502
// We don't care if isn't anybody on the other side
503503
let _ = instance_state_sender.send(ObservableInstanceState {
504504
default_ds: instance.default_ds(),
505-
current_ds: instance.current_ds(),
505+
current_ds: instance.current_ds(
506+
mut_bmca_ports
507+
.iter()
508+
.filter_map(|v| v.port_current_ds_contribution())
509+
.next(),
510+
),
506511
parent_ds: instance.parent_ds(),
507512
time_properties_ds: instance.time_properties_ds(),
508513
path_trace_ds: instance.path_trace_ds(),

statime-linux/src/metrics/format.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,19 @@ pub fn format_current_ds(
136136
Some(Unit::Nanoseconds),
137137
vec![Measurement {
138138
labels: labels.clone(),
139-
value: current_ds.offset_from_master,
139+
value: current_ds.offset_from_master.seconds(),
140+
}],
141+
)?;
142+
143+
format_metric(
144+
w,
145+
"mean_delay",
146+
"Packet delay between a Master PTP Instance as calculated by the Slave instance",
147+
MetricType::Gauge,
148+
Some(Unit::Nanoseconds),
149+
vec![Measurement {
150+
labels: labels.clone(),
151+
value: current_ds.mean_delay.seconds(),
140152
}],
141153
)?;
142154

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::time::Duration;
2-
31
#[derive(Default, Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
42
pub(crate) struct InternalCurrentDS {
53
pub(crate) steps_removed: u16,
6-
pub(crate) offset_from_master: Duration,
74
}

statime/src/filters/basic.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub struct BasicFilter {
3232
gain: f64,
3333

3434
cur_freq: f64,
35+
36+
last_offset: Duration,
37+
last_delay: Duration,
3538
}
3639

3740
impl Filter for BasicFilter {
@@ -44,17 +47,21 @@ impl Filter for BasicFilter {
4447
freq_confidence: 1e-4,
4548
gain,
4649
cur_freq: 0.0,
50+
last_offset: Duration::ZERO,
51+
last_delay: Duration::ZERO,
4752
}
4853
}
4954

5055
fn measurement<C: Clock>(&mut self, measurement: Measurement, clock: &mut C) -> FilterUpdate {
5156
let mut update = FilterUpdate::default();
5257

5358
if let Some(delay) = measurement.delay {
59+
self.last_delay = delay;
5460
update.mean_delay = Some(delay);
5561
}
5662

5763
if let Some(peer_delay) = measurement.peer_delay {
64+
self.last_delay = peer_delay;
5865
update.mean_delay = Some(peer_delay);
5966
}
6067

@@ -63,6 +70,8 @@ impl Filter for BasicFilter {
6370
return update;
6471
};
6572

73+
self.last_offset = offset;
74+
6675
// Reset on too-large difference
6776
if offset.abs() > Duration::from_nanos(1_000_000_000) {
6877
log::debug!("Offset too large, stepping {}", offset);
@@ -154,4 +163,11 @@ impl Filter for BasicFilter {
154163
// ignore
155164
Default::default()
156165
}
166+
167+
fn current_estimates(&self) -> super::FilterEstimate {
168+
super::FilterEstimate {
169+
offset_from_master: self.last_offset,
170+
mean_delay: self.last_delay,
171+
}
172+
}
157173
}

statime/src/filters/kalman.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use super::matrix::{Matrix, Vector};
1+
use super::{
2+
matrix::{Matrix, Vector},
3+
FilterEstimate,
4+
};
25
#[allow(unused_imports)]
36
use crate::float_polyfill::FloatPolyfill;
47
use crate::{
@@ -622,6 +625,13 @@ impl Filter for KalmanFilter {
622625
// of correct
623626
self.change_frequency(0.0, clock);
624627
}
628+
629+
fn current_estimates(&self) -> super::FilterEstimate {
630+
FilterEstimate {
631+
offset_from_master: Duration::from_seconds(self.running_filter.offset()),
632+
mean_delay: Duration::from_seconds(self.running_filter.mean_delay()),
633+
}
634+
}
625635
}
626636

627637
impl KalmanFilter {

statime/src/filters/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ pub struct FilterUpdate {
2323
pub mean_delay: Option<Duration>,
2424
}
2525

26+
/// Current estimate of various dataset members as generated by the filter.
27+
pub struct FilterEstimate {
28+
/// Offset from the remote master, see also (IEEE 1588-2019 section 8.2.2.3)
29+
pub offset_from_master: Duration,
30+
/// Estimate of packet propagation delay, see also (IEEE 1588-2019 section
31+
/// 8.2.2.4)
32+
pub mean_delay: Duration,
33+
}
34+
2635
/// A filter for post-processing time measurements.
2736
///
2837
/// Filters are responsible for dealing with the network noise, and should
@@ -51,4 +60,8 @@ pub trait Filter {
5160
/// Handle ending of time synchronization from the source
5261
/// associated with this filter.
5362
fn demobilize<C: Clock>(self, clock: &mut C);
63+
64+
/// Provide estimates for the Current dataset
65+
/// mean delay and
66+
fn current_estimates(&self) -> FilterEstimate;
5467
}
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::datastructures::datasets::InternalCurrentDS;
1+
use crate::{datastructures::datasets::InternalCurrentDS, filters::FilterEstimate, time::Duration};
22

33
/// A concrete implementation of the PTP Current dataset (IEEE1588-2019 section
44
/// 8.2.2)
@@ -11,14 +11,27 @@ pub struct CurrentDS {
1111
/// See *IEEE1588-2019 section 8.2.2.2*.
1212
pub steps_removed: u16,
1313
/// See *IEEE1588-2019 section 8.2.2.3*.
14-
pub offset_from_master: i128,
14+
pub offset_from_master: Duration,
15+
/// See *IEEE1588-2019 section 8.2.2.3*.
16+
pub mean_delay: Duration,
1517
}
1618

17-
impl From<&InternalCurrentDS> for CurrentDS {
18-
fn from(v: &InternalCurrentDS) -> Self {
19-
Self {
20-
steps_removed: v.steps_removed,
21-
offset_from_master: v.offset_from_master.nanos_rounded(),
19+
impl CurrentDS {
20+
pub(crate) fn from_state(
21+
current_ds: &InternalCurrentDS,
22+
port_contribution: Option<FilterEstimate>,
23+
) -> Self {
24+
match port_contribution {
25+
Some(port_contribution) => Self {
26+
steps_removed: current_ds.steps_removed,
27+
offset_from_master: port_contribution.offset_from_master,
28+
mean_delay: port_contribution.mean_delay,
29+
},
30+
None => Self {
31+
steps_removed: current_ds.steps_removed,
32+
offset_from_master: Duration::ZERO,
33+
mean_delay: Duration::ZERO,
34+
},
2235
}
2336
}
2437
}

statime/src/port/bmca.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ impl<A, C: Clock, F: Filter, R: Rng, S: PtpInstanceStateMutex> Port<'_, InBmca,
162162
debug_assert!(!default_ds.slave_only);
163163

164164
current_ds.steps_removed = 0;
165-
current_ds.offset_from_master = Duration::ZERO;
166165

167166
parent_ds.parent_port_identity.clock_identity = defaultds.clock_identity;
168167
parent_ds.parent_port_identity.port_number = 0;

statime/src/port/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
common::PortIdentity,
3030
messages::{Message, MessageBody},
3131
},
32-
filters::Filter,
32+
filters::{Filter, FilterEstimate},
3333
observability::{self, port::PortDS},
3434
ptp_instance::{PtpInstanceState, PtpInstanceStateMutex},
3535
time::{Duration, Time},
@@ -639,6 +639,16 @@ impl<L, A, R, C, F: Filter, S> Port<'_, L, A, R, C, F, S> {
639639
master_only: self.config.master_only,
640640
}
641641
}
642+
643+
/// If this port is in the slave state, this returns the current estimate
644+
/// of the current_ds offset_to_master and mean_delay fields.
645+
pub fn port_current_ds_contribution(&self) -> Option<FilterEstimate> {
646+
if matches!(self.port_state, PortState::Slave(_)) {
647+
Some(self.filter.current_estimates())
648+
} else {
649+
None
650+
}
651+
}
642652
}
643653

644654
impl<'a, A, C, F: Filter, R: Rng, S: PtpInstanceStateMutex> Port<'a, InBmca, A, R, C, F, S> {

statime/src/port/slave.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,10 @@ mod tests {
617617
fn update<C: Clock>(&mut self, _clock: &mut C) -> FilterUpdate {
618618
Default::default()
619619
}
620+
621+
fn current_estimates(&self) -> crate::filters::FilterEstimate {
622+
unimplemented!()
623+
}
620624
}
621625

622626
#[test]

0 commit comments

Comments
 (0)