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 #include "content/browser/histogram_synchronizer.h"
8 #include "base/lazy_instance.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/histogram_delta_serialization.h"
13 #include "base/pickle.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "content/browser/histogram_controller.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/histogram_fetcher.h"
20 #include "content/public/common/content_constants.h"
23 using base::TimeDelta
;
24 using base::TimeTicks
;
28 // Negative numbers are never used as sequence numbers. We explicitly pick a
29 // negative number that is "so negative" that even when we add one (as is done
30 // when we generated the next sequence number) that it will still be negative.
31 // We have code that handles wrapping around on an overflow into negative
33 static const int kNeverUsableSequenceNumber
= -2;
35 } // anonymous namespace
39 // The "RequestContext" structure describes an individual request received from
40 // the UI. All methods are accessible on UI thread.
41 class HistogramSynchronizer::RequestContext
{
43 // A map from sequence_number_ to the actual RequestContexts.
44 typedef std::map
<int, RequestContext
*> RequestContextMap
;
46 RequestContext(const base::Closure
& callback
, int sequence_number
)
47 : callback_(callback
),
48 sequence_number_(sequence_number
),
49 received_process_group_count_(0),
50 processes_pending_(0) {
54 void SetReceivedProcessGroupCount(bool done
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
56 received_process_group_count_
= done
;
59 // Methods for book keeping of processes_pending_.
60 void AddProcessesPending(int processes_pending
) {
61 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
62 processes_pending_
+= processes_pending
;
65 void DecrementProcessesPending() {
66 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
70 // Records that we are waiting for one less histogram data from a process for
71 // the given sequence number. If |received_process_group_count_| and
72 // |processes_pending_| are zero, then delete the current object by calling
74 void DeleteIfAllDone() {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
77 if (processes_pending_
<= 0 && received_process_group_count_
)
78 RequestContext::Unregister(sequence_number_
);
81 // Register |callback| in |outstanding_requests_| map for the given
83 static void Register(const base::Closure
& callback
, int sequence_number
) {
84 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
86 RequestContext
* request
= new RequestContext(callback
, sequence_number
);
87 outstanding_requests_
.Get()[sequence_number
] = request
;
90 // Find the |RequestContext| in |outstanding_requests_| map for the given
92 static RequestContext
* GetRequestContext(int sequence_number
) {
93 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
95 RequestContextMap::iterator it
=
96 outstanding_requests_
.Get().find(sequence_number
);
97 if (it
== outstanding_requests_
.Get().end())
100 RequestContext
* request
= it
->second
;
101 DCHECK_EQ(sequence_number
, request
->sequence_number_
);
105 // Delete the entry for the given |sequence_number| from
106 // |outstanding_requests_| map. This method is called when all changes have
107 // been acquired, or when the wait time expires (whichever is sooner).
108 static void Unregister(int sequence_number
) {
109 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
111 RequestContextMap::iterator it
=
112 outstanding_requests_
.Get().find(sequence_number
);
113 if (it
== outstanding_requests_
.Get().end())
116 RequestContext
* request
= it
->second
;
117 DCHECK_EQ(sequence_number
, request
->sequence_number_
);
118 bool received_process_group_count
= request
->received_process_group_count_
;
119 int unresponsive_processes
= request
->processes_pending_
;
121 request
->callback_
.Run();
124 outstanding_requests_
.Get().erase(it
);
126 UMA_HISTOGRAM_BOOLEAN("Histogram.ReceivedProcessGroupCount",
127 received_process_group_count
);
128 UMA_HISTOGRAM_COUNTS("Histogram.PendingProcessNotResponding",
129 unresponsive_processes
);
132 // Delete all the entries in |outstanding_requests_| map.
133 static void OnShutdown() {
134 // Just in case we have any pending tasks, clear them out.
135 while (!outstanding_requests_
.Get().empty()) {
136 RequestContextMap::iterator it
= outstanding_requests_
.Get().begin();
138 outstanding_requests_
.Get().erase(it
);
142 // Requests are made to asynchronously send data to the |callback_|.
143 base::Closure callback_
;
145 // The sequence number used by the most recent update request to contact all
147 int sequence_number_
;
149 // Indicates if we have received all pending processes count.
150 bool received_process_group_count_
;
152 // The number of pending processes (all renderer processes and browser child
153 // processes) that have not yet responded to requests.
154 int processes_pending_
;
156 // Map of all outstanding RequestContexts, from sequence_number_ to
158 static base::LazyInstance
<RequestContextMap
>::Leaky outstanding_requests_
;
163 <HistogramSynchronizer::RequestContext::RequestContextMap
>::Leaky
164 HistogramSynchronizer::RequestContext::outstanding_requests_
=
165 LAZY_INSTANCE_INITIALIZER
;
167 HistogramSynchronizer::HistogramSynchronizer()
169 callback_thread_(NULL
),
170 last_used_sequence_number_(kNeverUsableSequenceNumber
),
171 async_sequence_number_(kNeverUsableSequenceNumber
) {
172 HistogramController::GetInstance()->Register(this);
175 HistogramSynchronizer::~HistogramSynchronizer() {
176 RequestContext::OnShutdown();
178 // Just in case we have any pending tasks, clear them out.
179 SetCallbackTaskAndThread(NULL
, base::Closure());
182 HistogramSynchronizer
* HistogramSynchronizer::GetInstance() {
183 return Singleton
<HistogramSynchronizer
,
184 LeakySingletonTraits
<HistogramSynchronizer
> >::get();
188 void HistogramSynchronizer::FetchHistograms() {
189 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
190 BrowserThread::PostTask(
191 BrowserThread::UI
, FROM_HERE
,
192 base::Bind(&HistogramSynchronizer::FetchHistograms
));
195 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
197 HistogramSynchronizer
* current_synchronizer
=
198 HistogramSynchronizer::GetInstance();
199 if (current_synchronizer
== NULL
)
202 current_synchronizer
->RegisterAndNotifyAllProcesses(
203 HistogramSynchronizer::UNKNOWN
,
204 base::TimeDelta::FromMinutes(1));
207 void FetchHistogramsAsynchronously(base::MessageLoop
* callback_thread
,
208 const base::Closure
& callback
,
209 base::TimeDelta wait_time
) {
210 HistogramSynchronizer::FetchHistogramsAsynchronously(
211 callback_thread
, callback
, wait_time
);
215 void HistogramSynchronizer::FetchHistogramsAsynchronously(
216 base::MessageLoop
* callback_thread
,
217 const base::Closure
& callback
,
218 base::TimeDelta wait_time
) {
219 DCHECK(callback_thread
!= NULL
);
220 DCHECK(!callback
.is_null());
222 HistogramSynchronizer
* current_synchronizer
=
223 HistogramSynchronizer::GetInstance();
224 current_synchronizer
->SetCallbackTaskAndThread(
225 callback_thread
, callback
);
227 current_synchronizer
->RegisterAndNotifyAllProcesses(
228 HistogramSynchronizer::ASYNC_HISTOGRAMS
, wait_time
);
231 void HistogramSynchronizer::RegisterAndNotifyAllProcesses(
232 ProcessHistogramRequester requester
,
233 base::TimeDelta wait_time
) {
234 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
236 int sequence_number
= GetNextAvailableSequenceNumber(requester
);
238 base::Closure callback
= base::Bind(
239 &HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback
,
240 base::Unretained(this),
243 RequestContext::Register(callback
, sequence_number
);
245 // Get histogram data from renderer and browser child processes.
246 HistogramController::GetInstance()->GetHistogramData(sequence_number
);
248 // Post a task that would be called after waiting for wait_time. This acts
249 // as a watchdog, to cancel the requests for non-responsive processes.
250 BrowserThread::PostDelayedTask(
251 BrowserThread::UI
, FROM_HERE
,
252 base::Bind(&RequestContext::Unregister
, sequence_number
),
256 void HistogramSynchronizer::OnPendingProcesses(int sequence_number
,
257 int pending_processes
,
259 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
261 RequestContext
* request
= RequestContext::GetRequestContext(sequence_number
);
264 request
->AddProcessesPending(pending_processes
);
265 request
->SetReceivedProcessGroupCount(end
);
266 request
->DeleteIfAllDone();
269 void HistogramSynchronizer::OnHistogramDataCollected(
271 const std::vector
<std::string
>& pickled_histograms
) {
272 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
274 base::HistogramDeltaSerialization::DeserializeAndAddSamples(
277 RequestContext
* request
= RequestContext::GetRequestContext(sequence_number
);
281 // Delete request if we have heard back from all child processes.
282 request
->DecrementProcessesPending();
283 request
->DeleteIfAllDone();
286 void HistogramSynchronizer::SetCallbackTaskAndThread(
287 base::MessageLoop
* callback_thread
,
288 const base::Closure
& callback
) {
289 base::Closure old_callback
;
290 base::MessageLoop
* old_thread
= NULL
;
292 base::AutoLock
auto_lock(lock_
);
293 old_callback
= callback_
;
294 callback_
= callback
;
295 old_thread
= callback_thread_
;
296 callback_thread_
= callback_thread
;
297 // Prevent premature calling of our new callbacks.
298 async_sequence_number_
= kNeverUsableSequenceNumber
;
300 // Just in case there was a task pending....
301 InternalPostTask(old_thread
, old_callback
);
304 void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback(
305 int sequence_number
) {
306 base::Closure callback
;
307 base::MessageLoop
* thread
= NULL
;
309 base::AutoLock
lock(lock_
);
310 if (sequence_number
!= async_sequence_number_
)
312 callback
= callback_
;
313 thread
= callback_thread_
;
315 callback_thread_
= NULL
;
317 InternalPostTask(thread
, callback
);
320 void HistogramSynchronizer::InternalPostTask(base::MessageLoop
* thread
,
321 const base::Closure
& callback
) {
322 if (callback
.is_null() || !thread
)
324 thread
->task_runner()->PostTask(FROM_HERE
, callback
);
327 int HistogramSynchronizer::GetNextAvailableSequenceNumber(
328 ProcessHistogramRequester requester
) {
329 base::AutoLock
auto_lock(lock_
);
330 ++last_used_sequence_number_
;
331 // Watch out for wrapping to a negative number.
332 if (last_used_sequence_number_
< 0) {
333 // Bypass the reserved number, which is used when a renderer spontaneously
334 // decides to send some histogram data.
335 last_used_sequence_number_
=
336 kHistogramSynchronizerReservedSequenceNumber
+ 1;
338 DCHECK_NE(last_used_sequence_number_
,
339 kHistogramSynchronizerReservedSequenceNumber
);
340 if (requester
== ASYNC_HISTOGRAMS
)
341 async_sequence_number_
= last_used_sequence_number_
;
342 return last_used_sequence_number_
;
345 } // namespace content