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
This commit is contained in:
Luigi Semenzato 2011-02-28 11:17:43 -08:00
parent 8d3305eb92
commit 0f132bba6f
4 changed files with 39 additions and 12 deletions

View File

@ -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

View File

@ -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;

View File

@ -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_

View File

@ -3,20 +3,44 @@
// found in the LICENSE file.
#include <base/logging.h>
#include <base/string_util.h>
#include <gflags/gflags.h>
#include <rootdev/rootdev.h>
#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);
}