Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 16 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 17 | #include <base/at_exit.h> |
Luigi Semenzato | 254d22e | 2014-04-21 14:33:32 -0700 | [diff] [blame] | 18 | #include <base/command_line.h> |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 19 | #include <base/logging.h> |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 20 | #include <base/strings/string_util.h> |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 21 | #include <brillo/flag_helper.h> |
| 22 | #include <brillo/syslog_logging.h> |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 23 | #include <rootdev.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 24 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 25 | #include "constants.h" |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 26 | #include "metrics_daemon.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 27 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 28 | const char kScalingMaxFreqPath[] = |
| 29 | "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"; |
| 30 | const char kCpuinfoMaxFreqPath[] = |
| 31 | "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; |
| 32 | |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 33 | // Returns the path to the disk stats in the sysfs. Returns the null string if |
| 34 | // it cannot find the disk stats file. |
| 35 | static |
| 36 | const 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 Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 55 | int main(int argc, char** argv) { |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 56 | 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 SIMONNET | fec4d2c | 2015-08-05 16:04:14 -0700 | [diff] [blame] | 67 | // Enable dbus. |
| 68 | DEFINE_bool(withdbus, true, "Enable dbus"); |
| 69 | |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 70 | // 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 SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 76 | metrics::kMetricsServer, |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 77 | "Server to upload the metrics to. (needs -uploader)"); |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 78 | DEFINE_string(metrics_directory, |
| 79 | metrics::kMetricsDirectory, |
| 80 | "Root of the configuration files (testing only)"); |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 81 | |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 82 | brillo::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon"); |
Luigi Semenzato | 254d22e | 2014-04-21 14:33:32 -0700 | [diff] [blame] | 83 | |
| 84 | // Also log to stderr when not running as daemon. |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 85 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader | |
| 86 | (FLAGS_daemon ? 0 : brillo::kLogToStderr)); |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 87 | |
| 88 | if (FLAGS_daemon && daemon(0, 0) != 0) { |
| 89 | return errno; |
| 90 | } |
| 91 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 92 | MetricsLibrary metrics_lib; |
Bertrand SIMONNET | a5b40d0 | 2015-10-02 16:40:51 -0700 | [diff] [blame] | 93 | metrics_lib.InitWithNoCaching(); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 94 | MetricsDaemon daemon; |
Bertrand SIMONNET | eb815be | 2014-09-11 07:38:17 -0700 | [diff] [blame] | 95 | daemon.Init(FLAGS_uploader_test, |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 96 | FLAGS_uploader | FLAGS_uploader_test, |
Bertrand SIMONNET | fec4d2c | 2015-08-05 16:04:14 -0700 | [diff] [blame] | 97 | FLAGS_withdbus, |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 98 | &metrics_lib, |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 99 | MetricsMainDiskStatsPath(), |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 100 | kScalingMaxFreqPath, |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 101 | kCpuinfoMaxFreqPath, |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame] | 102 | base::TimeDelta::FromSeconds(FLAGS_upload_interval_secs), |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 103 | FLAGS_server, |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 104 | base::FilePath(FLAGS_metrics_directory)); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 105 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 106 | if (FLAGS_uploader_test) { |
| 107 | daemon.RunUploaderTest(); |
| 108 | return 0; |
| 109 | } |
| 110 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 111 | daemon.Run(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 112 | } |