| OLD | NEW |
| 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 #include "content/public/browser/resource_request_info.h" |
| 10 | 11 |
| 11 namespace task_manager { | 12 namespace task_manager { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 TaskManagerIoThreadHelper* g_io_thread_helper = nullptr; | 16 TaskManagerIoThreadHelper* g_io_thread_helper = nullptr; |
| 16 | 17 |
| 17 } // namespace | 18 } // namespace |
| 18 | 19 |
| 19 IoThreadHelperManager::IoThreadHelperManager() { | 20 IoThreadHelperManager::IoThreadHelperManager( |
| 21 BytesTransferredCallback result_callback) { |
| 20 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 21 | 23 |
| 22 content::BrowserThread::PostTask( | 24 content::BrowserThread::PostTask( |
| 23 content::BrowserThread::IO, | 25 content::BrowserThread::IO, FROM_HERE, |
| 24 FROM_HERE, | 26 base::Bind(&TaskManagerIoThreadHelper::CreateInstance, |
| 25 base::Bind(&TaskManagerIoThreadHelper::CreateInstance)); | 27 std::move(result_callback))); |
| 26 } | 28 } |
| 27 | 29 |
| 28 IoThreadHelperManager::~IoThreadHelperManager() { | 30 IoThreadHelperManager::~IoThreadHelperManager() { |
| 29 // This may be called at exit time when the main thread is no longer | 31 // This may be called at exit time when the main thread is no longer |
| 30 // registered as the UI thread. | 32 // registered as the UI thread. |
| 31 DCHECK( | 33 DCHECK( |
| 32 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) || | 34 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) || |
| 33 !content::BrowserThread::IsMessageLoopValid(content::BrowserThread::UI)); | 35 !content::BrowserThread::IsMessageLoopValid(content::BrowserThread::UI)); |
| 34 | 36 |
| 35 content::BrowserThread::PostTask( | 37 content::BrowserThread::PostTask( |
| 36 content::BrowserThread::IO, | 38 content::BrowserThread::IO, |
| 37 FROM_HERE, | 39 FROM_HERE, |
| 38 base::Bind(&TaskManagerIoThreadHelper::DeleteInstance)); | 40 base::Bind(&TaskManagerIoThreadHelper::DeleteInstance)); |
| 39 } | 41 } |
| 40 | 42 |
| 41 // static | 43 // static |
| 42 void TaskManagerIoThreadHelper::CreateInstance() { | 44 void TaskManagerIoThreadHelper::CreateInstance( |
| 45 BytesTransferredCallback result_callback) { |
| 43 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 46 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 44 DCHECK(!g_io_thread_helper); | 47 DCHECK(!g_io_thread_helper); |
| 45 | 48 |
| 46 g_io_thread_helper = new TaskManagerIoThreadHelper; | 49 g_io_thread_helper = |
| 50 new TaskManagerIoThreadHelper(std::move(result_callback)); |
| 47 } | 51 } |
| 48 | 52 |
| 49 // static | 53 // static |
| 50 void TaskManagerIoThreadHelper::DeleteInstance() { | 54 void TaskManagerIoThreadHelper::DeleteInstance() { |
| 51 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 55 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 52 | 56 |
| 53 delete g_io_thread_helper; | 57 delete g_io_thread_helper; |
| 54 g_io_thread_helper = nullptr; | 58 g_io_thread_helper = nullptr; |
| 55 } | 59 } |
| 56 | 60 |
| 57 // static | 61 // static |
| 58 void TaskManagerIoThreadHelper::OnRawBytesRead(const net::URLRequest& request, | 62 void TaskManagerIoThreadHelper::OnRawBytesTransferred(BytesTransferredKey key, |
| 59 int64_t bytes_read) { | 63 int64_t bytes_read, |
| 64 int64_t bytes_sent) { |
| 60 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 65 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 61 | 66 |
| 62 if (g_io_thread_helper) { | 67 if (g_io_thread_helper) { |
| 63 int64_t bytes_sent = 0; | 68 g_io_thread_helper->OnNetworkBytesTransferred(key, bytes_read, bytes_sent); |
| 64 g_io_thread_helper->OnNetworkBytesTransferred(request, bytes_read, | |
| 65 bytes_sent); | |
| 66 } | 69 } |
| 67 } | 70 } |
| 68 | 71 |
| 69 // static | 72 TaskManagerIoThreadHelper::TaskManagerIoThreadHelper( |
| 70 void TaskManagerIoThreadHelper::OnRawBytesSent(const net::URLRequest& request, | 73 BytesTransferredCallback result_callback) |
| 71 int64_t bytes_sent) { | 74 : 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); | 75 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 83 } | 76 } |
| 84 | 77 |
| 85 TaskManagerIoThreadHelper::~TaskManagerIoThreadHelper() { | 78 TaskManagerIoThreadHelper::~TaskManagerIoThreadHelper() { |
| 86 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 79 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 87 } | 80 } |
| 88 | 81 |
| 89 void TaskManagerIoThreadHelper::OnMultipleBytesTransferredIO() { | 82 void TaskManagerIoThreadHelper::OnMultipleBytesTransferredIO() { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 91 | 84 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 | 85 |
| 98 content::BrowserThread::PostTask( | 86 content::BrowserThread::PostTask( |
| 99 content::BrowserThread::UI, FROM_HERE, | 87 content::BrowserThread::UI, FROM_HERE, |
| 100 base::Bind(&TaskManagerImpl::OnMultipleBytesTransferredUI, | 88 base::Bind(result_callback_, |
| 101 base::Owned(bytes_read_buffer))); | 89 std::move(bytes_transferred_unordered_map_))); |
| 90 bytes_transferred_unordered_map_.clear(); |
| 91 DCHECK(bytes_transferred_unordered_map_.empty()); |
| 102 } | 92 } |
| 103 | 93 |
| 104 void TaskManagerIoThreadHelper::OnNetworkBytesTransferred( | 94 void TaskManagerIoThreadHelper::OnNetworkBytesTransferred( |
| 105 const net::URLRequest& request, | 95 BytesTransferredKey key, |
| 106 int64_t bytes_read, | 96 int64_t bytes_read, |
| 107 int64_t bytes_sent) { | 97 int64_t bytes_sent) { |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 98 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 109 | 99 |
| 110 // Only net::URLRequestJob instances created by the ResourceDispatcherHost | 100 if (bytes_transferred_unordered_map_.empty()) { |
| 111 // have an associated ResourceRequestInfo and a render frame associated. | 101 // 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 | 102 // 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, | 103 // per second so we collect as many requests during one seconds before the |
| 114 // otherwise BytesRead() will attribute the activity to the Browser resource. | 104 // below delayed TaskManagerIoThreadHelper::OnMultipleBytesReadIO() process |
| 115 const content::ResourceRequestInfo* info = | 105 // them after one second from now. |
| 116 content::ResourceRequestInfo::ForRequest(&request); | 106 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 117 int child_id = -1; | 107 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, | 108 base::Bind(&TaskManagerIoThreadHelper::OnMultipleBytesTransferredIO, |
| 135 weak_factory_.GetWeakPtr()), | 109 weak_factory_.GetWeakPtr()), |
| 136 base::TimeDelta::FromSeconds(1)); | 110 base::TimeDelta::FromSeconds(1)); |
| 137 } | 111 } |
| 138 | 112 |
| 139 bytes_transferred_buffer_.push_back(BytesTransferredParam( | 113 BytesTransferredParam& entry = bytes_transferred_unordered_map_[key]; |
| 140 origin_pid, child_id, route_id, bytes_read, bytes_sent)); | 114 entry.byte_read_count += bytes_read; |
| 115 entry.byte_sent_count += bytes_sent; |
| 141 } | 116 } |
| 142 | 117 |
| 143 } // namespace task_manager | 118 } // namespace task_manager |
| OLD | NEW |