winlin

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

@@ -207,6 +207,7 @@ Supported operating systems and hardware: @@ -207,6 +207,7 @@ Supported operating systems and hardware:
207 * 2013-10-17, Created.<br/> 207 * 2013-10-17, Created.<br/>
208 208
209 ## History 209 ## History
  210 +* 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.
210 * 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. 211 * 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.
211 * 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. 212 * 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.
212 * 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. 213 * 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.
@@ -228,29 +228,33 @@ bool get_proc_system_stat(SrsProcSystemStat& r) @@ -228,29 +228,33 @@ bool get_proc_system_stat(SrsProcSystemStat& r)
228 return false; 228 return false;
229 } 229 }
230 230
231 - r.ok = false;  
232 -  
233 static char buf[1024]; 231 static char buf[1024];
234 while (fgets(buf, sizeof(buf), f)) { 232 while (fgets(buf, sizeof(buf), f)) {
235 if (strncmp(buf, "cpu ", 4) != 0) { 233 if (strncmp(buf, "cpu ", 4) != 0) {
236 continue; 234 continue;
237 } 235 }
238 236
239 - int ret = sscanf(buf, "cpu %llu %llu %llu %llu %llu "  
240 - "%llu %llu %llu %llu\n",  
241 - &r.user, &r.nice, &r.sys, &r.idle, &r.iowait,  
242 - &r.irq, &r.softirq, &r.steal, &r.guest);  
243 - srs_assert(ret == 9);  
244 -  
245 - // matched ok.  
246 - r.ok = true; 237 + // @see: read_stat_cpu() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L88
  238 + // @remark, ignore the filed 10 cpu_guest_nice
  239 + sscanf(buf + 5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  240 + &r.user,
  241 + &r.nice,
  242 + &r.sys,
  243 + &r.idle,
  244 + &r.iowait,
  245 + &r.irq,
  246 + &r.softirq,
  247 + &r.steal,
  248 + &r.guest);
247 249
248 break; 250 break;
249 } 251 }
250 252
251 fclose(f); 253 fclose(f);
  254 +
  255 + r.ok = true;
252 256
253 - return r.ok; 257 + return true;
254 } 258 }
255 259
256 bool get_proc_self_stat(SrsProcSelfStat& r) 260 bool get_proc_self_stat(SrsProcSelfStat& r)
@@ -261,7 +265,7 @@ bool get_proc_self_stat(SrsProcSelfStat& r) @@ -261,7 +265,7 @@ bool get_proc_self_stat(SrsProcSelfStat& r)
261 return false; 265 return false;
262 } 266 }
263 267
264 - int ret = fscanf(f, "%d %32s %c %d %d %d %d " 268 + fscanf(f, "%d %32s %c %d %d %d %d "
265 "%d %u %lu %lu %lu %lu " 269 "%d %u %lu %lu %lu %lu "
266 "%lu %lu %ld %ld %ld %ld " 270 "%lu %lu %ld %ld %ld %ld "
267 "%ld %ld %llu %lu %ld " 271 "%ld %ld %llu %lu %ld "
@@ -281,12 +285,10 @@ bool get_proc_self_stat(SrsProcSelfStat& r) @@ -281,12 +285,10 @@ bool get_proc_self_stat(SrsProcSelfStat& r)
281 &r.guest_time, &r.cguest_time); 285 &r.guest_time, &r.cguest_time);
282 286
283 fclose(f); 287 fclose(f);
284 -  
285 - if (ret >= 0) {  
286 - r.ok = true;  
287 - }  
288 288
289 - return r.ok; 289 + r.ok = true;
  290 +
  291 + return true;
290 } 292 }
291 293
292 void srs_update_proc_stat() 294 void srs_update_proc_stat()
@@ -386,19 +388,15 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r) @@ -386,19 +388,15 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
386 return false; 388 return false;
387 } 389 }
388 390
389 - r.ok = false;  
390 r.sample_time = srs_get_system_time_ms(); 391 r.sample_time = srs_get_system_time_ms();
391 392
392 static char buf[1024]; 393 static char buf[1024];
393 while (fgets(buf, sizeof(buf), f)) { 394 while (fgets(buf, sizeof(buf), f)) {
394 - unsigned long value = 0;  
395 - int ret = sscanf(buf, "%*s %lu\n", &value);  
396 - srs_assert(ret == 1);  
397 - 395 + // @see: read_vmstat_paging() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L495
398 if (strncmp(buf, "pgpgin ", 7) == 0) { 396 if (strncmp(buf, "pgpgin ", 7) == 0) {
399 - r.pgpgin = value; 397 + sscanf(buf + 7, "%lu\n", &r.pgpgin);
400 } else if (strncmp(buf, "pgpgout ", 8) == 0) { 398 } else if (strncmp(buf, "pgpgout ", 8) == 0) {
401 - r.pgpgout = value; 399 + sscanf(buf + 8, "%lu\n", &r.pgpgout);
402 } 400 }
403 } 401 }
404 402
@@ -411,13 +409,12 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r) @@ -411,13 +409,12 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
411 409
412 bool srs_get_disk_diskstats_stat(SrsDiskStat& r) 410 bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
413 { 411 {
414 - r.ok = false; 412 + r.ok = true;
415 r.sample_time = srs_get_system_time_ms(); 413 r.sample_time = srs_get_system_time_ms();
416 414
417 // if disabled, ignore all devices. 415 // if disabled, ignore all devices.
418 SrsConfDirective* conf = _srs_config->get_stats_disk_device(); 416 SrsConfDirective* conf = _srs_config->get_stats_disk_device();
419 if (conf == NULL) { 417 if (conf == NULL) {
420 - r.ok = true;  
421 return true; 418 return true;
422 } 419 }
423 420
@@ -444,12 +441,22 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r) @@ -444,12 +441,22 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
444 unsigned int ticks = 0; 441 unsigned int ticks = 0;
445 unsigned int aveq = 0; 442 unsigned int aveq = 0;
446 memset(name, sizeof(name), 0); 443 memset(name, sizeof(name), 0);
447 - int ret = sscanf(buf,  
448 - "%4d %4d %31s %u %u %llu %u %u %u %llu %u %u %u %u",  
449 - &major, &minor, name, &rd_ios, &rd_merges,  
450 - &rd_sectors, &rd_ticks, &wr_ios, &wr_merges,  
451 - &wr_sectors, &wr_ticks, &nb_current, &ticks, &aveq);  
452 - srs_assert(ret == 14); 444 +
  445 + sscanf(buf, "%4d %4d %31s %u %u %llu %u %u %u %llu %u %u %u %u",
  446 + &major,
  447 + &minor,
  448 + name,
  449 + &rd_ios,
  450 + &rd_merges,
  451 + &rd_sectors,
  452 + &rd_ticks,
  453 + &wr_ios,
  454 + &wr_merges,
  455 + &wr_sectors,
  456 + &wr_ticks,
  457 + &nb_current,
  458 + &ticks,
  459 + &aveq);
453 460
454 for (int i = 0; i < (int)conf->args.size(); i++) { 461 for (int i = 0; i < (int)conf->args.size(); i++) {
455 string name_ok = conf->args.at(i); 462 string name_ok = conf->args.at(i);
@@ -524,7 +531,7 @@ void srs_update_disk_stat() @@ -524,7 +531,7 @@ void srs_update_disk_stat()
524 && cpuinfo->ok && cpuinfo->nb_processors > 0 531 && cpuinfo->ok && cpuinfo->nb_processors > 0
525 && o.ticks < r.ticks 532 && o.ticks < r.ticks
526 ) { 533 ) {
527 - // @see: print_partition_stats() of iostat.c 534 + // @see: write_ext_stat() from https://github.com/sysstat/sysstat/blob/master/iostat.c#L979
528 // TODO: FIXME: the USER_HZ assert to 100, so the total_delta ticks *10 is ms. 535 // TODO: FIXME: the USER_HZ assert to 100, so the total_delta ticks *10 is ms.
529 double delta_ms = r.cpu.total_delta * 10 / cpuinfo->nb_processors; 536 double delta_ms = r.cpu.total_delta * 10 / cpuinfo->nb_processors;
530 unsigned int ticks = r.ticks - o.ticks; 537 unsigned int ticks = r.ticks - o.ticks;
@@ -572,26 +579,22 @@ void srs_update_meminfo() @@ -572,26 +579,22 @@ void srs_update_meminfo()
572 } 579 }
573 580
574 SrsMemInfo& r = _srs_system_meminfo; 581 SrsMemInfo& r = _srs_system_meminfo;
575 - r.ok = false;  
576 582
577 static char buf[1024]; 583 static char buf[1024];
578 while (fgets(buf, sizeof(buf), f)) { 584 while (fgets(buf, sizeof(buf), f)) {
579 - static unsigned long value;  
580 - int ret = sscanf(buf, "%*s %lu", &value);  
581 - srs_assert(ret == 1);  
582 - 585 + // @see: read_meminfo() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L227
583 if (strncmp(buf, "MemTotal:", 9) == 0) { 586 if (strncmp(buf, "MemTotal:", 9) == 0) {
584 - r.MemTotal = value; 587 + sscanf(buf + 9, "%lu", &r.MemTotal);
585 } else if (strncmp(buf, "MemFree:", 8) == 0) { 588 } else if (strncmp(buf, "MemFree:", 8) == 0) {
586 - r.MemFree = value; 589 + sscanf(buf + 8, "%lu", &r.MemFree);
587 } else if (strncmp(buf, "Buffers:", 8) == 0) { 590 } else if (strncmp(buf, "Buffers:", 8) == 0) {
588 - r.Buffers = value; 591 + sscanf(buf + 8, "%lu", &r.Buffers);
589 } else if (strncmp(buf, "Cached:", 7) == 0) { 592 } else if (strncmp(buf, "Cached:", 7) == 0) {
590 - r.Cached = value; 593 + sscanf(buf + 7, "%lu", &r.Cached);
591 } else if (strncmp(buf, "SwapTotal:", 10) == 0) { 594 } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
592 - r.SwapTotal = value; 595 + sscanf(buf + 10, "%lu", &r.SwapTotal);
593 } else if (strncmp(buf, "SwapFree:", 9) == 0) { 596 } else if (strncmp(buf, "SwapFree:", 9) == 0) {
594 - r.SwapFree = value; 597 + sscanf(buf + 9, "%lu", &r.SwapFree);
595 } 598 }
596 } 599 }
597 600
@@ -602,13 +605,14 @@ void srs_update_meminfo() @@ -602,13 +605,14 @@ void srs_update_meminfo()
602 r.RealInUse = r.MemActive - r.Buffers - r.Cached; 605 r.RealInUse = r.MemActive - r.Buffers - r.Cached;
603 r.NotInUse = r.MemTotal - r.RealInUse; 606 r.NotInUse = r.MemTotal - r.RealInUse;
604 607
605 - r.ok = true;  
606 if (r.MemTotal > 0) { 608 if (r.MemTotal > 0) {
607 r.percent_ram = (float)(r.RealInUse / (double)r.MemTotal); 609 r.percent_ram = (float)(r.RealInUse / (double)r.MemTotal);
608 } 610 }
609 if (r.SwapTotal > 0) { 611 if (r.SwapTotal > 0) {
610 r.percent_swap = (float)((r.SwapTotal - r.SwapFree) / (double)r.SwapTotal); 612 r.percent_swap = (float)((r.SwapTotal - r.SwapFree) / (double)r.SwapTotal);
611 } 613 }
  614 +
  615 + r.ok = true;
612 } 616 }
613 617
614 SrsCpuInfo::SrsCpuInfo() 618 SrsCpuInfo::SrsCpuInfo()
@@ -659,7 +663,6 @@ SrsPlatformInfo* srs_get_platform_info() @@ -659,7 +663,6 @@ SrsPlatformInfo* srs_get_platform_info()
659 void srs_update_platform_info() 663 void srs_update_platform_info()
660 { 664 {
661 SrsPlatformInfo& r = _srs_system_platform_info; 665 SrsPlatformInfo& r = _srs_system_platform_info;
662 - r.ok = true;  
663 666
664 r.srs_startup_time = srs_get_system_startup_time_ms(); 667 r.srs_startup_time = srs_get_system_startup_time_ms();
665 668
@@ -670,13 +673,9 @@ void srs_update_platform_info() @@ -670,13 +673,9 @@ void srs_update_platform_info()
670 return; 673 return;
671 } 674 }
672 675
673 - int ret = fscanf(f, "%lf %lf\n", &r.os_uptime, &r.os_ilde_time); 676 + fscanf(f, "%lf %lf\n", &r.os_uptime, &r.os_ilde_time);
674 677
675 fclose(f); 678 fclose(f);
676 -  
677 - if (ret < 0) {  
678 - r.ok = false;  
679 - }  
680 } 679 }
681 680
682 if (true) { 681 if (true) {
@@ -686,15 +685,17 @@ void srs_update_platform_info() @@ -686,15 +685,17 @@ void srs_update_platform_info()
686 return; 685 return;
687 } 686 }
688 687
689 - int ret = fscanf(f, "%lf %lf %lf\n",  
690 - &r.load_one_minutes, &r.load_five_minutes, &r.load_fifteen_minutes); 688 + // @see: read_loadavg() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L402
  689 + // @remark, we use our algorithm, not sysstat.
  690 + fscanf(f, "%lf %lf %lf\n",
  691 + &r.load_one_minutes,
  692 + &r.load_five_minutes,
  693 + &r.load_fifteen_minutes);
691 694
692 fclose(f); 695 fclose(f);
693 -  
694 - if (ret < 0) {  
695 - r.ok = false;  
696 - }  
697 } 696 }
  697 +
  698 + r.ok = true;
698 } 699 }
699 700
700 SrsNetworkDevices::SrsNetworkDevices() 701 SrsNetworkDevices::SrsNetworkDevices()
@@ -752,24 +753,23 @@ void srs_update_network_devices() @@ -752,24 +753,23 @@ void srs_update_network_devices()
752 fgets(buf, sizeof(buf), f); 753 fgets(buf, sizeof(buf), f);
753 754
754 for (int i = 0; i < MAX_NETWORK_DEVICES_COUNT; i++) { 755 for (int i = 0; i < MAX_NETWORK_DEVICES_COUNT; i++) {
  756 + if (!fgets(buf, sizeof(buf), f)) {
  757 + break;
  758 + }
  759 +
755 SrsNetworkDevices& r = _srs_system_network_devices[i]; 760 SrsNetworkDevices& r = _srs_system_network_devices[i];
756 - r.ok = false;  
757 - r.sample_time = 0;  
758 761
759 - int ret = fscanf(f, "%6[^:]:%llu %lu %lu %lu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu %lu %lu\n", 762 + // @see: read_net_dev() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L786
  763 + // @remark, we use our algorithm, not sysstat.
  764 + sscanf(buf, "%6[^:]:%llu %lu %lu %lu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu %lu %lu\n",
760 r.name, &r.rbytes, &r.rpackets, &r.rerrs, &r.rdrop, &r.rfifo, &r.rframe, &r.rcompressed, &r.rmulticast, 765 r.name, &r.rbytes, &r.rpackets, &r.rerrs, &r.rdrop, &r.rfifo, &r.rframe, &r.rcompressed, &r.rmulticast,
761 &r.sbytes, &r.spackets, &r.serrs, &r.sdrop, &r.sfifo, &r.scolls, &r.scarrier, &r.scompressed); 766 &r.sbytes, &r.spackets, &r.serrs, &r.sdrop, &r.sfifo, &r.scolls, &r.scarrier, &r.scompressed);
762 767
763 - if (ret == 17) {  
764 - r.ok = true;  
765 - r.name[sizeof(r.name) - 1] = 0;  
766 - _nb_srs_system_network_devices = i + 1;  
767 - r.sample_time = srs_get_system_time_ms();  
768 - } 768 + r.name[sizeof(r.name) - 1] = 0;
  769 + _nb_srs_system_network_devices = i + 1;
769 770
770 - if (ret == EOF) {  
771 - break;  
772 - } 771 + r.sample_time = srs_get_system_time_ms();
  772 + r.ok = true;
773 } 773 }
774 774
775 fclose(f); 775 fclose(f);
@@ -832,19 +832,19 @@ void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps) @@ -832,19 +832,19 @@ void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps)
832 static char buf[1024]; 832 static char buf[1024];
833 fgets(buf, sizeof(buf), f); 833 fgets(buf, sizeof(buf), f);
834 834
835 - // @see: https://github.com/shemminger/iproute2/blob/master/misc/ss.c  
836 while (fgets(buf, sizeof(buf), f)) { 835 while (fgets(buf, sizeof(buf), f)) {
837 - // @see: get_sockstat_line() 836 + // @see: et_sockstat_line() from https://github.com/shemminger/iproute2/blob/master/misc/ss.c
838 if (strncmp(buf, "sockets: used ", 14) == 0) { 837 if (strncmp(buf, "sockets: used ", 14) == 0) {
839 - int ret = sscanf(buf, "%*s %*s %d\n", &nb_socks);  
840 - srs_assert(ret == 1); 838 + sscanf(buf + 14, "%d\n", &nb_socks);
841 } else if (strncmp(buf, "TCP: ", 5) == 0) { 839 } else if (strncmp(buf, "TCP: ", 5) == 0) {
842 - int ret = sscanf(buf, "%*s %*s %d %*s %d %*s %d %*s %d %*s %d\n",  
843 - &nb_tcp4_hashed, &nb_tcp_orphans, &nb_tcp_tws, &nb_tcp_total, &nb_tcp_mem);  
844 - srs_assert(ret == 5); 840 + sscanf(buf + 5, "%*s %d %*s %d %*s %d %*s %d %*s %d\n",
  841 + &nb_tcp4_hashed,
  842 + &nb_tcp_orphans,
  843 + &nb_tcp_tws,
  844 + &nb_tcp_total,
  845 + &nb_tcp_mem);
845 } else if (strncmp(buf, "UDP: ", 5) == 0) { 846 } else if (strncmp(buf, "UDP: ", 5) == 0) {
846 - int ret = sscanf(buf, "%*s %*s %d\n", &nb_udp4);  
847 - srs_assert(ret == 1); 847 + sscanf(buf + 5, "%*s %d\n", &nb_udp4);
848 } 848 }
849 } 849 }
850 850
@@ -875,8 +875,7 @@ void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps) @@ -875,8 +875,7 @@ void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps)
875 } 875 }
876 // parse tcp stat data 876 // parse tcp stat data
877 if (strncmp(buf, "Tcp: ", 5) == 0) { 877 if (strncmp(buf, "Tcp: ", 5) == 0) {
878 - int ret = sscanf(buf, "%*s %*d %*d %*d %*d %*d %*d %*d %*d %d\n", &nb_tcp_estab);  
879 - srs_assert(ret == 1); 878 + sscanf(buf + 5, "%*d %*d %*d %*d %*d %*d %*d %*d %d\n", &nb_tcp_estab);
880 } 879 }
881 } 880 }
882 } 881 }
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "184" 34 +#define VERSION_REVISION "185"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"