-
Notifications
You must be signed in to change notification settings - Fork 3
fix: harden SV pacing estimator and isolate unrelated traffic #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
87a5ec1
fix: require steady evidence before activating SV pacing
masarray 2bec4b7
fix: isolate SV pacing from PTP and GOOSE injection
masarray 34201a5
test: expose Npcap pacing internals to engine tests
masarray 1fce360
test: reference Npcap transport for pacing coverage
masarray 09453e8
test: cover stable SV pacing interval acquisition
masarray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/AR.Iec61850.Transports.Npcap/SvTransmitIntervalEstimator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| namespace AR.Iec61850.Transports.Npcap; | ||
|
|
||
| /// <summary> | ||
| /// Learns a stable SV transmit interval without allowing one scheduler-late observation | ||
| /// to become the permanent wire rate. The estimator activates only after several | ||
| /// mutually consistent intervals have been observed. | ||
| /// </summary> | ||
| internal sealed class SvTransmitIntervalEstimator | ||
| { | ||
| private readonly long _minimumIntervalTicks; | ||
| private readonly long _maximumIntervalTicks; | ||
| private readonly int _requiredConsistentIntervals; | ||
| private long _candidateIntervalTicks; | ||
| private int _candidateCount; | ||
|
|
||
| public SvTransmitIntervalEstimator( | ||
| long minimumIntervalTicks, | ||
| long maximumIntervalTicks, | ||
| int requiredConsistentIntervals = 4) | ||
| { | ||
| if (minimumIntervalTicks <= 0) | ||
| throw new ArgumentOutOfRangeException(nameof(minimumIntervalTicks)); | ||
| if (maximumIntervalTicks < minimumIntervalTicks) | ||
| throw new ArgumentOutOfRangeException(nameof(maximumIntervalTicks)); | ||
| if (requiredConsistentIntervals < 2) | ||
| throw new ArgumentOutOfRangeException(nameof(requiredConsistentIntervals)); | ||
|
|
||
| _minimumIntervalTicks = minimumIntervalTicks; | ||
| _maximumIntervalTicks = maximumIntervalTicks; | ||
| _requiredConsistentIntervals = requiredConsistentIntervals; | ||
| } | ||
|
|
||
| public long NominalIntervalTicks { get; private set; } | ||
| public int CandidateCount => _candidateCount; | ||
|
|
||
| public void Observe(long intervalTicks) | ||
| { | ||
| if (intervalTicks < _minimumIntervalTicks || intervalTicks > _maximumIntervalTicks) | ||
| { | ||
| if (NominalIntervalTicks == 0) | ||
| ResetCandidate(); | ||
| return; | ||
| } | ||
|
|
||
| if (NominalIntervalTicks > 0) | ||
| { | ||
| var minimumAccepted = NominalIntervalTicks * 3 / 4; | ||
| var maximumAccepted = NominalIntervalTicks * 3 / 2; | ||
| if (intervalTicks >= minimumAccepted && intervalTicks <= maximumAccepted) | ||
| { | ||
| NominalIntervalTicks = (long)Math.Round( | ||
| (NominalIntervalTicks * 0.9) + (intervalTicks * 0.1)); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (_candidateCount == 0) | ||
| { | ||
| _candidateIntervalTicks = intervalTicks; | ||
| _candidateCount = 1; | ||
| return; | ||
| } | ||
|
|
||
| var tolerance = Math.Max( | ||
| _minimumIntervalTicks / 2, | ||
| (long)Math.Round(_candidateIntervalTicks * 0.15)); | ||
| if (Math.Abs(intervalTicks - _candidateIntervalTicks) > tolerance) | ||
| { | ||
| _candidateIntervalTicks = intervalTicks; | ||
| _candidateCount = 1; | ||
| return; | ||
| } | ||
|
|
||
| _candidateIntervalTicks = (long)Math.Round( | ||
| ((_candidateIntervalTicks * _candidateCount) + intervalTicks) / | ||
| (double)(_candidateCount + 1)); | ||
| _candidateCount++; | ||
|
|
||
| if (_candidateCount >= _requiredConsistentIntervals) | ||
| NominalIntervalTicks = _candidateIntervalTicks; | ||
| } | ||
|
|
||
| private void ResetCandidate() | ||
| { | ||
| _candidateIntervalTicks = 0; | ||
| _candidateCount = 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/AR.Iec61850.Tests/Transports/SvTransmitIntervalEstimatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using AR.Iec61850.Transports.Npcap; | ||
|
|
||
| namespace AR.Iec61850.Tests.Transports; | ||
|
|
||
| public sealed class SvTransmitIntervalEstimatorTests | ||
| { | ||
| [Fact] | ||
| public void SingleLateObservation_DoesNotBecomePermanentNominalRate() | ||
| { | ||
| var estimator = new SvTransmitIntervalEstimator(20, 5_000, requiredConsistentIntervals: 4); | ||
|
|
||
| estimator.Observe(1_000); // Scheduler-late first interval. | ||
| Assert.Equal(0, estimator.NominalIntervalTicks); | ||
|
|
||
| estimator.Observe(250); | ||
| estimator.Observe(248); | ||
| estimator.Observe(252); | ||
| Assert.Equal(0, estimator.NominalIntervalTicks); | ||
|
|
||
| estimator.Observe(251); | ||
|
|
||
| Assert.InRange(estimator.NominalIntervalTicks, 248, 252); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void InconsistentIntervals_DoNotActivatePacing() | ||
| { | ||
| var estimator = new SvTransmitIntervalEstimator(20, 5_000, requiredConsistentIntervals: 4); | ||
|
|
||
| foreach (var interval in new long[] { 250, 800, 240, 1_200, 260, 700 }) | ||
| estimator.Observe(interval); | ||
|
|
||
| Assert.Equal(0, estimator.NominalIntervalTicks); | ||
| Assert.Equal(1, estimator.CandidateCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ActiveNominalRate_IgnoresLongSchedulerStall() | ||
| { | ||
| var estimator = new SvTransmitIntervalEstimator(20, 5_000, requiredConsistentIntervals: 4); | ||
| foreach (var interval in new long[] { 250, 249, 251, 250 }) | ||
| estimator.Observe(interval); | ||
|
|
||
| var nominalBeforeStall = estimator.NominalIntervalTicks; | ||
| estimator.Observe(4_000); | ||
|
|
||
| Assert.Equal(nominalBeforeStall, estimator.NominalIntervalTicks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ActiveNominalRate_TracksOnlyNearbySteadyIntervals() | ||
| { | ||
| var estimator = new SvTransmitIntervalEstimator(20, 5_000, requiredConsistentIntervals: 4); | ||
| foreach (var interval in new long[] { 250, 250, 250, 250 }) | ||
| estimator.Observe(interval); | ||
|
|
||
| estimator.Observe(260); | ||
|
|
||
| Assert.InRange(estimator.NominalIntervalTicks, 250, 252); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When pacing is active and one send is moderately late—for example, 300 ticks against a 250-tick nominal—this branch accepts the observation and raises the nominal to 255. Because
PaceSampledValuesAsyncsubsequently enforces at least that new nominal between commits, normal 250-tick caller traffic cannot produce a shorter observation to restore the rate; recurring scheduler or injection delays below the 1.5× cutoff can therefore ratchet an SV stream progressively slower. Require consistent independent evidence before updating an active nominal rather than feeding individual paced intervals back into it.AGENTS.md reference: AGENTS.md:L173-L178
Useful? React with 👍 / 👎.