blob: 1df9e5d63054c42b650cec4ac14799c3fa197cf5 [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
15#include "shill/refptr_types.h"
16#include "shill/service.h"
17
18namespace shill {
19
20class WiFiService;
21
22class Metrics {
23 public:
24 enum WiFiChannel {
25 kWiFiChannelUndef = 0,
26 kWiFiChannel2412 = 1,
27 kWiFiChannel2417 = 2,
28 kWiFiChannel2422 = 3,
29 kWiFiChannel2427 = 4,
30 kWiFiChannel2432 = 5,
31 kWiFiChannel2437 = 6,
32 kWiFiChannel2442 = 7,
33 kWiFiChannel2447 = 8,
34 kWiFiChannel2452 = 9,
35 kWiFiChannel2457 = 10,
36 kWiFiChannel2462 = 11,
37 kWiFiChannel2467 = 12,
38 kWiFiChannel2472 = 13,
39 kWiFiChannel2484 = 14,
40
41 kWiFiChannel5180 = 15,
42 kWiFiChannel5200 = 16,
43 kWiFiChannel5220 = 17,
44 kWiFiChannel5240 = 18,
45 kWiFiChannel5260 = 19,
46 kWiFiChannel5280 = 20,
47 kWiFiChannel5300 = 21,
48 kWiFiChannel5320 = 22,
49
50 kWiFiChannel5500 = 23,
51 kWiFiChannel5520 = 24,
52 kWiFiChannel5540 = 25,
53 kWiFiChannel5560 = 26,
54 kWiFiChannel5580 = 27,
55 kWiFiChannel5600 = 28,
56 kWiFiChannel5620 = 29,
57 kWiFiChannel5640 = 30,
58 kWiFiChannel5660 = 31,
59 kWiFiChannel5680 = 32,
60 kWiFiChannel5700 = 33,
61
62 kWiFiChannel5745 = 34,
63 kWiFiChannel5765 = 35,
64 kWiFiChannel5785 = 36,
65 kWiFiChannel5805 = 37,
66 kWiFiChannel5825 = 38,
67
68 kWiFiChannel5170 = 39,
69 kWiFiChannel5190 = 40,
70 kWiFiChannel5210 = 41,
71 kWiFiChannel5230 = 42,
72
73 /* NB: ignore old 11b bands 2312..2372 and 2512..2532 */
74 /* NB: ignore regulated bands 4920..4980 and 5020..5160 */
75 kWiFiChannelMax
76 };
77
Thieu Lead1ec2c2012-01-05 23:39:48 +000078 enum WiFiNetworkPhyMode {
79 kWiFiNetworkPhyModeUndef = 0, // Unknown/undefined
80 kWiFiNetworkPhyMode11a = 1, // 802.11a
81 kWiFiNetworkPhyMode11b = 2, // 802.11b
82 kWiFiNetworkPhyMode11g = 3, // 802.11g
83 kWiFiNetworkPhyMode11n = 4, // 802.11n
84 kWiFiNetworkPhyModeHalf = 5, // PSB Half-width
85 kWiFiNetworkPhyModeQuarter = 6, // PSB Quarter-width
86 kWiFiNetworkPhyModeTurbo = 7, // Atheros Turbo mode
87
88 kWiFiNetworkPhyModeMax
89 };
90
91 enum WiFiSecurity {
92 kWiFiSecurityUnknown = 0,
93 kWiFiSecurityNone = 1,
94 kWiFiSecurityWep = 2,
95 kWiFiSecurityWpa = 3,
96 kWiFiSecurityRsn = 4,
97 kWiFiSecurity8021x = 5,
98 kWiFiSecurityPsk = 6,
99
100 kWiFiSecurityMax
101 };
102
Thieu Le48e6d6d2011-12-06 00:40:27 +0000103 static const char kMetricNetworkChannel[];
104 static const int kMetricNetworkChannelMax;
Thieu Lead1ec2c2012-01-05 23:39:48 +0000105 static const char kMetricNetworkPhyMode[];
106 static const int kMetricNetworkPhyModeMax;
107 static const char kMetricNetworkSecurity[];
108 static const int kMetricNetworkSecurityMax;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000109 static const char kMetricNetworkServiceErrors[];
110 static const int kMetricNetworkServiceErrorsMax;
111 static const char kMetricTimeToConfigMilliseconds[];
112 static const char kMetricTimeToJoinMilliseconds[];
113 static const char kMetricTimeToOnlineMilliseconds[];
114 static const char kMetricTimeToPortalMilliseconds[];
115 static const int kTimerHistogramMaxMilliseconds;
116 static const int kTimerHistogramMinMilliseconds;
117 static const int kTimerHistogramNumBuckets;
118
Thieu Le3426c8f2012-01-11 17:35:11 -0800119 Metrics();
Thieu Le48e6d6d2011-12-06 00:40:27 +0000120 virtual ~Metrics();
121
Thieu Le48e6d6d2011-12-06 00:40:27 +0000122 // Converts the WiFi frequency into the associated UMA channel enumerator.
123 static WiFiChannel WiFiFrequencyToChannel(uint16 frequency);
124
Thieu Lead1ec2c2012-01-05 23:39:48 +0000125 // Converts a flimflam security string into its UMA security enumerator.
126 static WiFiSecurity WiFiSecurityStringToEnum(const std::string &security);
127
Thieu Le48e6d6d2011-12-06 00:40:27 +0000128 // Registers a service with this object so it can use the timers to track
129 // state transition metrics.
130 void RegisterService(const Service *service);
131
132 // Deregisters the service from this class. All state transition timers
133 // will be removed.
134 void DeregisterService(const Service *service);
135
136 // Tracks the time it takes |service| to go from |start_state| to
137 // |stop_state|. When |stop_state| is reached, the time is sent to UMA.
138 void AddServiceStateTransitionTimer(const Service *service,
139 const std::string &histogram_name,
140 Service::ConnectState start_state,
141 Service::ConnectState stop_state);
142
143 // Specializes |metric_name| for the specified |technology_id|.
144 std::string GetFullMetricName(const char *metric_name,
145 Technology::Identifier technology_id);
146
147 // Notifies this object that the default service has changed.
148 // |service| is the new default service.
149 void NotifyDefaultServiceChanged(const Service *service);
150
151 // Notifies this object that |service| state has changed.
152 virtual void NotifyServiceStateChanged(const Service *service,
153 Service::ConnectState new_state);
154
155 // Notifies this object that |service| has been disconnected and whether
156 // the disconnect was requested by the user or not.
157 void NotifyServiceDisconnect(const Service *service,
158 bool manual_disconnect);
159
160 // Notifies this object of a power management event.
161 void NotifyPower();
162
163 // Sends linear histogram data to UMA.
164 bool SendEnumToUMA(const std::string &name, int sample, int max);
165
166 private:
167 friend struct base::DefaultLazyInstanceTraits<Metrics>;
168 friend class MetricsTest;
169 FRIEND_TEST(MetricsTest, TimeToConfig);
170 FRIEND_TEST(MetricsTest, TimeToPortal);
171 FRIEND_TEST(MetricsTest, TimeToOnline);
172 FRIEND_TEST(MetricsTest, ServiceFailure);
173 FRIEND_TEST(MetricsTest, WiFiServiceChannel);
174 FRIEND_TEST(MetricsTest, FrequencyToChannel);
175
176 typedef ScopedVector<chromeos_metrics::TimerReporter> TimerReporters;
177 typedef std::list<chromeos_metrics::TimerReporter *> TimerReportersList;
178 typedef std::map<Service::ConnectState, TimerReportersList>
179 TimerReportersByState;
180 struct ServiceMetrics {
181 ServiceMetrics() : service(NULL) {}
182 // The service is registered/deregistered in the Service
183 // constructor/destructor, therefore there is no need to keep a ref count.
184 const Service *service;
185 // All TimerReporter objects are stored in |timers| which owns the objects.
186 // |start_on_state| and |stop_on_state| contain pointers to the
187 // TimerReporter objects and control when to start and stop the timers.
188 TimerReporters timers;
189 TimerReportersByState start_on_state;
190 TimerReportersByState stop_on_state;
191 };
192 typedef std::map<const Service *, std::tr1::shared_ptr<ServiceMetrics> >
193 ServiceMetricsLookupMap;
194
195 static const uint16 kWiFiBandwidth5MHz;
196 static const uint16 kWiFiBandwidth20MHz;
197 static const uint16 kWiFiFrequency2412;
198 static const uint16 kWiFiFrequency2472;
199 static const uint16 kWiFiFrequency2484;
200 static const uint16 kWiFiFrequency5170;
201 static const uint16 kWiFiFrequency5180;
202 static const uint16 kWiFiFrequency5230;
203 static const uint16 kWiFiFrequency5240;
204 static const uint16 kWiFiFrequency5320;
205 static const uint16 kWiFiFrequency5500;
206 static const uint16 kWiFiFrequency5700;
207 static const uint16 kWiFiFrequency5745;
208 static const uint16 kWiFiFrequency5825;
209
Thieu Le48e6d6d2011-12-06 00:40:27 +0000210 void InitializeCommonServiceMetrics(const Service *service);
211 void UpdateServiceStateTransitionMetrics(ServiceMetrics *service_metrics,
212 Service::ConnectState new_state);
213 void SendServiceFailure(const Service *service);
214
215 // For unit test purposes.
216 void set_library(MetricsLibraryInterface *library);
217
218 // |library_| points to |metrics_library_| when shill runs normally.
219 // However, in order to allow for unit testing, we point |library_| to a
220 // MetricsLibraryMock object instead.
221 MetricsLibrary metrics_library_;
222 MetricsLibraryInterface *library_;
223 ServiceMetricsLookupMap services_metrics_;
224
225 DISALLOW_COPY_AND_ASSIGN(Metrics);
226};
227
228} // namespace shill
229
230#endif // SHILL_METRICS_