winlin

add total system in and out KBps disk stat. 0.9.174

@@ -72,6 +72,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -72,6 +72,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
72 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_CPU_STAT_RESOLUTION_TIMES 72 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_CPU_STAT_RESOLUTION_TIMES
73 #define SRS_SYS_CPU_STAT_RESOLUTION_TIMES 30 73 #define SRS_SYS_CPU_STAT_RESOLUTION_TIMES 30
74 74
  75 +// update the disk iops interval:
  76 +// SRS_SYS_CYCLE_INTERVAL * SRS_SYS_DISK_STAT_RESOLUTION_TIMES
  77 +// @remark, depends on SRS_SYS_CPU_STAT_RESOLUTION_TIMES, for the disk util
  78 +// depends on cpu time, so the disk stat times must be times of cpu stat time.
  79 +// for example, when cpu is 30, disk must be 30*1 or 30*2 ..., 30*N is ok.
  80 +#define SRS_SYS_DISK_STAT_RESOLUTION_TIMES 60
  81 +
75 // update rusage interval: 82 // update rusage interval:
76 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_MEMINFO_RESOLUTION_TIMES 83 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_MEMINFO_RESOLUTION_TIMES
77 #define SRS_SYS_MEMINFO_RESOLUTION_TIMES 60 84 #define SRS_SYS_MEMINFO_RESOLUTION_TIMES 60
@@ -669,6 +676,7 @@ int SrsServer::do_cycle() @@ -669,6 +676,7 @@ int SrsServer::do_cycle()
669 int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES); 676 int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES);
670 max = srs_max(max, SRS_SYS_RUSAGE_RESOLUTION_TIMES); 677 max = srs_max(max, SRS_SYS_RUSAGE_RESOLUTION_TIMES);
671 max = srs_max(max, SRS_SYS_CPU_STAT_RESOLUTION_TIMES); 678 max = srs_max(max, SRS_SYS_CPU_STAT_RESOLUTION_TIMES);
  679 + max = srs_max(max, SRS_SYS_DISK_STAT_RESOLUTION_TIMES);
672 max = srs_max(max, SRS_SYS_MEMINFO_RESOLUTION_TIMES); 680 max = srs_max(max, SRS_SYS_MEMINFO_RESOLUTION_TIMES);
673 max = srs_max(max, SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES); 681 max = srs_max(max, SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES);
674 max = srs_max(max, SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES); 682 max = srs_max(max, SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES);
@@ -719,9 +727,13 @@ int SrsServer::do_cycle() @@ -719,9 +727,13 @@ int SrsServer::do_cycle()
719 srs_update_system_rusage(); 727 srs_update_system_rusage();
720 } 728 }
721 if ((i % SRS_SYS_CPU_STAT_RESOLUTION_TIMES) == 0) { 729 if ((i % SRS_SYS_CPU_STAT_RESOLUTION_TIMES) == 0) {
722 - srs_info("update cpu info, usage."); 730 + srs_info("update cpu info, cpu usage.");
723 srs_update_proc_stat(); 731 srs_update_proc_stat();
724 } 732 }
  733 + if ((i % SRS_SYS_DISK_STAT_RESOLUTION_TIMES) == 0) {
  734 + srs_info("update disk info, disk iops.");
  735 + srs_update_disk_stat();
  736 + }
725 if ((i % SRS_SYS_MEMINFO_RESOLUTION_TIMES) == 0) { 737 if ((i % SRS_SYS_MEMINFO_RESOLUTION_TIMES) == 0) {
726 srs_info("update memory info, usage/free."); 738 srs_info("update memory info, usage/free.");
727 srs_update_meminfo(); 739 srs_update_meminfo();
@@ -350,6 +350,82 @@ void srs_update_proc_stat() @@ -350,6 +350,82 @@ void srs_update_proc_stat()
350 } 350 }
351 } 351 }
352 352
  353 +SrsDiskStat::SrsDiskStat()
  354 +{
  355 + ok = false;
  356 + sample_time = 0;
  357 + in_KBps = out_KBps = 0;
  358 +
  359 + pgpgin = 0;
  360 + pgpgout = 0;
  361 +}
  362 +
  363 +static SrsDiskStat _srs_disk_stat;
  364 +
  365 +SrsDiskStat* srs_get_disk_stat()
  366 +{
  367 + return &_srs_disk_stat;
  368 +}
  369 +
  370 +bool srs_get_disk_stat(SrsDiskStat& r)
  371 +{
  372 + FILE* f = fopen("/proc/vmstat", "r");
  373 + if (f == NULL) {
  374 + srs_warn("open vmstat failed, ignore");
  375 + return false;
  376 + }
  377 +
  378 + r.ok = false;
  379 + r.sample_time = srs_get_system_time_ms();
  380 +
  381 + for (;;) {
  382 + static char label[64];
  383 + static unsigned long value;
  384 + int ret = fscanf(f, "%64s %lu\n", label, &value);
  385 +
  386 + if (ret == EOF) {
  387 + break;
  388 + }
  389 +
  390 + if (strcmp("pgpgin", label) == 0) {
  391 + r.pgpgin = value;
  392 + } else if (strcmp("pgpgout", label) == 0) {
  393 + r.pgpgout = value;
  394 + }
  395 + }
  396 +
  397 + fclose(f);
  398 +
  399 + r.ok = true;
  400 +
  401 + return true;
  402 +}
  403 +
  404 +void srs_update_disk_stat()
  405 +{
  406 + SrsDiskStat r;
  407 + if (!srs_get_disk_stat(r)) {
  408 + return;
  409 + }
  410 +
  411 + SrsDiskStat& o = _srs_disk_stat;
  412 + if (o.ok) {
  413 + int64_t duration_ms = r.sample_time - o.sample_time;
  414 +
  415 + if (o.pgpgin > 0 && r.pgpgin > o.pgpgin && duration_ms > 0) {
  416 + // KBps = KB * 1000 / ms = KB/s
  417 + r.in_KBps = (r.pgpgin - o.pgpgin) * 1000 / duration_ms;
  418 + }
  419 +
  420 + if (o.pgpgout > 0 && r.pgpgout > o.pgpgout && duration_ms > 0) {
  421 + // KBps = KB * 1000 / ms = KB/s
  422 + r.out_KBps = (r.pgpgout - o.pgpgout) * 1000 / duration_ms;
  423 + }
  424 + }
  425 +
  426 + _srs_disk_stat = r;
  427 +}
  428 +
353 SrsMemInfo::SrsMemInfo() 429 SrsMemInfo::SrsMemInfo()
354 { 430 {
355 ok = false; 431 ok = false;
@@ -610,19 +686,19 @@ SrsNetworkRtmpServer* srs_get_network_rtmp_server() @@ -610,19 +686,19 @@ SrsNetworkRtmpServer* srs_get_network_rtmp_server()
610 686
611 // @see: http://stackoverflow.com/questions/5992211/list-of-possible-internal-socket-statuses-from-proc 687 // @see: http://stackoverflow.com/questions/5992211/list-of-possible-internal-socket-statuses-from-proc
612 enum { 688 enum {
613 - SYS_TCP_ESTABLISHED = 1,  
614 - SYS_TCP_SYN_SENT,  
615 - SYS_TCP_SYN_RECV,  
616 - SYS_TCP_FIN_WAIT1,  
617 - SYS_TCP_FIN_WAIT2,  
618 - SYS_TCP_TIME_WAIT,  
619 - SYS_TCP_CLOSE,  
620 - SYS_TCP_CLOSE_WAIT,  
621 - SYS_TCP_LAST_ACK,  
622 - SYS_TCP_LISTEN,  
623 - SYS_TCP_CLOSING, /* Now a valid state */  
624 -  
625 - SYS_TCP_MAX_STATES /* Leave at the end! */ 689 + SYS_TCP_ESTABLISHED = 0x01,
  690 + SYS_TCP_SYN_SENT, // 0x02
  691 + SYS_TCP_SYN_RECV, // 0x03
  692 + SYS_TCP_FIN_WAIT1, // 0x04
  693 + SYS_TCP_FIN_WAIT2, // 0x05
  694 + SYS_TCP_TIME_WAIT, // 0x06
  695 + SYS_TCP_CLOSE, // 0x07
  696 + SYS_TCP_CLOSE_WAIT, // 0x08
  697 + SYS_TCP_LAST_ACK, // 0x09
  698 + SYS_TCP_LISTEN, // 0x0A
  699 + SYS_TCP_CLOSING, // 0x0B /* Now a valid state */
  700 +
  701 + SYS_TCP_MAX_STATES // 0x0C /* Leave at the end! */
626 }; 702 };
627 703
628 void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps) 704 void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps)
@@ -811,6 +887,7 @@ void srs_api_dump_summaries(std::stringstream& ss) @@ -811,6 +887,7 @@ void srs_api_dump_summaries(std::stringstream& ss)
811 SrsPlatformInfo* p = srs_get_platform_info(); 887 SrsPlatformInfo* p = srs_get_platform_info();
812 SrsNetworkDevices* n = srs_get_network_devices(); 888 SrsNetworkDevices* n = srs_get_network_devices();
813 SrsNetworkRtmpServer* nrs = srs_get_network_rtmp_server(); 889 SrsNetworkRtmpServer* nrs = srs_get_network_rtmp_server();
  890 + SrsDiskStat* d = srs_get_disk_stat();
814 891
815 float self_mem_percent = 0; 892 float self_mem_percent = 0;
816 if (m->MemTotal > 0) { 893 if (m->MemTotal > 0) {
@@ -847,6 +924,7 @@ void srs_api_dump_summaries(std::stringstream& ss) @@ -847,6 +924,7 @@ void srs_api_dump_summaries(std::stringstream& ss)
847 << __SRS_JFIELD_ORG("self_cpu_stat_ok", (u->ok? "true":"false")) << __SRS_JFIELD_CONT 924 << __SRS_JFIELD_ORG("self_cpu_stat_ok", (u->ok? "true":"false")) << __SRS_JFIELD_CONT
848 << __SRS_JFIELD_ORG("system_cpu_stat_ok", (s->ok? "true":"false")) << __SRS_JFIELD_CONT 925 << __SRS_JFIELD_ORG("system_cpu_stat_ok", (s->ok? "true":"false")) << __SRS_JFIELD_CONT
849 << __SRS_JFIELD_ORG("cpuinfo_ok", (c->ok? "true":"false")) << __SRS_JFIELD_CONT 926 << __SRS_JFIELD_ORG("cpuinfo_ok", (c->ok? "true":"false")) << __SRS_JFIELD_CONT
  927 + << __SRS_JFIELD_ORG("disk_ok", (d->ok? "true":"false")) << __SRS_JFIELD_CONT
850 << __SRS_JFIELD_ORG("meminfo_ok", (m->ok? "true":"false")) << __SRS_JFIELD_CONT 928 << __SRS_JFIELD_ORG("meminfo_ok", (m->ok? "true":"false")) << __SRS_JFIELD_CONT
851 << __SRS_JFIELD_ORG("platform_ok", (p->ok? "true":"false")) << __SRS_JFIELD_CONT 929 << __SRS_JFIELD_ORG("platform_ok", (p->ok? "true":"false")) << __SRS_JFIELD_CONT
852 << __SRS_JFIELD_ORG("network_ok", (n_ok? "true":"false")) << __SRS_JFIELD_CONT 930 << __SRS_JFIELD_ORG("network_ok", (n_ok? "true":"false")) << __SRS_JFIELD_CONT
@@ -865,6 +943,8 @@ void srs_api_dump_summaries(std::stringstream& ss) @@ -865,6 +943,8 @@ void srs_api_dump_summaries(std::stringstream& ss)
865 << __SRS_JOBJECT_END << __SRS_JFIELD_CONT 943 << __SRS_JOBJECT_END << __SRS_JFIELD_CONT
866 << __SRS_JFIELD_ORG("system", __SRS_JOBJECT_START) 944 << __SRS_JFIELD_ORG("system", __SRS_JOBJECT_START)
867 << __SRS_JFIELD_ORG("cpu_percent", s->percent) << __SRS_JFIELD_CONT 945 << __SRS_JFIELD_ORG("cpu_percent", s->percent) << __SRS_JFIELD_CONT
  946 + << __SRS_JFIELD_ORG("disk_in_KBps", d->in_KBps) << __SRS_JFIELD_CONT
  947 + << __SRS_JFIELD_ORG("disk_out_KBps", d->out_KBps) << __SRS_JFIELD_CONT
868 << __SRS_JFIELD_ORG("mem_ram_kbyte", m->MemTotal) << __SRS_JFIELD_CONT 948 << __SRS_JFIELD_ORG("mem_ram_kbyte", m->MemTotal) << __SRS_JFIELD_CONT
869 << __SRS_JFIELD_ORG("mem_ram_percent", m->percent_ram) << __SRS_JFIELD_CONT 949 << __SRS_JFIELD_ORG("mem_ram_percent", m->percent_ram) << __SRS_JFIELD_CONT
870 << __SRS_JFIELD_ORG("mem_swap_kbyte", m->SwapTotal) << __SRS_JFIELD_CONT 950 << __SRS_JFIELD_ORG("mem_swap_kbyte", m->SwapTotal) << __SRS_JFIELD_CONT
@@ -59,8 +59,10 @@ public: @@ -59,8 +59,10 @@ public:
59 // the time in ms when sample. 59 // the time in ms when sample.
60 int64_t sample_time; 60 int64_t sample_time;
61 61
  62 +public:
62 rusage r; 63 rusage r;
63 64
  65 +public:
64 SrsRusage(); 66 SrsRusage();
65 }; 67 };
66 68
@@ -81,6 +83,8 @@ public: @@ -81,6 +83,8 @@ public:
81 // the percent of usage. 0.153 is 15.3%. 83 // the percent of usage. 0.153 is 15.3%.
82 float percent; 84 float percent;
83 85
  86 +// data of /proc/[pid]/stat
  87 +public:
84 // pid %d The process ID. 88 // pid %d The process ID.
85 int pid; 89 int pid;
86 // comm %s The filename of the executable, in parentheses. This is visible whether or not the executable is 90 // comm %s The filename of the executable, in parentheses. This is visible whether or not the executable is
@@ -219,6 +223,7 @@ public: @@ -219,6 +223,7 @@ public:
219 // Guest time of the process’s children, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). 223 // Guest time of the process’s children, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
220 long cguest_time; 224 long cguest_time;
221 225
  226 +public:
222 SrsProcSelfStat(); 227 SrsProcSelfStat();
223 }; 228 };
224 229
@@ -271,6 +276,7 @@ public: @@ -271,6 +276,7 @@ public:
271 // always be cpu 276 // always be cpu
272 char label[32]; 277 char label[32];
273 278
  279 +// data of /proc/stat
274 public: 280 public:
275 // The amount of time, measured in units of USER_HZ 281 // The amount of time, measured in units of USER_HZ
276 // (1/100ths of a second on most architectures, use 282 // (1/100ths of a second on most architectures, use
@@ -304,6 +310,7 @@ public: @@ -304,6 +310,7 @@ public:
304 // operating systems under the control of the Linux kernel. 310 // operating systems under the control of the Linux kernel.
305 unsigned long guest; 311 unsigned long guest;
306 312
  313 +public:
307 SrsProcSystemStat(); 314 SrsProcSystemStat();
308 315
309 // get total cpu units. 316 // get total cpu units.
@@ -317,6 +324,40 @@ extern SrsProcSystemStat* srs_get_system_proc_stat(); @@ -317,6 +324,40 @@ extern SrsProcSystemStat* srs_get_system_proc_stat();
317 // the deamon st-thread will update it. 324 // the deamon st-thread will update it.
318 extern void srs_update_proc_stat(); 325 extern void srs_update_proc_stat();
319 326
  327 +// stat disk iops
  328 +// @see: http://stackoverflow.com/questions/4458183/how-the-util-of-iostat-is-computed
  329 +// for total disk io, @see: cat /proc/vmstat |grep pgpg
  330 +// for device disk io, @see: cat /proc/diskstats
  331 +class SrsDiskStat
  332 +{
  333 +public:
  334 + // whether the data is ok.
  335 + bool ok;
  336 + // the time in ms when sample.
  337 + int64_t sample_time;
  338 + // input(read) KBytes per seconds
  339 + int in_KBps;
  340 + // output(write) KBytes per seconds
  341 + int out_KBps;
  342 +
  343 +public:
  344 + // @see: cat /proc/vmstat
  345 + // the in(read) page count, pgpgin*1024 is the read bytes.
  346 + // Total number of kilobytes the system paged in from disk per second.
  347 + unsigned long pgpgin;
  348 + // the out(write) page count, pgpgout*1024 is the write bytes.
  349 + // Total number of kilobytes the system paged out to disk per second.
  350 + unsigned long pgpgout;
  351 +
  352 +public:
  353 + SrsDiskStat();
  354 +};
  355 +
  356 +// get disk stat, use cache to avoid performance problem.
  357 +extern SrsDiskStat* srs_get_disk_stat();
  358 +// the deamon st-thread will update it.
  359 +extern void srs_update_disk_stat();
  360 +
320 // stat system memory info 361 // stat system memory info
321 // @see: cat /proc/meminfo 362 // @see: cat /proc/meminfo
322 class SrsMemInfo 363 class SrsMemInfo
@@ -330,6 +371,8 @@ public: @@ -330,6 +371,8 @@ public:
330 float percent_ram; 371 float percent_ram;
331 float percent_swap; 372 float percent_swap;
332 373
  374 +// data of /proc/meminfo
  375 +public:
333 // MemActive = MemTotal - MemFree 376 // MemActive = MemTotal - MemFree
334 int64_t MemActive; 377 int64_t MemActive;
335 // RealInUse = MemActive - Buffers - Cached 378 // RealInUse = MemActive - Buffers - Cached
@@ -347,6 +390,7 @@ public: @@ -347,6 +390,7 @@ public:
347 int64_t SwapTotal; 390 int64_t SwapTotal;
348 int64_t SwapFree; 391 int64_t SwapFree;
349 392
  393 +public:
350 SrsMemInfo(); 394 SrsMemInfo();
351 }; 395 };
352 396
@@ -357,17 +401,21 @@ extern void srs_update_meminfo(); @@ -357,17 +401,21 @@ extern void srs_update_meminfo();
357 401
358 // system cpu hardware info. 402 // system cpu hardware info.
359 // @see: cat /proc/cpuinfo 403 // @see: cat /proc/cpuinfo
  404 +// @remark, we use sysconf(_SC_NPROCESSORS_CONF) to get the cpu count.
360 class SrsCpuInfo 405 class SrsCpuInfo
361 { 406 {
362 public: 407 public:
363 // whether the data is ok. 408 // whether the data is ok.
364 bool ok; 409 bool ok;
365 410
  411 +// data of /proc/cpuinfo
  412 +public:
366 // The number of processors configured. 413 // The number of processors configured.
367 int nb_processors; 414 int nb_processors;
368 // The number of processors currently online (available). 415 // The number of processors currently online (available).
369 int nb_processors_online; 416 int nb_processors_online;
370 417
  418 +public:
371 SrsCpuInfo(); 419 SrsCpuInfo();
372 }; 420 };
373 421
@@ -384,6 +432,7 @@ public: @@ -384,6 +432,7 @@ public:
384 // srs startup time, in ms. 432 // srs startup time, in ms.
385 int64_t srs_startup_time; 433 int64_t srs_startup_time;
386 434
  435 +public:
387 // @see: cat /proc/uptime 436 // @see: cat /proc/uptime
388 // system startup time in seconds. 437 // system startup time in seconds.
389 double os_uptime; 438 double os_uptime;
@@ -397,6 +446,7 @@ public: @@ -397,6 +446,7 @@ public:
397 double load_five_minutes; 446 double load_five_minutes;
398 double load_fifteen_minutes; 447 double load_fifteen_minutes;
399 448
  449 +public:
400 SrsPlatformInfo(); 450 SrsPlatformInfo();
401 }; 451 };
402 452
@@ -418,6 +468,7 @@ public: @@ -418,6 +468,7 @@ public:
418 // the sample time in ms. 468 // the sample time in ms.
419 int64_t sample_time; 469 int64_t sample_time;
420 470
  471 +public:
421 // data for receive. 472 // data for receive.
422 unsigned long long rbytes; 473 unsigned long long rbytes;
423 unsigned long rpackets; 474 unsigned long rpackets;
@@ -438,6 +489,7 @@ public: @@ -438,6 +489,7 @@ public:
438 unsigned long scarrier; 489 unsigned long scarrier;
439 unsigned long scompressed; 490 unsigned long scompressed;
440 491
  492 +public:
441 SrsNetworkDevices(); 493 SrsNetworkDevices();
442 }; 494 };
443 495
@@ -457,6 +509,7 @@ public: @@ -457,6 +509,7 @@ public:
457 // the sample time in ms. 509 // the sample time in ms.
458 int64_t sample_time; 510 int64_t sample_time;
459 511
  512 +public:
460 // data for receive. 513 // data for receive.
461 int64_t rbytes; 514 int64_t rbytes;
462 int rkbps; 515 int rkbps;
@@ -476,6 +529,7 @@ public: @@ -476,6 +529,7 @@ public:
476 int nb_conn_sys_ls; // listen 529 int nb_conn_sys_ls; // listen
477 int nb_conn_srs; 530 int nb_conn_srs;
478 531
  532 +public:
479 SrsNetworkRtmpServer(); 533 SrsNetworkRtmpServer();
480 }; 534 };
481 535
@@ -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 "173" 34 +#define VERSION_REVISION "174"
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"