Use weighted loss to detect loss based congesiton signal. (#3169)

* Use weighted loss to detect loss based congesiton signal.

- Increase JQR min loss to 0.25.
- Use weighted loss ratio so that more packet rate gets higher
  weightage. At default config, 10 packets in 1/2 second will form a
  valid packet group for loss based congestion signal consideration. Two
  packets lost in that group may not be bad. So, bumped up the
  JQR min loss to 0.25. However, 20% loss (or even much lesser loss)
  could be problematic if the packet rate is higher (potentially
  multiple streams affected and there could be a lot of NACKs as a result).
  So, weight it by packet rate so that higher packet rates enter JQR
  at lower losses.

* WIP

* use aggregated loss
This commit is contained in:
Raja Subramanian
2024-11-12 09:21:28 +05:30
committed by GitHub
parent 57b3dfdcf4
commit a825661aff
7 changed files with 488 additions and 206 deletions
+42 -105
View File
@@ -1,8 +1,21 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sendsidebwe
import (
"errors"
"math"
"time"
"github.com/livekit/protocol/logger"
@@ -22,18 +35,12 @@ var (
type PacketGroupConfig struct {
MinPackets int `yaml:"min_packets,omitempty"`
MaxWindowDuration time.Duration `yaml:"max_window_duration,omitempty"`
// should have at least this fraction of `MinPackets` for loss penalty consideration
LossPenaltyMinPacketsRatio float64 `yaml:"loss_penalty_min_packet_ratio,omitempty"`
LossPenaltyFactor float64 `yaml:"loss_penalty_factor,omitempty"`
}
var (
DefaultPacketGroupConfig = PacketGroupConfig{
MinPackets: 20,
MaxWindowDuration: 500 * time.Millisecond,
LossPenaltyMinPacketsRatio: 0.5,
LossPenaltyFactor: 0.25,
MinPackets: 20,
MaxWindowDuration: 500 * time.Millisecond,
}
)
@@ -108,8 +115,9 @@ func (c classStat) MarshalLogObject(e zapcore.ObjectEncoder) error {
// -------------------------------------------------------------
type packetGroupParams struct {
Config PacketGroupConfig
Logger logger.Logger
Config PacketGroupConfig
WeightedLoss WeightedLossConfig
Logger logger.Logger
}
type packetGroup struct {
@@ -212,6 +220,10 @@ func (p *packetGroup) MinSendTime() int64 {
return p.minSendTime
}
func (p *packetGroup) SendWindow() (int64, int64) {
return p.maxSendTime, p.minSendTime
}
func (p *packetGroup) PropagatedQueuingDelay() (int64, bool) {
if !p.isFinalized {
return 0, false
@@ -224,57 +236,16 @@ func (p *packetGroup) PropagatedQueuingDelay() (int64, bool) {
return max(0, p.aggregateRecvDelta-p.aggregateSendDelta), true
}
func (p *packetGroup) SendDuration() int64 {
if !p.isFinalized {
return 0
func (p *packetGroup) Traffic() *trafficStats {
return &trafficStats{
minSendTime: p.minSendTime,
maxSendTime: p.maxSendTime,
sendDelta: p.aggregateSendDelta,
recvDelta: p.aggregateRecvDelta,
ackedPackets: p.acked.numPackets(),
ackedBytes: p.acked.numBytes(),
lostPackets: p.lost.numPackets(),
}
return p.maxSendTime - p.minSendTime
}
func (p *packetGroup) CapturedTrafficRatio() float64 {
capturedTrafficRatio := float64(0.0)
if p.aggregateRecvDelta != 0 {
// apply a penalty for lost packets,
// tha rationale being packet dropping is a strategy to relieve congestion
// and if they were not dropped, they would have increased queuing delay,
// as it is not possible to know the reason for the losses,
// apply a small penalty to receive delta aggregate to simulate those packets
// build up queuing delay.
//
// note that it is applied only for determining rate and
// not while determining queuing region, adding synthetic delays
// like this could cause queuing region to be stuck in JQR
capturedTrafficRatio = float64(p.aggregateSendDelta) / float64(p.aggregateRecvDelta+p.getLossPenalty())
}
return min(1.0, capturedTrafficRatio)
}
func (p *packetGroup) Traffic() (int64, int64, int, float64) {
numBytes := int(float64(p.acked.numBytes()) * p.CapturedTrafficRatio())
fullness := max(
float64(p.acked.numPackets())/float64(p.params.Config.MinPackets),
float64(p.maxSendTime-p.minSendTime)/float64(p.params.Config.MaxWindowDuration.Microseconds()),
)
return p.minSendTime, p.maxSendTime - p.minSendTime, numBytes, fullness
}
func (p *packetGroup) LossRatio() (float64, bool) {
if !p.isFinalized {
return 0.0, false
}
return p.lossRatio()
}
func (p *packetGroup) lossRatio() (float64, bool) {
lostPackets := p.lost.numPackets()
totalPackets := float64(lostPackets + p.acked.numPackets())
lossRatio := float64(lostPackets) / totalPackets
// indeterminate if not enough packets, the second return value will be false if indeterminate
return lossRatio, totalPackets >= float64(p.params.Config.MinPackets)*p.params.Config.LossPenaltyMinPacketsRatio
}
func (p *packetGroup) MarshalLogObject(e zapcore.ObjectEncoder) error {
@@ -282,26 +253,15 @@ func (p *packetGroup) MarshalLogObject(e zapcore.ObjectEncoder) error {
return nil
}
e.AddInt64("minSendTime", p.minSendTime)
e.AddInt64("maxSendTime", p.maxSendTime)
sendDuration := time.Duration((p.maxSendTime - p.minSendTime) * 1000)
e.AddDuration("sendDuration", sendDuration)
e.AddInt64("minRecvTime", p.minRecvTime)
e.AddInt64("maxRecvTime", p.maxRecvTime)
recvDuration := time.Duration((p.maxRecvTime - p.minRecvTime) * 1000)
e.AddDuration("recvDuration", recvDuration)
e.AddUint64("minSequenceNumber", p.minSequenceNumber)
e.AddUint64("maxSequenceNumber", p.maxSequenceNumber)
e.AddObject("acked", p.acked)
e.AddObject("lost", p.lost)
sendBitrate := float64(0)
if sendDuration != 0 {
sendBitrate = float64(p.acked.numBytes()*8) / sendDuration.Seconds()
e.AddFloat64("sendBitrate", sendBitrate)
}
e.AddInt64("minRecvTime", p.minRecvTime)
e.AddInt64("maxRecvTime", p.maxRecvTime)
recvDuration := time.Duration((p.maxRecvTime - p.minRecvTime) * 1000)
e.AddDuration("recvDuration", recvDuration)
recvBitrate := float64(0)
if recvDuration != 0 {
@@ -309,20 +269,12 @@ func (p *packetGroup) MarshalLogObject(e zapcore.ObjectEncoder) error {
e.AddFloat64("recvBitrate", recvBitrate)
}
e.AddInt64("aggregateSendDelta", p.aggregateSendDelta)
e.AddInt64("aggregateRecvDelta", p.aggregateRecvDelta)
e.AddInt64("queuingDelay", p.queuingDelay)
e.AddInt64("groupDelay", p.aggregateRecvDelta-p.aggregateSendDelta)
lossRatio, lossRatioValid := p.lossRatio()
e.AddFloat64("lossRatio", lossRatio)
e.AddBool("lossRatioValid", lossRatioValid)
e.AddInt64("lossPenalty", p.getLossPenalty())
capturedTrafficRatio := p.CapturedTrafficRatio()
e.AddFloat64("capturedTrafficRatio", capturedTrafficRatio)
e.AddFloat64("estimatedAvailableChannelCapacity", sendBitrate*capturedTrafficRatio)
ts := newTrafficStats(trafficStatsParams{
Config: p.params.WeightedLoss,
Logger: p.params.Logger,
})
ts.Merge(p.Traffic())
e.AddObject("trafficStats", ts)
e.AddBool("isFinalized", p.isFinalized)
return nil
@@ -339,18 +291,3 @@ func (p *packetGroup) inGroup(sequenceNumber uint64) error {
return nil
}
func (p *packetGroup) getLossPenalty() int64 {
// Log10 is used to give higher weight for the same loss ratio at higher packet rates,
// for e.g. with a penalty factor of 0.25
// - 10% loss at 20 total packets = 0.1 * log10(20) * 0.25 = 0.032
// - 10% loss at 100 total packets = 0.1 * log10(100) * 0.25 = 0.05
// - 10% loss at 1000 total packets = 0.1 * log10(100) * 0.25 = 0.075
lossRatio, _ := p.lossRatio()
return int64(
float64(p.aggregateRecvDelta) *
lossRatio *
math.Log10(float64(p.acked.numPackets()+p.lost.numPackets())) *
p.params.Config.LossPenaltyFactor,
)
}