Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: components/data_use_measurement/content/data_use_measurement.cc

Issue 1818613002: Implement UMA log throttling for cellular connections (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium 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 "components/data_use_measurement/content/data_use_measurement.h" 5 #include "components/data_use_measurement/content/data_use_measurement.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "base/metrics/sparse_histogram.h" 8 #include "base/metrics/sparse_histogram.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
(...skipping 26 matching lines...) Expand all
37 void IncreaseSparseHistogramByValue(const std::string& name, 37 void IncreaseSparseHistogramByValue(const std::string& name,
38 int64_t sample, 38 int64_t sample,
39 int64_t value) { 39 int64_t value) {
40 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( 40 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
41 name, base::HistogramBase::kUmaTargetedHistogramFlag); 41 name, base::HistogramBase::kUmaTargetedHistogramFlag);
42 histogram->AddCount(sample, value); 42 histogram->AddCount(sample, value);
43 } 43 }
44 44
45 } // namespace 45 } // namespace
46 46
47 DataUseMeasurement::DataUseMeasurement() 47 DataUseMeasurement::DataUseMeasurement(
48 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder)
49 : metrics_data_use_forwarder_(metrics_data_use_forwarder)
48 #if defined(OS_ANDROID) 50 #if defined(OS_ANDROID)
49 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), 51 ,
52 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES),
50 app_listener_(new base::android::ApplicationStatusListener( 53 app_listener_(new base::android::ApplicationStatusListener(
51 base::Bind(&DataUseMeasurement::OnApplicationStateChange, 54 base::Bind(&DataUseMeasurement::OnApplicationStateChange,
52 base::Unretained(this)))) 55 base::Unretained(this))))
53 #endif 56 #endif
54 { 57 {
55 } 58 }
56 59
57 DataUseMeasurement::~DataUseMeasurement(){}; 60 DataUseMeasurement::~DataUseMeasurement(){};
58 61
59 void DataUseMeasurement::ReportDataUseUMA( 62 void DataUseMeasurement::ReportDataUseUMA(
60 const net::URLRequest* request) const { 63 const net::URLRequest* request) const {
61 const content::ResourceRequestInfo* info = 64 const content::ResourceRequestInfo* info =
62 content::ResourceRequestInfo::ForRequest(request); 65 content::ResourceRequestInfo::ForRequest(request);
63 // Having |info| is the sign of a request for a web content from user. For now 66 // Having |info| is the sign of a request for a web content from user. For now
64 // we could add a condition to check ProcessType in info is 67 // we could add a condition to check ProcessType in info is
65 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming 68 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming
66 // PlzNavigate architecture. So just existence of |info| is verified, and the 69 // PlzNavigate architecture. So just existence of |info| is verified, and the
67 // current check should be compatible with upcoming changes in PlzNavigate. 70 // current check should be compatible with upcoming changes in PlzNavigate.
68 bool is_user_traffic = info != nullptr; 71 bool is_user_traffic = info != nullptr;
69 72
70 // Counts rely on URLRequest::GetTotalReceivedBytes() and 73 // Counts rely on URLRequest::GetTotalReceivedBytes() and
71 // URLRequest::GetTotalSentBytes(), which does not include the send path, 74 // URLRequest::GetTotalSentBytes(), which does not include the send path,
72 // network layer overhead, TLS overhead, and DNS. 75 // network layer overhead, TLS overhead, and DNS.
73 // TODO(amohammadkhan): Make these measured bytes more in line with number of 76 // TODO(amohammadkhan): Make these measured bytes more in line with number of
74 // bytes in lower levels. 77 // bytes in lower levels.
75 int64_t total_upload_bytes = request->GetTotalSentBytes(); 78 int64_t total_upload_bytes = request->GetTotalSentBytes();
76 int64_t total_received_bytes = request->GetTotalReceivedBytes(); 79 int64_t total_received_bytes = request->GetTotalReceivedBytes();
77 80
81 bool is_connection_cellular =
82 net::NetworkChangeNotifier::IsConnectionCellular(
83 net::NetworkChangeNotifier::GetConnectionType());
78 RecordUMAHistogramCount( 84 RecordUMAHistogramCount(
79 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" 85 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User"
80 : "DataUse.TrafficSize.System", 86 : "DataUse.TrafficSize.System",
81 UPSTREAM), 87 UPSTREAM, is_connection_cellular),
82 total_upload_bytes); 88 total_upload_bytes);
83 RecordUMAHistogramCount( 89 RecordUMAHistogramCount(
84 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" 90 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User"
85 : "DataUse.TrafficSize.System", 91 : "DataUse.TrafficSize.System",
86 DOWNSTREAM), 92 DOWNSTREAM, is_connection_cellular),
87 total_received_bytes); 93 total_received_bytes);
88 94
89 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( 95 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>(
90 request->GetUserData(DataUseUserData::kUserDataKey)); 96 request->GetUserData(DataUseUserData::kUserDataKey));
97 DataUseUserData::ServiceName service_name =
98 attached_service_data ? attached_service_data->service_name()
99 : DataUseUserData::NOT_TAGGED;
100 if (!is_user_traffic) {
101 ReportDataUsageServices(service_name, UPSTREAM, is_connection_cellular,
102 total_upload_bytes);
103 ReportDataUsageServices(service_name, DOWNSTREAM, is_connection_cellular,
104 total_received_bytes);
105 }
91 106
92 if (!is_user_traffic) { 107 // Update data use prefs for cellular connections.
93 DataUseUserData::ServiceName service_name = 108 if (!metrics_data_use_forwarder_.is_null()) {
94 attached_service_data ? attached_service_data->service_name() 109 metrics_data_use_forwarder_.Run(
95 : DataUseUserData::NOT_TAGGED; 110 attached_service_data->GetServiceNameAsString(service_name),
96 ReportDataUsageServices(service_name, UPSTREAM, total_upload_bytes); 111 total_upload_bytes + total_received_bytes, is_connection_cellular);
97 ReportDataUsageServices(service_name, DOWNSTREAM, total_received_bytes);
98 } 112 }
99 } 113 }
100 114
101 #if defined(OS_ANDROID) 115 #if defined(OS_ANDROID)
102 void DataUseMeasurement::OnApplicationStateChangeForTesting( 116 void DataUseMeasurement::OnApplicationStateChangeForTesting(
103 base::android::ApplicationState application_state) { 117 base::android::ApplicationState application_state) {
104 app_state_ = application_state; 118 app_state_ = application_state;
105 } 119 }
106 #endif 120 #endif
107 121
108 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { 122 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const {
109 #if defined(OS_ANDROID) 123 #if defined(OS_ANDROID)
110 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) 124 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES)
111 return BACKGROUND; 125 return BACKGROUND;
112 #endif 126 #endif
113 // If the OS is not Android, all the requests are considered Foreground. 127 // If the OS is not Android, all the requests are considered Foreground.
114 return FOREGROUND; 128 return FOREGROUND;
115 } 129 }
116 130
117 std::string DataUseMeasurement::GetHistogramName(const char* prefix, 131 std::string DataUseMeasurement::GetHistogramName(
118 TrafficDirection dir) const { 132 const char* prefix,
133 TrafficDirection dir,
134 bool is_connection_cellular) const {
119 AppState app_state = CurrentAppState(); 135 AppState app_state = CurrentAppState();
120 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular(
121 net::NetworkChangeNotifier::GetConnectionType());
122 return base::StringPrintf( 136 return base::StringPrintf(
123 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", 137 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream",
124 app_state == BACKGROUND ? "Background" : "Foreground", 138 app_state == BACKGROUND ? "Background" : "Foreground",
125 is_conn_cellular ? "Cellular" : "NotCellular"); 139 is_connection_cellular ? "Cellular" : "NotCellular");
126 } 140 }
127 141
128 #if defined(OS_ANDROID) 142 #if defined(OS_ANDROID)
129 void DataUseMeasurement::OnApplicationStateChange( 143 void DataUseMeasurement::OnApplicationStateChange(
130 base::android::ApplicationState application_state) { 144 base::android::ApplicationState application_state) {
131 app_state_ = application_state; 145 app_state_ = application_state;
132 } 146 }
133 #endif 147 #endif
134 148
135 void DataUseMeasurement::ReportDataUsageServices( 149 void DataUseMeasurement::ReportDataUsageServices(
136 DataUseUserData::ServiceName service, 150 DataUseUserData::ServiceName service,
137 TrafficDirection dir, 151 TrafficDirection dir,
152 bool is_connection_cellular,
138 int64_t message_size) const { 153 int64_t message_size) const {
139 RecordUMAHistogramCount( 154 RecordUMAHistogramCount(
140 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), 155 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service),
141 message_size); 156 message_size);
142 if (message_size > 0) { 157 if (message_size > 0) {
143 IncreaseSparseHistogramByValue( 158 IncreaseSparseHistogramByValue(
144 GetHistogramName("DataUse.MessageSize.AllServices", dir), service, 159 GetHistogramName("DataUse.MessageSize.AllServices", dir,
145 message_size); 160 is_connection_cellular),
161 service, message_size);
146 } 162 }
147 } 163 }
148 164
149 } // namespace data_use_measurement 165 } // namespace data_use_measurement
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698