Reducing rtp stats memory consumption - part 2 (#2078)

* WIP commit

* move a struct to sender only

* Snapshot intervals

* make receiver history 4K too
This commit is contained in:
Raja Subramanian
2023-09-16 18:54:18 +05:30
committed by GitHub
parent 340906267f
commit 97048a923c
3 changed files with 352 additions and 249 deletions
+13 -138
View File
@@ -30,8 +30,6 @@ const (
cGapHistogramNumBins = 101
cNumSequenceNumbers = 65536
cFirstSnapshotID = 1
cSnInfoSize = 8192
cSnInfoMask = cSnInfoSize - 1
cFirstPacketTimeAdjustWindow = 2 * time.Minute
cFirstPacketTimeAdjustThreshold = 5 * time.Second
@@ -53,18 +51,6 @@ func RTPDriftToString(r *livekit.RTPDrift) string {
// -------------------------------------------------------
type intervalStats struct {
packets uint64
bytes uint64
headerBytes uint64
packetsPadding uint64
bytesPadding uint64
headerBytesPadding uint64
packetsLost uint64
packetsOutOfOrder uint64
frames uint32
}
type RTPDeltaInfo struct {
StartTime time.Time
Duration time.Duration
@@ -119,14 +105,6 @@ type snapshot struct {
maxJitter float64
}
type snInfo struct {
hdrSize uint16
pktSize uint16
isPaddingOnly bool
marker bool
isOutOfOrder bool
}
type RTCPSenderReportData struct {
RTPTimestamp uint32
RTPTimestampExt uint64
@@ -177,8 +155,6 @@ type rtpStatsBase struct {
jitter float64
maxJitter float64
snInfos [cSnInfoSize]snInfo
gapHistogram [cGapHistogramNumBins]uint32
nacks uint32
@@ -251,8 +227,6 @@ func (r *rtpStatsBase) seed(from *rtpStatsBase) bool {
r.jitter = from.jitter
r.maxJitter = from.maxJitter
r.snInfos = from.snInfos
r.gapHistogram = from.gapHistogram
r.nacks = from.nacks
@@ -309,14 +283,14 @@ func (r *rtpStatsBase) newSnapshotID(extStartSN uint64) uint32 {
id := r.nextSnapshotID
r.nextSnapshotID++
if cap(r.snapshots) < int(r.nextSnapshotID) {
snapshots := make([]snapshot, r.nextSnapshotID)
if cap(r.snapshots) < int(r.nextSnapshotID-cFirstSnapshotID) {
snapshots := make([]snapshot, r.nextSnapshotID-cFirstSnapshotID)
copy(snapshots, r.snapshots)
r.snapshots = snapshots
}
if r.initialized {
r.snapshots[id] = r.initSnapshot(time.Now(), extStartSN)
r.snapshots[id-cFirstSnapshotID] = r.initSnapshot(time.Now(), extStartSN)
}
return id
}
@@ -467,7 +441,8 @@ func (r *rtpStatsBase) UpdateRtt(rtt uint32) {
r.maxRtt = rtt
}
for _, s := range r.snapshots {
for i := uint32(0); i < r.nextSnapshotID-cFirstSnapshotID; i++ {
s := &r.snapshots[i]
if rtt > s.maxRtt {
s.maxRtt = rtt
}
@@ -545,7 +520,6 @@ func (r *rtpStatsBase) getTotalPacketsPrimary(extStartSN, extHighestSN uint64) u
func (r *rtpStatsBase) deltaInfo(snapshotID uint32, extStartSN uint64, extHighestSN uint64) *RTPDeltaInfo {
then, now := r.getAndResetSnapshot(snapshotID, extStartSN, extHighestSN)
if now == nil || then == nil {
return nil
}
@@ -772,107 +746,6 @@ func (r *rtpStatsBase) toProto(
return p
}
func (r *rtpStatsBase) getSnInfoOutOfOrderSlot(esn uint64, ehsn uint64) int {
offset := int64(ehsn - esn)
if offset >= cSnInfoSize || offset < 0 {
// too old OR too new (i. e. ahead of highest)
return -1
}
return int(esn & cSnInfoMask)
}
func (r *rtpStatsBase) setSnInfo(esn uint64, ehsn uint64, pktSize uint16, hdrSize uint16, payloadSize uint16, marker bool, isOutOfOrder bool) {
var slot int
if int64(esn-ehsn) < 0 {
slot = r.getSnInfoOutOfOrderSlot(esn, ehsn)
if slot < 0 {
return
}
} else {
slot = int(esn & cSnInfoMask)
}
snInfo := &r.snInfos[slot]
snInfo.pktSize = pktSize
snInfo.hdrSize = hdrSize
snInfo.isPaddingOnly = payloadSize == 0
snInfo.marker = marker
snInfo.isOutOfOrder = isOutOfOrder
}
func (r *rtpStatsBase) clearSnInfos(extStartInclusive uint64, extEndExclusive uint64) {
if extEndExclusive <= extStartInclusive {
return
}
for esn := extStartInclusive; esn != extEndExclusive; esn++ {
snInfo := &r.snInfos[esn&cSnInfoMask]
snInfo.pktSize = 0
snInfo.hdrSize = 0
snInfo.isPaddingOnly = false
snInfo.marker = false
}
}
func (r *rtpStatsBase) isSnInfoLost(esn uint64, ehsn uint64) bool {
slot := r.getSnInfoOutOfOrderSlot(esn, ehsn)
if slot < 0 {
return false
}
return r.snInfos[slot].pktSize == 0
}
func (r *rtpStatsBase) getIntervalStats(extStartInclusive uint64, extEndExclusive uint64, ehsn uint64) (intervalStats intervalStats) {
packetsNotFound := uint32(0)
processESN := func(esn uint64, ehsn uint64) {
slot := r.getSnInfoOutOfOrderSlot(esn, ehsn)
if slot < 0 {
packetsNotFound++
return
}
snInfo := &r.snInfos[slot]
switch {
case snInfo.pktSize == 0:
intervalStats.packetsLost++
case snInfo.isPaddingOnly:
intervalStats.packetsPadding++
intervalStats.bytesPadding += uint64(snInfo.pktSize)
intervalStats.headerBytesPadding += uint64(snInfo.hdrSize)
default:
intervalStats.packets++
intervalStats.bytes += uint64(snInfo.pktSize)
intervalStats.headerBytes += uint64(snInfo.hdrSize)
if snInfo.isOutOfOrder {
intervalStats.packetsOutOfOrder++
}
}
if snInfo.marker {
intervalStats.frames++
}
}
for esn := extStartInclusive; esn != extEndExclusive; esn++ {
processESN(esn, ehsn)
}
if packetsNotFound != 0 {
r.logger.Errorw(
"could not find some packets", nil,
"start", extStartInclusive,
"end", extEndExclusive,
"count", packetsNotFound,
"highestSN", ehsn,
)
}
return
}
func (r *rtpStatsBase) updateJitter(ets uint64, packetTime time.Time) float64 {
// Do not update jitter on multiple packets of same frame.
// All packets of a frame have the same time stamp.
@@ -896,7 +769,8 @@ func (r *rtpStatsBase) updateJitter(ets uint64, packetTime time.Time) float64 {
r.maxJitter = r.jitter
}
for _, s := range r.snapshots {
for i := uint32(0); i < r.nextSnapshotID-cFirstSnapshotID; i++ {
s := &r.snapshots[i]
if r.jitter > s.maxJitter {
s.maxJitter = r.jitter
}
@@ -914,15 +788,16 @@ func (r *rtpStatsBase) getAndResetSnapshot(snapshotID uint32, extStartSN uint64,
return nil, nil
}
then := r.snapshots[snapshotID]
idx := snapshotID - cFirstSnapshotID
then := r.snapshots[idx]
if !then.isValid {
then = r.initSnapshot(r.startTime, extStartSN)
r.snapshots[snapshotID] = then
r.snapshots[idx] = then
}
// snapshot now
now := r.getSnapshot(time.Now(), extHighestSN+1)
r.snapshots[snapshotID] = now
r.snapshots[idx] = now
return &then, &now
}
@@ -983,7 +858,7 @@ func (r *rtpStatsBase) updateGapHistogram(gap int) {
func (r *rtpStatsBase) initSnapshot(startTime time.Time, extStartSN uint64) snapshot {
return snapshot{
isValid: true,
startTime: time.Now(),
startTime: startTime,
extStartSN: extStartSN,
}
}
@@ -991,7 +866,7 @@ func (r *rtpStatsBase) initSnapshot(startTime time.Time, extStartSN uint64) snap
func (r *rtpStatsBase) getSnapshot(startTime time.Time, extStartSN uint64) snapshot {
return snapshot{
isValid: true,
startTime: time.Now(),
startTime: startTime,
extStartSN: extStartSN,
bytes: r.bytes,
headerBytes: r.headerBytes,