winlin

fix #142, tcp stat slow bug, use /proc/net/sockstat instead, refer to 'ss -s'. 0.9.183.

@@ -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 [#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.
210 * 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. 211 * 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.
211 * v1.0, 2014-07-27, support build on OSX(Darwin). 0.9.177 212 * v1.0, 2014-07-27, support build on OSX(Darwin). 0.9.177
212 * v1.0, 2014-07-27, api connections add udp, add disk iops. 0.9.176 213 * v1.0, 2014-07-27, api connections add udp, add disk iops. 0.9.176
@@ -68,6 +68,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -68,6 +68,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_RUSAGE_RESOLUTION_TIMES 68 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_RUSAGE_RESOLUTION_TIMES
69 #define SRS_SYS_RUSAGE_RESOLUTION_TIMES 30 69 #define SRS_SYS_RUSAGE_RESOLUTION_TIMES 30
70 70
  71 +// update network devices info interval:
  72 +// SRS_SYS_CYCLE_INTERVAL * SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES
  73 +#define SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES 30
  74 +
71 // update rusage interval: 75 // update rusage interval:
72 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_CPU_STAT_RESOLUTION_TIMES 76 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_CPU_STAT_RESOLUTION_TIMES
73 #define SRS_SYS_CPU_STAT_RESOLUTION_TIMES 30 77 #define SRS_SYS_CPU_STAT_RESOLUTION_TIMES 30
@@ -88,10 +92,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -88,10 +92,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES 92 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES
89 #define SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES 90 93 #define SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES 90
90 94
91 -// update network devices info interval:  
92 -// SRS_SYS_CYCLE_INTERVAL * SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES  
93 -#define SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES 90  
94 -  
95 SrsListener::SrsListener(SrsServer* server, SrsListenerType type) 95 SrsListener::SrsListener(SrsServer* server, SrsListenerType type)
96 { 96 {
97 fd = -1; 97 fd = -1;
@@ -781,7 +781,7 @@ SrsNetworkRtmpServer::SrsNetworkRtmpServer() @@ -781,7 +781,7 @@ SrsNetworkRtmpServer::SrsNetworkRtmpServer()
781 ok = false; 781 ok = false;
782 sample_time = rbytes = sbytes = 0; 782 sample_time = rbytes = sbytes = 0;
783 nb_conn_sys = nb_conn_srs = 0; 783 nb_conn_sys = nb_conn_srs = 0;
784 - nb_conn_sys_et = nb_conn_sys_tw = nb_conn_sys_ls = 0; 784 + nb_conn_sys_et = nb_conn_sys_tw = 0;
785 nb_conn_sys_udp = 0; 785 nb_conn_sys_udp = 0;
786 } 786 }
787 787
@@ -812,88 +812,88 @@ enum { @@ -812,88 +812,88 @@ enum {
812 void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps) 812 void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps)
813 { 813 {
814 SrsNetworkRtmpServer& r = _srs_network_rtmp_server; 814 SrsNetworkRtmpServer& r = _srs_network_rtmp_server;
815 -  
816 - // reset total.  
817 - r.nb_conn_sys = 0; 815 +
  816 + int nb_socks = 0;
  817 + int nb_tcp4_hashed = 0;
  818 + int nb_tcp_orphans = 0;
  819 + int nb_tcp_tws = 0;
  820 + int nb_tcp_total = 0;
  821 + int nb_tcp_mem = 0;
  822 + int nb_udp4 = 0;
818 823
819 if (true) { 824 if (true) {
820 - FILE* f = fopen("/proc/net/tcp", "r"); 825 + FILE* f = fopen("/proc/net/sockstat", "r");
821 if (f == NULL) { 826 if (f == NULL) {
822 - srs_warn("open proc network tcp failed, ignore"); 827 + srs_warn("open proc network sockstat failed, ignore");
823 return; 828 return;
824 } 829 }
825 830
826 // ignore title. 831 // ignore title.
827 static char buf[1024]; 832 static char buf[1024];
828 fgets(buf, sizeof(buf), f); 833 fgets(buf, sizeof(buf), f);
829 -  
830 - int nb_conn_sys_established = 0;  
831 - int nb_conn_sys_time_wait = 0;  
832 - int nb_conn_sys_listen = 0;  
833 - int nb_conn_sys_other = 0;  
834 834
835 - // @see: http://tester-higkoo.googlecode.com/svn-history/r14/trunk/Tools/iostat/iostat.c 835 + // @see: https://github.com/shemminger/iproute2/blob/master/misc/ss.c
836 while (fgets(buf, sizeof(buf), f)) { 836 while (fgets(buf, sizeof(buf), f)) {
837 - int st = 0;  
838 - int ret = sscanf(buf, "%*s %*s %*s %2x\n", &st);  
839 -  
840 - if (ret == 1) {  
841 - if (st == SYS_TCP_ESTABLISHED) {  
842 - nb_conn_sys_established++;  
843 - } else if (st == SYS_TCP_TIME_WAIT) {  
844 - nb_conn_sys_time_wait++;  
845 - } else if (st == SYS_TCP_LISTEN) {  
846 - nb_conn_sys_listen++;  
847 - } else {  
848 - nb_conn_sys_other++;  
849 - }  
850 - }  
851 -  
852 - if (ret == EOF) {  
853 - break; 837 + // @see: get_sockstat_line()
  838 + if (strncmp(buf, "sockets: used ", 14) == 0) {
  839 + int ret = sscanf(buf, "%*s %*s %d\n", &nb_socks);
  840 + srs_assert(ret == 1);
  841 + } 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);
  845 + } else if (strncmp(buf, "UDP: ", 5) == 0) {
  846 + int ret = sscanf(buf, "%*s %*s %d\n", &nb_udp4);
  847 + srs_assert(ret == 1);
854 } 848 }
855 } 849 }
856 -  
857 - r.nb_conn_sys = nb_conn_sys_established + nb_conn_sys_time_wait + nb_conn_sys_listen + nb_conn_sys_other;  
858 - r.nb_conn_sys_et = nb_conn_sys_established;  
859 - r.nb_conn_sys_tw = nb_conn_sys_time_wait;  
860 - r.nb_conn_sys_ls = nb_conn_sys_listen;  
861 850
862 fclose(f); 851 fclose(f);
863 } 852 }
864 853
  854 + int nb_tcp_estab = 0;
  855 +
865 if (true) { 856 if (true) {
866 - FILE* f = fopen("/proc/net/udp", "r"); 857 + FILE* f = fopen("/proc/net/snmp", "r");
867 if (f == NULL) { 858 if (f == NULL) {
868 - srs_warn("open proc network udp failed, ignore"); 859 + srs_warn("open proc network snmp failed, ignore");
869 return; 860 return;
870 } 861 }
871 862
872 // ignore title. 863 // ignore title.
873 static char buf[1024]; 864 static char buf[1024];
874 fgets(buf, sizeof(buf), f); 865 fgets(buf, sizeof(buf), f);
875 -  
876 - // all udp is close state.  
877 - int nb_conn_sys_close = 0;  
878 866
879 - // @see: http://tester-higkoo.googlecode.com/svn-history/r14/trunk/Tools/iostat/iostat.c 867 + // @see: https://github.com/shemminger/iproute2/blob/master/misc/ss.c
880 while (fgets(buf, sizeof(buf), f)) { 868 while (fgets(buf, sizeof(buf), f)) {
881 - int st = 0;  
882 - int ret = sscanf(buf, "%*s %*s %*s %2x\n", &st);  
883 -  
884 - if (ret == EOF) {  
885 - break; 869 + // @see: get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab)
  870 + // tcp stat title
  871 + if (strncmp(buf, "Tcp: ", 5) == 0) {
  872 + // read tcp stat data
  873 + if (!fgets(buf, sizeof(buf), f)) {
  874 + break;
  875 + }
  876 + // parse tcp stat data
  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);
  880 + }
886 } 881 }
887 -  
888 - nb_conn_sys_close++;  
889 } 882 }
890 -  
891 - r.nb_conn_sys += nb_conn_sys_close;  
892 - r.nb_conn_sys_udp = nb_conn_sys_close;  
893 883
894 fclose(f); 884 fclose(f);
895 } 885 }
896 886
  887 + // @see: https://github.com/shemminger/iproute2/blob/master/misc/ss.c
  888 + // TODO: FIXME: ignore the slabstat, @see: get_slabstat()
  889 + if (true) {
  890 + // @see: print_summary()
  891 + r.nb_conn_sys = nb_tcp_total + nb_tcp_tws;
  892 + r.nb_conn_sys_et = nb_tcp_estab;
  893 + r.nb_conn_sys_tw = nb_tcp_tws;
  894 + r.nb_conn_sys_udp = nb_udp4;
  895 + }
  896 +
897 if (true) { 897 if (true) {
898 r.ok = true; 898 r.ok = true;
899 899
@@ -1113,7 +1113,6 @@ void srs_api_dump_summaries(std::stringstream& ss) @@ -1113,7 +1113,6 @@ void srs_api_dump_summaries(std::stringstream& ss)
1113 << __SRS_JFIELD_ORG("conn_sys", nrs->nb_conn_sys) << __SRS_JFIELD_CONT 1113 << __SRS_JFIELD_ORG("conn_sys", nrs->nb_conn_sys) << __SRS_JFIELD_CONT
1114 << __SRS_JFIELD_ORG("conn_sys_et", nrs->nb_conn_sys_et) << __SRS_JFIELD_CONT 1114 << __SRS_JFIELD_ORG("conn_sys_et", nrs->nb_conn_sys_et) << __SRS_JFIELD_CONT
1115 << __SRS_JFIELD_ORG("conn_sys_tw", nrs->nb_conn_sys_tw) << __SRS_JFIELD_CONT 1115 << __SRS_JFIELD_ORG("conn_sys_tw", nrs->nb_conn_sys_tw) << __SRS_JFIELD_CONT
1116 - << __SRS_JFIELD_ORG("conn_sys_ls", nrs->nb_conn_sys_ls) << __SRS_JFIELD_CONT  
1117 << __SRS_JFIELD_ORG("conn_sys_udp", nrs->nb_conn_sys_udp) << __SRS_JFIELD_CONT 1116 << __SRS_JFIELD_ORG("conn_sys_udp", nrs->nb_conn_sys_udp) << __SRS_JFIELD_CONT
1118 << __SRS_JFIELD_ORG("conn_srs", nrs->nb_conn_srs) 1117 << __SRS_JFIELD_ORG("conn_srs", nrs->nb_conn_srs)
1119 << __SRS_JOBJECT_END 1118 << __SRS_JOBJECT_END
@@ -595,11 +595,14 @@ public: @@ -595,11 +595,14 @@ public:
595 int skbps_5m; 595 int skbps_5m;
596 596
597 // connections 597 // connections
  598 + // @see: /proc/net/snmp
  599 + // @see: /proc/net/sockstat
598 int nb_conn_sys; 600 int nb_conn_sys;
599 int nb_conn_sys_et; // established 601 int nb_conn_sys_et; // established
600 int nb_conn_sys_tw; // time wait 602 int nb_conn_sys_tw; // time wait
601 - int nb_conn_sys_ls; // listen  
602 int nb_conn_sys_udp; // udp 603 int nb_conn_sys_udp; // udp
  604 +
  605 + // retrieve from srs interface
603 int nb_conn_srs; 606 int nb_conn_srs;
604 607
605 public: 608 public:
@@ -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 "182" 34 +#define VERSION_REVISION "183"
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"