From 0f132bba6fbf1b8ce5a3e0927f39ba41ec4aebd1 Mon Sep 17 00:00:00 2001 From: Luigi Semenzato Date: Mon, 28 Feb 2011 11:17:43 -0800 Subject: [PATCH] Find device-dependent disk stats file, and skip disk stats if not available. Change-Id: I03afb85e3357dd4c2cf5effd98b194c71d77c71d BUG=12171 TEST=unit tested Review URL: http://codereview.chromium.org/6541007 --- metrics/Makefile | 3 ++- metrics/metrics_daemon.cc | 14 ++++++++------ metrics/metrics_daemon.h | 4 ++-- metrics/metrics_daemon_main.cc | 30 +++++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/metrics/Makefile b/metrics/Makefile index 5e3ed14bb..032ea73bf 100644 --- a/metrics/Makefile +++ b/metrics/Makefile @@ -38,7 +38,8 @@ TESTLIB_OBJS = \ metrics_library_test.o TESTCOUNTER_LIBS = -lgmock -lgtest -lbase -lrt -lpthread -lglib-2.0 -DAEMON_LDFLAGS = $(LDFLAGS) $(LDCONFIG) -lrt -lbase -lpthread -lgflags -lglib-2.0 +DAEMON_LDFLAGS = $(LDFLAGS) $(LDCONFIG) -lrt -lbase -lpthread -lgflags \ + -lglib-2.0 -lrootdev TESTDAEMON_LIBS = -lgmock -lgtest TESTLIB_LIBS = -lgtest -lbase -lrt -lpthread -lglib-2.0 diff --git a/metrics/metrics_daemon.cc b/metrics/metrics_daemon.cc index 1e15e1dde..4820590d1 100644 --- a/metrics/metrics_daemon.cc +++ b/metrics/metrics_daemon.cc @@ -146,8 +146,7 @@ MetricsDaemon::MetricsDaemon() session_state_(kUnknownSessionState), user_active_(false), usemon_interval_(0), - usemon_source_(NULL), - diskstats_path_(NULL) {} + usemon_source_(NULL) {} MetricsDaemon::~MetricsDaemon() { DeleteFrequencyCounters(); @@ -215,7 +214,7 @@ void MetricsDaemon::ConfigureCrashFrequencyReporter( } void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib, - const char* diskstats_path) { + string diskstats_path) { testing_ = testing; DCHECK(metrics_lib != NULL); metrics_lib_ = metrics_lib; @@ -243,8 +242,11 @@ void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib, ConfigureCrashFrequencyReporter(kMetricUserCrashesDailyName); ConfigureCrashFrequencyReporter(kMetricUserCrashesWeeklyName); - diskstats_path_ = diskstats_path; - DiskStatsReporterInit(); + // Don't attempt to collect disk stats if there is no disk stats file. + if (!diskstats_path.empty()) { + diskstats_path_ = diskstats_path; + DiskStatsReporterInit(); + } // Don't setup D-Bus and GLib in test mode. if (testing) @@ -541,7 +543,7 @@ void MetricsDaemon::DiskStatsReadStats(long int* read_sectors, int nchars; int nitems; char line[200]; - int file = HANDLE_EINTR(open(diskstats_path_, O_RDONLY)); + int file = HANDLE_EINTR(open(diskstats_path_.c_str(), O_RDONLY)); if (file < 0) { PLOG(WARNING) << "cannot open " << diskstats_path_; return; diff --git a/metrics/metrics_daemon.h b/metrics/metrics_daemon.h index dd613227d..525251829 100644 --- a/metrics/metrics_daemon.h +++ b/metrics/metrics_daemon.h @@ -30,7 +30,7 @@ class MetricsDaemon { // Initializes. void Init(bool testing, MetricsLibraryInterface* metrics_lib, - const char* diskstats_path); + std::string diskstats_path); // Does all the work. If |run_as_daemon| is true, daemonizes by // forking. @@ -304,7 +304,7 @@ class MetricsDaemon { long int write_sectors_; DiskStatsState diskstats_state_; - const char* diskstats_path_; + std::string diskstats_path_; }; #endif // METRICS_DAEMON_H_ diff --git a/metrics/metrics_daemon_main.cc b/metrics/metrics_daemon_main.cc index 899256cff..1ee061177 100644 --- a/metrics/metrics_daemon_main.cc +++ b/metrics/metrics_daemon_main.cc @@ -3,20 +3,44 @@ // found in the LICENSE file. +#include +#include #include +#include #include "metrics_daemon.h" DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)"); -// Path to disk stats. This may be system dependent. -const char kMetricsMainDiskStatsPath[] = "/sys/class/block/sda/stat"; +// Return the path to the disk stats in the sysfs. +static +const std::string MetricsMainDiskStatsPath() { + char dev_path_cstr[PATH_MAX]; + std::string dev_prefix = "/dev/"; + std::string dev_path; + std::string dev_name; + + int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true); + if (ret != 0) { + LOG(WARNING) << "error " << ret << " determining root device"; + return ""; + } + dev_path = dev_path_cstr; + // Check that rootdev begins with "/dev/". + if (!StartsWithASCII(dev_path, dev_prefix, false)) { + LOG(WARNING) << "unexpected root device " << dev_path; + return ""; + } + // Get the device name, e.g. "sda" from "/dev/sda". + dev_name = dev_path.substr(dev_prefix.length()); + return "/sys/class/block/" + dev_name + "/stat"; +} int main(int argc, char** argv) { google::ParseCommandLineFlags(&argc, &argv, true); MetricsLibrary metrics_lib; metrics_lib.Init(); MetricsDaemon daemon; - daemon.Init(false, &metrics_lib, kMetricsMainDiskStatsPath); + daemon.Init(false, &metrics_lib, MetricsMainDiskStatsPath()); daemon.Run(FLAGS_daemon); }