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 #include "metrics_daemon.h" | 5 #include "metrics_daemon.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <base/file_util.h> | 9 #include <base/file_util.h> |
10 #include <base/logging.h> | 10 #include <base/logging.h> |
11 #include <dbus/dbus-glib-lowlevel.h> | 11 #include <dbus/dbus-glib-lowlevel.h> |
12 | 12 |
13 #include "counter.h" | 13 #include "counter.h" |
14 | 14 |
15 using base::Time; | 15 using base::Time; |
16 using base::TimeDelta; | 16 using base::TimeDelta; |
17 using base::TimeTicks; | 17 using base::TimeTicks; |
18 using std::string; | 18 using std::string; |
19 | 19 |
20 #define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error") | 20 #define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error") |
21 #define DBUS_IFACE_CRASH_REPORTER "org.chromium.CrashReporter" | 21 #define DBUS_IFACE_CRASH_REPORTER "org.chromium.CrashReporter" |
22 #define DBUS_IFACE_FLIMFLAM_MANAGER "org.chromium.flimflam.Manager" | |
23 #define DBUS_IFACE_POWER_MANAGER "org.chromium.PowerManager" | 22 #define DBUS_IFACE_POWER_MANAGER "org.chromium.PowerManager" |
24 #define DBUS_IFACE_SESSION_MANAGER "org.chromium.SessionManagerInterface" | 23 #define DBUS_IFACE_SESSION_MANAGER "org.chromium.SessionManagerInterface" |
25 | 24 |
26 static const int kSecondsPerMinute = 60; | 25 static const int kSecondsPerMinute = 60; |
27 static const int kMinutesPerHour = 60; | 26 static const int kMinutesPerHour = 60; |
28 static const int kHoursPerDay = 24; | 27 static const int kHoursPerDay = 24; |
29 static const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour; | 28 static const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour; |
30 static const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay; | 29 static const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay; |
31 static const int kDaysPerWeek = 7; | 30 static const int kDaysPerWeek = 7; |
32 static const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek; | 31 static const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek; |
(...skipping 11 matching lines...) Expand all Loading... |
44 static const char kUncleanShutdownDetectedFile[] = | 43 static const char kUncleanShutdownDetectedFile[] = |
45 "/tmp/unclean-shutdown-detected"; | 44 "/tmp/unclean-shutdown-detected"; |
46 | 45 |
47 // static metrics parameters | 46 // static metrics parameters |
48 const char MetricsDaemon::kMetricDailyUseTimeName[] = | 47 const char MetricsDaemon::kMetricDailyUseTimeName[] = |
49 "Logging.DailyUseTime"; | 48 "Logging.DailyUseTime"; |
50 const int MetricsDaemon::kMetricDailyUseTimeMin = 1; | 49 const int MetricsDaemon::kMetricDailyUseTimeMin = 1; |
51 const int MetricsDaemon::kMetricDailyUseTimeMax = kMinutesPerDay; | 50 const int MetricsDaemon::kMetricDailyUseTimeMax = kMinutesPerDay; |
52 const int MetricsDaemon::kMetricDailyUseTimeBuckets = 50; | 51 const int MetricsDaemon::kMetricDailyUseTimeBuckets = 50; |
53 | 52 |
54 const char MetricsDaemon::kMetricTimeToNetworkDropName[] = | |
55 "Network.TimeToDrop"; | |
56 const int MetricsDaemon::kMetricTimeToNetworkDropMin = 1; | |
57 const int MetricsDaemon::kMetricTimeToNetworkDropMax = | |
58 8 /* hours */ * kMinutesPerHour * kSecondsPerMinute; | |
59 const int MetricsDaemon::kMetricTimeToNetworkDropBuckets = 50; | |
60 | |
61 // crash interval metrics | 53 // crash interval metrics |
62 const char MetricsDaemon::kMetricKernelCrashIntervalName[] = | 54 const char MetricsDaemon::kMetricKernelCrashIntervalName[] = |
63 "Logging.KernelCrashInterval"; | 55 "Logging.KernelCrashInterval"; |
64 const char MetricsDaemon::kMetricUncleanShutdownIntervalName[] = | 56 const char MetricsDaemon::kMetricUncleanShutdownIntervalName[] = |
65 "Logging.UncleanShutdownInterval"; | 57 "Logging.UncleanShutdownInterval"; |
66 const char MetricsDaemon::kMetricUserCrashIntervalName[] = | 58 const char MetricsDaemon::kMetricUserCrashIntervalName[] = |
67 "Logging.UserCrashInterval"; | 59 "Logging.UserCrashInterval"; |
68 | 60 |
69 const int MetricsDaemon::kMetricCrashIntervalMin = 1; | 61 const int MetricsDaemon::kMetricCrashIntervalMin = 1; |
70 const int MetricsDaemon::kMetricCrashIntervalMax = | 62 const int MetricsDaemon::kMetricCrashIntervalMax = |
(...skipping 26 matching lines...) Expand all Loading... |
97 | 89 |
98 | 90 |
99 // static | 91 // static |
100 const char* MetricsDaemon::kDBusMatches_[] = { | 92 const char* MetricsDaemon::kDBusMatches_[] = { |
101 "type='signal'," | 93 "type='signal'," |
102 "interface='" DBUS_IFACE_CRASH_REPORTER "'," | 94 "interface='" DBUS_IFACE_CRASH_REPORTER "'," |
103 "path='/'," | 95 "path='/'," |
104 "member='UserCrash'", | 96 "member='UserCrash'", |
105 | 97 |
106 "type='signal'," | 98 "type='signal'," |
107 "sender='org.chromium.flimflam'," | |
108 "interface='" DBUS_IFACE_FLIMFLAM_MANAGER "'," | |
109 "path='/'," | |
110 "member='StateChanged'", | |
111 | |
112 "type='signal'," | |
113 "interface='" DBUS_IFACE_POWER_MANAGER "'," | 99 "interface='" DBUS_IFACE_POWER_MANAGER "'," |
114 "path='/'", | 100 "path='/'", |
115 | 101 |
116 "type='signal'," | 102 "type='signal'," |
117 "sender='org.chromium.SessionManager'," | 103 "sender='org.chromium.SessionManager'," |
118 "interface='" DBUS_IFACE_SESSION_MANAGER "'," | 104 "interface='" DBUS_IFACE_SESSION_MANAGER "'," |
119 "path='/org/chromium/SessionManager'," | 105 "path='/org/chromium/SessionManager'," |
120 "member='SessionStateChanged'", | 106 "member='SessionStateChanged'", |
121 }; | 107 }; |
122 | 108 |
123 // static | 109 // static |
124 const char* MetricsDaemon::kNetworkStates_[] = { | |
125 #define STATE(name, capname) #name, | |
126 #include "network_states.h" | |
127 }; | |
128 | |
129 // static | |
130 const char* MetricsDaemon::kPowerStates_[] = { | 110 const char* MetricsDaemon::kPowerStates_[] = { |
131 #define STATE(name, capname) #name, | 111 #define STATE(name, capname) #name, |
132 #include "power_states.h" | 112 #include "power_states.h" |
133 }; | 113 }; |
134 | 114 |
135 // static | 115 // static |
136 const char* MetricsDaemon::kSessionStates_[] = { | 116 const char* MetricsDaemon::kSessionStates_[] = { |
137 #define STATE(name, capname) #name, | 117 #define STATE(name, capname) #name, |
138 #include "session_states.h" | 118 #include "session_states.h" |
139 }; | 119 }; |
140 | 120 |
141 // Invokes a remote method over D-Bus that takes no input arguments | |
142 // and returns a string result. The method call is issued with a 2 | |
143 // second blocking timeout. Returns an empty string on failure or | |
144 // timeout. | |
145 static string DBusGetString(DBusConnection* connection, | |
146 const string& destination, | |
147 const string& path, | |
148 const string& interface, | |
149 const string& method) { | |
150 DBusMessage* message = | |
151 dbus_message_new_method_call(destination.c_str(), | |
152 path.c_str(), | |
153 interface.c_str(), | |
154 method.c_str()); | |
155 if (!message) { | |
156 DLOG(WARNING) << "DBusGetString: unable to allocate a message"; | |
157 return ""; | |
158 } | |
159 | |
160 DBusError error; | |
161 dbus_error_init(&error); | |
162 const int kTimeout = 2000; // ms | |
163 DLOG(INFO) << "DBusGetString: dest=" << destination << " path=" << path | |
164 << " iface=" << interface << " method=" << method; | |
165 DBusMessage* reply = | |
166 dbus_connection_send_with_reply_and_block(connection, message, kTimeout, | |
167 &error); | |
168 dbus_message_unref(message); | |
169 if (dbus_error_is_set(&error) || !reply) { | |
170 DLOG(WARNING) << "DBusGetString: call failed"; | |
171 return ""; | |
172 } | |
173 DBusMessageIter iter; | |
174 dbus_message_iter_init(reply, &iter); | |
175 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) { | |
176 NOTREACHED(); | |
177 dbus_message_unref(reply); | |
178 return ""; | |
179 } | |
180 const char* c_result = ""; | |
181 dbus_message_iter_get_basic(&iter, &c_result); | |
182 string result = c_result; | |
183 DLOG(INFO) << "DBusGetString: result=" << result; | |
184 dbus_message_unref(reply); | |
185 return result; | |
186 } | |
187 | |
188 MetricsDaemon::MetricsDaemon() | 121 MetricsDaemon::MetricsDaemon() |
189 : network_state_(kUnknownNetworkState), | 122 : power_state_(kUnknownPowerState), |
190 power_state_(kUnknownPowerState), | |
191 session_state_(kUnknownSessionState), | 123 session_state_(kUnknownSessionState), |
192 user_active_(false), | 124 user_active_(false), |
193 usemon_interval_(0), | 125 usemon_interval_(0), |
194 usemon_source_(NULL) {} | 126 usemon_source_(NULL) {} |
195 | 127 |
196 MetricsDaemon::~MetricsDaemon() { | 128 MetricsDaemon::~MetricsDaemon() { |
197 DeleteFrequencyCounters(); | 129 DeleteFrequencyCounters(); |
198 } | 130 } |
199 | 131 |
200 void MetricsDaemon::DeleteFrequencyCounters() { | 132 void MetricsDaemon::DeleteFrequencyCounters() { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 DLOG(INFO) << "adding dbus match: " << match; | 242 DLOG(INFO) << "adding dbus match: " << match; |
311 dbus_bus_add_match(connection, match, &error); | 243 dbus_bus_add_match(connection, match, &error); |
312 LOG_IF(FATAL, dbus_error_is_set(&error)) << | 244 LOG_IF(FATAL, dbus_error_is_set(&error)) << |
313 "unable to add a match: " << SAFE_MESSAGE(error); | 245 "unable to add a match: " << SAFE_MESSAGE(error); |
314 } | 246 } |
315 | 247 |
316 // Adds the D-Bus filter routine to be called back whenever one of | 248 // Adds the D-Bus filter routine to be called back whenever one of |
317 // the registered D-Bus matches is successful. The daemon is not | 249 // the registered D-Bus matches is successful. The daemon is not |
318 // activated for D-Bus messages that don't match. | 250 // activated for D-Bus messages that don't match. |
319 CHECK(dbus_connection_add_filter(connection, MessageFilter, this, NULL)); | 251 CHECK(dbus_connection_add_filter(connection, MessageFilter, this, NULL)); |
320 | |
321 // Initializes the current network state by retrieving it from flimflam. | |
322 string state_name = DBusGetString(connection, "org.chromium.flimflam", "/", | |
323 DBUS_IFACE_FLIMFLAM_MANAGER, "GetState"); | |
324 NetStateChanged(state_name.c_str(), TimeTicks::Now()); | |
325 } | 252 } |
326 | 253 |
327 void MetricsDaemon::Loop() { | 254 void MetricsDaemon::Loop() { |
328 GMainLoop* loop = g_main_loop_new(NULL, false); | 255 GMainLoop* loop = g_main_loop_new(NULL, false); |
329 g_main_loop_run(loop); | 256 g_main_loop_run(loop); |
330 } | 257 } |
331 | 258 |
332 // static | 259 // static |
333 DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection, | 260 DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection, |
334 DBusMessage* message, | 261 DBusMessage* message, |
(...skipping 13 matching lines...) Expand all Loading... |
348 CHECK(interface != NULL); | 275 CHECK(interface != NULL); |
349 | 276 |
350 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data); | 277 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data); |
351 | 278 |
352 DBusMessageIter iter; | 279 DBusMessageIter iter; |
353 dbus_message_iter_init(message, &iter); | 280 dbus_message_iter_init(message, &iter); |
354 if (strcmp(interface, DBUS_IFACE_CRASH_REPORTER) == 0) { | 281 if (strcmp(interface, DBUS_IFACE_CRASH_REPORTER) == 0) { |
355 CHECK(strcmp(dbus_message_get_member(message), | 282 CHECK(strcmp(dbus_message_get_member(message), |
356 "UserCrash") == 0); | 283 "UserCrash") == 0); |
357 daemon->ProcessUserCrash(); | 284 daemon->ProcessUserCrash(); |
358 } else if (strcmp(interface, DBUS_IFACE_FLIMFLAM_MANAGER) == 0) { | |
359 CHECK(strcmp(dbus_message_get_member(message), | |
360 "StateChanged") == 0); | |
361 | |
362 char* state_name; | |
363 dbus_message_iter_get_basic(&iter, &state_name); | |
364 daemon->NetStateChanged(state_name, ticks); | |
365 } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) { | 285 } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) { |
366 const char* member = dbus_message_get_member(message); | 286 const char* member = dbus_message_get_member(message); |
367 if (strcmp(member, "ScreenIsLocked") == 0) { | 287 if (strcmp(member, "ScreenIsLocked") == 0) { |
368 daemon->SetUserActiveState(false, now); | 288 daemon->SetUserActiveState(false, now); |
369 } else if (strcmp(member, "ScreenIsUnlocked") == 0) { | 289 } else if (strcmp(member, "ScreenIsUnlocked") == 0) { |
370 daemon->SetUserActiveState(true, now); | 290 daemon->SetUserActiveState(true, now); |
371 } else if (strcmp(member, "PowerStateChanged") == 0) { | 291 } else if (strcmp(member, "PowerStateChanged") == 0) { |
372 char* state_name; | 292 char* state_name; |
373 dbus_message_iter_get_basic(&iter, &state_name); | 293 dbus_message_iter_get_basic(&iter, &state_name); |
374 daemon->PowerStateChanged(state_name, now); | 294 daemon->PowerStateChanged(state_name, now); |
375 } | 295 } |
376 } else if (strcmp(interface, DBUS_IFACE_SESSION_MANAGER) == 0) { | 296 } else if (strcmp(interface, DBUS_IFACE_SESSION_MANAGER) == 0) { |
377 CHECK(strcmp(dbus_message_get_member(message), | 297 CHECK(strcmp(dbus_message_get_member(message), |
378 "SessionStateChanged") == 0); | 298 "SessionStateChanged") == 0); |
379 | 299 |
380 char* state_name; | 300 char* state_name; |
381 dbus_message_iter_get_basic(&iter, &state_name); | 301 dbus_message_iter_get_basic(&iter, &state_name); |
382 daemon->SessionStateChanged(state_name, now); | 302 daemon->SessionStateChanged(state_name, now); |
383 } else { | 303 } else { |
384 DLOG(WARNING) << "unexpected interface: " << interface; | 304 DLOG(WARNING) << "unexpected interface: " << interface; |
385 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 305 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
386 } | 306 } |
387 | 307 |
388 return DBUS_HANDLER_RESULT_HANDLED; | 308 return DBUS_HANDLER_RESULT_HANDLED; |
389 } | 309 } |
390 | 310 |
391 void MetricsDaemon::NetStateChanged(const char* state_name, TimeTicks ticks) { | |
392 DLOG(INFO) << "network state: " << state_name; | |
393 | |
394 NetworkState state = LookupNetworkState(state_name); | |
395 | |
396 // Logs the time in seconds between the network going online to | |
397 // going offline (or, more precisely, going not online) in order to | |
398 // measure the mean time to network dropping. Going offline as part | |
399 // of suspend-to-RAM is not logged as network drop -- the assumption | |
400 // is that the message for suspend-to-RAM comes before the network | |
401 // offline message which seems to and should be the case. | |
402 if (state != kNetworkStateOnline && | |
403 network_state_ == kNetworkStateOnline && | |
404 power_state_ != kPowerStateMem) { | |
405 TimeDelta since_online = ticks - network_state_last_; | |
406 int online_time = static_cast<int>(since_online.InSeconds()); | |
407 SendMetric(kMetricTimeToNetworkDropName, online_time, | |
408 kMetricTimeToNetworkDropMin, | |
409 kMetricTimeToNetworkDropMax, | |
410 kMetricTimeToNetworkDropBuckets); | |
411 } | |
412 | |
413 network_state_ = state; | |
414 network_state_last_ = ticks; | |
415 } | |
416 | |
417 MetricsDaemon::NetworkState | |
418 MetricsDaemon::LookupNetworkState(const char* state_name) { | |
419 for (int i = 0; i < kNumberNetworkStates; i++) { | |
420 if (strcmp(state_name, kNetworkStates_[i]) == 0) { | |
421 return static_cast<NetworkState>(i); | |
422 } | |
423 } | |
424 DLOG(WARNING) << "unknown network connection state: " << state_name; | |
425 return kUnknownNetworkState; | |
426 } | |
427 | |
428 void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) { | 311 void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) { |
429 DLOG(INFO) << "power state: " << state_name; | 312 DLOG(INFO) << "power state: " << state_name; |
430 power_state_ = LookupPowerState(state_name); | 313 power_state_ = LookupPowerState(state_name); |
431 | 314 |
432 if (power_state_ != kPowerStateOn) | 315 if (power_state_ != kPowerStateOn) |
433 SetUserActiveState(false, now); | 316 SetUserActiveState(false, now); |
434 } | 317 } |
435 | 318 |
436 MetricsDaemon::PowerState | 319 MetricsDaemon::PowerState |
437 MetricsDaemon::LookupPowerState(const char* state_name) { | 320 MetricsDaemon::LookupPowerState(const char* state_name) { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 kMetricDailyUseTimeMax, | 507 kMetricDailyUseTimeMax, |
625 kMetricDailyUseTimeBuckets); | 508 kMetricDailyUseTimeBuckets); |
626 } | 509 } |
627 | 510 |
628 void MetricsDaemon::SendMetric(const string& name, int sample, | 511 void MetricsDaemon::SendMetric(const string& name, int sample, |
629 int min, int max, int nbuckets) { | 512 int min, int max, int nbuckets) { |
630 DLOG(INFO) << "received metric: " << name << " " << sample << " " | 513 DLOG(INFO) << "received metric: " << name << " " << sample << " " |
631 << min << " " << max << " " << nbuckets; | 514 << min << " " << max << " " << nbuckets; |
632 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); | 515 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
633 } | 516 } |
OLD | NEW |