blob: ac97c3e2a099d9cf4e10c7a9ebb42fe64cbca40c [file] [log] [blame]
Darin Petkov65b01462010-04-14 13:32:20 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -07005#include "metrics/metrics_library.h"
Darin Petkov65b01462010-04-14 13:32:20 -07006
Luigi Semenzato41c54502014-05-13 15:16:24 -07007#include <base/logging.h>
8#include <base/strings/stringprintf.h>
Darin Petkov65b01462010-04-14 13:32:20 -07009#include <errno.h>
10#include <sys/file.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070011#include <sys/stat.h>
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070012
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070013#include <cstdio>
14#include <cstring>
Darin Petkov65b01462010-04-14 13:32:20 -070015
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -070016#include "components/metrics/chromeos/metric_sample.h"
17#include "components/metrics/chromeos/serialization_utils.h"
Chris Masonee10b5482013-02-14 12:15:35 -080018
Ken Mixterb2f17092011-07-22 14:59:51 -070019#include "policy/device_policy.h"
Darin Petkov8842c8c2011-02-24 12:48:30 -080020
Chih-Chung Chang6844c062013-04-01 14:27:39 +080021#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Darin Petkov65b01462010-04-14 13:32:20 -070022
Ken Mixter4c5daa42010-08-26 18:35:06 -070023static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
Luigi Semenzato31cda982014-05-19 08:30:42 -070024static const char kUMAEventsPath[] = "/var/run/metrics/uma-events";
Ken Mixter4c5daa42010-08-26 18:35:06 -070025static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Luigi Semenzato32684222013-03-13 10:53:55 -070026static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
27static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070028
Chih-Chung Chang6844c062013-04-01 14:27:39 +080029/* Add new cros events here.
30 *
31 * The index of the event is sent in the message, so please do not
32 * reorder the names.
33 */
34static const char *kCrosEventNames[] = {
35 "ModemManagerCommandSendFailure", // 0
36 "HwWatchdogReboot", // 1
37 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070038 "Chaps.DatabaseCorrupted", // 3
39 "Chaps.DatabaseRepairFailure", // 4
40 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070041 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080042 "SpringPowerSupply.Original.High", // 7
43 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080044 "SpringPowerSupply.Original.Low", // 9
45 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080046 "TPM.NonZeroDictionaryAttackCounter", // 11
Chih-Chung Chang6844c062013-04-01 14:27:39 +080047};
48
Ken Mixter4c5daa42010-08-26 18:35:06 -070049time_t MetricsLibrary::cached_enabled_time_ = 0;
50bool MetricsLibrary::cached_enabled_ = false;
51
Luigi Semenzato41c54502014-05-13 15:16:24 -070052MetricsLibrary::MetricsLibrary() : consent_file_(kConsentFile) {}
Daniel Eratfd158292014-03-09 21:39:08 -070053MetricsLibrary::~MetricsLibrary() {}
54
Ken Mixtereafbbdf2010-10-01 15:38:42 -070055// We take buffer and buffer_size as parameters in order to simplify testing
56// of various alignments of the |device_name| with |buffer_size|.
57bool MetricsLibrary::IsDeviceMounted(const char* device_name,
58 const char* mounts_file,
59 char* buffer,
60 int buffer_size,
61 bool* result) {
62 if (buffer == NULL || buffer_size < 1)
63 return false;
64 int mounts_fd = open(mounts_file, O_RDONLY);
65 if (mounts_fd < 0)
66 return false;
67 // match_offset describes:
68 // -1 -- not beginning of line
69 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
70 // strlen(device_name) -- matched full name, just need a space.
71 int match_offset = 0;
72 bool match = false;
73 while (!match) {
74 int read_size = read(mounts_fd, buffer, buffer_size);
75 if (read_size <= 0) {
76 if (errno == -EINTR)
77 continue;
78 break;
79 }
80 for (int i = 0; i < read_size; ++i) {
81 if (buffer[i] == '\n') {
82 match_offset = 0;
83 continue;
84 }
85 if (match_offset < 0) {
86 continue;
87 }
88 if (device_name[match_offset] == '\0') {
89 if (buffer[i] == ' ') {
90 match = true;
91 break;
92 }
93 match_offset = -1;
94 continue;
95 }
96
97 if (buffer[i] == device_name[match_offset]) {
98 ++match_offset;
99 } else {
100 match_offset = -1;
101 }
102 }
103 }
104 close(mounts_fd);
105 *result = match;
106 return true;
107}
108
109bool MetricsLibrary::IsGuestMode() {
110 char buffer[256];
111 bool result = false;
112 if (!IsDeviceMounted("guestfs",
113 "/proc/mounts",
114 buffer,
115 sizeof(buffer),
116 &result)) {
117 return false;
118 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700119 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700120}
121
Ken Mixter4c5daa42010-08-26 18:35:06 -0700122bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200123 static struct stat stat_buffer;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700124 time_t this_check_time = time(NULL);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700125 if (this_check_time != cached_enabled_time_) {
126 cached_enabled_time_ = this_check_time;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200127
128 if (!policy_provider_.get())
129 policy_provider_.reset(new policy::PolicyProvider());
Julian Pastarmovd605a002013-02-04 17:58:14 +0100130 policy_provider_->Reload();
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200131 // We initialize with the default value which is false and will be preserved
132 // if the policy is not set.
133 bool enabled = false;
134 bool has_policy = false;
135 if (policy_provider_->device_policy_is_loaded()) {
136 has_policy =
137 policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
138 }
139 // If policy couldn't be loaded or the metrics policy is not set we should
140 // still respect the consent file if it is present for migration purposes.
141 // TODO(pastarmovj)
142 if (!has_policy) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700143 enabled = stat(consent_file_.c_str(), &stat_buffer) >= 0;
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200144 }
145
Ken Mixterb2f17092011-07-22 14:59:51 -0700146 if (enabled && !IsGuestMode())
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700147 cached_enabled_ = true;
148 else
149 cached_enabled_ = false;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700150 }
151 return cached_enabled_;
152}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700153
Darin Petkovfc91b422010-05-12 13:05:45 -0700154void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700155 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700156}
157
Luigi Semenzato41c54502014-05-13 15:16:24 -0700158bool MetricsLibrary::SendToAutotest(const std::string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700159 FILE* autotest_file = fopen(kAutotestPath, "a+");
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700160 if (autotest_file == NULL) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700161 PLOG(ERROR) << kAutotestPath << ": fopen";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700162 return false;
163 }
164
165 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
166 fclose(autotest_file);
167 return true;
168}
169
Luigi Semenzato41c54502014-05-13 15:16:24 -0700170bool MetricsLibrary::SendToUMA(const std::string& name,
171 int sample,
172 int min,
173 int max,
174 int nbuckets) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700175 return metrics::SerializationUtils::WriteMetricToFile(
176 *metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
177 .get(),
178 kUMAEventsPath);
Darin Petkov65b01462010-04-14 13:32:20 -0700179}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700180
Darin Petkov21cd2c52010-05-12 15:26:16 -0700181bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
182 int max) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700183 return metrics::SerializationUtils::WriteMetricToFile(
184 *metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
185 kUMAEventsPath);
Darin Petkoved824852011-01-06 10:51:47 -0800186}
187
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700188bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700189 return metrics::SerializationUtils::WriteMetricToFile(
190 *metrics::MetricSample::SparseHistogramSample(name, sample).get(),
191 kUMAEventsPath);
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700192}
193
Darin Petkoved824852011-01-06 10:51:47 -0800194bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700195 return metrics::SerializationUtils::WriteMetricToFile(
196 *metrics::MetricSample::UserActionSample(action).get(), kUMAEventsPath);
Darin Petkov5b7dce12010-04-21 15:45:10 -0700197}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800198
199bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700200 return metrics::SerializationUtils::WriteMetricToFile(
201 *metrics::MetricSample::CrashSample(crash_kind).get(), kUMAEventsPath);
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800202}
Ken Mixterb2f17092011-07-22 14:59:51 -0700203
204void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
205 policy_provider_.reset(provider);
206}
Luigi Semenzato32684222013-03-13 10:53:55 -0700207
208bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800209 for (size_t i = 0; i < ARRAY_SIZE(kCrosEventNames); i++) {
210 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}