Darin Petkov | 8032dd0 | 2011-05-09 16:33:19 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 5 | // 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 Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 10 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 11 | #include <fcntl.h> |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 12 | #include <inttypes.h> |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 13 | #include <math.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 14 | #include <string.h> |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 15 | #include <time.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 16 | |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 17 | #include <base/files/file_path.h> |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 18 | #include <base/files/file_util.h> |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 19 | #include <base/hash.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 20 | #include <base/logging.h> |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 21 | #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 Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 25 | #include <base/sys_info.h> |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 26 | #include <chromeos/dbus/service_constants.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 27 | #include <dbus/dbus-glib-lowlevel.h> |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 28 | #include "uploader/upload_service.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 29 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 30 | using base::FilePath; |
| 31 | using base::StringPrintf; |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 32 | using base::Time; |
| 33 | using base::TimeDelta; |
| 34 | using base::TimeTicks; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 35 | using chromeos_metrics::PersistentInteger; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 36 | using std::map; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 37 | using std::string; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 38 | using std::vector; |
| 39 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 40 | namespace { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 41 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 42 | #define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error") |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 43 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 44 | const char kCrashReporterInterface[] = "org.chromium.CrashReporter"; |
| 45 | const char kCrashReporterUserCrashSignal[] = "UserCrash"; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 46 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 47 | const int kSecondsPerMinute = 60; |
| 48 | const int kMinutesPerHour = 60; |
| 49 | const int kHoursPerDay = 24; |
| 50 | const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour; |
| 51 | const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay; |
| 52 | const int kDaysPerWeek = 7; |
| 53 | const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 54 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 55 | // Interval between calls to UpdateStats(). |
| 56 | const guint kUpdateStatsIntervalMs = 300000; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 57 | |
Luigi Semenzato | c5a9234 | 2014-02-14 15:05:51 -0800 | [diff] [blame] | 58 | const char kKernelCrashDetectedFile[] = "/var/run/kernel-crash-detected"; |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 59 | const char kUncleanShutdownDetectedFile[] = |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 60 | "/var/run/unclean-shutdown-detected"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 61 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 62 | } // namespace |
| 63 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 64 | // disk stats metrics |
| 65 | |
| 66 | // The {Read,Write}Sectors numbers are in sectors/second. |
| 67 | // A sector is usually 512 bytes. |
| 68 | |
| 69 | const char MetricsDaemon::kMetricReadSectorsLongName[] = |
| 70 | "Platform.ReadSectorsLong"; |
| 71 | const char MetricsDaemon::kMetricWriteSectorsLongName[] = |
| 72 | "Platform.WriteSectorsLong"; |
| 73 | const char MetricsDaemon::kMetricReadSectorsShortName[] = |
| 74 | "Platform.ReadSectorsShort"; |
| 75 | const char MetricsDaemon::kMetricWriteSectorsShortName[] = |
| 76 | "Platform.WriteSectorsShort"; |
| 77 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 78 | const int MetricsDaemon::kMetricStatsShortInterval = 1; // seconds |
| 79 | const int MetricsDaemon::kMetricStatsLongInterval = 30; // seconds |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 80 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 81 | const int MetricsDaemon::kMetricMeminfoInterval = 30; // seconds |
| 82 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 83 | // Assume a max rate of 250Mb/s for reads (worse for writes) and 512 byte |
| 84 | // sectors. |
| 85 | const int MetricsDaemon::kMetricSectorsIOMax = 500000; // sectors/second |
| 86 | const int MetricsDaemon::kMetricSectorsBuckets = 50; // buckets |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 87 | // Page size is 4k, sector size is 0.5k. We're not interested in page fault |
| 88 | // rates that the disk cannot sustain. |
| 89 | const int MetricsDaemon::kMetricPageFaultsMax = kMetricSectorsIOMax / 8; |
| 90 | const int MetricsDaemon::kMetricPageFaultsBuckets = 50; |
| 91 | |
| 92 | // Major page faults, i.e. the ones that require data to be read from disk. |
| 93 | |
| 94 | const char MetricsDaemon::kMetricPageFaultsLongName[] = |
| 95 | "Platform.PageFaultsLong"; |
| 96 | const char MetricsDaemon::kMetricPageFaultsShortName[] = |
| 97 | "Platform.PageFaultsShort"; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 98 | |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 99 | // Swap in and Swap out |
| 100 | |
| 101 | const char MetricsDaemon::kMetricSwapInLongName[] = |
| 102 | "Platform.SwapInLong"; |
| 103 | const char MetricsDaemon::kMetricSwapInShortName[] = |
| 104 | "Platform.SwapInShort"; |
| 105 | |
| 106 | const char MetricsDaemon::kMetricSwapOutLongName[] = |
| 107 | "Platform.SwapOutLong"; |
| 108 | const char MetricsDaemon::kMetricSwapOutShortName[] = |
| 109 | "Platform.SwapOutShort"; |
| 110 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 111 | const char MetricsDaemon::kMetricsProcStatFileName[] = "/proc/stat"; |
| 112 | const int MetricsDaemon::kMetricsProcStatFirstLineItemsCount = 11; |
| 113 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 114 | // Thermal CPU throttling. |
| 115 | |
| 116 | const char MetricsDaemon::kMetricScaledCpuFrequencyName[] = |
| 117 | "Platform.CpuFrequencyThermalScaling"; |
| 118 | |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 119 | // Zram sysfs entries. |
| 120 | |
| 121 | const char MetricsDaemon::kComprDataSizeName[] = "compr_data_size"; |
| 122 | const char MetricsDaemon::kOrigDataSizeName[] = "orig_data_size"; |
| 123 | const char MetricsDaemon::kZeroPagesName[] = "zero_pages"; |
| 124 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 125 | // 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. |
| 129 | static 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 Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 137 | MetricsDaemon::MetricsDaemon() |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 138 | : update_stats_timeout_id_(-1), |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 139 | memuse_final_time_(0), |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 140 | memuse_interval_index_(0), |
| 141 | read_sectors_(0), |
| 142 | write_sectors_(0), |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 143 | vmstats_(), |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 144 | stats_state_(kStatsShort), |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 145 | stats_initial_time_(0), |
| 146 | ticks_per_second_(0), |
| 147 | latest_cpu_use_ticks_(0) {} |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 148 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 149 | MetricsDaemon::~MetricsDaemon() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 150 | if (update_stats_timeout_id_ > -1) |
| 151 | g_source_remove(update_stats_timeout_id_); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 154 | double 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 Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 161 | return ts.tv_sec + static_cast<double>(ts.tv_nsec) / (1000 * 1000 * 1000); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 165 | void MetricsDaemon::Run(bool run_as_daemon) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 166 | if (run_as_daemon && daemon(0, 0) != 0) |
| 167 | return; |
| 168 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 169 | if (CheckSystemCrash(kKernelCrashDetectedFile)) { |
| 170 | ProcessKernelCrash(); |
| 171 | } |
| 172 | |
| 173 | if (CheckSystemCrash(kUncleanShutdownDetectedFile)) { |
| 174 | ProcessUncleanShutdown(); |
| 175 | } |
| 176 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 177 | // On OS version change, clear version stats (which are reported daily). |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 178 | int32_t version = GetOsVersionHash(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 179 | if (version_cycle_->Get() != version) { |
| 180 | version_cycle_->Set(version); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 181 | kernel_crashes_version_count_->Set(0); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 182 | version_cumulative_active_use_->Set(0); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 183 | version_cumulative_cpu_use_->Set(0); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 186 | Loop(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 189 | void MetricsDaemon::RunUploaderTest() { |
Bertrand SIMONNET | eb815be | 2014-09-11 07:38:17 -0700 | [diff] [blame^] | 190 | upload_service_.reset(new UploadService(testing_)); |
| 191 | upload_service_->Init(); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 192 | upload_service_->UploadEvent(); |
| 193 | } |
| 194 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 195 | uint32_t MetricsDaemon::GetOsVersionHash() { |
| 196 | static uint32_t cached_version_hash = 0; |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 197 | 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 SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 212 | void MetricsDaemon::Init(bool testing, |
| 213 | bool uploader_active, |
| 214 | MetricsLibraryInterface* metrics_lib, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 215 | const string& diskstats_path, |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 216 | const string& vmstats_path, |
| 217 | const string& scaling_max_freq_path, |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 218 | const string& cpuinfo_max_freq_path) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 219 | testing_ = testing; |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 220 | DCHECK(metrics_lib != nullptr); |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 221 | metrics_lib_ = metrics_lib; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 222 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 223 | // Get ticks per second (HZ) on this system. |
| 224 | // Sysconf cannot fail, so no sanity checks are needed. |
| 225 | ticks_per_second_ = sysconf(_SC_CLK_TCK); |
| 226 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 227 | daily_active_use_.reset( |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 228 | new PersistentInteger("Logging.DailyUseTime")); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 229 | version_cumulative_active_use_.reset( |
| 230 | new PersistentInteger("Logging.CumulativeDailyUseTime")); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 231 | version_cumulative_cpu_use_.reset( |
| 232 | new PersistentInteger("Logging.CumulativeCpuTime")); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 233 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 234 | kernel_crash_interval_.reset( |
| 235 | new PersistentInteger("Logging.KernelCrashInterval")); |
| 236 | unclean_shutdown_interval_.reset( |
| 237 | new PersistentInteger("Logging.UncleanShutdownInterval")); |
| 238 | user_crash_interval_.reset( |
| 239 | new PersistentInteger("Logging.UserCrashInterval")); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 240 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 241 | any_crashes_daily_count_.reset( |
| 242 | new PersistentInteger("Logging.AnyCrashesDaily")); |
| 243 | any_crashes_weekly_count_.reset( |
| 244 | new PersistentInteger("Logging.AnyCrashesWeekly")); |
| 245 | user_crashes_daily_count_.reset( |
| 246 | new PersistentInteger("Logging.UserCrashesDaily")); |
| 247 | user_crashes_weekly_count_.reset( |
| 248 | new PersistentInteger("Logging.UserCrashesWeekly")); |
| 249 | kernel_crashes_daily_count_.reset( |
| 250 | new PersistentInteger("Logging.KernelCrashesDaily")); |
| 251 | kernel_crashes_weekly_count_.reset( |
| 252 | new PersistentInteger("Logging.KernelCrashesWeekly")); |
| 253 | kernel_crashes_version_count_.reset( |
| 254 | new PersistentInteger("Logging.KernelCrashesSinceUpdate")); |
| 255 | unclean_shutdowns_daily_count_.reset( |
| 256 | new PersistentInteger("Logging.UncleanShutdownsDaily")); |
| 257 | unclean_shutdowns_weekly_count_.reset( |
| 258 | new PersistentInteger("Logging.UncleanShutdownsWeekly")); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 259 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 260 | daily_cycle_.reset(new PersistentInteger("daily.cycle")); |
| 261 | weekly_cycle_.reset(new PersistentInteger("weekly.cycle")); |
| 262 | version_cycle_.reset(new PersistentInteger("version.cycle")); |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 263 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 264 | diskstats_path_ = diskstats_path; |
| 265 | vmstats_path_ = vmstats_path; |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 266 | scaling_max_freq_path_ = scaling_max_freq_path; |
| 267 | cpuinfo_max_freq_path_ = cpuinfo_max_freq_path; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 268 | StatsReporterInit(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 269 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 270 | // Start collecting meminfo stats. |
| 271 | ScheduleMeminfoCallback(kMetricMeminfoInterval); |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 272 | memuse_final_time_ = GetActiveTime() + kMemuseIntervals[0]; |
| 273 | ScheduleMemuseCallback(kMemuseIntervals[0]); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 274 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 275 | // Don't setup D-Bus and GLib in test mode. |
| 276 | if (testing) |
| 277 | return; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 278 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 279 | g_type_init(); |
Ben Chan | 6f59842 | 2013-06-22 06:29:36 -0700 | [diff] [blame] | 280 | dbus_threads_init_default(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 281 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 282 | DBusError error; |
| 283 | dbus_error_init(&error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 284 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 285 | DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 286 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 287 | "No D-Bus connection: " << SAFE_MESSAGE(error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 288 | |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 289 | dbus_connection_setup_with_g_main(connection, nullptr); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 290 | |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 291 | vector<string> matches; |
| 292 | matches.push_back( |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 293 | base::StringPrintf("type='signal',interface='%s',path='/',member='%s'", |
| 294 | kCrashReporterInterface, |
| 295 | kCrashReporterUserCrashSignal)); |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 296 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 297 | // Registers D-Bus matches for the signals we would like to catch. |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 298 | for (vector<string>::const_iterator it = matches.begin(); |
| 299 | it != matches.end(); ++it) { |
| 300 | const char* match = it->c_str(); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 301 | DLOG(INFO) << "adding dbus match: " << match; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 302 | dbus_bus_add_match(connection, match, &error); |
| 303 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 304 | "unable to add a match: " << SAFE_MESSAGE(error); |
| 305 | } |
| 306 | |
| 307 | // Adds the D-Bus filter routine to be called back whenever one of |
| 308 | // the registered D-Bus matches is successful. The daemon is not |
| 309 | // activated for D-Bus messages that don't match. |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 310 | CHECK(dbus_connection_add_filter(connection, MessageFilter, this, nullptr)); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 311 | |
| 312 | update_stats_timeout_id_ = |
| 313 | g_timeout_add(kUpdateStatsIntervalMs, &HandleUpdateStatsTimeout, this); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 314 | |
| 315 | if (uploader_active) { |
Bertrand SIMONNET | eb815be | 2014-09-11 07:38:17 -0700 | [diff] [blame^] | 316 | upload_service_.reset(new UploadService(testing_)); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 317 | upload_service_->Init(); |
| 318 | } |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void MetricsDaemon::Loop() { |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 322 | GMainLoop* loop = g_main_loop_new(nullptr, false); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 323 | g_main_loop_run(loop); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 326 | // static |
| 327 | DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection, |
| 328 | DBusMessage* message, |
| 329 | void* user_data) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 330 | int message_type = dbus_message_get_type(message); |
| 331 | if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 332 | DLOG(WARNING) << "unexpected message type " << message_type; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 333 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 334 | } |
| 335 | |
| 336 | // Signal messages always have interfaces. |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 337 | const std::string interface(dbus_message_get_interface(message)); |
| 338 | const std::string member(dbus_message_get_member(message)); |
| 339 | DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal"; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 340 | |
| 341 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data); |
| 342 | |
| 343 | DBusMessageIter iter; |
| 344 | dbus_message_iter_init(message, &iter); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 345 | if (interface == kCrashReporterInterface) { |
| 346 | CHECK_EQ(member, kCrashReporterUserCrashSignal); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 347 | daemon->ProcessUserCrash(); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 348 | } else { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 349 | // Ignore messages from the bus itself. |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 350 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 351 | } |
| 352 | |
| 353 | return DBUS_HANDLER_RESULT_HANDLED; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 356 | // One might argue that parts of this should go into |
| 357 | // chromium/src/base/sys_info_chromeos.c instead, but put it here for now. |
| 358 | |
| 359 | TimeDelta MetricsDaemon::GetIncrementalCpuUse() { |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 360 | FilePath proc_stat_path = FilePath(kMetricsProcStatFileName); |
| 361 | std::string proc_stat_string; |
| 362 | if (!base::ReadFileToString(proc_stat_path, &proc_stat_string)) { |
| 363 | LOG(WARNING) << "cannot open " << kMetricsProcStatFileName; |
| 364 | return TimeDelta(); |
| 365 | } |
| 366 | |
| 367 | std::vector<std::string> proc_stat_lines; |
| 368 | base::SplitString(proc_stat_string, '\n', &proc_stat_lines); |
| 369 | if (proc_stat_lines.empty()) { |
| 370 | LOG(WARNING) << "cannot parse " << kMetricsProcStatFileName |
| 371 | << ": " << proc_stat_string; |
| 372 | return TimeDelta(); |
| 373 | } |
| 374 | std::vector<std::string> proc_stat_totals; |
| 375 | base::SplitStringAlongWhitespace(proc_stat_lines[0], &proc_stat_totals); |
| 376 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 377 | uint64_t user_ticks, user_nice_ticks, system_ticks; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 378 | if (proc_stat_totals.size() != kMetricsProcStatFirstLineItemsCount || |
| 379 | proc_stat_totals[0] != "cpu" || |
| 380 | !base::StringToUint64(proc_stat_totals[1], &user_ticks) || |
| 381 | !base::StringToUint64(proc_stat_totals[2], &user_nice_ticks) || |
| 382 | !base::StringToUint64(proc_stat_totals[3], &system_ticks)) { |
| 383 | LOG(WARNING) << "cannot parse first line: " << proc_stat_lines[0]; |
| 384 | return TimeDelta(base::TimeDelta::FromSeconds(0)); |
| 385 | } |
| 386 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 387 | uint64_t total_cpu_use_ticks = user_ticks + user_nice_ticks + system_ticks; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 388 | |
| 389 | // Sanity check. |
| 390 | if (total_cpu_use_ticks < latest_cpu_use_ticks_) { |
| 391 | LOG(WARNING) << "CPU time decreasing from " << latest_cpu_use_ticks_ |
| 392 | << " to " << total_cpu_use_ticks; |
| 393 | return TimeDelta(); |
| 394 | } |
| 395 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 396 | uint64_t diff = total_cpu_use_ticks - latest_cpu_use_ticks_; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 397 | latest_cpu_use_ticks_ = total_cpu_use_ticks; |
| 398 | // Use microseconds to avoid significant truncations. |
| 399 | return base::TimeDelta::FromMicroseconds( |
| 400 | diff * 1000 * 1000 / ticks_per_second_); |
| 401 | } |
| 402 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 403 | void MetricsDaemon::ProcessUserCrash() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 404 | // Counts the active time up to now. |
| 405 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 406 | |
| 407 | // Reports the active use time since the last crash and resets it. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 408 | SendCrashIntervalSample(user_crash_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 409 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 410 | any_crashes_daily_count_->Add(1); |
| 411 | any_crashes_weekly_count_->Add(1); |
| 412 | user_crashes_daily_count_->Add(1); |
| 413 | user_crashes_weekly_count_->Add(1); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 416 | void MetricsDaemon::ProcessKernelCrash() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 417 | // Counts the active time up to now. |
| 418 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 419 | |
| 420 | // Reports the active use time since the last crash and resets it. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 421 | SendCrashIntervalSample(kernel_crash_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 422 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 423 | any_crashes_daily_count_->Add(1); |
| 424 | any_crashes_weekly_count_->Add(1); |
| 425 | kernel_crashes_daily_count_->Add(1); |
| 426 | kernel_crashes_weekly_count_->Add(1); |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 427 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 428 | kernel_crashes_version_count_->Add(1); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 431 | void MetricsDaemon::ProcessUncleanShutdown() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 432 | // Counts the active time up to now. |
| 433 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 434 | |
| 435 | // Reports the active use time since the last crash and resets it. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 436 | SendCrashIntervalSample(unclean_shutdown_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 437 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 438 | unclean_shutdowns_daily_count_->Add(1); |
| 439 | unclean_shutdowns_weekly_count_->Add(1); |
| 440 | any_crashes_daily_count_->Add(1); |
| 441 | any_crashes_weekly_count_->Add(1); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 444 | bool MetricsDaemon::CheckSystemCrash(const string& crash_file) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 445 | FilePath crash_detected(crash_file); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 446 | if (!base::PathExists(crash_detected)) |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 447 | return false; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 448 | |
| 449 | // Deletes the crash-detected file so that the daemon doesn't report |
| 450 | // another kernel crash in case it's restarted. |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 451 | base::DeleteFile(crash_detected, false); // not recursive |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 452 | return true; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 455 | void MetricsDaemon::StatsReporterInit() { |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 456 | DiskStatsReadStats(&read_sectors_, &write_sectors_); |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 457 | VmStatsReadStats(&vmstats_); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 458 | // The first time around just run the long stat, so we don't delay boot. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 459 | stats_state_ = kStatsLong; |
| 460 | stats_initial_time_ = GetActiveTime(); |
| 461 | if (stats_initial_time_ < 0) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 462 | LOG(WARNING) << "not collecting disk stats"; |
| 463 | } else { |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 464 | ScheduleStatsCallback(kMetricStatsLongInterval); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 465 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 468 | void MetricsDaemon::ScheduleStatsCallback(int wait) { |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 469 | if (testing_) { |
| 470 | return; |
| 471 | } |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 472 | g_timeout_add_seconds(wait, StatsCallbackStatic, this); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 475 | bool MetricsDaemon::DiskStatsReadStats(uint64_t* read_sectors, |
| 476 | uint64_t* write_sectors) { |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 477 | int nchars; |
| 478 | int nitems; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 479 | bool success = false; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 480 | char line[200]; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 481 | if (diskstats_path_.empty()) { |
| 482 | return false; |
| 483 | } |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 484 | int file = HANDLE_EINTR(open(diskstats_path_.c_str(), O_RDONLY)); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 485 | if (file < 0) { |
| 486 | PLOG(WARNING) << "cannot open " << diskstats_path_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 487 | return false; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 488 | } |
| 489 | nchars = HANDLE_EINTR(read(file, line, sizeof(line))); |
| 490 | if (nchars < 0) { |
| 491 | PLOG(WARNING) << "cannot read from " << diskstats_path_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 492 | return false; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 493 | } else { |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 494 | LOG_IF(WARNING, nchars == sizeof(line)) |
| 495 | << "line too long in " << diskstats_path_; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 496 | line[nchars] = '\0'; |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 497 | nitems = sscanf(line, "%*d %*d %" PRIu64 "d %*d %*d %*d %" PRIu64 "d", |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 498 | read_sectors, write_sectors); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 499 | if (nitems == 2) { |
| 500 | success = true; |
| 501 | } else { |
| 502 | LOG(WARNING) << "found " << nitems << " items in " |
| 503 | << diskstats_path_ << ", expected 2"; |
| 504 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 505 | } |
Mike Frysinger | 3e8a851 | 2014-05-14 16:14:37 -0400 | [diff] [blame] | 506 | IGNORE_EINTR(close(file)); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 507 | return success; |
| 508 | } |
| 509 | |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 510 | bool MetricsDaemon::VmStatsParseStats(const char* stats, |
| 511 | struct VmstatRecord* record) { |
| 512 | // a mapping of string name to field in VmstatRecord and whether we found it |
| 513 | struct mapping { |
| 514 | const string name; |
| 515 | uint64_t* value_p; |
| 516 | bool found; |
| 517 | } map[] = |
| 518 | { { .name = "pgmajfault", |
| 519 | .value_p = &record->page_faults_, |
| 520 | .found = false }, |
| 521 | { .name = "pswpin", |
| 522 | .value_p = &record->swap_in_, |
| 523 | .found = false }, |
| 524 | { .name = "pswpout", |
| 525 | .value_p = &record->swap_out_, |
| 526 | .found = false }, }; |
| 527 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 528 | // Each line in the file has the form |
| 529 | // <ID> <VALUE> |
| 530 | // for instance: |
| 531 | // nr_free_pages 213427 |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 532 | vector<string> lines; |
| 533 | Tokenize(stats, "\n", &lines); |
| 534 | for (vector<string>::iterator it = lines.begin(); |
| 535 | it != lines.end(); ++it) { |
| 536 | vector<string> tokens; |
| 537 | base::SplitString(*it, ' ', &tokens); |
| 538 | if (tokens.size() == 2) { |
| 539 | for (unsigned int i = 0; i < sizeof(map)/sizeof(struct mapping); i++) { |
| 540 | if (!tokens[0].compare(map[i].name)) { |
| 541 | if (!base::StringToUint64(tokens[1], map[i].value_p)) |
| 542 | return false; |
| 543 | map[i].found = true; |
| 544 | } |
| 545 | } |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 546 | } else { |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 547 | LOG(WARNING) << "unexpected vmstat format"; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 550 | // make sure we got all the stats |
| 551 | for (unsigned i = 0; i < sizeof(map)/sizeof(struct mapping); i++) { |
| 552 | if (map[i].found == false) { |
| 553 | LOG(WARNING) << "vmstat missing " << map[i].name; |
| 554 | return false; |
| 555 | } |
| 556 | } |
| 557 | return true; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 560 | bool MetricsDaemon::VmStatsReadStats(struct VmstatRecord* stats) { |
| 561 | string value_string; |
| 562 | FilePath* path = new FilePath(vmstats_path_); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 563 | if (!base::ReadFileToString(*path, &value_string)) { |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 564 | delete path; |
| 565 | LOG(WARNING) << "cannot read " << vmstats_path_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 566 | return false; |
| 567 | } |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 568 | delete path; |
| 569 | return VmStatsParseStats(value_string.c_str(), stats); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 572 | bool MetricsDaemon::ReadFreqToInt(const string& sysfs_file_name, int* value) { |
Luigi Semenzato | d92d18c | 2013-06-04 13:24:21 -0700 | [diff] [blame] | 573 | const FilePath sysfs_path(sysfs_file_name); |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 574 | string value_string; |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 575 | if (!base::ReadFileToString(sysfs_path, &value_string)) { |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 576 | LOG(WARNING) << "cannot read " << sysfs_path.value().c_str(); |
| 577 | return false; |
| 578 | } |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 579 | if (!base::RemoveChars(value_string, "\n", &value_string)) { |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 580 | LOG(WARNING) << "no newline in " << value_string; |
| 581 | // Continue even though the lack of newline is suspicious. |
| 582 | } |
| 583 | if (!base::StringToInt(value_string, value)) { |
| 584 | LOG(WARNING) << "cannot convert " << value_string << " to int"; |
| 585 | return false; |
| 586 | } |
| 587 | return true; |
| 588 | } |
| 589 | |
| 590 | void MetricsDaemon::SendCpuThrottleMetrics() { |
| 591 | // |max_freq| is 0 only the first time through. |
| 592 | static int max_freq = 0; |
| 593 | if (max_freq == -1) |
| 594 | // Give up, as sysfs did not report max_freq correctly. |
| 595 | return; |
| 596 | if (max_freq == 0 || testing_) { |
| 597 | // One-time initialization of max_freq. (Every time when testing.) |
| 598 | if (!ReadFreqToInt(cpuinfo_max_freq_path_, &max_freq)) { |
| 599 | max_freq = -1; |
| 600 | return; |
| 601 | } |
| 602 | if (max_freq == 0) { |
| 603 | LOG(WARNING) << "sysfs reports 0 max CPU frequency\n"; |
| 604 | max_freq = -1; |
| 605 | return; |
| 606 | } |
| 607 | if (max_freq % 10000 == 1000) { |
| 608 | // Special case: system has turbo mode, and max non-turbo frequency is |
| 609 | // max_freq - 1000. This relies on "normal" (non-turbo) frequencies |
| 610 | // being multiples of (at least) 10 MHz. Although there is no guarantee |
| 611 | // of this, it seems a fairly reasonable assumption. Otherwise we should |
| 612 | // read scaling_available_frequencies, sort the frequencies, compare the |
| 613 | // two highest ones, and check if they differ by 1000 (kHz) (and that's a |
| 614 | // hack too, no telling when it will change). |
| 615 | max_freq -= 1000; |
| 616 | } |
| 617 | } |
| 618 | int scaled_freq = 0; |
| 619 | if (!ReadFreqToInt(scaling_max_freq_path_, &scaled_freq)) |
| 620 | return; |
| 621 | // Frequencies are in kHz. If scaled_freq > max_freq, turbo is on, but |
| 622 | // scaled_freq is not the actual turbo frequency. We indicate this situation |
| 623 | // with a 101% value. |
| 624 | int percent = scaled_freq > max_freq ? 101 : scaled_freq / (max_freq / 100); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 625 | SendLinearSample(kMetricScaledCpuFrequencyName, percent, 101, 102); |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 628 | // static |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 629 | gboolean MetricsDaemon::StatsCallbackStatic(void* handle) { |
| 630 | (static_cast<MetricsDaemon*>(handle))->StatsCallback(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 631 | return false; // one-time callback |
| 632 | } |
| 633 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 634 | // Collects disk and vm stats alternating over a short and a long interval. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 635 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 636 | void MetricsDaemon::StatsCallback() { |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 637 | uint64_t read_sectors_now, write_sectors_now; |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 638 | struct VmstatRecord vmstats_now; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 639 | double time_now = GetActiveTime(); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 640 | double delta_time = time_now - stats_initial_time_; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 641 | if (testing_) { |
| 642 | // Fake the time when testing. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 643 | delta_time = stats_state_ == kStatsShort ? |
| 644 | kMetricStatsShortInterval : kMetricStatsLongInterval; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 645 | } |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 646 | bool diskstats_success = DiskStatsReadStats(&read_sectors_now, |
| 647 | &write_sectors_now); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 648 | int delta_read = read_sectors_now - read_sectors_; |
| 649 | int delta_write = write_sectors_now - write_sectors_; |
| 650 | int read_sectors_per_second = delta_read / delta_time; |
| 651 | int write_sectors_per_second = delta_write / delta_time; |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 652 | bool vmstats_success = VmStatsReadStats(&vmstats_now); |
| 653 | uint64_t delta_faults = vmstats_now.page_faults_ - vmstats_.page_faults_; |
| 654 | uint64_t delta_swap_in = vmstats_now.swap_in_ - vmstats_.swap_in_; |
| 655 | uint64_t delta_swap_out = vmstats_now.swap_out_ - vmstats_.swap_out_; |
| 656 | uint64_t page_faults_per_second = delta_faults / delta_time; |
| 657 | uint64_t swap_in_per_second = delta_swap_in / delta_time; |
| 658 | uint64_t swap_out_per_second = delta_swap_out / delta_time; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 659 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 660 | switch (stats_state_) { |
| 661 | case kStatsShort: |
| 662 | if (diskstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 663 | SendSample(kMetricReadSectorsShortName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 664 | read_sectors_per_second, |
| 665 | 1, |
| 666 | kMetricSectorsIOMax, |
| 667 | kMetricSectorsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 668 | SendSample(kMetricWriteSectorsShortName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 669 | write_sectors_per_second, |
| 670 | 1, |
| 671 | kMetricSectorsIOMax, |
| 672 | kMetricSectorsBuckets); |
| 673 | } |
| 674 | if (vmstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 675 | SendSample(kMetricPageFaultsShortName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 676 | page_faults_per_second, |
| 677 | 1, |
| 678 | kMetricPageFaultsMax, |
| 679 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 680 | SendSample(kMetricSwapInShortName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 681 | swap_in_per_second, |
| 682 | 1, |
| 683 | kMetricPageFaultsMax, |
| 684 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 685 | SendSample(kMetricSwapOutShortName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 686 | swap_out_per_second, |
| 687 | 1, |
| 688 | kMetricPageFaultsMax, |
| 689 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 690 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 691 | // Schedule long callback. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 692 | stats_state_ = kStatsLong; |
| 693 | ScheduleStatsCallback(kMetricStatsLongInterval - |
| 694 | kMetricStatsShortInterval); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 695 | break; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 696 | case kStatsLong: |
| 697 | if (diskstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 698 | SendSample(kMetricReadSectorsLongName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 699 | read_sectors_per_second, |
| 700 | 1, |
| 701 | kMetricSectorsIOMax, |
| 702 | kMetricSectorsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 703 | SendSample(kMetricWriteSectorsLongName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 704 | write_sectors_per_second, |
| 705 | 1, |
| 706 | kMetricSectorsIOMax, |
| 707 | kMetricSectorsBuckets); |
| 708 | // Reset sector counters. |
| 709 | read_sectors_ = read_sectors_now; |
| 710 | write_sectors_ = write_sectors_now; |
| 711 | } |
| 712 | if (vmstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 713 | SendSample(kMetricPageFaultsLongName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 714 | page_faults_per_second, |
| 715 | 1, |
| 716 | kMetricPageFaultsMax, |
| 717 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 718 | SendSample(kMetricSwapInLongName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 719 | swap_in_per_second, |
| 720 | 1, |
| 721 | kMetricPageFaultsMax, |
| 722 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 723 | SendSample(kMetricSwapOutLongName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 724 | swap_out_per_second, |
| 725 | 1, |
| 726 | kMetricPageFaultsMax, |
| 727 | kMetricPageFaultsBuckets); |
| 728 | |
| 729 | vmstats_ = vmstats_now; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 730 | } |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 731 | SendCpuThrottleMetrics(); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 732 | // Set start time for new cycle. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 733 | stats_initial_time_ = time_now; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 734 | // Schedule short callback. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 735 | stats_state_ = kStatsShort; |
| 736 | ScheduleStatsCallback(kMetricStatsShortInterval); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 737 | break; |
| 738 | default: |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 739 | LOG(FATAL) << "Invalid stats state"; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 743 | void MetricsDaemon::ScheduleMeminfoCallback(int wait) { |
| 744 | if (testing_) { |
| 745 | return; |
| 746 | } |
| 747 | g_timeout_add_seconds(wait, MeminfoCallbackStatic, this); |
| 748 | } |
| 749 | |
| 750 | // static |
| 751 | gboolean MetricsDaemon::MeminfoCallbackStatic(void* handle) { |
| 752 | return (static_cast<MetricsDaemon*>(handle))->MeminfoCallback(); |
| 753 | } |
| 754 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 755 | bool MetricsDaemon::MeminfoCallback() { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 756 | string meminfo_raw; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 757 | const FilePath meminfo_path("/proc/meminfo"); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 758 | if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 759 | LOG(WARNING) << "cannot read " << meminfo_path.value().c_str(); |
| 760 | return false; |
| 761 | } |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 762 | // Make both calls even if the first one fails. |
| 763 | bool success = ProcessMeminfo(meminfo_raw); |
| 764 | return ReportZram(base::FilePath(FILE_PATH_LITERAL("/sys/block/zram0"))) && |
| 765 | success; |
| 766 | } |
| 767 | |
| 768 | // static |
| 769 | bool MetricsDaemon::ReadFileToUint64(const base::FilePath& path, |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 770 | uint64_t* value) { |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 771 | std::string content; |
| 772 | if (!base::ReadFileToString(path, &content)) { |
| 773 | PLOG(WARNING) << "cannot read " << path.MaybeAsASCII(); |
| 774 | return false; |
| 775 | } |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 776 | // Remove final newline. |
| 777 | base::TrimWhitespaceASCII(content, base::TRIM_TRAILING, &content); |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 778 | if (!base::StringToUint64(content, value)) { |
| 779 | LOG(WARNING) << "invalid integer: " << content; |
| 780 | return false; |
| 781 | } |
| 782 | return true; |
| 783 | } |
| 784 | |
| 785 | bool MetricsDaemon::ReportZram(const base::FilePath& zram_dir) { |
| 786 | // Data sizes are in bytes. |zero_pages| is in number of pages. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 787 | uint64_t compr_data_size, orig_data_size, zero_pages; |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 788 | const size_t page_size = 4096; |
| 789 | |
| 790 | if (!ReadFileToUint64(zram_dir.Append(kComprDataSizeName), |
| 791 | &compr_data_size) || |
| 792 | !ReadFileToUint64(zram_dir.Append(kOrigDataSizeName), &orig_data_size) || |
| 793 | !ReadFileToUint64(zram_dir.Append(kZeroPagesName), &zero_pages)) { |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | // |orig_data_size| does not include zero-filled pages. |
| 798 | orig_data_size += zero_pages * page_size; |
| 799 | |
| 800 | const int compr_data_size_mb = compr_data_size >> 20; |
| 801 | const int savings_mb = (orig_data_size - compr_data_size) >> 20; |
| 802 | const int zero_ratio_percent = zero_pages * page_size * 100 / orig_data_size; |
| 803 | |
| 804 | // Report compressed size in megabytes. 100 MB or less has little impact. |
| 805 | SendSample("Platform.ZramCompressedSize", compr_data_size_mb, 100, 4000, 50); |
| 806 | SendSample("Platform.ZramSavings", savings_mb, 100, 4000, 50); |
| 807 | // The compression ratio is multiplied by 100 for better resolution. The |
| 808 | // ratios of interest are between 1 and 6 (100% and 600% as reported). We |
| 809 | // don't want samples when very little memory is being compressed. |
| 810 | if (compr_data_size_mb >= 1) { |
| 811 | SendSample("Platform.ZramCompressionRatioPercent", |
| 812 | orig_data_size * 100 / compr_data_size, 100, 600, 50); |
| 813 | } |
| 814 | // The values of interest for zero_pages are between 1MB and 1GB. The units |
| 815 | // are number of pages. |
| 816 | SendSample("Platform.ZramZeroPages", zero_pages, 256, 256 * 1024, 50); |
| 817 | SendSample("Platform.ZramZeroRatioPercent", zero_ratio_percent, 1, 50, 50); |
| 818 | |
| 819 | return true; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 822 | bool MetricsDaemon::ProcessMeminfo(const string& meminfo_raw) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 823 | static const MeminfoRecord fields_array[] = { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 824 | { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory |
| 825 | { "MemFree", "MemFree" }, |
| 826 | { "Buffers", "Buffers" }, |
| 827 | { "Cached", "Cached" }, |
| 828 | // { "SwapCached", "SwapCached" }, |
| 829 | { "Active", "Active" }, |
| 830 | { "Inactive", "Inactive" }, |
| 831 | { "ActiveAnon", "Active(anon)" }, |
| 832 | { "InactiveAnon", "Inactive(anon)" }, |
| 833 | { "ActiveFile" , "Active(file)" }, |
| 834 | { "InactiveFile", "Inactive(file)" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 835 | { "Unevictable", "Unevictable", kMeminfoOp_HistLog }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 836 | // { "Mlocked", "Mlocked" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 837 | { "SwapTotal", "SwapTotal", kMeminfoOp_SwapTotal }, |
| 838 | { "SwapFree", "SwapFree", kMeminfoOp_SwapFree }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 839 | // { "Dirty", "Dirty" }, |
| 840 | // { "Writeback", "Writeback" }, |
| 841 | { "AnonPages", "AnonPages" }, |
| 842 | { "Mapped", "Mapped" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 843 | { "Shmem", "Shmem", kMeminfoOp_HistLog }, |
| 844 | { "Slab", "Slab", kMeminfoOp_HistLog }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 845 | // { "SReclaimable", "SReclaimable" }, |
| 846 | // { "SUnreclaim", "SUnreclaim" }, |
| 847 | }; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 848 | vector<MeminfoRecord> fields(fields_array, |
| 849 | fields_array + arraysize(fields_array)); |
| 850 | if (!FillMeminfo(meminfo_raw, &fields)) { |
| 851 | return false; |
| 852 | } |
| 853 | int total_memory = fields[0].value; |
| 854 | if (total_memory == 0) { |
| 855 | // this "cannot happen" |
| 856 | LOG(WARNING) << "borked meminfo parser"; |
| 857 | return false; |
| 858 | } |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 859 | int swap_total = 0; |
| 860 | int swap_free = 0; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 861 | // Send all fields retrieved, except total memory. |
| 862 | for (unsigned int i = 1; i < fields.size(); i++) { |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 863 | string metrics_name = base::StringPrintf("Platform.Meminfo%s", |
| 864 | fields[i].name); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 865 | int percent; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 866 | switch (fields[i].op) { |
| 867 | case kMeminfoOp_HistPercent: |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 868 | // report value as percent of total memory |
| 869 | percent = fields[i].value * 100 / total_memory; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 870 | SendLinearSample(metrics_name, percent, 100, 101); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 871 | break; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 872 | case kMeminfoOp_HistLog: |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 873 | // report value in kbytes, log scale, 4Gb max |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 874 | SendSample(metrics_name, fields[i].value, 1, 4 * 1000 * 1000, 100); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 875 | break; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 876 | case kMeminfoOp_SwapTotal: |
| 877 | swap_total = fields[i].value; |
| 878 | case kMeminfoOp_SwapFree: |
| 879 | swap_free = fields[i].value; |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 880 | break; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 881 | } |
| 882 | } |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 883 | if (swap_total > 0) { |
| 884 | int swap_used = swap_total - swap_free; |
| 885 | int swap_used_percent = swap_used * 100 / swap_total; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 886 | SendSample("Platform.MeminfoSwapUsed", swap_used, 1, 8 * 1000 * 1000, 100); |
| 887 | SendLinearSample("Platform.MeminfoSwapUsedPercent", swap_used_percent, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 888 | 100, 101); |
| 889 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 890 | return true; |
| 891 | } |
| 892 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 893 | bool MetricsDaemon::FillMeminfo(const string& meminfo_raw, |
| 894 | vector<MeminfoRecord>* fields) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 895 | vector<string> lines; |
| 896 | unsigned int nlines = Tokenize(meminfo_raw, "\n", &lines); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 897 | |
| 898 | // Scan meminfo output and collect field values. Each field name has to |
| 899 | // match a meminfo entry (case insensitive) after removing non-alpha |
| 900 | // characters from the entry. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 901 | unsigned int ifield = 0; |
| 902 | for (unsigned int iline = 0; |
| 903 | iline < nlines && ifield < fields->size(); |
| 904 | iline++) { |
| 905 | vector<string> tokens; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 906 | Tokenize(lines[iline], ": ", &tokens); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 907 | if (strcmp((*fields)[ifield].match, tokens[0].c_str()) == 0) { |
| 908 | // Name matches. Parse value and save. |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 909 | char* rest; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 910 | (*fields)[ifield].value = |
| 911 | static_cast<int>(strtol(tokens[1].c_str(), &rest, 10)); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 912 | if (*rest != '\0') { |
| 913 | LOG(WARNING) << "missing meminfo value"; |
| 914 | return false; |
| 915 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 916 | ifield++; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 917 | } |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 918 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 919 | if (ifield < fields->size()) { |
| 920 | // End of input reached while scanning. |
| 921 | LOG(WARNING) << "cannot find field " << (*fields)[ifield].match |
| 922 | << " and following"; |
| 923 | return false; |
| 924 | } |
| 925 | return true; |
| 926 | } |
| 927 | |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 928 | void MetricsDaemon::ScheduleMemuseCallback(double interval) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 929 | if (testing_) { |
| 930 | return; |
| 931 | } |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 932 | g_timeout_add_seconds(interval, MemuseCallbackStatic, this); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // static |
| 936 | gboolean MetricsDaemon::MemuseCallbackStatic(void* handle) { |
| 937 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); |
| 938 | daemon->MemuseCallback(); |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | void MetricsDaemon::MemuseCallback() { |
| 943 | // Since we only care about active time (i.e. uptime minus sleep time) but |
| 944 | // the callbacks are driven by real time (uptime), we check if we should |
| 945 | // reschedule this callback due to intervening sleep periods. |
| 946 | double now = GetActiveTime(); |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 947 | // Avoid intervals of less than one second. |
| 948 | double remaining_time = ceil(memuse_final_time_ - now); |
| 949 | if (remaining_time > 0) { |
| 950 | ScheduleMemuseCallback(remaining_time); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 951 | } else { |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 952 | // Report stats and advance the measurement interval unless there are |
| 953 | // errors or we've completed the last interval. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 954 | if (MemuseCallbackWork() && |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 955 | memuse_interval_index_ < arraysize(kMemuseIntervals)) { |
| 956 | double interval = kMemuseIntervals[memuse_interval_index_++]; |
| 957 | memuse_final_time_ = now + interval; |
| 958 | ScheduleMemuseCallback(interval); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 963 | bool MetricsDaemon::MemuseCallbackWork() { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 964 | string meminfo_raw; |
| 965 | const FilePath meminfo_path("/proc/meminfo"); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 966 | if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 967 | LOG(WARNING) << "cannot read " << meminfo_path.value().c_str(); |
| 968 | return false; |
| 969 | } |
| 970 | return ProcessMemuse(meminfo_raw); |
| 971 | } |
| 972 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 973 | bool MetricsDaemon::ProcessMemuse(const string& meminfo_raw) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 974 | static const MeminfoRecord fields_array[] = { |
| 975 | { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory |
| 976 | { "ActiveAnon", "Active(anon)" }, |
| 977 | { "InactiveAnon", "Inactive(anon)" }, |
| 978 | }; |
| 979 | vector<MeminfoRecord> fields(fields_array, |
| 980 | fields_array + arraysize(fields_array)); |
| 981 | if (!FillMeminfo(meminfo_raw, &fields)) { |
| 982 | return false; |
| 983 | } |
| 984 | int total = fields[0].value; |
| 985 | int active_anon = fields[1].value; |
| 986 | int inactive_anon = fields[2].value; |
| 987 | if (total == 0) { |
| 988 | // this "cannot happen" |
| 989 | LOG(WARNING) << "borked meminfo parser"; |
| 990 | return false; |
| 991 | } |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 992 | string metrics_name = base::StringPrintf("Platform.MemuseAnon%d", |
| 993 | memuse_interval_index_); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 994 | SendLinearSample(metrics_name, (active_anon + inactive_anon) * 100 / total, |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 995 | 100, 101); |
| 996 | return true; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Luigi Semenzato | 5ef2e39 | 2014-04-15 15:15:02 -0700 | [diff] [blame] | 999 | void MetricsDaemon::ReportDailyUse(int use_seconds) { |
| 1000 | if (use_seconds <= 0) |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 1001 | return; |
| 1002 | |
Luigi Semenzato | 5ef2e39 | 2014-04-15 15:15:02 -0700 | [diff] [blame] | 1003 | int minutes = (use_seconds + kSecondsPerMinute / 2) / kSecondsPerMinute; |
| 1004 | SendSample("Logging.DailyUseTime", |
| 1005 | minutes, |
| 1006 | 1, |
| 1007 | kMinutesPerDay * 30 * 2, // cumulative---two months worth |
| 1008 | 50); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1011 | void MetricsDaemon::SendSample(const string& name, int sample, |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 1012 | int min, int max, int nbuckets) { |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 1013 | metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1014 | } |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 1015 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1016 | void MetricsDaemon::SendKernelCrashesCumulativeCountStats() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1017 | // Report the number of crashes for this OS version, but don't clear the |
| 1018 | // counter. It is cleared elsewhere on version change. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 1019 | int64_t crashes_count = kernel_crashes_version_count_->Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1020 | SendSample(kernel_crashes_version_count_->Name(), |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1021 | crashes_count, |
| 1022 | 1, // value of first bucket |
| 1023 | 500, // value of last bucket |
| 1024 | 100); // number of buckets |
| 1025 | |
| 1026 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 1027 | int64_t cpu_use_ms = version_cumulative_cpu_use_->Get(); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1028 | SendSample(version_cumulative_cpu_use_->Name(), |
| 1029 | cpu_use_ms / 1000, // stat is in seconds |
| 1030 | 1, // device may be used very little... |
| 1031 | 8 * 1000 * 1000, // ... or a lot (a little over 90 days) |
| 1032 | 100); |
| 1033 | |
| 1034 | // On the first run after an autoupdate, cpu_use_ms and active_use_seconds |
| 1035 | // can be zero. Avoid division by zero. |
| 1036 | if (cpu_use_ms > 0) { |
| 1037 | // Send the crash frequency since update in number of crashes per CPU year. |
| 1038 | SendSample("Logging.KernelCrashesPerCpuYear", |
| 1039 | crashes_count * kSecondsPerDay * 365 * 1000 / cpu_use_ms, |
| 1040 | 1, |
| 1041 | 1000 * 1000, // about one crash every 30s of CPU time |
| 1042 | 100); |
| 1043 | } |
| 1044 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 1045 | int64_t active_use_seconds = version_cumulative_active_use_->Get(); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1046 | if (active_use_seconds > 0) { |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1047 | SendSample(version_cumulative_active_use_->Name(), |
| 1048 | active_use_seconds / 1000, // stat is in seconds |
| 1049 | 1, // device may be used very little... |
| 1050 | 8 * 1000 * 1000, // ... or a lot (about 90 days) |
| 1051 | 100); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1052 | // Same as above, but per year of active time. |
| 1053 | SendSample("Logging.KernelCrashesPerActiveYear", |
| 1054 | crashes_count * kSecondsPerDay * 365 / active_use_seconds, |
| 1055 | 1, |
| 1056 | 1000 * 1000, // about one crash every 30s of active time |
| 1057 | 100); |
| 1058 | } |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1061 | void MetricsDaemon::SendDailyUseSample( |
| 1062 | const scoped_ptr<PersistentInteger>& use) { |
| 1063 | SendSample(use->Name(), |
| 1064 | use->GetAndClear(), |
| 1065 | 1, // value of first bucket |
| 1066 | kSecondsPerDay, // value of last bucket |
| 1067 | 50); // number of buckets |
| 1068 | } |
| 1069 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1070 | void MetricsDaemon::SendCrashIntervalSample( |
| 1071 | const scoped_ptr<PersistentInteger>& interval) { |
| 1072 | SendSample(interval->Name(), |
| 1073 | interval->GetAndClear(), |
| 1074 | 1, // value of first bucket |
| 1075 | 4 * kSecondsPerWeek, // value of last bucket |
| 1076 | 50); // number of buckets |
| 1077 | } |
| 1078 | |
| 1079 | void MetricsDaemon::SendCrashFrequencySample( |
| 1080 | const scoped_ptr<PersistentInteger>& frequency) { |
| 1081 | SendSample(frequency->Name(), |
| 1082 | frequency->GetAndClear(), |
| 1083 | 1, // value of first bucket |
| 1084 | 100, // value of last bucket |
| 1085 | 50); // number of buckets |
| 1086 | } |
| 1087 | |
| 1088 | void MetricsDaemon::SendLinearSample(const string& name, int sample, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 1089 | int max, int nbuckets) { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 1090 | // TODO(semenzato): add a proper linear histogram to the Chrome external |
| 1091 | // metrics API. |
| 1092 | LOG_IF(FATAL, nbuckets != max + 1) << "unsupported histogram scale"; |
| 1093 | metrics_lib_->SendEnumToUMA(name, sample, max); |
| 1094 | } |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1095 | |
| 1096 | void MetricsDaemon::UpdateStats(TimeTicks now_ticks, |
| 1097 | Time now_wall_time) { |
| 1098 | const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds(); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1099 | daily_active_use_->Add(elapsed_seconds); |
| 1100 | version_cumulative_active_use_->Add(elapsed_seconds); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1101 | user_crash_interval_->Add(elapsed_seconds); |
| 1102 | kernel_crash_interval_->Add(elapsed_seconds); |
| 1103 | version_cumulative_cpu_use_->Add(GetIncrementalCpuUse().InMilliseconds()); |
| 1104 | last_update_stats_time_ = now_ticks; |
| 1105 | |
| 1106 | const TimeDelta since_epoch = now_wall_time - Time::UnixEpoch(); |
| 1107 | const int day = since_epoch.InDays(); |
| 1108 | const int week = day / 7; |
| 1109 | |
| 1110 | if (daily_cycle_->Get() != day) { |
| 1111 | daily_cycle_->Set(day); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1112 | SendDailyUseSample(daily_active_use_); |
| 1113 | SendDailyUseSample(version_cumulative_active_use_); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1114 | SendCrashFrequencySample(any_crashes_daily_count_); |
| 1115 | SendCrashFrequencySample(user_crashes_daily_count_); |
| 1116 | SendCrashFrequencySample(kernel_crashes_daily_count_); |
| 1117 | SendCrashFrequencySample(unclean_shutdowns_daily_count_); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1118 | SendKernelCrashesCumulativeCountStats(); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | if (weekly_cycle_->Get() != week) { |
| 1122 | weekly_cycle_->Set(week); |
| 1123 | SendCrashFrequencySample(any_crashes_weekly_count_); |
| 1124 | SendCrashFrequencySample(user_crashes_weekly_count_); |
| 1125 | SendCrashFrequencySample(kernel_crashes_weekly_count_); |
| 1126 | SendCrashFrequencySample(unclean_shutdowns_weekly_count_); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | // static |
| 1131 | gboolean MetricsDaemon::HandleUpdateStatsTimeout(gpointer data) { |
| 1132 | static_cast<MetricsDaemon*>(data)->UpdateStats(TimeTicks::Now(), Time::Now()); |
| 1133 | return TRUE; |
| 1134 | } |