Reduce packet meta data cache - part 1 (#2073)

* Reduce packet meta data cache - part 1

Packet meta data cache takes a good amount of space.
That cache is 8K entries deep and each entry is 8 bytes.
So, that takes 64KB per RTP stream.

It is mostly needed for down stream to line up with receiver reports.

So, removing cache from up stream (RTPStatsReceiver) as part 1.
Will look at optimising the down stream in part 2.

* Remove caching from RTPStatsReceiver

* clean up a bit more

* maintain history and fix test
This commit is contained in:
Raja Subramanian
2023-09-15 21:39:03 +05:30
committed by GitHub
parent 7ac0abdf1b
commit 044f6cec40
4 changed files with 196 additions and 156 deletions
+73 -10
View File
@@ -24,6 +24,10 @@ import (
"github.com/livekit/protocol/livekit"
)
const (
cHistorySize = 2048
)
type RTPFlowState struct {
IsNotHandled bool
@@ -47,6 +51,8 @@ type RTPStatsReceiver struct {
sequenceNumber *utils.WrapAround[uint16, uint64]
timestamp *utils.WrapAround[uint32, uint64]
history [cHistorySize / 64]uint64
}
func NewRTPStatsReceiver(params RTPStatsParams) *RTPStatsReceiver {
@@ -107,10 +113,7 @@ func (r *RTPStatsReceiver) Update(
// initialize snapshots if any
for i := uint32(cFirstSnapshotID); i < r.nextSnapshotID; i++ {
r.snapshots[i] = &snapshot{
startTime: r.startTime,
extStartSN: r.sequenceNumber.GetExtendedStart(),
}
r.snapshots[i] = r.initSnapshot(r.startTime, r.sequenceNumber.GetExtendedStart())
}
r.logger.Debugw(
@@ -170,14 +173,14 @@ func (r *RTPStatsReceiver) Update(
)
}
if !r.isSnInfoLost(resSN.ExtendedVal, resSN.PreExtendedHighest) {
if !r.isLost(resSN.ExtendedVal, resSN.PreExtendedHighest) {
r.bytesDuplicate += pktSize
r.headerBytesDuplicate += uint64(hdrSize)
r.packetsDuplicate++
flowState.IsDuplicate = true
} else {
r.packetsLost--
r.setSnInfo(resSN.ExtendedVal, resSN.PreExtendedHighest, uint16(pktSize), uint16(hdrSize), uint16(payloadSize), marker, true)
r.setHistory(resSN.ExtendedVal, resSN.PreExtendedHighest)
}
flowState.IsOutOfOrder = true
@@ -188,10 +191,10 @@ func (r *RTPStatsReceiver) Update(
r.updateGapHistogram(int(gapSN))
// update missing sequence numbers
r.clearSnInfos(resSN.PreExtendedHighest+1, resSN.ExtendedVal)
r.clearHistory(resSN.PreExtendedHighest+1, resSN.ExtendedVal, resSN.PreExtendedHighest)
r.packetsLost += uint64(gapSN - 1)
r.setSnInfo(resSN.ExtendedVal, resSN.PreExtendedHighest, uint16(pktSize), uint16(hdrSize), uint16(payloadSize), marker, false)
r.setHistory(resSN.ExtendedVal, resSN.PreExtendedHighest)
if timestamp != uint32(resTS.PreExtendedHighest) {
// update only on first packet as same timestamp could be in multiple packets.
@@ -409,8 +412,10 @@ func (r *RTPStatsReceiver) GetRtcpReceptionReport(ssrc uint32, proxyFracLost uin
return nil
}
intervalStats := r.getIntervalStats(then.extStartSN, now.extStartSN, extHighestSN)
packetsLost := intervalStats.packetsLost
packetsLost := uint32(now.packetsLost - then.packetsLost)
if int32(packetsLost) < 0 {
packetsLost = 0
}
lossRate := float32(packetsLost) / float32(packetsExpected)
fracLost := uint8(lossRate * 256.0)
if proxyFracLost > fracLost {
@@ -468,4 +473,62 @@ func (r *RTPStatsReceiver) ToProto() *livekit.RTPStats {
)
}
func (r *RTPStatsReceiver) getOutOfOrderHistorySlot(esn uint64, ehsn uint64) (int, int) {
diff := int64(ehsn - esn)
if diff >= cHistorySize || diff < 0 {
// too old OR too new (i. e. ahead of highest)
return -1, -1
}
return int(esn) % len(r.history), int(esn & 63)
}
func (r *RTPStatsReceiver) getHistorySlot(esn uint64, ehsn uint64) (int, int) {
if int64(esn-ehsn) < 0 {
return r.getOutOfOrderHistorySlot(esn, ehsn)
}
return int(esn) % len(r.history), int(esn & 63)
}
func (r *RTPStatsReceiver) setHistory(esn uint64, ehsn uint64) {
slot, offset := r.getHistorySlot(esn, ehsn)
if slot < 0 {
return
}
r.history[slot] |= (1 << offset)
}
func (r *RTPStatsReceiver) clearHistory(extStartInclusive uint64, extEndExclusive uint64, ehsn uint64) {
if extEndExclusive <= extStartInclusive {
return
}
slot, offset := r.getHistorySlot(extStartInclusive, ehsn)
if slot < 0 {
return
}
for esn := extStartInclusive; esn != extEndExclusive; esn++ {
r.history[slot] &= ^(1 << offset)
offset++
if offset > 63 {
offset -= 64
slot++
if slot >= len(r.history) {
slot -= len(r.history)
}
}
}
}
func (r *RTPStatsReceiver) isLost(esn uint64, ehsn uint64) bool {
slot, offset := r.getHistorySlot(esn, ehsn)
if slot < 0 {
return false
}
return r.history[slot]&(1<<offset) == 0
}
// ----------------------------------