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/tick_clock.h" | |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | 18 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
18 #include "content/public/browser/profiler_subscriber.h" | 19 #include "content/public/browser/profiler_subscriber.h" |
19 | 20 |
20 // This class maintains state that is used to upload profiler data from the | 21 // This class maintains state that is used to upload profiler data from the |
21 // various processes, into the browser process. Such transactions are usually | 22 // various processes, into the browser process. Such transactions are usually |
22 // 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 |
23 // profiler data, and transmitting the pickled profiler data. We collect the | 24 // profiler data, and transmitting the pickled profiler data. We collect the |
24 // data in asynchronous mode that doesn't block the UI thread. | 25 // data in asynchronous mode that doesn't block the UI thread. |
25 // | 26 // |
(...skipping 10 matching lines...) Expand all Loading... | |
36 | 37 |
37 class TrackingSynchronizerObserver; | 38 class TrackingSynchronizerObserver; |
38 | 39 |
39 class TrackingSynchronizer | 40 class TrackingSynchronizer |
40 : public content::ProfilerSubscriber, | 41 : public content::ProfilerSubscriber, |
41 public base::RefCountedThreadSafe<TrackingSynchronizer> { | 42 public base::RefCountedThreadSafe<TrackingSynchronizer> { |
42 public: | 43 public: |
43 // Construction also sets up the global singleton instance. This instance is | 44 // Construction also sets up the global singleton instance. This instance is |
44 // 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 |
45 // 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 |
46 // already completed, and will not need this instance any further. |now| is | 47 // already completed, and will not need this instance any further. |
47 // the current time, but can be something else in tests. | 48 // |clock| is a clock used for durations of profiling phases. |
48 explicit TrackingSynchronizer(base::TimeTicks now); | 49 // TrackingSynchronizer takes ownership of this pointer. |
50 explicit TrackingSynchronizer(base::TickClock* clock); | |
Ilya Sherman
2015/04/07 22:58:35
nit: Please pass a scoped_ptr, rather than simply
vadimt
2015/04/08 01:09:50
Done.
| |
49 | 51 |
50 // Contact all processes, and get them to upload to the browser any/all | 52 // Contact all processes, and get them to upload to the browser any/all |
51 // changes to profiler data. It calls |callback_object|'s SetData method with | 53 // changes to profiler data. It calls |callback_object|'s SetData method with |
52 // the data received from each sub-process. | 54 // the data received from each sub-process. |
53 // This method is accessible on UI thread. | 55 // This method is accessible on UI thread. |
54 static void FetchProfilerDataAsynchronously( | 56 static void FetchProfilerDataAsynchronously( |
55 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); | 57 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); |
56 | 58 |
59 // Called when a profiling phase completes. |profiling_event| is the event | |
60 // that triggered the completion of the current phase, and begins a new phase. | |
61 static void OnProfilingPhaseCompleted( | |
62 ProfilerEventProto::ProfilerEvent profiling_event); | |
63 | |
57 // ------------------------------------------------------ | 64 // ------------------------------------------------------ |
58 // ProfilerSubscriber methods for browser child processes | 65 // ProfilerSubscriber methods for browser child processes |
59 // ------------------------------------------------------ | 66 // ------------------------------------------------------ |
60 | 67 |
61 // Update the number of pending processes for the given |sequence_number|. | 68 // Update the number of pending processes for the given |sequence_number|. |
62 // This is called on UI thread. | 69 // This is called on UI thread. |
63 void OnPendingProcesses(int sequence_number, | 70 void OnPendingProcesses(int sequence_number, |
64 int pending_processes, | 71 int pending_processes, |
65 bool end) override; | 72 bool end) override; |
66 | 73 |
74 protected: | |
75 ~TrackingSynchronizer() override; | |
76 | |
77 // Update the sequence of completed phases with a new phase completion info. | |
78 void RegisterPhaseCompletion( | |
79 ProfilerEventProto::ProfilerEvent profiling_event); | |
80 | |
81 // Notify |observer| about |profiler_data| received from process of type | |
82 // |process_type|. | |
83 void SendData(const tracked_objects::ProcessDataSnapshot& profiler_data, | |
84 content::ProcessType process_type, | |
85 TrackingSynchronizerObserver* observer) const; | |
86 | |
67 private: | 87 private: |
68 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; | 88 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
69 // TODO(vadimt): Remove friending TrackingSynchronizerTest_ProfilerData_Test. | |
70 friend class TrackingSynchronizerTest_ProfilerData_Test; | |
71 | 89 |
72 class RequestContext; | 90 class RequestContext; |
73 | 91 |
74 ~TrackingSynchronizer() override; | |
75 | |
76 // Send profiler_data back to callback_object_ by calling | 92 // Send profiler_data back to callback_object_ by calling |
77 // DecrementPendingProcessesAndSendData which records that we are waiting | 93 // DecrementPendingProcessesAndSendData which records that we are waiting |
78 // for one less profiler data from renderer or browser child process for the | 94 // for one less profiler data from renderer or browser child process for the |
79 // given sequence number. This method is accessible on UI thread. | 95 // given sequence number. This method is accessible on UI thread. |
80 void OnProfilerDataCollected( | 96 void OnProfilerDataCollected( |
81 int sequence_number, | 97 int sequence_number, |
82 const tracked_objects::ProcessDataSnapshot& profiler_data, | 98 const tracked_objects::ProcessDataSnapshot& profiler_data, |
83 content::ProcessType process_type) override; | 99 content::ProcessType process_type) override; |
84 | 100 |
85 // Establish a new sequence_number_, and use it to notify all the processes of | 101 // Establish a new sequence_number_, and use it to notify all the processes of |
86 // the need to supply, to the browser, their tracking data. It also registers | 102 // the need to supply, to the browser, their tracking data. It also registers |
87 // |callback_object| in |outstanding_requests_| map. Return the | 103 // |callback_object| in |outstanding_requests_| map. Return the |
88 // sequence_number_ that was used. This method is accessible on UI thread. | 104 // sequence_number_ that was used. This method is accessible on UI thread. |
89 int RegisterAndNotifyAllProcesses( | 105 int RegisterAndNotifyAllProcesses( |
90 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); | 106 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); |
91 | 107 |
92 // Notify |observer| about |profiler_data| received from process of type | 108 // Notifies all processes of a completion of a profiling phase. |
93 // |process_type|. |now| is the current time, but can be something else in | 109 // |profiling_event| is the event associated with the phase change. |
94 // tests. | 110 void NotifyAllProcessesOfProfilingPhaseCompletion( |
95 void SendData(const tracked_objects::ProcessDataSnapshot& profiler_data, | 111 ProfilerEventProto::ProfilerEvent profiling_event); |
96 content::ProcessType process_type, | |
97 base::TimeTicks now, | |
98 TrackingSynchronizerObserver* observer) const; | |
99 | 112 |
100 // It finds the RequestContext for the given |sequence_number| and notifies | 113 // It finds the RequestContext for the given |sequence_number| and notifies |
101 // the RequestContext's |callback_object_| about the |value|. This is called | 114 // the RequestContext's |callback_object_| about the |value|. This is called |
102 // whenever we receive profiler data from processes. It also records that we | 115 // whenever we receive profiler data from processes. It also records that we |
103 // are waiting for one less profiler data from a process for the given | 116 // are waiting for one less profiler data from a process for the given |
104 // sequence number. If we have received a response from all renderers and | 117 // sequence number. If we have received a response from all renderers and |
105 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete | 118 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete |
106 // the entry for sequence_number. This method is accessible on UI thread. | 119 // the entry for sequence_number. This method is accessible on UI thread. |
107 void DecrementPendingProcessesAndSendData( | 120 void DecrementPendingProcessesAndSendData( |
108 int sequence_number, | 121 int sequence_number, |
(...skipping 11 matching lines...) Expand all Loading... | |
120 // requests. All sequence numbers used are non-negative. | 133 // requests. All sequence numbers used are non-negative. |
121 // last_used_sequence_number_ is the most recently used number (used to avoid | 134 // last_used_sequence_number_ is the most recently used number (used to avoid |
122 // reuse for a long time). | 135 // reuse for a long time). |
123 int last_used_sequence_number_; | 136 int last_used_sequence_number_; |
124 | 137 |
125 // Sequence of events associated with already completed profiling phases. The | 138 // Sequence of events associated with already completed profiling phases. The |
126 // index in the vector is the phase number. The current phase is not included. | 139 // index in the vector is the phase number. The current phase is not included. |
127 std::vector<ProfilerEventProto::ProfilerEvent> | 140 std::vector<ProfilerEventProto::ProfilerEvent> |
128 phase_completion_events_sequence_; | 141 phase_completion_events_sequence_; |
129 | 142 |
130 // TODO(vadimt): consider moving 2 fields below to metrics service. | 143 // Clock for profiling phase durations. |
144 const scoped_ptr<base::TickClock> clock_; | |
145 | |
131 // Time of the profiling start. Used to calculate times of phase change | 146 // Time of the profiling start. Used to calculate times of phase change |
132 // moments relative to this value. | 147 // moments relative to this value. |
133 const base::TimeTicks start_time_; | 148 const base::TimeTicks start_time_; |
134 | 149 |
135 // Times of starts of all profiling phases, including the current phase. The | 150 // Times of starts of all profiling phases, including the current phase. The |
136 // index in the vector is the phase number. | 151 // index in the vector is the phase number. |
137 std::vector<base::TimeTicks> phase_start_times_; | 152 std::vector<base::TimeTicks> phase_start_times_; |
138 | 153 |
139 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); | 154 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
140 }; | 155 }; |
141 | 156 |
142 } // namespace metrics | 157 } // namespace metrics |
143 | 158 |
144 #endif // COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ | 159 #endif // COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_ |
OLD | NEW |