2015-08-10 22:18:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2010-04-14 20:32:20 +00:00
|
|
|
|
2014-06-25 21:38:07 +00:00
|
|
|
#include <base/at_exit.h>
|
2014-04-21 21:33:32 +00:00
|
|
|
#include <base/command_line.h>
|
2011-02-28 19:17:43 +00:00
|
|
|
#include <base/logging.h>
|
2014-02-06 07:26:25 +00:00
|
|
|
#include <base/strings/string_util.h>
|
2015-10-13 16:23:34 +00:00
|
|
|
#include <brillo/flag_helper.h>
|
|
|
|
#include <brillo/syslog_logging.h>
|
2015-09-08 17:13:35 +00:00
|
|
|
#include <rootdev.h>
|
2010-04-14 20:32:20 +00:00
|
|
|
|
2015-08-04 21:04:51 +00:00
|
|
|
#include "constants.h"
|
2015-07-28 22:38:14 +00:00
|
|
|
#include "metrics_daemon.h"
|
2010-04-14 20:32:20 +00:00
|
|
|
|
2013-05-07 23:55:00 +00:00
|
|
|
const char kScalingMaxFreqPath[] =
|
|
|
|
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq";
|
|
|
|
const char kCpuinfoMaxFreqPath[] =
|
|
|
|
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
|
|
|
|
|
2015-09-08 17:13:35 +00:00
|
|
|
// Returns the path to the disk stats in the sysfs. Returns the null string if
|
|
|
|
// it cannot find the disk stats file.
|
|
|
|
static
|
|
|
|
const std::string MetricsMainDiskStatsPath() {
|
|
|
|
char dev_path_cstr[PATH_MAX];
|
|
|
|
std::string dev_prefix = "/dev/block/";
|
|
|
|
std::string dev_path;
|
|
|
|
|
|
|
|
int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true);
|
|
|
|
if (ret != 0) {
|
|
|
|
LOG(WARNING) << "error " << ret << " determining root device";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
dev_path = dev_path_cstr;
|
|
|
|
// Check that rootdev begins with "/dev/block/".
|
|
|
|
if (!base::StartsWithASCII(dev_path, dev_prefix, false)) {
|
|
|
|
LOG(WARNING) << "unexpected root device " << dev_path;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return "/sys/class/block/" + dev_path.substr(dev_prefix.length()) + "/stat";
|
|
|
|
}
|
|
|
|
|
2010-04-14 20:32:20 +00:00
|
|
|
int main(int argc, char** argv) {
|
2014-10-06 22:15:30 +00:00
|
|
|
DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)");
|
|
|
|
|
|
|
|
// The uploader is disabled by default on ChromeOS as Chrome is responsible
|
|
|
|
// for sending the metrics.
|
|
|
|
DEFINE_bool(uploader, false, "activate the uploader");
|
|
|
|
|
|
|
|
// Upload the metrics once and exit. (used for testing)
|
|
|
|
DEFINE_bool(uploader_test,
|
|
|
|
false,
|
|
|
|
"run the uploader once and exit");
|
|
|
|
|
2015-08-05 23:04:14 +00:00
|
|
|
// Enable dbus.
|
|
|
|
DEFINE_bool(withdbus, true, "Enable dbus");
|
|
|
|
|
2014-10-06 22:15:30 +00:00
|
|
|
// Upload Service flags.
|
|
|
|
DEFINE_int32(upload_interval_secs,
|
|
|
|
1800,
|
|
|
|
"Interval at which metrics_daemon sends the metrics. (needs "
|
|
|
|
"-uploader)");
|
|
|
|
DEFINE_string(server,
|
2015-08-04 21:04:51 +00:00
|
|
|
metrics::kMetricsServer,
|
2014-10-06 22:15:30 +00:00
|
|
|
"Server to upload the metrics to. (needs -uploader)");
|
2015-09-09 17:38:20 +00:00
|
|
|
DEFINE_string(metrics_directory,
|
|
|
|
metrics::kMetricsDirectory,
|
|
|
|
"Root of the configuration files (testing only)");
|
2014-10-06 22:15:30 +00:00
|
|
|
|
2015-10-13 16:23:34 +00:00
|
|
|
brillo::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
|
2014-04-21 21:33:32 +00:00
|
|
|
|
|
|
|
// Also log to stderr when not running as daemon.
|
2015-10-13 16:23:34 +00:00
|
|
|
brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader |
|
|
|
|
(FLAGS_daemon ? 0 : brillo::kLogToStderr));
|
2014-12-01 21:38:21 +00:00
|
|
|
|
|
|
|
if (FLAGS_daemon && daemon(0, 0) != 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2010-05-18 18:00:59 +00:00
|
|
|
MetricsLibrary metrics_lib;
|
2015-10-02 23:40:51 +00:00
|
|
|
metrics_lib.InitWithNoCaching();
|
2010-05-18 18:00:59 +00:00
|
|
|
MetricsDaemon daemon;
|
2014-09-11 14:38:17 +00:00
|
|
|
daemon.Init(FLAGS_uploader_test,
|
2014-06-25 21:38:07 +00:00
|
|
|
FLAGS_uploader | FLAGS_uploader_test,
|
2015-08-05 23:04:14 +00:00
|
|
|
FLAGS_withdbus,
|
2014-06-25 21:38:07 +00:00
|
|
|
&metrics_lib,
|
2015-09-08 17:13:35 +00:00
|
|
|
MetricsMainDiskStatsPath(),
|
2014-06-25 21:38:07 +00:00
|
|
|
kScalingMaxFreqPath,
|
2014-10-06 22:15:30 +00:00
|
|
|
kCpuinfoMaxFreqPath,
|
2014-10-09 17:14:13 +00:00
|
|
|
base::TimeDelta::FromSeconds(FLAGS_upload_interval_secs),
|
2014-10-06 22:15:30 +00:00
|
|
|
FLAGS_server,
|
2015-09-09 17:38:20 +00:00
|
|
|
base::FilePath(FLAGS_metrics_directory));
|
2014-06-25 21:38:07 +00:00
|
|
|
|
|
|
|
if (FLAGS_uploader_test) {
|
|
|
|
daemon.RunUploaderTest();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-01 21:38:21 +00:00
|
|
|
daemon.Run();
|
2010-04-14 20:32:20 +00:00
|
|
|
}
|