| 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/common/child_process_info.h" | 19 #include "content/public/browser/profiler_subscriber.h" |
| 20 #include "content/public/browser/browser_thread.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) OVERRIDE; |
| 99 static void IsTrackingEnabled(int process_id); | |
| 100 | 65 |
| 101 // Get the current tracking status from the browser process and set it in the | 66 // Send profiler_data back to callback_object_ by calling |
| 102 // renderer process. |process_id| is used to find the renderer process. | 67 // DecrementPendingProcessesAndSendData which records that we are waiting |
| 103 // This method is accessible on UI thread. | 68 // for one less profiler data from renderer or browser child process for the |
| 104 static void SetTrackingStatusInProcess(int process_id); | 69 // given sequence number. This method is accessible on UI thread. |
| 105 | 70 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, | 71 int sequence_number, |
| 112 const std::string& tracking_data, | 72 base::DictionaryValue* profiler_data) OVERRIDE; |
| 113 ChildProcessInfo::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 ChildProcessInfo::ProcessType process_type); | |
| 121 | 73 |
| 122 private: | 74 private: |
| 123 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; | 75 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
| 76 friend class RequestContext; |
| 124 | 77 |
| 125 virtual ~TrackingSynchronizer(); | 78 virtual ~TrackingSynchronizer(); |
| 126 | 79 |
| 80 // Send profiler_data back to callback_object_. It records that we are waiting |
| 81 // for one less profiler data from renderer or browser child process for the |
| 82 // given sequence number. This method is accessible on UI thread. |
| 83 void OnProfilerDataCollectedOnUI(int sequence_number, |
| 84 base::DictionaryValue* profiler_data); |
| 85 |
| 127 // Establish a new sequence_number_, and use it to notify all the processes of | 86 // 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 | 87 // the need to supply, to the browser, their tracking data. It also registers |
| 129 // |callback_object| in |outstanding_requests_| map. Return the | 88 // |callback_object| in |outstanding_requests_| map. Return the |
| 130 // sequence_number_ that was used. This method is accessible on UI thread. | 89 // sequence_number_ that was used. This method is accessible on UI thread. |
| 131 int RegisterAndNotifyAllProcesses( | 90 int RegisterAndNotifyAllProcesses( |
| 132 const base::WeakPtr<ProfilerUI>& callback_object); | 91 const base::WeakPtr<ProfilerUI>& callback_object); |
| 133 | 92 |
| 134 // It finds the |callback_object_| in |outstanding_requests_| map for the | 93 // It finds the RequestContext for the given |sequence_number| and notifies |
| 135 // given |sequence_number| and notifies the |callback_object_| about the | 94 // the RequestContext's |callback_object_| about the |value|. This is called |
| 136 // |value|. This is called whenever we receive tracked data from processes. It | 95 // 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 | 96 // 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 | 97 // sequence number. If we have received a response from all renderers and |
| 139 // renderers, then it deletes the entry for sequence_number from | 98 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete |
| 140 // |outstanding_requests_| map. This method is accessible on UI thread. | 99 // the entry for sequence_number. This method is accessible on UI thread. |
| 141 void DecrementPendingProcessesAndSendData(int sequence_number, | 100 void DecrementPendingProcessesAndSendData(int sequence_number, |
| 142 base::DictionaryValue* value); | 101 base::DictionaryValue* value); |
| 143 | 102 |
| 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. | 103 // Get a new sequence number to be sent to processes from browser process. |
| 156 // This method is accessible on UI thread. | 104 // This method is accessible on UI thread. |
| 157 int GetNextAvailableSequenceNumber(); | 105 int GetNextAvailableSequenceNumber(); |
| 158 | 106 |
| 159 // Map of all outstanding RequestContexts, from sequence_number_ to | 107 // Return pointer to the singleton instance, which is allocated and |
| 160 // RequestContext. | 108 // deallocated on the main UI thread (during system startup and teardown). |
| 161 RequestContextMap outstanding_requests_; | 109 // This method is accessible on UI thread. |
| 110 static TrackingSynchronizer* CurrentSynchronizer(); |
| 162 | 111 |
| 163 // We don't track the actual processes that are contacted for an update, only | 112 // 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 | 113 // 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 | 114 // 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 | 115 // sure a response from a process is associated with the current round of |
| 167 // requests. All sequence numbers used are non-negative. | 116 // requests. All sequence numbers used are non-negative. |
| 168 // last_used_sequence_number_ is the most recently used number (used to avoid | 117 // last_used_sequence_number_ is the most recently used number (used to avoid |
| 169 // reuse for a long time). | 118 // reuse for a long time). |
| 170 int last_used_sequence_number_; | 119 int last_used_sequence_number_; |
| 171 | 120 |
| 172 // This singleton instance should be started during the single threaded | 121 // This singleton instance should be started during the single threaded |
| 173 // portion of main(). It initializes globals to provide support for all future | 122 // 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 | 123 // 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 | 124 // 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. | 125 // from the UI thread, or for about:profiler. |
| 177 static TrackingSynchronizer* tracking_synchronizer_; | 126 static TrackingSynchronizer* tracking_synchronizer_; |
| 178 | 127 |
| 179 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); | 128 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
| 180 }; | 129 }; |
| 181 | 130 |
| 182 } // namespace chrome_browser_metrics | 131 } // namespace chrome_browser_metrics |
| 183 | 132 |
| 184 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 133 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
| OLD | NEW |