2015-08-11 06:18:00 +08: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-15 04:32:20 +08:00
|
|
|
|
2014-07-10 07:34:29 +08:00
|
|
|
#include "metrics/metrics_library.h"
|
2010-04-15 04:32:20 +08:00
|
|
|
|
2014-05-14 06:16:24 +08:00
|
|
|
#include <base/logging.h>
|
|
|
|
#include <base/strings/stringprintf.h>
|
2010-04-15 04:32:20 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/file.h>
|
2010-08-27 09:35:06 +08:00
|
|
|
#include <sys/stat.h>
|
2010-04-16 07:40:23 +08:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2010-04-15 04:32:20 +08:00
|
|
|
|
2015-08-05 05:04:51 +08:00
|
|
|
#include "constants.h"
|
2015-07-29 06:38:14 +08:00
|
|
|
#include "serialization/metric_sample.h"
|
|
|
|
#include "serialization/serialization_utils.h"
|
2013-02-15 04:15:35 +08:00
|
|
|
|
2013-03-14 01:53:55 +08:00
|
|
|
static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
|
|
|
|
static const int kCrosEventHistogramMax = 100;
|
2010-04-15 04:32:20 +08:00
|
|
|
|
2013-04-01 14:27:39 +08:00
|
|
|
/* Add new cros events here.
|
|
|
|
*
|
|
|
|
* The index of the event is sent in the message, so please do not
|
|
|
|
* reorder the names.
|
|
|
|
*/
|
|
|
|
static const char *kCrosEventNames[] = {
|
|
|
|
"ModemManagerCommandSendFailure", // 0
|
|
|
|
"HwWatchdogReboot", // 1
|
|
|
|
"Cras.NoCodecsFoundAtBoot", // 2
|
2013-07-20 05:09:50 +08:00
|
|
|
"Chaps.DatabaseCorrupted", // 3
|
|
|
|
"Chaps.DatabaseRepairFailure", // 4
|
|
|
|
"Chaps.DatabaseCreateFailure", // 5
|
2013-07-27 04:37:20 +08:00
|
|
|
"Attestation.OriginSpecificExhausted", // 6
|
2013-11-12 06:24:44 +08:00
|
|
|
"SpringPowerSupply.Original.High", // 7
|
|
|
|
"SpringPowerSupply.Other.High", // 8
|
2013-11-14 08:28:43 +08:00
|
|
|
"SpringPowerSupply.Original.Low", // 9
|
|
|
|
"SpringPowerSupply.ChargerIdle", // 10
|
2014-02-08 08:51:15 +08:00
|
|
|
"TPM.NonZeroDictionaryAttackCounter", // 11
|
2015-03-20 08:18:24 +08:00
|
|
|
"TPM.EarlyResetDuringCommand", // 12
|
2013-04-01 14:27:39 +08:00
|
|
|
};
|
|
|
|
|
2015-09-10 01:38:20 +08:00
|
|
|
MetricsLibrary::MetricsLibrary() {}
|
2014-03-10 12:39:08 +08:00
|
|
|
MetricsLibrary::~MetricsLibrary() {}
|
|
|
|
|
2010-10-02 06:38:42 +08:00
|
|
|
// We take buffer and buffer_size as parameters in order to simplify testing
|
|
|
|
// of various alignments of the |device_name| with |buffer_size|.
|
|
|
|
bool MetricsLibrary::IsDeviceMounted(const char* device_name,
|
|
|
|
const char* mounts_file,
|
|
|
|
char* buffer,
|
|
|
|
int buffer_size,
|
|
|
|
bool* result) {
|
2014-08-29 05:59:56 +08:00
|
|
|
if (buffer == nullptr || buffer_size < 1)
|
2010-10-02 06:38:42 +08:00
|
|
|
return false;
|
|
|
|
int mounts_fd = open(mounts_file, O_RDONLY);
|
|
|
|
if (mounts_fd < 0)
|
|
|
|
return false;
|
|
|
|
// match_offset describes:
|
|
|
|
// -1 -- not beginning of line
|
|
|
|
// 0..strlen(device_name)-1 -- this offset in device_name is next to match
|
|
|
|
// strlen(device_name) -- matched full name, just need a space.
|
|
|
|
int match_offset = 0;
|
|
|
|
bool match = false;
|
|
|
|
while (!match) {
|
|
|
|
int read_size = read(mounts_fd, buffer, buffer_size);
|
|
|
|
if (read_size <= 0) {
|
|
|
|
if (errno == -EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < read_size; ++i) {
|
|
|
|
if (buffer[i] == '\n') {
|
|
|
|
match_offset = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (match_offset < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (device_name[match_offset] == '\0') {
|
|
|
|
if (buffer[i] == ' ') {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
match_offset = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == device_name[match_offset]) {
|
|
|
|
++match_offset;
|
|
|
|
} else {
|
|
|
|
match_offset = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(mounts_fd);
|
|
|
|
*result = match;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MetricsLibrary::IsGuestMode() {
|
|
|
|
char buffer[256];
|
|
|
|
bool result = false;
|
|
|
|
if (!IsDeviceMounted("guestfs",
|
|
|
|
"/proc/mounts",
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
&result)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-27 03:22:22 +08:00
|
|
|
return result && (access("/var/run/state/logged-in", F_OK) == 0);
|
2010-10-02 06:38:42 +08:00
|
|
|
}
|
|
|
|
|
2010-08-27 09:35:06 +08:00
|
|
|
bool MetricsLibrary::AreMetricsEnabled() {
|
2011-08-02 22:10:49 +08:00
|
|
|
static struct stat stat_buffer;
|
2014-08-29 05:59:56 +08:00
|
|
|
time_t this_check_time = time(nullptr);
|
2015-10-03 07:40:51 +08:00
|
|
|
if (!use_caching_ || this_check_time != cached_enabled_time_) {
|
2010-08-27 09:35:06 +08:00
|
|
|
cached_enabled_time_ = this_check_time;
|
2015-09-10 01:38:20 +08:00
|
|
|
cached_enabled_ = stat(consent_file_.value().data(), &stat_buffer) >= 0;
|
2010-08-27 09:35:06 +08:00
|
|
|
}
|
|
|
|
return cached_enabled_;
|
|
|
|
}
|
2010-05-19 02:00:59 +08:00
|
|
|
|
2010-05-13 04:05:45 +08:00
|
|
|
void MetricsLibrary::Init() {
|
2015-09-10 01:38:20 +08:00
|
|
|
base::FilePath dir = base::FilePath(metrics::kMetricsDirectory);
|
|
|
|
uma_events_file_ = dir.Append(metrics::kMetricsEventsFileName);
|
|
|
|
consent_file_ = dir.Append(metrics::kConsentFileName);
|
2015-09-29 02:04:29 +08:00
|
|
|
cached_enabled_ = false;
|
|
|
|
cached_enabled_time_ = 0;
|
2015-10-03 07:40:51 +08:00
|
|
|
use_caching_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetricsLibrary::InitWithNoCaching() {
|
|
|
|
Init();
|
|
|
|
use_caching_ = false;
|
2010-05-13 04:05:45 +08:00
|
|
|
}
|
|
|
|
|
2015-09-10 01:38:20 +08:00
|
|
|
void MetricsLibrary::InitForTest(const base::FilePath& metrics_directory) {
|
|
|
|
uma_events_file_ = metrics_directory.Append(metrics::kMetricsEventsFileName);
|
|
|
|
consent_file_ = metrics_directory.Append(metrics::kConsentFileName);
|
2015-09-29 02:04:29 +08:00
|
|
|
cached_enabled_ = false;
|
|
|
|
cached_enabled_time_ = 0;
|
2015-10-03 07:40:51 +08:00
|
|
|
use_caching_ = true;
|
2015-09-01 02:11:57 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 06:16:24 +08:00
|
|
|
bool MetricsLibrary::SendToUMA(const std::string& name,
|
|
|
|
int sample,
|
|
|
|
int min,
|
|
|
|
int max,
|
|
|
|
int nbuckets) {
|
2014-07-10 07:34:29 +08:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
|
|
|
|
.get(),
|
2015-09-10 01:38:20 +08:00
|
|
|
uma_events_file_.value());
|
2010-04-15 04:32:20 +08:00
|
|
|
}
|
2010-04-22 06:45:10 +08:00
|
|
|
|
2010-05-13 06:26:16 +08:00
|
|
|
bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
|
|
|
|
int max) {
|
2014-07-10 07:34:29 +08:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
|
2015-09-10 01:38:20 +08:00
|
|
|
uma_events_file_.value());
|
2011-01-07 02:51:47 +08:00
|
|
|
}
|
|
|
|
|
2015-11-07 03:02:45 +08:00
|
|
|
bool MetricsLibrary::SendBoolToUMA(const std::string& name, bool sample) {
|
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::LinearHistogramSample(name,
|
|
|
|
sample ? 1 : 0, 2).get(),
|
|
|
|
uma_events_file_.value());
|
|
|
|
}
|
|
|
|
|
2013-03-20 06:02:42 +08:00
|
|
|
bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
|
2014-07-10 07:34:29 +08:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::SparseHistogramSample(name, sample).get(),
|
2015-09-10 01:38:20 +08:00
|
|
|
uma_events_file_.value());
|
2013-03-20 06:02:42 +08:00
|
|
|
}
|
|
|
|
|
2011-01-07 02:51:47 +08:00
|
|
|
bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
|
2014-07-10 07:34:29 +08:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
2015-09-10 01:38:20 +08:00
|
|
|
*metrics::MetricSample::UserActionSample(action).get(),
|
|
|
|
uma_events_file_.value());
|
2010-04-22 06:45:10 +08:00
|
|
|
}
|
2011-01-22 22:15:56 +08:00
|
|
|
|
|
|
|
bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
|
2014-07-10 07:34:29 +08:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
2015-09-10 01:38:20 +08:00
|
|
|
*metrics::MetricSample::CrashSample(crash_kind).get(),
|
|
|
|
uma_events_file_.value());
|
2011-01-22 22:15:56 +08:00
|
|
|
}
|
2011-07-23 05:59:51 +08:00
|
|
|
|
2013-03-14 01:53:55 +08:00
|
|
|
bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
|
2014-12-12 01:48:46 +08:00
|
|
|
for (size_t i = 0; i < arraysize(kCrosEventNames); i++) {
|
2013-04-01 14:27:39 +08:00
|
|
|
if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
|
|
|
|
return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
|
|
|
|
}
|
2013-03-14 01:53:55 +08:00
|
|
|
}
|
2013-04-01 14:27:39 +08:00
|
|
|
return false;
|
2013-03-14 01:53:55 +08:00
|
|
|
}
|