blob: fb80354c655b3d31cbbc16fe50ce40ca458f50d9 [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 SIMONNET4b915ae2015-07-28 15:38:14 -070016#include "serialization/metric_sample.h"
17#include "serialization/serialization_utils.h"
Chris Masonee10b5482013-02-14 12:15:35 -080018
Ken Mixter4c5daa42010-08-26 18:35:06 -070019static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
Luigi Semenzato93408422015-03-20 08:35:30 -070020static const char kUMAEventsPath[] = "/var/lib/metrics/uma-events";
Ken Mixter4c5daa42010-08-26 18:35:06 -070021static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
Luigi Semenzato32684222013-03-13 10:53:55 -070022static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
23static const int kCrosEventHistogramMax = 100;
Darin Petkov65b01462010-04-14 13:32:20 -070024
Chih-Chung Chang6844c062013-04-01 14:27:39 +080025/* Add new cros events here.
26 *
27 * The index of the event is sent in the message, so please do not
28 * reorder the names.
29 */
30static const char *kCrosEventNames[] = {
31 "ModemManagerCommandSendFailure", // 0
32 "HwWatchdogReboot", // 1
33 "Cras.NoCodecsFoundAtBoot", // 2
Darren Krahn6e55c1152013-07-19 14:09:50 -070034 "Chaps.DatabaseCorrupted", // 3
35 "Chaps.DatabaseRepairFailure", // 4
36 "Chaps.DatabaseCreateFailure", // 5
Darren Krahn86830ba2013-07-26 13:37:20 -070037 "Attestation.OriginSpecificExhausted", // 6
Luigi Semenzatoe57398a2013-11-11 14:24:44 -080038 "SpringPowerSupply.Original.High", // 7
39 "SpringPowerSupply.Other.High", // 8
Luigi Semenzatoe8fd9682013-11-13 16:28:43 -080040 "SpringPowerSupply.Original.Low", // 9
41 "SpringPowerSupply.ChargerIdle", // 10
Darren Krahn09a15fa2014-02-07 16:51:15 -080042 "TPM.NonZeroDictionaryAttackCounter", // 11
Luigi Semenzato538e2092015-03-19 17:18:24 -070043 "TPM.EarlyResetDuringCommand", // 12
Chih-Chung Chang6844c062013-04-01 14:27:39 +080044};
45
Ken Mixter4c5daa42010-08-26 18:35:06 -070046time_t MetricsLibrary::cached_enabled_time_ = 0;
47bool MetricsLibrary::cached_enabled_ = false;
48
Luigi Semenzato41c54502014-05-13 15:16:24 -070049MetricsLibrary::MetricsLibrary() : consent_file_(kConsentFile) {}
Daniel Eratfd158292014-03-09 21:39:08 -070050MetricsLibrary::~MetricsLibrary() {}
51
Ken Mixtereafbbdf2010-10-01 15:38:42 -070052// We take buffer and buffer_size as parameters in order to simplify testing
53// of various alignments of the |device_name| with |buffer_size|.
54bool MetricsLibrary::IsDeviceMounted(const char* device_name,
55 const char* mounts_file,
56 char* buffer,
57 int buffer_size,
58 bool* result) {
Alex Vakulenko14595032014-08-28 14:59:56 -070059 if (buffer == nullptr || buffer_size < 1)
Ken Mixtereafbbdf2010-10-01 15:38:42 -070060 return false;
61 int mounts_fd = open(mounts_file, O_RDONLY);
62 if (mounts_fd < 0)
63 return false;
64 // match_offset describes:
65 // -1 -- not beginning of line
66 // 0..strlen(device_name)-1 -- this offset in device_name is next to match
67 // strlen(device_name) -- matched full name, just need a space.
68 int match_offset = 0;
69 bool match = false;
70 while (!match) {
71 int read_size = read(mounts_fd, buffer, buffer_size);
72 if (read_size <= 0) {
73 if (errno == -EINTR)
74 continue;
75 break;
76 }
77 for (int i = 0; i < read_size; ++i) {
78 if (buffer[i] == '\n') {
79 match_offset = 0;
80 continue;
81 }
82 if (match_offset < 0) {
83 continue;
84 }
85 if (device_name[match_offset] == '\0') {
86 if (buffer[i] == ' ') {
87 match = true;
88 break;
89 }
90 match_offset = -1;
91 continue;
92 }
93
94 if (buffer[i] == device_name[match_offset]) {
95 ++match_offset;
96 } else {
97 match_offset = -1;
98 }
99 }
100 }
101 close(mounts_fd);
102 *result = match;
103 return true;
104}
105
106bool MetricsLibrary::IsGuestMode() {
107 char buffer[256];
108 bool result = false;
109 if (!IsDeviceMounted("guestfs",
110 "/proc/mounts",
111 buffer,
112 sizeof(buffer),
113 &result)) {
114 return false;
115 }
Arkaitz Ruiz Alvarez9f1a7742011-05-26 12:22:22 -0700116 return result && (access("/var/run/state/logged-in", F_OK) == 0);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700117}
118
Ken Mixter4c5daa42010-08-26 18:35:06 -0700119bool MetricsLibrary::AreMetricsEnabled() {
Julian Pastarmov70b7abd2011-08-02 16:10:49 +0200120 static struct stat stat_buffer;
Alex Vakulenko14595032014-08-28 14:59:56 -0700121 time_t this_check_time = time(nullptr);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700122 if (this_check_time != cached_enabled_time_) {
123 cached_enabled_time_ = this_check_time;
Bertrand SIMONNET5db66c32015-07-28 15:42:42 -0700124 cached_enabled_ = stat(consent_file_.c_str(), &stat_buffer) >= 0;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700125 }
126 return cached_enabled_;
127}
Darin Petkov11b8eb32010-05-18 11:00:59 -0700128
Darin Petkovfc91b422010-05-12 13:05:45 -0700129void MetricsLibrary::Init() {
Darin Petkov11b8eb32010-05-18 11:00:59 -0700130 uma_events_file_ = kUMAEventsPath;
Darin Petkovfc91b422010-05-12 13:05:45 -0700131}
132
Luigi Semenzato41c54502014-05-13 15:16:24 -0700133bool MetricsLibrary::SendToAutotest(const std::string& name, int value) {
David James3b3add52010-06-04 15:01:19 -0700134 FILE* autotest_file = fopen(kAutotestPath, "a+");
Alex Vakulenko14595032014-08-28 14:59:56 -0700135 if (autotest_file == nullptr) {
Luigi Semenzato41c54502014-05-13 15:16:24 -0700136 PLOG(ERROR) << kAutotestPath << ": fopen";
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700137 return false;
138 }
139
140 fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
141 fclose(autotest_file);
142 return true;
143}
144
Luigi Semenzato41c54502014-05-13 15:16:24 -0700145bool MetricsLibrary::SendToUMA(const std::string& name,
146 int sample,
147 int min,
148 int max,
149 int nbuckets) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700150 return metrics::SerializationUtils::WriteMetricToFile(
151 *metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
152 .get(),
153 kUMAEventsPath);
Darin Petkov65b01462010-04-14 13:32:20 -0700154}
Darin Petkov5b7dce12010-04-21 15:45:10 -0700155
Darin Petkov21cd2c52010-05-12 15:26:16 -0700156bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
157 int max) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700158 return metrics::SerializationUtils::WriteMetricToFile(
159 *metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
160 kUMAEventsPath);
Darin Petkoved824852011-01-06 10:51:47 -0800161}
162
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700163bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700164 return metrics::SerializationUtils::WriteMetricToFile(
165 *metrics::MetricSample::SparseHistogramSample(name, sample).get(),
166 kUMAEventsPath);
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700167}
168
Darin Petkoved824852011-01-06 10:51:47 -0800169bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700170 return metrics::SerializationUtils::WriteMetricToFile(
171 *metrics::MetricSample::UserActionSample(action).get(), kUMAEventsPath);
Darin Petkov5b7dce12010-04-21 15:45:10 -0700172}
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800173
174bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700175 return metrics::SerializationUtils::WriteMetricToFile(
176 *metrics::MetricSample::CrashSample(crash_kind).get(), kUMAEventsPath);
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800177}
Ken Mixterb2f17092011-07-22 14:59:51 -0700178
Luigi Semenzato32684222013-03-13 10:53:55 -0700179bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
Alex Vakulenko788d3b62014-12-11 09:48:46 -0800180 for (size_t i = 0; i < arraysize(kCrosEventNames); i++) {
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800181 if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
182 return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
183 }
Luigi Semenzato32684222013-03-13 10:53:55 -0700184 }
Chih-Chung Chang6844c062013-04-01 14:27:39 +0800185 return false;
Luigi Semenzato32684222013-03-13 10:53:55 -0700186}