blob: 4726f18be93902f746daba4352153f41d4473009 [file] [log] [blame]
[email protected]83ab4a282012-07-12 18:19:451// 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.
4
5#include "content/browser/histogram_synchronizer.h"
6
fdoray0c755de2016-10-19 15:28:447#include <utility>
8
[email protected]83ab4a282012-07-12 18:19:459#include "base/bind.h"
10#include "base/lazy_instance.h"
skyostil95082a62015-06-05 19:53:0711#include "base/location.h"
[email protected]83ab4a282012-07-12 18:19:4512#include "base/logging.h"
[email protected]6afa90f2013-10-23 01:16:0413#include "base/metrics/histogram_delta_serialization.h"
asvitkine30330812016-08-30 04:01:0814#include "base/metrics/histogram_macros.h"
[email protected]c50c21d2013-01-11 21:52:4415#include "base/pickle.h"
skyostil95082a62015-06-05 19:53:0716#include "base/single_thread_task_runner.h"
[email protected]83ab4a282012-07-12 18:19:4517#include "base/threading/thread.h"
18#include "base/threading/thread_restrictions.h"
19#include "content/browser/histogram_controller.h"
20#include "content/public/browser/browser_thread.h"
21#include "content/public/browser/histogram_fetcher.h"
22#include "content/public/common/content_constants.h"
23
Daniel Bratellb8d076c2018-01-03 10:41:4724namespace content {
25
[email protected]83ab4a282012-07-12 18:19:4526using base::Time;
27using base::TimeDelta;
28using base::TimeTicks;
29
30namespace {
31
32// Negative numbers are never used as sequence numbers. We explicitly pick a
33// negative number that is "so negative" that even when we add one (as is done
34// when we generated the next sequence number) that it will still be negative.
35// We have code that handles wrapping around on an overflow into negative
36// territory.
37static const int kNeverUsableSequenceNumber = -2;
38
39} // anonymous namespace
40
[email protected]83ab4a282012-07-12 18:19:4541// The "RequestContext" structure describes an individual request received from
42// the UI. All methods are accessible on UI thread.
43class HistogramSynchronizer::RequestContext {
44 public:
45 // A map from sequence_number_ to the actual RequestContexts.
46 typedef std::map<int, RequestContext*> RequestContextMap;
47
48 RequestContext(const base::Closure& callback, int sequence_number)
49 : callback_(callback),
50 sequence_number_(sequence_number),
51 received_process_group_count_(0),
52 processes_pending_(0) {
53 }
54 ~RequestContext() {}
55
56 void SetReceivedProcessGroupCount(bool done) {
mostynb4c27d042015-03-18 21:47:4757 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:4558 received_process_group_count_ = done;
59 }
60
61 // Methods for book keeping of processes_pending_.
62 void AddProcessesPending(int processes_pending) {
mostynb4c27d042015-03-18 21:47:4763 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:4564 processes_pending_ += processes_pending;
65 }
66
67 void DecrementProcessesPending() {
mostynb4c27d042015-03-18 21:47:4768 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:4569 --processes_pending_;
70 }
71
72 // Records that we are waiting for one less histogram data from a process for
73 // the given sequence number. If |received_process_group_count_| and
74 // |processes_pending_| are zero, then delete the current object by calling
75 // Unregister.
76 void DeleteIfAllDone() {
mostynb4c27d042015-03-18 21:47:4777 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:4578
79 if (processes_pending_ <= 0 && received_process_group_count_)
80 RequestContext::Unregister(sequence_number_);
81 }
82
83 // Register |callback| in |outstanding_requests_| map for the given
84 // |sequence_number|.
85 static void Register(const base::Closure& callback, int sequence_number) {
mostynb4c27d042015-03-18 21:47:4786 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:4587
88 RequestContext* request = new RequestContext(callback, sequence_number);
89 outstanding_requests_.Get()[sequence_number] = request;
90 }
91
92 // Find the |RequestContext| in |outstanding_requests_| map for the given
93 // |sequence_number|.
94 static RequestContext* GetRequestContext(int sequence_number) {
mostynb4c27d042015-03-18 21:47:4795 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:4596
97 RequestContextMap::iterator it =
98 outstanding_requests_.Get().find(sequence_number);
99 if (it == outstanding_requests_.Get().end())
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28100 return nullptr;
[email protected]83ab4a282012-07-12 18:19:45101
102 RequestContext* request = it->second;
103 DCHECK_EQ(sequence_number, request->sequence_number_);
104 return request;
105 }
106
107 // Delete the entry for the given |sequence_number| from
108 // |outstanding_requests_| map. This method is called when all changes have
109 // been acquired, or when the wait time expires (whichever is sooner).
110 static void Unregister(int sequence_number) {
mostynb4c27d042015-03-18 21:47:47111 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:45112
113 RequestContextMap::iterator it =
114 outstanding_requests_.Get().find(sequence_number);
115 if (it == outstanding_requests_.Get().end())
116 return;
117
118 RequestContext* request = it->second;
119 DCHECK_EQ(sequence_number, request->sequence_number_);
120 bool received_process_group_count = request->received_process_group_count_;
121 int unresponsive_processes = request->processes_pending_;
122
123 request->callback_.Run();
124
125 delete request;
126 outstanding_requests_.Get().erase(it);
127
128 UMA_HISTOGRAM_BOOLEAN("Histogram.ReceivedProcessGroupCount",
129 received_process_group_count);
Steven Holte95922222018-09-14 20:06:23130 UMA_HISTOGRAM_COUNTS_1M("Histogram.PendingProcessNotResponding",
131 unresponsive_processes);
[email protected]83ab4a282012-07-12 18:19:45132 }
133
134 // Delete all the entries in |outstanding_requests_| map.
135 static void OnShutdown() {
136 // Just in case we have any pending tasks, clear them out.
137 while (!outstanding_requests_.Get().empty()) {
138 RequestContextMap::iterator it = outstanding_requests_.Get().begin();
139 delete it->second;
140 outstanding_requests_.Get().erase(it);
141 }
142 }
143
144 // Requests are made to asynchronously send data to the |callback_|.
145 base::Closure callback_;
146
147 // The sequence number used by the most recent update request to contact all
148 // processes.
149 int sequence_number_;
150
151 // Indicates if we have received all pending processes count.
152 bool received_process_group_count_;
153
154 // The number of pending processes (all renderer processes and browser child
155 // processes) that have not yet responded to requests.
156 int processes_pending_;
157
158 // Map of all outstanding RequestContexts, from sequence_number_ to
159 // RequestContext.
160 static base::LazyInstance<RequestContextMap>::Leaky outstanding_requests_;
161};
162
163// static
164base::LazyInstance
165 <HistogramSynchronizer::RequestContext::RequestContextMap>::Leaky
166 HistogramSynchronizer::RequestContext::outstanding_requests_ =
167 LAZY_INSTANCE_INITIALIZER;
168
169HistogramSynchronizer::HistogramSynchronizer()
170 : lock_(),
[email protected]83ab4a282012-07-12 18:19:45171 last_used_sequence_number_(kNeverUsableSequenceNumber),
172 async_sequence_number_(kNeverUsableSequenceNumber) {
[email protected]46488322012-10-30 03:22:20173 HistogramController::GetInstance()->Register(this);
[email protected]83ab4a282012-07-12 18:19:45174}
175
176HistogramSynchronizer::~HistogramSynchronizer() {
177 RequestContext::OnShutdown();
178
179 // Just in case we have any pending tasks, clear them out.
fdoray0c755de2016-10-19 15:28:44180 SetTaskRunnerAndCallback(nullptr, base::Closure());
[email protected]83ab4a282012-07-12 18:19:45181}
182
183HistogramSynchronizer* HistogramSynchronizer::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:22184 return base::Singleton<
185 HistogramSynchronizer,
186 base::LeakySingletonTraits<HistogramSynchronizer>>::get();
[email protected]83ab4a282012-07-12 18:19:45187}
188
189// static
190void HistogramSynchronizer::FetchHistograms() {
191 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
192 BrowserThread::PostTask(
193 BrowserThread::UI, FROM_HERE,
tzike2aca992017-09-05 08:50:54194 base::BindOnce(&HistogramSynchronizer::FetchHistograms));
[email protected]83ab4a282012-07-12 18:19:45195 return;
196 }
mostynb4c27d042015-03-18 21:47:47197 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:45198
199 HistogramSynchronizer* current_synchronizer =
200 HistogramSynchronizer::GetInstance();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28201 if (current_synchronizer == nullptr)
[email protected]83ab4a282012-07-12 18:19:45202 return;
203
204 current_synchronizer->RegisterAndNotifyAllProcesses(
205 HistogramSynchronizer::UNKNOWN,
206 base::TimeDelta::FromMinutes(1));
207}
208
fdoray0c755de2016-10-19 15:28:44209void FetchHistogramsAsynchronously(scoped_refptr<base::TaskRunner> task_runner,
[email protected]83ab4a282012-07-12 18:19:45210 const base::Closure& callback,
211 base::TimeDelta wait_time) {
fdoray0c755de2016-10-19 15:28:44212 HistogramSynchronizer::FetchHistogramsAsynchronously(std::move(task_runner),
213 callback, wait_time);
[email protected]83ab4a282012-07-12 18:19:45214}
215
216// static
217void HistogramSynchronizer::FetchHistogramsAsynchronously(
fdoray0c755de2016-10-19 15:28:44218 scoped_refptr<base::TaskRunner> task_runner,
[email protected]83ab4a282012-07-12 18:19:45219 const base::Closure& callback,
220 base::TimeDelta wait_time) {
fdoray0c755de2016-10-19 15:28:44221 DCHECK(task_runner);
[email protected]83ab4a282012-07-12 18:19:45222 DCHECK(!callback.is_null());
223
224 HistogramSynchronizer* current_synchronizer =
225 HistogramSynchronizer::GetInstance();
fdoray0c755de2016-10-19 15:28:44226 current_synchronizer->SetTaskRunnerAndCallback(std::move(task_runner),
227 callback);
[email protected]83ab4a282012-07-12 18:19:45228
229 current_synchronizer->RegisterAndNotifyAllProcesses(
230 HistogramSynchronizer::ASYNC_HISTOGRAMS, wait_time);
231}
232
233void HistogramSynchronizer::RegisterAndNotifyAllProcesses(
234 ProcessHistogramRequester requester,
235 base::TimeDelta wait_time) {
mostynb4c27d042015-03-18 21:47:47236 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:45237
238 int sequence_number = GetNextAvailableSequenceNumber(requester);
239
240 base::Closure callback = base::Bind(
241 &HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback,
242 base::Unretained(this),
243 sequence_number);
244
Tommy Nyquist4b749d02018-03-20 21:46:29245 RequestContext::Register(std::move(callback), sequence_number);
[email protected]83ab4a282012-07-12 18:19:45246
247 // Get histogram data from renderer and browser child processes.
[email protected]46488322012-10-30 03:22:20248 HistogramController::GetInstance()->GetHistogramData(sequence_number);
[email protected]83ab4a282012-07-12 18:19:45249
250 // Post a task that would be called after waiting for wait_time. This acts
251 // as a watchdog, to cancel the requests for non-responsive processes.
252 BrowserThread::PostDelayedTask(
253 BrowserThread::UI, FROM_HERE,
tzike2aca992017-09-05 08:50:54254 base::BindOnce(&RequestContext::Unregister, sequence_number), wait_time);
[email protected]83ab4a282012-07-12 18:19:45255}
256
257void HistogramSynchronizer::OnPendingProcesses(int sequence_number,
258 int pending_processes,
259 bool end) {
mostynb4c27d042015-03-18 21:47:47260 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:45261
262 RequestContext* request = RequestContext::GetRequestContext(sequence_number);
263 if (!request)
264 return;
265 request->AddProcessesPending(pending_processes);
266 request->SetReceivedProcessGroupCount(end);
267 request->DeleteIfAllDone();
268}
269
270void HistogramSynchronizer::OnHistogramDataCollected(
271 int sequence_number,
272 const std::vector<std::string>& pickled_histograms) {
mostynb4c27d042015-03-18 21:47:47273 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]83ab4a282012-07-12 18:19:45274
[email protected]6afa90f2013-10-23 01:16:04275 base::HistogramDeltaSerialization::DeserializeAndAddSamples(
276 pickled_histograms);
277
[email protected]83ab4a282012-07-12 18:19:45278 RequestContext* request = RequestContext::GetRequestContext(sequence_number);
[email protected]83ab4a282012-07-12 18:19:45279 if (!request)
280 return;
281
282 // Delete request if we have heard back from all child processes.
283 request->DecrementProcessesPending();
284 request->DeleteIfAllDone();
285}
286
fdoray0c755de2016-10-19 15:28:44287void HistogramSynchronizer::SetTaskRunnerAndCallback(
288 scoped_refptr<base::TaskRunner> task_runner,
[email protected]83ab4a282012-07-12 18:19:45289 const base::Closure& callback) {
290 base::Closure old_callback;
fdoray0c755de2016-10-19 15:28:44291 scoped_refptr<base::TaskRunner> old_task_runner;
[email protected]83ab4a282012-07-12 18:19:45292 {
293 base::AutoLock auto_lock(lock_);
294 old_callback = callback_;
295 callback_ = callback;
fdoray0c755de2016-10-19 15:28:44296 old_task_runner = std::move(callback_task_runner_);
297 callback_task_runner_ = std::move(task_runner);
[email protected]83ab4a282012-07-12 18:19:45298 // Prevent premature calling of our new callbacks.
299 async_sequence_number_ = kNeverUsableSequenceNumber;
300 }
301 // Just in case there was a task pending....
Tommy Nyquist4b749d02018-03-20 21:46:29302 InternalPostTask(std::move(old_task_runner), std::move(old_callback));
[email protected]83ab4a282012-07-12 18:19:45303}
304
305void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback(
306 int sequence_number) {
307 base::Closure callback;
fdoray0c755de2016-10-19 15:28:44308 scoped_refptr<base::TaskRunner> task_runner;
[email protected]83ab4a282012-07-12 18:19:45309 {
310 base::AutoLock lock(lock_);
311 if (sequence_number != async_sequence_number_)
312 return;
313 callback = callback_;
fdoray0c755de2016-10-19 15:28:44314 task_runner = std::move(callback_task_runner_);
[email protected]83ab4a282012-07-12 18:19:45315 callback_.Reset();
[email protected]83ab4a282012-07-12 18:19:45316 }
Tommy Nyquist4b749d02018-03-20 21:46:29317 InternalPostTask(std::move(task_runner), std::move(callback));
[email protected]83ab4a282012-07-12 18:19:45318}
319
fdoray0c755de2016-10-19 15:28:44320void HistogramSynchronizer::InternalPostTask(
321 scoped_refptr<base::TaskRunner> task_runner,
322 const base::Closure& callback) {
323 if (callback.is_null() || !task_runner)
[email protected]83ab4a282012-07-12 18:19:45324 return;
fdoray0c755de2016-10-19 15:28:44325 task_runner->PostTask(FROM_HERE, callback);
[email protected]83ab4a282012-07-12 18:19:45326}
327
328int HistogramSynchronizer::GetNextAvailableSequenceNumber(
329 ProcessHistogramRequester requester) {
330 base::AutoLock auto_lock(lock_);
331 ++last_used_sequence_number_;
332 // Watch out for wrapping to a negative number.
333 if (last_used_sequence_number_ < 0) {
334 // Bypass the reserved number, which is used when a renderer spontaneously
335 // decides to send some histogram data.
336 last_used_sequence_number_ =
337 kHistogramSynchronizerReservedSequenceNumber + 1;
338 }
339 DCHECK_NE(last_used_sequence_number_,
340 kHistogramSynchronizerReservedSequenceNumber);
341 if (requester == ASYNC_HISTOGRAMS)
342 async_sequence_number_ = last_used_sequence_number_;
343 return last_used_sequence_number_;
344}
345
346} // namespace content