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. | |
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 BytesTransferredKey(int origin_pid, int child_id, int route_id) | |
43 : origin_pid(origin_pid), child_id(child_id), route_id(route_id) {} | |
ncarter (slow)
2017/06/29 23:40:00
If you erase this ctor, does everything still comp
cburn
2017/06/30 18:04:38
Done.
| |
44 struct Hasher { | |
45 size_t operator()(const BytesTransferredKey& key) const { | |
46 if (key.child_id != -1) { | |
47 return base::HashInts(key.child_id, key.route_id); | |
48 } else { | |
49 return std::hash<int>()(key.origin_pid); | |
50 } | |
51 } | |
52 }; | |
53 | |
54 bool operator==(const BytesTransferredKey& other) const { | |
55 return origin_pid == other.origin_pid && child_id == other.child_id && | |
56 route_id == other.route_id; | |
57 } | |
58 }; | |
59 | |
60 // This is the entry of the unordered map that tracks bytes transfered by task. | |
61 struct BytesTransferredParam { | |
36 // The number of bytes read. | 62 // The number of bytes read. |
37 int64_t byte_read_count; | 63 int64_t byte_read_count; |
ncarter (slow)
2017/06/29 23:40:01
byte_read_count = 0;
cburn
2017/06/30 18:04:38
Done.
| |
38 | 64 |
39 // The number of bytes sent. | 65 // The number of bytes sent. |
40 int64_t byte_sent_count; | 66 int64_t byte_sent_count; |
ncarter (slow)
2017/06/29 23:40:01
byte_sent_count = 0;
cburn
2017/06/30 18:04:38
Done.
| |
41 | 67 BytesTransferredParam() : byte_read_count(0), byte_sent_count(0) {} |
ncarter (slow)
2017/06/29 23:40:01
If you use the inline member initialization (sugge
cburn
2017/06/30 18:04:38
Done.
| |
42 BytesTransferredParam(int origin_pid, | 68 BytesTransferredParam(int64_t byte_read_count, int64_t byte_sent_count) |
ncarter (slow)
2017/06/29 23:40:01
I don't see the 2-arg ctor being used anywhere.
cburn
2017/06/30 18:04:38
Removed this, I think it was created before I unde
| |
43 int child_id, | 69 : byte_read_count(byte_read_count), byte_sent_count(byte_sent_count) {} |
44 int route_id, | |
45 int64_t byte_read_count, | |
46 int64_t byte_sent_count) | |
47 : origin_pid(origin_pid), | |
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 }; | 70 }; |
ncarter (slow)
2017/06/29 23:40:01
newline here
cburn
2017/06/30 18:04:38
Done.
| |
71 using BytesTransferredMap = std::unordered_map<BytesTransferredKey, | |
72 BytesTransferredParam, | |
73 BytesTransferredKey::Hasher>; | |
ncarter (slow)
2017/06/29 23:40:01
newline here
cburn
2017/06/30 18:04:38
Done.
| |
74 using BytesTransferredCallback = | |
75 base::RepeatingCallback<void(BytesTransferredMap)>; | |
53 | 76 |
54 // Defines a utility class used to schedule the creation and removal of the | 77 // Defines a utility class used to schedule the creation and removal of the |
55 // TaskManagerIoThreadHelper on the IO thread. | 78 // TaskManagerIoThreadHelper on the IO thread. |
56 class IoThreadHelperManager { | 79 class IoThreadHelperManager { |
57 public: | 80 public: |
58 IoThreadHelperManager(); | 81 IoThreadHelperManager(BytesTransferredCallback result_callback); |
ncarter (slow)
2017/06/29 23:40:01
add 'explicit' before IoThreadHelperManager, since
cburn
2017/06/30 18:04:38
Done.
| |
59 ~IoThreadHelperManager(); | 82 ~IoThreadHelperManager(); |
60 | 83 |
61 private: | 84 private: |
62 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); | 85 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); |
63 }; | 86 }; |
64 | 87 |
65 // Defines a class used by the task manager to receive notifications of the | 88 // Defines a class used by the task manager to receive notifications of the |
66 // network bytes transferred by the various tasks. | 89 // network bytes transferred by the various tasks. |
67 // This object lives entirely only on the IO thread. | 90 // This object lives entirely only on the IO thread. |
68 class TaskManagerIoThreadHelper { | 91 class TaskManagerIoThreadHelper { |
69 public: | 92 public: |
70 // Create and delete the instance of this class. They must be called on the IO | 93 // Create and delete the instance of this class. They must be called on the IO |
71 // thread. | 94 // thread. |
72 static void CreateInstance(); | 95 static void CreateInstance(BytesTransferredCallback result_callback); |
73 static void DeleteInstance(); | 96 static void DeleteInstance(); |
74 | 97 |
75 // This is used to forward the call to update the network bytes with read | 98 // 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. | 99 // transferred bytes from the TaskManagerInterface if the new task manager is |
77 static void OnRawBytesRead(const net::URLRequest& request, | 100 // enabled. |
78 int64_t bytes_read); | 101 static void OnRawBytesTransferred(BytesTransferredKey key, |
79 | 102 int64_t bytes_read, |
80 // This is used to forward the call to update the network bytes with sent | 103 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 | 104 |
85 private: | 105 private: |
86 TaskManagerIoThreadHelper(); | 106 TaskManagerIoThreadHelper(BytesTransferredCallback result_callback); |
ncarter (slow)
2017/06/29 23:40:00
add 'explicit' here too
cburn
2017/06/30 18:04:38
Done.
| |
87 ~TaskManagerIoThreadHelper(); | 107 ~TaskManagerIoThreadHelper(); |
88 | 108 |
89 // We gather multiple notifications on the IO thread in one second before a | 109 // 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 | 110 // call is made to the following function to start the processing for |
91 // transferred bytes. | 111 // transferred bytes. |
92 void OnMultipleBytesTransferredIO(); | 112 void OnMultipleBytesTransferredIO(); |
93 | 113 |
94 // This will update the task manager with the network bytes read. | 114 // This will update the task manager with the network bytes read. |
95 void OnNetworkBytesTransferred(const net::URLRequest& request, | 115 void OnNetworkBytesTransferred(BytesTransferredKey key, |
96 int64_t bytes_read, | 116 int64_t bytes_read, |
97 int64_t bytes_sent); | 117 int64_t bytes_sent); |
98 | 118 |
99 // This buffer will be filled on IO thread with information about the number | 119 // This unordered_map will be filled on IO thread with information about the |
100 // of bytes transferred from URLRequests. | 120 // number of bytes transferred from URLRequests. |
101 std::vector<BytesTransferredParam> bytes_transferred_buffer_; | 121 |
122 BytesTransferredMap bytes_transferred_unordered_map_; | |
123 | |
124 BytesTransferredCallback result_callback_; | |
102 | 125 |
103 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; | 126 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; |
104 | 127 |
105 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); | 128 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); |
106 }; | 129 }; |
107 | 130 |
108 } // namespace task_manager | 131 } // namespace task_manager |
109 | 132 |
110 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ | 133 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ |
OLD | NEW |