OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_METRICS_TRACKING_SYNCHRONIZER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
6 #define CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 6 #define CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/lazy_instance.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
16 #include "base/time.h" | |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/ui/webui/profiler_ui.h" | 18 #include "chrome/browser/ui/webui/profiler_ui.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/profiler_subscriber.h" |
20 #include "content/public/common/process_type.h" | |
21 | 20 |
22 // This class maintains state that is used to upload tracking data from the | 21 // This class maintains state that is used to upload profiler data from the |
23 // various processes, into the browser process. Such transactions are usually | 22 // various processes, into the browser process. Such transactions are usually |
24 // instigated by the browser. In general, a process will respond by gathering | 23 // instigated by the browser. In general, a process will respond by gathering |
25 // tracking data, and transmitting the pickled tracking data. We collect the | 24 // profiler data, and transmitting the pickled profiler data. We collect the |
26 // data in asynchronous mode that doesn't block the UI thread. | 25 // data in asynchronous mode that doesn't block the UI thread. |
27 // | 26 // |
28 // To assure that all the processes have responded, a counter is maintained | 27 // To assure that all the processes have responded, a counter is maintained |
29 // to indicate the number of pending (not yet responsive) processes. We tag | 28 // to indicate the number of pending (not yet responsive) processes. We tag |
30 // each group of requests with a sequence number. For each group of requests, we | 29 // each group of requests with a sequence number. For each group of requests, we |
31 // create RequestContext object which stores the sequence number, pending | 30 // create RequestContext object which stores the sequence number, pending |
32 // processes and the callback_object that needs to be notified when we receive | 31 // processes and the callback_object that needs to be notified when we receive |
33 // an update from processes. When an update arrives we find the RequestContext | 32 // an update from processes. When an update arrives we find the RequestContext |
34 // associated with sequence number and send the unpickled tracking data to the | 33 // associated with sequence number and send the unpickled profiler data to the |
35 // |callback_object_|. | 34 // |callback_object_|. |
36 | 35 |
37 namespace chrome_browser_metrics { | 36 namespace chrome_browser_metrics { |
38 | 37 |
39 class TrackingSynchronizer : public | 38 class RequestContext; |
40 base::RefCountedThreadSafe<TrackingSynchronizer> { | 39 |
| 40 class TrackingSynchronizer |
| 41 : public content::ProfilerSubscriber, |
| 42 public base::RefCountedThreadSafe<TrackingSynchronizer> { |
41 public: | 43 public: |
42 // The "RequestContext" structure describes an individual request received | |
43 // from the UI. | |
44 struct RequestContext { | |
45 RequestContext(const base::WeakPtr<ProfilerUI>& callback_object, | |
46 int sequence_number, | |
47 int processes_pending, | |
48 base::TimeTicks callback_start_time) | |
49 : callback_object_(callback_object), | |
50 sequence_number_(sequence_number), | |
51 processes_pending_(processes_pending), | |
52 request_start_time_(callback_start_time) { | |
53 } | |
54 | |
55 ~RequestContext() {} | |
56 | |
57 // Requests are made to asynchronously send data to the |callback_object_|. | |
58 base::WeakPtr<ProfilerUI> callback_object_; | |
59 | |
60 // The sequence number used by the most recent update request to contact all | |
61 // processes. | |
62 int sequence_number_; | |
63 | |
64 // The number of processes that have not yet responded to requests. | |
65 int processes_pending_; | |
66 | |
67 // The time when we were told to start the fetching of data from processes. | |
68 base::TimeTicks request_start_time_; | |
69 }; | |
70 | |
71 // A map from sequence_number_ to the actual RequestContexts. | |
72 typedef std::map<int, RequestContext*> RequestContextMap; | |
73 | |
74 // Construction also sets up the global singleton instance. This instance is | 44 // Construction also sets up the global singleton instance. This instance is |
75 // used to communicate between the IO and UI thread, and is destroyed only as | 45 // used to communicate between the IO and UI thread, and is destroyed only as |
76 // the main thread (browser_main) terminates, which means the IO thread has | 46 // the main thread (browser_main) terminates, which means the IO thread has |
77 // already completed, and will not need this instance any further. | 47 // already completed, and will not need this instance any further. |
78 TrackingSynchronizer(); | 48 TrackingSynchronizer(); |
79 | 49 |
80 // Return pointer to the singleton instance, which is allocated and | |
81 // deallocated on the main UI thread (during system startup and teardown). | |
82 static TrackingSynchronizer* CurrentSynchronizer(); | |
83 | |
84 // Contact all processes, and get them to upload to the browser any/all | 50 // Contact all processes, and get them to upload to the browser any/all |
85 // changes to tracking data. It calls |callback_object|'s SetData method with | 51 // changes to profiler data. It calls |callback_object|'s SetData method with |
86 // the data received from each sub-process. | 52 // the data received from each sub-process. |
87 // This method is accessible on UI thread. | 53 // This method is accessible on UI thread. |
88 static void FetchTrackingDataAsynchronously( | 54 static void FetchProfilerDataAsynchronously( |
89 const base::WeakPtr<ProfilerUI>& callback_object); | 55 const base::WeakPtr<ProfilerUI>& callback_object); |
90 | 56 |
91 // Contact all processes and set tracking status to |enable|. | 57 // ------------------------------------------------------ |
92 // This method is accessible on UI thread. | 58 // ProfilerSubscriber methods for browser child processes |
93 static void SetTrackingStatus(bool enable); | 59 // ------------------------------------------------------ |
94 | 60 |
95 // Respond to this message from the renderer by setting the tracking status | 61 // Update the number of pending processes for the given |sequence_number|. |
96 // (SetTrackingStatusInProcess) in that renderer process. | 62 // This is called on UI thread. |
97 // |process_id| is used to find the renderer process. | 63 virtual void OnPendingProcesses(int sequence_number, |
98 // This method is accessible on IO thread. | 64 int pending_processes, |
99 static void IsTrackingEnabled(int process_id); | 65 bool end) OVERRIDE; |
100 | 66 |
101 // Get the current tracking status from the browser process and set it in the | 67 // Send profiler_data back to callback_object_ by calling |
102 // renderer process. |process_id| is used to find the renderer process. | 68 // DecrementPendingProcessesAndSendData which records that we are waiting |
103 // This method is accessible on UI thread. | 69 // for one less profiler data from renderer or browser child process for the |
104 static void SetTrackingStatusInProcess(int process_id); | 70 // given sequence number. This method is accessible on UI thread. |
105 | 71 virtual void OnProfilerDataCollected( |
106 // Deserialize the tracking data and record that we have received tracking | |
107 // data from a process. This method posts a task to call | |
108 // DeserializeTrackingListOnUI on UI thread to send the |tracking_data| to | |
109 // callback_object_. This method is accessible on IO thread. | |
110 static void DeserializeTrackingList( | |
111 int sequence_number, | 72 int sequence_number, |
112 const std::string& tracking_data, | 73 base::DictionaryValue* profiler_data) OVERRIDE; |
113 content::ProcessType process_type); | |
114 | |
115 // Deserialize the tracking data and record that we have received tracking | |
116 // data from a process. This method is accessible on UI thread. | |
117 static void DeserializeTrackingListOnUI( | |
118 int sequence_number, | |
119 const std::string& tracking_data, | |
120 content::ProcessType process_type); | |
121 | 74 |
122 private: | 75 private: |
123 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; | 76 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
| 77 friend class RequestContext; |
124 | 78 |
125 virtual ~TrackingSynchronizer(); | 79 virtual ~TrackingSynchronizer(); |
126 | 80 |
| 81 // Send profiler_data back to callback_object_. It records that we are waiting |
| 82 // for one less profiler data from renderer or browser child process for the |
| 83 // given sequence number. This method is accessible on UI thread. |
| 84 void OnProfilerDataCollectedOnUI(int sequence_number, |
| 85 base::DictionaryValue* profiler_data); |
| 86 |
127 // Establish a new sequence_number_, and use it to notify all the processes of | 87 // Establish a new sequence_number_, and use it to notify all the processes of |
128 // the need to supply, to the browser, their tracking data. It also registers | 88 // the need to supply, to the browser, their tracking data. It also registers |
129 // |callback_object| in |outstanding_requests_| map. Return the | 89 // |callback_object| in |outstanding_requests_| map. Return the |
130 // sequence_number_ that was used. This method is accessible on UI thread. | 90 // sequence_number_ that was used. This method is accessible on UI thread. |
131 int RegisterAndNotifyAllProcesses( | 91 int RegisterAndNotifyAllProcesses( |
132 const base::WeakPtr<ProfilerUI>& callback_object); | 92 const base::WeakPtr<ProfilerUI>& callback_object); |
133 | 93 |
134 // It finds the |callback_object_| in |outstanding_requests_| map for the | 94 // It finds the RequestContext for the given |sequence_number| and notifies |
135 // given |sequence_number| and notifies the |callback_object_| about the | 95 // the RequestContext's |callback_object_| about the |value|. This is called |
136 // |value|. This is called whenever we receive tracked data from processes. It | 96 // whenever we receive profiler data from processes. It also records that we |
137 // also records that we are waiting for one less tracking data from a process | 97 // are waiting for one less profiler data from a process for the given |
138 // for the given sequence number. If we have received a response from all | 98 // sequence number. If we have received a response from all renderers and |
139 // renderers, then it deletes the entry for sequence_number from | 99 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete |
140 // |outstanding_requests_| map. This method is accessible on UI thread. | 100 // the entry for sequence_number. This method is accessible on UI thread. |
141 void DecrementPendingProcessesAndSendData(int sequence_number, | 101 void DecrementPendingProcessesAndSendData(int sequence_number, |
142 base::DictionaryValue* value); | 102 base::DictionaryValue* value); |
143 | 103 |
144 // Records that we are waiting for one less tracking data from a process for | |
145 // the given sequence number. | |
146 // This method is accessible on UI thread. | |
147 void DecrementPendingProcesses(int sequence_number); | |
148 | |
149 // When all changes have been acquired, or when the wait time expires | |
150 // (whichever is sooner), this method is called. This method deletes the entry | |
151 // for the given sequence_number from |outstanding_requests_| map. | |
152 // This method is accessible on UI thread. | |
153 void ForceTrackingSynchronizationDoneCallback(int sequence_number); | |
154 | |
155 // Get a new sequence number to be sent to processes from browser process. | 104 // Get a new sequence number to be sent to processes from browser process. |
156 // This method is accessible on UI thread. | 105 // This method is accessible on UI thread. |
157 int GetNextAvailableSequenceNumber(); | 106 int GetNextAvailableSequenceNumber(); |
158 | 107 |
159 // Map of all outstanding RequestContexts, from sequence_number_ to | 108 // Return pointer to the singleton instance, which is allocated and |
160 // RequestContext. | 109 // deallocated on the main UI thread (during system startup and teardown). |
161 RequestContextMap outstanding_requests_; | 110 // This method is accessible on UI thread. |
| 111 static TrackingSynchronizer* CurrentSynchronizer(); |
162 | 112 |
163 // We don't track the actual processes that are contacted for an update, only | 113 // We don't track the actual processes that are contacted for an update, only |
164 // the count of the number of processes, and we can sometimes time-out and | 114 // the count of the number of processes, and we can sometimes time-out and |
165 // give up on a "slow to respond" process. We use a sequence_number to be | 115 // give up on a "slow to respond" process. We use a sequence_number to be |
166 // sure a response from a process is associated with the current round of | 116 // sure a response from a process is associated with the current round of |
167 // requests. All sequence numbers used are non-negative. | 117 // requests. All sequence numbers used are non-negative. |
168 // last_used_sequence_number_ is the most recently used number (used to avoid | 118 // last_used_sequence_number_ is the most recently used number (used to avoid |
169 // reuse for a long time). | 119 // reuse for a long time). |
170 int last_used_sequence_number_; | 120 int last_used_sequence_number_; |
171 | 121 |
172 // This singleton instance should be started during the single threaded | 122 // This singleton instance should be started during the single threaded |
173 // portion of main(). It initializes globals to provide support for all future | 123 // portion of main(). It initializes globals to provide support for all future |
174 // calls. This object is created on the UI thread, and it is destroyed after | 124 // calls. This object is created on the UI thread, and it is destroyed after |
175 // all the other threads have gone away. As a result, it is ok to call it | 125 // all the other threads have gone away. As a result, it is ok to call it |
176 // from the UI thread, or for about:tracking. | 126 // from the UI thread, or for about:profiler. |
177 static TrackingSynchronizer* tracking_synchronizer_; | 127 static TrackingSynchronizer* tracking_synchronizer_; |
178 | 128 |
179 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); | 129 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
180 }; | 130 }; |
181 | 131 |
182 } // namespace chrome_browser_metrics | 132 } // namespace chrome_browser_metrics |
183 | 133 |
184 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 134 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
OLD | NEW |