| 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 #include <map> | 
| 10 | 11 | 
|  | 12 #include <base/file_path.h> | 
| 11 #include <base/scoped_ptr.h> | 13 #include <base/scoped_ptr.h> | 
| 12 #include <base/time.h> | 14 #include <base/time.h> | 
| 13 #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 15 #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 
| 14 | 16 | 
| 15 #include "metrics_library.h" | 17 #include "metrics_library.h" | 
| 16 | 18 | 
| 17 namespace chromeos_metrics { | 19 namespace chromeos_metrics { | 
| 18 class FrequencyCounter; | 20 class FrequencyCounter; | 
| 19 class TaggedCounterInterface; | 21 class TaggedCounter; | 
|  | 22 class TaggedCounterReporter; | 
| 20 } | 23 } | 
| 21 | 24 | 
| 22 class MetricsDaemon { | 25 class MetricsDaemon { | 
| 23 | 26 | 
| 24  public: | 27  public: | 
| 25   MetricsDaemon(); | 28   MetricsDaemon(); | 
| 26   ~MetricsDaemon(); | 29   ~MetricsDaemon(); | 
| 27 | 30 | 
| 28   // Initializes. | 31   // Initializes. | 
| 29   void Init(bool testing, MetricsLibraryInterface* metrics_lib); | 32   void Init(bool testing, MetricsLibraryInterface* metrics_lib); | 
| 30 | 33 | 
| 31   // Does all the work. If |run_as_daemon| is true, daemonizes by | 34   // Does all the work. If |run_as_daemon| is true, daemonizes by | 
| 32   // forking. | 35   // forking. | 
| 33   void Run(bool run_as_daemon); | 36   void Run(bool run_as_daemon); | 
| 34 | 37 | 
| 35  private: | 38  private: | 
| 36   friend class MetricsDaemonTest; | 39   friend class MetricsDaemonTest; | 
| 37   FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash); | 40   FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash); | 
|  | 41   FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent); | 
|  | 42   FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast); | 
|  | 43   FRIEND_TEST(MetricsDaemonTest, GetHistogramPath); | 
|  | 44   FRIEND_TEST(MetricsDaemonTest, IsNewEpoch); | 
| 38   FRIEND_TEST(MetricsDaemonTest, LookupNetworkState); | 45   FRIEND_TEST(MetricsDaemonTest, LookupNetworkState); | 
| 39   FRIEND_TEST(MetricsDaemonTest, LookupPowerState); | 46   FRIEND_TEST(MetricsDaemonTest, LookupPowerState); | 
| 40   FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); | 47   FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); | 
| 41   FRIEND_TEST(MetricsDaemonTest, LookupSessionState); | 48   FRIEND_TEST(MetricsDaemonTest, LookupSessionState); | 
| 42   FRIEND_TEST(MetricsDaemonTest, MessageFilter); | 49   FRIEND_TEST(MetricsDaemonTest, MessageFilter); | 
| 43   FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop); | 50   FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop); | 
| 44   FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend); | 51   FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend); | 
| 45   FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); | 52   FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); | 
| 46   FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash); | 53   FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash); | 
| 47   FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown); | 54   FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown); | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 82   }; | 89   }; | 
| 83 | 90 | 
| 84   // Data record for aggregating daily usage. | 91   // Data record for aggregating daily usage. | 
| 85   class UseRecord { | 92   class UseRecord { | 
| 86    public: | 93    public: | 
| 87     UseRecord() : day_(0), seconds_(0) {} | 94     UseRecord() : day_(0), seconds_(0) {} | 
| 88     int day_; | 95     int day_; | 
| 89     int seconds_; | 96     int seconds_; | 
| 90   }; | 97   }; | 
| 91 | 98 | 
|  | 99   typedef std::map<std::string, chromeos_metrics::FrequencyCounter*> | 
|  | 100     FrequencyCounters; | 
|  | 101 | 
| 92   // Metric parameters. | 102   // Metric parameters. | 
| 93   static const char kMetricAnyCrashesDailyName[]; | 103   static const char kMetricAnyCrashesDailyName[]; | 
| 94   static const char kMetricCrashesDailyBuckets; | 104   static const char kMetricAnyCrashesWeeklyName[]; | 
| 95   static const char kMetricCrashesDailyMax; | 105   static const char kMetricCrashFrequencyBuckets; | 
| 96   static const char kMetricCrashesDailyMin; | 106   static const char kMetricCrashFrequencyMax; | 
|  | 107   static const char kMetricCrashFrequencyMin; | 
| 97   static const int  kMetricCrashIntervalBuckets; | 108   static const int  kMetricCrashIntervalBuckets; | 
| 98   static const int  kMetricCrashIntervalMax; | 109   static const int  kMetricCrashIntervalMax; | 
| 99   static const int  kMetricCrashIntervalMin; | 110   static const int  kMetricCrashIntervalMin; | 
| 100   static const int  kMetricDailyUseTimeBuckets; | 111   static const int  kMetricDailyUseTimeBuckets; | 
| 101   static const int  kMetricDailyUseTimeMax; | 112   static const int  kMetricDailyUseTimeMax; | 
| 102   static const int  kMetricDailyUseTimeMin; | 113   static const int  kMetricDailyUseTimeMin; | 
| 103   static const char kMetricDailyUseTimeName[]; | 114   static const char kMetricDailyUseTimeName[]; | 
| 104   static const char kMetricKernelCrashesDailyName[]; | 115   static const char kMetricKernelCrashesDailyName[]; | 
|  | 116   static const char kMetricKernelCrashesWeeklyName[]; | 
| 105   static const char kMetricKernelCrashIntervalName[]; | 117   static const char kMetricKernelCrashIntervalName[]; | 
|  | 118   static const char kMetricsPath[]; | 
| 106   static const int  kMetricTimeToNetworkDropBuckets; | 119   static const int  kMetricTimeToNetworkDropBuckets; | 
| 107   static const int  kMetricTimeToNetworkDropMax; | 120   static const int  kMetricTimeToNetworkDropMax; | 
| 108   static const int  kMetricTimeToNetworkDropMin; | 121   static const int  kMetricTimeToNetworkDropMin; | 
| 109   static const char kMetricTimeToNetworkDropName[]; | 122   static const char kMetricTimeToNetworkDropName[]; | 
| 110   static const char kMetricUncleanShutdownIntervalName[]; | 123   static const char kMetricUncleanShutdownIntervalName[]; | 
| 111   static const char kMetricUncleanShutdownsDailyName[]; | 124   static const char kMetricUncleanShutdownsDailyName[]; | 
|  | 125   static const char kMetricUncleanShutdownsWeeklyName[]; | 
| 112   static const char kMetricUserCrashesDailyName[]; | 126   static const char kMetricUserCrashesDailyName[]; | 
|  | 127   static const char kMetricUserCrashesWeeklyName[]; | 
| 113   static const char kMetricUserCrashIntervalName[]; | 128   static const char kMetricUserCrashIntervalName[]; | 
| 114 | 129 | 
| 115   // D-Bus message match strings. | 130   // D-Bus message match strings. | 
| 116   static const char* kDBusMatches_[]; | 131   static const char* kDBusMatches_[]; | 
| 117 | 132 | 
| 118   // Array of network states. | 133   // Array of network states. | 
| 119   static const char* kNetworkStates_[kNumberNetworkStates]; | 134   static const char* kNetworkStates_[kNumberNetworkStates]; | 
| 120 | 135 | 
| 121   // Array of power states. | 136   // Array of power states. | 
| 122   static const char* kPowerStates_[kNumberPowerStates]; | 137   static const char* kPowerStates_[kNumberPowerStates]; | 
| 123 | 138 | 
| 124   // Array of user session states. | 139   // Array of user session states. | 
| 125   static const char* kSessionStates_[kNumberSessionStates]; | 140   static const char* kSessionStates_[kNumberSessionStates]; | 
| 126 | 141 | 
|  | 142   // Clears and deletes the data contained in frequency_counters_. | 
|  | 143   void DeleteFrequencyCounters(); | 
|  | 144 | 
|  | 145   // Configures the given crash interval reporter. | 
|  | 146   void ConfigureCrashIntervalReporter( | 
|  | 147       const char* histogram_name, | 
|  | 148       scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter); | 
|  | 149 | 
|  | 150   // Configures the given frequency counter reporter. | 
|  | 151   void ConfigureCrashFrequencyReporter(const char* histogram_name); | 
|  | 152 | 
|  | 153   // Returns file path to persistent file for generating given histogram. | 
|  | 154   FilePath GetHistogramPath(const char* histogram_name); | 
|  | 155 | 
| 127   // Creates the event loop and enters it. | 156   // Creates the event loop and enters it. | 
| 128   void Loop(); | 157   void Loop(); | 
| 129 | 158 | 
| 130   // D-Bus filter callback. | 159   // D-Bus filter callback. | 
| 131   static DBusHandlerResult MessageFilter(DBusConnection* connection, | 160   static DBusHandlerResult MessageFilter(DBusConnection* connection, | 
| 132                                          DBusMessage* message, | 161                                          DBusMessage* message, | 
| 133                                          void* user_data); | 162                                          void* user_data); | 
| 134 | 163 | 
| 135   // Processes network state change. | 164   // Processes network state change. | 
| 136   void NetStateChanged(const char* state_name, base::TimeTicks ticks); | 165   void NetStateChanged(const char* state_name, base::TimeTicks ticks); | 
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 196   // |backoff| is false, unschedules it first. Doesn't schedule a | 225   // |backoff| is false, unschedules it first. Doesn't schedule a | 
| 197   // monitor for more than kUseMonitorIntervalMax seconds in the | 226   // monitor for more than kUseMonitorIntervalMax seconds in the | 
| 198   // future (see metrics_daemon.cc). Returns true if a new use monitor | 227   // future (see metrics_daemon.cc). Returns true if a new use monitor | 
| 199   // was scheduled, false otherwise (note that if |backoff| is false a | 228   // was scheduled, false otherwise (note that if |backoff| is false a | 
| 200   // new use monitor will always be scheduled). | 229   // new use monitor will always be scheduled). | 
| 201   bool ScheduleUseMonitor(int interval, bool backoff); | 230   bool ScheduleUseMonitor(int interval, bool backoff); | 
| 202 | 231 | 
| 203   // Unschedules a scheduled use monitor, if any. | 232   // Unschedules a scheduled use monitor, if any. | 
| 204   void UnscheduleUseMonitor(); | 233   void UnscheduleUseMonitor(); | 
| 205 | 234 | 
|  | 235   // Report daily use through UMA. | 
|  | 236   static void ReportDailyUse(void* handle, int tag, int count); | 
|  | 237 | 
| 206   // Sends a regular (exponential) histogram sample to Chrome for | 238   // Sends a regular (exponential) histogram sample to Chrome for | 
| 207   // transport to UMA. See MetricsLibrary::SendToUMA in | 239   // transport to UMA. See MetricsLibrary::SendToUMA in | 
| 208   // metrics_library.h for a description of the arguments. | 240   // metrics_library.h for a description of the arguments. | 
| 209   void SendMetric(const std::string& name, int sample, | 241   void SendMetric(const std::string& name, int sample, | 
| 210                   int min, int max, int nbuckets); | 242                   int min, int max, int nbuckets); | 
| 211 | 243 | 
| 212   // TaggedCounter callback to process aggregated daily usage data and |  | 
| 213   // send to UMA. |  | 
| 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); |  | 
| 219 |  | 
| 220   // TaggedCounter callback to process time between user-space process |  | 
| 221   // crashes and send to UMA. |  | 
| 222   static void ReportUserCrashInterval(void* data, int tag, int count); |  | 
| 223 |  | 
| 224   // TaggedCounter callback to process time between kernel crashes and |  | 
| 225   // send to UMA. |  | 
| 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); |  | 
| 247 |  | 
| 248   // Test mode. | 244   // Test mode. | 
| 249   bool testing_; | 245   bool testing_; | 
| 250 | 246 | 
| 251   // The metrics library handle. | 247   // The metrics library handle. | 
| 252   MetricsLibraryInterface* metrics_lib_; | 248   MetricsLibraryInterface* metrics_lib_; | 
| 253 | 249 | 
| 254   // Current network state. | 250   // Current network state. | 
| 255   NetworkState network_state_; | 251   NetworkState network_state_; | 
| 256 | 252 | 
| 257   // Timestamps last network state update.  This timestamp is used to | 253   // Timestamps last network state update.  This timestamp is used to | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 268   // Is the user currently active: power is on, user session has | 264   // Is the user currently active: power is on, user session has | 
| 269   // started, screen is not locked. | 265   // started, screen is not locked. | 
| 270   bool user_active_; | 266   bool user_active_; | 
| 271 | 267 | 
| 272   // Timestamps last user active update. Active use time is aggregated | 268   // Timestamps last user active update. Active use time is aggregated | 
| 273   // each day before sending to UMA so using time since the epoch as | 269   // each day before sending to UMA so using time since the epoch as | 
| 274   // the timestamp. | 270   // the timestamp. | 
| 275   base::Time user_active_last_; | 271   base::Time user_active_last_; | 
| 276 | 272 | 
| 277   // Daily active use time in seconds. | 273   // Daily active use time in seconds. | 
| 278   scoped_ptr<chromeos_metrics::TaggedCounterInterface> daily_use_; | 274   scoped_ptr<chromeos_metrics::TaggedCounter> daily_use_; | 
| 279 | 275 | 
| 280   // Active use time between user-space process crashes. | 276   // Active use time between user-space process crashes. | 
| 281   scoped_ptr<chromeos_metrics::TaggedCounterInterface> user_crash_interval_; | 277   scoped_ptr<chromeos_metrics::TaggedCounterReporter> user_crash_interval_; | 
| 282 | 278 | 
| 283   // Active use time between kernel crashes. | 279   // Active use time between kernel crashes. | 
| 284   scoped_ptr<chromeos_metrics::TaggedCounterInterface> kernel_crash_interval_; | 280   scoped_ptr<chromeos_metrics::TaggedCounterReporter> kernel_crash_interval_; | 
| 285 | 281 | 
| 286   // Active use time between unclean shutdowns crashes. | 282   // Active use time between unclean shutdowns crashes. | 
| 287   scoped_ptr<chromeos_metrics::TaggedCounterInterface> | 283   scoped_ptr<chromeos_metrics::TaggedCounterReporter> | 
| 288       unclean_shutdown_interval_; | 284       unclean_shutdown_interval_; | 
| 289 | 285 | 
| 290   // Daily count of user-space process crashes. | 286   // Map of all frequency counters, to simplify flushing them. | 
| 291   scoped_ptr<chromeos_metrics::FrequencyCounter> user_crashes_daily_; | 287   FrequencyCounters frequency_counters_; | 
| 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 | 288 | 
| 303   // Sleep period until the next daily usage aggregation performed by | 289   // Sleep period until the next daily usage aggregation performed by | 
| 304   // the daily use monitor (see ScheduleUseMonitor). | 290   // the daily use monitor (see ScheduleUseMonitor). | 
| 305   int usemon_interval_; | 291   int usemon_interval_; | 
| 306 | 292 | 
| 307   // Scheduled daily use monitor source (see ScheduleUseMonitor). | 293   // Scheduled daily use monitor source (see ScheduleUseMonitor). | 
| 308   GSource* usemon_source_; | 294   GSource* usemon_source_; | 
| 309 }; | 295 }; | 
| 310 | 296 | 
| 311 #endif  // METRICS_DAEMON_H_ | 297 #endif  // METRICS_DAEMON_H_ | 
| OLD | NEW | 
|---|