blob: 0a8f6a5aa5485fe16feb4b214f89ec4c6b466af7 [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Darin Petkov65b01462010-04-14 13:32:20 -070016
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070017#ifndef METRICS_METRICS_DAEMON_H_
18#define METRICS_METRICS_DAEMON_H_
Darin Petkov65b01462010-04-14 13:32:20 -070019
Ben Chanf05ab402014-08-07 00:54:59 -070020#include <stdint.h>
21
Ken Mixter4c5daa42010-08-26 18:35:06 -070022#include <map>
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070023#include <string>
24#include <vector>
Darin Petkov65b01462010-04-14 13:32:20 -070025
Ben Chan2e6543d2014-02-05 23:26:25 -080026#include <base/files/file_path.h>
Chris Masone817016a2011-05-12 14:14:48 -070027#include <base/memory/scoped_ptr.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080028#include <base/time/time.h>
Bertrand SIMONNET59890e22015-10-02 16:45:18 -070029#include <buffet/dbus-proxies.h>
Steve Funge86591e2014-12-01 13:38:21 -080030#include <chromeos/daemons/dbus_daemon.h>
Darin Petkovf1e85e42010-06-10 15:59:53 -070031#include <gtest/gtest_prod.h> // for FRIEND_TEST
32
Bertrand SIMONNET7a964052015-09-29 11:07:24 -070033#include "collectors/averaged_statistics_collector.h"
Bertrand SIMONNET5658dc52015-09-18 13:38:10 -070034#include "collectors/disk_usage_collector.h"
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070035#include "metrics/metrics_library.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070036#include "persistent_integer.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070037#include "uploader/upload_service.h"
Darin Petkovfc91b422010-05-12 13:05:45 -070038
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080039using chromeos_metrics::PersistentInteger;
Darin Petkov2ccef012010-05-05 16:06:37 -070040
Steve Funge86591e2014-12-01 13:38:21 -080041class MetricsDaemon : public chromeos::DBusDaemon {
Darin Petkov65b01462010-04-14 13:32:20 -070042 public:
Darin Petkovf1e85e42010-06-10 15:59:53 -070043 MetricsDaemon();
44 ~MetricsDaemon();
Darin Petkov65b01462010-04-14 13:32:20 -070045
Steve Funge86591e2014-12-01 13:38:21 -080046 // Initializes metrics class variables.
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070047 void Init(bool testing,
48 bool uploader_active,
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -070049 bool dbus_enabled,
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070050 MetricsLibraryInterface* metrics_lib,
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070051 const std::string& diskstats_path,
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070052 const std::string& cpuinfo_max_freq_path,
Steve Fung67906c62014-10-06 15:15:30 -070053 const std::string& scaling_max_freq_path,
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -070054 const base::TimeDelta& upload_interval,
Steve Fung67906c62014-10-06 15:15:30 -070055 const std::string& server,
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070056 const base::FilePath& metrics_directory);
Darin Petkov11b8eb32010-05-18 11:00:59 -070057
Steve Funge86591e2014-12-01 13:38:21 -080058 // Initializes DBus and MessageLoop variables before running the MessageLoop.
59 int OnInit() override;
60
61 // Clean up data set up in OnInit before shutting down message loop.
62 void OnShutdown(int* return_code) override;
63
64 // Does all the work.
65 int Run() override;
Darin Petkov65b01462010-04-14 13:32:20 -070066
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070067 // Triggers an upload event and exit. (Used to test UploadService)
68 void RunUploaderTest();
69
Bertrand SIMONNET7a964052015-09-29 11:07:24 -070070 // Returns the active time since boot (uptime minus sleep time) in seconds.
71 static double GetActiveTime();
72
Luigi Semenzato96360192014-06-04 10:53:35 -070073 protected:
74 // Used also by the unit tests.
75 static const char kComprDataSizeName[];
76 static const char kOrigDataSizeName[];
77 static const char kZeroPagesName[];
78
Darin Petkov65b01462010-04-14 13:32:20 -070079 private:
Darin Petkov2ccef012010-05-05 16:06:37 -070080 friend class MetricsDaemonTest;
Ken Mixterccd84c02010-08-16 19:57:13 -070081 FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash);
Ken Mixter4c5daa42010-08-26 18:35:06 -070082 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent);
83 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast);
84 FRIEND_TEST(MetricsDaemonTest, GetHistogramPath);
85 FRIEND_TEST(MetricsDaemonTest, IsNewEpoch);
Darin Petkove579d662010-05-05 16:19:39 -070086 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkov38d5cb02010-06-24 12:10:26 -070087 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -070088 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo);
89 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo2);
Ken Mixterccd84c02010-08-16 19:57:13 -070090 FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown);
Darin Petkov1bb904e2010-06-16 15:58:06 -070091 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070092 FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070093 FRIEND_TEST(MetricsDaemonTest, ReadFreqToInt);
Ken Mixterccd84c02010-08-16 19:57:13 -070094 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval);
95 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval);
96 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080097 FRIEND_TEST(MetricsDaemonTest, SendSample);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070098 FRIEND_TEST(MetricsDaemonTest, SendCpuThrottleMetrics);
Luigi Semenzato96360192014-06-04 10:53:35 -070099 FRIEND_TEST(MetricsDaemonTest, SendZramMetrics);
Darin Petkov41e06232010-05-03 16:45:37 -0700100
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800101 // 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 Semenzato942cbab2013-02-12 13:17:07 -0800105 enum MeminfoOp {
106 kMeminfoOp_HistPercent = 0,
107 kMeminfoOp_HistLog,
108 kMeminfoOp_SwapTotal,
109 kMeminfoOp_SwapFree,
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800110 };
111
Luigi Semenzato8accd332011-05-17 16:37:18 -0700112 // Record for retrieving and reporting values from /proc/meminfo.
113 struct MeminfoRecord {
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800114 const char* name; // print name
115 const char* match; // string to match in output of /proc/meminfo
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800116 MeminfoOp op; // histogram scale selector, or other operator
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800117 int value; // value from /proc/meminfo
Luigi Semenzato8accd332011-05-17 16:37:18 -0700118 };
119
Darin Petkov703ec972010-04-27 11:02:18 -0700120 // D-Bus filter callback.
121 static DBusHandlerResult MessageFilter(DBusConnection* connection,
122 DBusMessage* message,
123 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700124
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700125 // Callback for Weave commands.
126 void OnWeaveCommand(com::android::Weave::CommandProxy* command);
127
128 // Enables metrics reporting.
129 void OnEnableMetrics(com::android::Weave::CommandProxy* command);
130
131 // Disables metrics reporting.
132 void OnDisableMetrics(com::android::Weave::CommandProxy* command);
133
134 // Updates the weave device state.
135 void UpdateWeaveState(com::android::Weave::ManagerProxy* manager);
136
137 // Tells Weave that the state has changed.
138 void NotifyStateChanged();
139
Darin Petkov1bb904e2010-06-16 15:58:06 -0700140 // Updates the active use time and logs time between user-space
141 // process crashes.
142 void ProcessUserCrash();
143
Darin Petkov38d5cb02010-06-24 12:10:26 -0700144 // Updates the active use time and logs time between kernel crashes.
145 void ProcessKernelCrash();
146
Ken Mixterccd84c02010-08-16 19:57:13 -0700147 // Updates the active use time and logs time between unclean shutdowns.
148 void ProcessUncleanShutdown();
149
150 // Checks if a kernel crash has been detected and returns true if
151 // so. The method assumes that a kernel crash has happened if
152 // |crash_file| exists. It removes the file immediately if it
153 // exists, so it must not be called more than once.
154 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700155
Darin Petkov11b8eb32010-05-18 11:00:59 -0700156 // Sends a regular (exponential) histogram sample to Chrome for
157 // transport to UMA. See MetricsLibrary::SendToUMA in
158 // metrics_library.h for a description of the arguments.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800159 void SendSample(const std::string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -0700160 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700161
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700162 // Sends a linear histogram sample to Chrome for transport to UMA. See
163 // MetricsLibrary::SendToUMA in metrics_library.h for a description of the
164 // arguments.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800165 void SendLinearSample(const std::string& name, int sample,
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700166 int max, int nbuckets);
167
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700168 // Sends various cumulative kernel crash-related stats, for instance the
169 // total number of kernel crashes since the last version update.
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700170 void SendKernelCrashesCumulativeCountStats();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700171
172 // Returns the total (system-wide) CPU usage between the time of the most
173 // recent call to this function and now.
174 base::TimeDelta GetIncrementalCpuUse();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800175
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700176 // Sends a sample representing the number of seconds of active use
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700177 // for a 24-hour period and reset |use|.
178 void SendAndResetDailyUseSample(
179 const scoped_ptr<PersistentInteger>& use);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700180
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800181 // Sends a sample representing a time interval between two crashes of the
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700182 // same type and reset |interval|.
183 void SendAndResetCrashIntervalSample(
184 const scoped_ptr<PersistentInteger>& interval);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800185
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700186 // Sends a sample representing a frequency of crashes of some type and reset
187 // |frequency|.
188 void SendAndResetCrashFrequencySample(
189 const scoped_ptr<PersistentInteger>& frequency);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800190
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700191 // Initializes vm and disk stats reporting.
192 void StatsReporterInit();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800193
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700194 // Schedules meminfo collection callback.
195 void ScheduleMeminfoCallback(int wait);
196
Steve Funge86591e2014-12-01 13:38:21 -0800197 // Reports memory statistics. Reschedules callback on success.
198 void MeminfoCallback(base::TimeDelta wait);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700199
200 // Parses content of /proc/meminfo and sends fields of interest to UMA.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700201 // Returns false on errors. |meminfo_raw| contains the content of
202 // /proc/meminfo.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700203 bool ProcessMeminfo(const std::string& meminfo_raw);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700204
205 // Parses meminfo data from |meminfo_raw|. |fields| is a vector containing
206 // the fields of interest. The order of the fields must be the same in which
207 // /proc/meminfo prints them. The result of parsing fields[i] is placed in
208 // fields[i].value.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700209 bool FillMeminfo(const std::string& meminfo_raw,
210 std::vector<MeminfoRecord>* fields);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700211
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800212 // Schedule a memory use callback in |interval| seconds.
213 void ScheduleMemuseCallback(double interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700214
Luigi Semenzato8accd332011-05-17 16:37:18 -0700215 // Calls MemuseCallbackWork, and possibly schedules next callback, if enough
216 // active time has passed. Otherwise reschedules itself to simulate active
217 // time callbacks (i.e. wall clock time minus sleep time).
218 void MemuseCallback();
219
220 // Reads /proc/meminfo and sends total anonymous memory usage to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700221 bool MemuseCallbackWork();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700222
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700223 // Parses meminfo data and sends it to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700224 bool ProcessMemuse(const std::string& meminfo_raw);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700225
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700226 // Sends stats for thermal CPU throttling.
227 void SendCpuThrottleMetrics();
228
229 // Reads an integer CPU frequency value from sysfs.
230 bool ReadFreqToInt(const std::string& sysfs_file_name, int* value);
231
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800232 // Reads the current OS version from /etc/lsb-release and hashes it
233 // to a unsigned 32-bit int.
Ben Chanf05ab402014-08-07 00:54:59 -0700234 uint32_t GetOsVersionHash();
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800235
Daniel Eratc83975a2014-04-04 08:53:44 -0700236 // Updates stats, additionally sending them to UMA if enough time has elapsed
237 // since the last report.
238 void UpdateStats(base::TimeTicks now_ticks, base::Time now_wall_time);
239
240 // Invoked periodically by |update_stats_timeout_id_| to call UpdateStats().
Steve Funge86591e2014-12-01 13:38:21 -0800241 void HandleUpdateStatsTimeout();
Daniel Eratc83975a2014-04-04 08:53:44 -0700242
Luigi Semenzato96360192014-06-04 10:53:35 -0700243 // Reports zram statistics.
244 bool ReportZram(const base::FilePath& zram_dir);
245
Ben Chanf05ab402014-08-07 00:54:59 -0700246 // Reads a string from a file and converts it to uint64_t.
247 static bool ReadFileToUint64(const base::FilePath& path, uint64_t* value);
Luigi Semenzato96360192014-06-04 10:53:35 -0700248
249 // VARIABLES
250
Darin Petkov2ccef012010-05-05 16:06:37 -0700251 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700252 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700253
Steve Funge86591e2014-12-01 13:38:21 -0800254 // Whether the uploader is enabled or disabled.
255 bool uploader_active_;
256
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700257 // Whether or not dbus should be used.
258 // If disabled, we will not collect the frequency of crashes.
259 bool dbus_enabled_;
260
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700261 // Root of the configuration files to use.
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700262 base::FilePath metrics_directory_;
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700263
Darin Petkovfc91b422010-05-12 13:05:45 -0700264 // The metrics library handle.
265 MetricsLibraryInterface* metrics_lib_;
266
Daniel Eratc83975a2014-04-04 08:53:44 -0700267 // The last time that UpdateStats() was called.
268 base::TimeTicks last_update_stats_time_;
Darin Petkov41e06232010-05-03 16:45:37 -0700269
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800270 // End time of current memuse stat collection interval.
271 double memuse_final_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700272
273 // Selects the wait time for the next memory use callback.
274 unsigned int memuse_interval_index_;
275
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700276 // The system "HZ", or frequency of ticks. Some system data uses ticks as a
277 // unit, and this is used to convert to standard time units.
Ben Chanf05ab402014-08-07 00:54:59 -0700278 uint32_t ticks_per_second_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700279 // Used internally by GetIncrementalCpuUse() to return the CPU utilization
280 // between calls.
Ben Chanf05ab402014-08-07 00:54:59 -0700281 uint64_t latest_cpu_use_ticks_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700282
283 // Persistent values and accumulators for crash statistics.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800284 scoped_ptr<PersistentInteger> daily_cycle_;
285 scoped_ptr<PersistentInteger> weekly_cycle_;
286 scoped_ptr<PersistentInteger> version_cycle_;
287
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700288 // Active use accumulated in a day.
289 scoped_ptr<PersistentInteger> daily_active_use_;
290 // Active use accumulated since the latest version update.
291 scoped_ptr<PersistentInteger> version_cumulative_active_use_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800292
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700293 // The CPU time accumulator. This contains the CPU time, in milliseconds,
294 // used by the system since the most recent OS version update.
295 scoped_ptr<PersistentInteger> version_cumulative_cpu_use_;
296
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800297 scoped_ptr<PersistentInteger> user_crash_interval_;
298 scoped_ptr<PersistentInteger> kernel_crash_interval_;
299 scoped_ptr<PersistentInteger> unclean_shutdown_interval_;
300
301 scoped_ptr<PersistentInteger> any_crashes_daily_count_;
302 scoped_ptr<PersistentInteger> any_crashes_weekly_count_;
303 scoped_ptr<PersistentInteger> user_crashes_daily_count_;
304 scoped_ptr<PersistentInteger> user_crashes_weekly_count_;
305 scoped_ptr<PersistentInteger> kernel_crashes_daily_count_;
306 scoped_ptr<PersistentInteger> kernel_crashes_weekly_count_;
307 scoped_ptr<PersistentInteger> kernel_crashes_version_count_;
308 scoped_ptr<PersistentInteger> unclean_shutdowns_daily_count_;
309 scoped_ptr<PersistentInteger> unclean_shutdowns_weekly_count_;
Bertrand SIMONNET5658dc52015-09-18 13:38:10 -0700310 scoped_ptr<DiskUsageCollector> disk_usage_collector_;
Bertrand SIMONNET7a964052015-09-29 11:07:24 -0700311 scoped_ptr<AveragedStatisticsCollector> averaged_stats_collector_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800312
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700313 std::string scaling_max_freq_path_;
314 std::string cpuinfo_max_freq_path_;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700315
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700316 base::TimeDelta upload_interval_;
Steve Fung67906c62014-10-06 15:15:30 -0700317 std::string server_;
Steve Fung67906c62014-10-06 15:15:30 -0700318
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700319 scoped_ptr<UploadService> upload_service_;
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700320 scoped_ptr<com::android::Weave::ObjectManagerProxy> weaved_object_mgr_;
Darin Petkov65b01462010-04-14 13:32:20 -0700321};
322
Luigi Semenzato4a6c9422014-06-30 18:12:28 -0700323#endif // METRICS_METRICS_DAEMON_H_