blob: 7226594cb0f01dfa47051fa9447404f21b943570 [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() {
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700190 upload_service_.reset(new UploadService(new SystemProfileCache(true,
191 config_root_),
192 server_));
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700193 upload_service_->Init(upload_interval_, metrics_file_);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700194 upload_service_->UploadEvent();
195}
196
Ben Chanf05ab402014-08-07 00:54:59 -0700197uint32_t MetricsDaemon::GetOsVersionHash() {
198 static uint32_t cached_version_hash = 0;
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800199 static bool version_hash_is_cached = false;
200 if (version_hash_is_cached)
201 return cached_version_hash;
202 version_hash_is_cached = true;
203 std::string version;
204 if (base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_VERSION", &version)) {
205 cached_version_hash = base::Hash(version);
206 } else if (testing_) {
207 cached_version_hash = 42; // return any plausible value for the hash
208 } else {
209 LOG(FATAL) << "could not find CHROMEOS_RELEASE_VERSION";
210 }
211 return cached_version_hash;
212}
213
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700214void MetricsDaemon::Init(bool testing,
215 bool uploader_active,
216 MetricsLibraryInterface* metrics_lib,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700217 const string& diskstats_path,
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700218 const string& vmstats_path,
219 const string& scaling_max_freq_path,
Steve Fung67906c62014-10-06 15:15:30 -0700220 const string& cpuinfo_max_freq_path,
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700221 const base::TimeDelta& upload_interval,
Steve Fung67906c62014-10-06 15:15:30 -0700222 const string& server,
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700223 const string& metrics_file,
224 const string& config_root) {
Darin Petkov65b01462010-04-14 13:32:20 -0700225 testing_ = testing;
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700226 config_root_ = config_root;
Alex Vakulenko14595032014-08-28 14:59:56 -0700227 DCHECK(metrics_lib != nullptr);
Darin Petkovfc91b422010-05-12 13:05:45 -0700228 metrics_lib_ = metrics_lib;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700229
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700230 upload_interval_ = upload_interval;
Steve Fung67906c62014-10-06 15:15:30 -0700231 server_ = server;
232 metrics_file_ = metrics_file;
233
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700234 // Get ticks per second (HZ) on this system.
235 // Sysconf cannot fail, so no sanity checks are needed.
236 ticks_per_second_ = sysconf(_SC_CLK_TCK);
237
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700238 daily_active_use_.reset(
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800239 new PersistentInteger("Logging.DailyUseTime"));
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700240 version_cumulative_active_use_.reset(
241 new PersistentInteger("Logging.CumulativeDailyUseTime"));
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700242 version_cumulative_cpu_use_.reset(
243 new PersistentInteger("Logging.CumulativeCpuTime"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700244
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800245 kernel_crash_interval_.reset(
246 new PersistentInteger("Logging.KernelCrashInterval"));
247 unclean_shutdown_interval_.reset(
248 new PersistentInteger("Logging.UncleanShutdownInterval"));
249 user_crash_interval_.reset(
250 new PersistentInteger("Logging.UserCrashInterval"));
Darin Petkov2ccef012010-05-05 16:06:37 -0700251
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800252 any_crashes_daily_count_.reset(
253 new PersistentInteger("Logging.AnyCrashesDaily"));
254 any_crashes_weekly_count_.reset(
255 new PersistentInteger("Logging.AnyCrashesWeekly"));
256 user_crashes_daily_count_.reset(
257 new PersistentInteger("Logging.UserCrashesDaily"));
258 user_crashes_weekly_count_.reset(
259 new PersistentInteger("Logging.UserCrashesWeekly"));
260 kernel_crashes_daily_count_.reset(
261 new PersistentInteger("Logging.KernelCrashesDaily"));
262 kernel_crashes_weekly_count_.reset(
263 new PersistentInteger("Logging.KernelCrashesWeekly"));
264 kernel_crashes_version_count_.reset(
265 new PersistentInteger("Logging.KernelCrashesSinceUpdate"));
266 unclean_shutdowns_daily_count_.reset(
267 new PersistentInteger("Logging.UncleanShutdownsDaily"));
268 unclean_shutdowns_weekly_count_.reset(
269 new PersistentInteger("Logging.UncleanShutdownsWeekly"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700270
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800271 daily_cycle_.reset(new PersistentInteger("daily.cycle"));
272 weekly_cycle_.reset(new PersistentInteger("weekly.cycle"));
273 version_cycle_.reset(new PersistentInteger("version.cycle"));
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800274
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700275 diskstats_path_ = diskstats_path;
276 vmstats_path_ = vmstats_path;
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700277 scaling_max_freq_path_ = scaling_max_freq_path;
278 cpuinfo_max_freq_path_ = cpuinfo_max_freq_path;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700279 StatsReporterInit();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800280
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700281 // Start collecting meminfo stats.
282 ScheduleMeminfoCallback(kMetricMeminfoInterval);
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800283 memuse_final_time_ = GetActiveTime() + kMemuseIntervals[0];
284 ScheduleMemuseCallback(kMemuseIntervals[0]);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700285
Darin Petkov2ccef012010-05-05 16:06:37 -0700286 // Don't setup D-Bus and GLib in test mode.
287 if (testing)
288 return;
Darin Petkov65b01462010-04-14 13:32:20 -0700289
Darin Petkov703ec972010-04-27 11:02:18 -0700290 g_type_init();
Ben Chan6f598422013-06-22 06:29:36 -0700291 dbus_threads_init_default();
Darin Petkov65b01462010-04-14 13:32:20 -0700292
Darin Petkov703ec972010-04-27 11:02:18 -0700293 DBusError error;
294 dbus_error_init(&error);
Darin Petkov65b01462010-04-14 13:32:20 -0700295
David James3b3add52010-06-04 15:01:19 -0700296 DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkov703ec972010-04-27 11:02:18 -0700297 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
298 "No D-Bus connection: " << SAFE_MESSAGE(error);
Darin Petkov65b01462010-04-14 13:32:20 -0700299
Alex Vakulenko14595032014-08-28 14:59:56 -0700300 dbus_connection_setup_with_g_main(connection, nullptr);
Darin Petkov65b01462010-04-14 13:32:20 -0700301
Darin Petkov40f25732013-04-29 15:07:31 +0200302 vector<string> matches;
303 matches.push_back(
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800304 base::StringPrintf("type='signal',interface='%s',path='/',member='%s'",
305 kCrashReporterInterface,
306 kCrashReporterUserCrashSignal));
Darin Petkov40f25732013-04-29 15:07:31 +0200307
Darin Petkov703ec972010-04-27 11:02:18 -0700308 // Registers D-Bus matches for the signals we would like to catch.
Darin Petkov40f25732013-04-29 15:07:31 +0200309 for (vector<string>::const_iterator it = matches.begin();
310 it != matches.end(); ++it) {
311 const char* match = it->c_str();
Darin Petkov41e06232010-05-03 16:45:37 -0700312 DLOG(INFO) << "adding dbus match: " << match;
Darin Petkov703ec972010-04-27 11:02:18 -0700313 dbus_bus_add_match(connection, match, &error);
314 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
315 "unable to add a match: " << SAFE_MESSAGE(error);
316 }
317
318 // Adds the D-Bus filter routine to be called back whenever one of
319 // the registered D-Bus matches is successful. The daemon is not
320 // activated for D-Bus messages that don't match.
Alex Vakulenko14595032014-08-28 14:59:56 -0700321 CHECK(dbus_connection_add_filter(connection, MessageFilter, this, nullptr));
Daniel Eratc83975a2014-04-04 08:53:44 -0700322
323 update_stats_timeout_id_ =
324 g_timeout_add(kUpdateStatsIntervalMs, &HandleUpdateStatsTimeout, this);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700325
326 if (uploader_active) {
Nathan Bullock0be0f732014-09-11 10:29:17 -0400327 LOG(INFO) << "uploader enabled";
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700328 upload_service_.reset(new UploadService(new SystemProfileCache(), server_));
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700329 upload_service_->Init(upload_interval_, metrics_file_);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700330 }
Darin Petkov65b01462010-04-14 13:32:20 -0700331}
332
333void MetricsDaemon::Loop() {
Alex Vakulenko14595032014-08-28 14:59:56 -0700334 GMainLoop* loop = g_main_loop_new(nullptr, false);
Darin Petkov703ec972010-04-27 11:02:18 -0700335 g_main_loop_run(loop);
Darin Petkov65b01462010-04-14 13:32:20 -0700336}
337
Darin Petkov703ec972010-04-27 11:02:18 -0700338// static
339DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
340 DBusMessage* message,
341 void* user_data) {
Darin Petkov703ec972010-04-27 11:02:18 -0700342 int message_type = dbus_message_get_type(message);
343 if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) {
Darin Petkov41e06232010-05-03 16:45:37 -0700344 DLOG(WARNING) << "unexpected message type " << message_type;
Darin Petkov703ec972010-04-27 11:02:18 -0700345 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
346 }
347
348 // Signal messages always have interfaces.
Daniel Eratc83975a2014-04-04 08:53:44 -0700349 const std::string interface(dbus_message_get_interface(message));
350 const std::string member(dbus_message_get_member(message));
351 DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal";
Darin Petkov703ec972010-04-27 11:02:18 -0700352
353 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data);
354
355 DBusMessageIter iter;
356 dbus_message_iter_init(message, &iter);
Daniel Eratc83975a2014-04-04 08:53:44 -0700357 if (interface == kCrashReporterInterface) {
358 CHECK_EQ(member, kCrashReporterUserCrashSignal);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700359 daemon->ProcessUserCrash();
Darin Petkov703ec972010-04-27 11:02:18 -0700360 } else {
Daniel Eratc83975a2014-04-04 08:53:44 -0700361 // Ignore messages from the bus itself.
Darin Petkov703ec972010-04-27 11:02:18 -0700362 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
363 }
364
365 return DBUS_HANDLER_RESULT_HANDLED;
Darin Petkov65b01462010-04-14 13:32:20 -0700366}
367
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700368// One might argue that parts of this should go into
369// chromium/src/base/sys_info_chromeos.c instead, but put it here for now.
370
371TimeDelta MetricsDaemon::GetIncrementalCpuUse() {
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700372 FilePath proc_stat_path = FilePath(kMetricsProcStatFileName);
373 std::string proc_stat_string;
374 if (!base::ReadFileToString(proc_stat_path, &proc_stat_string)) {
375 LOG(WARNING) << "cannot open " << kMetricsProcStatFileName;
376 return TimeDelta();
377 }
378
379 std::vector<std::string> proc_stat_lines;
380 base::SplitString(proc_stat_string, '\n', &proc_stat_lines);
381 if (proc_stat_lines.empty()) {
382 LOG(WARNING) << "cannot parse " << kMetricsProcStatFileName
383 << ": " << proc_stat_string;
384 return TimeDelta();
385 }
386 std::vector<std::string> proc_stat_totals;
387 base::SplitStringAlongWhitespace(proc_stat_lines[0], &proc_stat_totals);
388
Ben Chanf05ab402014-08-07 00:54:59 -0700389 uint64_t user_ticks, user_nice_ticks, system_ticks;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700390 if (proc_stat_totals.size() != kMetricsProcStatFirstLineItemsCount ||
391 proc_stat_totals[0] != "cpu" ||
392 !base::StringToUint64(proc_stat_totals[1], &user_ticks) ||
393 !base::StringToUint64(proc_stat_totals[2], &user_nice_ticks) ||
394 !base::StringToUint64(proc_stat_totals[3], &system_ticks)) {
395 LOG(WARNING) << "cannot parse first line: " << proc_stat_lines[0];
396 return TimeDelta(base::TimeDelta::FromSeconds(0));
397 }
398
Ben Chanf05ab402014-08-07 00:54:59 -0700399 uint64_t total_cpu_use_ticks = user_ticks + user_nice_ticks + system_ticks;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700400
401 // Sanity check.
402 if (total_cpu_use_ticks < latest_cpu_use_ticks_) {
403 LOG(WARNING) << "CPU time decreasing from " << latest_cpu_use_ticks_
404 << " to " << total_cpu_use_ticks;
405 return TimeDelta();
406 }
407
Ben Chanf05ab402014-08-07 00:54:59 -0700408 uint64_t diff = total_cpu_use_ticks - latest_cpu_use_ticks_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700409 latest_cpu_use_ticks_ = total_cpu_use_ticks;
410 // Use microseconds to avoid significant truncations.
411 return base::TimeDelta::FromMicroseconds(
412 diff * 1000 * 1000 / ticks_per_second_);
413}
414
Darin Petkov1bb904e2010-06-16 15:58:06 -0700415void MetricsDaemon::ProcessUserCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700416 // Counts the active time up to now.
417 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov1bb904e2010-06-16 15:58:06 -0700418
419 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800420 SendCrashIntervalSample(user_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700421
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800422 any_crashes_daily_count_->Add(1);
423 any_crashes_weekly_count_->Add(1);
424 user_crashes_daily_count_->Add(1);
425 user_crashes_weekly_count_->Add(1);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700426}
427
Darin Petkov38d5cb02010-06-24 12:10:26 -0700428void MetricsDaemon::ProcessKernelCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700429 // Counts the active time up to now.
430 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov38d5cb02010-06-24 12:10:26 -0700431
432 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800433 SendCrashIntervalSample(kernel_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700434
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800435 any_crashes_daily_count_->Add(1);
436 any_crashes_weekly_count_->Add(1);
437 kernel_crashes_daily_count_->Add(1);
438 kernel_crashes_weekly_count_->Add(1);
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800439
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800440 kernel_crashes_version_count_->Add(1);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700441}
442
Ken Mixterccd84c02010-08-16 19:57:13 -0700443void MetricsDaemon::ProcessUncleanShutdown() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700444 // Counts the active time up to now.
445 UpdateStats(TimeTicks::Now(), Time::Now());
Ken Mixterccd84c02010-08-16 19:57:13 -0700446
447 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800448 SendCrashIntervalSample(unclean_shutdown_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700449
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800450 unclean_shutdowns_daily_count_->Add(1);
451 unclean_shutdowns_weekly_count_->Add(1);
452 any_crashes_daily_count_->Add(1);
453 any_crashes_weekly_count_->Add(1);
Ken Mixterccd84c02010-08-16 19:57:13 -0700454}
455
Luigi Semenzato8accd332011-05-17 16:37:18 -0700456bool MetricsDaemon::CheckSystemCrash(const string& crash_file) {
Darin Petkov38d5cb02010-06-24 12:10:26 -0700457 FilePath crash_detected(crash_file);
Ben Chan2e6543d2014-02-05 23:26:25 -0800458 if (!base::PathExists(crash_detected))
Ken Mixterccd84c02010-08-16 19:57:13 -0700459 return false;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700460
461 // Deletes the crash-detected file so that the daemon doesn't report
462 // another kernel crash in case it's restarted.
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800463 base::DeleteFile(crash_detected, false); // not recursive
Ken Mixterccd84c02010-08-16 19:57:13 -0700464 return true;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700465}
466
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700467void MetricsDaemon::StatsReporterInit() {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800468 DiskStatsReadStats(&read_sectors_, &write_sectors_);
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700469 VmStatsReadStats(&vmstats_);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800470 // The first time around just run the long stat, so we don't delay boot.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700471 stats_state_ = kStatsLong;
472 stats_initial_time_ = GetActiveTime();
473 if (stats_initial_time_ < 0) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700474 LOG(WARNING) << "not collecting disk stats";
475 } else {
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700476 ScheduleStatsCallback(kMetricStatsLongInterval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700477 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800478}
479
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700480void MetricsDaemon::ScheduleStatsCallback(int wait) {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800481 if (testing_) {
482 return;
483 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700484 g_timeout_add_seconds(wait, StatsCallbackStatic, this);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800485}
486
Ben Chanf05ab402014-08-07 00:54:59 -0700487bool MetricsDaemon::DiskStatsReadStats(uint64_t* read_sectors,
488 uint64_t* write_sectors) {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800489 int nchars;
490 int nitems;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700491 bool success = false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800492 char line[200];
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700493 if (diskstats_path_.empty()) {
494 return false;
495 }
Luigi Semenzato0f132bb2011-02-28 11:17:43 -0800496 int file = HANDLE_EINTR(open(diskstats_path_.c_str(), O_RDONLY));
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800497 if (file < 0) {
498 PLOG(WARNING) << "cannot open " << diskstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700499 return false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800500 }
501 nchars = HANDLE_EINTR(read(file, line, sizeof(line)));
502 if (nchars < 0) {
503 PLOG(WARNING) << "cannot read from " << diskstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700504 return false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800505 } else {
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700506 LOG_IF(WARNING, nchars == sizeof(line))
507 << "line too long in " << diskstats_path_;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800508 line[nchars] = '\0';
Nathan Bullockdc1ef3c2014-09-17 09:24:10 -0400509 nitems = sscanf(line, "%*d %*d %" PRIu64 " %*d %*d %*d %" PRIu64,
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800510 read_sectors, write_sectors);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700511 if (nitems == 2) {
512 success = true;
513 } else {
514 LOG(WARNING) << "found " << nitems << " items in "
515 << diskstats_path_ << ", expected 2";
516 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800517 }
Mike Frysinger3e8a8512014-05-14 16:14:37 -0400518 IGNORE_EINTR(close(file));
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700519 return success;
520}
521
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700522bool MetricsDaemon::VmStatsParseStats(const char* stats,
523 struct VmstatRecord* record) {
524 // a mapping of string name to field in VmstatRecord and whether we found it
525 struct mapping {
526 const string name;
527 uint64_t* value_p;
528 bool found;
529 } map[] =
530 { { .name = "pgmajfault",
531 .value_p = &record->page_faults_,
532 .found = false },
533 { .name = "pswpin",
534 .value_p = &record->swap_in_,
535 .found = false },
536 { .name = "pswpout",
537 .value_p = &record->swap_out_,
538 .found = false }, };
539
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700540 // Each line in the file has the form
541 // <ID> <VALUE>
542 // for instance:
543 // nr_free_pages 213427
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700544 vector<string> lines;
545 Tokenize(stats, "\n", &lines);
546 for (vector<string>::iterator it = lines.begin();
547 it != lines.end(); ++it) {
548 vector<string> tokens;
549 base::SplitString(*it, ' ', &tokens);
550 if (tokens.size() == 2) {
551 for (unsigned int i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
552 if (!tokens[0].compare(map[i].name)) {
553 if (!base::StringToUint64(tokens[1], map[i].value_p))
554 return false;
555 map[i].found = true;
556 }
557 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700558 } else {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700559 LOG(WARNING) << "unexpected vmstat format";
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700560 }
561 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700562 // make sure we got all the stats
563 for (unsigned i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
564 if (map[i].found == false) {
565 LOG(WARNING) << "vmstat missing " << map[i].name;
566 return false;
567 }
568 }
569 return true;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700570}
571
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700572bool MetricsDaemon::VmStatsReadStats(struct VmstatRecord* stats) {
573 string value_string;
574 FilePath* path = new FilePath(vmstats_path_);
Ben Chan2e6543d2014-02-05 23:26:25 -0800575 if (!base::ReadFileToString(*path, &value_string)) {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700576 delete path;
577 LOG(WARNING) << "cannot read " << vmstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700578 return false;
579 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700580 delete path;
581 return VmStatsParseStats(value_string.c_str(), stats);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800582}
583
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700584bool MetricsDaemon::ReadFreqToInt(const string& sysfs_file_name, int* value) {
Luigi Semenzatod92d18c2013-06-04 13:24:21 -0700585 const FilePath sysfs_path(sysfs_file_name);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700586 string value_string;
Ben Chan2e6543d2014-02-05 23:26:25 -0800587 if (!base::ReadFileToString(sysfs_path, &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700588 LOG(WARNING) << "cannot read " << sysfs_path.value().c_str();
589 return false;
590 }
Ben Chan2e6543d2014-02-05 23:26:25 -0800591 if (!base::RemoveChars(value_string, "\n", &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700592 LOG(WARNING) << "no newline in " << value_string;
593 // Continue even though the lack of newline is suspicious.
594 }
595 if (!base::StringToInt(value_string, value)) {
596 LOG(WARNING) << "cannot convert " << value_string << " to int";
597 return false;
598 }
599 return true;
600}
601
602void MetricsDaemon::SendCpuThrottleMetrics() {
603 // |max_freq| is 0 only the first time through.
604 static int max_freq = 0;
605 if (max_freq == -1)
606 // Give up, as sysfs did not report max_freq correctly.
607 return;
608 if (max_freq == 0 || testing_) {
609 // One-time initialization of max_freq. (Every time when testing.)
610 if (!ReadFreqToInt(cpuinfo_max_freq_path_, &max_freq)) {
611 max_freq = -1;
612 return;
613 }
614 if (max_freq == 0) {
615 LOG(WARNING) << "sysfs reports 0 max CPU frequency\n";
616 max_freq = -1;
617 return;
618 }
619 if (max_freq % 10000 == 1000) {
620 // Special case: system has turbo mode, and max non-turbo frequency is
621 // max_freq - 1000. This relies on "normal" (non-turbo) frequencies
622 // being multiples of (at least) 10 MHz. Although there is no guarantee
623 // of this, it seems a fairly reasonable assumption. Otherwise we should
624 // read scaling_available_frequencies, sort the frequencies, compare the
625 // two highest ones, and check if they differ by 1000 (kHz) (and that's a
626 // hack too, no telling when it will change).
627 max_freq -= 1000;
628 }
629 }
630 int scaled_freq = 0;
631 if (!ReadFreqToInt(scaling_max_freq_path_, &scaled_freq))
632 return;
633 // Frequencies are in kHz. If scaled_freq > max_freq, turbo is on, but
634 // scaled_freq is not the actual turbo frequency. We indicate this situation
635 // with a 101% value.
636 int percent = scaled_freq > max_freq ? 101 : scaled_freq / (max_freq / 100);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800637 SendLinearSample(kMetricScaledCpuFrequencyName, percent, 101, 102);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700638}
639
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800640// static
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700641gboolean MetricsDaemon::StatsCallbackStatic(void* handle) {
642 (static_cast<MetricsDaemon*>(handle))->StatsCallback();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800643 return false; // one-time callback
644}
645
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700646// Collects disk and vm stats alternating over a short and a long interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700647
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700648void MetricsDaemon::StatsCallback() {
Ben Chanf05ab402014-08-07 00:54:59 -0700649 uint64_t read_sectors_now, write_sectors_now;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700650 struct VmstatRecord vmstats_now;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700651 double time_now = GetActiveTime();
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700652 double delta_time = time_now - stats_initial_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700653 if (testing_) {
654 // Fake the time when testing.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700655 delta_time = stats_state_ == kStatsShort ?
656 kMetricStatsShortInterval : kMetricStatsLongInterval;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700657 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700658 bool diskstats_success = DiskStatsReadStats(&read_sectors_now,
659 &write_sectors_now);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700660 int delta_read = read_sectors_now - read_sectors_;
661 int delta_write = write_sectors_now - write_sectors_;
662 int read_sectors_per_second = delta_read / delta_time;
663 int write_sectors_per_second = delta_write / delta_time;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700664 bool vmstats_success = VmStatsReadStats(&vmstats_now);
665 uint64_t delta_faults = vmstats_now.page_faults_ - vmstats_.page_faults_;
666 uint64_t delta_swap_in = vmstats_now.swap_in_ - vmstats_.swap_in_;
667 uint64_t delta_swap_out = vmstats_now.swap_out_ - vmstats_.swap_out_;
668 uint64_t page_faults_per_second = delta_faults / delta_time;
669 uint64_t swap_in_per_second = delta_swap_in / delta_time;
670 uint64_t swap_out_per_second = delta_swap_out / delta_time;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800671
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700672 switch (stats_state_) {
673 case kStatsShort:
674 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800675 SendSample(kMetricReadSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700676 read_sectors_per_second,
677 1,
678 kMetricSectorsIOMax,
679 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800680 SendSample(kMetricWriteSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700681 write_sectors_per_second,
682 1,
683 kMetricSectorsIOMax,
684 kMetricSectorsBuckets);
685 }
686 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800687 SendSample(kMetricPageFaultsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700688 page_faults_per_second,
689 1,
690 kMetricPageFaultsMax,
691 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800692 SendSample(kMetricSwapInShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700693 swap_in_per_second,
694 1,
695 kMetricPageFaultsMax,
696 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800697 SendSample(kMetricSwapOutShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700698 swap_out_per_second,
699 1,
700 kMetricPageFaultsMax,
701 kMetricPageFaultsBuckets);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700702 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800703 // Schedule long callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700704 stats_state_ = kStatsLong;
705 ScheduleStatsCallback(kMetricStatsLongInterval -
706 kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800707 break;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700708 case kStatsLong:
709 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800710 SendSample(kMetricReadSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700711 read_sectors_per_second,
712 1,
713 kMetricSectorsIOMax,
714 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800715 SendSample(kMetricWriteSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700716 write_sectors_per_second,
717 1,
718 kMetricSectorsIOMax,
719 kMetricSectorsBuckets);
720 // Reset sector counters.
721 read_sectors_ = read_sectors_now;
722 write_sectors_ = write_sectors_now;
723 }
724 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800725 SendSample(kMetricPageFaultsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700726 page_faults_per_second,
727 1,
728 kMetricPageFaultsMax,
729 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800730 SendSample(kMetricSwapInLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700731 swap_in_per_second,
732 1,
733 kMetricPageFaultsMax,
734 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800735 SendSample(kMetricSwapOutLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700736 swap_out_per_second,
737 1,
738 kMetricPageFaultsMax,
739 kMetricPageFaultsBuckets);
740
741 vmstats_ = vmstats_now;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700742 }
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700743 SendCpuThrottleMetrics();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700744 // Set start time for new cycle.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700745 stats_initial_time_ = time_now;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800746 // Schedule short callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700747 stats_state_ = kStatsShort;
748 ScheduleStatsCallback(kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800749 break;
750 default:
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700751 LOG(FATAL) << "Invalid stats state";
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800752 }
753}
754
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700755void MetricsDaemon::ScheduleMeminfoCallback(int wait) {
756 if (testing_) {
757 return;
758 }
759 g_timeout_add_seconds(wait, MeminfoCallbackStatic, this);
760}
761
762// static
763gboolean MetricsDaemon::MeminfoCallbackStatic(void* handle) {
764 return (static_cast<MetricsDaemon*>(handle))->MeminfoCallback();
765}
766
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700767bool MetricsDaemon::MeminfoCallback() {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700768 string meminfo_raw;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700769 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800770 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700771 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
772 return false;
773 }
Luigi Semenzato96360192014-06-04 10:53:35 -0700774 // Make both calls even if the first one fails.
775 bool success = ProcessMeminfo(meminfo_raw);
776 return ReportZram(base::FilePath(FILE_PATH_LITERAL("/sys/block/zram0"))) &&
777 success;
778}
779
780// static
781bool MetricsDaemon::ReadFileToUint64(const base::FilePath& path,
Ben Chanf05ab402014-08-07 00:54:59 -0700782 uint64_t* value) {
Luigi Semenzato96360192014-06-04 10:53:35 -0700783 std::string content;
784 if (!base::ReadFileToString(path, &content)) {
785 PLOG(WARNING) << "cannot read " << path.MaybeAsASCII();
786 return false;
787 }
Luigi Semenzato4a6c9422014-06-30 18:12:28 -0700788 // Remove final newline.
789 base::TrimWhitespaceASCII(content, base::TRIM_TRAILING, &content);
Luigi Semenzato96360192014-06-04 10:53:35 -0700790 if (!base::StringToUint64(content, value)) {
791 LOG(WARNING) << "invalid integer: " << content;
792 return false;
793 }
794 return true;
795}
796
797bool MetricsDaemon::ReportZram(const base::FilePath& zram_dir) {
798 // Data sizes are in bytes. |zero_pages| is in number of pages.
Ben Chanf05ab402014-08-07 00:54:59 -0700799 uint64_t compr_data_size, orig_data_size, zero_pages;
Luigi Semenzato96360192014-06-04 10:53:35 -0700800 const size_t page_size = 4096;
801
802 if (!ReadFileToUint64(zram_dir.Append(kComprDataSizeName),
803 &compr_data_size) ||
804 !ReadFileToUint64(zram_dir.Append(kOrigDataSizeName), &orig_data_size) ||
805 !ReadFileToUint64(zram_dir.Append(kZeroPagesName), &zero_pages)) {
806 return false;
807 }
808
809 // |orig_data_size| does not include zero-filled pages.
810 orig_data_size += zero_pages * page_size;
811
812 const int compr_data_size_mb = compr_data_size >> 20;
813 const int savings_mb = (orig_data_size - compr_data_size) >> 20;
814 const int zero_ratio_percent = zero_pages * page_size * 100 / orig_data_size;
815
816 // Report compressed size in megabytes. 100 MB or less has little impact.
817 SendSample("Platform.ZramCompressedSize", compr_data_size_mb, 100, 4000, 50);
818 SendSample("Platform.ZramSavings", savings_mb, 100, 4000, 50);
819 // The compression ratio is multiplied by 100 for better resolution. The
820 // ratios of interest are between 1 and 6 (100% and 600% as reported). We
821 // don't want samples when very little memory is being compressed.
822 if (compr_data_size_mb >= 1) {
823 SendSample("Platform.ZramCompressionRatioPercent",
824 orig_data_size * 100 / compr_data_size, 100, 600, 50);
825 }
826 // The values of interest for zero_pages are between 1MB and 1GB. The units
827 // are number of pages.
828 SendSample("Platform.ZramZeroPages", zero_pages, 256, 256 * 1024, 50);
829 SendSample("Platform.ZramZeroRatioPercent", zero_ratio_percent, 1, 50, 50);
830
831 return true;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700832}
833
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700834bool MetricsDaemon::ProcessMeminfo(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700835 static const MeminfoRecord fields_array[] = {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700836 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
837 { "MemFree", "MemFree" },
838 { "Buffers", "Buffers" },
839 { "Cached", "Cached" },
840 // { "SwapCached", "SwapCached" },
841 { "Active", "Active" },
842 { "Inactive", "Inactive" },
843 { "ActiveAnon", "Active(anon)" },
844 { "InactiveAnon", "Inactive(anon)" },
845 { "ActiveFile" , "Active(file)" },
846 { "InactiveFile", "Inactive(file)" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800847 { "Unevictable", "Unevictable", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700848 // { "Mlocked", "Mlocked" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800849 { "SwapTotal", "SwapTotal", kMeminfoOp_SwapTotal },
850 { "SwapFree", "SwapFree", kMeminfoOp_SwapFree },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700851 // { "Dirty", "Dirty" },
852 // { "Writeback", "Writeback" },
853 { "AnonPages", "AnonPages" },
854 { "Mapped", "Mapped" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800855 { "Shmem", "Shmem", kMeminfoOp_HistLog },
856 { "Slab", "Slab", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700857 // { "SReclaimable", "SReclaimable" },
858 // { "SUnreclaim", "SUnreclaim" },
859 };
Luigi Semenzato8accd332011-05-17 16:37:18 -0700860 vector<MeminfoRecord> fields(fields_array,
861 fields_array + arraysize(fields_array));
862 if (!FillMeminfo(meminfo_raw, &fields)) {
863 return false;
864 }
865 int total_memory = fields[0].value;
866 if (total_memory == 0) {
867 // this "cannot happen"
868 LOG(WARNING) << "borked meminfo parser";
869 return false;
870 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800871 int swap_total = 0;
872 int swap_free = 0;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700873 // Send all fields retrieved, except total memory.
874 for (unsigned int i = 1; i < fields.size(); i++) {
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800875 string metrics_name = base::StringPrintf("Platform.Meminfo%s",
876 fields[i].name);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800877 int percent;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800878 switch (fields[i].op) {
879 case kMeminfoOp_HistPercent:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800880 // report value as percent of total memory
881 percent = fields[i].value * 100 / total_memory;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800882 SendLinearSample(metrics_name, percent, 100, 101);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800883 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800884 case kMeminfoOp_HistLog:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800885 // report value in kbytes, log scale, 4Gb max
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800886 SendSample(metrics_name, fields[i].value, 1, 4 * 1000 * 1000, 100);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800887 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800888 case kMeminfoOp_SwapTotal:
889 swap_total = fields[i].value;
890 case kMeminfoOp_SwapFree:
891 swap_free = fields[i].value;
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800892 break;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700893 }
894 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800895 if (swap_total > 0) {
896 int swap_used = swap_total - swap_free;
897 int swap_used_percent = swap_used * 100 / swap_total;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800898 SendSample("Platform.MeminfoSwapUsed", swap_used, 1, 8 * 1000 * 1000, 100);
899 SendLinearSample("Platform.MeminfoSwapUsedPercent", swap_used_percent,
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800900 100, 101);
901 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700902 return true;
903}
904
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700905bool MetricsDaemon::FillMeminfo(const string& meminfo_raw,
906 vector<MeminfoRecord>* fields) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700907 vector<string> lines;
908 unsigned int nlines = Tokenize(meminfo_raw, "\n", &lines);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700909
910 // Scan meminfo output and collect field values. Each field name has to
911 // match a meminfo entry (case insensitive) after removing non-alpha
912 // characters from the entry.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700913 unsigned int ifield = 0;
914 for (unsigned int iline = 0;
915 iline < nlines && ifield < fields->size();
916 iline++) {
917 vector<string> tokens;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700918 Tokenize(lines[iline], ": ", &tokens);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700919 if (strcmp((*fields)[ifield].match, tokens[0].c_str()) == 0) {
920 // Name matches. Parse value and save.
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700921 char* rest;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700922 (*fields)[ifield].value =
923 static_cast<int>(strtol(tokens[1].c_str(), &rest, 10));
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700924 if (*rest != '\0') {
925 LOG(WARNING) << "missing meminfo value";
926 return false;
927 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700928 ifield++;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700929 }
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700930 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700931 if (ifield < fields->size()) {
932 // End of input reached while scanning.
933 LOG(WARNING) << "cannot find field " << (*fields)[ifield].match
934 << " and following";
935 return false;
936 }
937 return true;
938}
939
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800940void MetricsDaemon::ScheduleMemuseCallback(double interval) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700941 if (testing_) {
942 return;
943 }
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800944 g_timeout_add_seconds(interval, MemuseCallbackStatic, this);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700945}
946
947// static
948gboolean MetricsDaemon::MemuseCallbackStatic(void* handle) {
949 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle);
950 daemon->MemuseCallback();
951 return false;
952}
953
954void MetricsDaemon::MemuseCallback() {
955 // Since we only care about active time (i.e. uptime minus sleep time) but
956 // the callbacks are driven by real time (uptime), we check if we should
957 // reschedule this callback due to intervening sleep periods.
958 double now = GetActiveTime();
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800959 // Avoid intervals of less than one second.
960 double remaining_time = ceil(memuse_final_time_ - now);
961 if (remaining_time > 0) {
962 ScheduleMemuseCallback(remaining_time);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700963 } else {
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800964 // Report stats and advance the measurement interval unless there are
965 // errors or we've completed the last interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700966 if (MemuseCallbackWork() &&
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800967 memuse_interval_index_ < arraysize(kMemuseIntervals)) {
968 double interval = kMemuseIntervals[memuse_interval_index_++];
969 memuse_final_time_ = now + interval;
970 ScheduleMemuseCallback(interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700971 }
972 }
973}
974
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700975bool MetricsDaemon::MemuseCallbackWork() {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700976 string meminfo_raw;
977 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800978 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700979 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
980 return false;
981 }
982 return ProcessMemuse(meminfo_raw);
983}
984
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700985bool MetricsDaemon::ProcessMemuse(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700986 static const MeminfoRecord fields_array[] = {
987 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
988 { "ActiveAnon", "Active(anon)" },
989 { "InactiveAnon", "Inactive(anon)" },
990 };
991 vector<MeminfoRecord> fields(fields_array,
992 fields_array + arraysize(fields_array));
993 if (!FillMeminfo(meminfo_raw, &fields)) {
994 return false;
995 }
996 int total = fields[0].value;
997 int active_anon = fields[1].value;
998 int inactive_anon = fields[2].value;
999 if (total == 0) {
1000 // this "cannot happen"
1001 LOG(WARNING) << "borked meminfo parser";
1002 return false;
1003 }
Luigi Semenzato859b3f02014-02-05 15:33:19 -08001004 string metrics_name = base::StringPrintf("Platform.MemuseAnon%d",
1005 memuse_interval_index_);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001006 SendLinearSample(metrics_name, (active_anon + inactive_anon) * 100 / total,
Luigi Semenzato8accd332011-05-17 16:37:18 -07001007 100, 101);
1008 return true;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001009}
1010
Luigi Semenzato5ef2e392014-04-15 15:15:02 -07001011void MetricsDaemon::ReportDailyUse(int use_seconds) {
1012 if (use_seconds <= 0)
Darin Petkov1bb904e2010-06-16 15:58:06 -07001013 return;
1014
Luigi Semenzato5ef2e392014-04-15 15:15:02 -07001015 int minutes = (use_seconds + kSecondsPerMinute / 2) / kSecondsPerMinute;
1016 SendSample("Logging.DailyUseTime",
1017 minutes,
1018 1,
1019 kMinutesPerDay * 30 * 2, // cumulative---two months worth
1020 50);
Darin Petkovf1e85e42010-06-10 15:59:53 -07001021}
1022
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001023void MetricsDaemon::SendSample(const string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -07001024 int min, int max, int nbuckets) {
Darin Petkovfc91b422010-05-12 13:05:45 -07001025 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -07001026}
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001027
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001028void MetricsDaemon::SendKernelCrashesCumulativeCountStats() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001029 // Report the number of crashes for this OS version, but don't clear the
1030 // counter. It is cleared elsewhere on version change.
Ben Chanf05ab402014-08-07 00:54:59 -07001031 int64_t crashes_count = kernel_crashes_version_count_->Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001032 SendSample(kernel_crashes_version_count_->Name(),
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001033 crashes_count,
1034 1, // value of first bucket
1035 500, // value of last bucket
1036 100); // number of buckets
1037
1038
Ben Chanf05ab402014-08-07 00:54:59 -07001039 int64_t cpu_use_ms = version_cumulative_cpu_use_->Get();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001040 SendSample(version_cumulative_cpu_use_->Name(),
1041 cpu_use_ms / 1000, // stat is in seconds
1042 1, // device may be used very little...
1043 8 * 1000 * 1000, // ... or a lot (a little over 90 days)
1044 100);
1045
1046 // On the first run after an autoupdate, cpu_use_ms and active_use_seconds
1047 // can be zero. Avoid division by zero.
1048 if (cpu_use_ms > 0) {
1049 // Send the crash frequency since update in number of crashes per CPU year.
1050 SendSample("Logging.KernelCrashesPerCpuYear",
1051 crashes_count * kSecondsPerDay * 365 * 1000 / cpu_use_ms,
1052 1,
1053 1000 * 1000, // about one crash every 30s of CPU time
1054 100);
1055 }
1056
Ben Chanf05ab402014-08-07 00:54:59 -07001057 int64_t active_use_seconds = version_cumulative_active_use_->Get();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001058 if (active_use_seconds > 0) {
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001059 SendSample(version_cumulative_active_use_->Name(),
1060 active_use_seconds / 1000, // stat is in seconds
1061 1, // device may be used very little...
1062 8 * 1000 * 1000, // ... or a lot (about 90 days)
1063 100);
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001064 // Same as above, but per year of active time.
1065 SendSample("Logging.KernelCrashesPerActiveYear",
1066 crashes_count * kSecondsPerDay * 365 / active_use_seconds,
1067 1,
1068 1000 * 1000, // about one crash every 30s of active time
1069 100);
1070 }
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001071}
1072
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001073void MetricsDaemon::SendDailyUseSample(
1074 const scoped_ptr<PersistentInteger>& use) {
1075 SendSample(use->Name(),
1076 use->GetAndClear(),
1077 1, // value of first bucket
1078 kSecondsPerDay, // value of last bucket
1079 50); // number of buckets
1080}
1081
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001082void MetricsDaemon::SendCrashIntervalSample(
1083 const scoped_ptr<PersistentInteger>& interval) {
1084 SendSample(interval->Name(),
1085 interval->GetAndClear(),
1086 1, // value of first bucket
1087 4 * kSecondsPerWeek, // value of last bucket
1088 50); // number of buckets
1089}
1090
1091void MetricsDaemon::SendCrashFrequencySample(
1092 const scoped_ptr<PersistentInteger>& frequency) {
1093 SendSample(frequency->Name(),
1094 frequency->GetAndClear(),
1095 1, // value of first bucket
1096 100, // value of last bucket
1097 50); // number of buckets
1098}
1099
1100void MetricsDaemon::SendLinearSample(const string& name, int sample,
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001101 int max, int nbuckets) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001102 // TODO(semenzato): add a proper linear histogram to the Chrome external
1103 // metrics API.
1104 LOG_IF(FATAL, nbuckets != max + 1) << "unsupported histogram scale";
1105 metrics_lib_->SendEnumToUMA(name, sample, max);
1106}
Daniel Eratc83975a2014-04-04 08:53:44 -07001107
1108void MetricsDaemon::UpdateStats(TimeTicks now_ticks,
1109 Time now_wall_time) {
1110 const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds();
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001111 daily_active_use_->Add(elapsed_seconds);
1112 version_cumulative_active_use_->Add(elapsed_seconds);
Daniel Eratc83975a2014-04-04 08:53:44 -07001113 user_crash_interval_->Add(elapsed_seconds);
1114 kernel_crash_interval_->Add(elapsed_seconds);
1115 version_cumulative_cpu_use_->Add(GetIncrementalCpuUse().InMilliseconds());
1116 last_update_stats_time_ = now_ticks;
1117
1118 const TimeDelta since_epoch = now_wall_time - Time::UnixEpoch();
1119 const int day = since_epoch.InDays();
1120 const int week = day / 7;
1121
1122 if (daily_cycle_->Get() != day) {
1123 daily_cycle_->Set(day);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001124 SendDailyUseSample(daily_active_use_);
1125 SendDailyUseSample(version_cumulative_active_use_);
Daniel Eratc83975a2014-04-04 08:53:44 -07001126 SendCrashFrequencySample(any_crashes_daily_count_);
1127 SendCrashFrequencySample(user_crashes_daily_count_);
1128 SendCrashFrequencySample(kernel_crashes_daily_count_);
1129 SendCrashFrequencySample(unclean_shutdowns_daily_count_);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001130 SendKernelCrashesCumulativeCountStats();
Daniel Eratc83975a2014-04-04 08:53:44 -07001131 }
1132
1133 if (weekly_cycle_->Get() != week) {
1134 weekly_cycle_->Set(week);
1135 SendCrashFrequencySample(any_crashes_weekly_count_);
1136 SendCrashFrequencySample(user_crashes_weekly_count_);
1137 SendCrashFrequencySample(kernel_crashes_weekly_count_);
1138 SendCrashFrequencySample(unclean_shutdowns_weekly_count_);
1139 }
1140}
1141
1142// static
1143gboolean MetricsDaemon::HandleUpdateStatsTimeout(gpointer data) {
1144 static_cast<MetricsDaemon*>(data)->UpdateStats(TimeTicks::Now(), Time::Now());
1145 return TRUE;
1146}