1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/histogram_synchronizer.h"
#include <utility>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_delta_serialization.h"
#include "base/metrics/histogram_macros.h"
#include "base/pickle.h"
#include "base/single_thread_task_runner.h"
#include "base/task/post_task.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "content/browser/histogram_controller.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/histogram_fetcher.h"
#include "content/public/common/content_constants.h"
namespace content {
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
namespace {
// Negative numbers are never used as sequence numbers. We explicitly pick a
// negative number that is "so negative" that even when we add one (as is done
// when we generated the next sequence number) that it will still be negative.
// We have code that handles wrapping around on an overflow into negative
// territory.
static const int kNeverUsableSequenceNumber = -2;
} // anonymous namespace
// The "RequestContext" structure describes an individual request received from
// the UI. All methods are accessible on UI thread.
class HistogramSynchronizer::RequestContext {
public:
// A map from sequence_number_ to the actual RequestContexts.
typedef std::map<int, RequestContext*> RequestContextMap;
RequestContext(base::OnceClosure callback, int sequence_number)
: callback_(std::move(callback)),
sequence_number_(sequence_number),
received_process_group_count_(0),
processes_pending_(0) {}
~RequestContext() {}
void SetReceivedProcessGroupCount(bool done) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
received_process_group_count_ = done;
}
// Methods for book keeping of processes_pending_.
void AddProcessesPending(int processes_pending) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
processes_pending_ += processes_pending;
}
void DecrementProcessesPending() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
--processes_pending_;
}
// Records that we are waiting for one less histogram data from a process for
// the given sequence number. If |received_process_group_count_| and
// |processes_pending_| are zero, then delete the current object by calling
// Unregister.
void DeleteIfAllDone() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (processes_pending_ <= 0 && received_process_group_count_)
RequestContext::Unregister(sequence_number_);
}
// Register |callback| in |outstanding_requests_| map for the given
// |sequence_number|.
static void Register(base::OnceClosure callback, int sequence_number) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RequestContext* request =
new RequestContext(std::move(callback), sequence_number);
outstanding_requests_.Get()[sequence_number] = request;
}
// Find the |RequestContext| in |outstanding_requests_| map for the given
// |sequence_number|.
static RequestContext* GetRequestContext(int sequence_number) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto it = outstanding_requests_.Get().find(sequence_number);
if (it == outstanding_requests_.Get().end())
return nullptr;
RequestContext* request = it->second;
DCHECK_EQ(sequence_number, request->sequence_number_);
return request;
}
// Delete the entry for the given |sequence_number| from
// |outstanding_requests_| map. This method is called when all changes have
// been acquired, or when the wait time expires (whichever is sooner).
static void Unregister(int sequence_number) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto it = outstanding_requests_.Get().find(sequence_number);
if (it == outstanding_requests_.Get().end())
return;
RequestContext* request = it->second;
DCHECK_EQ(sequence_number, request->sequence_number_);
bool received_process_group_count = request->received_process_group_count_;
int unresponsive_processes = request->processes_pending_;
std::move(request->callback_).Run();
delete request;
outstanding_requests_.Get().erase(it);
UMA_HISTOGRAM_BOOLEAN("Histogram.ReceivedProcessGroupCount",
received_process_group_count);
UMA_HISTOGRAM_COUNTS_1M("Histogram.PendingProcessNotResponding",
unresponsive_processes);
}
// Delete all the entries in |outstanding_requests_| map.
static void OnShutdown() {
// Just in case we have any pending tasks, clear them out.
while (!outstanding_requests_.Get().empty()) {
auto it = outstanding_requests_.Get().begin();
delete it->second;
outstanding_requests_.Get().erase(it);
}
}
// Requests are made to asynchronously send data to the |callback_|.
base::OnceClosure callback_;
// The sequence number used by the most recent update request to contact all
// processes.
int sequence_number_;
// Indicates if we have received all pending processes count.
bool received_process_group_count_;
// The number of pending processes (all renderer processes and browser child
// processes) that have not yet responded to requests.
int processes_pending_;
// Map of all outstanding RequestContexts, from sequence_number_ to
// RequestContext.
static base::LazyInstance<RequestContextMap>::Leaky outstanding_requests_;
};
// static
base::LazyInstance
<HistogramSynchronizer::RequestContext::RequestContextMap>::Leaky
HistogramSynchronizer::RequestContext::outstanding_requests_ =
LAZY_INSTANCE_INITIALIZER;
HistogramSynchronizer::HistogramSynchronizer()
: lock_(),
last_used_sequence_number_(kNeverUsableSequenceNumber),
async_sequence_number_(kNeverUsableSequenceNumber) {
HistogramController::GetInstance()->Register(this);
}
HistogramSynchronizer::~HistogramSynchronizer() {
RequestContext::OnShutdown();
// Just in case we have any pending tasks, clear them out.
SetTaskRunnerAndCallback(nullptr, base::NullCallback());
}
HistogramSynchronizer* HistogramSynchronizer::GetInstance() {
return base::Singleton<
HistogramSynchronizer,
base::LeakySingletonTraits<HistogramSynchronizer>>::get();
}
// static
void HistogramSynchronizer::FetchHistograms() {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&HistogramSynchronizer::FetchHistograms));
return;
}
DCHECK_CURRENTLY_ON(BrowserThread::UI);
HistogramSynchronizer* current_synchronizer =
HistogramSynchronizer::GetInstance();
if (current_synchronizer == nullptr)
return;
current_synchronizer->RegisterAndNotifyAllProcesses(
HistogramSynchronizer::UNKNOWN,
base::TimeDelta::FromMinutes(1));
}
void FetchHistogramsAsynchronously(scoped_refptr<base::TaskRunner> task_runner,
base::OnceClosure callback,
base::TimeDelta wait_time) {
HistogramSynchronizer::FetchHistogramsAsynchronously(
std::move(task_runner), std::move(callback), wait_time);
}
// static
void HistogramSynchronizer::FetchHistogramsAsynchronously(
scoped_refptr<base::TaskRunner> task_runner,
base::OnceClosure callback,
base::TimeDelta wait_time) {
DCHECK(task_runner);
DCHECK(callback);
HistogramSynchronizer* current_synchronizer =
HistogramSynchronizer::GetInstance();
current_synchronizer->SetTaskRunnerAndCallback(std::move(task_runner),
std::move(callback));
current_synchronizer->RegisterAndNotifyAllProcesses(
HistogramSynchronizer::ASYNC_HISTOGRAMS, wait_time);
}
void HistogramSynchronizer::RegisterAndNotifyAllProcesses(
ProcessHistogramRequester requester,
base::TimeDelta wait_time) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
int sequence_number = GetNextAvailableSequenceNumber(requester);
base::OnceClosure callback = base::BindOnce(
&HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback,
base::Unretained(this), sequence_number);
RequestContext::Register(std::move(callback), sequence_number);
// Get histogram data from renderer and browser child processes.
HistogramController::GetInstance()->GetHistogramData(sequence_number);
// Post a task that would be called after waiting for wait_time. This acts
// as a watchdog, to cancel the requests for non-responsive processes.
base::PostDelayedTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&RequestContext::Unregister, sequence_number), wait_time);
}
void HistogramSynchronizer::OnPendingProcesses(int sequence_number,
int pending_processes,
bool end) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RequestContext* request = RequestContext::GetRequestContext(sequence_number);
if (!request)
return;
request->AddProcessesPending(pending_processes);
request->SetReceivedProcessGroupCount(end);
request->DeleteIfAllDone();
}
void HistogramSynchronizer::OnHistogramDataCollected(
int sequence_number,
const std::vector<std::string>& pickled_histograms) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::HistogramDeltaSerialization::DeserializeAndAddSamples(
pickled_histograms);
RequestContext* request = RequestContext::GetRequestContext(sequence_number);
if (!request)
return;
// Delete request if we have heard back from all child processes.
request->DecrementProcessesPending();
request->DeleteIfAllDone();
}
void HistogramSynchronizer::SetTaskRunnerAndCallback(
scoped_refptr<base::TaskRunner> task_runner,
base::OnceClosure callback) {
base::OnceClosure old_callback;
scoped_refptr<base::TaskRunner> old_task_runner;
{
base::AutoLock auto_lock(lock_);
old_callback = std::move(callback_);
callback_ = std::move(callback);
old_task_runner = std::move(callback_task_runner_);
callback_task_runner_ = std::move(task_runner);
// Prevent premature calling of our new callbacks.
async_sequence_number_ = kNeverUsableSequenceNumber;
}
// Just in case there was a task pending....
InternalPostTask(std::move(old_task_runner), std::move(old_callback));
}
void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback(
int sequence_number) {
base::OnceClosure callback;
scoped_refptr<base::TaskRunner> task_runner;
{
base::AutoLock lock(lock_);
if (sequence_number != async_sequence_number_)
return;
callback = std::move(callback_);
task_runner = std::move(callback_task_runner_);
callback_.Reset();
}
InternalPostTask(std::move(task_runner), std::move(callback));
}
void HistogramSynchronizer::InternalPostTask(
scoped_refptr<base::TaskRunner> task_runner,
base::OnceClosure callback) {
if (callback.is_null() || !task_runner)
return;
task_runner->PostTask(FROM_HERE, std::move(callback));
}
int HistogramSynchronizer::GetNextAvailableSequenceNumber(
ProcessHistogramRequester requester) {
base::AutoLock auto_lock(lock_);
++last_used_sequence_number_;
// Watch out for wrapping to a negative number.
if (last_used_sequence_number_ < 0) {
// Bypass the reserved number, which is used when a renderer spontaneously
// decides to send some histogram data.
last_used_sequence_number_ =
kHistogramSynchronizerReservedSequenceNumber + 1;
}
DCHECK_NE(last_used_sequence_number_,
kHistogramSynchronizerReservedSequenceNumber);
if (requester == ASYNC_HISTOGRAMS)
async_sequence_number_ = last_used_sequence_number_;
return last_used_sequence_number_;
}
} // namespace content
|