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 CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ |
6 #define CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ | 6 #define CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
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/callback.h" | 13 #include "base/callback.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/synchronization/condition_variable.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "base/time.h" | 17 #include "base/time.h" |
18 #include "content/public/browser/histogram_subscriber.h" | |
18 | 19 |
19 class MessageLoop; | 20 class MessageLoop; |
20 | 21 |
21 // This class maintains state that is used to upload histogram data from the | 22 // This class maintains state that is used to upload histogram data from the |
jam
2012/06/01 18:23:23
now that you have the part that sends child proces
ramant (doing other things)
2012/06/07 02:04:41
Done.
| |
22 // various renderer processes, into the browser process. Such transactions are | 23 // various renderer and child processes, into the browser process. Such |
23 // usually instigated by the browser. In general, a renderer process will | 24 // transactions are usually instigated by the browser. In general, a |
24 // respond by gathering snapshots of all internal histograms, calculating what | 25 // renderer/child process will respond by gathering snapshots of all internal |
25 // has changed since its last upload, and transmitting a pickled collection of | 26 // histograms, calculating what has changed since its last upload, and |
26 // deltas. | 27 // transmitting a pickled collection of deltas. |
27 // | 28 // |
28 // There are actually two modes of update request. One is synchronous (and | 29 // There are actually two modes of update request. One is synchronous (and |
29 // blocks the UI thread, waiting to populate an about:histograms tab) and the | 30 // blocks the UI thread, waiting to populate an about:histograms tab) and the |
30 // other is asynchronous, and used by the metrics services in preparation for a | 31 // other is asynchronous, and used by the metrics services in preparation for a |
31 // log upload. | 32 // log upload. |
32 // | 33 // |
33 // To assure that all the renderers have responded, a counter is maintained (for | 34 // To assure that all the processes have responded, a counter is maintained to |
34 // each mode) to indicate the number of pending (not yet responsive) renderers. | 35 // indicate the number of pending (not yet responsive) processes. To avoid |
35 // To avoid confusion about a response (i.e., is the renderer responding to a | 36 // confusion about a response (i.e., is the process responding to a current |
36 // current request for an update, or to an old request for an update) we tag | 37 // request for an update, or to an old request for an update) we tag each group |
37 // each group of requests with a sequence number. When an update arrives we can | 38 // of requests with a sequence number. When an update arrives we can ignore it |
38 // ignore it (relative to the counter) if it does not relate to a current | 39 // (relative to the counter) if it does not relate to a current outstanding |
39 // outstanding sequence number. | 40 // sequence number. |
40 // | 41 // |
41 // There is one final mode of use, where a renderer spontaneously decides to | 42 // There is one final mode of use, where a renderer spontaneously decides to |
42 // transmit a collection of histogram data. This is designed for use when the | 43 // transmit a collection of histogram data. This is designed for use when the |
43 // renderer is terminating. Unfortunately, renders may be terminated without | 44 // renderer is terminating. Unfortunately, renders may be terminated without |
44 // warning, and the best we can do is periodically acquire data from a tab, such | 45 // warning, and the best we can do is periodically acquire data from a tab, such |
45 // as when a page load has completed. In this mode, the renderer uses a | 46 // as when a page load has completed. In this mode, the renderer uses a |
46 // reserved sequence number, different from any sequence number that might be | 47 // reserved sequence number, different from any sequence number that might be |
47 // specified by a browser request. Since this sequence number can't match an | 48 // specified by a browser request. Since this sequence number can't match an |
48 // outstanding sequence number, the pickled data is accepted into the browser, | 49 // outstanding sequence number, the pickled data is accepted into the browser, |
49 // but there is no impact on the counters. | 50 // but there is no impact on the counters. |
50 | 51 |
51 class HistogramSynchronizer : public | 52 class HistogramSynchronizer |
52 base::RefCountedThreadSafe<HistogramSynchronizer> { | 53 : public content::HistogramSubscriber, |
54 public base::RefCountedThreadSafe<HistogramSynchronizer> { | |
53 public: | 55 public: |
54 | 56 |
55 enum RendererHistogramRequester { | 57 enum ProcessHistogramRequester { |
56 ASYNC_HISTOGRAMS, | 58 ASYNC_HISTOGRAMS, |
57 SYNCHRONOUS_HISTOGRAMS | 59 SYNCHRONOUS_HISTOGRAMS |
58 }; | 60 }; |
59 | 61 |
60 // Construction also sets up the global singleton instance. This instance is | 62 // Construction also sets up the global singleton instance. This instance is |
61 // used to communicate between the IO and UI thread, and is destroyed only | 63 // used to communicate between the IO and UI thread, and is destroyed only |
62 // as the main thread (browser_main) terminates, which means the IO thread has | 64 // as the main thread (browser_main) terminates, which means the IO thread has |
63 // already completed, and will not need this instance any further. | 65 // already completed, and will not need this instance any further. |
64 HistogramSynchronizer(); | 66 HistogramSynchronizer(); |
65 | 67 |
66 // Return pointer to the singleton instance, which is allocated and | 68 // Return pointer to the singleton instance, which is allocated and |
67 // deallocated on the main UI thread (during system startup and teardown). | 69 // deallocated on the main UI thread (during system startup and teardown). |
68 static HistogramSynchronizer* CurrentSynchronizer(); | 70 static HistogramSynchronizer* CurrentSynchronizer(); |
69 | 71 |
70 // Contact all renderers, and get them to upload to the browser any/all | 72 // Contact all processes, and get them to upload to the browser any/all |
71 // changes to histograms. Return when all changes have been acquired, or when | 73 // changes to histograms. This method is called on the main UI thread from |
72 // the wait time expires (whichever is sooner). This method is called on the | 74 // about:histograms. |
73 // main UI thread from about:histograms. | 75 void FetchHistogramsSynchronously(base::TimeDelta wait_time); |
74 void FetchRendererHistogramsSynchronously(base::TimeDelta wait_time); | |
75 | 76 |
76 // Contact all renderers, and get them to upload to the browser any/all | 77 // Contact all processes, and get them to upload to the browser any/all |
77 // changes to histograms. When all changes have been acquired, or when the | 78 // changes to histograms. When all changes have been acquired, or when the |
78 // wait time expires (whichever is sooner), post the callback to the | 79 // wait time expires (whichever is sooner), post the callback to the |
79 // specified message loop. Note the callback is posted exactly once. | 80 // specified message loop. Note the callback is posted exactly once. |
80 static void FetchRendererHistogramsAsynchronously( | 81 static void FetchHistogramsAsynchronously(MessageLoop* callback_thread, |
81 MessageLoop* callback_thread, | 82 const base::Closure& callback, |
82 const base::Closure& callback, | 83 base::TimeDelta wait_time); |
83 base::TimeDelta wait_time); | |
84 | |
85 // This method is called on the IO thread. Deserializes the histograms and | |
86 // records that we have received histograms from a renderer process. | |
87 static void DeserializeHistogramList( | |
88 int sequence_number, const std::vector<std::string>& histograms); | |
89 | 84 |
90 private: | 85 private: |
91 friend class base::RefCountedThreadSafe<HistogramSynchronizer>; | 86 friend class base::RefCountedThreadSafe<HistogramSynchronizer>; |
92 | 87 |
93 ~HistogramSynchronizer(); | 88 virtual ~HistogramSynchronizer(); |
94 | 89 |
95 // Establish a new sequence_number_, and use it to notify all the renderers of | 90 // Establish a new sequence number, and use it to notify all processes |
96 // the need to supply, to the browser, any changes in their histograms. | 91 // (renderers, plugins, GPU, etc) of the need to supply, to the browser, |
97 // The argument indicates whether this will set async_sequence_number_ or | 92 // any/all changes to their histograms. It also posts a task |
98 // synchronous_sequence_number_. | 93 // (RequestContext::Unregister) that would be called after waiting |
99 // Return the sequence number that was used. | 94 // for |wait_time| (this task acts as a watchdog, to cancel the requests for |
100 int NotifyAllRenderers(RendererHistogramRequester requester); | 95 // non-responsive processes). |requester| argument indicates whether this will |
96 // set async_sequence_number_ or not and that sequence number is registered in | |
97 // |outstanding_requests_| map. | |
98 void RegisterAndNotifyAllProcesses(ProcessHistogramRequester requester, | |
99 base::TimeDelta wait_time); | |
101 | 100 |
102 // Records that we are waiting for one less histogram from a renderer for the | 101 // ------------------------------------------------------- |
103 // given sequence number. If we have received a response from all renderers, | 102 // HistogramSubscriber methods for browser child processes |
104 // either signal the waiting process or call the callback function. | 103 // ------------------------------------------------------- |
105 void DecrementPendingRenderers(int sequence_number); | 104 |
105 // Update the number of pending processes for the given |sequence_number|. | |
106 // This is called on UI thread. | |
107 virtual void OnPendingProcesses(int sequence_number, | |
108 int pending_processes, | |
109 bool end) OVERRIDE; | |
110 | |
111 // Send histogram_data back to caller by calling | |
112 // DecrementPendingProcessesAndSendData which records that we are waiting | |
113 // for one less histogram data from renderer or browser child process for the | |
114 // given sequence number. This method is accessible on UI thread. | |
115 virtual void OnHistogramDataCollected( | |
116 int sequence_number, | |
117 const std::vector<std::string>& histogram_data, | |
118 content::ProcessType process_type) OVERRIDE; | |
119 | |
120 // It finds the RequestContext for the given |sequence_number| and notifies | |
121 // the RequestContext's |callback_| about the |value|. This is called | |
122 // whenever we receive histogram data from processes. It also records that we | |
123 // are waiting for one less histogram data from a process for the given | |
124 // sequence number. If we have received a response from all renderers and | |
125 // browser child processes, then it calls RequestContext's DeleteIfAllDone to | |
126 // delete the entry for sequence_number. This method is accessible on UI | |
127 // thread. | |
128 void DecrementPendingProcessesAndSendData( | |
129 int sequence_number, | |
130 const std::vector<std::string>& histogram_data); | |
106 | 131 |
107 // Set the callback_thread_ and callback_ members. If these members already | 132 // Set the callback_thread_ and callback_ members. If these members already |
108 // had values, then as a side effect, post the old callback_ to the old | 133 // had values, then as a side effect, post the old callback_ to the old |
109 // callaback_thread_. This side effect should not generally happen, but is in | 134 // callaback_thread_. This side effect should not generally happen, but is in |
110 // place to assure correctness (that any tasks that were set, are eventually | 135 // place to assure correctness (that any tasks that were set, are eventually |
111 // called, and never merely discarded). | 136 // called, and never merely discarded). |
112 void SetCallbackTaskAndThread(MessageLoop* callback_thread, | 137 void SetCallbackTaskAndThread(MessageLoop* callback_thread, |
113 const base::Closure& callback); | 138 const base::Closure& callback); |
114 | 139 |
115 void ForceHistogramSynchronizationDoneCallback(int sequence_number); | 140 void ForceHistogramSynchronizationDoneCallback(int sequence_number); |
116 | 141 |
117 // Gets a new sequence number to be sent to renderers from browser process and | 142 // Internal helper function, to post task, and record callback stats. |
118 // set the number of pending responses for the given type to renderer_count. | 143 void InternalPostTask(MessageLoop* thread, const base::Closure& callback); |
119 int GetNextAvailableSequenceNumber(RendererHistogramRequester requster, | |
120 int renderer_count); | |
121 | 144 |
122 // Internal helper function, to post task, and record callback stats. | 145 // Gets a new sequence number to be sent to processes from browser process. |
123 void InternalPostTask(MessageLoop* thread, | 146 int GetNextAvailableSequenceNumber(ProcessHistogramRequester requster); |
124 const base::Closure& callback, | |
125 int unresponsive_renderers, | |
126 const base::TimeTicks& started); | |
127 | 147 |
128 // This lock_ protects access to all members. | 148 // This lock_ protects access to all members. |
129 base::Lock lock_; | 149 base::Lock lock_; |
130 | 150 |
131 // This condition variable is used to block caller of the synchronous request | |
132 // to update histograms, and to signal that thread when updates are completed. | |
133 base::ConditionVariable received_all_renderer_histograms_; | |
134 | |
135 // When a request is made to asynchronously update the histograms, we store | 151 // When a request is made to asynchronously update the histograms, we store |
136 // the task and thread we use to post a completion notification in | 152 // the task and thread we use to post a completion notification in |
137 // callback_ and callback_thread_. | 153 // callback_ and callback_thread_. |
138 base::Closure callback_; | 154 base::Closure callback_; |
139 MessageLoop* callback_thread_; | 155 MessageLoop* callback_thread_; |
140 | 156 |
141 // We don't track the actual renderers that are contacted for an update, only | 157 // We don't track the actual processes that are contacted for an update, only |
142 // the count of the number of renderers, and we can sometimes time-out and | 158 // the count of the number of processes, and we can sometimes time-out and |
143 // give up on a "slow to respond" renderer. We use a sequence_number to be | 159 // give up on a "slow to respond" process. We use a sequence_number to be |
144 // sure a response from a renderer is associated with the current round of | 160 // sure a response from a process is associated with the current round of |
145 // requests (and not merely a VERY belated prior response). | 161 // requests (and not merely a VERY belated prior response). |
146 // All sequence numbers used are non-negative. | 162 // All sequence numbers used are non-negative. |
147 // last_used_sequence_number_ is the most recently used number (used to avoid | 163 // last_used_sequence_number_ is the most recently used number (used to avoid |
148 // reuse for a long time). | 164 // reuse for a long time). |
149 int last_used_sequence_number_; | 165 int last_used_sequence_number_; |
150 | 166 |
151 // The sequence number used by the most recent asynchronous update request to | 167 // The sequence number used by the most recent asynchronous update request to |
152 // contact all renderers. | 168 // contact all processes. |
153 int async_sequence_number_; | 169 int async_sequence_number_; |
154 | 170 |
155 // The number of renderers that have not yet responded to requests (as part of | |
156 // an asynchronous update). | |
157 int async_renderers_pending_; | |
158 | |
159 // The time when we were told to start the fetch histograms asynchronously | |
160 // from renderers. | |
161 base::TimeTicks async_callback_start_time_; | |
162 | |
163 // The sequence number used by the most recent synchronous update request to | |
164 // contact all renderers. | |
165 int synchronous_sequence_number_; | |
166 | |
167 // The number of renderers that have not yet responded to requests (as part of | |
168 // a synchronous update). | |
169 int synchronous_renderers_pending_; | |
170 | |
171 // This singleton instance should be started during the single threaded | 171 // This singleton instance should be started during the single threaded |
172 // portion of main(). It initializes globals to provide support for all future | 172 // portion of main(). It initializes globals to provide support for all future |
173 // calls. This object is created on the UI thread, and it is destroyed after | 173 // calls. This object is created on the UI thread, and it is destroyed after |
174 // all the other threads have gone away. As a result, it is ok to call it | 174 // all the other threads have gone away. As a result, it is ok to call it |
175 // from the UI thread (for UMA uploads), or for about:histograms. | 175 // from the UI thread (for UMA uploads), or for about:histograms. |
176 static HistogramSynchronizer* histogram_synchronizer_; | 176 static HistogramSynchronizer* g_histogram_synchronizer; |
177 | 177 |
178 DISALLOW_COPY_AND_ASSIGN(HistogramSynchronizer); | 178 DISALLOW_COPY_AND_ASSIGN(HistogramSynchronizer); |
179 }; | 179 }; |
180 | 180 |
181 #endif // CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ | 181 #endif // CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ |
OLD | NEW |