blob: a1bb926d0b22192d4cd71182fe7c2b8f9b022036 [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#ifndef METRICS_METRICS_LIBRARY_H_
18#define METRICS_METRICS_LIBRARY_H_
Darin Petkov65b01462010-04-14 13:32:20 -070019
Darin Petkov11b8eb32010-05-18 11:00:59 -070020#include <sys/types.h>
Darin Petkov65b01462010-04-14 13:32:20 -070021#include <string>
Yunlian Jiang564c69f2012-05-02 14:24:04 -070022#include <unistd.h>
Darin Petkov65b01462010-04-14 13:32:20 -070023
Daniel Eratfd158292014-03-09 21:39:08 -070024#include <base/compiler_specific.h>
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070025#include <base/files/file_path.h>
Ben Chan652d6972014-09-03 17:23:46 -070026#include <base/macros.h>
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080027#include <binder/IServiceManager.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -070028#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov65b01462010-04-14 13:32:20 -070029
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080030namespace android {
31namespace brillo {
32namespace metrics {
33class IMetricsd;
34} // namespace metrics
35} // namespace brillo
36} // namespace android
37
Darin Petkovfc91b422010-05-12 13:05:45 -070038class MetricsLibraryInterface {
Darin Petkov65b01462010-04-14 13:32:20 -070039 public:
Darin Petkovfc91b422010-05-12 13:05:45 -070040 virtual void Init() = 0;
Bertrand SIMONNETe4fa61e2015-02-18 09:38:55 -080041 virtual bool AreMetricsEnabled() = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070042 virtual bool SendToUMA(const std::string& name, int sample,
43 int min, int max, int nbuckets) = 0;
44 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
Nathan Bullockadc1c232015-11-06 14:02:45 -050045 virtual bool SendBoolToUMA(const std::string& name, bool sample) = 0;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070046 virtual bool SendSparseToUMA(const std::string& name, int sample) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070047 virtual ~MetricsLibraryInterface() {}
48};
49
Bertrand SIMONNET475dfa62015-08-04 14:10:10 -070050// Library used to send metrics to Chrome/UMA.
Darin Petkovfc91b422010-05-12 13:05:45 -070051class MetricsLibrary : public MetricsLibraryInterface {
52 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070053 MetricsLibrary();
Daniel Eratfd158292014-03-09 21:39:08 -070054 virtual ~MetricsLibrary();
Darin Petkov11b8eb32010-05-18 11:00:59 -070055
Darin Petkovfc91b422010-05-12 13:05:45 -070056 // Initializes the library.
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070057 void Init() override;
Darin Petkovfc91b422010-05-12 13:05:45 -070058
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -070059 // Initializes the library and disables the cache of whether or not the
60 // metrics collection is enabled.
61 // By disabling this, we may have to check for the metrics status more often
62 // but the result will never be stale.
63 void InitWithNoCaching();
64
Ken Mixtereafbbdf2010-10-01 15:38:42 -070065 // Returns whether or not the machine is running in guest mode.
66 bool IsGuestMode();
67
Ken Mixter4c5daa42010-08-26 18:35:06 -070068 // Returns whether or not metrics collection is enabled.
Bertrand SIMONNETe4fa61e2015-02-18 09:38:55 -080069 bool AreMetricsEnabled() override;
Ken Mixter4c5daa42010-08-26 18:35:06 -070070
Darin Petkovc2526a12010-04-21 14:24:04 -070071 // Sends histogram data to Chrome for transport to UMA and returns
72 // true on success. This method results in the equivalent of an
73 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
74 // inside Chrome (see base/histogram.h).
75 //
76 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
77 // |min| is the minimum value of the histogram samples (|min| > 0).
78 // |max| is the maximum value of the histogram samples.
79 // |nbuckets| is the number of histogram buckets.
80 // [0,min) is the implicit underflow bucket.
81 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070082 //
83 // Note that the memory allocated in Chrome for each histogram is
84 // proportional to the number of buckets. Therefore, it is strongly
85 // recommended to keep this number low (e.g., 50 is normal, while
86 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070087 bool SendToUMA(const std::string& name, int sample,
Yunlian Jiang5a6ac9c2015-01-28 13:12:38 -080088 int min, int max, int nbuckets) override;
Darin Petkovfc91b422010-05-12 13:05:45 -070089
Darin Petkov5b7dce12010-04-21 15:45:10 -070090 // Sends linear histogram data to Chrome for transport to UMA and
91 // returns true on success. This method results in the equivalent of
92 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
93 // inside Chrome (see base/histogram.h).
94 //
95 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
96 // |max| is the maximum value of the histogram samples.
97 // 0 is the implicit underflow bucket.
98 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070099 //
Daniel Eratfd158292014-03-09 21:39:08 -0700100 // An enumeration histogram requires |max| + 1 number of
Darin Petkovc2bf95f2010-06-21 16:27:52 -0700101 // buckets. Note that the memory allocated in Chrome for each
102 // histogram is proportional to the number of buckets. Therefore, it
103 // is strongly recommended to keep this number low (e.g., 50 is
104 // normal, while 100 is high).
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -0700105 bool SendEnumToUMA(const std::string& name, int sample, int max) override;
Darin Petkovfc91b422010-05-12 13:05:45 -0700106
Nathan Bullockadc1c232015-11-06 14:02:45 -0500107 // Specialization of SendEnumToUMA for boolean values.
108 bool SendBoolToUMA(const std::string& name, bool sample) override;
109
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700110 // Sends sparse histogram sample to Chrome for transport to UMA. Returns
111 // true on success.
112 //
113 // |sample| is the 32-bit integer value to be recorded.
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -0700114 bool SendSparseToUMA(const std::string& name, int sample) override;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700115
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800116 // Sends a signal to UMA that a crash of the given |crash_kind|
117 // has occurred. Used by UMA to generate stability statistics.
118 bool SendCrashToUMA(const char *crash_kind);
119
Luigi Semenzato32684222013-03-13 10:53:55 -0700120 // Sends a "generic Chrome OS event" to UMA. This is an event name
121 // that is translated into an enumerated histogram entry. Event names
122 // are added to metrics_library.cc. Optionally, they can be added
123 // to histograms.xml---but part of the reason for this is to simplify
124 // the addition of events (at the cost of having to look them up by
125 // number in the histograms dashboard).
126 bool SendCrosEventToUMA(const std::string& event);
127
Bertrand SIMONNETb13527d2015-12-02 17:19:40 -0800128 // Debugging only.
129 // Dumps the histograms aggregated since metricsd started into |dump|.
130 // Returns true iff the dump succeeds.
131 bool GetHistogramsDump(std::string* dump);
132
Darin Petkov11b8eb32010-05-18 11:00:59 -0700133 private:
Sam Leffler10b301d2010-06-17 14:22:43 -0700134 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -0700135 friend class MetricsLibraryTest;
Bertrand SIMONNET4c8a8ad2015-09-09 13:27:33 -0700136 friend class UploadServiceTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700137 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700138 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabledNoCaching);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700139 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
140 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700141 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700142 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
143 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
144
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700145 void InitForTest(const base::FilePath& metrics_directory);
Bertrand SIMONNET12531862015-08-31 11:11:57 -0700146
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700147 // Sets |*result| to whether or not the |mounts_file| indicates that
148 // the |device_name| is currently mounted. Uses |buffer| of
149 // |buffer_size| to read the file. Returns false if any error.
150 bool IsDeviceMounted(const char* device_name,
151 const char* mounts_file,
152 char* buffer, int buffer_size,
153 bool* result);
154
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800155 // Connects to IMetricsd if the proxy does not exist or is not alive.
156 // Don't block if we fail to get the proxy for any reason.
157 bool CheckService();
158
Ken Mixter4c5daa42010-08-26 18:35:06 -0700159 // Time at which we last checked if metrics were enabled.
Bertrand SIMONNET3598d952015-09-28 11:04:29 -0700160 time_t cached_enabled_time_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700161
162 // Cached state of whether or not metrics were enabled.
Bertrand SIMONNET3598d952015-09-28 11:04:29 -0700163 bool cached_enabled_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700164
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -0700165 // True iff we should cache the enabled/disabled status.
166 bool use_caching_;
167
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -0800168 android::sp<android::IServiceManager> manager_;
169 android::sp<android::brillo::metrics::IMetricsd> metricsd_proxy_;
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700170 base::FilePath consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700171
Daniel Eratfd158292014-03-09 21:39:08 -0700172 DISALLOW_COPY_AND_ASSIGN(MetricsLibrary);
Darin Petkov65b01462010-04-14 13:32:20 -0700173};
174
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700175#endif // METRICS_METRICS_LIBRARY_H_