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
|
// Copyright (c) 2009 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 "chrome/browser/metrics/histogram_synchronizer.h"
#include "base/metrics/histogram.h"
#include "base/logging.h"
#include "base/thread.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/render_messages.h"
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
HistogramSynchronizer::HistogramSynchronizer()
: lock_(),
received_all_renderer_historgrams_(&lock_),
callback_task_(NULL),
callback_thread_(NULL),
io_message_loop_(NULL),
next_available_sequence_number_(0),
async_sequence_number_(0),
async_renderers_pending_(0),
async_callback_start_time_(TimeTicks::Now()),
synchronous_sequence_number_(0),
synchronous_renderers_pending_(0) {
DCHECK(histogram_synchronizer_ == NULL);
histogram_synchronizer_ = this;
}
HistogramSynchronizer::~HistogramSynchronizer() {
// Clean up.
delete callback_task_;
callback_task_ = NULL;
callback_thread_ = NULL;
histogram_synchronizer_ = NULL;
}
// static
HistogramSynchronizer* HistogramSynchronizer::CurrentSynchronizer() {
DCHECK(histogram_synchronizer_ != NULL);
return histogram_synchronizer_;
}
void HistogramSynchronizer::FetchRendererHistogramsSynchronously(
TimeDelta wait_time) {
DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI);
int sequence_number = GetNextAvaibleSequenceNumber(SYNCHRONOUS_HISTOGRAMS);
for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
!it.IsAtEnd(); it.Advance()) {
it.GetCurrentValue()->Send(
new ViewMsg_GetRendererHistograms(sequence_number));
IncrementPendingRenderers(SYNCHRONOUS_HISTOGRAMS);
}
TimeTicks start = TimeTicks::Now();
TimeTicks end_time = start + wait_time;
int unresponsive_renderer_count;
{
AutoLock auto_lock(lock_);
while (synchronous_renderers_pending_ > 0 &&
TimeTicks::Now() < end_time) {
wait_time = end_time - TimeTicks::Now();
received_all_renderer_historgrams_.TimedWait(wait_time);
}
unresponsive_renderer_count = synchronous_renderers_pending_;
synchronous_renderers_pending_ = 0;
synchronous_sequence_number_ = 0;
}
UMA_HISTOGRAM_COUNTS("Histogram.RendersNotRespondingSynchronous",
unresponsive_renderer_count);
if (!unresponsive_renderer_count)
UMA_HISTOGRAM_TIMES("Histogram.FetchRendererHistogramsSynchronously",
TimeTicks::Now() - start);
}
// static
void HistogramSynchronizer::FetchRendererHistogramsAsynchronously(
MessageLoop* callback_thread,
Task* callback_task,
int wait_time) {
DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI);
DCHECK(callback_thread != NULL);
DCHECK(callback_task != NULL);
HistogramSynchronizer* current_synchronizer =
HistogramSynchronizer::CurrentSynchronizer();
if (current_synchronizer == NULL) {
// System teardown is happening.
callback_thread->PostTask(FROM_HERE, callback_task);
return;
}
// callback_task_ member can only be accessed on IO thread.
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
current_synchronizer,
&HistogramSynchronizer::SetCallbackTaskToCallAfterGettingHistograms,
callback_thread,
callback_task));
// Tell all renderer processes to send their histograms.
int sequence_number =
current_synchronizer->GetNextAvaibleSequenceNumber(ASYNC_HISTOGRAMS);
for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
!it.IsAtEnd(); it.Advance()) {
it.GetCurrentValue()->Send(
new ViewMsg_GetRendererHistograms(sequence_number));
current_synchronizer->IncrementPendingRenderers(ASYNC_HISTOGRAMS);
}
// Post a task that would be called after waiting for wait_time.
BrowserThread::PostDelayedTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
current_synchronizer,
&HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback,
sequence_number),
wait_time);
}
// static
void HistogramSynchronizer::DeserializeHistogramList(
int sequence_number,
const std::vector<std::string>& histograms) {
HistogramSynchronizer* current_synchronizer =
HistogramSynchronizer::CurrentSynchronizer();
if (current_synchronizer == NULL)
return;
DCHECK(current_synchronizer->IsOnIoThread());
for (std::vector<std::string>::const_iterator it = histograms.begin();
it < histograms.end();
++it) {
base::Histogram::DeserializeHistogramInfo(*it);
}
// Record that we have received a histogram from renderer process.
current_synchronizer->RecordRendererHistogram(sequence_number);
}
bool HistogramSynchronizer::RecordRendererHistogram(int sequence_number) {
DCHECK(IsOnIoThread());
if (sequence_number == async_sequence_number_) {
if ((async_renderers_pending_ == 0) ||
(--async_renderers_pending_ > 0))
return false;
DCHECK(callback_task_ != NULL);
CallCallbackTaskAndResetData();
return true;
}
{
AutoLock auto_lock(lock_);
if (sequence_number != synchronous_sequence_number_) {
// No need to do anything if the sequence_number does not match current
// synchronous_sequence_number_ or async_sequence_number_.
return true;
}
if ((synchronous_renderers_pending_ == 0) ||
(--synchronous_renderers_pending_ > 0))
return false;
DCHECK_EQ(synchronous_renderers_pending_, 0);
}
// We could call Signal() without holding the lock.
received_all_renderer_historgrams_.Signal();
return true;
}
// This method is called on the IO thread.
void HistogramSynchronizer::SetCallbackTaskToCallAfterGettingHistograms(
MessageLoop* callback_thread,
Task* callback_task) {
DCHECK(IsOnIoThread());
// Test for the existence of a previous task, and post call to post it if it
// exists. We promised to post it after some timeout... and at this point, we
// should just force the posting.
if (callback_task_ != NULL) {
CallCallbackTaskAndResetData();
}
// Assert there was no callback_task_ already.
DCHECK(callback_task_ == NULL);
// Save the thread and the callback_task.
DCHECK(callback_thread != NULL);
DCHECK(callback_task != NULL);
callback_task_ = callback_task;
callback_thread_ = callback_thread;
async_callback_start_time_ = TimeTicks::Now();
}
void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback(
int sequence_number) {
DCHECK(IsOnIoThread());
if (sequence_number == async_sequence_number_) {
CallCallbackTaskAndResetData();
}
}
// If wait time has elapsed or if we have received all the histograms from all
// the renderers, call the callback_task if a callback_task exists. This is
// called on IO Thread.
void HistogramSynchronizer::CallCallbackTaskAndResetData() {
DCHECK(IsOnIoThread());
// callback_task_ would be set to NULL, if we have heard from all renderers
// and we would have called the callback_task already.
if (callback_task_ == NULL) {
return;
}
UMA_HISTOGRAM_COUNTS("Histogram.RendersNotRespondingAsynchronous",
async_renderers_pending_);
if (!async_renderers_pending_)
UMA_HISTOGRAM_TIMES("Histogram.FetchRendererHistogramsAsynchronously",
TimeTicks::Now() - async_callback_start_time_);
DCHECK(callback_thread_ != NULL);
DCHECK(callback_task_ != NULL);
callback_thread_->PostTask(FROM_HERE, callback_task_);
async_renderers_pending_ = 0;
async_callback_start_time_ = TimeTicks::Now();
callback_task_ = NULL;
callback_thread_ = NULL;
}
int HistogramSynchronizer::GetNextAvaibleSequenceNumber(
RendererHistogramRequester requester) {
AutoLock auto_lock(lock_);
++next_available_sequence_number_;
if (0 > next_available_sequence_number_) {
// We wrapped around.
next_available_sequence_number_ =
chrome::kHistogramSynchronizerReservedSequenceNumber + 1;
}
DCHECK_NE(next_available_sequence_number_,
chrome::kHistogramSynchronizerReservedSequenceNumber);
if (requester == ASYNC_HISTOGRAMS) {
async_sequence_number_ = next_available_sequence_number_;
async_renderers_pending_ = 0;
} else if (requester == SYNCHRONOUS_HISTOGRAMS) {
synchronous_sequence_number_ = next_available_sequence_number_;
synchronous_renderers_pending_ = 0;
}
return next_available_sequence_number_;
}
void HistogramSynchronizer::IncrementPendingRenderers(
RendererHistogramRequester requester) {
if (requester == ASYNC_HISTOGRAMS) {
async_renderers_pending_++;
} else {
synchronous_renderers_pending_++;
}
}
bool HistogramSynchronizer::IsOnIoThread() {
if (io_message_loop_ == NULL) {
io_message_loop_ = MessageLoop::current();
}
return (MessageLoop::current() == io_message_loop_);
}
// static
HistogramSynchronizer* HistogramSynchronizer::histogram_synchronizer_ = NULL;
|