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

Side by Side Diff: chrome/browser/task_manager/sampling/task_manager_io_thread_helper.cc

Issue 2964543002: TaskManager: use an unordered_map for tracking network usage (Closed)
Patch Set: updated unit tests and changed some names Created 3 years, 5 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 "chrome/browser/task_manager/sampling/task_manager_io_thread_helper.h" 5 #include "chrome/browser/task_manager/sampling/task_manager_io_thread_helper.h"
6 6
7 #include "base/threading/thread_task_runner_handle.h"
7 #include "chrome/browser/task_manager/sampling/task_manager_impl.h" 8 #include "chrome/browser/task_manager/sampling/task_manager_impl.h"
8 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/resource_request_info.h"
10 10
11 namespace task_manager { 11 namespace task_manager {
12 12
13 namespace { 13 namespace {
14 14
15 TaskManagerIoThreadHelper* g_io_thread_helper = nullptr; 15 TaskManagerIoThreadHelper* g_io_thread_helper = nullptr;
16 16
17 } // namespace 17 } // namespace
18 18
19 IoThreadHelperManager::IoThreadHelperManager() { 19 size_t BytesTransferredKey::Hasher::operator()(
20 const BytesTransferredKey& key) const {
21 if (key.child_id != -1) {
22 return base::HashInts(key.child_id, key.route_id);
23 } else {
24 return std::hash<int>()(key.origin_pid);
25 }
26 }
27
28 bool BytesTransferredKey::operator==(const BytesTransferredKey& other) const {
29 return origin_pid == other.origin_pid && child_id == other.child_id &&
30 route_id == other.route_id;
31 }
32
33 IoThreadHelperManager::IoThreadHelperManager(
34 BytesTransferredCallback result_callback) {
20 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
21 36
22 content::BrowserThread::PostTask( 37 content::BrowserThread::PostTask(
23 content::BrowserThread::IO, 38 content::BrowserThread::IO, FROM_HERE,
24 FROM_HERE, 39 base::Bind(&TaskManagerIoThreadHelper::CreateInstance,
25 base::Bind(&TaskManagerIoThreadHelper::CreateInstance)); 40 std::move(result_callback)));
26 } 41 }
27 42
28 IoThreadHelperManager::~IoThreadHelperManager() { 43 IoThreadHelperManager::~IoThreadHelperManager() {
29 // This may be called at exit time when the main thread is no longer 44 // This may be called at exit time when the main thread is no longer
30 // registered as the UI thread. 45 // registered as the UI thread.
31 DCHECK( 46 DCHECK(
32 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) || 47 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ||
33 !content::BrowserThread::IsMessageLoopValid(content::BrowserThread::UI)); 48 !content::BrowserThread::IsMessageLoopValid(content::BrowserThread::UI));
34 49
35 content::BrowserThread::PostTask( 50 content::BrowserThread::PostTask(
36 content::BrowserThread::IO, 51 content::BrowserThread::IO,
37 FROM_HERE, 52 FROM_HERE,
38 base::Bind(&TaskManagerIoThreadHelper::DeleteInstance)); 53 base::Bind(&TaskManagerIoThreadHelper::DeleteInstance));
39 } 54 }
40 55
41 // static 56 // static
42 void TaskManagerIoThreadHelper::CreateInstance() { 57 void TaskManagerIoThreadHelper::CreateInstance(
58 BytesTransferredCallback result_callback) {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 59 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
44 DCHECK(!g_io_thread_helper); 60 DCHECK(!g_io_thread_helper);
45 61
46 g_io_thread_helper = new TaskManagerIoThreadHelper; 62 g_io_thread_helper =
63 new TaskManagerIoThreadHelper(std::move(result_callback));
47 } 64 }
48 65
49 // static 66 // static
50 void TaskManagerIoThreadHelper::DeleteInstance() { 67 void TaskManagerIoThreadHelper::DeleteInstance() {
51 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 68 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
52 69
53 delete g_io_thread_helper; 70 delete g_io_thread_helper;
54 g_io_thread_helper = nullptr; 71 g_io_thread_helper = nullptr;
55 } 72 }
56 73
57 // static 74 // static
58 void TaskManagerIoThreadHelper::OnRawBytesRead(const net::URLRequest& request, 75 void TaskManagerIoThreadHelper::OnRawBytesTransferred(BytesTransferredKey key,
59 int64_t bytes_read) { 76 int64_t bytes_read,
77 int64_t bytes_sent) {
60 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 78 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
61 79
62 if (g_io_thread_helper) { 80 if (g_io_thread_helper) {
afakhry 2017/07/05 19:01:21 Nit: Remove single-line block braces.
cburn 2017/07/06 17:30:41 Done.
63 int64_t bytes_sent = 0; 81 g_io_thread_helper->OnNetworkBytesTransferred(key, bytes_read, bytes_sent);
64 g_io_thread_helper->OnNetworkBytesTransferred(request, bytes_read,
65 bytes_sent);
66 } 82 }
67 } 83 }
68 84
69 // static 85 TaskManagerIoThreadHelper::TaskManagerIoThreadHelper(
70 void TaskManagerIoThreadHelper::OnRawBytesSent(const net::URLRequest& request, 86 BytesTransferredCallback result_callback)
71 int64_t bytes_sent) { 87 : result_callback_(std::move(result_callback)), weak_factory_(this) {
72 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
73
74 if (g_io_thread_helper) {
75 int64_t bytes_read = 0;
76 g_io_thread_helper->OnNetworkBytesTransferred(request, bytes_read,
77 bytes_sent);
78 }
79 }
80
81 TaskManagerIoThreadHelper::TaskManagerIoThreadHelper() : weak_factory_(this) {
82 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 88 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
83 } 89 }
84 90
85 TaskManagerIoThreadHelper::~TaskManagerIoThreadHelper() { 91 TaskManagerIoThreadHelper::~TaskManagerIoThreadHelper() {
86 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 92 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
87 } 93 }
88 94
89 void TaskManagerIoThreadHelper::OnMultipleBytesTransferredIO() { 95 void TaskManagerIoThreadHelper::OnMultipleBytesTransferredIO() {
90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 96 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
91 97 DCHECK(!bytes_transferred_unordered_map_.empty());
92 DCHECK(!bytes_transferred_buffer_.empty());
93
94 std::vector<BytesTransferredParam>* bytes_read_buffer =
95 new std::vector<BytesTransferredParam>();
96 bytes_transferred_buffer_.swap(*bytes_read_buffer);
97 98
98 content::BrowserThread::PostTask( 99 content::BrowserThread::PostTask(
99 content::BrowserThread::UI, FROM_HERE, 100 content::BrowserThread::UI, FROM_HERE,
100 base::Bind(&TaskManagerImpl::OnMultipleBytesTransferredUI, 101 base::Bind(result_callback_,
101 base::Owned(bytes_read_buffer))); 102 std::move(bytes_transferred_unordered_map_)));
103 bytes_transferred_unordered_map_.clear();
104 DCHECK(bytes_transferred_unordered_map_.empty());
102 } 105 }
103 106
104 void TaskManagerIoThreadHelper::OnNetworkBytesTransferred( 107 void TaskManagerIoThreadHelper::OnNetworkBytesTransferred(
105 const net::URLRequest& request, 108 BytesTransferredKey key,
106 int64_t bytes_read, 109 int64_t bytes_read,
107 int64_t bytes_sent) { 110 int64_t bytes_sent) {
108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 111 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
109 112
110 // Only net::URLRequestJob instances created by the ResourceDispatcherHost 113 if (bytes_transferred_unordered_map_.empty()) {
111 // have an associated ResourceRequestInfo and a render frame associated. 114 // Schedule a task to process the transferred bytes requests a second from
112 // All other jobs will have -1 returned for the render process child and 115 // now. We're trying to calculate the tasks' network usage speed as bytes
113 // routing ids - the jobs may still match a resource based on their origin id, 116 // per second so we collect as many requests during one seconds before the
114 // otherwise BytesRead() will attribute the activity to the Browser resource. 117 // below delayed TaskManagerIoThreadHelper::OnMultipleBytesReadIO() process
115 const content::ResourceRequestInfo* info = 118 // them after one second from now.
116 content::ResourceRequestInfo::ForRequest(&request); 119 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
117 int child_id = -1; 120 FROM_HERE,
118 int route_id = -1;
119 if (info)
120 info->GetAssociatedRenderFrame(&child_id, &route_id);
121
122 // Get the origin PID of the request's originator. This will only be set for
123 // plugins - for renderer or browser initiated requests it will be zero.
124 int origin_pid = info ? info->GetOriginPID() : 0;
125
126 if (bytes_transferred_buffer_.empty()) {
127 // Schedule a task to process the received bytes requests a second from now.
128 // We're trying to calculate the tasks' network usage speed as bytes per
129 // second so we collect as many requests during one seconds before the below
130 // delayed TaskManagerIoThreadHelper::OnMultipleBytesReadIO() process them
131 // after one second from now.
132 content::BrowserThread::PostDelayedTask(
133 content::BrowserThread::IO, FROM_HERE,
134 base::Bind(&TaskManagerIoThreadHelper::OnMultipleBytesTransferredIO, 121 base::Bind(&TaskManagerIoThreadHelper::OnMultipleBytesTransferredIO,
135 weak_factory_.GetWeakPtr()), 122 weak_factory_.GetWeakPtr()),
136 base::TimeDelta::FromSeconds(1)); 123 base::TimeDelta::FromSeconds(1));
137 } 124 }
138 125
139 bytes_transferred_buffer_.push_back(BytesTransferredParam( 126 BytesTransferredParam& entry = bytes_transferred_unordered_map_[key];
140 origin_pid, child_id, route_id, bytes_read, bytes_sent)); 127 entry.byte_read_count += bytes_read;
128 entry.byte_sent_count += bytes_sent;
141 } 129 }
142 130
143 } // namespace task_manager 131 } // namespace task_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698