am a9f1ed16: Merge "metricsd: Use the metrics directory everywhere."

* commit 'a9f1ed16a839ec4cc974e66734c11a01e8d71820':
  metricsd: Use the metrics directory everywhere.
This commit is contained in:
Bertrand Simonnet 2015-09-14 22:53:08 +00:00 committed by Android Git Automerger
commit 7fb08a74fd
14 changed files with 62 additions and 69 deletions

View File

@ -19,10 +19,10 @@
namespace metrics {
static const char kMetricsDirectory[] = "/data/misc/metrics/";
static const char kMetricsEventsFilePath[] = "/data/misc/metrics/uma-events";
static const char kMetricsGUIDFilePath[] = "/data/misc/metrics/Sysinfo.GUID";
static const char kMetricsEventsFileName[] = "uma-events";
static const char kMetricsGUIDFileName[] = "Sysinfo.GUID";
static const char kMetricsServer[] = "https://clients4.google.com/uma/v2";
static const char kConsentFilePath[] = "/data/misc/metrics/enabled";
static const char kConsentFileName[] = "enabled";
static const char kDefaultVersion[] = "0.0.0.0";
// System properties used.

View File

@ -22,6 +22,7 @@
#include <unistd.h>
#include <base/compiler_specific.h>
#include <base/files/file_path.h>
#include <base/macros.h>
#include <base/memory/scoped_ptr.h>
#include <gtest/gtest_prod.h> // for FRIEND_TEST
@ -129,8 +130,7 @@ class MetricsLibrary : public MetricsLibraryInterface {
FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
void InitForTest(const std::string& uma_events_file,
const std::string& consent_file);
void InitForTest(const base::FilePath& metrics_directory);
// Sets |*result| to whether or not the |mounts_file| indicates that
// the |device_name| is currently mounted. Uses |buffer| of
@ -146,8 +146,8 @@ class MetricsLibrary : public MetricsLibraryInterface {
// Cached state of whether or not metrics were enabled.
static bool cached_enabled_;
std::string uma_events_file_;
std::string consent_file_;
base::FilePath uma_events_file_;
base::FilePath consent_file_;
DISALLOW_COPY_AND_ASSIGN(MetricsLibrary);
};

View File

@ -140,11 +140,13 @@ static int IsGuestMode() {
}
static int DumpLogs() {
printf("Metrics from %s\n\n", metrics::kMetricsEventsFilePath);
base::FilePath events_file = base::FilePath(
metrics::kMetricsDirectory).Append(metrics::kMetricsEventsFileName);
printf("Metrics from %s\n\n", events_file.value().data());
ScopedVector<metrics::MetricSample> metrics;
metrics::SerializationUtils::ReadMetricsFromFile(
metrics::kMetricsEventsFilePath, &metrics);
metrics::SerializationUtils::ReadMetricsFromFile(events_file.value(),
&metrics);
for (ScopedVector<metrics::MetricSample>::const_iterator i = metrics.begin();
i != metrics.end(); ++i) {

View File

@ -187,10 +187,10 @@ int MetricsDaemon::Run() {
void MetricsDaemon::RunUploaderTest() {
upload_service_.reset(new UploadService(
new SystemProfileCache(true, base::FilePath(config_root_)),
new SystemProfileCache(true, metrics_directory_),
metrics_lib_,
server_));
upload_service_->Init(upload_interval_, metrics_file_);
upload_service_->Init(upload_interval_, metrics_directory_);
upload_service_->UploadEvent();
}
@ -223,18 +223,16 @@ void MetricsDaemon::Init(bool testing,
const string& cpuinfo_max_freq_path,
const base::TimeDelta& upload_interval,
const string& server,
const string& metrics_file,
const string& config_root) {
const base::FilePath& metrics_directory) {
CHECK(metrics_lib);
testing_ = testing;
uploader_active_ = uploader_active;
dbus_enabled_ = dbus_enabled;
config_root_ = config_root;
metrics_directory_ = metrics_directory;
metrics_lib_ = metrics_lib;
upload_interval_ = upload_interval;
server_ = server;
metrics_file_ = metrics_file;
// Get ticks per second (HZ) on this system.
// Sysconf cannot fail, so no sanity checks are needed.
@ -337,7 +335,7 @@ int MetricsDaemon::OnInit() {
if (uploader_active_) {
upload_service_.reset(
new UploadService(new SystemProfileCache(), metrics_lib_, server_));
upload_service_->Init(upload_interval_, metrics_file_);
upload_service_->Init(upload_interval_, metrics_directory_);
}
return EX_OK;

View File

@ -50,8 +50,7 @@ class MetricsDaemon : public chromeos::DBusDaemon {
const std::string& scaling_max_freq_path,
const base::TimeDelta& upload_interval,
const std::string& server,
const std::string& metrics_file,
const std::string& config_root);
const base::FilePath& metrics_directory);
// Initializes DBus and MessageLoop variables before running the MessageLoop.
int OnInit() override;
@ -268,7 +267,7 @@ class MetricsDaemon : public chromeos::DBusDaemon {
bool dbus_enabled_;
// Root of the configuration files to use.
std::string config_root_;
base::FilePath metrics_directory_;
// The metrics library handle.
MetricsLibraryInterface* metrics_lib_;
@ -331,7 +330,6 @@ class MetricsDaemon : public chromeos::DBusDaemon {
base::TimeDelta upload_interval_;
std::string server_;
std::string metrics_file_;
scoped_ptr<UploadService> upload_service_;
};

View File

@ -75,11 +75,9 @@ int main(int argc, char** argv) {
DEFINE_string(server,
metrics::kMetricsServer,
"Server to upload the metrics to. (needs -uploader)");
DEFINE_string(metrics_file,
metrics::kMetricsEventsFilePath,
"File to use as a proxy for uploading the metrics");
DEFINE_string(config_root,
"/", "Root of the configuration files (testing only)");
DEFINE_string(metrics_directory,
metrics::kMetricsDirectory,
"Root of the configuration files (testing only)");
chromeos::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
@ -103,8 +101,7 @@ int main(int argc, char** argv) {
kCpuinfoMaxFreqPath,
base::TimeDelta::FromSeconds(FLAGS_upload_interval_secs),
FLAGS_server,
FLAGS_metrics_file,
FLAGS_config_root);
base::FilePath(FLAGS_metrics_directory));
if (FLAGS_uploader_test) {
daemon.RunUploaderTest();

View File

@ -87,8 +87,7 @@ class MetricsDaemonTest : public testing::Test {
cpu_max_freq_path_.value(),
base::TimeDelta::FromMinutes(30),
metrics::kMetricsServer,
metrics::kMetricsEventsFilePath,
"/");
temp_dir_.path());
}
// Adds a metrics library mock expectation that the specified metric

View File

@ -56,7 +56,7 @@ static const char *kCrosEventNames[] = {
time_t MetricsLibrary::cached_enabled_time_ = 0;
bool MetricsLibrary::cached_enabled_ = false;
MetricsLibrary::MetricsLibrary() : consent_file_(metrics::kConsentFilePath) {}
MetricsLibrary::MetricsLibrary() {}
MetricsLibrary::~MetricsLibrary() {}
// We take buffer and buffer_size as parameters in order to simplify testing
@ -131,19 +131,20 @@ bool MetricsLibrary::AreMetricsEnabled() {
time_t this_check_time = time(nullptr);
if (this_check_time != cached_enabled_time_) {
cached_enabled_time_ = this_check_time;
cached_enabled_ = stat(consent_file_.c_str(), &stat_buffer) >= 0;
cached_enabled_ = stat(consent_file_.value().data(), &stat_buffer) >= 0;
}
return cached_enabled_;
}
void MetricsLibrary::Init() {
uma_events_file_ = metrics::kMetricsEventsFilePath;
base::FilePath dir = base::FilePath(metrics::kMetricsDirectory);
uma_events_file_ = dir.Append(metrics::kMetricsEventsFileName);
consent_file_ = dir.Append(metrics::kConsentFileName);
}
void MetricsLibrary::InitForTest(const std::string& uma_events_file,
const std::string& consent_file) {
uma_events_file_ = uma_events_file;
consent_file_ = consent_file;
void MetricsLibrary::InitForTest(const base::FilePath& metrics_directory) {
uma_events_file_ = metrics_directory.Append(metrics::kMetricsEventsFileName);
consent_file_ = metrics_directory.Append(metrics::kConsentFileName);
}
bool MetricsLibrary::SendToUMA(const std::string& name,
@ -154,30 +155,32 @@ bool MetricsLibrary::SendToUMA(const std::string& name,
return metrics::SerializationUtils::WriteMetricToFile(
*metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
.get(),
metrics::kMetricsEventsFilePath);
uma_events_file_.value());
}
bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
int max) {
return metrics::SerializationUtils::WriteMetricToFile(
*metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
metrics::kMetricsEventsFilePath);
uma_events_file_.value());
}
bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
return metrics::SerializationUtils::WriteMetricToFile(
*metrics::MetricSample::SparseHistogramSample(name, sample).get(),
metrics::kMetricsEventsFilePath);
uma_events_file_.value());
}
bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
return metrics::SerializationUtils::WriteMetricToFile(
*metrics::MetricSample::UserActionSample(action).get(), metrics::kMetricsEventsFilePath);
*metrics::MetricSample::UserActionSample(action).get(),
uma_events_file_.value());
}
bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
return metrics::SerializationUtils::WriteMetricToFile(
*metrics::MetricSample::CrashSample(crash_kind).get(), metrics::kMetricsEventsFilePath);
*metrics::MetricSample::CrashSample(crash_kind).get(),
uma_events_file_.value());
}
bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {

View File

@ -28,19 +28,17 @@ class MetricsLibraryTest : public testing::Test {
protected:
virtual void SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
consent_file_ = temp_dir_.path().Append("consent");
uma_events_file_ = temp_dir_.path().Append("events");
lib_.InitForTest(uma_events_file_.value(), consent_file_.value());
EXPECT_EQ(0, WriteFile(uma_events_file_, "", 0));
lib_.InitForTest(temp_dir_.path());
EXPECT_EQ(0, WriteFile(lib_.uma_events_file_, "", 0));
// Defeat metrics enabled caching between tests.
lib_.cached_enabled_time_ = 0;
}
void SetMetricsConsent(bool enabled) {
if (enabled) {
ASSERT_EQ(base::WriteFile(consent_file_, "", 0), 0);
ASSERT_EQ(base::WriteFile(lib_.consent_file_, "", 0), 0);
} else {
ASSERT_TRUE(base::DeleteFile(consent_file_, false));
ASSERT_TRUE(base::DeleteFile(lib_.consent_file_, false));
}
}
@ -49,8 +47,6 @@ class MetricsLibraryTest : public testing::Test {
MetricsLibrary lib_;
base::ScopedTempDir temp_dir_;
base::FilePath consent_file_;
base::FilePath uma_events_file_;
};
TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) {

View File

@ -55,16 +55,16 @@ std::string ChannelToString(
SystemProfileCache::SystemProfileCache()
: initialized_(false),
testing_(false),
config_root_("/"),
metrics_directory_(metrics::kMetricsDirectory),
session_id_(new chromeos_metrics::PersistentInteger(
kPersistentSessionIdFilename)) {
}
SystemProfileCache::SystemProfileCache(bool testing,
const base::FilePath& config_root)
const base::FilePath& metrics_directory)
: initialized_(false),
testing_(testing),
config_root_(config_root),
metrics_directory_(metrics_directory),
session_id_(new chromeos_metrics::PersistentInteger(
kPersistentSessionIdFilename)) {
}
@ -91,9 +91,11 @@ bool SystemProfileCache::Initialize() {
channel = "";
profile_.version = metrics::kDefaultVersion;
}
profile_.client_id =
testing_ ? "client_id_test" :
GetPersistentGUID(metrics::kMetricsGUIDFilePath);
std::string guid_path = metrics_directory_.Append(
metrics::kMetricsGUIDFileName).value();
profile_.client_id = testing_ ?
"client_id_test" :
GetPersistentGUID(guid_path);
profile_.hardware_class = "unknown";
profile_.channel = ProtoChannelFromString(channel);
@ -155,7 +157,7 @@ std::string SystemProfileCache::GetPersistentGUID(
std::string SystemProfileCache::GetProperty(const std::string& name) {
if (testing_) {
std::string content;
base::ReadFileToString(config_root_.Append(name), &content);
base::ReadFileToString(metrics_directory_.Append(name), &content);
return content;
} else {
char value[PROPERTY_VALUE_MAX];

View File

@ -50,7 +50,7 @@ class SystemProfileCache : public SystemProfileSetter {
public:
SystemProfileCache();
SystemProfileCache(bool testing, const base::FilePath& config_root);
SystemProfileCache(bool testing, const base::FilePath& metrics_directory);
// Populates the ProfileSystem protobuf with system information.
bool Populate(metrics::ChromeUserMetricsExtension* metrics_proto) override;
@ -77,13 +77,13 @@ class SystemProfileCache : public SystemProfileSetter {
bool InitializeOrCheck();
// Gets a system property as a string.
// When |testing_| is true, reads the value from |config_root_|/|name|
// When |testing_| is true, reads the value from |metrics_directory_|/|name|
// instead.
std::string GetProperty(const std::string& name);
bool initialized_;
bool testing_;
base::FilePath config_root_;
base::FilePath metrics_directory_;
scoped_ptr<chromeos_metrics::PersistentInteger> session_id_;
SystemProfile profile_;
};

View File

@ -29,6 +29,7 @@
#include <base/metrics/statistics_recorder.h>
#include <base/sha1.h>
#include "constants.h"
#include "serialization/metric_sample.h"
#include "serialization/serialization_utils.h"
#include "uploader/metrics_log.h"
@ -56,9 +57,9 @@ UploadService::UploadService(SystemProfileSetter* setter,
}
void UploadService::Init(const base::TimeDelta& upload_interval,
const std::string& metrics_file) {
const base::FilePath& metrics_directory) {
base::StatisticsRecorder::Initialize();
metrics_file_ = metrics_file;
metrics_file_ = metrics_directory.Append(metrics::kMetricsEventsFileName);
if (!testing_) {
base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
@ -143,7 +144,7 @@ void UploadService::ReadMetrics() {
ScopedVector<metrics::MetricSample> vector;
metrics::SerializationUtils::ReadAndTruncateMetricsFromFile(
metrics_file_, &vector);
metrics_file_.value(), &vector);
int i = 0;
for (ScopedVector<metrics::MetricSample>::iterator it = vector.begin();

View File

@ -73,7 +73,7 @@ class UploadService : public base::HistogramFlattener {
const std::string& server);
void Init(const base::TimeDelta& upload_interval,
const std::string& metrics_file);
const base::FilePath& metrics_directory);
// Starts a new log. The log needs to be regenerated after each successful
// launch as it is destroyed when staging the log.
@ -157,7 +157,7 @@ class UploadService : public base::HistogramFlattener {
scoped_ptr<MetricsLog> current_log_;
scoped_ptr<MetricsLog> staged_log_;
std::string metrics_file_;
base::FilePath metrics_file_;
bool testing_;
};

View File

@ -42,8 +42,7 @@ class UploadServiceTest : public testing::Test {
&metrics_lib_, "", true));
upload_service_->sender_.reset(new SenderMock);
event_file_ = dir_.path().Append("event");
upload_service_->Init(base::TimeDelta::FromMinutes(30), event_file_.value());
upload_service_->Init(base::TimeDelta::FromMinutes(30), dir_.path());
upload_service_->GatherHistograms();
upload_service_->Reset();
@ -61,8 +60,6 @@ class UploadServiceTest : public testing::Test {
base::WriteFile(dir_.path().Append(name), value.data(), value.size()));
}
base::FilePath event_file_;
base::ScopedTempDir dir_;
scoped_ptr<UploadService> upload_service_;
MetricsLibraryMock metrics_lib_;