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 | |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 17 | #ifndef METRICS_METRICS_DAEMON_H_ |
| 18 | #define METRICS_METRICS_DAEMON_H_ |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 19 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 22 | #include <map> |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 25 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 26 | #include <base/files/file_path.h> |
Chris Masone | 817016a | 2011-05-12 14:14:48 -0700 | [diff] [blame] | 27 | #include <base/memory/scoped_ptr.h> |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 28 | #include <base/time/time.h> |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 29 | #include <chromeos/daemons/dbus_daemon.h> |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 30 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 31 | |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 32 | #include "metrics/metrics_library.h" |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 33 | #include "persistent_integer.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 34 | #include "uploader/upload_service.h" |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 35 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 36 | using chromeos_metrics::PersistentInteger; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 37 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 38 | class MetricsDaemon : public chromeos::DBusDaemon { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 39 | public: |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 40 | MetricsDaemon(); |
| 41 | ~MetricsDaemon(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 42 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 43 | // Initializes metrics class variables. |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 44 | void Init(bool testing, |
| 45 | bool uploader_active, |
Bertrand SIMONNET | fec4d2c | 2015-08-05 16:04:14 -0700 | [diff] [blame] | 46 | bool dbus_enabled, |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 47 | MetricsLibraryInterface* metrics_lib, |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 48 | const std::string& vmstats_path, |
| 49 | const std::string& cpuinfo_max_freq_path, |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 50 | const std::string& scaling_max_freq_path, |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame] | 51 | const base::TimeDelta& upload_interval, |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 52 | const std::string& server, |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 53 | const std::string& metrics_file, |
| 54 | const std::string& config_root); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 55 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 56 | // Initializes DBus and MessageLoop variables before running the MessageLoop. |
| 57 | int OnInit() override; |
| 58 | |
| 59 | // Clean up data set up in OnInit before shutting down message loop. |
| 60 | void OnShutdown(int* return_code) override; |
| 61 | |
| 62 | // Does all the work. |
| 63 | int Run() override; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 64 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 65 | // Triggers an upload event and exit. (Used to test UploadService) |
| 66 | void RunUploaderTest(); |
| 67 | |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 68 | protected: |
| 69 | // Used also by the unit tests. |
| 70 | static const char kComprDataSizeName[]; |
| 71 | static const char kOrigDataSizeName[]; |
| 72 | static const char kZeroPagesName[]; |
| 73 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 74 | private: |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 75 | friend class MetricsDaemonTest; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 76 | FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 77 | FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent); |
| 78 | FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast); |
| 79 | FRIEND_TEST(MetricsDaemonTest, GetHistogramPath); |
| 80 | FRIEND_TEST(MetricsDaemonTest, IsNewEpoch); |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 81 | FRIEND_TEST(MetricsDaemonTest, MessageFilter); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 82 | FRIEND_TEST(MetricsDaemonTest, ParseVmStats); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 83 | FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 84 | FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo); |
| 85 | FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo2); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 86 | FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 87 | FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 88 | FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency); |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 89 | FRIEND_TEST(MetricsDaemonTest, ReadFreqToInt); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 90 | FRIEND_TEST(MetricsDaemonTest, ReportDiskStats); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 91 | FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval); |
| 92 | FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval); |
| 93 | FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 94 | FRIEND_TEST(MetricsDaemonTest, SendSample); |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 95 | FRIEND_TEST(MetricsDaemonTest, SendCpuThrottleMetrics); |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 96 | FRIEND_TEST(MetricsDaemonTest, SendZramMetrics); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 97 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 98 | // State for disk stats collector callback. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 99 | enum StatsState { |
| 100 | kStatsShort, // short wait before short interval collection |
| 101 | kStatsLong, // final wait before new collection |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 104 | // Data record for aggregating daily usage. |
| 105 | class UseRecord { |
| 106 | public: |
| 107 | UseRecord() : day_(0), seconds_(0) {} |
| 108 | int day_; |
| 109 | int seconds_; |
| 110 | }; |
| 111 | |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 112 | // Type of scale to use for meminfo histograms. For most of them we use |
| 113 | // percent of total RAM, but for some we use absolute numbers, usually in |
| 114 | // megabytes, on a log scale from 0 to 4000, and 0 to 8000 for compressed |
| 115 | // swap (since it can be larger than total RAM). |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 116 | enum MeminfoOp { |
| 117 | kMeminfoOp_HistPercent = 0, |
| 118 | kMeminfoOp_HistLog, |
| 119 | kMeminfoOp_SwapTotal, |
| 120 | kMeminfoOp_SwapFree, |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 123 | // Record for retrieving and reporting values from /proc/meminfo. |
| 124 | struct MeminfoRecord { |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 125 | const char* name; // print name |
| 126 | const char* match; // string to match in output of /proc/meminfo |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 127 | MeminfoOp op; // histogram scale selector, or other operator |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 128 | int value; // value from /proc/meminfo |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 131 | // Record for retrieving and reporting values from /proc/vmstat |
| 132 | struct VmstatRecord { |
| 133 | uint64_t page_faults_; // major faults |
| 134 | uint64_t swap_in_; // pages swapped in |
| 135 | uint64_t swap_out_; // pages swapped out |
| 136 | }; |
| 137 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 138 | // Metric parameters. |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 139 | static const char kMetricReadSectorsLongName[]; |
| 140 | static const char kMetricReadSectorsShortName[]; |
| 141 | static const char kMetricWriteSectorsLongName[]; |
| 142 | static const char kMetricWriteSectorsShortName[]; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 143 | static const char kMetricPageFaultsShortName[]; |
| 144 | static const char kMetricPageFaultsLongName[]; |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 145 | static const char kMetricSwapInLongName[]; |
| 146 | static const char kMetricSwapInShortName[]; |
| 147 | static const char kMetricSwapOutLongName[]; |
| 148 | static const char kMetricSwapOutShortName[]; |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 149 | static const char kMetricScaledCpuFrequencyName[]; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 150 | static const int kMetricStatsShortInterval; |
| 151 | static const int kMetricStatsLongInterval; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 152 | static const int kMetricMeminfoInterval; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 153 | static const int kMetricSectorsIOMax; |
| 154 | static const int kMetricSectorsBuckets; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 155 | static const int kMetricPageFaultsMax; |
| 156 | static const int kMetricPageFaultsBuckets; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 157 | static const char kMetricsDiskStatsPath[]; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 158 | static const char kMetricsVmStatsPath[]; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 159 | static const char kMetricsProcStatFileName[]; |
| 160 | static const int kMetricsProcStatFirstLineItemsCount; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 161 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 162 | // Returns the active time since boot (uptime minus sleep time) in seconds. |
| 163 | double GetActiveTime(); |
| 164 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 165 | // D-Bus filter callback. |
| 166 | static DBusHandlerResult MessageFilter(DBusConnection* connection, |
| 167 | DBusMessage* message, |
| 168 | void* user_data); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 169 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 170 | // Updates the daily usage file, if necessary, by adding |seconds| |
| 171 | // of active use to the |day| since Epoch. If there's usage data for |
| 172 | // day in the past in the usage file, that data is sent to UMA and |
| 173 | // removed from the file. If there's already usage data for |day| in |
| 174 | // the usage file, the |seconds| are accumulated. |
| 175 | void LogDailyUseRecord(int day, int seconds); |
| 176 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 177 | // Updates the active use time and logs time between user-space |
| 178 | // process crashes. |
| 179 | void ProcessUserCrash(); |
| 180 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 181 | // Updates the active use time and logs time between kernel crashes. |
| 182 | void ProcessKernelCrash(); |
| 183 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 184 | // Updates the active use time and logs time between unclean shutdowns. |
| 185 | void ProcessUncleanShutdown(); |
| 186 | |
| 187 | // Checks if a kernel crash has been detected and returns true if |
| 188 | // so. The method assumes that a kernel crash has happened if |
| 189 | // |crash_file| exists. It removes the file immediately if it |
| 190 | // exists, so it must not be called more than once. |
| 191 | bool CheckSystemCrash(const std::string& crash_file); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 192 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 193 | // Sends a regular (exponential) histogram sample to Chrome for |
| 194 | // transport to UMA. See MetricsLibrary::SendToUMA in |
| 195 | // metrics_library.h for a description of the arguments. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 196 | void SendSample(const std::string& name, int sample, |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 197 | int min, int max, int nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 198 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 199 | // Sends a linear histogram sample to Chrome for transport to UMA. See |
| 200 | // MetricsLibrary::SendToUMA in metrics_library.h for a description of the |
| 201 | // arguments. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 202 | void SendLinearSample(const std::string& name, int sample, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 203 | int max, int nbuckets); |
| 204 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 205 | // Sends various cumulative kernel crash-related stats, for instance the |
| 206 | // total number of kernel crashes since the last version update. |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 207 | void SendKernelCrashesCumulativeCountStats(); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 208 | |
| 209 | // Returns the total (system-wide) CPU usage between the time of the most |
| 210 | // recent call to this function and now. |
| 211 | base::TimeDelta GetIncrementalCpuUse(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 212 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 213 | // Sends a sample representing the number of seconds of active use |
| 214 | // for a 24-hour period. |
| 215 | void SendDailyUseSample(const scoped_ptr<PersistentInteger>& use); |
| 216 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 217 | // Sends a sample representing a time interval between two crashes of the |
| 218 | // same type. |
| 219 | void SendCrashIntervalSample(const scoped_ptr<PersistentInteger>& interval); |
| 220 | |
| 221 | // Sends a sample representing a frequency of crashes of some type. |
| 222 | void SendCrashFrequencySample(const scoped_ptr<PersistentInteger>& frequency); |
| 223 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 224 | // Initializes vm and disk stats reporting. |
| 225 | void StatsReporterInit(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 226 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 227 | // Schedules a callback for the next vm and disk stats collection. |
| 228 | void ScheduleStatsCallback(int wait); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 229 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 230 | // Reads cumulative disk statistics from sysfs. Returns true for success. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 231 | bool DiskStatsReadStats(uint64_t* read_sectors, uint64_t* write_sectors); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 232 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 233 | // Reads cumulative vm statistics from procfs. Returns true for success. |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 234 | bool VmStatsReadStats(struct VmstatRecord* stats); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 235 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 236 | // Parse cumulative vm statistics from a C string. Returns true for success. |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 237 | bool VmStatsParseStats(const char* stats, struct VmstatRecord* record); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 238 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 239 | // Reports disk and vm statistics. |
| 240 | void StatsCallback(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 241 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 242 | // Schedules meminfo collection callback. |
| 243 | void ScheduleMeminfoCallback(int wait); |
| 244 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 245 | // Reports memory statistics. Reschedules callback on success. |
| 246 | void MeminfoCallback(base::TimeDelta wait); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 247 | |
| 248 | // Parses content of /proc/meminfo and sends fields of interest to UMA. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 249 | // Returns false on errors. |meminfo_raw| contains the content of |
| 250 | // /proc/meminfo. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 251 | bool ProcessMeminfo(const std::string& meminfo_raw); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 252 | |
| 253 | // Parses meminfo data from |meminfo_raw|. |fields| is a vector containing |
| 254 | // the fields of interest. The order of the fields must be the same in which |
| 255 | // /proc/meminfo prints them. The result of parsing fields[i] is placed in |
| 256 | // fields[i].value. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 257 | bool FillMeminfo(const std::string& meminfo_raw, |
| 258 | std::vector<MeminfoRecord>* fields); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 259 | |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 260 | // Schedule a memory use callback in |interval| seconds. |
| 261 | void ScheduleMemuseCallback(double interval); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 262 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 263 | // Calls MemuseCallbackWork, and possibly schedules next callback, if enough |
| 264 | // active time has passed. Otherwise reschedules itself to simulate active |
| 265 | // time callbacks (i.e. wall clock time minus sleep time). |
| 266 | void MemuseCallback(); |
| 267 | |
| 268 | // Reads /proc/meminfo and sends total anonymous memory usage to UMA. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 269 | bool MemuseCallbackWork(); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 270 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 271 | // Parses meminfo data and sends it to UMA. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 272 | bool ProcessMemuse(const std::string& meminfo_raw); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 273 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 274 | // Sends stats for thermal CPU throttling. |
| 275 | void SendCpuThrottleMetrics(); |
| 276 | |
| 277 | // Reads an integer CPU frequency value from sysfs. |
| 278 | bool ReadFreqToInt(const std::string& sysfs_file_name, int* value); |
| 279 | |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 280 | // Reads the current OS version from /etc/lsb-release and hashes it |
| 281 | // to a unsigned 32-bit int. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 282 | uint32_t GetOsVersionHash(); |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 283 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 284 | // Updates stats, additionally sending them to UMA if enough time has elapsed |
| 285 | // since the last report. |
| 286 | void UpdateStats(base::TimeTicks now_ticks, base::Time now_wall_time); |
| 287 | |
| 288 | // Invoked periodically by |update_stats_timeout_id_| to call UpdateStats(). |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 289 | void HandleUpdateStatsTimeout(); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 290 | |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 291 | // Reports zram statistics. |
| 292 | bool ReportZram(const base::FilePath& zram_dir); |
| 293 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 294 | // Reads a string from a file and converts it to uint64_t. |
| 295 | static bool ReadFileToUint64(const base::FilePath& path, uint64_t* value); |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 296 | |
| 297 | // VARIABLES |
| 298 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 299 | // Test mode. |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 300 | bool testing_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 301 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 302 | // Whether the uploader is enabled or disabled. |
| 303 | bool uploader_active_; |
| 304 | |
Bertrand SIMONNET | fec4d2c | 2015-08-05 16:04:14 -0700 | [diff] [blame] | 305 | // Whether or not dbus should be used. |
| 306 | // If disabled, we will not collect the frequency of crashes. |
| 307 | bool dbus_enabled_; |
| 308 | |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 309 | // Root of the configuration files to use. |
| 310 | std::string config_root_; |
| 311 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 312 | // The metrics library handle. |
| 313 | MetricsLibraryInterface* metrics_lib_; |
| 314 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 315 | // Timestamps last network state update. This timestamp is used to |
| 316 | // sample the time from the network going online to going offline so |
| 317 | // TimeTicks ensures a monotonically increasing TimeDelta. |
| 318 | base::TimeTicks network_state_last_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 319 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 320 | // The last time that UpdateStats() was called. |
| 321 | base::TimeTicks last_update_stats_time_; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 322 | |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 323 | // End time of current memuse stat collection interval. |
| 324 | double memuse_final_time_; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 325 | |
| 326 | // Selects the wait time for the next memory use callback. |
| 327 | unsigned int memuse_interval_index_; |
| 328 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 329 | // Contain the most recent disk and vm cumulative stats. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 330 | uint64_t read_sectors_; |
| 331 | uint64_t write_sectors_; |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 332 | struct VmstatRecord vmstats_; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 333 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 334 | StatsState stats_state_; |
| 335 | double stats_initial_time_; |
| 336 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 337 | // The system "HZ", or frequency of ticks. Some system data uses ticks as a |
| 338 | // unit, and this is used to convert to standard time units. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 339 | uint32_t ticks_per_second_; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 340 | // Used internally by GetIncrementalCpuUse() to return the CPU utilization |
| 341 | // between calls. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 342 | uint64_t latest_cpu_use_ticks_; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 343 | |
| 344 | // Persistent values and accumulators for crash statistics. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 345 | scoped_ptr<PersistentInteger> daily_cycle_; |
| 346 | scoped_ptr<PersistentInteger> weekly_cycle_; |
| 347 | scoped_ptr<PersistentInteger> version_cycle_; |
| 348 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 349 | // Active use accumulated in a day. |
| 350 | scoped_ptr<PersistentInteger> daily_active_use_; |
| 351 | // Active use accumulated since the latest version update. |
| 352 | scoped_ptr<PersistentInteger> version_cumulative_active_use_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 353 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 354 | // The CPU time accumulator. This contains the CPU time, in milliseconds, |
| 355 | // used by the system since the most recent OS version update. |
| 356 | scoped_ptr<PersistentInteger> version_cumulative_cpu_use_; |
| 357 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 358 | scoped_ptr<PersistentInteger> user_crash_interval_; |
| 359 | scoped_ptr<PersistentInteger> kernel_crash_interval_; |
| 360 | scoped_ptr<PersistentInteger> unclean_shutdown_interval_; |
| 361 | |
| 362 | scoped_ptr<PersistentInteger> any_crashes_daily_count_; |
| 363 | scoped_ptr<PersistentInteger> any_crashes_weekly_count_; |
| 364 | scoped_ptr<PersistentInteger> user_crashes_daily_count_; |
| 365 | scoped_ptr<PersistentInteger> user_crashes_weekly_count_; |
| 366 | scoped_ptr<PersistentInteger> kernel_crashes_daily_count_; |
| 367 | scoped_ptr<PersistentInteger> kernel_crashes_weekly_count_; |
| 368 | scoped_ptr<PersistentInteger> kernel_crashes_version_count_; |
| 369 | scoped_ptr<PersistentInteger> unclean_shutdowns_daily_count_; |
| 370 | scoped_ptr<PersistentInteger> unclean_shutdowns_weekly_count_; |
| 371 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 372 | std::string vmstats_path_; |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 373 | std::string scaling_max_freq_path_; |
| 374 | std::string cpuinfo_max_freq_path_; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 375 | |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame] | 376 | base::TimeDelta upload_interval_; |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 377 | std::string server_; |
| 378 | std::string metrics_file_; |
| 379 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 380 | scoped_ptr<UploadService> upload_service_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 383 | #endif // METRICS_METRICS_DAEMON_H_ |