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 // A callback that executes whenever there is activity that registers that |
| 67 // bytes have been transferred. It is called from task_manager_impl.cc binding |
| 68 // |OnMultipleBytesTranferred|. |
| 69 explicit IoThreadHelperManager(BytesTransferredCallback result_callback); |
59 ~IoThreadHelperManager(); | 70 ~IoThreadHelperManager(); |
60 | 71 |
61 private: | 72 private: |
62 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); | 73 DISALLOW_COPY_AND_ASSIGN(IoThreadHelperManager); |
63 }; | 74 }; |
64 | 75 |
65 // Defines a class used by the task manager to receive notifications of the | 76 // Defines a class used by the task manager to receive notifications of the |
66 // network bytes transferred by the various tasks. | 77 // network bytes transferred by the various tasks. |
67 // This object lives entirely only on the IO thread. | 78 // This object lives entirely only on the IO thread. |
68 class TaskManagerIoThreadHelper { | 79 class TaskManagerIoThreadHelper { |
69 public: | 80 public: |
70 // Create and delete the instance of this class. They must be called on the IO | 81 // Create and delete the instance of this class. They must be called on the IO |
71 // thread. | 82 // thread. |
72 static void CreateInstance(); | 83 static void CreateInstance(BytesTransferredCallback result_callback); |
73 static void DeleteInstance(); | 84 static void DeleteInstance(); |
74 | 85 |
75 // This is used to forward the call to update the network bytes with read | 86 // 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. | 87 // transferred bytes from the TaskManagerInterface. |
77 static void OnRawBytesRead(const net::URLRequest& request, | 88 static void OnRawBytesTransferred(BytesTransferredKey key, |
78 int64_t bytes_read); | 89 int64_t bytes_read, |
79 | 90 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 | 91 |
85 private: | 92 private: |
86 TaskManagerIoThreadHelper(); | 93 explicit TaskManagerIoThreadHelper(BytesTransferredCallback result_callback); |
87 ~TaskManagerIoThreadHelper(); | 94 ~TaskManagerIoThreadHelper(); |
88 | 95 |
89 // We gather multiple notifications on the IO thread in one second before a | 96 // 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 | 97 // call is made to the following function to start the processing for |
91 // transferred bytes. | 98 // transferred bytes. |
92 void OnMultipleBytesTransferredIO(); | 99 void OnMultipleBytesTransferredIO(); |
93 | 100 |
94 // This will update the task manager with the network bytes read. | 101 // This will update the task manager with the network bytes read. |
95 void OnNetworkBytesTransferred(const net::URLRequest& request, | 102 void OnNetworkBytesTransferred(BytesTransferredKey key, |
96 int64_t bytes_read, | 103 int64_t bytes_read, |
97 int64_t bytes_sent); | 104 int64_t bytes_sent); |
98 | 105 |
99 // This buffer will be filled on IO thread with information about the number | 106 // This unordered_map will be filled on IO thread with information about the |
100 // of bytes transferred from URLRequests. | 107 // number of bytes transferred from URLRequests. |
101 std::vector<BytesTransferredParam> bytes_transferred_buffer_; | 108 BytesTransferredMap bytes_transferred_unordered_map_; |
| 109 |
| 110 BytesTransferredCallback result_callback_; |
102 | 111 |
103 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; | 112 base::WeakPtrFactory<TaskManagerIoThreadHelper> weak_factory_; |
104 | 113 |
105 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); | 114 DISALLOW_COPY_AND_ASSIGN(TaskManagerIoThreadHelper); |
106 }; | 115 }; |
107 | 116 |
108 } // namespace task_manager | 117 } // namespace task_manager |
109 | 118 |
110 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ | 119 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IO_THREAD_HELPER_H_ |
OLD | NEW |