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