Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chrome/browser/net/passive_log_collector.h

Issue 4118004: Update NetLog to be thread safe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Response to comments (And net-internals refresh fix) Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ 5 #ifndef CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_
6 #define CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ 6 #define CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_
7 #pragma once 7 #pragma once
8 8
9 #include <deque> 9 #include <deque>
10 #include <string> 10 #include <string>
(...skipping 14 matching lines...) Expand all
25 // problem and it will contain a trace for the problem request. 25 // problem and it will contain a trace for the problem request.
26 // 26 //
27 // (This is in contrast to the "active logging" which captures every single 27 // (This is in contrast to the "active logging" which captures every single
28 // network event, but requires capturing to have been enabled *prior* to 28 // network event, but requires capturing to have been enabled *prior* to
29 // encountering the problem. Active capturing is enabled as long as 29 // encountering the problem. Active capturing is enabled as long as
30 // about:net-internals is open). 30 // about:net-internals is open).
31 // 31 //
32 // The data captured by PassiveLogCollector is grouped by NetLog::Source, into 32 // The data captured by PassiveLogCollector is grouped by NetLog::Source, into
33 // a SourceInfo structure. These in turn are grouped by NetLog::SourceType, and 33 // a SourceInfo structure. These in turn are grouped by NetLog::SourceType, and
34 // owned by a SourceTracker instance for the specific source type. 34 // owned by a SourceTracker instance for the specific source type.
35 class PassiveLogCollector : public ChromeNetLog::Observer { 35 //
36 // The PassiveLogCollector is owned by the ChromeNetLog itself, and is not
37 // threadsafe. The ChromeNetLog is responsible for calling it in a threadsafe
38 // manner.
39 class PassiveLogCollector : public ChromeNetLog::ThreadSafeObserver {
36 public: 40 public:
37 // This structure encapsulates all of the parameters of a captured event,
38 // including an "order" field that identifies when it was captured relative
39 // to other events.
40 struct Entry {
41 Entry(uint32 order,
42 net::NetLog::EventType type,
43 const base::TimeTicks& time,
44 net::NetLog::Source source,
45 net::NetLog::EventPhase phase,
46 net::NetLog::EventParameters* params);
47 ~Entry();
48
49 uint32 order;
50 net::NetLog::EventType type;
51 base::TimeTicks time;
52 net::NetLog::Source source;
53 net::NetLog::EventPhase phase;
54 scoped_refptr<net::NetLog::EventParameters> params;
55 };
56
57 typedef std::vector<Entry> EntryList;
58 typedef std::vector<net::NetLog::Source> SourceDependencyList; 41 typedef std::vector<net::NetLog::Source> SourceDependencyList;
59 42
60 struct SourceInfo { 43 struct SourceInfo {
61 SourceInfo(); 44 SourceInfo();
62 ~SourceInfo(); 45 ~SourceInfo();
63 46
64 // Returns the URL that corresponds with this source. This is 47 // Returns the URL that corresponds with this source. This is
65 // only meaningful for certain source types (URL_REQUEST, SOCKET_STREAM). 48 // only meaningful for certain source types (URL_REQUEST, SOCKET_STREAM).
66 // For the rest, it will return an empty string. 49 // For the rest, it will return an empty string.
67 std::string GetURL() const; 50 std::string GetURL() const;
68 51
69 uint32 source_id; 52 uint32 source_id;
70 EntryList entries; 53 ChromeNetLog::EntryList entries;
71 size_t num_entries_truncated; 54 size_t num_entries_truncated;
72 55
73 // List of other sources which contain information relevant to this 56 // List of other sources which contain information relevant to this
74 // source (for example, a url request might depend on the log items 57 // source (for example, a url request might depend on the log items
75 // for a connect job and for a socket that were bound to it.) 58 // for a connect job and for a socket that were bound to it.)
76 SourceDependencyList dependencies; 59 SourceDependencyList dependencies;
77 60
78 // Holds the count of how many other sources have added this as a 61 // Holds the count of how many other sources have added this as a
79 // dependent source. When it is 0, it means noone has referenced it so it 62 // dependent source. When it is 0, it means noone has referenced it so it
80 // can be deleted normally. 63 // can be deleted normally.
81 int reference_count; 64 int reference_count;
82 65
83 // |is_alive| is set to false once the source has been added to the 66 // |is_alive| is set to false once the source has been added to the
84 // tracker's graveyard (it may still be kept around due to a non-zero 67 // tracker's graveyard (it may still be kept around due to a non-zero
85 // reference_count, but it is still considered "dead"). 68 // reference_count, but it is still considered "dead").
86 bool is_alive; 69 bool is_alive;
87 }; 70 };
88 71
89 typedef std::vector<SourceInfo> SourceInfoList; 72 typedef std::vector<SourceInfo> SourceInfoList;
90 73
91 // Interface for consuming a NetLog entry. 74 // Interface for consuming a NetLog entry.
92 class SourceTrackerInterface { 75 class SourceTrackerInterface {
93 public: 76 public:
94 virtual ~SourceTrackerInterface() {} 77 virtual ~SourceTrackerInterface() {}
95 78
96 virtual void OnAddEntry(const Entry& entry) = 0; 79 virtual void OnAddEntry(const ChromeNetLog::Entry& entry) = 0;
97 80
98 // Clears all the passively logged data from this tracker. 81 // Clears all the passively logged data from this tracker.
99 virtual void Clear() = 0; 82 virtual void Clear() = 0;
100 83
101 // Appends all the captured entries to |out|. The ordering is undefined. 84 // Appends all the captured entries to |out|. The ordering is undefined.
102 virtual void AppendAllEntries(EntryList* out) const = 0; 85 virtual void AppendAllEntries(ChromeNetLog::EntryList* out) const = 0;
103 }; 86 };
104 87
105 // This source tracker is intended for TYPE_NONE. All entries go into a 88 // This source tracker is intended for TYPE_NONE. All entries go into a
106 // circular buffer, and there is no concept of live/dead requests. 89 // circular buffer, and there is no concept of live/dead requests.
107 class GlobalSourceTracker : public SourceTrackerInterface { 90 class GlobalSourceTracker : public SourceTrackerInterface {
108 public: 91 public:
109 GlobalSourceTracker(); 92 GlobalSourceTracker();
110 ~GlobalSourceTracker(); 93 ~GlobalSourceTracker();
111 94
112 // SourceTrackerInterface implementation: 95 // SourceTrackerInterface implementation:
113 virtual void OnAddEntry(const Entry& entry); 96 virtual void OnAddEntry(const ChromeNetLog::Entry& entry);
114 virtual void Clear(); 97 virtual void Clear();
115 virtual void AppendAllEntries(EntryList* out) const; 98 virtual void AppendAllEntries(ChromeNetLog::EntryList* out) const;
116 99
117 private: 100 private:
118 typedef std::deque<Entry> CircularEntryList; 101 typedef std::deque<ChromeNetLog::Entry> CircularEntryList;
119 CircularEntryList entries_; 102 CircularEntryList entries_;
120 DISALLOW_COPY_AND_ASSIGN(GlobalSourceTracker); 103 DISALLOW_COPY_AND_ASSIGN(GlobalSourceTracker);
121 }; 104 };
122 105
123 // This class stores and manages the passively logged information for 106 // This class stores and manages the passively logged information for
124 // URLRequests/SocketStreams/ConnectJobs. 107 // URLRequests/SocketStreams/ConnectJobs.
125 class SourceTracker : public SourceTrackerInterface { 108 class SourceTracker : public SourceTrackerInterface {
126 public: 109 public:
127 // Creates a SourceTracker that will track at most |max_num_sources|. 110 // Creates a SourceTracker that will track at most |max_num_sources|.
128 // Up to |max_graveyard_size| unreferenced sources will be kept around 111 // Up to |max_graveyard_size| unreferenced sources will be kept around
129 // before deleting them for good. |parent| may be NULL, and points to 112 // before deleting them for good. |parent| may be NULL, and points to
130 // the owning PassiveLogCollector (it is used when adding references 113 // the owning PassiveLogCollector (it is used when adding references
131 // to other sources). 114 // to other sources).
132 SourceTracker(size_t max_num_sources, 115 SourceTracker(size_t max_num_sources,
133 size_t max_graveyard_size, 116 size_t max_graveyard_size,
134 PassiveLogCollector* parent); 117 PassiveLogCollector* parent);
135 118
136 virtual ~SourceTracker(); 119 virtual ~SourceTracker();
137 120
138 // SourceTrackerInterface implementation: 121 // SourceTrackerInterface implementation:
139 virtual void OnAddEntry(const Entry& entry); 122 virtual void OnAddEntry(const ChromeNetLog::Entry& entry);
140 virtual void Clear(); 123 virtual void Clear();
141 virtual void AppendAllEntries(EntryList* out) const; 124 virtual void AppendAllEntries(ChromeNetLog::EntryList* out) const;
142 125
143 #ifdef UNIT_TEST 126 #ifdef UNIT_TEST
144 // Helper used to inspect the current state by unit-tests. 127 // Helper used to inspect the current state by unit-tests.
145 // Retuns a copy of the source infos held by the tracker. 128 // Retuns a copy of the source infos held by the tracker.
146 SourceInfoList GetAllDeadOrAliveSources(bool is_alive) const { 129 SourceInfoList GetAllDeadOrAliveSources(bool is_alive) const {
147 SourceInfoList result; 130 SourceInfoList result;
148 for (SourceIDToInfoMap::const_iterator it = sources_.begin(); 131 for (SourceIDToInfoMap::const_iterator it = sources_.begin();
149 it != sources_.end(); ++it) { 132 it != sources_.end(); ++it) {
150 if (it->second.is_alive == is_alive) 133 if (it->second.is_alive == is_alive)
151 result.push_back(it->second); 134 result.push_back(it->second);
(...skipping 13 matching lines...) Expand all
165 // kept alive at least as long as |info|. 148 // kept alive at least as long as |info|.
166 void AddReferenceToSourceDependency(const net::NetLog::Source& source, 149 void AddReferenceToSourceDependency(const net::NetLog::Source& source,
167 SourceInfo* info); 150 SourceInfo* info);
168 151
169 private: 152 private:
170 typedef base::hash_map<uint32, SourceInfo> SourceIDToInfoMap; 153 typedef base::hash_map<uint32, SourceInfo> SourceIDToInfoMap;
171 typedef std::deque<uint32> DeletionQueue; 154 typedef std::deque<uint32> DeletionQueue;
172 155
173 // Updates |out_info| with the information from |entry|. Returns an action 156 // Updates |out_info| with the information from |entry|. Returns an action
174 // to perform for this map entry on completion. 157 // to perform for this map entry on completion.
175 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info) = 0; 158 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
159 SourceInfo* out_info) = 0;
176 160
177 // Removes |source_id| from |sources_|. This also releases any references 161 // Removes |source_id| from |sources_|. This also releases any references
178 // to dependencies held by this source. 162 // to dependencies held by this source.
179 void DeleteSourceInfo(uint32 source_id); 163 void DeleteSourceInfo(uint32 source_id);
180 164
181 // Adds |source_id| to the FIFO queue (graveyard) for deletion. 165 // Adds |source_id| to the FIFO queue (graveyard) for deletion.
182 void AddToDeletionQueue(uint32 source_id); 166 void AddToDeletionQueue(uint32 source_id);
183 167
184 // Adds/Releases a reference from the source with ID |source_id|. 168 // Adds/Releases a reference from the source with ID |source_id|.
185 // Use |offset=-1| to do a release, and |offset=1| for an addref. 169 // Use |offset=-1| to do a release, and |offset=1| for an addref.
(...skipping 21 matching lines...) Expand all
207 191
208 // Specialization of SourceTracker for handling ConnectJobs. 192 // Specialization of SourceTracker for handling ConnectJobs.
209 class ConnectJobTracker : public SourceTracker { 193 class ConnectJobTracker : public SourceTracker {
210 public: 194 public:
211 static const size_t kMaxNumSources; 195 static const size_t kMaxNumSources;
212 static const size_t kMaxGraveyardSize; 196 static const size_t kMaxGraveyardSize;
213 197
214 explicit ConnectJobTracker(PassiveLogCollector* parent); 198 explicit ConnectJobTracker(PassiveLogCollector* parent);
215 199
216 protected: 200 protected:
217 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info); 201 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
202 SourceInfo* out_info);
218 private: 203 private:
219 DISALLOW_COPY_AND_ASSIGN(ConnectJobTracker); 204 DISALLOW_COPY_AND_ASSIGN(ConnectJobTracker);
220 }; 205 };
221 206
222 // Specialization of SourceTracker for handling Sockets. 207 // Specialization of SourceTracker for handling Sockets.
223 class SocketTracker : public SourceTracker { 208 class SocketTracker : public SourceTracker {
224 public: 209 public:
225 static const size_t kMaxNumSources; 210 static const size_t kMaxNumSources;
226 static const size_t kMaxGraveyardSize; 211 static const size_t kMaxGraveyardSize;
227 212
228 SocketTracker(); 213 SocketTracker();
229 214
230 protected: 215 protected:
231 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info); 216 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
217 SourceInfo* out_info);
232 218
233 private: 219 private:
234 DISALLOW_COPY_AND_ASSIGN(SocketTracker); 220 DISALLOW_COPY_AND_ASSIGN(SocketTracker);
235 }; 221 };
236 222
237 // Specialization of SourceTracker for handling URLRequest/SocketStream. 223 // Specialization of SourceTracker for handling URLRequest/SocketStream.
238 class RequestTracker : public SourceTracker { 224 class RequestTracker : public SourceTracker {
239 public: 225 public:
240 static const size_t kMaxNumSources; 226 static const size_t kMaxNumSources;
241 static const size_t kMaxGraveyardSize; 227 static const size_t kMaxGraveyardSize;
242 228
243 explicit RequestTracker(PassiveLogCollector* parent); 229 explicit RequestTracker(PassiveLogCollector* parent);
244 230
245 protected: 231 protected:
246 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info); 232 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
233 SourceInfo* out_info);
247 234
248 private: 235 private:
249 DISALLOW_COPY_AND_ASSIGN(RequestTracker); 236 DISALLOW_COPY_AND_ASSIGN(RequestTracker);
250 }; 237 };
251 238
252 // Specialization of SourceTracker for handling 239 // Specialization of SourceTracker for handling
253 // SOURCE_INIT_PROXY_RESOLVER. 240 // SOURCE_INIT_PROXY_RESOLVER.
254 class InitProxyResolverTracker : public SourceTracker { 241 class InitProxyResolverTracker : public SourceTracker {
255 public: 242 public:
256 static const size_t kMaxNumSources; 243 static const size_t kMaxNumSources;
257 static const size_t kMaxGraveyardSize; 244 static const size_t kMaxGraveyardSize;
258 245
259 InitProxyResolverTracker(); 246 InitProxyResolverTracker();
260 247
261 protected: 248 protected:
262 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info); 249 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
250 SourceInfo* out_info);
263 251
264 private: 252 private:
265 DISALLOW_COPY_AND_ASSIGN(InitProxyResolverTracker); 253 DISALLOW_COPY_AND_ASSIGN(InitProxyResolverTracker);
266 }; 254 };
267 255
268 // Tracks the log entries for the last seen SOURCE_SPDY_SESSION. 256 // Tracks the log entries for the last seen SOURCE_SPDY_SESSION.
269 class SpdySessionTracker : public SourceTracker { 257 class SpdySessionTracker : public SourceTracker {
270 public: 258 public:
271 static const size_t kMaxNumSources; 259 static const size_t kMaxNumSources;
272 static const size_t kMaxGraveyardSize; 260 static const size_t kMaxGraveyardSize;
273 261
274 SpdySessionTracker(); 262 SpdySessionTracker();
275 263
276 protected: 264 protected:
277 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info); 265 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
266 SourceInfo* out_info);
278 267
279 private: 268 private:
280 DISALLOW_COPY_AND_ASSIGN(SpdySessionTracker); 269 DISALLOW_COPY_AND_ASSIGN(SpdySessionTracker);
281 }; 270 };
282 271
283 // Tracks the log entries for the last seen SOURCE_HOST_RESOLVER_IMPL_REQUEST. 272 // Tracks the log entries for the last seen SOURCE_HOST_RESOLVER_IMPL_REQUEST.
284 class DNSRequestTracker : public SourceTracker { 273 class DNSRequestTracker : public SourceTracker {
285 public: 274 public:
286 static const size_t kMaxNumSources; 275 static const size_t kMaxNumSources;
287 static const size_t kMaxGraveyardSize; 276 static const size_t kMaxGraveyardSize;
288 277
289 DNSRequestTracker(); 278 DNSRequestTracker();
290 279
291 protected: 280 protected:
292 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info); 281 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
282 SourceInfo* out_info);
293 283
294 private: 284 private:
295 DISALLOW_COPY_AND_ASSIGN(DNSRequestTracker); 285 DISALLOW_COPY_AND_ASSIGN(DNSRequestTracker);
296 }; 286 };
297 287
298 // Tracks the log entries for the last seen SOURCE_HOST_RESOLVER_IMPL_JOB. 288 // Tracks the log entries for the last seen SOURCE_HOST_RESOLVER_IMPL_JOB.
299 class DNSJobTracker : public SourceTracker { 289 class DNSJobTracker : public SourceTracker {
300 public: 290 public:
301 static const size_t kMaxNumSources; 291 static const size_t kMaxNumSources;
302 static const size_t kMaxGraveyardSize; 292 static const size_t kMaxGraveyardSize;
303 293
304 DNSJobTracker(); 294 DNSJobTracker();
305 295
306 protected: 296 protected:
307 virtual Action DoAddEntry(const Entry& entry, SourceInfo* out_info); 297 virtual Action DoAddEntry(const ChromeNetLog::Entry& entry,
298 SourceInfo* out_info);
308 299
309 private: 300 private:
310 DISALLOW_COPY_AND_ASSIGN(DNSJobTracker); 301 DISALLOW_COPY_AND_ASSIGN(DNSJobTracker);
311 }; 302 };
312 303
313 PassiveLogCollector(); 304 PassiveLogCollector();
314 ~PassiveLogCollector(); 305 ~PassiveLogCollector();
315 306
316 // Observer implementation: 307 // ThreadSafeObserver implementation:
317 virtual void OnAddEntry(net::NetLog::EventType type, 308 virtual void OnAddEntry(net::NetLog::EventType type,
318 const base::TimeTicks& time, 309 const base::TimeTicks& time,
319 const net::NetLog::Source& source, 310 const net::NetLog::Source& source,
320 net::NetLog::EventPhase phase, 311 net::NetLog::EventPhase phase,
321 net::NetLog::EventParameters* params); 312 net::NetLog::EventParameters* params);
322 313
323 // Returns the tracker to use for sources of type |source_type|, or NULL.
324 SourceTrackerInterface* GetTrackerForSourceType(
325 net::NetLog::SourceType source_type);
326
327 // Clears all of the passively logged data. 314 // Clears all of the passively logged data.
328 void Clear(); 315 void Clear();
329 316
330 // Fills |out| with the full list of events that have been passively 317 // Fills |out| with the full list of events that have been passively
331 // captured. The list is ordered by capture time. 318 // captured. The list is ordered by capture time.
332 void GetAllCapturedEvents(EntryList* out) const; 319 void GetAllCapturedEvents(ChromeNetLog::EntryList* out) const;
333 320
334 private: 321 private:
322 // Returns the tracker to use for sources of type |source_type|, or NULL.
323 SourceTrackerInterface* GetTrackerForSourceType_(
324 net::NetLog::SourceType source_type);
325
335 FRIEND_TEST_ALL_PREFIXES(PassiveLogCollectorTest, 326 FRIEND_TEST_ALL_PREFIXES(PassiveLogCollectorTest,
336 HoldReferenceToDependentSource); 327 HoldReferenceToDependentSource);
337 FRIEND_TEST_ALL_PREFIXES(PassiveLogCollectorTest, 328 FRIEND_TEST_ALL_PREFIXES(PassiveLogCollectorTest,
338 HoldReferenceToDeletedSource); 329 HoldReferenceToDeletedSource);
339 330
340 GlobalSourceTracker global_source_tracker_; 331 GlobalSourceTracker global_source_tracker_;
341 ConnectJobTracker connect_job_tracker_; 332 ConnectJobTracker connect_job_tracker_;
342 SocketTracker socket_tracker_; 333 SocketTracker socket_tracker_;
343 RequestTracker url_request_tracker_; 334 RequestTracker url_request_tracker_;
344 RequestTracker socket_stream_tracker_; 335 RequestTracker socket_stream_tracker_;
345 InitProxyResolverTracker init_proxy_resolver_tracker_; 336 InitProxyResolverTracker init_proxy_resolver_tracker_;
346 SpdySessionTracker spdy_session_tracker_; 337 SpdySessionTracker spdy_session_tracker_;
347 DNSRequestTracker dns_request_tracker_; 338 DNSRequestTracker dns_request_tracker_;
348 DNSJobTracker dns_job_tracker_; 339 DNSJobTracker dns_job_tracker_;
349 340
350 // This array maps each NetLog::SourceType to one of the tracker instances 341 // This array maps each NetLog::SourceType to one of the tracker instances
351 // defined above. Use of this array avoid duplicating the list of trackers 342 // defined above. Use of this array avoid duplicating the list of trackers
352 // elsewhere. 343 // elsewhere.
353 SourceTrackerInterface* trackers_[net::NetLog::SOURCE_COUNT]; 344 SourceTrackerInterface* trackers_[net::NetLog::SOURCE_COUNT];
354 345
355 // The count of how many events have flowed through this log. Used to set the 346 // The count of how many events have flowed through this log. Used to set the
356 // "order" field on captured events. 347 // "order" field on captured events.
357 uint32 num_events_seen_; 348 uint32 num_events_seen_;
358 349
359 DISALLOW_COPY_AND_ASSIGN(PassiveLogCollector); 350 DISALLOW_COPY_AND_ASSIGN(PassiveLogCollector);
360 }; 351 };
361 352
362 #endif // CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ 353 #endif // CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698