blob: d211ab44898d6d0b1269d4443d8759de929e40c5 [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>
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080021#include <binder/IServiceManager.h>
Darin Petkov65b01462010-04-14 13:32:20 -070022#include <errno.h>
23#include <sys/file.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070024#include <sys/stat.h>
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080025#include <utils/String16.h>
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070026
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070027#include <cstdio>
28#include <cstring>
Darin Petkov65b01462010-04-14 13:32:20 -070029
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080030#include "android/brillo/metrics/IMetricsd.h"
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070031#include "constants.h"
Chris Masonee10b5482013-02-14 12:15:35 -080032
Luigi Semenzato32684222013-03-13 10:53:55 -070033static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
34static const int kCrosEventHistogramMax = 100;
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080035static const char kMetricsServiceName[] = "android.brillo.metrics.IMetricsd";
Darin Petkov65b01462010-04-14 13:32:20 -070036
Chih-Chung Chang6844c062013-04-01 14:27:39 +080037/* Add new cros events here.
38 *
39 * The index of the event is sent in the message, so please do not
40 * reorder the names.
41 */
42static const char *kCrosEventNames[] = {
43 "ModemManagerCommandSendFailure", // 0
44 "HwWatchdogReboot", // 1
45 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070046 "Chaps.DatabaseCorrupted", // 3
47 "Chaps.DatabaseRepairFailure", // 4
48 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070049 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080050 "SpringPowerSupply.Original.High", // 7
51 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080052 "SpringPowerSupply.Original.Low", // 9
53 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080054 "TPM.NonZeroDictionaryAttackCounter", // 11
Luigi Semenzato538e2092015-03-19 17:18:24 -070055 "TPM.EarlyResetDuringCommand", // 12
Chih-Chung Chang6844c062013-04-01 14:27:39 +080056};
57
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080058using android::binder::Status;
59using android::brillo::metrics::IMetricsd;
60using android::String16;
61
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070062MetricsLibrary::MetricsLibrary() {}
Daniel Eratfd158292014-03-09 21:39:08 -070063MetricsLibrary::~MetricsLibrary() {}
64
Ken Mixtereafbbdf2010-10-01 15:38:42 -070065// We take buffer and buffer_size as parameters in order to simplify testing
66// of various alignments of the |device_name| with |buffer_size|.
67bool MetricsLibrary::IsDeviceMounted(const char* device_name,
68 const char* mounts_file,
69 char* buffer,
70 int buffer_size,
71 bool* result) {
Alex Vakulenko14595032014-08-28 14:59:56 -070072 if (buffer == nullptr || buffer_size < 1)
Ken Mixtereafbbdf2010-10-01 15:38:42 -070073 return false;
74 int mounts_fd = open(mounts_file, O_RDONLY);
75 if (mounts_fd < 0)
76 return false;
77 // match_offset describes:
78 // -1 -- not beginning of line
79 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
80 // strlen(device_name) -- matched full name, just need a space.
81 int match_offset = 0;
82 bool match = false;
83 while (!match) {
84 int read_size = read(mounts_fd, buffer, buffer_size);
85 if (read_size <= 0) {
86 if (errno == -EINTR)
87 continue;
88 break;
89 }
90 for (int i = 0; i < read_size; ++i) {
91 if (buffer[i] == '\n') {
92 match_offset = 0;
93 continue;
94 }
95 if (match_offset < 0) {
96 continue;
97 }
98 if (device_name[match_offset] == '\0') {
99 if (buffer[i] == ' ') {
100 match = true;
101 break;
102 }
103 match_offset = -1;
104 continue;
105 }
106
107 if (buffer[i] == device_name[match_offset]) {
108 ++match_offset;
109 } else {
110 match_offset = -1;
111 }
112 }
113 }
114 close(mounts_fd);
115 *result = match;
116 return true;
117}
118
119bool MetricsLibrary::IsGuestMode() {
120 char buffer[256];
121 bool result = false;
122 if (!IsDeviceMounted("guestfs",
123 "/proc/mounts",
124 buffer,
125 sizeof(buffer),
126 &result)) {
127 return false;
128 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700129 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700130}
131
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800132bool MetricsLibrary::CheckService() {
133 if (metricsd_proxy_.get() &&
134 android::IInterface::asBinder(metricsd_proxy_)->isBinderAlive())
135 return true;
136
137 const String16 name(kMetricsServiceName);
138 metricsd_proxy_ = android::interface_cast<IMetricsd>(
139 android::defaultServiceManager()->checkService(name));
140 return metricsd_proxy_.get();
141}
142
Ken Mixter4c5daa42010-08-26 18:35:06 -0700143bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200144 static struct stat stat_buffer;
Alex Vakulenko14595032014-08-28 14:59:56 -0700145 time_t this_check_time = time(nullptr);
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700146 if (!use_caching_ || this_check_time != cached_enabled_time_) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700147 cached_enabled_time_ = this_check_time;
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700148 cached_enabled_ = stat(consent_file_.value().data(), &stat_buffer) >= 0;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700149 }
150 return cached_enabled_;
151}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700152
Darin Petkovfc91b422010-05-12 13:05:45 -0700153void MetricsLibrary::Init() {
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -0800154 base::FilePath dir = base::FilePath(metrics::kSharedMetricsDirectory);
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700155 consent_file_ = dir.Append(metrics::kConsentFileName);
Bertrand SIMONNET3598d952015-09-28 11:04:29 -0700156 cached_enabled_ = false;
157 cached_enabled_time_ = 0;
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700158 use_caching_ = true;
159}
160
161void MetricsLibrary::InitWithNoCaching() {
162 Init();
163 use_caching_ = false;
Darin Petkovfc91b422010-05-12 13:05:45 -0700164}
165
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700166void MetricsLibrary::InitForTest(const base::FilePath& metrics_directory) {
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700167 consent_file_ = metrics_directory.Append(metrics::kConsentFileName);
Bertrand SIMONNET3598d952015-09-28 11:04:29 -0700168 cached_enabled_ = false;
169 cached_enabled_time_ = 0;
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700170 use_caching_ = true;
Bertrand SIMONNET12531862015-08-31 11:11:57 -0700171}
172
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800173bool MetricsLibrary::SendToUMA(
174 const std::string& name, int sample, int min, int max, int nbuckets) {
175 return CheckService() &&
176 metricsd_proxy_->recordHistogram(String16(name.c_str()), sample, min,
177 max, nbuckets)
178 .isOk();
Darin Petkov65b01462010-04-14 13:32:20 -0700179}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700180
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800181bool MetricsLibrary::SendEnumToUMA(const std::string& name,
182 int sample,
Darin Petkov21cd2c52010-05-12 15:26:16 -0700183 int max) {
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800184 return CheckService() &&
185 metricsd_proxy_->recordLinearHistogram(String16(name.c_str()), sample,
186 max)
187 .isOk();
Darin Petkoved824852011-01-06 10:51:47 -0800188}
189
Nathan Bullockadc1c232015-11-06 14:02:45 -0500190bool MetricsLibrary::SendBoolToUMA(const std::string& name, bool sample) {
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800191 return CheckService() &&
192 metricsd_proxy_->recordLinearHistogram(String16(name.c_str()),
193 sample ? 1 : 0, 2)
194 .isOk();
Nathan Bullockadc1c232015-11-06 14:02:45 -0500195}
196
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700197bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800198 return CheckService() &&
199 metricsd_proxy_->recordSparseHistogram(String16(name.c_str()), sample)
200 .isOk();
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700201}
202
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800203bool MetricsLibrary::SendCrashToUMA(const char* crash_kind) {
204 return CheckService() &&
205 metricsd_proxy_->recordCrash(String16(crash_kind)).isOk();
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800206}
Ken Mixterb2f17092011-07-22 14:59:51 -0700207
Luigi Semenzato32684222013-03-13 10:53:55 -0700208bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Alex Vakulenko788d3b62014-12-11 09:48:46 -0800209 for (size_t i = 0; i < arraysize(kCrosEventNames); i++) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800210 if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
211 return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
212 }
Luigi Semenzato32684222013-03-13 10:53:55 -0700213 }
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800214 return false;
Luigi Semenzato32684222013-03-13 10:53:55 -0700215}
Bertrand SIMONNETb13527d2015-12-02 17:19:40 -0800216
217bool MetricsLibrary::GetHistogramsDump(std::string* dump) {
218 android::String16 temp_dump;
219 if (!CheckService() ||
220 !metricsd_proxy_->getHistogramsDump(&temp_dump).isOk()) {
221 return false;
222 }
223
224 *dump = android::String8(temp_dump).string();
225 return true;
226}