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