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> |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame^] | 7 | #include <base/strings/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 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 13 | const char kScalingMaxFreqPath[] = |
| 14 | "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"; |
| 15 | const char kCpuinfoMaxFreqPath[] = |
| 16 | "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; |
| 17 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 18 | DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)"); |
| 19 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 20 | // Returns the path to the disk stats in the sysfs. Returns the null string if |
| 21 | // it cannot find the disk stats file. |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 22 | static |
| 23 | const std::string MetricsMainDiskStatsPath() { |
| 24 | char dev_path_cstr[PATH_MAX]; |
| 25 | std::string dev_prefix = "/dev/"; |
| 26 | std::string dev_path; |
| 27 | std::string dev_name; |
| 28 | |
| 29 | int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true); |
| 30 | if (ret != 0) { |
| 31 | LOG(WARNING) << "error " << ret << " determining root device"; |
| 32 | return ""; |
| 33 | } |
| 34 | dev_path = dev_path_cstr; |
| 35 | // Check that rootdev begins with "/dev/". |
| 36 | if (!StartsWithASCII(dev_path, dev_prefix, false)) { |
| 37 | LOG(WARNING) << "unexpected root device " << dev_path; |
| 38 | return ""; |
| 39 | } |
| 40 | // Get the device name, e.g. "sda" from "/dev/sda". |
| 41 | dev_name = dev_path.substr(dev_prefix.length()); |
| 42 | return "/sys/class/block/" + dev_name + "/stat"; |
| 43 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 44 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 45 | int main(int argc, char** argv) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 46 | google::ParseCommandLineFlags(&argc, &argv, true); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 47 | MetricsLibrary metrics_lib; |
| 48 | metrics_lib.Init(); |
| 49 | MetricsDaemon daemon; |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 50 | daemon.Init(false, &metrics_lib, MetricsMainDiskStatsPath(), |
| 51 | "/proc/vmstat", kScalingMaxFreqPath, kCpuinfoMaxFreqPath); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 52 | daemon.Run(FLAGS_daemon); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 53 | } |