| 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 #include "metrics_library.h" | 6 #include "metrics_library.h" |
| 7 | 7 |
| 8 #include <glib-object.h> | 8 #include <glib-object.h> |
| 9 | 9 |
| 10 extern "C" { | 10 extern "C" { |
| 11 #include "marshal_void__string_boxed.h" | 11 #include "marshal_void__string_boxed.h" |
| 12 } | 12 } |
| 13 | 13 |
| 14 #include <base/logging.h> | 14 #include <base/logging.h> |
| 15 | 15 |
| 16 #define SAFE_MESSAGE(e) ((e && e->message) ? e->message : "unknown error") | 16 #define SAFE_MESSAGE(e) ((e && e->message) ? e->message : "unknown error") |
| 17 | 17 |
| 18 MetricsDaemon::NetworkState | 18 MetricsDaemon::NetworkState |
| 19 MetricsDaemon::network_states_[MetricsDaemon::kNumberNetworkStates] = { | 19 MetricsDaemon::network_states_[MetricsDaemon::kNumberNetworkStates] = { |
| 20 #define STATE(name, capname) { #name, "Connman" # capname }, | 20 #define STATE(name, capname) { #name, "Network.Connman" # capname }, |
| 21 #include "network_states.h" | 21 #include "network_states.h" |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 void MetricsDaemon::Run(bool run_as_daemon, bool testing) { | 24 void MetricsDaemon::Run(bool run_as_daemon, bool testing) { |
| 25 Init(testing); | 25 Init(testing); |
| 26 if (!run_as_daemon || daemon(0, 0) == 0) { | 26 if (!run_as_daemon || daemon(0, 0) == 0) { |
| 27 Loop(); | 27 Loop(); |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 106 } |
| 107 if (old_id != kUnknownNetworkStateId) { | 107 if (old_id != kUnknownNetworkStateId) { |
| 108 struct timeval diff; | 108 struct timeval diff; |
| 109 timersub(&now, &network_state_start_, &diff); | 109 timersub(&now, &network_state_start_, &diff); |
| 110 int diff_ms = diff.tv_usec / 1000 + diff.tv_sec * 1000; | 110 int diff_ms = diff.tv_usec / 1000 + diff.tv_sec * 1000; |
| 111 // Saturates rather than overflowing. We expect this to be statistically | 111 // Saturates rather than overflowing. We expect this to be statistically |
| 112 // insignificant, since INT_MAX milliseconds is 24.8 days. | 112 // insignificant, since INT_MAX milliseconds is 24.8 days. |
| 113 if (diff.tv_sec >= INT_MAX / 1000) { | 113 if (diff.tv_sec >= INT_MAX / 1000) { |
| 114 diff_ms = INT_MAX; | 114 diff_ms = INT_MAX; |
| 115 } | 115 } |
| 116 if (testing_) { | 116 PublishMetric(network_states_[old_id].stat_name, |
| 117 TestPublishMetric(network_states_[old_id].stat_name, diff_ms); | 117 diff_ms, |
| 118 } else { | 118 1, |
| 119 ChromePublishMetric(network_states_[old_id].stat_name, diff_ms); | 119 8 * 60 * 60 * 1000, // 8 hours in milliseconds |
| 120 } | 120 100); |
| 121 } | 121 } |
| 122 network_state_id_ = new_id; | 122 network_state_id_ = new_id; |
| 123 network_state_start_ = now; | 123 network_state_start_ = now; |
| 124 } | 124 } |
| 125 | 125 |
| 126 MetricsDaemon::NetworkStateId | 126 MetricsDaemon::NetworkStateId |
| 127 MetricsDaemon::GetNetworkStateId(const char* state_name) { | 127 MetricsDaemon::GetNetworkStateId(const char* state_name) { |
| 128 for (int i = 0; i < kNumberNetworkStates; i++) { | 128 for (int i = 0; i < kNumberNetworkStates; i++) { |
| 129 if (strcmp(state_name, network_states_[i].name) == 0) { | 129 if (strcmp(state_name, network_states_[i].name) == 0) { |
| 130 return static_cast<NetworkStateId>(i); | 130 return static_cast<NetworkStateId>(i); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 return static_cast<NetworkStateId>(-1); | 133 return static_cast<NetworkStateId>(-1); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void MetricsDaemon::ChromePublishMetric(const char* name, int value) { | 136 void MetricsDaemon::PublishMetric(const char* name, int sample, |
| 137 MetricsLibrary::SendToChrome(name, value); | 137 int min, int max, int nbuckets) { |
| 138 if (testing_) { |
| 139 LOG(INFO) << "received metric: " << name << " " << sample << |
| 140 " " << min << " " << max << " " << nbuckets; |
| 141 } else { |
| 142 MetricsLibrary::SendToChrome(name, sample, min, max, nbuckets); |
| 143 } |
| 138 } | 144 } |
| 139 | |
| 140 void MetricsDaemon::TestPublishMetric(const char* name, int value) { | |
| 141 LOG(INFO) << "received metric: " << name << " " << value; | |
| 142 } | |
| OLD | NEW |