Unverified 提交 56068f83 authored 作者: Vittorio Palmisano's avatar Vittorio Palmisano 提交者: GitHub

Rate calculator optimization (#538)

上级 75a0d637
...@@ -14,10 +14,14 @@ namespace RTC ...@@ -14,10 +14,14 @@ namespace RTC
public: public:
static constexpr size_t DefaultWindowSize{ 1000u }; static constexpr size_t DefaultWindowSize{ 1000u };
static constexpr float DefaultBpsScale{ 8000.0f }; static constexpr float DefaultBpsScale{ 8000.0f };
static constexpr uint16_t DefaultWindowItems{ 1000u };
public: public:
RateCalculator(size_t windowSize = DefaultWindowSize, float scale = DefaultBpsScale) RateCalculator(
: windowSize(windowSize), scale(scale) size_t windowSize = DefaultWindowSize,
float scale = DefaultBpsScale,
uint16_t windowItems = DefaultWindowItems)
: windowSize(windowSize), scale(scale), windowItems(windowItems)
{ {
Reset(); Reset();
} }
...@@ -38,9 +42,11 @@ namespace RTC ...@@ -38,9 +42,11 @@ namespace RTC
void RemoveOldData(uint64_t nowMs); void RemoveOldData(uint64_t nowMs);
void Reset(uint64_t nowMs) void Reset(uint64_t nowMs)
{ {
this->buffer.reset(new BufferItem[this->windowSize]); this->buffer.reset(new BufferItem[this->windowItems]);
this->oldestTime = nowMs - this->windowSize; this->newestTime = 0u;
this->oldestIndex = 0u; this->newestTimeIndex = -1;
this->oldestTimeIndex = 0u;
this->oldestIndex = -1;
this->totalCount = 0u; this->totalCount = 0u;
this->lastRate = 0u; this->lastRate = 0u;
this->lastTime = 0u; this->lastTime = 0u;
...@@ -50,6 +56,7 @@ namespace RTC ...@@ -50,6 +56,7 @@ namespace RTC
struct BufferItem struct BufferItem
{ {
size_t count{ 0u }; size_t count{ 0u };
uint64_t time{ 0u };
}; };
private: private:
...@@ -57,12 +64,18 @@ namespace RTC ...@@ -57,12 +64,18 @@ namespace RTC
size_t windowSize{ DefaultWindowSize }; size_t windowSize{ DefaultWindowSize };
// Scale in which the rate is represented. // Scale in which the rate is represented.
float scale{ DefaultBpsScale }; float scale{ DefaultBpsScale };
// Window Size (number of items).
uint16_t windowItems{ DefaultWindowItems };
// Buffer to keep data. // Buffer to keep data.
std::unique_ptr<BufferItem[]> buffer; std::unique_ptr<BufferItem[]> buffer;
// Time (in milliseconds) for last item in the time window.
uint64_t newestTime{ 0u };
// Index for the last item in the time window.
int32_t newestTimeIndex{ -1 };
// Time (in milliseconds) for oldest item in the time window. // Time (in milliseconds) for oldest item in the time window.
uint64_t oldestTime{ 0u }; uint64_t oldestTimeIndex{ 0u };
// Index for the oldest item in the time window. // Index for the oldest item in the time window.
uint32_t oldestIndex{ 0u }; int32_t oldestIndex{ -1 };
// Total count in the time window. // Total count in the time window.
size_t totalCount{ 0u }; size_t totalCount{ 0u };
// Total bytes transmitted. // Total bytes transmitted.
......
...@@ -12,7 +12,7 @@ namespace RTC ...@@ -12,7 +12,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
// Ignore too old data. Should never happen. // Ignore too old data. Should never happen.
if (nowMs < this->oldestTime) if (nowMs < this->oldestTimeIndex)
return; return;
// Increase bytes. // Increase bytes.
...@@ -20,14 +20,40 @@ namespace RTC ...@@ -20,14 +20,40 @@ namespace RTC
RemoveOldData(nowMs); RemoveOldData(nowMs);
// Set data in the index before the oldest index. // Increase the index.
uint32_t offset = this->windowSize - 1; if (++this->newestTimeIndex >= this->windowItems)
uint32_t index = this->oldestIndex + offset; this->newestTimeIndex = 0;
if (index >= this->windowSize) // Newest index overlaps with the oldest one, remove it.
index -= this->windowSize; if (this->newestTimeIndex == this->oldestIndex && this->oldestIndex != -1)
{
MS_WARN_TAG(
info,
"calculation buffer full, windowSize:%" PRIu64 " ms windowItems:%" PRIu16,
this->windowSize,
this->windowItems);
BufferItem& oldestItem = buffer[this->oldestIndex];
this->totalCount -= oldestItem.count;
oldestItem.count = 0u;
oldestItem.time = 0u;
if (++this->oldestIndex >= this->windowItems)
this->oldestIndex = 0;
}
// Update the newest item.
BufferItem& item = buffer[this->newestTimeIndex];
item.count += size;
item.time = nowMs;
this->newestTime = nowMs;
// Set the oldest item index and time, if not set.
if (this->oldestIndex < 0)
{
this->oldestIndex = this->newestTimeIndex;
this->oldestTimeIndex = nowMs;
}
this->buffer[index].count += size;
this->totalCount += size; this->totalCount += size;
// Reset lastRate and lastTime so GetRate() will calculate rate again even // Reset lastRate and lastTime so GetRate() will calculate rate again even
...@@ -57,45 +83,37 @@ namespace RTC ...@@ -57,45 +83,37 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
uint64_t newOldestTime = nowMs - this->windowSize; // No item set
if (this->newestTimeIndex < 0 || this->oldestIndex < 0)
// Should never happen.
if (newOldestTime < this->oldestTime)
{
MS_ERROR(
"current time [%" PRIu64 "] is older than a previous [%" PRIu64 "]",
newOldestTime,
this->oldestTime);
return; return;
}
// We are in the same time unit (ms) as the last entry. uint64_t newoldestTimeIndex = nowMs - this->windowSize;
if (newOldestTime == this->oldestTime)
// Oldest item already removed.
if (newoldestTimeIndex <= this->oldestTimeIndex)
return; return;
// A whole window size time has elapsed since last entry. Reset the buffer. // A whole window size time has elapsed since last entry. Reset the buffer.
if (newOldestTime > this->oldestTime + this->windowSize) if (newoldestTimeIndex > this->newestTime)
{ {
Reset(nowMs); Reset(nowMs);
return; return;
} }
while (this->oldestTime < newOldestTime) while (this->oldestTimeIndex < newoldestTimeIndex)
{ {
const BufferItem& oldestItem = buffer[this->oldestIndex]; BufferItem& oldestItem = buffer[this->oldestIndex];
this->totalCount -= oldestItem.count; this->totalCount -= oldestItem.count;
this->buffer[this->oldestIndex] = BufferItem(); oldestItem.count = 0u;
oldestItem.time = 0u;
if (++this->oldestIndex >= this->windowSize) if (++this->oldestIndex >= this->windowItems)
this->oldestIndex = 0; this->oldestIndex = 0;
++this->oldestTime; const BufferItem& newOldestItem = buffer[this->oldestIndex];
this->oldestTimeIndex = newOldestItem.time;
} }
this->oldestTime = newOldestTime;
} }
void RtpDataCounter::Update(RTC::RtpPacket* packet) void RtpDataCounter::Update(RTC::RtpPacket* packet)
......
...@@ -76,6 +76,29 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]") ...@@ -76,6 +76,29 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
validate(rate, nowMs, input); validate(rate, nowMs, input);
REQUIRE(rate.GetRate(nowMs + 3000) == 0); REQUIRE(rate.GetRate(nowMs + 3001) == 0);
}
SECTION("wrap")
{
RateCalculator rate(1000, 8000, 5);
// clang-format off
std::vector<data> input =
{
{ 1000, 1, 1*8 },
{ 1001, 1, 1*8 + 1*8 },
{ 1002, 1, 1*8 + 2*8 },
{ 1003, 1, 1*8 + 3*8 },
{ 1004, 1, 1*8 + 4*8 },
{ 1005, 1, 1*8 + (5-1)*8 }, // starts wrap here
{ 1006, 1, 1*8 + (6-2)*8 },
{ 1007, 1, 1*8 + (7-3)*8 },
{ 1008, 1, 1*8 + (8-4)*8 },
{ 1009, 1, 1*8 + (9-5)*8 },
};
// clang-format on
validate(rate, nowMs, input);
} }
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论