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 <dbus/dbus-glib-lowlevel.h> | 7 #include <dbus/dbus-glib-lowlevel.h> |
8 #include <sys/file.h> | 8 #include <sys/file.h> |
9 | 9 |
10 #include <base/eintr_wrapper.h> | 10 #include <base/eintr_wrapper.h> |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 #include "screensaver_states.h" | 93 #include "screensaver_states.h" |
94 }; | 94 }; |
95 | 95 |
96 // static | 96 // static |
97 const char* MetricsDaemon::kSessionStates_[] = { | 97 const char* MetricsDaemon::kSessionStates_[] = { |
98 #define STATE(name, capname) #name, | 98 #define STATE(name, capname) #name, |
99 #include "session_states.h" | 99 #include "session_states.h" |
100 }; | 100 }; |
101 | 101 |
102 void MetricsDaemon::Run(bool run_as_daemon) { | 102 void MetricsDaemon::Run(bool run_as_daemon) { |
103 MetricsLibrary metrics_lib; | |
104 metrics_lib.Init(); | |
105 Init(false, &metrics_lib); | |
106 if (!run_as_daemon || daemon(0, 0) == 0) { | 103 if (!run_as_daemon || daemon(0, 0) == 0) { |
107 Loop(); | 104 Loop(); |
108 } | 105 } |
109 } | 106 } |
110 | 107 |
111 void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) { | 108 void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) { |
112 testing_ = testing; | 109 testing_ = testing; |
113 DCHECK(metrics_lib != NULL); | 110 DCHECK(metrics_lib != NULL); |
114 metrics_lib_ = metrics_lib; | 111 metrics_lib_ = metrics_lib; |
115 daily_use_record_file_ = kDailyUseRecordFile; | 112 daily_use_record_file_ = kDailyUseRecordFile; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 // Logs the time in seconds between the network going online to | 213 // Logs the time in seconds between the network going online to |
217 // going offline (or, more precisely, going not online) in order to | 214 // going offline (or, more precisely, going not online) in order to |
218 // measure the mean time to network dropping. Going offline as part | 215 // measure the mean time to network dropping. Going offline as part |
219 // of suspend-to-RAM is not logged as network drop -- the assumption | 216 // of suspend-to-RAM is not logged as network drop -- the assumption |
220 // is that the message for suspend-to-RAM comes before the network | 217 // is that the message for suspend-to-RAM comes before the network |
221 // offline message which seems to and should be the case. | 218 // offline message which seems to and should be the case. |
222 if (state != kNetworkStateOnline && | 219 if (state != kNetworkStateOnline && |
223 network_state_ == kNetworkStateOnline && | 220 network_state_ == kNetworkStateOnline && |
224 power_state_ != kPowerStateMem) { | 221 power_state_ != kPowerStateMem) { |
225 int online_time = static_cast<int>(now - network_state_last_); | 222 int online_time = static_cast<int>(now - network_state_last_); |
226 PublishMetric(kMetricTimeToNetworkDropName, online_time, | 223 SendMetric(kMetricTimeToNetworkDropName, online_time, |
227 kMetricTimeToNetworkDropMin, | 224 kMetricTimeToNetworkDropMin, |
228 kMetricTimeToNetworkDropMax, | 225 kMetricTimeToNetworkDropMax, |
229 kMetricTimeToNetworkDropBuckets); | 226 kMetricTimeToNetworkDropBuckets); |
230 } | 227 } |
231 | 228 |
232 network_state_ = state; | 229 network_state_ = state; |
233 network_state_last_ = now; | 230 network_state_last_ = now; |
234 } | 231 } |
235 | 232 |
236 MetricsDaemon::NetworkState | 233 MetricsDaemon::NetworkState |
237 MetricsDaemon::LookupNetworkState(const char* state_name) { | 234 MetricsDaemon::LookupNetworkState(const char* state_name) { |
238 for (int i = 0; i < kNumberNetworkStates; i++) { | 235 for (int i = 0; i < kNumberNetworkStates; i++) { |
239 if (strcmp(state_name, kNetworkStates_[i]) == 0) { | 236 if (strcmp(state_name, kNetworkStates_[i]) == 0) { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 if (record.day_ == day) { | 339 if (record.day_ == day) { |
343 // If there's an existing record for today, aggregates the usage | 340 // If there's an existing record for today, aggregates the usage |
344 // time. | 341 // time. |
345 same_day = true; | 342 same_day = true; |
346 record.seconds_ += seconds; | 343 record.seconds_ += seconds; |
347 } else { | 344 } else { |
348 // If there's an existing record for a day in the past, rounds | 345 // If there's an existing record for a day in the past, rounds |
349 // the usage to the nearest minute and sends it to UMA. | 346 // the usage to the nearest minute and sends it to UMA. |
350 int minutes = | 347 int minutes = |
351 (record.seconds_ + kSecondsPerMinute / 2) / kSecondsPerMinute; | 348 (record.seconds_ + kSecondsPerMinute / 2) / kSecondsPerMinute; |
352 PublishMetric(kMetricDailyUseTimeName, minutes, | 349 SendMetric(kMetricDailyUseTimeName, minutes, |
353 kMetricDailyUseTimeMin, | 350 kMetricDailyUseTimeMin, |
354 kMetricDailyUseTimeMax, | 351 kMetricDailyUseTimeMax, |
355 kMetricDailyUseTimeBuckets); | 352 kMetricDailyUseTimeBuckets); |
356 | 353 |
357 // Truncates the usage file to ensure that no duplicate usage is | 354 // Truncates the usage file to ensure that no duplicate usage is |
358 // sent to UMA. | 355 // sent to UMA. |
359 PLOG_IF(WARNING, HANDLE_EINTR(ftruncate(fd, 0)) != 0); | 356 PLOG_IF(WARNING, HANDLE_EINTR(ftruncate(fd, 0)) != 0); |
360 } | 357 } |
361 } | 358 } |
362 | 359 |
363 // Updates the use record in the daily usage file if there's new | 360 // Updates the use record in the daily usage file if there's new |
364 // usage today. | 361 // usage today. |
365 if (seconds > 0) { | 362 if (seconds > 0) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 // If there's a use monitor scheduled already, destroys it. | 435 // If there's a use monitor scheduled already, destroys it. |
439 if (usemon_source_ == NULL) | 436 if (usemon_source_ == NULL) |
440 return; | 437 return; |
441 | 438 |
442 DLOG(INFO) << "destroying use monitor"; | 439 DLOG(INFO) << "destroying use monitor"; |
443 g_source_destroy(usemon_source_); | 440 g_source_destroy(usemon_source_); |
444 usemon_source_ = NULL; | 441 usemon_source_ = NULL; |
445 usemon_interval_ = 0; | 442 usemon_interval_ = 0; |
446 } | 443 } |
447 | 444 |
448 void MetricsDaemon::PublishMetric(const char* name, int sample, | 445 void MetricsDaemon::SendMetric(const std::string& name, int sample, |
449 int min, int max, int nbuckets) { | 446 int min, int max, int nbuckets) { |
450 DLOG(INFO) << "received metric: " << name << " " << sample << " " | 447 DLOG(INFO) << "received metric: " << name << " " << sample << " " |
451 << min << " " << max << " " << nbuckets; | 448 << min << " " << max << " " << nbuckets; |
452 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); | 449 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
453 } | 450 } |
OLD | NEW |