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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_
6 #define COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_
12 #include "base/basictypes.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time/tick_clock.h"
17 #include "base/time/time.h"
18 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
19 #include "content/public/browser/profiler_subscriber.h"
21 // This class maintains state that is used to upload profiler data from the
22 // various processes, into the browser process. Such transactions are usually
23 // instigated by the browser. In general, a process will respond by gathering
24 // profiler data, and transmitting the pickled profiler data. We collect the
25 // data in asynchronous mode that doesn't block the UI thread.
27 // To assure that all the processes have responded, a counter is maintained
28 // to indicate the number of pending (not yet responsive) processes. We tag
29 // each group of requests with a sequence number. For each group of requests, we
30 // create RequestContext object which stores the sequence number, pending
31 // processes and the callback_object that needs to be notified when we receive
32 // an update from processes. When an update arrives we find the RequestContext
33 // associated with sequence number and send the unpickled profiler data to the
34 // |callback_object_|.
38 class TrackingSynchronizerObserver
;
40 class TrackingSynchronizer
41 : public content::ProfilerSubscriber
,
42 public base::RefCountedThreadSafe
<TrackingSynchronizer
> {
44 // Construction also sets up the global singleton instance. This instance is
45 // used to communicate between the IO and UI thread, and is destroyed only as
46 // the main thread (browser_main) terminates, which means the IO thread has
47 // already completed, and will not need this instance any further.
48 // |clock| is a clock used for durations of profiling phases.
49 explicit TrackingSynchronizer(scoped_ptr
<base::TickClock
> clock
);
51 // Contact all processes, and get them to upload to the browser any/all
52 // changes to profiler data. It calls |callback_object|'s SetData method with
53 // the data received from each sub-process.
54 // This method is accessible on UI thread.
55 static void FetchProfilerDataAsynchronously(
56 const base::WeakPtr
<TrackingSynchronizerObserver
>& callback_object
);
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
);
63 // ------------------------------------------------------
64 // ProfilerSubscriber methods for browser child processes
65 // ------------------------------------------------------
67 // Update the number of pending processes for the given |sequence_number|.
68 // This is called on UI thread.
69 void OnPendingProcesses(int sequence_number
,
70 int pending_processes
,
74 ~TrackingSynchronizer() override
;
76 // Update the sequence of completed phases with a new phase completion info.
77 void RegisterPhaseCompletion(
78 ProfilerEventProto::ProfilerEvent profiling_event
);
80 // Notify |observer| about |profiler_data| received from process of type
82 void SendData(const tracked_objects::ProcessDataSnapshot
& profiler_data
,
83 content::ProcessType process_type
,
84 TrackingSynchronizerObserver
* observer
) const;
87 friend class base::RefCountedThreadSafe
<TrackingSynchronizer
>;
91 // Send profiler_data back to callback_object_ by calling
92 // DecrementPendingProcessesAndSendData which records that we are waiting
93 // for one less profiler data from renderer or browser child process for the
94 // given sequence number. This method is accessible on UI thread.
95 void OnProfilerDataCollected(
97 const tracked_objects::ProcessDataSnapshot
& profiler_data
,
98 content::ProcessType process_type
) override
;
100 // Establish a new sequence_number_, and use it to notify all the processes of
101 // the need to supply, to the browser, their tracking data. It also registers
102 // |callback_object| in |outstanding_requests_| map. Return the
103 // sequence_number_ that was used. This method is accessible on UI thread.
104 int RegisterAndNotifyAllProcesses(
105 const base::WeakPtr
<TrackingSynchronizerObserver
>& callback_object
);
107 // Notifies all processes of a completion of a profiling phase.
108 // |profiling_event| is the event associated with the phase change.
109 void NotifyAllProcessesOfProfilingPhaseCompletion(
110 ProfilerEventProto::ProfilerEvent profiling_event
);
112 // It finds the RequestContext for the given |sequence_number| and notifies
113 // the RequestContext's |callback_object_| about the |value|. This is called
114 // whenever we receive profiler data from processes. It also records that we
115 // are waiting for one less profiler data from a process for the given
116 // sequence number. If we have received a response from all renderers and
117 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete
118 // the entry for sequence_number. This method is accessible on UI thread.
119 void DecrementPendingProcessesAndSendData(
121 const tracked_objects::ProcessDataSnapshot
& profiler_data
,
122 content::ProcessType process_type
);
124 // Get a new sequence number to be sent to processes from browser process.
125 // This method is accessible on UI thread.
126 int GetNextAvailableSequenceNumber();
128 // We don't track the actual processes that are contacted for an update, only
129 // the count of the number of processes, and we can sometimes time-out and
130 // give up on a "slow to respond" process. We use a sequence_number to be
131 // sure a response from a process is associated with the current round of
132 // requests. All sequence numbers used are non-negative.
133 // last_used_sequence_number_ is the most recently used number (used to avoid
134 // reuse for a long time).
135 int last_used_sequence_number_
;
137 // Sequence of events associated with already completed profiling phases. The
138 // index in the vector is the phase number. The current phase is not included.
139 std::vector
<ProfilerEventProto::ProfilerEvent
>
140 phase_completion_events_sequence_
;
142 // Clock for profiling phase durations.
143 const scoped_ptr
<base::TickClock
> clock_
;
145 // Times of starts of all profiling phases, including the current phase. The
146 // index in the vector is the phase number.
147 std::vector
<base::TimeTicks
> phase_start_times_
;
149 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer
);
152 } // namespace metrics
154 #endif // COMPONENTS_METRICS_PROFILER_TRACKING_SYNCHRONIZER_H_