winlin

add config item for the stat disk device name

@@ -88,6 +88,9 @@ stats { @@ -88,6 +88,9 @@ stats {
88 # we may retrieve more than one network device. 88 # we may retrieve more than one network device.
89 # default: 0 89 # default: 0
90 network_device_index 0; 90 network_device_index 0;
  91 + # the device name to stat the disk iops.
  92 + # ignore the device of /proc/diskstats if not configed.
  93 + disk_device_name sda sdb xvda xvdb;
91 } 94 }
92 95
93 ############################################################################################# 96 #############################################################################################
@@ -1249,7 +1249,7 @@ int SrsConfig::check_config() @@ -1249,7 +1249,7 @@ int SrsConfig::check_config()
1249 SrsConfDirective* conf = get_stats(); 1249 SrsConfDirective* conf = get_stats();
1250 for (int i = 0; conf && i < (int)conf->directives.size(); i++) { 1250 for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
1251 string n = conf->at(i)->name; 1251 string n = conf->at(i)->name;
1252 - if (n != "network_device_index") { 1252 + if (n != "network_device_index" && n != "disk_device_name") {
1253 ret = ERROR_SYSTEM_CONFIG_INVALID; 1253 ret = ERROR_SYSTEM_CONFIG_INVALID;
1254 srs_error("unsupported stats directive %s, ret=%d", n.c_str(), ret); 1254 srs_error("unsupported stats directive %s, ret=%d", n.c_str(), ret);
1255 return ret; 1255 return ret;
@@ -3194,6 +3194,22 @@ int SrsConfig::get_stats_network_device_index() @@ -3194,6 +3194,22 @@ int SrsConfig::get_stats_network_device_index()
3194 return ::atoi(conf->arg0().c_str()); 3194 return ::atoi(conf->arg0().c_str());
3195 } 3195 }
3196 3196
  3197 +SrsConfDirective* SrsConfig::get_stats_disk_device()
  3198 +{
  3199 + SrsConfDirective* conf = get_stats();
  3200 +
  3201 + if (!conf) {
  3202 + return NULL;
  3203 + }
  3204 +
  3205 + conf = conf->get("disk_device_name");
  3206 + if (!conf || conf->args.size() == 0) {
  3207 + return NULL;
  3208 + }
  3209 +
  3210 + return conf;
  3211 +}
  3212 +
3197 namespace _srs_internal 3213 namespace _srs_internal
3198 { 3214 {
3199 SrsConfigBuffer::SrsConfigBuffer() 3215 SrsConfigBuffer::SrsConfigBuffer()
@@ -943,6 +943,12 @@ public: @@ -943,6 +943,12 @@ public:
943 * for example, 0 means the eth0 maybe. 943 * for example, 0 means the eth0 maybe.
944 */ 944 */
945 virtual int get_stats_network_device_index(); 945 virtual int get_stats_network_device_index();
  946 + /**
  947 + * get the disk stat device name list.
  948 + * the device name configed in args of directive.
  949 + * @return the disk device name to stat. NULL if not configed.
  950 + */
  951 + virtual SrsConfDirective* get_stats_disk_device();
946 }; 952 };
947 953
948 namespace _srs_internal 954 namespace _srs_internal
@@ -415,15 +415,22 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r) @@ -415,15 +415,22 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
415 415
416 bool srs_get_disk_diskstats_stat(SrsDiskStat& r) 416 bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
417 { 417 {
  418 + r.ok = false;
  419 + r.sample_time = srs_get_system_time_ms();
  420 +
  421 + // if disabled, ignore all devices.
  422 + SrsConfDirective* conf = _srs_config->get_stats_disk_device();
  423 + if (conf == NULL) {
  424 + r.ok = true;
  425 + return true;
  426 + }
  427 +
418 FILE* f = fopen("/proc/diskstats", "r"); 428 FILE* f = fopen("/proc/diskstats", "r");
419 if (f == NULL) { 429 if (f == NULL) {
420 srs_warn("open vmstat failed, ignore"); 430 srs_warn("open vmstat failed, ignore");
421 return false; 431 return false;
422 } 432 }
423 433
424 - r.ok = false;  
425 - r.sample_time = srs_get_system_time_ms();  
426 -  
427 static char buf[1024]; 434 static char buf[1024];
428 while (fgets(buf, sizeof(buf), f)) { 435 while (fgets(buf, sizeof(buf), f)) {
429 unsigned int major = 0; 436 unsigned int major = 0;
@@ -447,9 +454,14 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r) @@ -447,9 +454,14 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
447 &rd_sectors, &rd_ticks, &wr_ios, &wr_merges, 454 &rd_sectors, &rd_ticks, &wr_ios, &wr_merges,
448 &wr_sectors, &wr_ticks, &nb_current, &ticks, &aveq); 455 &wr_sectors, &wr_ticks, &nb_current, &ticks, &aveq);
449 srs_assert(ret == 14); 456 srs_assert(ret == 14);
450 -  
451 - // TODO: FIMXE: config it.  
452 - if (strcmp("sda", name) == 0) { 457 +
  458 + for (int i = 0; i < (int)conf->args.size(); i++) {
  459 + string name_ok = conf->args.at(i);
  460 +
  461 + if (strcmp(name_ok.c_str(), name) != 0) {
  462 + continue;
  463 + }
  464 +
453 r.rd_ios += rd_ios; 465 r.rd_ios += rd_ios;
454 r.rd_merges += rd_merges; 466 r.rd_merges += rd_merges;
455 r.rd_sectors += rd_sectors; 467 r.rd_sectors += rd_sectors;
@@ -461,6 +473,8 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r) @@ -461,6 +473,8 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
461 r.nb_current += nb_current; 473 r.nb_current += nb_current;
462 r.ticks += ticks; 474 r.ticks += ticks;
463 r.aveq += aveq; 475 r.aveq += aveq;
  476 +
  477 + break;
464 } 478 }
465 } 479 }
466 480
@@ -477,9 +491,18 @@ void srs_update_disk_stat() @@ -477,9 +491,18 @@ void srs_update_disk_stat()
477 if (!srs_get_disk_vmstat_stat(r)) { 491 if (!srs_get_disk_vmstat_stat(r)) {
478 return; 492 return;
479 } 493 }
  494 + if (!srs_get_disk_diskstats_stat(r)) {
  495 + return;
  496 + }
480 497
481 SrsDiskStat& o = _srs_disk_stat; 498 SrsDiskStat& o = _srs_disk_stat;
482 - if (o.ok) { 499 + if (!o.ok) {
  500 + _srs_disk_stat = r;
  501 + return;
  502 + }
  503 +
  504 + // vmstat
  505 + if (true) {
483 int64_t duration_ms = r.sample_time - o.sample_time; 506 int64_t duration_ms = r.sample_time - o.sample_time;
484 507
485 if (o.pgpgin > 0 && r.pgpgin > o.pgpgin && duration_ms > 0) { 508 if (o.pgpgin > 0 && r.pgpgin > o.pgpgin && duration_ms > 0) {
@@ -492,8 +515,6 @@ void srs_update_disk_stat() @@ -492,8 +515,6 @@ void srs_update_disk_stat()
492 r.out_KBps = (r.pgpgout - o.pgpgout) * 1000 / duration_ms; 515 r.out_KBps = (r.pgpgout - o.pgpgout) * 1000 / duration_ms;
493 } 516 }
494 } 517 }
495 -  
496 - _srs_disk_stat = r;  
497 } 518 }
498 519
499 SrsMemInfo::SrsMemInfo() 520 SrsMemInfo::SrsMemInfo()
@@ -156,6 +156,9 @@ std::string __full_conf = "" @@ -156,6 +156,9 @@ std::string __full_conf = ""
156 " # we may retrieve more than one network device. \n" 156 " # we may retrieve more than one network device. \n"
157 " # default: 0 \n" 157 " # default: 0 \n"
158 " network_device_index 0; \n" 158 " network_device_index 0; \n"
  159 + " # the device name to stat the disk iops. \n"
  160 + " # ignore the device of /proc/diskstats if not configed. \n"
  161 + " disk_device_name sda sdb xvda xvdb; \n"
159 "} \n" 162 "} \n"
160 " \n" 163 " \n"
161 "############################################################################################# \n" 164 "############################################################################################# \n"
@@ -1839,8 +1842,11 @@ VOID TEST(ConfigMainTest, ParseFullConf) @@ -1839,8 +1842,11 @@ VOID TEST(ConfigMainTest, ParseFullConf)
1839 EXPECT_EQ(9300, conf.get_heartbeat_interval()); 1842 EXPECT_EQ(9300, conf.get_heartbeat_interval());
1840 EXPECT_STREQ("http://127.0.0.1:8085/api/v1/servers", conf.get_heartbeat_url().c_str()); 1843 EXPECT_STREQ("http://127.0.0.1:8085/api/v1/servers", conf.get_heartbeat_url().c_str());
1841 EXPECT_STREQ("my-srs-device", conf.get_heartbeat_device_id().c_str()); 1844 EXPECT_STREQ("my-srs-device", conf.get_heartbeat_device_id().c_str());
1842 - EXPECT_EQ(0, conf.get_stats_network_device_index());  
1843 EXPECT_FALSE(conf.get_heartbeat_summaries()); 1845 EXPECT_FALSE(conf.get_heartbeat_summaries());
  1846 +
  1847 + EXPECT_EQ(0, conf.get_stats_network_device_index());
  1848 + ASSERT_TRUE(conf.get_stats_disk_device() != NULL);
  1849 + EXPECT_EQ(4, (int)conf.get_stats_disk_device()->args.size());
1844 1850
1845 EXPECT_TRUE(conf.get_http_api_enabled()); 1851 EXPECT_TRUE(conf.get_http_api_enabled());
1846 EXPECT_EQ(1985, conf.get_http_api_listen()); 1852 EXPECT_EQ(1985, conf.get_http_api_listen());
@@ -4633,6 +4639,16 @@ VOID TEST(ConfigMainTest, CheckConf_stats) @@ -4633,6 +4639,16 @@ VOID TEST(ConfigMainTest, CheckConf_stats)
4633 MockSrsConfig conf; 4639 MockSrsConfig conf;
4634 EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"stats{network_device_index -1;}")); 4640 EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"stats{network_device_index -1;}"));
4635 } 4641 }
  4642 +
  4643 + if (true) {
  4644 + MockSrsConfig conf;
  4645 + EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"stats{disk_device_name sda;}"));
  4646 + }
  4647 +
  4648 + if (true) {
  4649 + MockSrsConfig conf;
  4650 + EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"stats{disk_device_names sda;}"));
  4651 + }
4636 } 4652 }
4637 4653
4638 VOID TEST(ConfigMainTest, CheckConf_http_stream) 4654 VOID TEST(ConfigMainTest, CheckConf_http_stream)