blob: 14bb9352f02c0502758031514564db7875f90282 [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 SIMONNET608e4282015-11-12 17:52:17 -080026#include "metrics_collector.h"
Darin Petkov65b01462010-04-14 13:32:20 -070027
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070028
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070029// Returns the path to the disk stats in the sysfs. Returns the null string if
30// it cannot find the disk stats file.
31static
32const std::string MetricsMainDiskStatsPath() {
33 char dev_path_cstr[PATH_MAX];
34 std::string dev_prefix = "/dev/block/";
35 std::string dev_path;
36
37 int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true);
38 if (ret != 0) {
39 LOG(WARNING) << "error " << ret << " determining root device";
40 return "";
41 }
42 dev_path = dev_path_cstr;
43 // Check that rootdev begins with "/dev/block/".
Alex Vakulenkoea05ff92016-01-20 07:53:57 -080044 if (!base::StartsWith(dev_path, dev_prefix,
45 base::CompareCase::INSENSITIVE_ASCII)) {
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070046 LOG(WARNING) << "unexpected root device " << dev_path;
47 return "";
48 }
49 return "/sys/class/block/" + dev_path.substr(dev_prefix.length()) + "/stat";
50}
51
Darin Petkov65b01462010-04-14 13:32:20 -070052int main(int argc, char** argv) {
Bertrand SIMONNET0a1119f2015-11-06 13:25:41 -080053 DEFINE_bool(foreground, false, "Don't daemonize");
Steve Fung67906c62014-10-06 15:15:30 -070054
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -080055 DEFINE_string(private_directory, metrics::kMetricsCollectorDirectory,
56 "Path to the private directory used by metrics_collector "
57 "(testing only)");
58 DEFINE_string(shared_directory, metrics::kSharedMetricsDirectory,
59 "Path to the shared metrics directory, used by "
60 "metrics_collector, metricsd and all metrics clients "
61 "(testing only)");
Steve Fung67906c62014-10-06 15:15:30 -070062
Bertrand SIMONNET0a1119f2015-11-06 13:25:41 -080063 DEFINE_bool(logtostderr, false, "Log to standard error");
64 DEFINE_bool(logtosyslog, false, "Log to syslog");
65
Alex Vakulenko74dc6242015-10-13 09:23:34 -070066 brillo::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
Luigi Semenzato254d22e2014-04-21 14:33:32 -070067
Bertrand SIMONNET0a1119f2015-11-06 13:25:41 -080068 int logging_location = (FLAGS_foreground ? brillo::kLogToStderr
69 : brillo::kLogToSyslog);
70 if (FLAGS_logtosyslog)
71 logging_location = brillo::kLogToSyslog;
Steve Funge86591e2014-12-01 13:38:21 -080072
Bertrand SIMONNET0a1119f2015-11-06 13:25:41 -080073 if (FLAGS_logtostderr)
74 logging_location = brillo::kLogToStderr;
75
76 // Also log to stderr when not running as daemon.
77 brillo::InitLog(logging_location | brillo::kLogHeader);
78
79 if (FLAGS_logtostderr && FLAGS_logtosyslog) {
80 LOG(ERROR) << "only one of --logtosyslog and --logtostderr can be set";
81 return 1;
82 }
83
84 if (!FLAGS_foreground && daemon(0, 0) != 0) {
Steve Funge86591e2014-12-01 13:38:21 -080085 return errno;
86 }
87
Darin Petkov11b8eb32010-05-18 11:00:59 -070088 MetricsLibrary metrics_lib;
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -070089 metrics_lib.InitWithNoCaching();
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080090 MetricsCollector daemon;
91 daemon.Init(false,
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070092 &metrics_lib,
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070093 MetricsMainDiskStatsPath(),
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -080094 base::FilePath(FLAGS_private_directory),
95 base::FilePath(FLAGS_shared_directory));
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070096
Steve Funge86591e2014-12-01 13:38:21 -080097 daemon.Run();
Darin Petkov65b01462010-04-14 13:32:20 -070098}