Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 6 | #include <base/logging.h> |
| 7 | #include <base/string_util.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 8 | #include <gflags/gflags.h> |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 9 | #include <rootdev/rootdev.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 10 | |
| 11 | #include "metrics_daemon.h" |
| 12 | |
| 13 | DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)"); |
| 14 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame^] | 15 | // Returns the path to the disk stats in the sysfs. Returns the null string if |
| 16 | // it cannot find the disk stats file. |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 17 | static |
| 18 | const std::string MetricsMainDiskStatsPath() { |
| 19 | char dev_path_cstr[PATH_MAX]; |
| 20 | std::string dev_prefix = "/dev/"; |
| 21 | std::string dev_path; |
| 22 | std::string dev_name; |
| 23 | |
| 24 | int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true); |
| 25 | if (ret != 0) { |
| 26 | LOG(WARNING) << "error " << ret << " determining root device"; |
| 27 | return ""; |
| 28 | } |
| 29 | dev_path = dev_path_cstr; |
| 30 | // Check that rootdev begins with "/dev/". |
| 31 | if (!StartsWithASCII(dev_path, dev_prefix, false)) { |
| 32 | LOG(WARNING) << "unexpected root device " << dev_path; |
| 33 | return ""; |
| 34 | } |
| 35 | // Get the device name, e.g. "sda" from "/dev/sda". |
| 36 | dev_name = dev_path.substr(dev_prefix.length()); |
| 37 | return "/sys/class/block/" + dev_name + "/stat"; |
| 38 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 39 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 40 | int main(int argc, char** argv) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 41 | google::ParseCommandLineFlags(&argc, &argv, true); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 42 | MetricsLibrary metrics_lib; |
| 43 | metrics_lib.Init(); |
| 44 | MetricsDaemon daemon; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame^] | 45 | daemon.Init(false, &metrics_lib, MetricsMainDiskStatsPath(), "/proc/vmstat"); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 46 | daemon.Run(FLAGS_daemon); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 47 | } |