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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
// Copyright (c) 2010 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/net/passive_log_collector.h"
#include <algorithm>
#include "base/string_util.h"
#include "base/format_macros.h"
#include "chrome/browser/chrome_thread.h"
namespace {
const size_t kMaxNumEntriesPerLog = 50;
const size_t kMaxConnectJobGraveyardSize = 3;
const size_t kMaxRequestGraveyardSize = 25;
const size_t kMaxLiveRequests = 200;
// Sort function on source ID.
bool OrderBySourceID(const PassiveLogCollector::RequestInfo& a,
const PassiveLogCollector::RequestInfo& b) {
return a.source_id < b.source_id;
}
void AddEntryToRequestInfo(const PassiveLogCollector::Entry& entry,
bool is_unbounded,
PassiveLogCollector::RequestInfo* out_info) {
// Start dropping new entries when the log has gotten too big.
if (out_info->entries.size() + 1 <= kMaxNumEntriesPerLog || is_unbounded) {
out_info->entries.push_back(entry);
} else {
out_info->num_entries_truncated += 1;
out_info->entries[kMaxNumEntriesPerLog - 1] = entry;
}
}
void AppendToRequestInfo(const PassiveLogCollector::RequestInfo& info,
bool is_unbounded,
PassiveLogCollector::RequestInfo* out_info) {
for (size_t i = 0; i < info.entries.size(); ++i)
AddEntryToRequestInfo(info.entries[i], is_unbounded, out_info);
}
// Appends all of the logged events in |input| to |out|.
void AppendAllEntriesFromRequests(
const PassiveLogCollector::RequestInfoList& input,
PassiveLogCollector::EntryList* out) {
for (size_t i = 0; i < input.size(); ++i) {
const PassiveLogCollector::EntryList& entries = input[i].entries;
out->insert(out->end(), entries.begin(), entries.end());
}
}
// Comparator to sort entries by their |order| property, ascending.
bool SortByOrderComparator(const PassiveLogCollector::Entry& a,
const PassiveLogCollector::Entry& b) {
return a.order < b.order;
}
void SetSubordinateSource(PassiveLogCollector::RequestInfo* info,
const PassiveLogCollector::Entry& entry) {
info->subordinate_source.id = static_cast<net::NetLogIntegerParameter*>(
entry.extra_parameters.get())->value();
switch (entry.type) {
case net::NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_ID:
info->subordinate_source.type = net::NetLog::SOURCE_CONNECT_JOB;
break;
case net::NetLog::TYPE_SOCKET_POOL_SOCKET_ID:
info->subordinate_source.type = net::NetLog::SOURCE_SOCKET;
break;
default:
NOTREACHED();
break;
}
}
} // namespace
//----------------------------------------------------------------------------
// PassiveLogCollector
//----------------------------------------------------------------------------
PassiveLogCollector::PassiveLogCollector()
: url_request_tracker_(&connect_job_tracker_, &socket_tracker_),
socket_stream_tracker_(&connect_job_tracker_, &socket_tracker_),
num_events_seen_(0) {
}
PassiveLogCollector::~PassiveLogCollector() {
}
void PassiveLogCollector::OnAddEntry(
net::NetLog::EventType type,
const base::TimeTicks& time,
const net::NetLog::Source& source,
net::NetLog::EventPhase phase,
net::NetLog::EventParameters* extra_parameters) {
// Package the parameters into a single struct for convenience.
Entry entry(num_events_seen_++, type, time, source, phase, extra_parameters);
switch (entry.source.type) {
case net::NetLog::SOURCE_URL_REQUEST:
url_request_tracker_.OnAddEntry(entry);
break;
case net::NetLog::SOURCE_SOCKET_STREAM:
socket_stream_tracker_.OnAddEntry(entry);
break;
case net::NetLog::SOURCE_CONNECT_JOB:
connect_job_tracker_.OnAddEntry(entry);
break;
case net::NetLog::SOURCE_SOCKET:
socket_tracker_.OnAddEntry(entry);
break;
case net::NetLog::SOURCE_INIT_PROXY_RESOLVER:
init_proxy_resolver_tracker_.OnAddEntry(entry);
break;
default:
// Drop all other logged events.
break;
}
}
void PassiveLogCollector::Clear() {
connect_job_tracker_.Clear();
url_request_tracker_.Clear();
socket_stream_tracker_.Clear();
}
void PassiveLogCollector::GetAllCapturedEvents(EntryList* out) const {
out->clear();
// Append all of the captured entries held by the various trackers to
// |out|.
socket_stream_tracker_.AppendAllEntries(out);
url_request_tracker_.AppendAllEntries(out);
const EntryList& proxy_entries =
init_proxy_resolver_tracker_.entries();
out->insert(out->end(), proxy_entries.begin(), proxy_entries.end());
// Now sort the list of entries by their insertion time (ascending).
std::sort(out->begin(), out->end(), &SortByOrderComparator);
}
//----------------------------------------------------------------------------
// RequestTrackerBase
//----------------------------------------------------------------------------
PassiveLogCollector::RequestTrackerBase::RequestTrackerBase(
size_t max_graveyard_size)
: max_graveyard_size_(max_graveyard_size),
next_graveyard_index_(0),
is_unbounded_(false) {
}
void PassiveLogCollector::RequestTrackerBase::OnAddEntry(const Entry& entry) {
RequestInfo& info = live_requests_[entry.source.id];
info.source_id = entry.source.id; // In case this is a new entry.
Action result = DoAddEntry(entry, &info);
switch (result) {
case ACTION_MOVE_TO_GRAVEYARD:
InsertIntoGraveyard(info);
// (fall-through)
case ACTION_DELETE:
RemoveFromLiveRequests(entry.source.id);
break;
default:
break;
}
if (live_requests_.size() > kMaxLiveRequests) {
// This is a safety net in case something went wrong, to avoid continually
// growing memory.
LOG(WARNING) << "The passive log data has grown larger "
"than expected, resetting";
live_requests_.clear();
}
}
PassiveLogCollector::RequestInfoList
PassiveLogCollector::RequestTrackerBase::GetLiveRequests() const {
RequestInfoList list;
// Copy all of the live requests into the vector.
for (SourceIDToInfoMap::const_iterator it = live_requests_.begin();
it != live_requests_.end();
++it) {
list.push_back(it->second);
// We pass the copy (made by the list insert), so changes made in
// OnLiveRequest are only seen by our caller.
OnLiveRequest(&list.back());
std::sort(list.back().entries.begin(), list.back().entries.end(),
SortByOrderComparator);
}
std::sort(list.begin(), list.end(), OrderBySourceID);
return list;
}
void PassiveLogCollector::RequestTrackerBase::ClearRecentlyDeceased() {
next_graveyard_index_ = 0;
graveyard_.clear();
}
// Returns a list of recently completed Requests.
PassiveLogCollector::RequestInfoList
PassiveLogCollector::RequestTrackerBase::GetRecentlyDeceased() const {
RequestInfoList list;
// Copy the items from |graveyard_| (our circular queue of recently
// deceased request infos) into a vector, ordered from oldest to newest.
for (size_t i = 0; i < graveyard_.size(); ++i) {
size_t index = (next_graveyard_index_ + i) % graveyard_.size();
list.push_back(graveyard_[index]);
}
return list;
}
PassiveLogCollector::RequestInfo*
PassiveLogCollector::RequestTrackerBase::GetRequestInfo(uint32 source_id) {
// Look for it in the live requests first.
SourceIDToInfoMap::iterator it = live_requests_.find(source_id);
if (it != live_requests_.end())
return &(it->second);
// Otherwise, scan through the graveyard to find an entry for |source_id|.
for (size_t i = 0; i < graveyard_.size(); ++i) {
if (graveyard_[i].source_id == source_id) {
return &graveyard_[i];
}
}
return NULL;
}
void PassiveLogCollector::RequestTrackerBase::RemoveFromLiveRequests(
uint32 source_id) {
// Remove from |live_requests_|.
SourceIDToInfoMap::iterator it = live_requests_.find(source_id);
// TODO(eroman): Shouldn't have this 'if', is it actually really necessary?
if (it != live_requests_.end())
live_requests_.erase(it);
}
void PassiveLogCollector::RequestTrackerBase::SetUnbounded(
bool unbounded) {
// No change.
if (is_unbounded_ == unbounded)
return;
// If we are going from unbounded to bounded, we need to trim the
// graveyard. For simplicity we will simply clear it.
if (is_unbounded_ && !unbounded)
ClearRecentlyDeceased();
is_unbounded_ = unbounded;
}
void PassiveLogCollector::RequestTrackerBase::Clear() {
ClearRecentlyDeceased();
live_requests_.clear();
}
void PassiveLogCollector::RequestTrackerBase::AppendAllEntries(
EntryList* out) const {
AppendAllEntriesFromRequests(GetLiveRequests(), out);
AppendAllEntriesFromRequests(GetRecentlyDeceased(), out);
}
void PassiveLogCollector::RequestTrackerBase::InsertIntoGraveyard(
const RequestInfo& info) {
if (is_unbounded_) {
graveyard_.push_back(info);
return;
}
// Otherwise enforce a bound on the graveyard size, by treating it as a
// circular buffer.
if (graveyard_.size() < max_graveyard_size_) {
// Still growing to maximum capacity.
DCHECK_EQ(next_graveyard_index_, graveyard_.size());
graveyard_.push_back(info);
} else {
// At maximum capacity, overwite the oldest entry.
graveyard_[next_graveyard_index_] = info;
}
next_graveyard_index_ = (next_graveyard_index_ + 1) % max_graveyard_size_;
}
//----------------------------------------------------------------------------
// ConnectJobTracker
//----------------------------------------------------------------------------
const size_t PassiveLogCollector::ConnectJobTracker::kMaxGraveyardSize = 15;
PassiveLogCollector::ConnectJobTracker::ConnectJobTracker()
: RequestTrackerBase(kMaxGraveyardSize) {
}
PassiveLogCollector::RequestTrackerBase::Action
PassiveLogCollector::ConnectJobTracker::DoAddEntry(const Entry& entry,
RequestInfo* out_info) {
AddEntryToRequestInfo(entry, is_unbounded(), out_info);
if (entry.type == net::NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_ID) {
SetSubordinateSource(out_info, entry);
}
// If this is the end of the connect job, move the request to the graveyard.
if (entry.type == net::NetLog::TYPE_SOCKET_POOL_CONNECT_JOB &&
entry.phase == net::NetLog::PHASE_END) {
return ACTION_MOVE_TO_GRAVEYARD;
}
return ACTION_NONE;
}
void PassiveLogCollector::ConnectJobTracker::AppendLogEntries(
RequestInfo* out_info, bool unbounded, uint32 connect_id) {
RequestInfo* connect_info = GetRequestInfo(connect_id);
if (!connect_info) {
net::NetLogStringParameter* text = new net::NetLogStringParameter(
"todo", StringPrintf("Used ConnectJob id=%u", connect_id));
Entry new_entry(0, net::NetLog::TYPE_TODO_STRING, base::TimeTicks(),
net::NetLog::Source(net::NetLog::SOURCE_CONNECT_JOB,
connect_id),
net::NetLog::PHASE_NONE, text);
AddEntryToRequestInfo(new_entry, unbounded, out_info);
return;
}
AppendToRequestInfo(*connect_info, unbounded, out_info);
std::sort(out_info->entries.begin(), out_info->entries.end(),
&SortByOrderComparator);
out_info->num_entries_truncated += connect_info->num_entries_truncated;
if (connect_info->subordinate_source.is_valid())
AppendLogEntries(out_info, unbounded, connect_info->subordinate_source.id);
}
//----------------------------------------------------------------------------
// SocketTracker
//----------------------------------------------------------------------------
const size_t PassiveLogCollector::SocketTracker::kMaxGraveyardSize = 15;
PassiveLogCollector::SocketTracker::SocketTracker()
: RequestTrackerBase(kMaxGraveyardSize) {
}
PassiveLogCollector::RequestTrackerBase::Action
PassiveLogCollector::SocketTracker::DoAddEntry(const Entry& entry,
RequestInfo* out_info) {
int int_arg;
switch (entry.type) {
case net::NetLog::TYPE_SOCKET_BYTES_SENT:
int_arg = static_cast<net::NetLogIntegerParameter*>(
entry.extra_parameters.get())->value();
out_info->total_bytes_transmitted += int_arg;
out_info->bytes_transmitted += int_arg;
out_info->last_tx_rx_time = entry.time;
out_info->last_tx_rx_position = entry.order;
break;
case net::NetLog::TYPE_SOCKET_BYTES_RECEIVED:
int_arg = static_cast<net::NetLogIntegerParameter*>(
entry.extra_parameters.get())->value();
out_info->total_bytes_received += int_arg;
out_info->bytes_received += int_arg;
out_info->last_tx_rx_time = entry.time;
out_info->last_tx_rx_position = entry.order;
break;
case net::NetLog::TYPE_TCP_SOCKET_DONE:
return ACTION_MOVE_TO_GRAVEYARD;
default:
AddEntryToRequestInfo(entry, is_unbounded(), out_info);
break;
}
return ACTION_NONE;
}
void PassiveLogCollector::SocketTracker::AppendLogEntries(
RequestInfo* out_info, bool unbounded, uint32 socket_id, bool clear) {
RequestInfo* socket_info = GetRequestInfo(socket_id);
if (!socket_info) {
net::NetLogStringParameter* text = new net::NetLogStringParameter(
"todo", StringPrintf("Used Socket id=%u.", socket_id));
Entry new_entry(0, net::NetLog::TYPE_TODO_STRING, base::TimeTicks(),
net::NetLog::Source(net::NetLog::SOURCE_SOCKET, socket_id),
net::NetLog::PHASE_NONE, text);
AddEntryToRequestInfo(new_entry, unbounded, out_info);
return;
}
AppendToRequestInfo(*socket_info, unbounded, out_info);
out_info->num_entries_truncated += socket_info->num_entries_truncated;
// Synthesize a log entry for bytes sent and received.
if (socket_info->bytes_transmitted > 0 || socket_info->bytes_received > 0) {
net::NetLogStringParameter* text = new net::NetLogStringParameter(
"stats",
StringPrintf("Tx/Rx: %"PRIu64"/%"PRIu64" [%"PRIu64"/%"PRIu64
" total on socket] (Bytes)",
socket_info->bytes_transmitted,
socket_info->bytes_received,
socket_info->total_bytes_transmitted,
socket_info->total_bytes_received));
Entry new_entry(socket_info->last_tx_rx_position,
net::NetLog::TYPE_TODO_STRING,
socket_info->last_tx_rx_time,
net::NetLog::Source(net::NetLog::SOURCE_SOCKET, socket_id),
net::NetLog::PHASE_NONE,
text);
AddEntryToRequestInfo(new_entry, unbounded, out_info);
}
std::sort(out_info->entries.begin(), out_info->entries.end(),
&SortByOrderComparator);
if (clear)
ClearInfo(socket_info);
}
void PassiveLogCollector::SocketTracker::ClearInfo(RequestInfo* info) {
info->entries.clear();
info->num_entries_truncated = 0;
info->bytes_transmitted = 0;
info->bytes_received = 0;
info->last_tx_rx_position = 0;
info->last_tx_rx_time = base::TimeTicks();
}
//----------------------------------------------------------------------------
// RequestTracker
//----------------------------------------------------------------------------
const size_t PassiveLogCollector::RequestTracker::kMaxGraveyardSize = 25;
const size_t PassiveLogCollector::RequestTracker::kMaxGraveyardURLSize = 1000;
PassiveLogCollector::RequestTracker::RequestTracker(
ConnectJobTracker* connect_job_tracker, SocketTracker* socket_tracker)
: RequestTrackerBase(kMaxGraveyardSize),
connect_job_tracker_(connect_job_tracker),
socket_tracker_(socket_tracker) {
}
PassiveLogCollector::RequestTrackerBase::Action
PassiveLogCollector::RequestTracker::DoAddEntry(const Entry& entry,
RequestInfo* out_info) {
// We expect up to three events with IDs.
// - Begin SOCKET_POOL_CONNECT_JOB_ID: Means a ConnectJob was created for
// this request. Including it for now, but the resulting socket may be
// used for a different request.
// - End SOCKET_POOL_CONNECT_JOB_ID: The named ConnectJob completed and
// this request will be getting the socket from that request.
// - SOCKET_POOL_SOCKET_ID: The given socket will be used for this request.
//
// The action to take when seeing these events depends on the current
// content of the |subordinate_source| field:
// |subordinate_source| is invalid (fresh state).
// - Begin SOCKET_POOL_CONNECT_JOB_ID: Set |subordinate_source|.
// - End SOCKET_POOL_CONNECT_JOB_ID: Integrate the named ConnectJob ID.
// - SOCKET_POOL_SOCKET_ID: Set |subordinate_source|.
// |subordinate_source| is a ConnectJob:
// - Begin SOCKET_POOL_CONNECT_JOB_ID: Set |subordinate_source|.
// - End SOCKET_POOL_CONNECT_JOB_ID: Integrate the named ConnectJob ID and
// clear the |subordinate_source|.
// - SOCKET_POOL_SOCKET_ID: Set |subordinate_source|. (The request was
// assigned a new idle socket, after starting a ConnectJob.)
// |subordinate_source| is a Socket:
// First, integrate the subordinate socket source, then:
// - Begin SOCKET_POOL_CONNECT_JOB_ID: Set |subordinate_source|.
// (Connection restarted with a new ConnectJob.)
// - End SOCKET_POOL_CONNECT_JOB_ID: Integrate the named ConnectJob ID and
// clear the |subordinate_source|. (Connection restarted with a late bound
// ConnectJob.)
// - SOCKET_POOL_SOCKET_ID: Set |subordinate_source|. (Connection
// restarted and got an idle socket.)
if (entry.type == net::NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_ID ||
entry.type == net::NetLog::TYPE_SOCKET_POOL_SOCKET_ID) {
if (out_info->subordinate_source.is_valid() &&
out_info->subordinate_source.type == net::NetLog::SOURCE_SOCKET)
IntegrateSubordinateSource(out_info, true);
SetSubordinateSource(out_info, entry);
if (entry.phase == net::NetLog::PHASE_END &&
entry.type == net::NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_ID) {
IntegrateSubordinateSource(out_info, true);
out_info->subordinate_source.id = net::NetLog::Source::kInvalidId;
}
}
AddEntryToRequestInfo(entry, is_unbounded(), out_info);
// If this was the start of a URLRequest/SocketStream, extract the URL.
// Note: we look at the first *two* entries, since the outer REQUEST_ALIVE
// doesn't actually contain any data.
if (out_info->url.empty() && out_info->entries.size() <= 2 &&
entry.phase == net::NetLog::PHASE_BEGIN && entry.extra_parameters &&
(entry.type == net::NetLog::TYPE_URL_REQUEST_START ||
entry.type == net::NetLog::TYPE_SOCKET_STREAM_CONNECT)) {
out_info->url = static_cast<net::NetLogStringParameter*>(
entry.extra_parameters.get())->value();
}
// If the request has ended, move it to the graveyard.
if (entry.type == net::NetLog::TYPE_REQUEST_ALIVE &&
entry.phase == net::NetLog::PHASE_END) {
IntegrateSubordinateSource(out_info, true);
if (StartsWithASCII(out_info->url, "chrome://", false)) {
// Avoid sending "chrome://" requests to the graveyard, since it just
// adds to clutter.
return ACTION_DELETE;
}
return ACTION_MOVE_TO_GRAVEYARD;
}
return ACTION_NONE;
}
void PassiveLogCollector::RequestTracker::IntegrateSubordinateSource(
RequestInfo* info, bool clear_entries) const {
if (!info->subordinate_source.is_valid())
return;
uint32 subordinate_id = info->subordinate_source.id;
switch (info->subordinate_source.type) {
case net::NetLog::SOURCE_CONNECT_JOB:
connect_job_tracker_->AppendLogEntries(
info, connect_job_tracker_->is_unbounded(), subordinate_id);
break;
case net::NetLog::SOURCE_SOCKET:
socket_tracker_->AppendLogEntries(info, socket_tracker_->is_unbounded(),
subordinate_id, clear_entries);
break;
default:
NOTREACHED();
break;
}
}
//----------------------------------------------------------------------------
// InitProxyResolverTracker
//----------------------------------------------------------------------------
PassiveLogCollector::InitProxyResolverTracker::InitProxyResolverTracker() {}
void PassiveLogCollector::InitProxyResolverTracker::OnAddEntry(
const Entry& entry) {
if (entry.type == net::NetLog::TYPE_INIT_PROXY_RESOLVER &&
entry.phase == net::NetLog::PHASE_BEGIN) {
// If this is the start of a new InitProxyResolver, overwrite the old data.
entries_.clear();
entries_.push_back(entry);
} else {
// Otherwise append it to the log for the latest InitProxyResolver.
if (!entries_.empty() && entries_[0].source.id != entry.source.id) {
// If this entry doesn't match what we think was the latest
// InitProxyResolver, drop it. (This shouldn't happen, but we will guard
// against it).
return;
}
entries_.push_back(entry);
}
// Safety net: INIT_PROXY_RESOLVER shouldn't generate many messages, but in
// case something goes wrong, avoid exploding the memory usage.
if (entries_.size() > kMaxNumEntriesPerLog)
entries_.clear();
}
|