blob: a651b767649fa3cb8e77e49259b187f5158774df [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 SIMONNETd83ca802014-07-09 16:34:29 -070017#include "metrics/metrics_library.h"
Darin Petkov65b01462010-04-14 13:32:20 -070018
Luigi Semenzato41c54502014-05-13 15:16:24 -070019#include <base/logging.h>
20#include <base/strings/stringprintf.h>
Darin Petkov65b01462010-04-14 13:32:20 -070021#include <errno.h>
22#include <sys/file.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070023#include <sys/stat.h>
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070024
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070025#include <cstdio>
26#include <cstring>
Darin Petkov65b01462010-04-14 13:32:20 -070027
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070028#include "constants.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070029#include "serialization/metric_sample.h"
30#include "serialization/serialization_utils.h"
Chris Masonee10b5482013-02-14 12:15:35 -080031
Luigi Semenzato32684222013-03-13 10:53:55 -070032static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
33static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070034
Chih-Chung Chang6844c062013-04-01 14:27:39 +080035/* Add new cros events here.
36 *
37 * The index of the event is sent in the message, so please do not
38 * reorder the names.
39 */
40static const char *kCrosEventNames[] = {
41 "ModemManagerCommandSendFailure", // 0
42 "HwWatchdogReboot", // 1
43 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070044 "Chaps.DatabaseCorrupted", // 3
45 "Chaps.DatabaseRepairFailure", // 4
46 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070047 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080048 "SpringPowerSupply.Original.High", // 7
49 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080050 "SpringPowerSupply.Original.Low", // 9
51 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080052 "TPM.NonZeroDictionaryAttackCounter", // 11
Luigi Semenzato538e2092015-03-19 17:18:24 -070053 "TPM.EarlyResetDuringCommand", // 12
Chih-Chung Chang6844c062013-04-01 14:27:39 +080054};
55
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070056MetricsLibrary::MetricsLibrary() {}
Daniel Eratfd158292014-03-09 21:39:08 -070057MetricsLibrary::~MetricsLibrary() {}
58
Ken Mixtereafbbdf2010-10-01 15:38:42 -070059// We take buffer and buffer_size as parameters in order to simplify testing
60// of various alignments of the |device_name| with |buffer_size|.
61bool MetricsLibrary::IsDeviceMounted(const char* device_name,
62 const char* mounts_file,
63 char* buffer,
64 int buffer_size,
65 bool* result) {
Alex Vakulenko14595032014-08-28 14:59:56 -070066 if (buffer == nullptr || buffer_size < 1)
Ken Mixtereafbbdf2010-10-01 15:38:42 -070067 return false;
68 int mounts_fd = open(mounts_file, O_RDONLY);
69 if (mounts_fd < 0)
70 return false;
71 // match_offset describes:
72 // -1 -- not beginning of line
73 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
74 // strlen(device_name) -- matched full name, just need a space.
75 int match_offset = 0;
76 bool match = false;
77 while (!match) {
78 int read_size = read(mounts_fd, buffer, buffer_size);
79 if (read_size <= 0) {
80 if (errno == -EINTR)
81 continue;
82 break;
83 }
84 for (int i = 0; i < read_size; ++i) {
85 if (buffer[i] == '\n') {
86 match_offset = 0;
87 continue;
88 }
89 if (match_offset < 0) {
90 continue;
91 }
92 if (device_name[match_offset] == '\0') {
93 if (buffer[i] == ' ') {
94 match = true;
95 break;
96 }
97 match_offset = -1;
98 continue;
99 }
100
101 if (buffer[i] == device_name[match_offset]) {
102 ++match_offset;
103 } else {
104 match_offset = -1;
105 }
106 }
107 }
108 close(mounts_fd);
109 *result = match;
110 return true;
111}
112
113bool MetricsLibrary::IsGuestMode() {
114 char buffer[256];
115 bool result = false;
116 if (!IsDeviceMounted("guestfs",
117 "/proc/mounts",
118 buffer,
119 sizeof(buffer),
120 &result)) {
121 return false;
122 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700123 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700124}
125
Ken Mixter4c5daa42010-08-26 18:35:06 -0700126bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200127 static struct stat stat_buffer;
Alex Vakulenko14595032014-08-28 14:59:56 -0700128 time_t this_check_time = time(nullptr);
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700129 if (!use_caching_ || this_check_time != cached_enabled_time_) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700130 cached_enabled_time_ = this_check_time;
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700131 cached_enabled_ = stat(consent_file_.value().data(), &stat_buffer) >= 0;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700132 }
133 return cached_enabled_;
134}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700135
Darin Petkovfc91b422010-05-12 13:05:45 -0700136void MetricsLibrary::Init() {
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700137 base::FilePath dir = base::FilePath(metrics::kMetricsDirectory);
138 uma_events_file_ = dir.Append(metrics::kMetricsEventsFileName);
139 consent_file_ = dir.Append(metrics::kConsentFileName);
Bertrand SIMONNET3598d952015-09-28 11:04:29 -0700140 cached_enabled_ = false;
141 cached_enabled_time_ = 0;
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700142 use_caching_ = true;
143}
144
145void MetricsLibrary::InitWithNoCaching() {
146 Init();
147 use_caching_ = false;
Darin Petkovfc91b422010-05-12 13:05:45 -0700148}
149
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700150void MetricsLibrary::InitForTest(const base::FilePath& metrics_directory) {
151 uma_events_file_ = metrics_directory.Append(metrics::kMetricsEventsFileName);
152 consent_file_ = metrics_directory.Append(metrics::kConsentFileName);
Bertrand SIMONNET3598d952015-09-28 11:04:29 -0700153 cached_enabled_ = false;
154 cached_enabled_time_ = 0;
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700155 use_caching_ = true;
Bertrand SIMONNET12531862015-08-31 11:11:57 -0700156}
157
Luigi Semenzato41c54502014-05-13 15:16:24 -0700158bool MetricsLibrary::SendToUMA(const std::string& name,
159 int sample,
160 int min,
161 int max,
162 int nbuckets) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700163 return metrics::SerializationUtils::WriteMetricToFile(
164 *metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
165 .get(),
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700166 uma_events_file_.value());
Darin Petkov65b01462010-04-14 13:32:20 -0700167}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700168
Darin Petkov21cd2c52010-05-12 15:26:16 -0700169bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
170 int max) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700171 return metrics::SerializationUtils::WriteMetricToFile(
172 *metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700173 uma_events_file_.value());
Darin Petkoved824852011-01-06 10:51:47 -0800174}
175
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700176bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700177 return metrics::SerializationUtils::WriteMetricToFile(
178 *metrics::MetricSample::SparseHistogramSample(name, sample).get(),
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700179 uma_events_file_.value());
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700180}
181
Darin Petkoved824852011-01-06 10:51:47 -0800182bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700183 return metrics::SerializationUtils::WriteMetricToFile(
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700184 *metrics::MetricSample::UserActionSample(action).get(),
185 uma_events_file_.value());
Darin Petkov5b7dce12010-04-21 15:45:10 -0700186}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800187
188bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700189 return metrics::SerializationUtils::WriteMetricToFile(
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700190 *metrics::MetricSample::CrashSample(crash_kind).get(),
191 uma_events_file_.value());
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800192}
Ken Mixterb2f17092011-07-22 14:59:51 -0700193
Luigi Semenzato32684222013-03-13 10:53:55 -0700194bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Alex Vakulenko788d3b62014-12-11 09:48:46 -0800195 for (size_t i = 0; i < arraysize(kCrosEventNames); i++) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800196 if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
197 return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
198 }
Luigi Semenzato32684222013-03-13 10:53:55 -0700199 }
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800200 return false;
Luigi Semenzato32684222013-03-13 10:53:55 -0700201}