Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ | 5 #ifndef COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ |
| 6 #define COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ | 6 #define COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time/time.h" | |
| 17 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | |
| 16 #include "content/public/browser/profiler_subscriber.h" | 18 #include "content/public/browser/profiler_subscriber.h" |
| 17 | 19 |
| 18 // This class maintains state that is used to upload profiler data from the | 20 // This class maintains state that is used to upload profiler data from the |
| 19 // various processes, into the browser process. Such transactions are usually | 21 // various processes, into the browser process. Such transactions are usually |
| 20 // instigated by the browser. In general, a process will respond by gathering | 22 // instigated by the browser. In general, a process will respond by gathering |
| 21 // profiler data, and transmitting the pickled profiler data. We collect the | 23 // profiler data, and transmitting the pickled profiler data. We collect the |
| 22 // data in asynchronous mode that doesn't block the UI thread. | 24 // data in asynchronous mode that doesn't block the UI thread. |
| 23 // | 25 // |
| 24 // To assure that all the processes have responded, a counter is maintained | 26 // To assure that all the processes have responded, a counter is maintained |
| 25 // to indicate the number of pending (not yet responsive) processes. We tag | 27 // to indicate the number of pending (not yet responsive) processes. We tag |
| 26 // each group of requests with a sequence number. For each group of requests, we | 28 // each group of requests with a sequence number. For each group of requests, we |
| 27 // create RequestContext object which stores the sequence number, pending | 29 // create RequestContext object which stores the sequence number, pending |
| 28 // processes and the callback_object that needs to be notified when we receive | 30 // processes and the callback_object that needs to be notified when we receive |
| 29 // an update from processes. When an update arrives we find the RequestContext | 31 // an update from processes. When an update arrives we find the RequestContext |
| 30 // associated with sequence number and send the unpickled profiler data to the | 32 // associated with sequence number and send the unpickled profiler data to the |
| 31 // |callback_object_|. | 33 // |callback_object_|. |
| 32 | 34 |
| 33 namespace metrics { | 35 namespace metrics { |
| 34 | 36 |
| 35 class TrackingSynchronizerObserver; | 37 class TrackingSynchronizerObserver; |
| 36 | 38 |
| 37 class TrackingSynchronizer | 39 class TrackingSynchronizer |
| 38 : public content::ProfilerSubscriber, | 40 : public content::ProfilerSubscriber, |
| 39 public base::RefCountedThreadSafe<TrackingSynchronizer> { | 41 public base::RefCountedThreadSafe<TrackingSynchronizer> { |
| 40 public: | 42 public: |
| 41 // Construction also sets up the global singleton instance. This instance is | 43 // Construction also sets up the global singleton instance. This instance is |
| 42 // used to communicate between the IO and UI thread, and is destroyed only as | 44 // used to communicate between the IO and UI thread, and is destroyed only as |
| 43 // the main thread (browser_main) terminates, which means the IO thread has | 45 // the main thread (browser_main) terminates, which means the IO thread has |
| 44 // already completed, and will not need this instance any further. | 46 // already completed, and will not need this instance any further. |
| 45 TrackingSynchronizer(); | 47 explicit TrackingSynchronizer(const base::TimeTicks& now); |
| 46 | 48 |
| 47 // Contact all processes, and get them to upload to the browser any/all | 49 // Contact all processes, and get them to upload to the browser any/all |
| 48 // changes to profiler data. It calls |callback_object|'s SetData method with | 50 // changes to profiler data. It calls |callback_object|'s SetData method with |
| 49 // the data received from each sub-process. | 51 // the data received from each sub-process. |
| 50 // This method is accessible on UI thread. | 52 // This method is accessible on UI thread. |
| 51 static void FetchProfilerDataAsynchronously( | 53 static void FetchProfilerDataAsynchronously( |
| 52 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); | 54 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); |
| 53 | 55 |
| 54 // ------------------------------------------------------ | 56 // ------------------------------------------------------ |
| 55 // ProfilerSubscriber methods for browser child processes | 57 // ProfilerSubscriber methods for browser child processes |
| 56 // ------------------------------------------------------ | 58 // ------------------------------------------------------ |
| 57 | 59 |
| 58 // Update the number of pending processes for the given |sequence_number|. | 60 // Update the number of pending processes for the given |sequence_number|. |
| 59 // This is called on UI thread. | 61 // This is called on UI thread. |
| 60 void OnPendingProcesses(int sequence_number, | 62 void OnPendingProcesses(int sequence_number, |
| 61 int pending_processes, | 63 int pending_processes, |
| 62 bool end) override; | 64 bool end) override; |
| 63 | 65 |
| 64 private: | 66 private: |
| 65 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; | 67 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
| 68 // TODO(vadimt): Remove friending TrackingSynchronizerTest_ProfilerData_Test. | |
| 69 friend class TrackingSynchronizerTest_ProfilerData_Test; | |
| 66 | 70 |
| 67 class RequestContext; | 71 class RequestContext; |
| 68 | 72 |
| 69 ~TrackingSynchronizer() override; | 73 ~TrackingSynchronizer() override; |
| 70 | 74 |
| 71 // Send profiler_data back to callback_object_ by calling | 75 // Send profiler_data back to callback_object_ by calling |
| 72 // DecrementPendingProcessesAndSendData which records that we are waiting | 76 // DecrementPendingProcessesAndSendData which records that we are waiting |
| 73 // for one less profiler data from renderer or browser child process for the | 77 // for one less profiler data from renderer or browser child process for the |
| 74 // given sequence number. This method is accessible on UI thread. | 78 // given sequence number. This method is accessible on UI thread. |
| 75 void OnProfilerDataCollected( | 79 void OnProfilerDataCollected( |
| 76 int sequence_number, | 80 int sequence_number, |
| 77 const tracked_objects::ProcessDataSnapshot& profiler_data, | 81 const tracked_objects::ProcessDataSnapshot& profiler_data, |
| 78 int process_type) override; | 82 content::ProcessType process_type) override; |
| 79 | 83 |
| 80 // Establish a new sequence_number_, and use it to notify all the processes of | 84 // Establish a new sequence_number_, and use it to notify all the processes of |
| 81 // the need to supply, to the browser, their tracking data. It also registers | 85 // the need to supply, to the browser, their tracking data. It also registers |
| 82 // |callback_object| in |outstanding_requests_| map. Return the | 86 // |callback_object| in |outstanding_requests_| map. Return the |
| 83 // sequence_number_ that was used. This method is accessible on UI thread. | 87 // sequence_number_ that was used. This method is accessible on UI thread. |
| 84 int RegisterAndNotifyAllProcesses( | 88 int RegisterAndNotifyAllProcesses( |
| 85 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); | 89 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); |
| 86 | 90 |
| 91 // Notify |observer| about |profiler_data| received from process of type | |
| 92 // |process_type|. | |
|
Alexei Svitkine (slow)
2015/03/16 22:00:15
Nit: Mention that |now| is a param for tests.
vadimt
2015/03/16 23:09:13
Done.
| |
| 93 void SendData(const tracked_objects::ProcessDataSnapshot& profiler_data, | |
| 94 content::ProcessType process_type, | |
| 95 const base::TimeTicks& now, | |
| 96 TrackingSynchronizerObserver* observer) const; | |
| 97 | |
| 87 // It finds the RequestContext for the given |sequence_number| and notifies | 98 // It finds the RequestContext for the given |sequence_number| and notifies |
| 88 // the RequestContext's |callback_object_| about the |value|. This is called | 99 // the RequestContext's |callback_object_| about the |value|. This is called |
| 89 // whenever we receive profiler data from processes. It also records that we | 100 // whenever we receive profiler data from processes. It also records that we |
| 90 // are waiting for one less profiler data from a process for the given | 101 // are waiting for one less profiler data from a process for the given |
| 91 // sequence number. If we have received a response from all renderers and | 102 // sequence number. If we have received a response from all renderers and |
| 92 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete | 103 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete |
| 93 // the entry for sequence_number. This method is accessible on UI thread. | 104 // the entry for sequence_number. This method is accessible on UI thread. |
| 94 void DecrementPendingProcessesAndSendData( | 105 void DecrementPendingProcessesAndSendData( |
| 95 int sequence_number, | 106 int sequence_number, |
| 96 const tracked_objects::ProcessDataSnapshot& profiler_data, | 107 const tracked_objects::ProcessDataSnapshot& profiler_data, |
| 97 int process_type); | 108 content::ProcessType process_type); |
| 98 | 109 |
| 99 // Get a new sequence number to be sent to processes from browser process. | 110 // Get a new sequence number to be sent to processes from browser process. |
| 100 // This method is accessible on UI thread. | 111 // This method is accessible on UI thread. |
| 101 int GetNextAvailableSequenceNumber(); | 112 int GetNextAvailableSequenceNumber(); |
| 102 | 113 |
| 103 // We don't track the actual processes that are contacted for an update, only | 114 // We don't track the actual processes that are contacted for an update, only |
| 104 // the count of the number of processes, and we can sometimes time-out and | 115 // the count of the number of processes, and we can sometimes time-out and |
| 105 // give up on a "slow to respond" process. We use a sequence_number to be | 116 // give up on a "slow to respond" process. We use a sequence_number to be |
| 106 // sure a response from a process is associated with the current round of | 117 // sure a response from a process is associated with the current round of |
| 107 // requests. All sequence numbers used are non-negative. | 118 // requests. All sequence numbers used are non-negative. |
| 108 // last_used_sequence_number_ is the most recently used number (used to avoid | 119 // last_used_sequence_number_ is the most recently used number (used to avoid |
| 109 // reuse for a long time). | 120 // reuse for a long time). |
| 110 int last_used_sequence_number_; | 121 int last_used_sequence_number_; |
| 111 | 122 |
| 123 // Sequence of events associated with already completed profiling phases. The | |
| 124 // index in the vector is the phase number. The current phase is not included. | |
| 125 std::vector<ProfilerEventProto::ProfilerEvent> | |
| 126 phase_completion_events_sequence_; | |
| 127 | |
| 128 // TODO(vadimt): consider moving 2 fields below to metrics service. | |
| 129 // Time of the profiling start. Used to calculate times of phase change | |
| 130 // moments relative to this value. | |
| 131 const base::TimeTicks start_time_; | |
| 132 | |
| 133 // Times of starts of all profiling phases, including the current phase. The | |
| 134 // index in the vector is the phase number. | |
| 135 std::vector<base::TimeTicks> phase_start_times_; | |
| 136 | |
| 112 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); | 137 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
| 113 }; | 138 }; |
| 114 | 139 |
| 115 } // namespace metrics | 140 } // namespace metrics |
| 116 | 141 |
| 117 #endif // COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ | 142 #endif // COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ |
| OLD | NEW |