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