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 // This will be the key that is used in an unordered_map to track the number of |
| 22 // reception and transmission of bytes notifications. | 24 // bytes sent and read by a task. The hash function either does a hash of the |
| 23 struct BytesTransferredParam { | 25 // |child_id| and the |route_id| if the task is distinguished that way or it is |
| 26 // hashed by the |origin_pid|. The operator== is defined to be true when all | |
| 27 // |origin_pid|, |child_id|, and |route_id| are all the same between the | |
| 28 // compared keys. | |
|
ncarter (slow)
2017/06/30 20:08:28
This comment falls somewhat into the trap of "desc
cburn
2017/07/05 16:30:06
Done.
| |
| 29 struct BytesTransferredKey { | |
| 24 // The PID of the originating process of the URLRequest, if the request is | 30 // The PID of the originating process of the URLRequest, if the request is |
| 25 // sent on behalf of another process. Otherwise it's 0. | 31 // sent on behalf of another process. Otherwise it's 0. |
| 26 int origin_pid; | 32 int origin_pid; |
| 27 | 33 |
| 28 // The unique ID of the host of the child process requester. | 34 // The unique ID of the host of the child process requester. |
| 29 int child_id; | 35 int child_id; |
| 30 | 36 |
| 31 // The ID of the IPC route for the URLRequest (this identifies the | 37 // 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 | 38 // RenderView or like-thing in the renderer that the request gets routed |
| 33 // to). | 39 // to). |
| 34 int route_id; | 40 int route_id; |
| 35 | 41 |
| 42 struct Hasher { | |
| 43 size_t operator()(const BytesTransferredKey& key) const { | |
|
ncarter (slow)
2017/06/30 20:08:28
This is nontrivial, I'd move it to the cc file ins
cburn
2017/07/05 16:30:06
Done.
| |
| 44 if (key.child_id != -1) { | |
| 45 return base::HashInts(key.child_id, key.route_id); | |
| 46 } else { | |
| 47 return std::hash<int>()(key.origin_pid); | |
| 48 } | |
| 49 } | |
| 50 }; | |
| 51 | |
| 52 bool operator==(const BytesTransferredKey& other) const { | |
|
ncarter (slow)
2017/06/30 20:08:28
Move this to the cc file too
cburn
2017/07/05 16:30:06
Done.
| |
| 53 return origin_pid == other.origin_pid && child_id == other.child_id && | |
| 54 route_id == other.route_id; | |
| 55 } | |
| 56 }; | |
| 57 | |
| 58 // This is the entry of the unordered map that tracks bytes transfered by task. | |
| 59 struct BytesTransferredParam { | |
| 36 // The number of bytes read. | 60 // The number of bytes read. |
| 37 int64_t byte_read_count; | 61 int64_t byte_read_count = 0; |
| 38 | 62 |
| 39 // The number of bytes sent. | 63 // The number of bytes sent. |
| 40 int64_t byte_sent_count; | 64 int64_t byte_sent_count = 0; |
| 65 }; | |
| 41 | 66 |
| 42 BytesTransferredParam(int origin_pid, | 67 using BytesTransferredMap = std::unordered_map<BytesTransferredKey, |
| 43 int child_id, | 68 BytesTransferredParam, |
| 44 int route_id, | 69 BytesTransferredKey::Hasher>; |
| 45 int64_t byte_read_count, | 70 |
| 46 int64_t byte_sent_count) | 71 using BytesTransferredCallback = |
| 47 : origin_pid(origin_pid), | 72 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 | 73 |
| 54 // Defines a utility class used to schedule the creation and removal of the | 74 // Defines a utility class used to schedule the creation and removal of the |
| 55 // TaskManagerIoThreadHelper on the IO thread. | 75 // TaskManagerIoThreadHelper on the IO thread. |
| 56 class IoThreadHelperManager { | 76 class IoThreadHelperManager { |
| 57 public: | 77 public: |
| 58 IoThreadHelperManager(); | 78 explicit IoThreadHelperManager(BytesTransferredCallback result_callback); |
| 59 ~IoThreadHelperManager(); | 79 ~IoThreadHelperManager(); |
| 60 | 80 |
| 61 private: | 81 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); | 82 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); |
| 63 }; | 83 }; |
| 64 | 84 |
| 65 // Defines a class used by the task manager to receive notifications of the | 85 // Defines a class used by the task manager to receive notifications of the |
| 66 // network bytes transferred by the various tasks. | 86 // network bytes transferred by the various tasks. |
| 67 // This object lives entirely only on the IO thread. | 87 // This object lives entirely only on the IO thread. |
| 68 class TaskManagerIoThreadHelper { | 88 class TaskManagerIoThreadHelper { |
| 69 public: | 89 public: |
| 70 // Create and delete the instance of this class. They must be called on the IO | 90 // Create and delete the instance of this class. They must be called on the IO |
| 71 // thread. | 91 // thread. |
| 72 static void CreateInstance(); | 92 static void CreateInstance(BytesTransferredCallback result_callback); |
| 73 static void DeleteInstance(); | 93 static void DeleteInstance(); |
| 74 | 94 |
| 75 // This is used to forward the call to update the network bytes with read | 95 // 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. | 96 // transferred bytes from the TaskManagerInterface if the new task manager is |
| 77 static void OnRawBytesRead(const net::URLRequest& request, | 97 // enabled. |
|
ncarter (slow)
2017/06/30 20:08:28
not your code but while we're here, let's delete t
cburn
2017/07/05 16:30:06
Done.
| |
| 78 int64_t bytes_read); | 98 static void OnRawBytesTransferred(BytesTransferredKey key, |
| 79 | 99 int64_t bytes_read, |
| 80 // This is used to forward the call to update the network bytes with sent | 100 int64_t bytes_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 | 101 |
| 85 private: | 102 private: |
| 86 TaskManagerIoThreadHelper(); | 103 explicit TaskManagerIoThreadHelper(BytesTransferredCallback result_callback); |
| 87 ~TaskManagerIoThreadHelper(); | 104 ~TaskManagerIoThreadHelper(); |
| 88 | 105 |
| 89 // We gather multiple notifications on the IO thread in one second before a | 106 // 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 | 107 // call is made to the following function to start the processing for |
| 91 // transferred bytes. | 108 // transferred bytes. |
| 92 void OnMultipleBytesTransferredIO(); | 109 void OnMultipleBytesTransferredIO(); |
| 93 | 110 |
| 94 // This will update the task manager with the network bytes read. | 111 // This will update the task manager with the network bytes read. |
| 95 void OnNetworkBytesTransferred(const net::URLRequest& request, | 112 void OnNetworkBytesTransferred(BytesTransferredKey key, |
| 96 int64_t bytes_read, | 113 int64_t bytes_read, |
| 97 int64_t bytes_sent); | 114 int64_t bytes_sent); |
| 98 | 115 |
| 99 // This buffer will be filled on IO thread with information about the number | 116 // This unordered_map will be filled on IO thread with information about the |
| 100 // of bytes transferred from URLRequests. | 117 // number of bytes transferred from URLRequests. |
| 101 std::vector<BytesTransferredParam> bytes_transferred_buffer_; | 118 |
|
ncarter (slow)
2017/06/30 20:08:29
Remove this newline; the comment should adhere to
cburn
2017/07/05 16:30:06
Done.
| |
| 119 BytesTransferredMap bytes_transferred_unordered_map_; | |
| 120 | |
| 121 BytesTransferredCallback result_callback_; | |
| 102 | 122 |
| 103 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; | 123 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; |
| 104 | 124 |
| 105 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); | 125 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); |
| 106 }; | 126 }; |
| 107 | 127 |
| 108 } // namespace task_manager | 128 } // namespace task_manager |
| 109 | 129 |
| 110 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ | 130 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ |
| OLD | NEW |