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
+227 -101
View File
@@ -1,3 +1,17 @@
// 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 package sendsidebwe
import ( import (
@@ -43,36 +57,138 @@ var (
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
type congestionSignalCalculator[T int64 | float64] struct { type qdMeasurement struct {
thresholdMin T earlyWarningConfig CongestionSignalConfig
thresholdMax T congestedConfig CongestionSignalConfig
isSealed bool jqrMin int64
numGroups int dqrMax int64
duration int64
numGroups int
minSendTime int64
maxSendTime int64
isSealed bool
} }
func (c *congestionSignalCalculator[T]) processSample(val T, duration int64) { func newQdMeasurement(
if c.isSealed { earlyWarningConfig CongestionSignalConfig,
return congestedConfig CongestionSignalConfig,
} jqrMin int64,
dqrMax int64,
if val < c.thresholdMin { ) *qdMeasurement {
// any DQR group breaks the continuity return &qdMeasurement{
c.isSealed = true earlyWarningConfig: earlyWarningConfig,
return congestedConfig: congestedConfig,
} jqrMin: jqrMin,
dqrMax: dqrMax,
// INDETERMINATE group is treated as a no-op
// JQR group builds up congestion signal
if val > c.thresholdMax {
c.numGroups++
c.duration += duration
} }
} }
func (c *congestionSignalCalculator[T]) isTriggered(config CongestionSignalConfig) bool { func (q *qdMeasurement) ProcessPacketGroup(pg *packetGroup) {
return c.numGroups >= config.MinNumberOfGroups && c.duration >= config.MinDuration.Microseconds() if q.isSealed {
return
}
pqd, pqdOk := pg.PropagatedQueuingDelay()
if !pqdOk {
return
}
if pqd < q.dqrMax {
// a DQR breaks continuity
q.isSealed = true
return
}
if pqd > q.jqrMin {
q.numGroups++
minSendTime, maxSendTime := pg.SendWindow()
if q.minSendTime == 0 || minSendTime < q.minSendTime {
q.minSendTime = minSendTime
}
if maxSendTime > q.maxSendTime {
q.maxSendTime = maxSendTime
}
}
// can seal if congested config thresholds are met as they are longer
if q.numGroups >= q.congestedConfig.MinNumberOfGroups && (q.maxSendTime-q.minSendTime) >= q.congestedConfig.MinDuration.Microseconds() {
q.isSealed = true
}
}
func (q *qdMeasurement) IsSealed() bool {
return q.isSealed
}
func (q *qdMeasurement) IsEarlyWarningTriggered() bool {
return q.numGroups >= q.earlyWarningConfig.MinNumberOfGroups && (q.maxSendTime-q.minSendTime) >= q.earlyWarningConfig.MinDuration.Microseconds()
}
func (q *qdMeasurement) IsCongestedTriggered() bool {
return q.numGroups >= q.congestedConfig.MinNumberOfGroups && (q.maxSendTime-q.minSendTime) >= q.congestedConfig.MinDuration.Microseconds()
}
// -------------------------------------------------------------------------------
type lossMeasurement struct {
earlyWarningConfig CongestionSignalConfig
congestedConfig CongestionSignalConfig
congestionMinLoss float64
numGroups int
ts *trafficStats
earlyWarningWeightedLoss float64
congestedWeightedLoss float64
isSealed bool
}
func newLossMeasurement(
earlyWarningConfig CongestionSignalConfig,
congestedConfig CongestionSignalConfig,
weightedLossConfig WeightedLossConfig,
congestionMinLoss float64,
logger logger.Logger,
) *lossMeasurement {
return &lossMeasurement{
earlyWarningConfig: earlyWarningConfig,
congestedConfig: congestedConfig,
congestionMinLoss: congestionMinLoss,
ts: newTrafficStats(trafficStatsParams{
Config: weightedLossConfig,
Logger: logger,
}),
}
}
func (l *lossMeasurement) ProcessPacketGroup(pg *packetGroup) {
if l.isSealed {
return
}
l.ts.Merge(pg.Traffic())
duration := l.ts.Duration()
if l.numGroups >= l.earlyWarningConfig.MinNumberOfGroups && duration >= l.earlyWarningConfig.MinDuration.Microseconds() {
l.earlyWarningWeightedLoss = l.ts.WeightedLoss()
}
if l.numGroups >= l.congestedConfig.MinNumberOfGroups && duration >= l.congestedConfig.MinDuration.Microseconds() {
l.congestedWeightedLoss = l.ts.WeightedLoss()
l.isSealed = true // can seal if congested thresholds are satisfied as those should be higher
}
}
func (l *lossMeasurement) IsSealed() bool {
return l.isSealed
}
func (l *lossMeasurement) IsEarlyWarningTriggered() bool {
return l.earlyWarningWeightedLoss > l.congestionMinLoss
}
func (l *lossMeasurement) IsCongestedTriggered() bool {
return l.congestedWeightedLoss > l.congestionMinLoss
} }
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
@@ -84,8 +200,8 @@ type CongestionDetectorConfig struct {
JQRMinDelay time.Duration `yaml:"jqr_min_delay,omitempty"` JQRMinDelay time.Duration `yaml:"jqr_min_delay,omitempty"`
DQRMaxDelay time.Duration `yaml:"dqr_max_delay,omitempty"` DQRMaxDelay time.Duration `yaml:"dqr_max_delay,omitempty"`
JQRMinLoss float64 `yaml:"jqr_min_loss,omitempty"` WeightedLoss WeightedLossConfig `yaml:"weighted_loss,omitempty"`
DQRMaxLoss float64 `yaml:"dqr_max_loss,omitempty"` CongestionMinLoss float64 `yaml:"congestion_min_loss,omitempty"`
QueuingDelayEarlyWarning CongestionSignalConfig `yaml:"queuing_delay_early_warning,omitempty"` QueuingDelayEarlyWarning CongestionSignalConfig `yaml:"queuing_delay_early_warning,omitempty"`
LossEarlyWarning CongestionSignalConfig `yaml:"loss_early_warning,omitempty"` LossEarlyWarning CongestionSignalConfig `yaml:"loss_early_warning,omitempty"`
@@ -95,7 +211,6 @@ type CongestionDetectorConfig struct {
LossCongested CongestionSignalConfig `yaml:"loss_congested,omitempty"` LossCongested CongestionSignalConfig `yaml:"loss_congested,omitempty"`
CongestedHangover time.Duration `yaml:"congested_hangover,omitempty"` CongestedHangover time.Duration `yaml:"congested_hangover,omitempty"`
RateMeasurementWindowFullnessMin float64 `yaml:"rate_measurement_window_fullness_min,omitempty"`
RateMeasurementWindowDurationMin time.Duration `yaml:"rate_measurement_window_duration_min,omitempty"` RateMeasurementWindowDurationMin time.Duration `yaml:"rate_measurement_window_duration_min,omitempty"`
RateMeasurementWindowDurationMax time.Duration `yaml:"rate_measurement_window_duration_max,omitempty"` RateMeasurementWindowDurationMax time.Duration `yaml:"rate_measurement_window_duration_max,omitempty"`
@@ -109,7 +224,7 @@ var (
defaultTrendDetectorConfigCongestedCTR = ccutils.TrendDetectorConfig{ defaultTrendDetectorConfigCongestedCTR = ccutils.TrendDetectorConfig{
RequiredSamples: 5, RequiredSamples: 5,
RequiredSamplesMin: 2, RequiredSamplesMin: 2,
DownwardTrendThreshold: -0.6, DownwardTrendThreshold: -0.5,
DownwardTrendMaxWait: 5 * time.Second, DownwardTrendMaxWait: 5 * time.Second,
CollapseThreshold: 500 * time.Millisecond, CollapseThreshold: 500 * time.Millisecond,
ValidityWindow: 10 * time.Second, ValidityWindow: 10 * time.Second,
@@ -120,15 +235,14 @@ var (
PacketGroupMaxAge: 30 * time.Second, PacketGroupMaxAge: 30 * time.Second,
JQRMinDelay: 15 * time.Millisecond, JQRMinDelay: 15 * time.Millisecond,
DQRMaxDelay: 5 * time.Millisecond, DQRMaxDelay: 5 * time.Millisecond,
JQRMinLoss: 0.15, WeightedLoss: defaultWeightedLossConfig,
DQRMaxLoss: 0.05, CongestionMinLoss: 0.25,
QueuingDelayEarlyWarning: DefaultQueuingDelayEarlyWarningCongestionSignalConfig, QueuingDelayEarlyWarning: DefaultQueuingDelayEarlyWarningCongestionSignalConfig,
LossEarlyWarning: DefaultLossEarlyWarningCongestionSignalConfig, LossEarlyWarning: DefaultLossEarlyWarningCongestionSignalConfig,
EarlyWarningHangover: 500 * time.Millisecond, EarlyWarningHangover: 500 * time.Millisecond,
QueuingDelayCongested: DefaultQueuingDelayCongestedCongestionSignalConfig, QueuingDelayCongested: DefaultQueuingDelayCongestedCongestionSignalConfig,
LossCongested: DefaultLossCongestedCongestionSignalConfig, LossCongested: DefaultLossCongestedCongestionSignalConfig,
CongestedHangover: 3 * time.Second, CongestedHangover: 3 * time.Second,
RateMeasurementWindowFullnessMin: 0.8,
RateMeasurementWindowDurationMin: 800 * time.Millisecond, RateMeasurementWindowDurationMin: 800 * time.Millisecond,
RateMeasurementWindowDurationMax: 2 * time.Second, RateMeasurementWindowDurationMax: 2 * time.Second,
PeriodicCheckInterval: 2 * time.Second, PeriodicCheckInterval: 2 * time.Second,
@@ -168,6 +282,7 @@ type congestionDetector struct {
congestionState CongestionState congestionState CongestionState
congestionStateSwitchedAt time.Time congestionStateSwitchedAt time.Time
congestedCTRTrend *ccutils.TrendDetector[float64] congestedCTRTrend *ccutils.TrendDetector[float64]
congestedTrafficStats *trafficStats
onCongestionStateChange func(congestionState CongestionState, estimatedAvailableChannelCapacity int64) onCongestionStateChange func(congestionState CongestionState, estimatedAvailableChannelCapacity int64)
} }
@@ -235,12 +350,17 @@ func (c *congestionDetector) updateCongestionState(state CongestionState, reason
// reducing/relieving congestion // reducing/relieving congestion
if state == CongestionStateCongested && prevState != CongestionStateCongested { if state == CongestionStateCongested && prevState != CongestionStateCongested {
c.congestedCTRTrend = ccutils.NewTrendDetector[float64](ccutils.TrendDetectorParams{ c.congestedCTRTrend = ccutils.NewTrendDetector[float64](ccutils.TrendDetectorParams{
Name: "ssbwe-estimate", Name: "ssbwe-ctr",
Logger: c.params.Logger, Logger: c.params.Logger,
Config: c.params.Config.CongestedCTRTrend, Config: c.params.Config.CongestedCTRTrend,
}) })
c.congestedTrafficStats = newTrafficStats(trafficStatsParams{
Config: c.params.Config.WeightedLoss,
Logger: c.params.Logger,
})
} else if state != CongestionStateCongested { } else if state != CongestionStateCongested {
c.congestedCTRTrend = nil c.congestedCTRTrend = nil
c.congestedTrafficStats = nil
} }
} }
@@ -268,72 +388,67 @@ func (c *congestionDetector) prunePacketGroups() {
return return
} }
threshold := c.packetGroups[len(c.packetGroups)-1].MinSendTime() - c.params.Config.PacketGroupMaxAge.Microseconds() bst := c.packetTracker.BaseSendTime()
if bst == 0 {
return
}
threshold := mono.UnixMicro() - bst - c.params.Config.PacketGroupMaxAge.Microseconds()
for idx, pg := range c.packetGroups { for idx, pg := range c.packetGroups {
if mst := pg.MinSendTime(); mst < threshold { if mst := pg.MinSendTime(); mst > threshold {
c.packetGroups = c.packetGroups[idx+1:] c.packetGroups = c.packetGroups[idx:]
return return
} }
} }
} }
func (c *congestionDetector) isCongestionSignalTriggered() (bool, string, bool, string, int) { func (c *congestionDetector) isCongestionSignalTriggered() (bool, string, bool, string, int) {
earlyWarningTriggered := false qdMeasurement := newQdMeasurement(
earlyWarningReason := "" c.params.Config.QueuingDelayEarlyWarning,
c.params.Config.QueuingDelayCongested,
congestedTriggered := false c.params.Config.JQRMinDelay.Microseconds(),
congestedReason := "" c.params.Config.DQRMaxDelay.Microseconds(),
)
qd := &congestionSignalCalculator[int64]{ lossMeasurement := newLossMeasurement(
thresholdMin: c.params.Config.JQRMinDelay.Microseconds(), c.params.Config.LossEarlyWarning,
thresholdMax: c.params.Config.DQRMaxDelay.Microseconds(), c.params.Config.LossCongested,
} c.params.Config.WeightedLoss,
loss := &congestionSignalCalculator[float64]{ c.params.Config.CongestionMinLoss,
thresholdMin: c.params.Config.JQRMinLoss, c.params.Logger,
thresholdMax: c.params.Config.DQRMaxLoss, )
}
var idx int var idx int
for idx = len(c.packetGroups) - 1; idx >= 0; idx-- { for idx = len(c.packetGroups) - 1; idx >= 0; idx-- {
pg := c.packetGroups[idx] pg := c.packetGroups[idx]
pqd, pqdOk := pg.PropagatedQueuingDelay() qdMeasurement.ProcessPacketGroup(pg)
lr, lrOk := pg.LossRatio() lossMeasurement.ProcessPacketGroup(pg)
if !pqdOk && !lrOk {
continue
}
// `queueing delay` and `loss` based congestion signals are determined independently, // if both measurements have enough data to make a decision, stop processing groups
// i. e. one packet group triggering `queueing delay` and another group triggering if qdMeasurement.IsSealed() && lossMeasurement.IsSealed() {
// `loss` will not combine to trigger the aggregate congestion signal break
sendDuration := pg.SendDuration()
if pqdOk {
qd.processSample(pqd, sendDuration)
}
if lrOk {
loss.processSample(lr, sendDuration)
} }
}
if !earlyWarningTriggered && qd.isTriggered(c.params.Config.QueuingDelayEarlyWarning) { earlyWarningReason := ""
earlyWarningTriggered = true earlyWarningTriggered := qdMeasurement.IsEarlyWarningTriggered()
earlyWarningReason = "queuing-delay" if earlyWarningTriggered {
} earlyWarningReason = "queuing-delay"
if !earlyWarningTriggered && loss.isTriggered(c.params.Config.LossEarlyWarning) { } else {
earlyWarningTriggered = true earlyWarningTriggered = lossMeasurement.IsEarlyWarningTriggered()
if earlyWarningTriggered {
earlyWarningReason = "loss" earlyWarningReason = "loss"
} }
}
if !congestedTriggered && qd.isTriggered(c.params.Config.QueuingDelayCongested) { congestedReason := ""
congestedTriggered = true congestedTriggered := qdMeasurement.IsCongestedTriggered()
congestedReason = "queuing-delay" if congestedTriggered {
} congestedReason = "queuing-delay"
if !congestedTriggered && loss.isTriggered(c.params.Config.LossCongested) { } else {
congestedTriggered = true congestedTriggered = lossMeasurement.IsCongestedTriggered()
if congestedTriggered {
congestedReason = "loss" congestedReason = "loss"
} }
if earlyWarningTriggered && congestedTriggered {
break
}
} }
return earlyWarningTriggered, earlyWarningReason, congestedTriggered, congestedReason, idx return earlyWarningTriggered, earlyWarningReason, congestedTriggered, congestedReason, idx
@@ -401,12 +516,17 @@ func (c *congestionDetector) congestionDetectionStateMachine() {
} }
} }
func (c *congestionDetector) updateTrend(ctr float64) { func (c *congestionDetector) updateTrend(pg *packetGroup) {
if c.congestedCTRTrend == nil { if c.congestedCTRTrend == nil {
return return
} }
// quantise the CTR to filter out small changes // progressively keep increasing the window and make measurements over longer windows,
// if congestion is not relieving, CTR will trend down
c.congestedTrafficStats.Merge(pg.Traffic())
ctr := c.congestedTrafficStats.CapturedTrafficRatio()
// quantise CTR to filter out small changes
c.congestedCTRTrend.AddValue(float64(int((ctr+(c.params.Config.CongestedCTREpsilon/2))/c.params.Config.CongestedCTREpsilon)) * c.params.Config.CongestedCTREpsilon) c.congestedCTRTrend.AddValue(float64(int((ctr+(c.params.Config.CongestedCTREpsilon/2))/c.params.Config.CongestedCTREpsilon)) * c.params.Config.CongestedCTREpsilon)
if c.congestedCTRTrend.GetDirection() == ccutils.TrendDirectionDownward { if c.congestedCTRTrend.GetDirection() == ccutils.TrendDirectionDownward {
@@ -424,10 +544,14 @@ func (c *congestionDetector) updateTrend(ctr float64) {
// reset to get new set of samples for next trend // reset to get new set of samples for next trend
c.congestedCTRTrend = ccutils.NewTrendDetector[float64](ccutils.TrendDetectorParams{ c.congestedCTRTrend = ccutils.NewTrendDetector[float64](ccutils.TrendDetectorParams{
Name: "ssbwe-estimate", Name: "ssbwe-ctr",
Logger: c.params.Logger, Logger: c.params.Logger,
Config: c.params.Config.CongestedCTRTrend, Config: c.params.Config.CongestedCTRTrend,
}) })
c.congestedTrafficStats = newTrafficStats(trafficStatsParams{
Config: c.params.Config.WeightedLoss,
Logger: c.params.Logger,
})
} }
} }
@@ -436,31 +560,33 @@ func (c *congestionDetector) estimateAvailableChannelCapacity() {
return return
} }
totalDuration := int64(0) bst := c.packetTracker.BaseSendTime()
totalBytes := 0 if bst == 0 {
threshold := c.packetGroups[len(c.packetGroups)-1].MinSendTime() - c.params.Config.RateMeasurementWindowDurationMax.Microseconds() return
}
threshold := mono.UnixMicro() - bst - c.params.Config.RateMeasurementWindowDurationMax.Microseconds()
agg := newTrafficStats(trafficStatsParams{
Config: c.params.Config.WeightedLoss,
Logger: c.params.Logger,
})
for idx := len(c.packetGroups) - 1; idx >= 0; idx-- { for idx := len(c.packetGroups) - 1; idx >= 0; idx-- {
pg := c.packetGroups[idx] pg := c.packetGroups[idx]
mst, dur, nbytes, fullness := pg.Traffic() if pg.MinSendTime() < threshold {
if mst < threshold {
break break
} }
if fullness < c.params.Config.RateMeasurementWindowFullnessMin { agg.Merge(pg.Traffic())
continue
}
totalDuration += dur
totalBytes += nbytes
} }
if totalDuration >= c.params.Config.RateMeasurementWindowDurationMin.Microseconds() { if agg.Duration() < c.params.Config.RateMeasurementWindowDurationMin.Microseconds() {
c.lock.Lock() c.params.Logger.Infow("not enough data to estimate available channel capacity", "duration", agg.Duration())
c.estimatedAvailableChannelCapacity = int64(totalBytes) * 8 * 1e6 / totalDuration return
c.lock.Unlock()
} else {
c.params.Logger.Infow("not enough data to estimate available channel capacity", "totalDuration", totalDuration)
} }
c.lock.Lock()
c.estimatedAvailableChannelCapacity = agg.AcknowledgedBitrate()
c.lock.Unlock()
} }
func (c *congestionDetector) processFeedbackReport(fbr feedbackReport) { func (c *congestionDetector) processFeedbackReport(fbr feedbackReport) {
@@ -495,7 +621,7 @@ func (c *congestionDetector) processFeedbackReport(fbr feedbackReport) {
if err == errGroupFinalized { if err == errGroupFinalized {
// previous group ended, start a new group // previous group ended, start a new group
c.updateTrend(pg.CapturedTrafficRatio()) c.updateTrend(pg)
// SSBWE-REMOVE c.params.Logger.Infow("packet group done", "group", pg, "numGroups", len(c.packetGroups)) // SSBWE-REMOVE // SSBWE-REMOVE c.params.Logger.Infow("packet group done", "group", pg, "numGroups", len(c.packetGroups)) // SSBWE-REMOVE
pqd, _ := pg.PropagatedQueuingDelay() pqd, _ := pg.PropagatedQueuingDelay()
+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 package sendsidebwe
import ( import (
"errors" "errors"
"math"
"time" "time"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
@@ -22,18 +35,12 @@ var (
type PacketGroupConfig struct { type PacketGroupConfig struct {
MinPackets int `yaml:"min_packets,omitempty"` MinPackets int `yaml:"min_packets,omitempty"`
MaxWindowDuration time.Duration `yaml:"max_window_duration,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 ( var (
DefaultPacketGroupConfig = PacketGroupConfig{ DefaultPacketGroupConfig = PacketGroupConfig{
MinPackets: 20, MinPackets: 20,
MaxWindowDuration: 500 * time.Millisecond, MaxWindowDuration: 500 * time.Millisecond,
LossPenaltyMinPacketsRatio: 0.5,
LossPenaltyFactor: 0.25,
} }
) )
@@ -108,8 +115,9 @@ func (c classStat) MarshalLogObject(e zapcore.ObjectEncoder) error {
// ------------------------------------------------------------- // -------------------------------------------------------------
type packetGroupParams struct { type packetGroupParams struct {
Config PacketGroupConfig Config PacketGroupConfig
Logger logger.Logger WeightedLoss WeightedLossConfig
Logger logger.Logger
} }
type packetGroup struct { type packetGroup struct {
@@ -212,6 +220,10 @@ func (p *packetGroup) MinSendTime() int64 {
return p.minSendTime return p.minSendTime
} }
func (p *packetGroup) SendWindow() (int64, int64) {
return p.maxSendTime, p.minSendTime
}
func (p *packetGroup) PropagatedQueuingDelay() (int64, bool) { func (p *packetGroup) PropagatedQueuingDelay() (int64, bool) {
if !p.isFinalized { if !p.isFinalized {
return 0, false return 0, false
@@ -224,57 +236,16 @@ func (p *packetGroup) PropagatedQueuingDelay() (int64, bool) {
return max(0, p.aggregateRecvDelta-p.aggregateSendDelta), true return max(0, p.aggregateRecvDelta-p.aggregateSendDelta), true
} }
func (p *packetGroup) SendDuration() int64 { func (p *packetGroup) Traffic() *trafficStats {
if !p.isFinalized { return &trafficStats{
return 0 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 { func (p *packetGroup) MarshalLogObject(e zapcore.ObjectEncoder) error {
@@ -282,26 +253,15 @@ func (p *packetGroup) MarshalLogObject(e zapcore.ObjectEncoder) error {
return nil 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("minSequenceNumber", p.minSequenceNumber)
e.AddUint64("maxSequenceNumber", p.maxSequenceNumber) e.AddUint64("maxSequenceNumber", p.maxSequenceNumber)
e.AddObject("acked", p.acked) e.AddObject("acked", p.acked)
e.AddObject("lost", p.lost) e.AddObject("lost", p.lost)
sendBitrate := float64(0) e.AddInt64("minRecvTime", p.minRecvTime)
if sendDuration != 0 { e.AddInt64("maxRecvTime", p.maxRecvTime)
sendBitrate = float64(p.acked.numBytes()*8) / sendDuration.Seconds() recvDuration := time.Duration((p.maxRecvTime - p.minRecvTime) * 1000)
e.AddFloat64("sendBitrate", sendBitrate) e.AddDuration("recvDuration", recvDuration)
}
recvBitrate := float64(0) recvBitrate := float64(0)
if recvDuration != 0 { if recvDuration != 0 {
@@ -309,20 +269,12 @@ func (p *packetGroup) MarshalLogObject(e zapcore.ObjectEncoder) error {
e.AddFloat64("recvBitrate", recvBitrate) e.AddFloat64("recvBitrate", recvBitrate)
} }
e.AddInt64("aggregateSendDelta", p.aggregateSendDelta) ts := newTrafficStats(trafficStatsParams{
e.AddInt64("aggregateRecvDelta", p.aggregateRecvDelta) Config: p.params.WeightedLoss,
e.AddInt64("queuingDelay", p.queuingDelay) Logger: p.params.Logger,
e.AddInt64("groupDelay", p.aggregateRecvDelta-p.aggregateSendDelta) })
ts.Merge(p.Traffic())
lossRatio, lossRatioValid := p.lossRatio() e.AddObject("trafficStats", ts)
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)
e.AddBool("isFinalized", p.isFinalized) e.AddBool("isFinalized", p.isFinalized)
return nil return nil
@@ -339,18 +291,3 @@ func (p *packetGroup) inGroup(sequenceNumber uint64) error {
return nil 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,
)
}
+14
View File
@@ -1,3 +1,17 @@
// 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 package sendsidebwe
import ( import (
+21
View File
@@ -1,3 +1,17 @@
// 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 package sendsidebwe
import ( import (
@@ -70,6 +84,13 @@ func (p *packetTracker) RecordPacketSendAndGetSequenceNumber(at time.Time, size
return uint16(pi.sequenceNumber) return uint16(pi.sequenceNumber)
} }
func (p *packetTracker) BaseSendTime() int64 {
p.lock.Lock()
defer p.lock.Unlock()
return p.baseSendTime
}
func (p *packetTracker) RecordPacketIndicationFromRemote(sn uint16, recvTime int64) (piRecv packetInfo, sendDelta, recvDelta int64) { func (p *packetTracker) RecordPacketIndicationFromRemote(sn uint16, recvTime int64) (piRecv packetInfo, sendDelta, recvDelta int64) {
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()
+14
View File
@@ -1,3 +1,17 @@
// 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 package sendsidebwe
import ( import (
+156
View File
@@ -0,0 +1,156 @@
// 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 (
"math"
"time"
"github.com/livekit/protocol/logger"
"go.uber.org/zap/zapcore"
)
// -----------------------------------------------------------
type WeightedLossConfig struct {
MinPacketsForLossValidity int `yaml:"min_packets_for_loss_validity,omitempty"`
LossPenaltyFactor float64 `yaml:"loss_penalty_factor,omitempty"`
}
var (
defaultWeightedLossConfig = WeightedLossConfig{
MinPacketsForLossValidity: 20,
LossPenaltyFactor: 0.25,
}
)
// -----------------------------------------------------------
type trafficStatsParams struct {
Config WeightedLossConfig
Logger logger.Logger
}
type trafficStats struct {
params trafficStatsParams
minSendTime int64
maxSendTime int64
sendDelta int64
recvDelta int64
ackedPackets int
ackedBytes int
lostPackets int
}
func newTrafficStats(params trafficStatsParams) *trafficStats {
return &trafficStats{
params: params,
}
}
func (ts *trafficStats) Merge(rhs *trafficStats) {
if rhs.minSendTime == 0 || rhs.minSendTime < ts.minSendTime {
ts.minSendTime = rhs.minSendTime
}
if rhs.maxSendTime > ts.maxSendTime {
ts.maxSendTime = rhs.maxSendTime
}
ts.sendDelta += rhs.sendDelta
ts.recvDelta += rhs.recvDelta
ts.ackedPackets += rhs.ackedPackets
ts.ackedBytes += rhs.ackedBytes
ts.lostPackets += rhs.lostPackets
}
func (ts *trafficStats) Duration() int64 {
return ts.maxSendTime - ts.minSendTime
}
func (ts *trafficStats) AcknowledgedBitrate() int64 {
ackedBitrate := int64(ts.ackedBytes) * 8 * 1e6 / ts.Duration()
return int64(float64(ackedBitrate) * ts.CapturedTrafficRatio())
}
func (ts *trafficStats) CapturedTrafficRatio() float64 {
if ts.recvDelta == 0 {
return 0.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
// building up queuing delay.
return min(1.0, float64(ts.sendDelta)/float64(ts.recvDelta+ts.lossPenalty()))
}
func (ts *trafficStats) WeightedLoss() float64 {
totalPackets := float64(ts.lostPackets + ts.ackedPackets)
if int(totalPackets) < ts.params.Config.MinPacketsForLossValidity {
return 0.0
}
lossRatio := float64(0.0)
if totalPackets != 0 {
lossRatio = float64(ts.lostPackets) / totalPackets
}
pps := totalPackets * 1e6 / float64(ts.Duration())
// 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 pps = 0.1 * log10(20) * 0.25 = 0.032
// - 10% loss at 100 pps = 0.1 * log10(100) * 0.25 = 0.05
// - 10% loss at 1000 pps = 0.1 * log10(1000) * 0.25 = 0.075
return lossRatio * math.Log10(pps)
}
func (ts *trafficStats) lossPenalty() int64 {
return int64(float64(ts.recvDelta) * ts.WeightedLoss() * ts.params.Config.LossPenaltyFactor)
}
func (ts *trafficStats) MarshalLogObject(e zapcore.ObjectEncoder) error {
if ts == nil {
return nil
}
e.AddInt64("minSendTime", ts.minSendTime)
e.AddInt64("maxSendTime", ts.maxSendTime)
duration := time.Duration(ts.Duration() * 1000)
e.AddDuration("duration", duration)
bitrate := float64(0)
if duration != 0 {
bitrate = float64(ts.ackedBytes*8) / duration.Seconds()
e.AddFloat64("bitrate", bitrate)
}
e.AddInt64("sendDelta", ts.sendDelta)
e.AddInt64("recvDelta", ts.recvDelta)
e.AddInt64("groupDelay", ts.recvDelta-ts.sendDelta)
e.AddFloat64("weightedLoss", ts.WeightedLoss())
if (ts.ackedPackets + ts.lostPackets) != 0 {
e.AddFloat64("rawLoss", float64(ts.lostPackets)/float64(ts.ackedPackets+ts.lostPackets))
}
e.AddInt64("lossPenalty", ts.lossPenalty())
capturedTrafficRatio := ts.CapturedTrafficRatio()
e.AddFloat64("capturedTrafficRatio", capturedTrafficRatio)
e.AddFloat64("estimatedAvailableChannelCapacity", bitrate*capturedTrafficRatio)
return nil
}
+14
View File
@@ -1,3 +1,17 @@
// 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 package sendsidebwe
import ( import (