Skip to content

Commit

Permalink
[core] Changing stat var name and put back partial inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
npanhaleux committed Aug 22, 2024
1 parent 1d3e608 commit 895b50d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 73 deletions.
24 changes: 11 additions & 13 deletions srtcore/buffer_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ CSndRateEstimator::CSndRateEstimator(const time_point& tsNow)
: m_tsFirstSampleTime(tsNow)
, m_iCurSampleIdx(0)
, m_iRateBps(0)
, m_Samples(NUM_PERIODS)
, m_iFirstSampleIdx(0)
, m_Samples(NUM_PERIODS)
{

}
Expand Down Expand Up @@ -275,11 +275,9 @@ int CSndRateEstimator::incSampleIdx(int val, int inc) const
}

CMovingRateEstimator::CMovingRateEstimator()
: m_tsFirstSampleTime(sync::steady_clock::now())
, m_iFirstSampleIdx(0)
, m_iCurSampleIdx(0)
, m_iRateBps(0)
, m_Samples(NUM_PERIODS)
: CSndRateEstimator(sync::steady_clock::now())
, m_MovingSamples(NUM_PERIODS)
, lastSlotTimestamp(sync::steady_clock::now())
{
resetRate(0, NUM_PERIODS);
}
Expand All @@ -291,8 +289,8 @@ void CMovingRateEstimator::addSample(int pkts, double bytes)

if (iSampleDeltaIdx == 0)
{
m_Samples[m_iCurSampleIdx].m_iBytesCount += bytes;
m_Samples[m_iCurSampleIdx].m_iPktsCount += pkts;
m_MovingSamples[m_iCurSampleIdx].m_iBytesCount += bytes;
m_MovingSamples[m_iCurSampleIdx].m_iPktsCount += pkts;
}
else
{
Expand All @@ -305,9 +303,9 @@ void CMovingRateEstimator::addSample(int pkts, double bytes)
resetRate(0, loopbackDiff);
}

m_iCurSampleIdx = ((m_iCurSampleIdx + iSampleDeltaIdx) % NUM_PERIODS);
m_Samples[m_iCurSampleIdx].m_iBytesCount = bytes;
m_Samples[m_iCurSampleIdx].m_iPktsCount = pkts;
m_iCurSampleIdx = ((m_iCurSampleIdx + iSampleDeltaIdx) % NUM_PERIODS);
m_MovingSamples[m_iCurSampleIdx].m_iBytesCount = bytes;
m_MovingSamples[m_iCurSampleIdx].m_iPktsCount = pkts;

lastSlotTimestamp = now;
}
Expand All @@ -318,7 +316,7 @@ void CMovingRateEstimator::addSample(int pkts, double bytes)
void CMovingRateEstimator::resetRate(int from, int to)
{
for (int i = max(0, from); i < min(int(NUM_PERIODS), to); i++)
m_Samples[i].reset();
m_MovingSamples[i].reset();
}

void CMovingRateEstimator::computeAverageValue()
Expand All @@ -329,7 +327,7 @@ void CMovingRateEstimator::computeAverageValue()
m_iRateBps = 0;

for (int i = 0; i < NUM_PERIODS; i++)
m_iRateBps += (m_Samples[i].m_iBytesCount + (CPacket::HDR_SIZE * m_Samples[i].m_iPktsCount));
m_iRateBps += (m_MovingSamples[i].m_iBytesCount + (CPacket::HDR_SIZE * m_MovingSamples[i].m_iPktsCount));

if (isFirstPeriod)
m_iRateBps = m_iRateBps * 1000 / startDelta;
Expand Down
61 changes: 9 additions & 52 deletions srtcore/buffer_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,16 @@ class CSndRateEstimator
bool empty() const { return m_iPktsCount == 0; }
};

srt::FixedArray<Sample> m_Samples; // Table of stored data

private:
static const int NUM_PERIODS = 10;
static const int SAMPLE_DURATION_MS = 100; // 100 ms
int m_iFirstSampleIdx; //< Index of the first sample.
static const int NUM_PERIODS = 10;
static const int SAMPLE_DURATION_MS = 100; // 100 ms
int m_iFirstSampleIdx; //< Index of the first sample.
srt::FixedArray<Sample> m_Samples; // Table of stored data

int incSampleIdx(int val, int inc = 1) const;
};

class CMovingRateEstimator
class CMovingRateEstimator : CSndRateEstimator
{
typedef sync::steady_clock::time_point time_point;

Expand All @@ -221,52 +220,10 @@ class CMovingRateEstimator
private:
// We would like responsiveness (accuracy) of rate estimation higher than 100 ms
// (ideally around 50 ms) for network adaptive algorithms.
const int NUM_PERIODS = 100; // To get 1s of values
const int SAMPLE_DURATION_MS = 10; // 10 ms
time_point m_tsFirstSampleTime; //< Start time of the first sample.
time_point lastSlotTimestamp; // Used to compute the delta between 2 calls
int m_iFirstSampleIdx; //< Index of the first sample.
int m_iCurSampleIdx; //< Index of the current sample being collected.
int m_iRateBps; //< Rate in Bytes/sec.

struct Sample
{
int m_iPktsCount; // number of payload packets
int m_iBytesCount; // number of payload bytes

void reset()
{
m_iPktsCount = 0;
m_iBytesCount = 0;
}

Sample()
: m_iPktsCount(0)
, m_iBytesCount(0)
{
}

Sample(int iPkts, int iBytes)
: m_iPktsCount(iPkts)
, m_iBytesCount(iBytes)
{
}

Sample operator+(const Sample& other)
{
return Sample(m_iPktsCount + other.m_iPktsCount, m_iBytesCount + other.m_iBytesCount);
}

Sample& operator+=(const Sample& other)
{
*this = *this + other;
return *this;
}

bool empty() const { return m_iPktsCount == 0; }
};

srt::FixedArray<Sample> m_Samples; // Table of stored data
const int NUM_PERIODS = 100; // To get 1s of values
const int SAMPLE_DURATION_MS = 10; // 10 ms
srt::FixedArray<Sample> m_MovingSamples; // Table of stored data
time_point lastSlotTimestamp; //< Start time of the first sample.

/// This method will compute the average value based on all table's measures and the period
/// (NUM_PERIODS*SAMPLE_DURATION_MS)
Expand Down
16 changes: 8 additions & 8 deletions srtcore/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ struct Sender
Metric<Packets> recvdAck; // The number of ACK packets received by the sender.
Metric<Packets> recvdNak; // The number of ACK packets received by the sender.

CMovingRateEstimator mobileRateEstimator; // The average Mbps over last second
CMovingRateEstimator mavgRateEstimator; // The average Mbps over last second

void reset()
{
Expand All @@ -171,11 +171,11 @@ struct Sender
sentFilterExtra.resetTrace();
}

void updateRate(int pkts, double bytes) { mobileRateEstimator.addSample(pkts, bytes); }
void updateRate(int pkts, double bytes) { mavgRateEstimator.addSample(pkts, bytes); }

void resetRate() { mobileRateEstimator.resetRate(); }
void resetRate() { mavgRateEstimator.resetRate(); }

int getAverageValue() { return mobileRateEstimator.getRate(); }
int getAverageValue() { return mavgRateEstimator.getRate(); }
};

/// Receiver-side statistics.
Expand All @@ -196,7 +196,7 @@ struct Receiver
Metric<Packets> sentAck; // The number of ACK packets sent by the receiver.
Metric<Packets> sentNak; // The number of NACK packets sent by the receiver.

CMovingRateEstimator mobileRateEstimator; // The average Mbps over last second
CMovingRateEstimator mavgRateEstimator; // The average Mbps over last second

void reset()
{
Expand Down Expand Up @@ -230,11 +230,11 @@ struct Receiver
sentNak.resetTrace();
}

void updateRate(int pkts, double bytes) { mobileRateEstimator.addSample(pkts, bytes); }
void updateRate(int pkts, double bytes) { mavgRateEstimator.addSample(pkts, bytes); }

void resetRate() { mobileRateEstimator.resetRate(); }
void resetRate() { mavgRateEstimator.resetRate(); }

int getAverageValue() { return mobileRateEstimator.getRate(); }
int getAverageValue() { return mavgRateEstimator.getRate(); }
};

} // namespace stats
Expand Down

0 comments on commit 895b50d

Please sign in to comment.