blob: d4e7e69b148a3ec95e9cfbba1ab3c267c95b8519 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Thieu Le48e6d6d2011-12-06 00:40:27 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_METRICS_
6#define SHILL_METRICS_
7
8#include <list>
9
10#include <base/lazy_instance.h>
11#include <base/memory/scoped_vector.h>
12#include <metrics/metrics_library.h>
13#include <metrics/timer.h>
14
Thieu Le85e050b2012-03-13 15:04:38 -070015#include "shill/portal_detector.h"
Thieu Leb84ba342012-03-02 15:15:19 -080016#include "shill/power_manager.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000017#include "shill/refptr_types.h"
18#include "shill/service.h"
19
20namespace shill {
21
22class WiFiService;
23
24class Metrics {
25 public:
26 enum WiFiChannel {
27 kWiFiChannelUndef = 0,
28 kWiFiChannel2412 = 1,
29 kWiFiChannel2417 = 2,
30 kWiFiChannel2422 = 3,
31 kWiFiChannel2427 = 4,
32 kWiFiChannel2432 = 5,
33 kWiFiChannel2437 = 6,
34 kWiFiChannel2442 = 7,
35 kWiFiChannel2447 = 8,
36 kWiFiChannel2452 = 9,
37 kWiFiChannel2457 = 10,
38 kWiFiChannel2462 = 11,
39 kWiFiChannel2467 = 12,
40 kWiFiChannel2472 = 13,
41 kWiFiChannel2484 = 14,
42
43 kWiFiChannel5180 = 15,
44 kWiFiChannel5200 = 16,
45 kWiFiChannel5220 = 17,
46 kWiFiChannel5240 = 18,
47 kWiFiChannel5260 = 19,
48 kWiFiChannel5280 = 20,
49 kWiFiChannel5300 = 21,
50 kWiFiChannel5320 = 22,
51
52 kWiFiChannel5500 = 23,
53 kWiFiChannel5520 = 24,
54 kWiFiChannel5540 = 25,
55 kWiFiChannel5560 = 26,
56 kWiFiChannel5580 = 27,
57 kWiFiChannel5600 = 28,
58 kWiFiChannel5620 = 29,
59 kWiFiChannel5640 = 30,
60 kWiFiChannel5660 = 31,
61 kWiFiChannel5680 = 32,
62 kWiFiChannel5700 = 33,
63
64 kWiFiChannel5745 = 34,
65 kWiFiChannel5765 = 35,
66 kWiFiChannel5785 = 36,
67 kWiFiChannel5805 = 37,
68 kWiFiChannel5825 = 38,
69
70 kWiFiChannel5170 = 39,
71 kWiFiChannel5190 = 40,
72 kWiFiChannel5210 = 41,
73 kWiFiChannel5230 = 42,
74
75 /* NB: ignore old 11b bands 2312..2372 and 2512..2532 */
76 /* NB: ignore regulated bands 4920..4980 and 5020..5160 */
77 kWiFiChannelMax
78 };
79
Thieu Lead1ec2c2012-01-05 23:39:48 +000080 enum WiFiNetworkPhyMode {
81 kWiFiNetworkPhyModeUndef = 0, // Unknown/undefined
82 kWiFiNetworkPhyMode11a = 1, // 802.11a
83 kWiFiNetworkPhyMode11b = 2, // 802.11b
84 kWiFiNetworkPhyMode11g = 3, // 802.11g
85 kWiFiNetworkPhyMode11n = 4, // 802.11n
86 kWiFiNetworkPhyModeHalf = 5, // PSB Half-width
87 kWiFiNetworkPhyModeQuarter = 6, // PSB Quarter-width
88 kWiFiNetworkPhyModeTurbo = 7, // Atheros Turbo mode
89
90 kWiFiNetworkPhyModeMax
91 };
92
93 enum WiFiSecurity {
94 kWiFiSecurityUnknown = 0,
95 kWiFiSecurityNone = 1,
96 kWiFiSecurityWep = 2,
97 kWiFiSecurityWpa = 3,
98 kWiFiSecurityRsn = 4,
99 kWiFiSecurity8021x = 5,
100 kWiFiSecurityPsk = 6,
101
102 kWiFiSecurityMax
103 };
104
Thieu Le85e050b2012-03-13 15:04:38 -0700105 enum PortalResult {
106 kPortalResultSuccess = 0,
107 kPortalResultDNSFailure = 1,
108 kPortalResultDNSTimeout = 2,
109 kPortalResultConnectionFailure = 3,
110 kPortalResultConnectionTimeout = 4,
111 kPortalResultHTTPFailure = 5,
112 kPortalResultHTTPTimeout = 6,
113 kPortalResultContentFailure = 7,
114 kPortalResultContentTimeout = 8,
115 kPortalResultUnknown = 9,
116
117 kPortalResultMax
118 };
119
Thieu Le67370f62012-02-14 23:01:42 +0000120 static const char kMetricDisconnect[];
121 static const int kMetricDisconnectMax;
122 static const int kMetricDisconnectMin;
123 static const int kMetricDisconnectNumBuckets;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000124 static const char kMetricNetworkChannel[];
125 static const int kMetricNetworkChannelMax;
Thieu Lead1ec2c2012-01-05 23:39:48 +0000126 static const char kMetricNetworkPhyMode[];
127 static const int kMetricNetworkPhyModeMax;
128 static const char kMetricNetworkSecurity[];
129 static const int kMetricNetworkSecurityMax;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000130 static const char kMetricNetworkServiceErrors[];
131 static const int kMetricNetworkServiceErrorsMax;
Thieu Lea20cbc22012-01-09 22:01:43 +0000132 static const char kMetricTimeOnlineSeconds[];
133 static const int kMetricTimeOnlineSecondsMax;
134 static const int kMetricTimeOnlineSecondsMin;
135 static const int kMetricTimeOnlineSecondsNumBuckets;
Thieu Leb84ba342012-03-02 15:15:19 -0800136 static const char kMetricTimeResumeToReadyMilliseconds[];
Thieu Le48e6d6d2011-12-06 00:40:27 +0000137 static const char kMetricTimeToConfigMilliseconds[];
Thieu Lea20cbc22012-01-09 22:01:43 +0000138 static const char kMetricTimeToDropSeconds[];
139 static const int kMetricTimeToDropSecondsMax;
140 static const int kMetricTimeToDropSecondsMin;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000141 static const char kMetricTimeToJoinMilliseconds[];
142 static const char kMetricTimeToOnlineMilliseconds[];
143 static const char kMetricTimeToPortalMilliseconds[];
Thieu Lea20cbc22012-01-09 22:01:43 +0000144 static const int kTimerHistogramMillisecondsMax;
145 static const int kTimerHistogramMillisecondsMin;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000146 static const int kTimerHistogramNumBuckets;
147
Thieu Le85e050b2012-03-13 15:04:38 -0700148 // The number of portal detections attempted for each pass.
149 // This includes both failure/timeout attempts and successful attempt
150 // (if any).
151 static const char kMetricPortalAttempts[];
152 static const int kMetricPortalAttemptsMax;
153 static const int kMetricPortalAttemptsMin;
154 static const int kMetricPortalAttemptsNumBuckets;
155
156 // The total number of portal detections attempted between the Connected
157 // state and the Online state. This includes both failure/timeout attempts
158 // and the final successful attempt.
159 static const char kMetricPortalAttemptsToOnline[];
160 static const int kMetricPortalAttemptsToOnlineMax;
161 static const int kMetricPortalAttemptsToOnlineMin;
162 static const int kMetricPortalAttemptsToOnlineNumBuckets;
163
164 // The result of the portal detection.
165 static const char kMetricPortalResult[];
166
Thieu Leb84ba342012-03-02 15:15:19 -0800167 static const char kMetricPowerManagerKey[];
168
Thieu Le3426c8f2012-01-11 17:35:11 -0800169 Metrics();
Thieu Le48e6d6d2011-12-06 00:40:27 +0000170 virtual ~Metrics();
171
Thieu Le48e6d6d2011-12-06 00:40:27 +0000172 // Converts the WiFi frequency into the associated UMA channel enumerator.
173 static WiFiChannel WiFiFrequencyToChannel(uint16 frequency);
174
Thieu Lead1ec2c2012-01-05 23:39:48 +0000175 // Converts a flimflam security string into its UMA security enumerator.
176 static WiFiSecurity WiFiSecurityStringToEnum(const std::string &security);
177
Thieu Le85e050b2012-03-13 15:04:38 -0700178 // Converts portal detection result to UMA portal result enumerator.
179 static PortalResult PortalDetectionResultToEnum(
180 const PortalDetector::Result &result);
181
Thieu Le48e6d6d2011-12-06 00:40:27 +0000182 // Registers a service with this object so it can use the timers to track
183 // state transition metrics.
184 void RegisterService(const Service *service);
185
186 // Deregisters the service from this class. All state transition timers
187 // will be removed.
188 void DeregisterService(const Service *service);
189
190 // Tracks the time it takes |service| to go from |start_state| to
191 // |stop_state|. When |stop_state| is reached, the time is sent to UMA.
192 void AddServiceStateTransitionTimer(const Service *service,
193 const std::string &histogram_name,
194 Service::ConnectState start_state,
195 Service::ConnectState stop_state);
196
197 // Specializes |metric_name| for the specified |technology_id|.
198 std::string GetFullMetricName(const char *metric_name,
199 Technology::Identifier technology_id);
200
201 // Notifies this object that the default service has changed.
202 // |service| is the new default service.
Thieu Lea20cbc22012-01-09 22:01:43 +0000203 virtual void NotifyDefaultServiceChanged(const Service *service);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000204
205 // Notifies this object that |service| state has changed.
206 virtual void NotifyServiceStateChanged(const Service *service,
207 Service::ConnectState new_state);
208
Thieu Le67370f62012-02-14 23:01:42 +0000209 // Notifies this object that |service| has been disconnected.
210 void NotifyServiceDisconnect(const Service *service);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000211
Thieu Leb84ba342012-03-02 15:15:19 -0800212 // Notifies this object of a power management state change.
213 void NotifyPowerStateChange(PowerManager::SuspendState new_state);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000214
215 // Sends linear histogram data to UMA.
Thieu Le85e050b2012-03-13 15:04:38 -0700216 virtual bool SendEnumToUMA(const std::string &name, int sample, int max);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000217
Thieu Lea20cbc22012-01-09 22:01:43 +0000218 // Send histogram data to UMA.
Thieu Le85e050b2012-03-13 15:04:38 -0700219 virtual bool SendToUMA(const std::string &name, int sample, int min,
220 int max, int num_buckets);
Thieu Lea20cbc22012-01-09 22:01:43 +0000221
Thieu Le48e6d6d2011-12-06 00:40:27 +0000222 private:
223 friend struct base::DefaultLazyInstanceTraits<Metrics>;
224 friend class MetricsTest;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000225 FRIEND_TEST(MetricsTest, FrequencyToChannel);
Thieu Leb84ba342012-03-02 15:15:19 -0800226 FRIEND_TEST(MetricsTest, ServiceFailure);
Thieu Lea20cbc22012-01-09 22:01:43 +0000227 FRIEND_TEST(MetricsTest, TimeOnlineTimeToDrop);
Thieu Leb84ba342012-03-02 15:15:19 -0800228 FRIEND_TEST(MetricsTest, TimeToConfig);
229 FRIEND_TEST(MetricsTest, TimeToOnline);
230 FRIEND_TEST(MetricsTest, TimeToPortal);
231 FRIEND_TEST(MetricsTest, WiFiServiceChannel);
232 FRIEND_TEST(MetricsTest, WiFiServicePostReady);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000233
234 typedef ScopedVector<chromeos_metrics::TimerReporter> TimerReporters;
235 typedef std::list<chromeos_metrics::TimerReporter *> TimerReportersList;
236 typedef std::map<Service::ConnectState, TimerReportersList>
237 TimerReportersByState;
238 struct ServiceMetrics {
239 ServiceMetrics() : service(NULL) {}
240 // The service is registered/deregistered in the Service
241 // constructor/destructor, therefore there is no need to keep a ref count.
242 const Service *service;
243 // All TimerReporter objects are stored in |timers| which owns the objects.
244 // |start_on_state| and |stop_on_state| contain pointers to the
245 // TimerReporter objects and control when to start and stop the timers.
246 TimerReporters timers;
247 TimerReportersByState start_on_state;
248 TimerReportersByState stop_on_state;
249 };
250 typedef std::map<const Service *, std::tr1::shared_ptr<ServiceMetrics> >
251 ServiceMetricsLookupMap;
252
253 static const uint16 kWiFiBandwidth5MHz;
254 static const uint16 kWiFiBandwidth20MHz;
255 static const uint16 kWiFiFrequency2412;
256 static const uint16 kWiFiFrequency2472;
257 static const uint16 kWiFiFrequency2484;
258 static const uint16 kWiFiFrequency5170;
259 static const uint16 kWiFiFrequency5180;
260 static const uint16 kWiFiFrequency5230;
261 static const uint16 kWiFiFrequency5240;
262 static const uint16 kWiFiFrequency5320;
263 static const uint16 kWiFiFrequency5500;
264 static const uint16 kWiFiFrequency5700;
265 static const uint16 kWiFiFrequency5745;
266 static const uint16 kWiFiFrequency5825;
267
Thieu Le48e6d6d2011-12-06 00:40:27 +0000268 void InitializeCommonServiceMetrics(const Service *service);
269 void UpdateServiceStateTransitionMetrics(ServiceMetrics *service_metrics,
270 Service::ConnectState new_state);
271 void SendServiceFailure(const Service *service);
272
273 // For unit test purposes.
274 void set_library(MetricsLibraryInterface *library);
Thieu Lea20cbc22012-01-09 22:01:43 +0000275 void set_time_online_timer(chromeos_metrics::Timer *timer) {
276 time_online_timer_.reset(timer); // Passes ownership
277 }
278 void set_time_to_drop_timer(chromeos_metrics::Timer *timer) {
279 time_to_drop_timer_.reset(timer); // Passes ownership
280 }
Thieu Leb84ba342012-03-02 15:15:19 -0800281 void set_time_resume_to_ready_timer(chromeos_metrics::Timer *timer) {
282 time_resume_to_ready_timer_.reset(timer); // Passes ownership
283 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000284
285 // |library_| points to |metrics_library_| when shill runs normally.
286 // However, in order to allow for unit testing, we point |library_| to a
287 // MetricsLibraryMock object instead.
288 MetricsLibrary metrics_library_;
289 MetricsLibraryInterface *library_;
290 ServiceMetricsLookupMap services_metrics_;
Thieu Lea20cbc22012-01-09 22:01:43 +0000291 Technology::Identifier last_default_technology_;
292 bool was_online_;
293 scoped_ptr<chromeos_metrics::Timer> time_online_timer_;
294 scoped_ptr<chromeos_metrics::Timer> time_to_drop_timer_;
Thieu Leb84ba342012-03-02 15:15:19 -0800295 scoped_ptr<chromeos_metrics::Timer> time_resume_to_ready_timer_;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000296
297 DISALLOW_COPY_AND_ASSIGN(Metrics);
298};
299
300} // namespace shill
301
302#endif // SHILL_METRICS_