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

Unified Diff: components/data_use_measurement/content/data_use_measurement.cc

Issue 1279543002: Support needed to measure user and service traffic in Chrome. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@NewHistogram
Patch Set: Adding sclittle to OWNERs and little fix in ChromeNetworkDelegate test. Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: components/data_use_measurement/content/data_use_measurement.cc
diff --git a/components/data_use_measurement/content/data_use_measurement.cc b/components/data_use_measurement/content/data_use_measurement.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9d9e7bfc02111d681c31673fb3b4cb5a8c057167
--- /dev/null
+++ b/components/data_use_measurement/content/data_use_measurement.cc
@@ -0,0 +1,165 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <components/data_use_measurement/content/data_use_measurement.h>
+#include "base/metrics/histogram.h"
+#include "base/metrics/sparse_histogram.h"
+#include "base/strings/stringprintf.h"
+#include "content/public/browser/resource_request_info.h"
+#include "net/base/network_change_notifier.h"
+#include "net/base/upload_data_stream.h"
+#include "net/url_request/url_request.h"
+
+namespace data_use_measurement {
+
+namespace {
+
+// Records the occurrence of |sample| in |name| histogram. Conventional UMA
+// histograms are not used because the |name| is not static.
+void RecordUMAHistogramCount(const std::string& name, int64_t sample) {
+ base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet(
+ name,
+ 1, // Minimum sample size.
+ 1000000, // Maximum sample size. Should cover most of the requests by
Alexei Svitkine (slow) 2015/08/31 17:26:31 What's the units?
amohammadkhan 2015/09/01 04:52:48 Done.
+ // services.
+ 50, // Bucket count.
+ base::HistogramBase::kUmaTargetedHistogramFlag);
+ histogram_pointer->Add(sample);
+}
+
+// This function increases the value of |sample| bucket in |name| sparse
+// histogram by |value|. Conventional UMA histograms are not used because the
+// |name| is not static.
+void RecordSparseHistogramWithValue(const std::string& name,
+ int64_t sample,
+ int64_t value) {
+ base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
+ name, base::HistogramBase::kUmaTargetedHistogramFlag);
+ histogram->AddCount(sample, value);
+}
+
+} // namespace
+
+DataUseMeasurement::DataUseMeasurement()
+#if defined(OS_ANDROID)
+ : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES),
+ app_listener_(new base::android::ApplicationStatusListener(
+ base::Bind(&DataUseMeasurement::OnApplicationStateChange,
+ base::Unretained(this))))
+#endif
+{
+}
+
+DataUseMeasurement::~DataUseMeasurement() {
+#if defined(OS_ANDROID)
+ // |app_listener_| is destroyed before DataUseMeasurement instance is
+ // destroyed, so a callback isn't sent between the DataUseMeasurement instance
+ // being destroyed and the |app_listener_| being destroyed.
+ app_listener_.reset();
+#endif
+}
+void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) {
+ bool is_user_traffic = false;
+
+ const content::ResourceRequestInfo* info =
blundell 2015/08/31 09:15:40 IIUC, the only usage of //content in this code is
amohammadkhan 2015/09/01 04:52:48 Thanks for heads up. I see your point. But current
+ content::ResourceRequestInfo::ForRequest(request);
+ // Having |info| is the sign of a request for a web content from user. For now
+ // we could add a condition to check ProcessType in info is
+ // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming
+ // PlzNavigate architecture. So just existence of |info| is verified, and the
+ // current check should be compatible with upcoming changes in PlzNavigate.
+ if (info) {
+ is_user_traffic = true;
+ }
+
+ // These number won't be the number of bytes handed to the kernel because
+ // session layer framing and compression is not being accounted for.
+ // TODO(amohammadkhan): Make these measured bytes more in line with number of
+ // bytes in lower levels.
+ int64_t request_body_bytes = 0;
+ int64_t request_header_bytes = 0;
+ int64_t total_upload_bytes = 0;
+ if (request->has_upload())
+ request_body_bytes = request->get_upload()->size();
mmenke 2015/08/31 17:41:29 This is -1 for chunked uploads, which a lot of int
amohammadkhan 2015/09/01 04:52:48 After reading your comment, I wanted to try to und
mmenke 2015/09/01 19:23:37 sclittle is working on adding methods to expose a
+ net::HttpRequestHeaders request_headers;
+ if (request->GetFullRequestHeaders(&request_headers))
mmenke 2015/08/31 17:41:29 This method doesn't really work - it's not fully h
amohammadkhan 2015/09/01 04:52:48 Do you have any suggestion for a better substitute
+ request_header_bytes = request_headers.ToString().length();
mmenke 2015/08/31 17:41:28 Even if the method worked, this is only true for H
mmenke 2015/08/31 17:42:06 "For HTTP, and not SPDY (Or QUIC)", rather
amohammadkhan 2015/09/01 04:52:48 I see your points. For now I am trying to get a ro
amohammadkhan 2015/09/01 04:52:48 I agree.
+ total_upload_bytes = request_body_bytes + request_header_bytes;
+ int64_t total_received_bytes = request->GetTotalReceivedBytes();
+
+ ReportDataUsageURLRequest(is_user_traffic ? USER_TRAFFIC : SERVICE_TRAFFIC,
+ UPLOAD, total_upload_bytes);
+ ReportDataUsageURLRequest(is_user_traffic ? USER_TRAFFIC : SERVICE_TRAFFIC,
+ DOWNLOAD, total_received_bytes);
mmenke 2015/08/31 17:41:29 Having "service traffic" an independent dimension
amohammadkhan 2015/09/01 04:52:48 Good idea. I made a bucket for all untagged servic
+ DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>(
+ request->GetUserData(DataUseUserData::kUserDataKey));
+
+ if (attached_service_data) {
+ ReportDataUsage(attached_service_data->service_type(), UPLOAD,
+ total_upload_bytes);
+ ReportDataUsage(attached_service_data->service_type(), DOWNLOAD,
+ total_received_bytes);
+ }
+}
+
+#if defined(OS_ANDROID)
+void DataUseMeasurement::OnApplicationStateChange(
+ base::android::ApplicationState application_state) {
+ app_state_ = application_state;
+}
+#endif
+
+DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() {
+#if defined(OS_ANDROID)
+ if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES)
+ return BACKGROUND;
+#endif
+ // If the OS is not Android, all the requests are considered Foreground.
+ return FOREGROUND;
+}
+
+DataUseMeasurement::ConnectionType DataUseMeasurement::CurrentConnectionType() {
mmenke 2015/08/31 17:41:29 I don't think having this as a method, or mapping
amohammadkhan 2015/09/01 04:52:48 Done.
+ if (net::NetworkChangeNotifier::IsConnectionCellular(
+ net::NetworkChangeNotifier::GetConnectionType())) {
+ return CELLULAR;
+ }
+ return NOT_CELLULAR;
+}
+
+std::string DataUseMeasurement::GetDimensionForHistogramName(
mmenke 2015/08/31 17:41:29 Dimension -> Suffix? This results a string,not di
amohammadkhan 2015/09/01 04:52:48 Done.
+ TrafficDirection dir) {
+ AppState app_state = CurrentAppState();
+ ConnectionType conn_type = CurrentConnectionType();
+ return base::StringPrintf(
+ ".%s.%s.%s", dir == UPLOAD ? "Upload" : "Download",
mmenke 2015/08/31 17:41:29 This isn't categorizing requests as uploads or dow
amohammadkhan 2015/09/01 04:52:48 Done.
+ app_state == BACKGROUND ? "Background" : "Foreground",
+ conn_type == NOT_CELLULAR ? "NotCellular" : "Cellular");
+}
+
+void DataUseMeasurement::ReportDataUsage(
+ data_use_measurement::DataUseUserData::ServiceType service,
+ TrafficDirection dir,
+ int64_t message_size) {
+ std::string service_histogram_name =
+ base::StringPrintf("DataUse.Service.%s", GetServiceName(service));
+ std::string combined_histogram_name =
+ "DataUse.Services" + GetDimensionForHistogramName(dir);
mmenke 2015/08/31 17:41:29 Can't these two lines be combined, like in ReportD
amohammadkhan 2015/09/01 04:52:48 Our ideal was to have all dimensions for all servi
+ RecordUMAHistogramCount(service_histogram_name, message_size);
+ if (message_size > 0) {
+ RecordSparseHistogramWithValue(combined_histogram_name, service,
+ message_size);
+ }
+}
+
+void DataUseMeasurement::ReportDataUsageURLRequest(
+ ChromeTrafficType service_type,
+ TrafficDirection dir,
+ int64_t message_size) {
+ std::string histogram_name = base::StringPrintf(
+ "DataUse.%s%s", service_type == USER_TRAFFIC ? "User" : "NotUser",
+ GetDimensionForHistogramName(dir).c_str());
+ RecordUMAHistogramCount(histogram_name, message_size);
+}
+
+} // namespace data_use_measurement

Powered by Google App Engine
This is Rietveld 408576698