blob: 50c279dfa61f6fcfd8b2633eca0d1346fc2ff871 [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Darin Petkov65b01462010-04-14 13:32:20 -070016
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070017#include <base/at_exit.h>
Luigi Semenzato254d22e2014-04-21 14:33:32 -070018#include <base/command_line.h>
Luigi Semenzato0f132bb2011-02-28 11:17:43 -080019#include <base/logging.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080020#include <base/strings/string_util.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070021#include <brillo/flag_helper.h>
22#include <brillo/syslog_logging.h>
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070023#include <rootdev.h>
Darin Petkov65b01462010-04-14 13:32:20 -070024
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070025#include "constants.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070026#include "metrics_daemon.h"
Darin Petkov65b01462010-04-14 13:32:20 -070027
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070028const char kScalingMaxFreqPath[] =
29 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq";
30const char kCpuinfoMaxFreqPath[] =
31 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
32
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070033// Returns the path to the disk stats in the sysfs. Returns the null string if
34// it cannot find the disk stats file.
35static
36const std::string MetricsMainDiskStatsPath() {
37 char dev_path_cstr[PATH_MAX];
38 std::string dev_prefix = "/dev/block/";
39 std::string dev_path;
40
41 int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true);
42 if (ret != 0) {
43 LOG(WARNING) << "error " << ret << " determining root device";
44 return "";
45 }
46 dev_path = dev_path_cstr;
47 // Check that rootdev begins with "/dev/block/".
48 if (!base::StartsWithASCII(dev_path, dev_prefix, false)) {
49 LOG(WARNING) << "unexpected root device " << dev_path;
50 return "";
51 }
52 return "/sys/class/block/" + dev_path.substr(dev_prefix.length()) + "/stat";
53}
54
Darin Petkov65b01462010-04-14 13:32:20 -070055int main(int argc, char** argv) {
Steve Fung67906c62014-10-06 15:15:30 -070056 DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)");
57
58 // The uploader is disabled by default on ChromeOS as Chrome is responsible
59 // for sending the metrics.
60 DEFINE_bool(uploader, false, "activate the uploader");
61
62 // Upload the metrics once and exit. (used for testing)
63 DEFINE_bool(uploader_test,
64 false,
65 "run the uploader once and exit");
66
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -070067 // Enable dbus.
68 DEFINE_bool(withdbus, true, "Enable dbus");
69
Steve Fung67906c62014-10-06 15:15:30 -070070 // Upload Service flags.
71 DEFINE_int32(upload_interval_secs,
72 1800,
73 "Interval at which metrics_daemon sends the metrics. (needs "
74 "-uploader)");
75 DEFINE_string(server,
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070076 metrics::kMetricsServer,
Steve Fung67906c62014-10-06 15:15:30 -070077 "Server to upload the metrics to. (needs -uploader)");
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070078 DEFINE_string(metrics_directory,
79 metrics::kMetricsDirectory,
80 "Root of the configuration files (testing only)");
Steve Fung67906c62014-10-06 15:15:30 -070081
Alex Vakulenko74dc6242015-10-13 09:23:34 -070082 brillo::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
Luigi Semenzato254d22e2014-04-21 14:33:32 -070083
84 // Also log to stderr when not running as daemon.
Alex Vakulenko74dc6242015-10-13 09:23:34 -070085 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader |
86 (FLAGS_daemon ? 0 : brillo::kLogToStderr));
Steve Funge86591e2014-12-01 13:38:21 -080087
88 if (FLAGS_daemon && daemon(0, 0) != 0) {
89 return errno;
90 }
91
Darin Petkov11b8eb32010-05-18 11:00:59 -070092 MetricsLibrary metrics_lib;
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -070093 metrics_lib.InitWithNoCaching();
Darin Petkov11b8eb32010-05-18 11:00:59 -070094 MetricsDaemon daemon;
Bertrand SIMONNETeb815be2014-09-11 07:38:17 -070095 daemon.Init(FLAGS_uploader_test,
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070096 FLAGS_uploader | FLAGS_uploader_test,
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -070097 FLAGS_withdbus,
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070098 &metrics_lib,
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070099 MetricsMainDiskStatsPath(),
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700100 kScalingMaxFreqPath,
Steve Fung67906c62014-10-06 15:15:30 -0700101 kCpuinfoMaxFreqPath,
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700102 base::TimeDelta::FromSeconds(FLAGS_upload_interval_secs),
Steve Fung67906c62014-10-06 15:15:30 -0700103 FLAGS_server,
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700104 base::FilePath(FLAGS_metrics_directory));
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700105
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700106 if (FLAGS_uploader_test) {
107 daemon.RunUploaderTest();
108 return 0;
109 }
110
Steve Funge86591e2014-12-01 13:38:21 -0800111 daemon.Run();
Darin Petkov65b01462010-04-14 13:32:20 -0700112}