Skip to content

Commit

Permalink
Threads-RECV: Refine the stat for SNMP UDP recv/error
Browse files Browse the repository at this point in the history
1. Remove the delta of _srs_snmp_udp_stat.
2. Use _srs_pps_rloss for receive loss rate.
3. Use _srs_pps_sloss for send loss rate.
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent 85ea39d commit 1c6e941
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 63 deletions.
8 changes: 5 additions & 3 deletions trunk/src/app/srs_app_rtc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ extern SrsPps* _srs_pps_rnack2;
extern SrsPps* _srs_pps_rhnack;
extern SrsPps* _srs_pps_rmnack;

extern SrsPps* _srs_pps_rloss;
extern SrsPps* _srs_pps_sloss;

SrsRtcBlackhole::SrsRtcBlackhole()
{
blackhole = false;
Expand Down Expand Up @@ -697,9 +700,8 @@ srs_error_t SrsRtcServer::on_timer(srs_utime_t interval, srs_utime_t tick)
}

string loss_desc;
SrsSnmpUdpStat* s = srs_get_udp_snmp_stat();
if (s->rcv_buf_errors_delta || s->snd_buf_errors_delta) {
snprintf(buf, sizeof(buf), ", loss=(r:%d,s:%d)", s->rcv_buf_errors_delta, s->snd_buf_errors_delta);
if (_srs_pps_rloss->r10s() || _srs_pps_sloss->r10s()) {
snprintf(buf, sizeof(buf), ", loss=(r:%d,s:%d)", _srs_pps_rloss->r10s(), _srs_pps_sloss->r10s());
loss_desc = buf;
}

Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ srs_error_t SrsServer::setup_ticks()
return srs_error_wrap(err, "tick");
}

if ((err = timer_->tick(10, 9 * SRS_UTIME_SECONDS)) != srs_success) {
if ((err = timer_->tick(10, 5 * SRS_UTIME_SECONDS)) != srs_success) {
return srs_error_wrap(err, "tick");
}
}
Expand Down
96 changes: 41 additions & 55 deletions trunk/src/app/srs_app_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ using namespace std;
#include <srs_protocol_amf0.hpp>
#include <srs_kernel_utility.hpp>

#include <srs_protocol_kbps.hpp>

SrsPps* _srs_pps_rloss = new SrsPps();
SrsPps* _srs_pps_sloss = new SrsPps();

// the longest time to wait for a process to quit.
#define SRS_PROCESS_QUIT_TIMEOUT_MS 1000

Expand Down Expand Up @@ -862,9 +867,6 @@ SrsSnmpUdpStat::SrsSnmpUdpStat()
rcv_buf_errors = 0;
snd_buf_errors = 0;
in_csum_errors = 0;

rcv_buf_errors_delta = 0;
snd_buf_errors_delta = 0;
}

SrsSnmpUdpStat::~SrsSnmpUdpStat()
Expand All @@ -873,72 +875,56 @@ SrsSnmpUdpStat::~SrsSnmpUdpStat()

static SrsSnmpUdpStat _srs_snmp_udp_stat;

bool get_udp_snmp_statistic(SrsSnmpUdpStat& r)
void srs_update_udp_snmp_statistic()
{
SrsSnmpUdpStat& r = _srs_snmp_udp_stat;

#ifndef SRS_OSX
if (true) {
FILE* f = fopen("/proc/net/snmp", "r");
if (f == NULL) {
srs_warn("open proc network snmp failed, ignore");
return false;
}
FILE* f = fopen("/proc/net/snmp", "r");
if (f == NULL) {
return;
}

// ignore title.
static char buf[1024];
fgets(buf, sizeof(buf), f);
static char buf[1024];
while (fgets(buf, sizeof(buf), f)) {
// Ignore lines except UDP.
if (strncmp(buf, "Udp: ", 5) != 0) {
continue;
}

while (fgets(buf, sizeof(buf), f)) {
// udp stat title
if (strncmp(buf, "Udp: ", 5) == 0) {
// read tcp stat data
if (!fgets(buf, sizeof(buf), f)) {
break;
}
// parse tcp stat data
if (strncmp(buf, "Udp: ", 5) == 0) {
sscanf(buf + 5, "%llu %llu %llu %llu %llu %llu %llu\n",
&r.in_datagrams,
&r.no_ports,
&r.in_errors,
&r.out_datagrams,
&r.rcv_buf_errors,
&r.snd_buf_errors,
&r.in_csum_errors);
}
}
// Ignore UDP stat title.
// Udp: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors InCsumErrors
if (strncmp(buf, "Udp: InDatagrams", 16) == 0) {
continue;
}
fclose(f);

// Parse the UDP stat messages.
// Udp: 22000790151 77826210 229174183 24889592909 229174182 420017 1
sscanf(buf + 5, "%llu %llu %llu %llu %llu %llu %llu\n",
&r.in_datagrams,
&r.no_ports,
&r.in_errors,
&r.out_datagrams,
&r.rcv_buf_errors,
&r.snd_buf_errors,
&r.in_csum_errors);
break;
}
#endif
r.ok = true;
fclose(f);

return true;
// Update the pps for recv/send loss.
_srs_pps_rloss->update(r.rcv_buf_errors);
_srs_pps_sloss->update(r.snd_buf_errors);

r.ok = true;
#endif
}

SrsSnmpUdpStat* srs_get_udp_snmp_stat()
{
return &_srs_snmp_udp_stat;
}

void srs_update_udp_snmp_statistic()
{
SrsSnmpUdpStat r;
if (!get_udp_snmp_statistic(r)) {
return;
}

SrsSnmpUdpStat& o = _srs_snmp_udp_stat;
if (o.rcv_buf_errors > 0) {
r.rcv_buf_errors_delta = int(r.rcv_buf_errors - o.rcv_buf_errors);
}

if (o.snd_buf_errors > 0) {
r.snd_buf_errors_delta = int(r.snd_buf_errors - o.snd_buf_errors);
}

_srs_snmp_udp_stat = r;
}

SrsNetworkDevices::SrsNetworkDevices()
{
ok = false;
Expand Down
5 changes: 1 addition & 4 deletions trunk/src/app/srs_app_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,9 @@ class SrsSnmpUdpStat
public:
// Whether the data is ok.
bool ok;
// send and recv buffer error delta
int rcv_buf_errors_delta;
int snd_buf_errors_delta;

public:
// @see: cat /proc/uptimecat /proc/net/snmp|grep 'Udp:'
// @see: cat /proc/uptime && cat /proc/net/snmp|grep 'Udp:'
// @see: https://blog.packagecloud.io/eng/2017/02/06/monitoring-tuning-linux-networking-stack-sending-data/#procnetsnmp
// InDatagrams: incremented when recvmsg was used by a userland program to read datagram.
// also incremented when a UDP packet is encapsulated and sent back for processing.
Expand Down

0 comments on commit 1c6e941

Please sign in to comment.