From b3340218367371131fbac193e61e298a9c69ad57 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 27 Jul 2014 16:52:00 +0800 Subject: [PATCH] add config item for the stat disk device name --- trunk/conf/full.conf | 3 +++ trunk/src/app/srs_app_config.cpp | 18 ++++++++++++- trunk/src/app/srs_app_config.hpp | 6 +++++ trunk/src/app/srs_app_utility.cpp | 39 +++++++++++++++++++++------- trunk/src/utest/srs_utest_config.cpp | 18 ++++++++++++- 5 files changed, 73 insertions(+), 11 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index a209d0ce5..c9c84118b 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -88,6 +88,9 @@ stats { # we may retrieve more than one network device. # default: 0 network_device_index 0; + # the device name to stat the disk iops. + # ignore the device of /proc/diskstats if not configed. + disk_device_name sda sdb xvda xvdb; } ############################################################################################# diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 1d101dca9..3215556b3 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1249,7 +1249,7 @@ int SrsConfig::check_config() SrsConfDirective* conf = get_stats(); for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; - if (n != "network_device_index") { + if (n != "network_device_index" && n != "disk_device_name") { ret = ERROR_SYSTEM_CONFIG_INVALID; srs_error("unsupported stats directive %s, ret=%d", n.c_str(), ret); return ret; @@ -3194,6 +3194,22 @@ int SrsConfig::get_stats_network_device_index() return ::atoi(conf->arg0().c_str()); } +SrsConfDirective* SrsConfig::get_stats_disk_device() +{ + SrsConfDirective* conf = get_stats(); + + if (!conf) { + return NULL; + } + + conf = conf->get("disk_device_name"); + if (!conf || conf->args.size() == 0) { + return NULL; + } + + return conf; +} + namespace _srs_internal { SrsConfigBuffer::SrsConfigBuffer() diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 07777a1a0..7a34ab8a4 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -943,6 +943,12 @@ public: * for example, 0 means the eth0 maybe. */ virtual int get_stats_network_device_index(); + /** + * get the disk stat device name list. + * the device name configed in args of directive. + * @return the disk device name to stat. NULL if not configed. + */ + virtual SrsConfDirective* get_stats_disk_device(); }; namespace _srs_internal diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index 334189d44..f28131cdf 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -415,15 +415,22 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r) bool srs_get_disk_diskstats_stat(SrsDiskStat& r) { + r.ok = false; + r.sample_time = srs_get_system_time_ms(); + + // if disabled, ignore all devices. + SrsConfDirective* conf = _srs_config->get_stats_disk_device(); + if (conf == NULL) { + r.ok = true; + return true; + } + FILE* f = fopen("/proc/diskstats", "r"); if (f == NULL) { srs_warn("open vmstat failed, ignore"); return false; } - r.ok = false; - r.sample_time = srs_get_system_time_ms(); - static char buf[1024]; while (fgets(buf, sizeof(buf), f)) { unsigned int major = 0; @@ -447,9 +454,14 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r) &rd_sectors, &rd_ticks, &wr_ios, &wr_merges, &wr_sectors, &wr_ticks, &nb_current, &ticks, &aveq); srs_assert(ret == 14); - - // TODO: FIMXE: config it. - if (strcmp("sda", name) == 0) { + + for (int i = 0; i < (int)conf->args.size(); i++) { + string name_ok = conf->args.at(i); + + if (strcmp(name_ok.c_str(), name) != 0) { + continue; + } + r.rd_ios += rd_ios; r.rd_merges += rd_merges; r.rd_sectors += rd_sectors; @@ -461,6 +473,8 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r) r.nb_current += nb_current; r.ticks += ticks; r.aveq += aveq; + + break; } } @@ -477,9 +491,18 @@ void srs_update_disk_stat() if (!srs_get_disk_vmstat_stat(r)) { return; } + if (!srs_get_disk_diskstats_stat(r)) { + return; + } SrsDiskStat& o = _srs_disk_stat; - if (o.ok) { + if (!o.ok) { + _srs_disk_stat = r; + return; + } + + // vmstat + if (true) { int64_t duration_ms = r.sample_time - o.sample_time; if (o.pgpgin > 0 && r.pgpgin > o.pgpgin && duration_ms > 0) { @@ -492,8 +515,6 @@ void srs_update_disk_stat() r.out_KBps = (r.pgpgout - o.pgpgout) * 1000 / duration_ms; } } - - _srs_disk_stat = r; } SrsMemInfo::SrsMemInfo() diff --git a/trunk/src/utest/srs_utest_config.cpp b/trunk/src/utest/srs_utest_config.cpp index ec5c69b50..be453eff2 100644 --- a/trunk/src/utest/srs_utest_config.cpp +++ b/trunk/src/utest/srs_utest_config.cpp @@ -156,6 +156,9 @@ std::string __full_conf = "" " # we may retrieve more than one network device. \n" " # default: 0 \n" " network_device_index 0; \n" + " # the device name to stat the disk iops. \n" + " # ignore the device of /proc/diskstats if not configed. \n" + " disk_device_name sda sdb xvda xvdb; \n" "} \n" " \n" "############################################################################################# \n" @@ -1839,8 +1842,11 @@ VOID TEST(ConfigMainTest, ParseFullConf) EXPECT_EQ(9300, conf.get_heartbeat_interval()); EXPECT_STREQ("http://127.0.0.1:8085/api/v1/servers", conf.get_heartbeat_url().c_str()); EXPECT_STREQ("my-srs-device", conf.get_heartbeat_device_id().c_str()); - EXPECT_EQ(0, conf.get_stats_network_device_index()); EXPECT_FALSE(conf.get_heartbeat_summaries()); + + EXPECT_EQ(0, conf.get_stats_network_device_index()); + ASSERT_TRUE(conf.get_stats_disk_device() != NULL); + EXPECT_EQ(4, (int)conf.get_stats_disk_device()->args.size()); EXPECT_TRUE(conf.get_http_api_enabled()); EXPECT_EQ(1985, conf.get_http_api_listen()); @@ -4633,6 +4639,16 @@ VOID TEST(ConfigMainTest, CheckConf_stats) MockSrsConfig conf; EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"stats{network_device_index -1;}")); } + + if (true) { + MockSrsConfig conf; + EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"stats{disk_device_name sda;}")); + } + + if (true) { + MockSrsConfig conf; + EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"stats{disk_device_names sda;}")); + } } VOID TEST(ConfigMainTest, CheckConf_http_stream)