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

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

Powered by Google App Engine
This is Rietveld 408576698