Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 16 | |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 17 | #ifndef METRICS_METRICS_LIBRARY_H_ |
| 18 | #define METRICS_METRICS_LIBRARY_H_ |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 19 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 21 | #include <string> |
Yunlian Jiang | 564c69f | 2012-05-02 14:24:04 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 23 | |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 24 | #include <base/compiler_specific.h> |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 25 | #include <base/files/file_path.h> |
Ben Chan | 652d697 | 2014-09-03 17:23:46 -0700 | [diff] [blame] | 26 | #include <base/macros.h> |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 28 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 29 | |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace brillo { |
| 32 | namespace metrics { |
| 33 | class IMetricsd; |
| 34 | } // namespace metrics |
| 35 | } // namespace brillo |
| 36 | } // namespace android |
| 37 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 38 | class MetricsLibraryInterface { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 39 | public: |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 40 | virtual void Init() = 0; |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 41 | virtual bool AreMetricsEnabled() = 0; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 42 | 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 Bullock | adc1c23 | 2015-11-06 14:02:45 -0500 | [diff] [blame] | 45 | virtual bool SendBoolToUMA(const std::string& name, bool sample) = 0; |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 46 | virtual bool SendSparseToUMA(const std::string& name, int sample) = 0; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 47 | virtual ~MetricsLibraryInterface() {} |
| 48 | }; |
| 49 | |
Bertrand SIMONNET | 475dfa6 | 2015-08-04 14:10:10 -0700 | [diff] [blame] | 50 | // Library used to send metrics to Chrome/UMA. |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 51 | class MetricsLibrary : public MetricsLibraryInterface { |
| 52 | public: |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 53 | MetricsLibrary(); |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 54 | virtual ~MetricsLibrary(); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 55 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 56 | // Initializes the library. |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 57 | void Init() override; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 58 | |
Bertrand SIMONNET | a5b40d0 | 2015-10-02 16:40:51 -0700 | [diff] [blame] | 59 | // 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 Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 65 | // Returns whether or not the machine is running in guest mode. |
| 66 | bool IsGuestMode(); |
| 67 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 68 | // Returns whether or not metrics collection is enabled. |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 69 | bool AreMetricsEnabled() override; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 70 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 71 | // 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 Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 82 | // |
| 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 Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 87 | bool SendToUMA(const std::string& name, int sample, |
Yunlian Jiang | 5a6ac9c | 2015-01-28 13:12:38 -0800 | [diff] [blame] | 88 | int min, int max, int nbuckets) override; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 89 | |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 90 | // 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 Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 99 | // |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 100 | // An enumeration histogram requires |max| + 1 number of |
Darin Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 101 | // 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 Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 105 | bool SendEnumToUMA(const std::string& name, int sample, int max) override; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 106 | |
Nathan Bullock | adc1c23 | 2015-11-06 14:02:45 -0500 | [diff] [blame] | 107 | // Specialization of SendEnumToUMA for boolean values. |
| 108 | bool SendBoolToUMA(const std::string& name, bool sample) override; |
| 109 | |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 110 | // 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 Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 114 | bool SendSparseToUMA(const std::string& name, int sample) override; |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 115 | |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 116 | // 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 Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 120 | // 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 SIMONNET | b13527d | 2015-12-02 17:19:40 -0800 | [diff] [blame] | 128 | // 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 Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 133 | private: |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 134 | friend class CMetricsLibraryTest; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 135 | friend class MetricsLibraryTest; |
Bertrand SIMONNET | 4c8a8ad | 2015-09-09 13:27:33 -0700 | [diff] [blame] | 136 | friend class UploadServiceTest; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 137 | FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled); |
Bertrand SIMONNET | a5b40d0 | 2015-10-02 16:40:51 -0700 | [diff] [blame] | 138 | FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabledNoCaching); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 139 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage); |
| 140 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 141 | FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 142 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome); |
| 143 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation); |
| 144 | |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 145 | void InitForTest(const base::FilePath& metrics_directory); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 146 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 147 | // 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 SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 155 | // 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 Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 159 | // Time at which we last checked if metrics were enabled. |
Bertrand SIMONNET | 3598d95 | 2015-09-28 11:04:29 -0700 | [diff] [blame] | 160 | time_t cached_enabled_time_; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 161 | |
| 162 | // Cached state of whether or not metrics were enabled. |
Bertrand SIMONNET | 3598d95 | 2015-09-28 11:04:29 -0700 | [diff] [blame] | 163 | bool cached_enabled_; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 164 | |
Bertrand SIMONNET | a5b40d0 | 2015-10-02 16:40:51 -0700 | [diff] [blame] | 165 | // True iff we should cache the enabled/disabled status. |
| 166 | bool use_caching_; |
| 167 | |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 168 | android::sp<android::IServiceManager> manager_; |
| 169 | android::sp<android::brillo::metrics::IMetricsd> metricsd_proxy_; |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 170 | base::FilePath consent_file_; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 171 | |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 172 | DISALLOW_COPY_AND_ASSIGN(MetricsLibrary); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 175 | #endif // METRICS_METRICS_LIBRARY_H_ |