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 <time.h> | 10 #include <time.h> |
10 | 11 |
11 class MetricsDaemon { | 12 class MetricsDaemon { |
12 | 13 |
13 public: | 14 public: |
14 MetricsDaemon() | 15 MetricsDaemon() |
15 : testing_(false), | 16 : network_state_(kUnknownNetworkState), |
16 network_state_(kUnknownNetworkState), | 17 network_state_last_(0), |
17 network_state_changed_(0), | 18 power_state_(kUnknownPowerState), |
18 power_state_(kUnknownPowerState) {} | 19 screensaver_state_(kUnknownScreenSaverState), |
| 20 session_state_(kUnknownSessionState), |
| 21 user_active_(false), |
| 22 user_active_last_(0), |
| 23 daily_use_day_last_(0), |
| 24 usemon_interval_(0), |
| 25 usemon_source_(NULL) {} |
19 ~MetricsDaemon() {} | 26 ~MetricsDaemon() {} |
20 | 27 |
21 // Does all the work. If |run_as_daemon| is true, daemonizes by | 28 // Does all the work. If |run_as_daemon| is true, daemonizes by |
22 // forking. If |testing| is true, logs the stats instead of sending | 29 // forking. If |testing| is true, logs the stats instead of sending |
23 // them to Chrome. | 30 // them to Chrome. |
24 void Run(bool run_as_daemon, bool testing); | 31 void Run(bool run_as_daemon, bool testing); |
25 | 32 |
26 private: | 33 private: |
27 // The network states (see network_states.h). | 34 // The network states (see network_states.h). |
28 enum NetworkState { | 35 enum NetworkState { |
29 kUnknownNetworkState = -1, // Initial/unknown network state. | 36 kUnknownNetworkState = -1, // Initial/unknown network state. |
30 #define STATE(name, capname) kNetworkState ## capname, | 37 #define STATE(name, capname) kNetworkState ## capname, |
31 #include "network_states.h" | 38 #include "network_states.h" |
32 kNumberNetworkStates | 39 kNumberNetworkStates |
33 }; | 40 }; |
34 | 41 |
35 // The power states (see power_states.h). | 42 // The power states (see power_states.h). |
36 enum PowerState { | 43 enum PowerState { |
37 kUnknownPowerState = -1, // Initial/unknown power state. | 44 kUnknownPowerState = -1, // Initial/unknown power state. |
38 #define STATE(name, capname) kPowerState ## capname, | 45 #define STATE(name, capname) kPowerState ## capname, |
39 #include "power_states.h" | 46 #include "power_states.h" |
40 kNumberPowerStates | 47 kNumberPowerStates |
41 }; | 48 }; |
42 | 49 |
| 50 // The screen-saver states (see screensaver_states.h). |
| 51 enum ScreenSaverState { |
| 52 kUnknownScreenSaverState = -1, // Initial/unknown screen-saver state. |
| 53 #define STATE(name, capname) kScreenSaverState ## capname, |
| 54 #include "screensaver_states.h" |
| 55 kNumberScreenSaverStates |
| 56 }; |
| 57 |
| 58 // The user session states (see session_states.h). |
| 59 enum SessionState { |
| 60 kUnknownSessionState = -1, // Initial/unknown user session state. |
| 61 #define STATE(name, capname) kSessionState ## capname, |
| 62 #include "session_states.h" |
| 63 kNumberSessionStates |
| 64 }; |
| 65 |
| 66 // Data record for aggregating daily usage. |
| 67 class UseRecord { |
| 68 public: |
| 69 UseRecord() : day_(0), seconds_(0) {} |
| 70 int day_; |
| 71 int seconds_; |
| 72 }; |
| 73 |
| 74 // D-Bus message match strings. |
| 75 static const char* kDBusMatches_[]; |
| 76 |
| 77 // Array of network states. |
| 78 static const char* kNetworkStates_[kNumberNetworkStates]; |
| 79 |
| 80 // Array of power states. |
| 81 static const char* kPowerStates_[kNumberPowerStates]; |
| 82 |
| 83 // Array of screen-saver states. |
| 84 static const char* kScreenSaverStates_[kNumberScreenSaverStates]; |
| 85 |
| 86 // Array of user session states. |
| 87 static const char* kSessionStates_[kNumberSessionStates]; |
| 88 |
43 // Initializes. | 89 // Initializes. |
44 void Init(bool testing); | 90 void Init(bool testing); |
45 | 91 |
46 // Creates the event loop and enters it. | 92 // Creates the event loop and enters it. |
47 void Loop(); | 93 void Loop(); |
48 | 94 |
49 // D-Bus filter callback. | 95 // D-Bus filter callback. |
50 static DBusHandlerResult MessageFilter(DBusConnection* connection, | 96 static DBusHandlerResult MessageFilter(DBusConnection* connection, |
51 DBusMessage* message, | 97 DBusMessage* message, |
52 void* user_data); | 98 void* user_data); |
53 | 99 |
54 // Processes network state change. | 100 // Processes network state change. |
55 void NetStateChanged(const char* state_name); | 101 void NetStateChanged(const char* state_name, time_t now); |
56 | 102 |
57 // Given the state name, returns the state id. | 103 // Given the state name, returns the state id. |
58 NetworkState LookupNetworkState(const char* state_name); | 104 NetworkState LookupNetworkState(const char* state_name); |
59 | 105 |
60 // Processes power state change. | 106 // Processes power state change. |
61 void PowerStateChanged(const char* state_name); | 107 void PowerStateChanged(const char* state_name, time_t now); |
62 | 108 |
63 // Given the state name, returns the state id. | 109 // Given the state name, returns the state id. |
64 PowerState LookupPowerState(const char* state_name); | 110 PowerState LookupPowerState(const char* state_name); |
65 | 111 |
| 112 // Processes screen-saver state change. |
| 113 void ScreenSaverStateChanged(const char* state_name, time_t now); |
| 114 |
| 115 // Given the state name, returns the state id. |
| 116 ScreenSaverState LookupScreenSaverState(const char* state_name); |
| 117 |
| 118 // Processes user session state change. |
| 119 void SessionStateChanged(const char* state_name, time_t now); |
| 120 |
| 121 // Given the state name, returns the state id. |
| 122 SessionState LookupSessionState(const char* state_name); |
| 123 |
| 124 // Updates the user-active state to |active| and logs the usage data |
| 125 // since the last update. If the user has just become active, |
| 126 // reschedule the daily use monitor for more frequent updates -- |
| 127 // this is followed by an exponential back-off (see UseMonitor). |
| 128 void SetUserActiveState(bool active, time_t now); |
| 129 |
| 130 // Updates the daily usage file, if necessary, by adding |seconds| |
| 131 // of active use to the |day| since Epoch. If there's usage data for |
| 132 // day in the past in the usage file, that data is sent to UMA and |
| 133 // removed from the file. If there's already usage data for |day| in |
| 134 // the usage file, the |seconds| are accumulated. |
| 135 void LogDailyUseRecord(int day, int seconds); |
| 136 |
| 137 // Callbacks for the daily use monitor. The daily use monitor uses |
| 138 // LogDailyUseRecord to aggregate current usage data and send it to |
| 139 // UMA, if necessary. It also reschedules itself using an |
| 140 // exponentially bigger interval (up to a certain maximum) -- so |
| 141 // usage is monitored less frequently with longer active use. |
| 142 static gboolean UseMonitorStatic(gpointer data); |
| 143 bool UseMonitor(); |
| 144 |
| 145 // Schedules or reschedules a daily use monitor for |interval| |
| 146 // seconds from now. |backoff| mode is used by the use monitor to |
| 147 // reschedule itself. If there's a monitor scheduled already and |
| 148 // |backoff| is false, unschedules it first. Doesn't schedule a |
| 149 // monitor for more than kUseMonitorIntervalMax seconds in the |
| 150 // future (see metrics_daemon.cc). Returns true if a new use monitor |
| 151 // was scheduled, false otherwise (note that if |backoff| is false a |
| 152 // new use monitor will always be scheduled). |
| 153 bool ScheduleUseMonitor(int interval, bool backoff); |
| 154 |
| 155 // Unschedules a scheduled use monitor, if any. |
| 156 void UnscheduleUseMonitor(); |
| 157 |
66 // Sends a stat to Chrome for transport to UMA (or prints it for | 158 // Sends a stat to Chrome for transport to UMA (or prints it for |
67 // testing). See MetricsLibrary::SendToChrome in metrics_library.h | 159 // testing). See MetricsLibrary::SendToChrome in metrics_library.h |
68 // for a description of the arguments. | 160 // for a description of the arguments. |
69 void PublishMetric(const char* name, int sample, | 161 void PublishMetric(const char* name, int sample, |
70 int min, int max, int nbuckets); | 162 int min, int max, int nbuckets); |
71 | 163 |
72 // D-Bus message match strings. | 164 // Testing mode. |
73 static const char* dbus_matches_[]; | 165 bool testing_; |
74 | 166 |
75 // Array of network states. | 167 // Current network state. |
76 static const char* network_states_[kNumberNetworkStates]; | 168 NetworkState network_state_; |
77 | 169 |
78 // Array of power states. | 170 // Timestamps last network state update. |
79 static const char* power_states_[kNumberPowerStates]; | 171 time_t network_state_last_; |
80 | 172 |
81 bool testing_; // just testing | 173 // Current power state. |
82 NetworkState network_state_; // current network state | 174 PowerState power_state_; |
83 time_t network_state_changed_; // timestamp last net state change | 175 |
84 PowerState power_state_; // current power state | 176 // Current screen-saver state. |
| 177 ScreenSaverState screensaver_state_; |
| 178 |
| 179 // Current user session state. |
| 180 SessionState session_state_; |
| 181 |
| 182 // Is the user currently active: power is on, user session has |
| 183 // started, screen is not locked. |
| 184 bool user_active_; |
| 185 |
| 186 // Timestamps last user active update. |
| 187 time_t user_active_last_; |
| 188 |
| 189 // Last stored daily use day (since epoch). |
| 190 int daily_use_day_last_; |
| 191 |
| 192 // Sleep period until the next daily usage aggregation performed by |
| 193 // the daily use monitor (see ScheduleUseMonitor). |
| 194 int usemon_interval_; |
| 195 |
| 196 // Scheduled daily use monitor source (see ScheduleUseMonitor). |
| 197 GSource* usemon_source_; |
85 }; | 198 }; |
86 | 199 |
87 #endif // METRICS_DAEMON_H_ | 200 #endif // METRICS_DAEMON_H_ |
OLD | NEW |