-
Notifications
You must be signed in to change notification settings - Fork 49
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
Add egress loss stat #195
Merged
Merged
Add egress loss stat #195
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -1097,6 +1097,53 @@ impl TwccSendRegister { | |
Some(&self.queue[index]) | ||
} | ||
|
||
/// Calculate the egress loss for given time window. | ||
/// | ||
/// **Note:** The register only keeps a limited number of records and using `duration` values | ||
/// larger than ~1-2 seconds is liable to be inaccurate since some packets sent might have already | ||
/// been evicted from the register. | ||
/// | ||
/// Packets sent very recently, within one RTT, are not considered because it's not likely that | ||
/// a TWCC report could've been generated by the sender and received yet. | ||
pub fn loss(&self, duration: Duration, now: Instant) -> Option<f32> { | ||
let recent_rtt = self | ||
.queue | ||
.iter() | ||
.rev() | ||
.find_map(|r| r.rtt()) | ||
.map(|rtt| rtt.clamp(Duration::from_millis(5), Duration::from_millis(150))) | ||
.unwrap_or(Duration::from_millis(50)); | ||
|
||
// Consider only packets that were sent at least one RTT ago. For more recent sends we will | ||
// likely not have received the TWCC report in any case. | ||
let upper_bound = now - recent_rtt; | ||
// Consider only packets in the span specified by the caller | ||
let lower_bound = now - duration; | ||
|
||
let packets = self | ||
.queue | ||
.iter() | ||
.rev() | ||
.skip_while(|s| s.local_send_time >= upper_bound) | ||
.take_while(|s| s.local_send_time >= lower_bound); | ||
|
||
let (total, lost) = packets.fold((0, 0), |(total, lost), s| { | ||
let was_lost = s | ||
.recv_report | ||
.as_ref() | ||
.map(|rr| rr.remote_recv_time.is_none()) | ||
.unwrap_or(true); | ||
|
||
(total + 1, lost + u64::from(was_lost)) | ||
}); | ||
|
||
if total == 0 { | ||
return None; | ||
} | ||
|
||
Some((lost as f32) / (total as f32)) | ||
} | ||
|
||
/// Get all send records in a range. | ||
pub fn send_records( | ||
&self, | ||
|
@@ -1811,4 +1858,56 @@ mod test { | |
vec![0, 1, 2, 3, 4, 5, 6, 7] | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_twcc_register_loss() { | ||
let mut reg = TwccSendRegister::new(25); | ||
let mut now = Instant::now(); | ||
for i in 0..9 { | ||
reg.register_seq(i.into(), now, 0); | ||
now = now + Duration::from_millis(15); | ||
} | ||
|
||
// This packet was sent too recently to be included when calculating loss | ||
now = now + Duration::from_millis(35); | ||
reg.register_seq(10.into(), now, 0); | ||
|
||
now = now + Duration::from_millis(5); | ||
reg.apply_report( | ||
Twcc { | ||
sender_ssrc: Ssrc::new(), | ||
ssrc: Ssrc::new(), | ||
base_seq: 0, | ||
status_count: 9, | ||
reference_time: 35, | ||
feedback_count: 0, | ||
chunks: [ | ||
PacketChunk::VectorDouble(0b11_01_01_01_00_01_00_01, 7), | ||
PacketChunk::Run(PacketStatus::ReceivedSmallDelta, 2), | ||
] | ||
.into(), | ||
delta: [ | ||
Delta::Small(10), | ||
Delta::Small(10), | ||
Delta::Small(10), | ||
Delta::Small(10), | ||
Delta::Small(10), | ||
Delta::Small(10), | ||
Delta::Small(10), | ||
] | ||
.into(), | ||
}, | ||
now, | ||
) | ||
.expect("apply_report to return Some(_)"); | ||
|
||
now = now + Duration::from_millis(20); | ||
let loss = reg | ||
.loss(Duration::from_millis(150), now) | ||
.expect("Should be able to calcualte loss"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. calculate |
||
|
||
let pct = (loss * 100.0).floor() as u32; | ||
|
||
assert_eq!(pct, 33, "The loss percentage should be 33"); | ||
} | ||
} |
This file contains 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 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
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.
This looks a bit expensive. Is it?
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.
Shouldn't be, it's
O(2n)
at worst withn == 1000
currently and runs every second. I don't really think the2n
case will happen either