blob: bdd181c6410f7817d8f07586013a235709bc1df3 [file] [log] [blame]
Darin Petkov8032dd02011-05-09 16:33:19 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkov65b01462010-04-14 13:32:20 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Luigi Semenzato4a6c9422014-06-30 18:12:28 -07005// For PRIu64 in inttypes.h, used by scanf. TODO(semenzato): replace
6// with libchromeos methods.
7#define __STDC_FORMAT_MACROS
8
9#include "metrics/metrics_daemon.h"
Darin Petkov65b01462010-04-14 13:32:20 -070010
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080011#include <fcntl.h>
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070012#include <inttypes.h>
Luigi Semenzato8accd332011-05-17 16:37:18 -070013#include <math.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070014#include <string.h>
Luigi Semenzato8accd332011-05-17 16:37:18 -070015#include <time.h>
Darin Petkov65b01462010-04-14 13:32:20 -070016
Luigi Semenzato859b3f02014-02-05 15:33:19 -080017#include <base/files/file_path.h>
Ben Chan51bf92a2014-09-05 08:21:06 -070018#include <base/files/file_util.h>
Luigi Semenzato859b3f02014-02-05 15:33:19 -080019#include <base/hash.h>
Darin Petkov65b01462010-04-14 13:32:20 -070020#include <base/logging.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080021#include <base/strings/string_number_conversions.h>
22#include <base/strings/string_split.h>
23#include <base/strings/string_util.h>
24#include <base/strings/stringprintf.h>
Luigi Semenzato859b3f02014-02-05 15:33:19 -080025#include <base/sys_info.h>
Darin Petkov40f25732013-04-29 15:07:31 +020026#include <chromeos/dbus/service_constants.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070027#include <dbus/dbus-glib-lowlevel.h>
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070028#include "uploader/upload_service.h"
Darin Petkov65b01462010-04-14 13:32:20 -070029
Ben Chan2e6543d2014-02-05 23:26:25 -080030using base::FilePath;
31using base::StringPrintf;
Darin Petkovf27f0362010-06-04 13:14:19 -070032using base::Time;
33using base::TimeDelta;
34using base::TimeTicks;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080035using chromeos_metrics::PersistentInteger;
Luigi Semenzato8accd332011-05-17 16:37:18 -070036using std::map;
Darin Petkov38d5cb02010-06-24 12:10:26 -070037using std::string;
Luigi Semenzato8accd332011-05-17 16:37:18 -070038using std::vector;
39
Daniel Eratc83975a2014-04-04 08:53:44 -070040namespace {
Darin Petkovf27f0362010-06-04 13:14:19 -070041
Darin Petkov703ec972010-04-27 11:02:18 -070042#define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error")
Darin Petkov40f25732013-04-29 15:07:31 +020043
Daniel Eratc83975a2014-04-04 08:53:44 -070044const char kCrashReporterInterface[] = "org.chromium.CrashReporter";
45const char kCrashReporterUserCrashSignal[] = "UserCrash";
Darin Petkov41e06232010-05-03 16:45:37 -070046
Daniel Eratc83975a2014-04-04 08:53:44 -070047const int kSecondsPerMinute = 60;
48const int kMinutesPerHour = 60;
49const int kHoursPerDay = 24;
50const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour;
51const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay;
52const int kDaysPerWeek = 7;
53const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek;
Darin Petkov41e06232010-05-03 16:45:37 -070054
Daniel Eratc83975a2014-04-04 08:53:44 -070055// Interval between calls to UpdateStats().
56const guint kUpdateStatsIntervalMs = 300000;
Darin Petkov65b01462010-04-14 13:32:20 -070057
Luigi Semenzatoc5a92342014-02-14 15:05:51 -080058const char kKernelCrashDetectedFile[] = "/var/run/kernel-crash-detected";
Daniel Eratc83975a2014-04-04 08:53:44 -070059const char kUncleanShutdownDetectedFile[] =
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080060 "/var/run/unclean-shutdown-detected";
Ken Mixterccd84c02010-08-16 19:57:13 -070061
Daniel Eratc83975a2014-04-04 08:53:44 -070062} // namespace
63
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080064// disk stats metrics
65
66// The {Read,Write}Sectors numbers are in sectors/second.
67// A sector is usually 512 bytes.
68
69const char MetricsDaemon::kMetricReadSectorsLongName[] =
70 "Platform.ReadSectorsLong";
71const char MetricsDaemon::kMetricWriteSectorsLongName[] =
72 "Platform.WriteSectorsLong";
73const char MetricsDaemon::kMetricReadSectorsShortName[] =
74 "Platform.ReadSectorsShort";
75const char MetricsDaemon::kMetricWriteSectorsShortName[] =
76 "Platform.WriteSectorsShort";
77
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070078const int MetricsDaemon::kMetricStatsShortInterval = 1; // seconds
79const int MetricsDaemon::kMetricStatsLongInterval = 30; // seconds
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080080
Luigi Semenzato29c7ef92011-04-12 14:12:35 -070081const int MetricsDaemon::kMetricMeminfoInterval = 30; // seconds
82
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080083// Assume a max rate of 250Mb/s for reads (worse for writes) and 512 byte
84// sectors.
85const int MetricsDaemon::kMetricSectorsIOMax = 500000; // sectors/second
86const int MetricsDaemon::kMetricSectorsBuckets = 50; // buckets
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070087// Page size is 4k, sector size is 0.5k. We're not interested in page fault
88// rates that the disk cannot sustain.
89const int MetricsDaemon::kMetricPageFaultsMax = kMetricSectorsIOMax / 8;
90const int MetricsDaemon::kMetricPageFaultsBuckets = 50;
91
92// Major page faults, i.e. the ones that require data to be read from disk.
93
94const char MetricsDaemon::kMetricPageFaultsLongName[] =
95 "Platform.PageFaultsLong";
96const char MetricsDaemon::kMetricPageFaultsShortName[] =
97 "Platform.PageFaultsShort";
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080098
Sonny Rao4b8aebb2013-07-31 23:18:31 -070099// Swap in and Swap out
100
101const char MetricsDaemon::kMetricSwapInLongName[] =
102 "Platform.SwapInLong";
103const char MetricsDaemon::kMetricSwapInShortName[] =
104 "Platform.SwapInShort";
105
106const char MetricsDaemon::kMetricSwapOutLongName[] =
107 "Platform.SwapOutLong";
108const char MetricsDaemon::kMetricSwapOutShortName[] =
109 "Platform.SwapOutShort";
110
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700111const char MetricsDaemon::kMetricsProcStatFileName[] = "/proc/stat";
112const int MetricsDaemon::kMetricsProcStatFirstLineItemsCount = 11;
113
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700114// Thermal CPU throttling.
115
116const char MetricsDaemon::kMetricScaledCpuFrequencyName[] =
117 "Platform.CpuFrequencyThermalScaling";
118
Luigi Semenzato96360192014-06-04 10:53:35 -0700119// Zram sysfs entries.
120
121const char MetricsDaemon::kComprDataSizeName[] = "compr_data_size";
122const char MetricsDaemon::kOrigDataSizeName[] = "orig_data_size";
123const char MetricsDaemon::kZeroPagesName[] = "zero_pages";
124
Luigi Semenzato8accd332011-05-17 16:37:18 -0700125// Memory use stats collection intervals. We collect some memory use interval
126// at these intervals after boot, and we stop collecting after the last one,
127// with the assumption that in most cases the memory use won't change much
128// after that.
129static const int kMemuseIntervals[] = {
130 1 * kSecondsPerMinute, // 1 minute mark
131 4 * kSecondsPerMinute, // 5 minute mark
132 25 * kSecondsPerMinute, // 0.5 hour mark
133 120 * kSecondsPerMinute, // 2.5 hour mark
134 600 * kSecondsPerMinute, // 12.5 hour mark
135};
136
Darin Petkovf1e85e42010-06-10 15:59:53 -0700137MetricsDaemon::MetricsDaemon()
Daniel Eratc83975a2014-04-04 08:53:44 -0700138 : update_stats_timeout_id_(-1),
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800139 memuse_final_time_(0),
Luigi Semenzato8accd332011-05-17 16:37:18 -0700140 memuse_interval_index_(0),
141 read_sectors_(0),
142 write_sectors_(0),
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700143 vmstats_(),
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700144 stats_state_(kStatsShort),
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700145 stats_initial_time_(0),
146 ticks_per_second_(0),
147 latest_cpu_use_ticks_(0) {}
Darin Petkovf1e85e42010-06-10 15:59:53 -0700148
Ken Mixter4c5daa42010-08-26 18:35:06 -0700149MetricsDaemon::~MetricsDaemon() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700150 if (update_stats_timeout_id_ > -1)
151 g_source_remove(update_stats_timeout_id_);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700152}
153
Luigi Semenzato8accd332011-05-17 16:37:18 -0700154double MetricsDaemon::GetActiveTime() {
155 struct timespec ts;
156 int r = clock_gettime(CLOCK_MONOTONIC, &ts);
157 if (r < 0) {
158 PLOG(WARNING) << "clock_gettime(CLOCK_MONOTONIC) failed";
159 return 0;
160 } else {
Luigi Semenzato4a6c9422014-06-30 18:12:28 -0700161 return ts.tv_sec + static_cast<double>(ts.tv_nsec) / (1000 * 1000 * 1000);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700162 }
163}
164
Darin Petkov2ccef012010-05-05 16:06:37 -0700165void MetricsDaemon::Run(bool run_as_daemon) {
Darin Petkov38d5cb02010-06-24 12:10:26 -0700166 if (run_as_daemon && daemon(0, 0) != 0)
167 return;
168
Ken Mixterccd84c02010-08-16 19:57:13 -0700169 if (CheckSystemCrash(kKernelCrashDetectedFile)) {
170 ProcessKernelCrash();
171 }
172
173 if (CheckSystemCrash(kUncleanShutdownDetectedFile)) {
174 ProcessUncleanShutdown();
175 }
176
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800177 // On OS version change, clear version stats (which are reported daily).
Ben Chanf05ab402014-08-07 00:54:59 -0700178 int32_t version = GetOsVersionHash();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800179 if (version_cycle_->Get() != version) {
180 version_cycle_->Set(version);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800181 kernel_crashes_version_count_->Set(0);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700182 version_cumulative_active_use_->Set(0);
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700183 version_cumulative_cpu_use_->Set(0);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800184 }
185
Darin Petkov38d5cb02010-06-24 12:10:26 -0700186 Loop();
Darin Petkov65b01462010-04-14 13:32:20 -0700187}
188
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700189void MetricsDaemon::RunUploaderTest() {
Steve Fung67906c62014-10-06 15:15:30 -0700190 upload_service_.reset(new UploadService(testing_, server_));
191 upload_service_->Init(upload_interval_secs_, metrics_file_);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700192 upload_service_->UploadEvent();
193}
194
Ben Chanf05ab402014-08-07 00:54:59 -0700195uint32_t MetricsDaemon::GetOsVersionHash() {
196 static uint32_t cached_version_hash = 0;
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800197 static bool version_hash_is_cached = false;
198 if (version_hash_is_cached)
199 return cached_version_hash;
200 version_hash_is_cached = true;
201 std::string version;
202 if (base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_VERSION", &version)) {
203 cached_version_hash = base::Hash(version);
204 } else if (testing_) {
205 cached_version_hash = 42; // return any plausible value for the hash
206 } else {
207 LOG(FATAL) << "could not find CHROMEOS_RELEASE_VERSION";
208 }
209 return cached_version_hash;
210}
211
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700212void MetricsDaemon::Init(bool testing,
213 bool uploader_active,
214 MetricsLibraryInterface* metrics_lib,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700215 const string& diskstats_path,
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700216 const string& vmstats_path,
217 const string& scaling_max_freq_path,
Steve Fung67906c62014-10-06 15:15:30 -0700218 const string& cpuinfo_max_freq_path,
219 int upload_interval_secs,
220 const string& server,
221 const string& metrics_file) {
Darin Petkov65b01462010-04-14 13:32:20 -0700222 testing_ = testing;
Alex Vakulenko14595032014-08-28 14:59:56 -0700223 DCHECK(metrics_lib != nullptr);
Darin Petkovfc91b422010-05-12 13:05:45 -0700224 metrics_lib_ = metrics_lib;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700225
Steve Fung67906c62014-10-06 15:15:30 -0700226 upload_interval_secs_ = upload_interval_secs;
227 server_ = server;
228 metrics_file_ = metrics_file;
229
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700230 // Get ticks per second (HZ) on this system.
231 // Sysconf cannot fail, so no sanity checks are needed.
232 ticks_per_second_ = sysconf(_SC_CLK_TCK);
233
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700234 daily_active_use_.reset(
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800235 new PersistentInteger("Logging.DailyUseTime"));
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700236 version_cumulative_active_use_.reset(
237 new PersistentInteger("Logging.CumulativeDailyUseTime"));
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700238 version_cumulative_cpu_use_.reset(
239 new PersistentInteger("Logging.CumulativeCpuTime"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700240
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800241 kernel_crash_interval_.reset(
242 new PersistentInteger("Logging.KernelCrashInterval"));
243 unclean_shutdown_interval_.reset(
244 new PersistentInteger("Logging.UncleanShutdownInterval"));
245 user_crash_interval_.reset(
246 new PersistentInteger("Logging.UserCrashInterval"));
Darin Petkov2ccef012010-05-05 16:06:37 -0700247
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800248 any_crashes_daily_count_.reset(
249 new PersistentInteger("Logging.AnyCrashesDaily"));
250 any_crashes_weekly_count_.reset(
251 new PersistentInteger("Logging.AnyCrashesWeekly"));
252 user_crashes_daily_count_.reset(
253 new PersistentInteger("Logging.UserCrashesDaily"));
254 user_crashes_weekly_count_.reset(
255 new PersistentInteger("Logging.UserCrashesWeekly"));
256 kernel_crashes_daily_count_.reset(
257 new PersistentInteger("Logging.KernelCrashesDaily"));
258 kernel_crashes_weekly_count_.reset(
259 new PersistentInteger("Logging.KernelCrashesWeekly"));
260 kernel_crashes_version_count_.reset(
261 new PersistentInteger("Logging.KernelCrashesSinceUpdate"));
262 unclean_shutdowns_daily_count_.reset(
263 new PersistentInteger("Logging.UncleanShutdownsDaily"));
264 unclean_shutdowns_weekly_count_.reset(
265 new PersistentInteger("Logging.UncleanShutdownsWeekly"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700266
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800267 daily_cycle_.reset(new PersistentInteger("daily.cycle"));
268 weekly_cycle_.reset(new PersistentInteger("weekly.cycle"));
269 version_cycle_.reset(new PersistentInteger("version.cycle"));
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800270
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700271 diskstats_path_ = diskstats_path;
272 vmstats_path_ = vmstats_path;
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700273 scaling_max_freq_path_ = scaling_max_freq_path;
274 cpuinfo_max_freq_path_ = cpuinfo_max_freq_path;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700275 StatsReporterInit();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800276
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700277 // Start collecting meminfo stats.
278 ScheduleMeminfoCallback(kMetricMeminfoInterval);
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800279 memuse_final_time_ = GetActiveTime() + kMemuseIntervals[0];
280 ScheduleMemuseCallback(kMemuseIntervals[0]);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700281
Darin Petkov2ccef012010-05-05 16:06:37 -0700282 // Don't setup D-Bus and GLib in test mode.
283 if (testing)
284 return;
Darin Petkov65b01462010-04-14 13:32:20 -0700285
Darin Petkov703ec972010-04-27 11:02:18 -0700286 g_type_init();
Ben Chan6f598422013-06-22 06:29:36 -0700287 dbus_threads_init_default();
Darin Petkov65b01462010-04-14 13:32:20 -0700288
Darin Petkov703ec972010-04-27 11:02:18 -0700289 DBusError error;
290 dbus_error_init(&error);
Darin Petkov65b01462010-04-14 13:32:20 -0700291
David James3b3add52010-06-04 15:01:19 -0700292 DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkov703ec972010-04-27 11:02:18 -0700293 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
294 "No D-Bus connection: " << SAFE_MESSAGE(error);
Darin Petkov65b01462010-04-14 13:32:20 -0700295
Alex Vakulenko14595032014-08-28 14:59:56 -0700296 dbus_connection_setup_with_g_main(connection, nullptr);
Darin Petkov65b01462010-04-14 13:32:20 -0700297
Darin Petkov40f25732013-04-29 15:07:31 +0200298 vector<string> matches;
299 matches.push_back(
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800300 base::StringPrintf("type='signal',interface='%s',path='/',member='%s'",
301 kCrashReporterInterface,
302 kCrashReporterUserCrashSignal));
Darin Petkov40f25732013-04-29 15:07:31 +0200303
Darin Petkov703ec972010-04-27 11:02:18 -0700304 // Registers D-Bus matches for the signals we would like to catch.
Darin Petkov40f25732013-04-29 15:07:31 +0200305 for (vector<string>::const_iterator it = matches.begin();
306 it != matches.end(); ++it) {
307 const char* match = it->c_str();
Darin Petkov41e06232010-05-03 16:45:37 -0700308 DLOG(INFO) << "adding dbus match: " << match;
Darin Petkov703ec972010-04-27 11:02:18 -0700309 dbus_bus_add_match(connection, match, &error);
310 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
311 "unable to add a match: " << SAFE_MESSAGE(error);
312 }
313
314 // Adds the D-Bus filter routine to be called back whenever one of
315 // the registered D-Bus matches is successful. The daemon is not
316 // activated for D-Bus messages that don't match.
Alex Vakulenko14595032014-08-28 14:59:56 -0700317 CHECK(dbus_connection_add_filter(connection, MessageFilter, this, nullptr));
Daniel Eratc83975a2014-04-04 08:53:44 -0700318
319 update_stats_timeout_id_ =
320 g_timeout_add(kUpdateStatsIntervalMs, &HandleUpdateStatsTimeout, this);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700321
322 if (uploader_active) {
Nathan Bullock0be0f732014-09-11 10:29:17 -0400323 LOG(INFO) << "uploader enabled";
Steve Fung67906c62014-10-06 15:15:30 -0700324 upload_service_.reset(new UploadService(testing_, server_));
325 upload_service_->Init(upload_interval_secs_, metrics_file_);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700326 }
Darin Petkov65b01462010-04-14 13:32:20 -0700327}
328
329void MetricsDaemon::Loop() {
Alex Vakulenko14595032014-08-28 14:59:56 -0700330 GMainLoop* loop = g_main_loop_new(nullptr, false);
Darin Petkov703ec972010-04-27 11:02:18 -0700331 g_main_loop_run(loop);
Darin Petkov65b01462010-04-14 13:32:20 -0700332}
333
Darin Petkov703ec972010-04-27 11:02:18 -0700334// static
335DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
336 DBusMessage* message,
337 void* user_data) {
Darin Petkov703ec972010-04-27 11:02:18 -0700338 int message_type = dbus_message_get_type(message);
339 if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) {
Darin Petkov41e06232010-05-03 16:45:37 -0700340 DLOG(WARNING) << "unexpected message type " << message_type;
Darin Petkov703ec972010-04-27 11:02:18 -0700341 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
342 }
343
344 // Signal messages always have interfaces.
Daniel Eratc83975a2014-04-04 08:53:44 -0700345 const std::string interface(dbus_message_get_interface(message));
346 const std::string member(dbus_message_get_member(message));
347 DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal";
Darin Petkov703ec972010-04-27 11:02:18 -0700348
349 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data);
350
351 DBusMessageIter iter;
352 dbus_message_iter_init(message, &iter);
Daniel Eratc83975a2014-04-04 08:53:44 -0700353 if (interface == kCrashReporterInterface) {
354 CHECK_EQ(member, kCrashReporterUserCrashSignal);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700355 daemon->ProcessUserCrash();
Darin Petkov703ec972010-04-27 11:02:18 -0700356 } else {
Daniel Eratc83975a2014-04-04 08:53:44 -0700357 // Ignore messages from the bus itself.
Darin Petkov703ec972010-04-27 11:02:18 -0700358 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
359 }
360
361 return DBUS_HANDLER_RESULT_HANDLED;
Darin Petkov65b01462010-04-14 13:32:20 -0700362}
363
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700364// One might argue that parts of this should go into
365// chromium/src/base/sys_info_chromeos.c instead, but put it here for now.
366
367TimeDelta MetricsDaemon::GetIncrementalCpuUse() {
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700368 FilePath proc_stat_path = FilePath(kMetricsProcStatFileName);
369 std::string proc_stat_string;
370 if (!base::ReadFileToString(proc_stat_path, &proc_stat_string)) {
371 LOG(WARNING) << "cannot open " << kMetricsProcStatFileName;
372 return TimeDelta();
373 }
374
375 std::vector<std::string> proc_stat_lines;
376 base::SplitString(proc_stat_string, '\n', &proc_stat_lines);
377 if (proc_stat_lines.empty()) {
378 LOG(WARNING) << "cannot parse " << kMetricsProcStatFileName
379 << ": " << proc_stat_string;
380 return TimeDelta();
381 }
382 std::vector<std::string> proc_stat_totals;
383 base::SplitStringAlongWhitespace(proc_stat_lines[0], &proc_stat_totals);
384
Ben Chanf05ab402014-08-07 00:54:59 -0700385 uint64_t user_ticks, user_nice_ticks, system_ticks;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700386 if (proc_stat_totals.size() != kMetricsProcStatFirstLineItemsCount ||
387 proc_stat_totals[0] != "cpu" ||
388 !base::StringToUint64(proc_stat_totals[1], &user_ticks) ||
389 !base::StringToUint64(proc_stat_totals[2], &user_nice_ticks) ||
390 !base::StringToUint64(proc_stat_totals[3], &system_ticks)) {
391 LOG(WARNING) << "cannot parse first line: " << proc_stat_lines[0];
392 return TimeDelta(base::TimeDelta::FromSeconds(0));
393 }
394
Ben Chanf05ab402014-08-07 00:54:59 -0700395 uint64_t total_cpu_use_ticks = user_ticks + user_nice_ticks + system_ticks;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700396
397 // Sanity check.
398 if (total_cpu_use_ticks < latest_cpu_use_ticks_) {
399 LOG(WARNING) << "CPU time decreasing from " << latest_cpu_use_ticks_
400 << " to " << total_cpu_use_ticks;
401 return TimeDelta();
402 }
403
Ben Chanf05ab402014-08-07 00:54:59 -0700404 uint64_t diff = total_cpu_use_ticks - latest_cpu_use_ticks_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700405 latest_cpu_use_ticks_ = total_cpu_use_ticks;
406 // Use microseconds to avoid significant truncations.
407 return base::TimeDelta::FromMicroseconds(
408 diff * 1000 * 1000 / ticks_per_second_);
409}
410
Darin Petkov1bb904e2010-06-16 15:58:06 -0700411void MetricsDaemon::ProcessUserCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700412 // Counts the active time up to now.
413 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov1bb904e2010-06-16 15:58:06 -0700414
415 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800416 SendCrashIntervalSample(user_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700417
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800418 any_crashes_daily_count_->Add(1);
419 any_crashes_weekly_count_->Add(1);
420 user_crashes_daily_count_->Add(1);
421 user_crashes_weekly_count_->Add(1);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700422}
423
Darin Petkov38d5cb02010-06-24 12:10:26 -0700424void MetricsDaemon::ProcessKernelCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700425 // Counts the active time up to now.
426 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov38d5cb02010-06-24 12:10:26 -0700427
428 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800429 SendCrashIntervalSample(kernel_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700430
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800431 any_crashes_daily_count_->Add(1);
432 any_crashes_weekly_count_->Add(1);
433 kernel_crashes_daily_count_->Add(1);
434 kernel_crashes_weekly_count_->Add(1);
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800435
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800436 kernel_crashes_version_count_->Add(1);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700437}
438
Ken Mixterccd84c02010-08-16 19:57:13 -0700439void MetricsDaemon::ProcessUncleanShutdown() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700440 // Counts the active time up to now.
441 UpdateStats(TimeTicks::Now(), Time::Now());
Ken Mixterccd84c02010-08-16 19:57:13 -0700442
443 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800444 SendCrashIntervalSample(unclean_shutdown_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700445
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800446 unclean_shutdowns_daily_count_->Add(1);
447 unclean_shutdowns_weekly_count_->Add(1);
448 any_crashes_daily_count_->Add(1);
449 any_crashes_weekly_count_->Add(1);
Ken Mixterccd84c02010-08-16 19:57:13 -0700450}
451
Luigi Semenzato8accd332011-05-17 16:37:18 -0700452bool MetricsDaemon::CheckSystemCrash(const string& crash_file) {
Darin Petkov38d5cb02010-06-24 12:10:26 -0700453 FilePath crash_detected(crash_file);
Ben Chan2e6543d2014-02-05 23:26:25 -0800454 if (!base::PathExists(crash_detected))
Ken Mixterccd84c02010-08-16 19:57:13 -0700455 return false;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700456
457 // Deletes the crash-detected file so that the daemon doesn't report
458 // another kernel crash in case it's restarted.
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800459 base::DeleteFile(crash_detected, false); // not recursive
Ken Mixterccd84c02010-08-16 19:57:13 -0700460 return true;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700461}
462
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700463void MetricsDaemon::StatsReporterInit() {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800464 DiskStatsReadStats(&read_sectors_, &write_sectors_);
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700465 VmStatsReadStats(&vmstats_);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800466 // The first time around just run the long stat, so we don't delay boot.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700467 stats_state_ = kStatsLong;
468 stats_initial_time_ = GetActiveTime();
469 if (stats_initial_time_ < 0) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700470 LOG(WARNING) << "not collecting disk stats";
471 } else {
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700472 ScheduleStatsCallback(kMetricStatsLongInterval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700473 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800474}
475
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700476void MetricsDaemon::ScheduleStatsCallback(int wait) {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800477 if (testing_) {
478 return;
479 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700480 g_timeout_add_seconds(wait, StatsCallbackStatic, this);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800481}
482
Ben Chanf05ab402014-08-07 00:54:59 -0700483bool MetricsDaemon::DiskStatsReadStats(uint64_t* read_sectors,
484 uint64_t* write_sectors) {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800485 int nchars;
486 int nitems;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700487 bool success = false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800488 char line[200];
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700489 if (diskstats_path_.empty()) {
490 return false;
491 }
Luigi Semenzato0f132bb2011-02-28 11:17:43 -0800492 int file = HANDLE_EINTR(open(diskstats_path_.c_str(), O_RDONLY));
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800493 if (file < 0) {
494 PLOG(WARNING) << "cannot open " << diskstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700495 return false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800496 }
497 nchars = HANDLE_EINTR(read(file, line, sizeof(line)));
498 if (nchars < 0) {
499 PLOG(WARNING) << "cannot read from " << diskstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700500 return false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800501 } else {
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700502 LOG_IF(WARNING, nchars == sizeof(line))
503 << "line too long in " << diskstats_path_;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800504 line[nchars] = '\0';
Nathan Bullockdc1ef3c2014-09-17 09:24:10 -0400505 nitems = sscanf(line, "%*d %*d %" PRIu64 " %*d %*d %*d %" PRIu64,
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800506 read_sectors, write_sectors);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700507 if (nitems == 2) {
508 success = true;
509 } else {
510 LOG(WARNING) << "found " << nitems << " items in "
511 << diskstats_path_ << ", expected 2";
512 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800513 }
Mike Frysinger3e8a8512014-05-14 16:14:37 -0400514 IGNORE_EINTR(close(file));
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700515 return success;
516}
517
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700518bool MetricsDaemon::VmStatsParseStats(const char* stats,
519 struct VmstatRecord* record) {
520 // a mapping of string name to field in VmstatRecord and whether we found it
521 struct mapping {
522 const string name;
523 uint64_t* value_p;
524 bool found;
525 } map[] =
526 { { .name = "pgmajfault",
527 .value_p = &record->page_faults_,
528 .found = false },
529 { .name = "pswpin",
530 .value_p = &record->swap_in_,
531 .found = false },
532 { .name = "pswpout",
533 .value_p = &record->swap_out_,
534 .found = false }, };
535
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700536 // Each line in the file has the form
537 // <ID> <VALUE>
538 // for instance:
539 // nr_free_pages 213427
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700540 vector<string> lines;
541 Tokenize(stats, "\n", &lines);
542 for (vector<string>::iterator it = lines.begin();
543 it != lines.end(); ++it) {
544 vector<string> tokens;
545 base::SplitString(*it, ' ', &tokens);
546 if (tokens.size() == 2) {
547 for (unsigned int i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
548 if (!tokens[0].compare(map[i].name)) {
549 if (!base::StringToUint64(tokens[1], map[i].value_p))
550 return false;
551 map[i].found = true;
552 }
553 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700554 } else {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700555 LOG(WARNING) << "unexpected vmstat format";
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700556 }
557 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700558 // make sure we got all the stats
559 for (unsigned i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
560 if (map[i].found == false) {
561 LOG(WARNING) << "vmstat missing " << map[i].name;
562 return false;
563 }
564 }
565 return true;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700566}
567
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700568bool MetricsDaemon::VmStatsReadStats(struct VmstatRecord* stats) {
569 string value_string;
570 FilePath* path = new FilePath(vmstats_path_);
Ben Chan2e6543d2014-02-05 23:26:25 -0800571 if (!base::ReadFileToString(*path, &value_string)) {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700572 delete path;
573 LOG(WARNING) << "cannot read " << vmstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700574 return false;
575 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700576 delete path;
577 return VmStatsParseStats(value_string.c_str(), stats);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800578}
579
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700580bool MetricsDaemon::ReadFreqToInt(const string& sysfs_file_name, int* value) {
Luigi Semenzatod92d18c2013-06-04 13:24:21 -0700581 const FilePath sysfs_path(sysfs_file_name);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700582 string value_string;
Ben Chan2e6543d2014-02-05 23:26:25 -0800583 if (!base::ReadFileToString(sysfs_path, &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700584 LOG(WARNING) << "cannot read " << sysfs_path.value().c_str();
585 return false;
586 }
Ben Chan2e6543d2014-02-05 23:26:25 -0800587 if (!base::RemoveChars(value_string, "\n", &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700588 LOG(WARNING) << "no newline in " << value_string;
589 // Continue even though the lack of newline is suspicious.
590 }
591 if (!base::StringToInt(value_string, value)) {
592 LOG(WARNING) << "cannot convert " << value_string << " to int";
593 return false;
594 }
595 return true;
596}
597
598void MetricsDaemon::SendCpuThrottleMetrics() {
599 // |max_freq| is 0 only the first time through.
600 static int max_freq = 0;
601 if (max_freq == -1)
602 // Give up, as sysfs did not report max_freq correctly.
603 return;
604 if (max_freq == 0 || testing_) {
605 // One-time initialization of max_freq. (Every time when testing.)
606 if (!ReadFreqToInt(cpuinfo_max_freq_path_, &max_freq)) {
607 max_freq = -1;
608 return;
609 }
610 if (max_freq == 0) {
611 LOG(WARNING) << "sysfs reports 0 max CPU frequency\n";
612 max_freq = -1;
613 return;
614 }
615 if (max_freq % 10000 == 1000) {
616 // Special case: system has turbo mode, and max non-turbo frequency is
617 // max_freq - 1000. This relies on "normal" (non-turbo) frequencies
618 // being multiples of (at least) 10 MHz. Although there is no guarantee
619 // of this, it seems a fairly reasonable assumption. Otherwise we should
620 // read scaling_available_frequencies, sort the frequencies, compare the
621 // two highest ones, and check if they differ by 1000 (kHz) (and that's a
622 // hack too, no telling when it will change).
623 max_freq -= 1000;
624 }
625 }
626 int scaled_freq = 0;
627 if (!ReadFreqToInt(scaling_max_freq_path_, &scaled_freq))
628 return;
629 // Frequencies are in kHz. If scaled_freq > max_freq, turbo is on, but
630 // scaled_freq is not the actual turbo frequency. We indicate this situation
631 // with a 101% value.
632 int percent = scaled_freq > max_freq ? 101 : scaled_freq / (max_freq / 100);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800633 SendLinearSample(kMetricScaledCpuFrequencyName, percent, 101, 102);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700634}
635
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800636// static
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700637gboolean MetricsDaemon::StatsCallbackStatic(void* handle) {
638 (static_cast<MetricsDaemon*>(handle))->StatsCallback();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800639 return false; // one-time callback
640}
641
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700642// Collects disk and vm stats alternating over a short and a long interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700643
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700644void MetricsDaemon::StatsCallback() {
Ben Chanf05ab402014-08-07 00:54:59 -0700645 uint64_t read_sectors_now, write_sectors_now;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700646 struct VmstatRecord vmstats_now;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700647 double time_now = GetActiveTime();
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700648 double delta_time = time_now - stats_initial_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700649 if (testing_) {
650 // Fake the time when testing.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700651 delta_time = stats_state_ == kStatsShort ?
652 kMetricStatsShortInterval : kMetricStatsLongInterval;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700653 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700654 bool diskstats_success = DiskStatsReadStats(&read_sectors_now,
655 &write_sectors_now);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700656 int delta_read = read_sectors_now - read_sectors_;
657 int delta_write = write_sectors_now - write_sectors_;
658 int read_sectors_per_second = delta_read / delta_time;
659 int write_sectors_per_second = delta_write / delta_time;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700660 bool vmstats_success = VmStatsReadStats(&vmstats_now);
661 uint64_t delta_faults = vmstats_now.page_faults_ - vmstats_.page_faults_;
662 uint64_t delta_swap_in = vmstats_now.swap_in_ - vmstats_.swap_in_;
663 uint64_t delta_swap_out = vmstats_now.swap_out_ - vmstats_.swap_out_;
664 uint64_t page_faults_per_second = delta_faults / delta_time;
665 uint64_t swap_in_per_second = delta_swap_in / delta_time;
666 uint64_t swap_out_per_second = delta_swap_out / delta_time;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800667
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700668 switch (stats_state_) {
669 case kStatsShort:
670 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800671 SendSample(kMetricReadSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700672 read_sectors_per_second,
673 1,
674 kMetricSectorsIOMax,
675 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800676 SendSample(kMetricWriteSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700677 write_sectors_per_second,
678 1,
679 kMetricSectorsIOMax,
680 kMetricSectorsBuckets);
681 }
682 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800683 SendSample(kMetricPageFaultsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700684 page_faults_per_second,
685 1,
686 kMetricPageFaultsMax,
687 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800688 SendSample(kMetricSwapInShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700689 swap_in_per_second,
690 1,
691 kMetricPageFaultsMax,
692 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800693 SendSample(kMetricSwapOutShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700694 swap_out_per_second,
695 1,
696 kMetricPageFaultsMax,
697 kMetricPageFaultsBuckets);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700698 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800699 // Schedule long callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700700 stats_state_ = kStatsLong;
701 ScheduleStatsCallback(kMetricStatsLongInterval -
702 kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800703 break;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700704 case kStatsLong:
705 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800706 SendSample(kMetricReadSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700707 read_sectors_per_second,
708 1,
709 kMetricSectorsIOMax,
710 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800711 SendSample(kMetricWriteSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700712 write_sectors_per_second,
713 1,
714 kMetricSectorsIOMax,
715 kMetricSectorsBuckets);
716 // Reset sector counters.
717 read_sectors_ = read_sectors_now;
718 write_sectors_ = write_sectors_now;
719 }
720 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800721 SendSample(kMetricPageFaultsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700722 page_faults_per_second,
723 1,
724 kMetricPageFaultsMax,
725 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800726 SendSample(kMetricSwapInLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700727 swap_in_per_second,
728 1,
729 kMetricPageFaultsMax,
730 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800731 SendSample(kMetricSwapOutLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700732 swap_out_per_second,
733 1,
734 kMetricPageFaultsMax,
735 kMetricPageFaultsBuckets);
736
737 vmstats_ = vmstats_now;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700738 }
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700739 SendCpuThrottleMetrics();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700740 // Set start time for new cycle.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700741 stats_initial_time_ = time_now;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800742 // Schedule short callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700743 stats_state_ = kStatsShort;
744 ScheduleStatsCallback(kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800745 break;
746 default:
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700747 LOG(FATAL) << "Invalid stats state";
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800748 }
749}
750
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700751void MetricsDaemon::ScheduleMeminfoCallback(int wait) {
752 if (testing_) {
753 return;
754 }
755 g_timeout_add_seconds(wait, MeminfoCallbackStatic, this);
756}
757
758// static
759gboolean MetricsDaemon::MeminfoCallbackStatic(void* handle) {
760 return (static_cast<MetricsDaemon*>(handle))->MeminfoCallback();
761}
762
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700763bool MetricsDaemon::MeminfoCallback() {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700764 string meminfo_raw;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700765 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800766 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700767 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
768 return false;
769 }
Luigi Semenzato96360192014-06-04 10:53:35 -0700770 // Make both calls even if the first one fails.
771 bool success = ProcessMeminfo(meminfo_raw);
772 return ReportZram(base::FilePath(FILE_PATH_LITERAL("/sys/block/zram0"))) &&
773 success;
774}
775
776// static
777bool MetricsDaemon::ReadFileToUint64(const base::FilePath& path,
Ben Chanf05ab402014-08-07 00:54:59 -0700778 uint64_t* value) {
Luigi Semenzato96360192014-06-04 10:53:35 -0700779 std::string content;
780 if (!base::ReadFileToString(path, &content)) {
781 PLOG(WARNING) << "cannot read " << path.MaybeAsASCII();
782 return false;
783 }
Luigi Semenzato4a6c9422014-06-30 18:12:28 -0700784 // Remove final newline.
785 base::TrimWhitespaceASCII(content, base::TRIM_TRAILING, &content);
Luigi Semenzato96360192014-06-04 10:53:35 -0700786 if (!base::StringToUint64(content, value)) {
787 LOG(WARNING) << "invalid integer: " << content;
788 return false;
789 }
790 return true;
791}
792
793bool MetricsDaemon::ReportZram(const base::FilePath& zram_dir) {
794 // Data sizes are in bytes. |zero_pages| is in number of pages.
Ben Chanf05ab402014-08-07 00:54:59 -0700795 uint64_t compr_data_size, orig_data_size, zero_pages;
Luigi Semenzato96360192014-06-04 10:53:35 -0700796 const size_t page_size = 4096;
797
798 if (!ReadFileToUint64(zram_dir.Append(kComprDataSizeName),
799 &compr_data_size) ||
800 !ReadFileToUint64(zram_dir.Append(kOrigDataSizeName), &orig_data_size) ||
801 !ReadFileToUint64(zram_dir.Append(kZeroPagesName), &zero_pages)) {
802 return false;
803 }
804
805 // |orig_data_size| does not include zero-filled pages.
806 orig_data_size += zero_pages * page_size;
807
808 const int compr_data_size_mb = compr_data_size >> 20;
809 const int savings_mb = (orig_data_size - compr_data_size) >> 20;
810 const int zero_ratio_percent = zero_pages * page_size * 100 / orig_data_size;
811
812 // Report compressed size in megabytes. 100 MB or less has little impact.
813 SendSample("Platform.ZramCompressedSize", compr_data_size_mb, 100, 4000, 50);
814 SendSample("Platform.ZramSavings", savings_mb, 100, 4000, 50);
815 // The compression ratio is multiplied by 100 for better resolution. The
816 // ratios of interest are between 1 and 6 (100% and 600% as reported). We
817 // don't want samples when very little memory is being compressed.
818 if (compr_data_size_mb >= 1) {
819 SendSample("Platform.ZramCompressionRatioPercent",
820 orig_data_size * 100 / compr_data_size, 100, 600, 50);
821 }
822 // The values of interest for zero_pages are between 1MB and 1GB. The units
823 // are number of pages.
824 SendSample("Platform.ZramZeroPages", zero_pages, 256, 256 * 1024, 50);
825 SendSample("Platform.ZramZeroRatioPercent", zero_ratio_percent, 1, 50, 50);
826
827 return true;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700828}
829
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700830bool MetricsDaemon::ProcessMeminfo(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700831 static const MeminfoRecord fields_array[] = {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700832 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
833 { "MemFree", "MemFree" },
834 { "Buffers", "Buffers" },
835 { "Cached", "Cached" },
836 // { "SwapCached", "SwapCached" },
837 { "Active", "Active" },
838 { "Inactive", "Inactive" },
839 { "ActiveAnon", "Active(anon)" },
840 { "InactiveAnon", "Inactive(anon)" },
841 { "ActiveFile" , "Active(file)" },
842 { "InactiveFile", "Inactive(file)" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800843 { "Unevictable", "Unevictable", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700844 // { "Mlocked", "Mlocked" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800845 { "SwapTotal", "SwapTotal", kMeminfoOp_SwapTotal },
846 { "SwapFree", "SwapFree", kMeminfoOp_SwapFree },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700847 // { "Dirty", "Dirty" },
848 // { "Writeback", "Writeback" },
849 { "AnonPages", "AnonPages" },
850 { "Mapped", "Mapped" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800851 { "Shmem", "Shmem", kMeminfoOp_HistLog },
852 { "Slab", "Slab", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700853 // { "SReclaimable", "SReclaimable" },
854 // { "SUnreclaim", "SUnreclaim" },
855 };
Luigi Semenzato8accd332011-05-17 16:37:18 -0700856 vector<MeminfoRecord> fields(fields_array,
857 fields_array + arraysize(fields_array));
858 if (!FillMeminfo(meminfo_raw, &fields)) {
859 return false;
860 }
861 int total_memory = fields[0].value;
862 if (total_memory == 0) {
863 // this "cannot happen"
864 LOG(WARNING) << "borked meminfo parser";
865 return false;
866 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800867 int swap_total = 0;
868 int swap_free = 0;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700869 // Send all fields retrieved, except total memory.
870 for (unsigned int i = 1; i < fields.size(); i++) {
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800871 string metrics_name = base::StringPrintf("Platform.Meminfo%s",
872 fields[i].name);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800873 int percent;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800874 switch (fields[i].op) {
875 case kMeminfoOp_HistPercent:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800876 // report value as percent of total memory
877 percent = fields[i].value * 100 / total_memory;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800878 SendLinearSample(metrics_name, percent, 100, 101);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800879 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800880 case kMeminfoOp_HistLog:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800881 // report value in kbytes, log scale, 4Gb max
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800882 SendSample(metrics_name, fields[i].value, 1, 4 * 1000 * 1000, 100);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800883 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800884 case kMeminfoOp_SwapTotal:
885 swap_total = fields[i].value;
886 case kMeminfoOp_SwapFree:
887 swap_free = fields[i].value;
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800888 break;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700889 }
890 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800891 if (swap_total > 0) {
892 int swap_used = swap_total - swap_free;
893 int swap_used_percent = swap_used * 100 / swap_total;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800894 SendSample("Platform.MeminfoSwapUsed", swap_used, 1, 8 * 1000 * 1000, 100);
895 SendLinearSample("Platform.MeminfoSwapUsedPercent", swap_used_percent,
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800896 100, 101);
897 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700898 return true;
899}
900
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700901bool MetricsDaemon::FillMeminfo(const string& meminfo_raw,
902 vector<MeminfoRecord>* fields) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700903 vector<string> lines;
904 unsigned int nlines = Tokenize(meminfo_raw, "\n", &lines);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700905
906 // Scan meminfo output and collect field values. Each field name has to
907 // match a meminfo entry (case insensitive) after removing non-alpha
908 // characters from the entry.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700909 unsigned int ifield = 0;
910 for (unsigned int iline = 0;
911 iline < nlines && ifield < fields->size();
912 iline++) {
913 vector<string> tokens;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700914 Tokenize(lines[iline], ": ", &tokens);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700915 if (strcmp((*fields)[ifield].match, tokens[0].c_str()) == 0) {
916 // Name matches. Parse value and save.
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700917 char* rest;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700918 (*fields)[ifield].value =
919 static_cast<int>(strtol(tokens[1].c_str(), &rest, 10));
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700920 if (*rest != '\0') {
921 LOG(WARNING) << "missing meminfo value";
922 return false;
923 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700924 ifield++;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700925 }
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700926 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700927 if (ifield < fields->size()) {
928 // End of input reached while scanning.
929 LOG(WARNING) << "cannot find field " << (*fields)[ifield].match
930 << " and following";
931 return false;
932 }
933 return true;
934}
935
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800936void MetricsDaemon::ScheduleMemuseCallback(double interval) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700937 if (testing_) {
938 return;
939 }
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800940 g_timeout_add_seconds(interval, MemuseCallbackStatic, this);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700941}
942
943// static
944gboolean MetricsDaemon::MemuseCallbackStatic(void* handle) {
945 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle);
946 daemon->MemuseCallback();
947 return false;
948}
949
950void MetricsDaemon::MemuseCallback() {
951 // Since we only care about active time (i.e. uptime minus sleep time) but
952 // the callbacks are driven by real time (uptime), we check if we should
953 // reschedule this callback due to intervening sleep periods.
954 double now = GetActiveTime();
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800955 // Avoid intervals of less than one second.
956 double remaining_time = ceil(memuse_final_time_ - now);
957 if (remaining_time > 0) {
958 ScheduleMemuseCallback(remaining_time);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700959 } else {
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800960 // Report stats and advance the measurement interval unless there are
961 // errors or we've completed the last interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700962 if (MemuseCallbackWork() &&
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800963 memuse_interval_index_ < arraysize(kMemuseIntervals)) {
964 double interval = kMemuseIntervals[memuse_interval_index_++];
965 memuse_final_time_ = now + interval;
966 ScheduleMemuseCallback(interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700967 }
968 }
969}
970
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700971bool MetricsDaemon::MemuseCallbackWork() {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700972 string meminfo_raw;
973 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800974 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700975 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
976 return false;
977 }
978 return ProcessMemuse(meminfo_raw);
979}
980
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700981bool MetricsDaemon::ProcessMemuse(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700982 static const MeminfoRecord fields_array[] = {
983 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
984 { "ActiveAnon", "Active(anon)" },
985 { "InactiveAnon", "Inactive(anon)" },
986 };
987 vector<MeminfoRecord> fields(fields_array,
988 fields_array + arraysize(fields_array));
989 if (!FillMeminfo(meminfo_raw, &fields)) {
990 return false;
991 }
992 int total = fields[0].value;
993 int active_anon = fields[1].value;
994 int inactive_anon = fields[2].value;
995 if (total == 0) {
996 // this "cannot happen"
997 LOG(WARNING) << "borked meminfo parser";
998 return false;
999 }
Luigi Semenzato859b3f02014-02-05 15:33:19 -08001000 string metrics_name = base::StringPrintf("Platform.MemuseAnon%d",
1001 memuse_interval_index_);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001002 SendLinearSample(metrics_name, (active_anon + inactive_anon) * 100 / total,
Luigi Semenzato8accd332011-05-17 16:37:18 -07001003 100, 101);
1004 return true;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001005}
1006
Luigi Semenzato5ef2e392014-04-15 15:15:02 -07001007void MetricsDaemon::ReportDailyUse(int use_seconds) {
1008 if (use_seconds <= 0)
Darin Petkov1bb904e2010-06-16 15:58:06 -07001009 return;
1010
Luigi Semenzato5ef2e392014-04-15 15:15:02 -07001011 int minutes = (use_seconds + kSecondsPerMinute / 2) / kSecondsPerMinute;
1012 SendSample("Logging.DailyUseTime",
1013 minutes,
1014 1,
1015 kMinutesPerDay * 30 * 2, // cumulative---two months worth
1016 50);
Darin Petkovf1e85e42010-06-10 15:59:53 -07001017}
1018
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001019void MetricsDaemon::SendSample(const string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -07001020 int min, int max, int nbuckets) {
Darin Petkovfc91b422010-05-12 13:05:45 -07001021 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -07001022}
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001023
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001024void MetricsDaemon::SendKernelCrashesCumulativeCountStats() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001025 // Report the number of crashes for this OS version, but don't clear the
1026 // counter. It is cleared elsewhere on version change.
Ben Chanf05ab402014-08-07 00:54:59 -07001027 int64_t crashes_count = kernel_crashes_version_count_->Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001028 SendSample(kernel_crashes_version_count_->Name(),
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001029 crashes_count,
1030 1, // value of first bucket
1031 500, // value of last bucket
1032 100); // number of buckets
1033
1034
Ben Chanf05ab402014-08-07 00:54:59 -07001035 int64_t cpu_use_ms = version_cumulative_cpu_use_->Get();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001036 SendSample(version_cumulative_cpu_use_->Name(),
1037 cpu_use_ms / 1000, // stat is in seconds
1038 1, // device may be used very little...
1039 8 * 1000 * 1000, // ... or a lot (a little over 90 days)
1040 100);
1041
1042 // On the first run after an autoupdate, cpu_use_ms and active_use_seconds
1043 // can be zero. Avoid division by zero.
1044 if (cpu_use_ms > 0) {
1045 // Send the crash frequency since update in number of crashes per CPU year.
1046 SendSample("Logging.KernelCrashesPerCpuYear",
1047 crashes_count * kSecondsPerDay * 365 * 1000 / cpu_use_ms,
1048 1,
1049 1000 * 1000, // about one crash every 30s of CPU time
1050 100);
1051 }
1052
Ben Chanf05ab402014-08-07 00:54:59 -07001053 int64_t active_use_seconds = version_cumulative_active_use_->Get();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001054 if (active_use_seconds > 0) {
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001055 SendSample(version_cumulative_active_use_->Name(),
1056 active_use_seconds / 1000, // stat is in seconds
1057 1, // device may be used very little...
1058 8 * 1000 * 1000, // ... or a lot (about 90 days)
1059 100);
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001060 // Same as above, but per year of active time.
1061 SendSample("Logging.KernelCrashesPerActiveYear",
1062 crashes_count * kSecondsPerDay * 365 / active_use_seconds,
1063 1,
1064 1000 * 1000, // about one crash every 30s of active time
1065 100);
1066 }
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001067}
1068
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001069void MetricsDaemon::SendDailyUseSample(
1070 const scoped_ptr<PersistentInteger>& use) {
1071 SendSample(use->Name(),
1072 use->GetAndClear(),
1073 1, // value of first bucket
1074 kSecondsPerDay, // value of last bucket
1075 50); // number of buckets
1076}
1077
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001078void MetricsDaemon::SendCrashIntervalSample(
1079 const scoped_ptr<PersistentInteger>& interval) {
1080 SendSample(interval->Name(),
1081 interval->GetAndClear(),
1082 1, // value of first bucket
1083 4 * kSecondsPerWeek, // value of last bucket
1084 50); // number of buckets
1085}
1086
1087void MetricsDaemon::SendCrashFrequencySample(
1088 const scoped_ptr<PersistentInteger>& frequency) {
1089 SendSample(frequency->Name(),
1090 frequency->GetAndClear(),
1091 1, // value of first bucket
1092 100, // value of last bucket
1093 50); // number of buckets
1094}
1095
1096void MetricsDaemon::SendLinearSample(const string& name, int sample,
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001097 int max, int nbuckets) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001098 // TODO(semenzato): add a proper linear histogram to the Chrome external
1099 // metrics API.
1100 LOG_IF(FATAL, nbuckets != max + 1) << "unsupported histogram scale";
1101 metrics_lib_->SendEnumToUMA(name, sample, max);
1102}
Daniel Eratc83975a2014-04-04 08:53:44 -07001103
1104void MetricsDaemon::UpdateStats(TimeTicks now_ticks,
1105 Time now_wall_time) {
1106 const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds();
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001107 daily_active_use_->Add(elapsed_seconds);
1108 version_cumulative_active_use_->Add(elapsed_seconds);
Daniel Eratc83975a2014-04-04 08:53:44 -07001109 user_crash_interval_->Add(elapsed_seconds);
1110 kernel_crash_interval_->Add(elapsed_seconds);
1111 version_cumulative_cpu_use_->Add(GetIncrementalCpuUse().InMilliseconds());
1112 last_update_stats_time_ = now_ticks;
1113
1114 const TimeDelta since_epoch = now_wall_time - Time::UnixEpoch();
1115 const int day = since_epoch.InDays();
1116 const int week = day / 7;
1117
1118 if (daily_cycle_->Get() != day) {
1119 daily_cycle_->Set(day);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001120 SendDailyUseSample(daily_active_use_);
1121 SendDailyUseSample(version_cumulative_active_use_);
Daniel Eratc83975a2014-04-04 08:53:44 -07001122 SendCrashFrequencySample(any_crashes_daily_count_);
1123 SendCrashFrequencySample(user_crashes_daily_count_);
1124 SendCrashFrequencySample(kernel_crashes_daily_count_);
1125 SendCrashFrequencySample(unclean_shutdowns_daily_count_);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001126 SendKernelCrashesCumulativeCountStats();
Daniel Eratc83975a2014-04-04 08:53:44 -07001127 }
1128
1129 if (weekly_cycle_->Get() != week) {
1130 weekly_cycle_->Set(week);
1131 SendCrashFrequencySample(any_crashes_weekly_count_);
1132 SendCrashFrequencySample(user_crashes_weekly_count_);
1133 SendCrashFrequencySample(kernel_crashes_weekly_count_);
1134 SendCrashFrequencySample(unclean_shutdowns_weekly_count_);
1135 }
1136}
1137
1138// static
1139gboolean MetricsDaemon::HandleUpdateStatsTimeout(gpointer data) {
1140 static_cast<MetricsDaemon*>(data)->UpdateStats(TimeTicks::Now(), Time::Now());
1141 return TRUE;
1142}