Remove alloc in packet forwarding path. (#3305)

* Remove alloc in packet forwarding path.

Unlikely logger creation was doing allocs. Replace it with a function
like in rtpstats_receiver.go so that allocations do not happen
unnecessarily.

* variable rename

* one more place
This commit is contained in:
Raja Subramanian
2025-01-06 11:48:27 +05:30
committed by GitHub
parent c792d15244
commit bfbc4fa81f
2 changed files with 38 additions and 34 deletions
-1
View File
@@ -322,5 +322,4 @@ func BenchmarkMemcpu(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
copy(buf2, buf) copy(buf2, buf)
} }
} }
+38 -33
View File
@@ -26,6 +26,7 @@ import (
"github.com/livekit/mediatransportutil" "github.com/livekit/mediatransportutil"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils/mono" "github.com/livekit/protocol/utils/mono"
) )
@@ -363,18 +364,20 @@ func (r *RTPStatsSender) Update(
pktSize := uint64(hdrSize + payloadSize + paddingSize) pktSize := uint64(hdrSize + payloadSize + paddingSize)
isDuplicate := false isDuplicate := false
gapSN := int64(extSequenceNumber - r.extHighestSN) gapSN := int64(extSequenceNumber - r.extHighestSN)
logger := r.logger.WithUnlikelyValues( ulgr := func() logger.UnlikelyLogger {
"currSN", extSequenceNumber, return r.logger.WithUnlikelyValues(
"gapSN", gapSN, "currSN", extSequenceNumber,
"currTS", extTimestamp, "gapSN", gapSN,
"gapTS", int64(extTimestamp-r.extHighestTS), "currTS", extTimestamp,
"packetTime", packetTime, "gapTS", int64(extTimestamp-r.extHighestTS),
"marker", marker, "packetTime", packetTime,
"hdrSize", hdrSize, "marker", marker,
"payloadSize", payloadSize, "hdrSize", hdrSize,
"paddingSize", paddingSize, "payloadSize", payloadSize,
"rtpStats", lockedRTPStatsSenderLogEncoder{r}, "paddingSize", paddingSize,
) "rtpStats", lockedRTPStatsSenderLogEncoder{r},
)
}
if gapSN <= 0 { // duplicate OR out-of-order if gapSN <= 0 { // duplicate OR out-of-order
if payloadSize == 0 && extSequenceNumber < r.extStartSN { if payloadSize == 0 && extSequenceNumber < r.extStartSN {
// do not start on a padding only packet // do not start on a padding only packet
@@ -401,7 +404,7 @@ func (r *RTPStatsSender) Update(
} }
} }
logger.Infow( ulgr().Infow(
"adjusting start sequence number", "adjusting start sequence number",
"snAfter", extSequenceNumber, "snAfter", extSequenceNumber,
"tsAfter", extTimestamp, "tsAfter", extTimestamp,
@@ -426,7 +429,7 @@ func (r *RTPStatsSender) Update(
if !isDuplicate && -gapSN >= cSequenceNumberLargeJumpThreshold { if !isDuplicate && -gapSN >= cSequenceNumberLargeJumpThreshold {
r.largeJumpNegativeCount++ r.largeJumpNegativeCount++
if (r.largeJumpNegativeCount-1)%100 == 0 { if (r.largeJumpNegativeCount-1)%100 == 0 {
logger.Warnw( ulgr().Warnw(
"large sequence number gap negative", nil, "large sequence number gap negative", nil,
"count", r.largeJumpNegativeCount, "count", r.largeJumpNegativeCount,
) )
@@ -436,7 +439,7 @@ func (r *RTPStatsSender) Update(
if gapSN >= cSequenceNumberLargeJumpThreshold { if gapSN >= cSequenceNumberLargeJumpThreshold {
r.largeJumpCount++ r.largeJumpCount++
if (r.largeJumpCount-1)%100 == 0 { if (r.largeJumpCount-1)%100 == 0 {
logger.Warnw( ulgr().Warnw(
"large sequence number gap", nil, "large sequence number gap", nil,
"count", r.largeJumpCount, "count", r.largeJumpCount,
) )
@@ -446,7 +449,7 @@ func (r *RTPStatsSender) Update(
if extTimestamp < r.extHighestTS { if extTimestamp < r.extHighestTS {
r.timeReversedCount++ r.timeReversedCount++
if (r.timeReversedCount-1)%100 == 0 { if (r.timeReversedCount-1)%100 == 0 {
logger.Warnw( ulgr().Warnw(
"time reversed", nil, "time reversed", nil,
"count", r.timeReversedCount, "count", r.timeReversedCount,
) )
@@ -466,7 +469,7 @@ func (r *RTPStatsSender) Update(
} }
if extTimestamp < r.extStartTS { if extTimestamp < r.extStartTS {
logger.Infow( ulgr().Infow(
"adjusting start timestamp", "adjusting start timestamp",
"snAfter", extSequenceNumber, "snAfter", extSequenceNumber,
"tsAfter", extTimestamp, "tsAfter", extTimestamp,
@@ -732,20 +735,22 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, publisherSRData *livek
Octets: octetCount, Octets: octetCount,
} }
logger := r.logger.WithUnlikelyValues( ulgr := func() logger.UnlikelyLogger {
"curr", WrappedRTCPSenderReportStateLogger{srData}, return r.logger.WithUnlikelyValues(
"feed", WrappedRTCPSenderReportStateLogger{publisherSRData}, "curr", WrappedRTCPSenderReportStateLogger{srData},
"tsOffset", tsOffset, "feed", WrappedRTCPSenderReportStateLogger{publisherSRData},
"timeNow", time.Now(), "tsOffset", tsOffset,
"reportTime", time.Unix(0, reportTime), "timeNow", time.Now(),
"reportTimeAdjusted", time.Unix(0, reportTimeAdjusted), "reportTime", time.Unix(0, reportTime),
"timeSinceHighest", time.Since(time.Unix(0, r.highestTime)), "reportTimeAdjusted", time.Unix(0, reportTimeAdjusted),
"timeSinceFirst", time.Since(time.Unix(0, r.firstTime)), "timeSinceHighest", time.Since(time.Unix(0, r.highestTime)),
"timeSincePublisherSRAdjusted", time.Since(time.Unix(0, publisherSRData.AtAdjusted)), "timeSinceFirst", time.Since(time.Unix(0, r.firstTime)),
"timeSincePublisherSR", time.Since(time.Unix(0, publisherSRData.At)), "timeSincePublisherSRAdjusted", time.Since(time.Unix(0, publisherSRData.AtAdjusted)),
"nowRTPExt", nowRTPExt, "timeSincePublisherSR", time.Since(time.Unix(0, publisherSRData.At)),
"rtpStats", lockedRTPStatsSenderLogEncoder{r}, "nowRTPExt", nowRTPExt,
) "rtpStats", lockedRTPStatsSenderLogEncoder{r},
)
}
if r.srNewest != nil && nowRTPExt >= r.srNewest.RtpTimestampExt { if r.srNewest != nil && nowRTPExt >= r.srNewest.RtpTimestampExt {
timeSinceLastReport := nowNTP.Time().Sub(mediatransportutil.NtpTime(r.srNewest.NtpTimestamp).Time()) timeSinceLastReport := nowNTP.Time().Sub(mediatransportutil.NtpTime(r.srNewest.NtpTimestamp).Time())
@@ -754,7 +759,7 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, publisherSRData *livek
if timeSinceLastReport.Seconds() > 0.2 && math.Abs(float64(r.params.ClockRate)-windowClockRate) > 0.2*float64(r.params.ClockRate) { if timeSinceLastReport.Seconds() > 0.2 && math.Abs(float64(r.params.ClockRate)-windowClockRate) > 0.2*float64(r.params.ClockRate) {
r.clockSkewCount++ r.clockSkewCount++
if (r.clockSkewCount-1)%100 == 0 { if (r.clockSkewCount-1)%100 == 0 {
logger.Infow( ulgr().Infow(
"sending sender report, clock skew", "sending sender report, clock skew",
"timeSinceLastReport", timeSinceLastReport, "timeSinceLastReport", timeSinceLastReport,
"rtpDiffSinceLastReport", rtpDiffSinceLastReport, "rtpDiffSinceLastReport", rtpDiffSinceLastReport,
@@ -768,7 +773,7 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, publisherSRData *livek
if r.srNewest != nil && nowRTPExt < r.srNewest.RtpTimestampExt { if r.srNewest != nil && nowRTPExt < r.srNewest.RtpTimestampExt {
// If report being generated is behind the last report, skip it. // If report being generated is behind the last report, skip it.
// Should not happen. // Should not happen.
logger.Infow("sending sender report, out-of-order, skipping") ulgr().Infow("sending sender report, out-of-order, skipping")
return nil return nil
} }