blob: 643a2bf35bd28aedcf0558bedaed88b790ab7599 [file] [log] [blame]
Darin Petkov65b01462010-04-14 13:32:20 -07001// 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 Semenzato0f132bb2011-02-28 11:17:43 -08006#include <base/logging.h>
7#include <base/string_util.h>
Darin Petkov65b01462010-04-14 13:32:20 -07008#include <gflags/gflags.h>
Luigi Semenzato0f132bb2011-02-28 11:17:43 -08009#include <rootdev/rootdev.h>
Darin Petkov65b01462010-04-14 13:32:20 -070010
11#include "metrics_daemon.h"
12
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070013const char kScalingMaxFreqPath[] =
14 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq";
15const char kCpuinfoMaxFreqPath[] =
16 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
17
Darin Petkov65b01462010-04-14 13:32:20 -070018DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)");
19
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070020// Returns the path to the disk stats in the sysfs. Returns the null string if
21// it cannot find the disk stats file.
Luigi Semenzato0f132bb2011-02-28 11:17:43 -080022static
23const 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 Semenzatoc88e42d2011-02-17 10:21:16 -080044
Darin Petkov65b01462010-04-14 13:32:20 -070045int main(int argc, char** argv) {
Darin Petkov65b01462010-04-14 13:32:20 -070046 google::ParseCommandLineFlags(&argc, &argv, true);
Darin Petkov11b8eb32010-05-18 11:00:59 -070047 MetricsLibrary metrics_lib;
48 metrics_lib.Init();
49 MetricsDaemon daemon;
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070050 daemon.Init(false, &metrics_lib, MetricsMainDiskStatsPath(),
51 "/proc/vmstat", kScalingMaxFreqPath, kCpuinfoMaxFreqPath);
Darin Petkov11b8eb32010-05-18 11:00:59 -070052 daemon.Run(FLAGS_daemon);
Darin Petkov65b01462010-04-14 13:32:20 -070053}