| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef METRICS_DAEMON_H_ | 5 #ifndef METRICS_DAEMON_H_ |
| 6 #define METRICS_DAEMON_H_ | 6 #define METRICS_DAEMON_H_ |
| 7 | 7 |
| 8 #include <dbus/dbus.h> | 8 #include <dbus/dbus.h> |
| 9 #include <glib.h> | 9 #include <glib.h> |
| 10 | 10 |
| 11 #include <base/scoped_ptr.h> | 11 #include <base/scoped_ptr.h> |
| 12 #include <base/time.h> | 12 #include <base/time.h> |
| 13 #include <gtest/gtest_prod.h> // for FRIEND_TEST | 13 #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 14 | 14 |
| 15 #include "metrics_library.h" | 15 #include "metrics_library.h" |
| 16 | 16 |
| 17 namespace chromeos_metrics { class TaggedCounterInterface; } | 17 namespace chromeos_metrics { |
| 18 class FrequencyCounter; |
| 19 class TaggedCounterInterface; |
| 20 } |
| 18 | 21 |
| 19 class MetricsDaemon { | 22 class MetricsDaemon { |
| 20 | 23 |
| 21 public: | 24 public: |
| 22 MetricsDaemon(); | 25 MetricsDaemon(); |
| 23 ~MetricsDaemon(); | 26 ~MetricsDaemon(); |
| 24 | 27 |
| 25 // Initializes. | 28 // Initializes. |
| 26 void Init(bool testing, MetricsLibraryInterface* metrics_lib); | 29 void Init(bool testing, MetricsLibraryInterface* metrics_lib); |
| 27 | 30 |
| 28 // Does all the work. If |run_as_daemon| is true, daemonizes by | 31 // Does all the work. If |run_as_daemon| is true, daemonizes by |
| 29 // forking. | 32 // forking. |
| 30 void Run(bool run_as_daemon); | 33 void Run(bool run_as_daemon); |
| 31 | 34 |
| 32 private: | 35 private: |
| 33 friend class MetricsDaemonTest; | 36 friend class MetricsDaemonTest; |
| 34 FRIEND_TEST(MetricsDaemonTest, CheckKernelCrash); | 37 FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash); |
| 35 FRIEND_TEST(MetricsDaemonTest, DailyUseReporter); | |
| 36 FRIEND_TEST(MetricsDaemonTest, KernelCrashIntervalReporter); | |
| 37 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState); | 38 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState); |
| 38 FRIEND_TEST(MetricsDaemonTest, LookupPowerState); | 39 FRIEND_TEST(MetricsDaemonTest, LookupPowerState); |
| 39 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); | 40 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); |
| 40 FRIEND_TEST(MetricsDaemonTest, LookupSessionState); | 41 FRIEND_TEST(MetricsDaemonTest, LookupSessionState); |
| 41 FRIEND_TEST(MetricsDaemonTest, MessageFilter); | 42 FRIEND_TEST(MetricsDaemonTest, MessageFilter); |
| 42 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop); | 43 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop); |
| 43 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend); | 44 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend); |
| 44 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); | 45 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); |
| 45 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash); | 46 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash); |
| 47 FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown); |
| 46 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash); | 48 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash); |
| 49 FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency); |
| 50 FRIEND_TEST(MetricsDaemonTest, ReportDailyUse); |
| 51 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval); |
| 52 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval); |
| 53 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval); |
| 47 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); | 54 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); |
| 48 FRIEND_TEST(MetricsDaemonTest, SendMetric); | 55 FRIEND_TEST(MetricsDaemonTest, SendMetric); |
| 49 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); | 56 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); |
| 50 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState); | 57 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState); |
| 51 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump); | 58 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump); |
| 52 FRIEND_TEST(MetricsDaemonTest, UserCrashIntervalReporter); | |
| 53 | 59 |
| 54 // The network states (see network_states.h). | 60 // The network states (see network_states.h). |
| 55 enum NetworkState { | 61 enum NetworkState { |
| 56 kUnknownNetworkState = -1, // Initial/unknown network state. | 62 kUnknownNetworkState = -1, // Initial/unknown network state. |
| 57 #define STATE(name, capname) kNetworkState ## capname, | 63 #define STATE(name, capname) kNetworkState ## capname, |
| 58 #include "network_states.h" | 64 #include "network_states.h" |
| 59 kNumberNetworkStates | 65 kNumberNetworkStates |
| 60 }; | 66 }; |
| 61 | 67 |
| 62 // The power states (see power_states.h). | 68 // The power states (see power_states.h). |
| (...skipping 14 matching lines...) Expand all Loading... |
| 77 | 83 |
| 78 // Data record for aggregating daily usage. | 84 // Data record for aggregating daily usage. |
| 79 class UseRecord { | 85 class UseRecord { |
| 80 public: | 86 public: |
| 81 UseRecord() : day_(0), seconds_(0) {} | 87 UseRecord() : day_(0), seconds_(0) {} |
| 82 int day_; | 88 int day_; |
| 83 int seconds_; | 89 int seconds_; |
| 84 }; | 90 }; |
| 85 | 91 |
| 86 // Metric parameters. | 92 // Metric parameters. |
| 93 static const char kMetricAnyCrashesDailyName[]; |
| 94 static const char kMetricCrashesDailyBuckets; |
| 95 static const char kMetricCrashesDailyMax; |
| 96 static const char kMetricCrashesDailyMin; |
| 97 static const int kMetricCrashIntervalBuckets; |
| 98 static const int kMetricCrashIntervalMax; |
| 99 static const int kMetricCrashIntervalMin; |
| 100 static const int kMetricDailyUseTimeBuckets; |
| 101 static const int kMetricDailyUseTimeMax; |
| 102 static const int kMetricDailyUseTimeMin; |
| 87 static const char kMetricDailyUseTimeName[]; | 103 static const char kMetricDailyUseTimeName[]; |
| 88 static const int kMetricDailyUseTimeMin; | 104 static const char kMetricKernelCrashesDailyName[]; |
| 89 static const int kMetricDailyUseTimeMax; | |
| 90 static const int kMetricDailyUseTimeBuckets; | |
| 91 static const char kMetricKernelCrashIntervalName[]; | 105 static const char kMetricKernelCrashIntervalName[]; |
| 92 static const int kMetricKernelCrashIntervalMin; | 106 static const int kMetricTimeToNetworkDropBuckets; |
| 93 static const int kMetricKernelCrashIntervalMax; | 107 static const int kMetricTimeToNetworkDropMax; |
| 94 static const int kMetricKernelCrashIntervalBuckets; | 108 static const int kMetricTimeToNetworkDropMin; |
| 95 static const char kMetricTimeToNetworkDropName[]; | 109 static const char kMetricTimeToNetworkDropName[]; |
| 96 static const int kMetricTimeToNetworkDropMin; | 110 static const char kMetricUncleanShutdownIntervalName[]; |
| 97 static const int kMetricTimeToNetworkDropMax; | 111 static const char kMetricUncleanShutdownsDailyName[]; |
| 98 static const int kMetricTimeToNetworkDropBuckets; | 112 static const char kMetricUserCrashesDailyName[]; |
| 99 static const char kMetricUserCrashIntervalName[]; | 113 static const char kMetricUserCrashIntervalName[]; |
| 100 static const int kMetricUserCrashIntervalMin; | |
| 101 static const int kMetricUserCrashIntervalMax; | |
| 102 static const int kMetricUserCrashIntervalBuckets; | |
| 103 | 114 |
| 104 // D-Bus message match strings. | 115 // D-Bus message match strings. |
| 105 static const char* kDBusMatches_[]; | 116 static const char* kDBusMatches_[]; |
| 106 | 117 |
| 107 // Array of network states. | 118 // Array of network states. |
| 108 static const char* kNetworkStates_[kNumberNetworkStates]; | 119 static const char* kNetworkStates_[kNumberNetworkStates]; |
| 109 | 120 |
| 110 // Array of power states. | 121 // Array of power states. |
| 111 static const char* kPowerStates_[kNumberPowerStates]; | 122 static const char* kPowerStates_[kNumberPowerStates]; |
| 112 | 123 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // the usage file, the |seconds| are accumulated. | 166 // the usage file, the |seconds| are accumulated. |
| 156 void LogDailyUseRecord(int day, int seconds); | 167 void LogDailyUseRecord(int day, int seconds); |
| 157 | 168 |
| 158 // Updates the active use time and logs time between user-space | 169 // Updates the active use time and logs time between user-space |
| 159 // process crashes. | 170 // process crashes. |
| 160 void ProcessUserCrash(); | 171 void ProcessUserCrash(); |
| 161 | 172 |
| 162 // Updates the active use time and logs time between kernel crashes. | 173 // Updates the active use time and logs time between kernel crashes. |
| 163 void ProcessKernelCrash(); | 174 void ProcessKernelCrash(); |
| 164 | 175 |
| 165 // Checks if a kernel crash has been detected and processes if so. | 176 // Updates the active use time and logs time between unclean shutdowns. |
| 166 // The method assumes that a kernel crash has happened if | 177 void ProcessUncleanShutdown(); |
| 167 // |crash_file| exists. | 178 |
| 168 void CheckKernelCrash(const std::string& crash_file); | 179 // Checks if a kernel crash has been detected and returns true if |
| 180 // so. The method assumes that a kernel crash has happened if |
| 181 // |crash_file| exists. It removes the file immediately if it |
| 182 // exists, so it must not be called more than once. |
| 183 bool CheckSystemCrash(const std::string& crash_file); |
| 169 | 184 |
| 170 // Callbacks for the daily use monitor. The daily use monitor uses | 185 // Callbacks for the daily use monitor. The daily use monitor uses |
| 171 // LogDailyUseRecord to aggregate current usage data and send it to | 186 // LogDailyUseRecord to aggregate current usage data and send it to |
| 172 // UMA, if necessary. It also reschedules itself using an | 187 // UMA, if necessary. It also reschedules itself using an |
| 173 // exponentially bigger interval (up to a certain maximum) -- so | 188 // exponentially bigger interval (up to a certain maximum) -- so |
| 174 // usage is monitored less frequently with longer active use. | 189 // usage is monitored less frequently with longer active use. |
| 175 static gboolean UseMonitorStatic(gpointer data); | 190 static gboolean UseMonitorStatic(gpointer data); |
| 176 bool UseMonitor(); | 191 bool UseMonitor(); |
| 177 | 192 |
| 178 // Schedules or reschedules a daily use monitor for |interval| | 193 // Schedules or reschedules a daily use monitor for |interval| |
| (...skipping 10 matching lines...) Expand all Loading... |
| 189 void UnscheduleUseMonitor(); | 204 void UnscheduleUseMonitor(); |
| 190 | 205 |
| 191 // Sends a regular (exponential) histogram sample to Chrome for | 206 // Sends a regular (exponential) histogram sample to Chrome for |
| 192 // transport to UMA. See MetricsLibrary::SendToUMA in | 207 // transport to UMA. See MetricsLibrary::SendToUMA in |
| 193 // metrics_library.h for a description of the arguments. | 208 // metrics_library.h for a description of the arguments. |
| 194 void SendMetric(const std::string& name, int sample, | 209 void SendMetric(const std::string& name, int sample, |
| 195 int min, int max, int nbuckets); | 210 int min, int max, int nbuckets); |
| 196 | 211 |
| 197 // TaggedCounter callback to process aggregated daily usage data and | 212 // TaggedCounter callback to process aggregated daily usage data and |
| 198 // send to UMA. | 213 // send to UMA. |
| 199 static void DailyUseReporter(void* data, int tag, int count); | 214 static void ReportDailyUse(void* data, int tag, int count); |
| 215 |
| 216 // Helper to report a crash interval to UMA. |
| 217 static void ReportCrashInterval(const char* histogram_name, |
| 218 void* handle, int count); |
| 200 | 219 |
| 201 // TaggedCounter callback to process time between user-space process | 220 // TaggedCounter callback to process time between user-space process |
| 202 // crashes and send to UMA. | 221 // crashes and send to UMA. |
| 203 static void UserCrashIntervalReporter(void* data, int tag, int count); | 222 static void ReportUserCrashInterval(void* data, int tag, int count); |
| 204 | 223 |
| 205 // TaggedCounter callback to process time between kernel crashes and | 224 // TaggedCounter callback to process time between kernel crashes and |
| 206 // send to UMA. | 225 // send to UMA. |
| 207 static void KernelCrashIntervalReporter(void* data, int tag, int count); | 226 static void ReportKernelCrashInterval(void* data, int tag, int count); |
| 227 |
| 228 // TaggedCounter callback to process time between unclean shutdowns and |
| 229 // send to UMA. |
| 230 static void ReportUncleanShutdownInterval(void* data, int tag, int count); |
| 231 |
| 232 // Helper to report a daily crash frequency to UMA. |
| 233 static void ReportCrashesDailyFrequency(const char* histogram_name, |
| 234 void* handle, int count); |
| 235 |
| 236 // TaggedCounter callback to report daily crash frequency to UMA. |
| 237 static void ReportUserCrashesDaily(void* handle, int tag, int count); |
| 238 |
| 239 // TaggedCounter callback to report kernel crash frequency to UMA. |
| 240 static void ReportKernelCrashesDaily(void* handle, int tag, int count); |
| 241 |
| 242 // TaggedCounter callback to report unclean shutdown frequency to UMA. |
| 243 static void ReportUncleanShutdownsDaily(void* handle, int tag, int count); |
| 244 |
| 245 // TaggedCounter callback to report frequency of any crashes to UMA. |
| 246 static void ReportAnyCrashesDaily(void* handle, int tag, int count); |
| 208 | 247 |
| 209 // Test mode. | 248 // Test mode. |
| 210 bool testing_; | 249 bool testing_; |
| 211 | 250 |
| 212 // The metrics library handle. | 251 // The metrics library handle. |
| 213 MetricsLibraryInterface* metrics_lib_; | 252 MetricsLibraryInterface* metrics_lib_; |
| 214 | 253 |
| 215 // Current network state. | 254 // Current network state. |
| 216 NetworkState network_state_; | 255 NetworkState network_state_; |
| 217 | 256 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 237 | 276 |
| 238 // Daily active use time in seconds. | 277 // Daily active use time in seconds. |
| 239 scoped_ptr<chromeos_metrics::TaggedCounterInterface> daily_use_; | 278 scoped_ptr<chromeos_metrics::TaggedCounterInterface> daily_use_; |
| 240 | 279 |
| 241 // Active use time between user-space process crashes. | 280 // Active use time between user-space process crashes. |
| 242 scoped_ptr<chromeos_metrics::TaggedCounterInterface> user_crash_interval_; | 281 scoped_ptr<chromeos_metrics::TaggedCounterInterface> user_crash_interval_; |
| 243 | 282 |
| 244 // Active use time between kernel crashes. | 283 // Active use time between kernel crashes. |
| 245 scoped_ptr<chromeos_metrics::TaggedCounterInterface> kernel_crash_interval_; | 284 scoped_ptr<chromeos_metrics::TaggedCounterInterface> kernel_crash_interval_; |
| 246 | 285 |
| 286 // Active use time between unclean shutdowns crashes. |
| 287 scoped_ptr<chromeos_metrics::TaggedCounterInterface> |
| 288 unclean_shutdown_interval_; |
| 289 |
| 290 // Daily count of user-space process crashes. |
| 291 scoped_ptr<chromeos_metrics::FrequencyCounter> user_crashes_daily_; |
| 292 |
| 293 // Daily count of kernel crashes. |
| 294 scoped_ptr<chromeos_metrics::FrequencyCounter> kernel_crashes_daily_; |
| 295 |
| 296 // Daily count of unclean shutdowns. |
| 297 scoped_ptr<chromeos_metrics::FrequencyCounter> unclean_shutdowns_daily_; |
| 298 |
| 299 // Daily count of any crashes (user-space processes, kernel, or |
| 300 // unclean shutdowns). |
| 301 scoped_ptr<chromeos_metrics::FrequencyCounter> any_crashes_daily_; |
| 302 |
| 247 // Sleep period until the next daily usage aggregation performed by | 303 // Sleep period until the next daily usage aggregation performed by |
| 248 // the daily use monitor (see ScheduleUseMonitor). | 304 // the daily use monitor (see ScheduleUseMonitor). |
| 249 int usemon_interval_; | 305 int usemon_interval_; |
| 250 | 306 |
| 251 // Scheduled daily use monitor source (see ScheduleUseMonitor). | 307 // Scheduled daily use monitor source (see ScheduleUseMonitor). |
| 252 GSource* usemon_source_; | 308 GSource* usemon_source_; |
| 253 }; | 309 }; |
| 254 | 310 |
| 255 #endif // METRICS_DAEMON_H_ | 311 #endif // METRICS_DAEMON_H_ |
| OLD | NEW |