1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

fix #143, fix retrieve sys stat bug for all linux. 0.9.185.

This commit is contained in:
winlin 2014-08-03 10:42:01 +08:00
parent 7b24788445
commit d5ba529d32
3 changed files with 81 additions and 81 deletions

View file

@ -207,6 +207,7 @@ Supported operating systems and hardware:
* 2013-10-17, Created.<br/> * 2013-10-17, Created.<br/>
## History ## History
* v1.0, 2014-08-02, fix [#143](https://github.com/winlinvip/simple-rtmp-server/issues/143), fix retrieve sys stat bug for all linux. 0.9.185.
* v1.0, 2014-08-02, fix [#138](https://github.com/winlinvip/simple-rtmp-server/issues/138), fix http hooks bug, regression bug. 0.9.184. * v1.0, 2014-08-02, fix [#138](https://github.com/winlinvip/simple-rtmp-server/issues/138), fix http hooks bug, regression bug. 0.9.184.
* v1.0, 2014-08-02, fix [#142](https://github.com/winlinvip/simple-rtmp-server/issues/142), fix tcp stat slow bug, use /proc/net/sockstat instead, refer to 'ss -s'. 0.9.183. * v1.0, 2014-08-02, fix [#142](https://github.com/winlinvip/simple-rtmp-server/issues/142), fix tcp stat slow bug, use /proc/net/sockstat instead, refer to 'ss -s'. 0.9.183.
* v1.0, 2014-07-31, fix [#141](https://github.com/winlinvip/simple-rtmp-server/issues/141), support tun0(vpn network device) ip retrieve. 0.9.179. * v1.0, 2014-07-31, fix [#141](https://github.com/winlinvip/simple-rtmp-server/issues/141), support tun0(vpn network device) ip retrieve. 0.9.179.

View file

@ -228,29 +228,33 @@ bool get_proc_system_stat(SrsProcSystemStat& r)
return false; return false;
} }
r.ok = false;
static char buf[1024]; static char buf[1024];
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
if (strncmp(buf, "cpu ", 4) != 0) { if (strncmp(buf, "cpu ", 4) != 0) {
continue; continue;
} }
int ret = sscanf(buf, "cpu %llu %llu %llu %llu %llu " // @see: read_stat_cpu() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L88
"%llu %llu %llu %llu\n", // @remark, ignore the filed 10 cpu_guest_nice
&r.user, &r.nice, &r.sys, &r.idle, &r.iowait, sscanf(buf + 5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
&r.irq, &r.softirq, &r.steal, &r.guest); &r.user,
srs_assert(ret == 9); &r.nice,
&r.sys,
// matched ok. &r.idle,
r.ok = true; &r.iowait,
&r.irq,
&r.softirq,
&r.steal,
&r.guest);
break; break;
} }
fclose(f); fclose(f);
return r.ok; r.ok = true;
return true;
} }
bool get_proc_self_stat(SrsProcSelfStat& r) bool get_proc_self_stat(SrsProcSelfStat& r)
@ -261,7 +265,7 @@ bool get_proc_self_stat(SrsProcSelfStat& r)
return false; return false;
} }
int ret = fscanf(f, "%d %32s %c %d %d %d %d " fscanf(f, "%d %32s %c %d %d %d %d "
"%d %u %lu %lu %lu %lu " "%d %u %lu %lu %lu %lu "
"%lu %lu %ld %ld %ld %ld " "%lu %lu %ld %ld %ld %ld "
"%ld %ld %llu %lu %ld " "%ld %ld %llu %lu %ld "
@ -282,11 +286,9 @@ bool get_proc_self_stat(SrsProcSelfStat& r)
fclose(f); fclose(f);
if (ret >= 0) {
r.ok = true; r.ok = true;
}
return r.ok; return true;
} }
void srs_update_proc_stat() void srs_update_proc_stat()
@ -386,19 +388,15 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
return false; return false;
} }
r.ok = false;
r.sample_time = srs_get_system_time_ms(); r.sample_time = srs_get_system_time_ms();
static char buf[1024]; static char buf[1024];
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
unsigned long value = 0; // @see: read_vmstat_paging() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L495
int ret = sscanf(buf, "%*s %lu\n", &value);
srs_assert(ret == 1);
if (strncmp(buf, "pgpgin ", 7) == 0) { if (strncmp(buf, "pgpgin ", 7) == 0) {
r.pgpgin = value; sscanf(buf + 7, "%lu\n", &r.pgpgin);
} else if (strncmp(buf, "pgpgout ", 8) == 0) { } else if (strncmp(buf, "pgpgout ", 8) == 0) {
r.pgpgout = value; sscanf(buf + 8, "%lu\n", &r.pgpgout);
} }
} }
@ -411,13 +409,12 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
bool srs_get_disk_diskstats_stat(SrsDiskStat& r) bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
{ {
r.ok = false; r.ok = true;
r.sample_time = srs_get_system_time_ms(); r.sample_time = srs_get_system_time_ms();
// if disabled, ignore all devices. // if disabled, ignore all devices.
SrsConfDirective* conf = _srs_config->get_stats_disk_device(); SrsConfDirective* conf = _srs_config->get_stats_disk_device();
if (conf == NULL) { if (conf == NULL) {
r.ok = true;
return true; return true;
} }
@ -444,12 +441,22 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
unsigned int ticks = 0; unsigned int ticks = 0;
unsigned int aveq = 0; unsigned int aveq = 0;
memset(name, sizeof(name), 0); memset(name, sizeof(name), 0);
int ret = sscanf(buf,
"%4d %4d %31s %u %u %llu %u %u %u %llu %u %u %u %u", sscanf(buf, "%4d %4d %31s %u %u %llu %u %u %u %llu %u %u %u %u",
&major, &minor, name, &rd_ios, &rd_merges, &major,
&rd_sectors, &rd_ticks, &wr_ios, &wr_merges, &minor,
&wr_sectors, &wr_ticks, &nb_current, &ticks, &aveq); name,
srs_assert(ret == 14); &rd_ios,
&rd_merges,
&rd_sectors,
&rd_ticks,
&wr_ios,
&wr_merges,
&wr_sectors,
&wr_ticks,
&nb_current,
&ticks,
&aveq);
for (int i = 0; i < (int)conf->args.size(); i++) { for (int i = 0; i < (int)conf->args.size(); i++) {
string name_ok = conf->args.at(i); string name_ok = conf->args.at(i);
@ -524,7 +531,7 @@ void srs_update_disk_stat()
&& cpuinfo->ok && cpuinfo->nb_processors > 0 && cpuinfo->ok && cpuinfo->nb_processors > 0
&& o.ticks < r.ticks && o.ticks < r.ticks
) { ) {
// @see: print_partition_stats() of iostat.c // @see: write_ext_stat() from https://github.com/sysstat/sysstat/blob/master/iostat.c#L979
// TODO: FIXME: the USER_HZ assert to 100, so the total_delta ticks *10 is ms. // TODO: FIXME: the USER_HZ assert to 100, so the total_delta ticks *10 is ms.
double delta_ms = r.cpu.total_delta * 10 / cpuinfo->nb_processors; double delta_ms = r.cpu.total_delta * 10 / cpuinfo->nb_processors;
unsigned int ticks = r.ticks - o.ticks; unsigned int ticks = r.ticks - o.ticks;
@ -572,26 +579,22 @@ void srs_update_meminfo()
} }
SrsMemInfo& r = _srs_system_meminfo; SrsMemInfo& r = _srs_system_meminfo;
r.ok = false;
static char buf[1024]; static char buf[1024];
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
static unsigned long value; // @see: read_meminfo() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L227
int ret = sscanf(buf, "%*s %lu", &value);
srs_assert(ret == 1);
if (strncmp(buf, "MemTotal:", 9) == 0) { if (strncmp(buf, "MemTotal:", 9) == 0) {
r.MemTotal = value; sscanf(buf + 9, "%lu", &r.MemTotal);
} else if (strncmp(buf, "MemFree:", 8) == 0) { } else if (strncmp(buf, "MemFree:", 8) == 0) {
r.MemFree = value; sscanf(buf + 8, "%lu", &r.MemFree);
} else if (strncmp(buf, "Buffers:", 8) == 0) { } else if (strncmp(buf, "Buffers:", 8) == 0) {
r.Buffers = value; sscanf(buf + 8, "%lu", &r.Buffers);
} else if (strncmp(buf, "Cached:", 7) == 0) { } else if (strncmp(buf, "Cached:", 7) == 0) {
r.Cached = value; sscanf(buf + 7, "%lu", &r.Cached);
} else if (strncmp(buf, "SwapTotal:", 10) == 0) { } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
r.SwapTotal = value; sscanf(buf + 10, "%lu", &r.SwapTotal);
} else if (strncmp(buf, "SwapFree:", 9) == 0) { } else if (strncmp(buf, "SwapFree:", 9) == 0) {
r.SwapFree = value; sscanf(buf + 9, "%lu", &r.SwapFree);
} }
} }
@ -602,13 +605,14 @@ void srs_update_meminfo()
r.RealInUse = r.MemActive - r.Buffers - r.Cached; r.RealInUse = r.MemActive - r.Buffers - r.Cached;
r.NotInUse = r.MemTotal - r.RealInUse; r.NotInUse = r.MemTotal - r.RealInUse;
r.ok = true;
if (r.MemTotal > 0) { if (r.MemTotal > 0) {
r.percent_ram = (float)(r.RealInUse / (double)r.MemTotal); r.percent_ram = (float)(r.RealInUse / (double)r.MemTotal);
} }
if (r.SwapTotal > 0) { if (r.SwapTotal > 0) {
r.percent_swap = (float)((r.SwapTotal - r.SwapFree) / (double)r.SwapTotal); r.percent_swap = (float)((r.SwapTotal - r.SwapFree) / (double)r.SwapTotal);
} }
r.ok = true;
} }
SrsCpuInfo::SrsCpuInfo() SrsCpuInfo::SrsCpuInfo()
@ -659,7 +663,6 @@ SrsPlatformInfo* srs_get_platform_info()
void srs_update_platform_info() void srs_update_platform_info()
{ {
SrsPlatformInfo& r = _srs_system_platform_info; SrsPlatformInfo& r = _srs_system_platform_info;
r.ok = true;
r.srs_startup_time = srs_get_system_startup_time_ms(); r.srs_startup_time = srs_get_system_startup_time_ms();
@ -670,13 +673,9 @@ void srs_update_platform_info()
return; return;
} }
int ret = fscanf(f, "%lf %lf\n", &r.os_uptime, &r.os_ilde_time); fscanf(f, "%lf %lf\n", &r.os_uptime, &r.os_ilde_time);
fclose(f); fclose(f);
if (ret < 0) {
r.ok = false;
}
} }
if (true) { if (true) {
@ -686,15 +685,17 @@ void srs_update_platform_info()
return; return;
} }
int ret = fscanf(f, "%lf %lf %lf\n", // @see: read_loadavg() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L402
&r.load_one_minutes, &r.load_five_minutes, &r.load_fifteen_minutes); // @remark, we use our algorithm, not sysstat.
fscanf(f, "%lf %lf %lf\n",
&r.load_one_minutes,
&r.load_five_minutes,
&r.load_fifteen_minutes);
fclose(f); fclose(f);
}
if (ret < 0) { r.ok = true;
r.ok = false;
}
}
} }
SrsNetworkDevices::SrsNetworkDevices() SrsNetworkDevices::SrsNetworkDevices()
@ -752,24 +753,23 @@ void srs_update_network_devices()
fgets(buf, sizeof(buf), f); fgets(buf, sizeof(buf), f);
for (int i = 0; i < MAX_NETWORK_DEVICES_COUNT; i++) { for (int i = 0; i < MAX_NETWORK_DEVICES_COUNT; i++) {
SrsNetworkDevices& r = _srs_system_network_devices[i]; if (!fgets(buf, sizeof(buf), f)) {
r.ok = false; break;
r.sample_time = 0; }
int ret = fscanf(f, "%6[^:]:%llu %lu %lu %lu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu %lu %lu\n", SrsNetworkDevices& r = _srs_system_network_devices[i];
// @see: read_net_dev() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L786
// @remark, we use our algorithm, not sysstat.
sscanf(buf, "%6[^:]:%llu %lu %lu %lu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu %lu %lu\n",
r.name, &r.rbytes, &r.rpackets, &r.rerrs, &r.rdrop, &r.rfifo, &r.rframe, &r.rcompressed, &r.rmulticast, r.name, &r.rbytes, &r.rpackets, &r.rerrs, &r.rdrop, &r.rfifo, &r.rframe, &r.rcompressed, &r.rmulticast,
&r.sbytes, &r.spackets, &r.serrs, &r.sdrop, &r.sfifo, &r.scolls, &r.scarrier, &r.scompressed); &r.sbytes, &r.spackets, &r.serrs, &r.sdrop, &r.sfifo, &r.scolls, &r.scarrier, &r.scompressed);
if (ret == 17) {
r.ok = true;
r.name[sizeof(r.name) - 1] = 0; r.name[sizeof(r.name) - 1] = 0;
_nb_srs_system_network_devices = i + 1; _nb_srs_system_network_devices = i + 1;
r.sample_time = srs_get_system_time_ms();
}
if (ret == EOF) { r.sample_time = srs_get_system_time_ms();
break; r.ok = true;
}
} }
fclose(f); fclose(f);
@ -832,19 +832,19 @@ void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps)
static char buf[1024]; static char buf[1024];
fgets(buf, sizeof(buf), f); fgets(buf, sizeof(buf), f);
// @see: https://github.com/shemminger/iproute2/blob/master/misc/ss.c
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
// @see: get_sockstat_line() // @see: et_sockstat_line() from https://github.com/shemminger/iproute2/blob/master/misc/ss.c
if (strncmp(buf, "sockets: used ", 14) == 0) { if (strncmp(buf, "sockets: used ", 14) == 0) {
int ret = sscanf(buf, "%*s %*s %d\n", &nb_socks); sscanf(buf + 14, "%d\n", &nb_socks);
srs_assert(ret == 1);
} else if (strncmp(buf, "TCP: ", 5) == 0) { } else if (strncmp(buf, "TCP: ", 5) == 0) {
int ret = sscanf(buf, "%*s %*s %d %*s %d %*s %d %*s %d %*s %d\n", sscanf(buf + 5, "%*s %d %*s %d %*s %d %*s %d %*s %d\n",
&nb_tcp4_hashed, &nb_tcp_orphans, &nb_tcp_tws, &nb_tcp_total, &nb_tcp_mem); &nb_tcp4_hashed,
srs_assert(ret == 5); &nb_tcp_orphans,
&nb_tcp_tws,
&nb_tcp_total,
&nb_tcp_mem);
} else if (strncmp(buf, "UDP: ", 5) == 0) { } else if (strncmp(buf, "UDP: ", 5) == 0) {
int ret = sscanf(buf, "%*s %*s %d\n", &nb_udp4); sscanf(buf + 5, "%*s %d\n", &nb_udp4);
srs_assert(ret == 1);
} }
} }
@ -875,8 +875,7 @@ void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps)
} }
// parse tcp stat data // parse tcp stat data
if (strncmp(buf, "Tcp: ", 5) == 0) { if (strncmp(buf, "Tcp: ", 5) == 0) {
int ret = sscanf(buf, "%*s %*d %*d %*d %*d %*d %*d %*d %*d %d\n", &nb_tcp_estab); sscanf(buf + 5, "%*d %*d %*d %*d %*d %*d %*d %*d %d\n", &nb_tcp_estab);
srs_assert(ret == 1);
} }
} }
} }

View file

@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version // current release version
#define VERSION_MAJOR "0" #define VERSION_MAJOR "0"
#define VERSION_MINOR "9" #define VERSION_MINOR "9"
#define VERSION_REVISION "184" #define VERSION_REVISION "185"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info. // server info.
#define RTMP_SIG_SRS_KEY "SRS" #define RTMP_SIG_SRS_KEY "SRS"