Chromium Code Reviews| 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 #ifndef CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ | 5 #ifndef CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ |
| 6 #define CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ | 6 #define CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <unordered_map> |
| 11 | 11 |
| 12 #include "base/callback.h" | |
| 13 #include "base/hash.h" | |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 14 | 16 |
| 15 namespace net { | 17 namespace net { |
| 16 class URLRequest; | 18 class URLRequest; |
| 17 } // namespace net | 19 } // namespace net |
| 18 | 20 |
| 19 namespace task_manager { | 21 namespace task_manager { |
| 20 | 22 |
| 21 // Defines a wrapper of values that will be sent from IO to UI thread upon | 23 // Identifies the initiator of a network request, either by a (child_id, |
| 22 // reception and transmission of bytes notifications. | 24 // route_id) tuple, and/or via an OS process id. |
| 23 struct BytesTransferredParam { | 25 // BytesTransferredKey supports hashing and may be used as an unordered_map key. |
| 26 struct BytesTransferredKey { | |
| 24 // The PID of the originating process of the URLRequest, if the request is | 27 // The PID of the originating process of the URLRequest, if the request is |
| 25 // sent on behalf of another process. Otherwise it's 0. | 28 // sent on behalf of another process. Otherwise it's 0. |
| 26 int origin_pid; | 29 int origin_pid; |
| 27 | 30 |
| 28 // The unique ID of the host of the child process requester. | 31 // The unique ID of the host of the child process requester. |
| 29 int child_id; | 32 int child_id; |
| 30 | 33 |
| 31 // The ID of the IPC route for the URLRequest (this identifies the | 34 // The ID of the IPC route for the URLRequest (this identifies the |
| 32 // RenderView or like-thing in the renderer that the request gets routed | 35 // RenderView or like-thing in the renderer that the request gets routed |
| 33 // to). | 36 // to). |
| 34 int route_id; | 37 int route_id; |
| 35 | 38 |
| 39 struct Hasher { | |
| 40 size_t operator()(const BytesTransferredKey& key) const; | |
| 41 }; | |
| 42 | |
| 43 bool operator==(const BytesTransferredKey& other) const; | |
| 44 }; | |
| 45 | |
| 46 // This is the entry of the unordered map that tracks bytes transfered by task. | |
| 47 struct BytesTransferredParam { | |
| 36 // The number of bytes read. | 48 // The number of bytes read. |
| 37 int64_t byte_read_count; | 49 int64_t byte_read_count = 0; |
| 38 | 50 |
| 39 // The number of bytes sent. | 51 // The number of bytes sent. |
| 40 int64_t byte_sent_count; | 52 int64_t byte_sent_count = 0; |
| 53 }; | |
| 41 | 54 |
| 42 BytesTransferredParam(int origin_pid, | 55 using BytesTransferredMap = std::unordered_map<BytesTransferredKey, |
| 43 int child_id, | 56 BytesTransferredParam, |
| 44 int route_id, | 57 BytesTransferredKey::Hasher>; |
| 45 int64_t byte_read_count, | 58 |
| 46 int64_t byte_sent_count) | 59 using BytesTransferredCallback = |
| 47 : origin_pid(origin_pid), | 60 base::RepeatingCallback<void(BytesTransferredMap)>; |
| 48 child_id(child_id), | |
| 49 route_id(route_id), | |
| 50 byte_read_count(byte_read_count), | |
| 51 byte_sent_count(byte_sent_count) {} | |
| 52 }; | |
| 53 | 61 |
| 54 // Defines a utility class used to schedule the creation and removal of the | 62 // Defines a utility class used to schedule the creation and removal of the |
| 55 // TaskManagerIoThreadHelper on the IO thread. | 63 // TaskManagerIoThreadHelper on the IO thread. |
| 56 class IoThreadHelperManager { | 64 class IoThreadHelperManager { |
| 57 public: | 65 public: |
| 58 IoThreadHelperManager(); | 66 explicit IoThreadHelperManager(BytesTransferredCallback result_callback); |
|
afakhry
2017/07/05 19:01:21
Nit: Please document when this callback will be in
cburn
2017/07/06 17:30:41
Done.
| |
| 59 ~IoThreadHelperManager(); | 67 ~IoThreadHelperManager(); |
| 60 | 68 |
| 61 private: | 69 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); | 70 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); |
| 63 }; | 71 }; |
| 64 | 72 |
| 65 // Defines a class used by the task manager to receive notifications of the | 73 // Defines a class used by the task manager to receive notifications of the |
| 66 // network bytes transferred by the various tasks. | 74 // network bytes transferred by the various tasks. |
| 67 // This object lives entirely only on the IO thread. | 75 // This object lives entirely only on the IO thread. |
| 68 class TaskManagerIoThreadHelper { | 76 class TaskManagerIoThreadHelper { |
| 69 public: | 77 public: |
| 70 // Create and delete the instance of this class. They must be called on the IO | 78 // Create and delete the instance of this class. They must be called on the IO |
| 71 // thread. | 79 // thread. |
| 72 static void CreateInstance(); | 80 static void CreateInstance(BytesTransferredCallback result_callback); |
| 73 static void DeleteInstance(); | 81 static void DeleteInstance(); |
| 74 | 82 |
| 75 // This is used to forward the call to update the network bytes with read | 83 // This is used to forward the call to update the network bytes with |
| 76 // bytes from the TaskManagerInterface if the new task manager is enabled. | 84 // transferred bytes from the TaskManagerInterface. |
| 77 static void OnRawBytesRead(const net::URLRequest& request, | 85 static void OnRawBytesTransferred(BytesTransferredKey key, |
| 78 int64_t bytes_read); | 86 int64_t bytes_read, |
| 79 | 87 int64_t bytes_sent); |
| 80 // This is used to forward the call to update the network bytes with sent | |
| 81 // bytes from the TaskManagerInterface if the new task manager is enabled. | |
| 82 static void OnRawBytesSent(const net::URLRequest& request, | |
| 83 int64_t bytes_sent); | |
| 84 | 88 |
| 85 private: | 89 private: |
| 86 TaskManagerIoThreadHelper(); | 90 explicit TaskManagerIoThreadHelper(BytesTransferredCallback result_callback); |
| 87 ~TaskManagerIoThreadHelper(); | 91 ~TaskManagerIoThreadHelper(); |
| 88 | 92 |
| 89 // We gather multiple notifications on the IO thread in one second before a | 93 // We gather multiple notifications on the IO thread in one second before a |
| 90 // call is made to the following function to start the processing for | 94 // call is made to the following function to start the processing for |
| 91 // transferred bytes. | 95 // transferred bytes. |
| 92 void OnMultipleBytesTransferredIO(); | 96 void OnMultipleBytesTransferredIO(); |
| 93 | 97 |
| 94 // This will update the task manager with the network bytes read. | 98 // This will update the task manager with the network bytes read. |
| 95 void OnNetworkBytesTransferred(const net::URLRequest& request, | 99 void OnNetworkBytesTransferred(BytesTransferredKey key, |
| 96 int64_t bytes_read, | 100 int64_t bytes_read, |
| 97 int64_t bytes_sent); | 101 int64_t bytes_sent); |
| 98 | 102 |
| 99 // This buffer will be filled on IO thread with information about the number | 103 // This unordered_map will be filled on IO thread with information about the |
| 100 // of bytes transferred from URLRequests. | 104 // number of bytes transferred from URLRequests. |
| 101 std::vector<BytesTransferredParam> bytes_transferred_buffer_; | 105 BytesTransferredMap bytes_transferred_unordered_map_; |
| 106 | |
| 107 BytesTransferredCallback result_callback_; | |
| 102 | 108 |
| 103 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; | 109 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; |
| 104 | 110 |
| 105 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); | 111 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); |
| 106 }; | 112 }; |
| 107 | 113 |
| 108 } // namespace task_manager | 114 } // namespace task_manager |
| 109 | 115 |
| 110 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ | 116 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ |
| OLD | NEW |