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

Side by Side Diff: base/tracked_objects.h

Issue 8430004: Revert 107895 - Fully enable about:tracking by default (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 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
« no previous file with comments | « base/threading/worker_pool_win.cc ('k') | base/tracked_objects.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 BASE_TRACKED_OBJECTS_H_ 5 #ifndef BASE_TRACKED_OBJECTS_H_
6 #define BASE_TRACKED_OBJECTS_H_ 6 #define BASE_TRACKED_OBJECTS_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <stack> 10 #include <stack>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/base_export.h" 14 #include "base/base_export.h"
15 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/time.h" 16 #include "base/time.h"
17 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
18 #include "base/threading/thread_local_storage.h" 18 #include "base/threading/thread_local_storage.h"
19 #include "base/tracking_info.h"
20 #include "base/values.h" 19 #include "base/values.h"
21 20
22 #if defined(OS_WIN)
23 #include <mmsystem.h> // Declare timeGetTime();
24 #endif
25
26 // TrackedObjects provides a database of stats about objects (generally Tasks) 21 // TrackedObjects provides a database of stats about objects (generally Tasks)
27 // that are tracked. Tracking means their birth, death, duration, birth thread, 22 // that are tracked. Tracking means their birth, death, duration, birth thread,
28 // death thread, and birth place are recorded. This data is carefully spread 23 // death thread, and birth place are recorded. This data is carefully spread
29 // across a series of objects so that the counts and times can be rapidly 24 // across a series of objects so that the counts and times can be rapidly
30 // updated without (usually) having to lock the data, and hence there is usually 25 // updated without (usually) having to lock the data, and hence there is usually
31 // very little contention caused by the tracking. The data can be viewed via 26 // very little contention caused by the tracking. The data can be viewed via
32 // the about:tracking URL, with a variety of sorting and filtering choices. 27 // the about:tracking URL, with a variety of sorting and filtering choices.
33 // 28 //
34 // These classes serve as the basis of a profiler of sorts for the Tasks system. 29 // These classes serve as the basis of a profiler of sorts for the Tasks system.
35 // As a result, design decisions were made to maximize speed, by minimizing 30 // As a result, design decisions were made to maximize speed, by minimizing
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 // For Tasks, having now either constructed or found the Births instance 62 // For Tasks, having now either constructed or found the Births instance
68 // described above, a pointer to the Births instance is then recorded into the 63 // described above, a pointer to the Births instance is then recorded into the
69 // PendingTask structure in MessageLoop. This fact alone is very useful in 64 // PendingTask structure in MessageLoop. This fact alone is very useful in
70 // debugging, when there is a question of where an instance came from. In 65 // debugging, when there is a question of where an instance came from. In
71 // addition, the birth time is also recorded and used to later evaluate the 66 // addition, the birth time is also recorded and used to later evaluate the
72 // lifetime duration of the whole Task. As a result of the above embedding, we 67 // lifetime duration of the whole Task. As a result of the above embedding, we
73 // can find out a Task's location of birth, and thread of birth, without using 68 // can find out a Task's location of birth, and thread of birth, without using
74 // any locks, as all that data is constant across the life of the process. 69 // any locks, as all that data is constant across the life of the process.
75 // 70 //
76 // The above work *could* also be done for any other object as well by calling 71 // The above work *could* also be done for any other object as well by calling
77 // TallyABirthIfActive() and TallyRunOnNamedThreadIfTracking() as appropriate. 72 // TallyABirthIfActive() and TallyADeathIfActive() as appropriate.
78 // 73 //
79 // The amount of memory used in the above data structures depends on how many 74 // The amount of memory used in the above data structures depends on how many
80 // threads there are, and how many Locations of construction there are. 75 // threads there are, and how many Locations of construction there are.
81 // Fortunately, we don't use memory that is the product of those two counts, but 76 // Fortunately, we don't use memory that is the product of those two counts, but
82 // rather we only need one Births instance for each thread that constructs an 77 // rather we only need one Births instance for each thread that constructs an
83 // instance at a Location. In many cases, instances are only created on one 78 // instance at a Location. In many cases, instances are only created on one
84 // thread, so the memory utilization is actually fairly restrained. 79 // thread, so the memory utilization is actually fairly restrained.
85 // 80 //
86 // Lastly, when an instance is deleted, the final tallies of statistics are 81 // Lastly, when an instance is deleted, the final tallies of statistics are
87 // carefully accumulated. That tallying wrties into slots (members) in a 82 // carefully accumulated. That tallying wrties into slots (members) in a
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 // asynchronously relative to ongoing updates, and worse yet, some data fields 160 // asynchronously relative to ongoing updates, and worse yet, some data fields
166 // are 64bit quantities, and are not atomicly accessed (reset or incremented 161 // are 64bit quantities, and are not atomicly accessed (reset or incremented
167 // etc.). For basic profiling, this will work "most of the time," and should be 162 // etc.). For basic profiling, this will work "most of the time," and should be
168 // sufficient... but storing away DataCollections is the "right way" to do this. 163 // sufficient... but storing away DataCollections is the "right way" to do this.
169 164
170 class MessageLoop; 165 class MessageLoop;
171 166
172 namespace tracked_objects { 167 namespace tracked_objects {
173 168
174 //------------------------------------------------------------------------------ 169 //------------------------------------------------------------------------------
175
176 #define USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS
177
178 #if defined(USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS)
179
180 // TimeTicks maintains a wasteful 64 bits of data (we need less than 32), and on
181 // windows, a 64 bit timer is expensive to even obtain. We use a simple
182 // millisecond counter for most of our time values, as well as millisecond units
183 // of duration between those values. This means we can only handle durations
184 // up to 49 days (range), or 24 days (non-negative time durations).
185 // We only define enough methods to service the needs of the tracking classes,
186 // and our interfaces are modeled after what TimeTicks and TimeDelta use (so we
187 // can swap them into place if we want to use the "real" classes).
188
189 class BASE_EXPORT Duration { // Similar to base::TimeDelta.
190 public:
191 Duration() : ms_(0) {}
192
193 Duration& operator+=(const Duration& other) {
194 ms_ += other.ms_;
195 return *this;
196 }
197
198 Duration operator+(const Duration& other) const {
199 return Duration(ms_ + other.ms_);
200 }
201
202 bool operator==(const Duration& other) const { return ms_ == other.ms_; }
203 bool operator!=(const Duration& other) const { return ms_ != other.ms_; }
204 bool operator>(const Duration& other) const { return ms_ > other.ms_; }
205
206 static Duration FromMilliseconds(int ms) { return Duration(ms); }
207
208 int32 InMilliseconds() const { return ms_; }
209
210 private:
211 friend class TrackedTime;
212 explicit Duration(int32 duration) : ms_(duration) {}
213
214 // Internal time is stored directly in milliseconds.
215 int32 ms_;
216 };
217
218 class BASE_EXPORT TrackedTime { // Similar to base::TimeTicks.
219 public:
220 TrackedTime() : ms_(0) {}
221 explicit TrackedTime(const base::TimeTicks& time)
222 : ms_((time - base::TimeTicks()).InMilliseconds()) {
223 }
224
225 static TrackedTime Now() {
226 #if defined(OS_WIN)
227 // Use lock-free accessor to 32 bit time.
228 // Note that TimeTicks::Now() is built on this, so we have "compatible"
229 // times when we down-convert a TimeTicks sample.
230 // TODO(jar): Surface this interface via something in base/time.h.
231 return TrackedTime(static_cast<int32>(::timeGetTime()));
232 #else
233 // Posix has nice cheap 64 bit times, so we just down-convert it.
234 return TrackedTime(base::TimeTicks::Now());
235 #endif // OS_WIN
236 }
237
238 Duration operator-(const TrackedTime& other) const {
239 return Duration(ms_ - other.ms_);
240 }
241
242 TrackedTime operator+(const Duration& other) const {
243 return TrackedTime(ms_ + other.ms_);
244 }
245
246 bool is_null() const { return ms_ == 0; }
247
248 private:
249 friend class Duration;
250 explicit TrackedTime(int32 ms) : ms_(ms) {}
251
252 // Internal duration is stored directly in milliseconds.
253 uint32 ms_;
254 };
255
256 #else
257
258 // Just use full 64 bit time calculations, and the slower TimeTicks::Now().
259 typedef base::TimeTicks TrackedTime;
260 typedef base::TimeDelta Duration;
261
262 #endif // USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS
263
264 //------------------------------------------------------------------------------
265 // For a specific thread, and a specific birth place, the collection of all 170 // For a specific thread, and a specific birth place, the collection of all
266 // death info (with tallies for each death thread, to prevent access conflicts). 171 // death info (with tallies for each death thread, to prevent access conflicts).
267 class ThreadData; 172 class ThreadData;
268 class BASE_EXPORT BirthOnThread { 173 class BASE_EXPORT BirthOnThread {
269 public: 174 public:
270 BirthOnThread(const Location& location, const ThreadData& current); 175 BirthOnThread(const Location& location, const ThreadData& current);
271 176
272 const Location location() const { return location_; } 177 const Location location() const { return location_; }
273 const ThreadData* birth_thread() const { return birth_thread_; } 178 const ThreadData* birth_thread() const { return birth_thread_; }
274 179
(...skipping 30 matching lines...) Expand all
305 void Clear() { birth_count_ = 0; } 210 void Clear() { birth_count_ = 0; }
306 211
307 private: 212 private:
308 // The number of births on this thread for our location_. 213 // The number of births on this thread for our location_.
309 int birth_count_; 214 int birth_count_;
310 215
311 DISALLOW_COPY_AND_ASSIGN(Births); 216 DISALLOW_COPY_AND_ASSIGN(Births);
312 }; 217 };
313 218
314 //------------------------------------------------------------------------------ 219 //------------------------------------------------------------------------------
315 // Basic info summarizing multiple destructions of a tracked object with a 220 // Basic info summarizing multiple destructions of an object with a single
316 // single birthplace (fixed Location). Used both on specific threads, and also 221 // birthplace (fixed Location). Used both on specific threads, and also used
317 // in snapshots when integrating assembled data. 222 // in snapshots when integrating assembled data.
318 223
319 class BASE_EXPORT DeathData { 224 class BASE_EXPORT DeathData {
320 public: 225 public:
321 // Default initializer. 226 // Default initializer.
322 DeathData() : count_(0) {} 227 DeathData() : count_(0) {}
323 228
324 // When deaths have not yet taken place, and we gather data from all the 229 // When deaths have not yet taken place, and we gather data from all the
325 // threads, we create DeathData stats that tally the number of births without 230 // threads, we create DeathData stats that tally the number of births without
326 // a corrosponding death. 231 // a corrosponding death.
327 explicit DeathData(int count) : count_(count) {} 232 explicit DeathData(int count) : count_(count) {}
328 233
329 // Update stats for a task destruction (death) that had a Run() time of 234 // Update stats for a task destruction (death) that had a Run() time of
330 // |duration|, and has had a queueing delay of |queue_duration|. 235 // |duration|, and has had a queueing delay of |queue_duration|.
331 void RecordDeath(const Duration& queue_duration, 236 void RecordDeath(const base::TimeDelta& queue_duration,
332 const Duration& run_duration); 237 const base::TimeDelta& run_duration);
333 238
334 // Metrics accessors. 239 // Metrics accessors.
335 int count() const { return count_; } 240 int count() const { return count_; }
336 Duration run_duration() const { return run_duration_; } 241 base::TimeDelta run_duration() const { return run_duration_; }
337 int AverageMsRunDuration() const; 242 int AverageMsRunDuration() const;
338 Duration queue_duration() const { return queue_duration_; } 243 base::TimeDelta queue_duration() const { return queue_duration_; }
339 int AverageMsQueueDuration() const; 244 int AverageMsQueueDuration() const;
340 245
341 // Accumulate metrics from other into this. This method is never used on 246 // Accumulate metrics from other into this. This method is never used on
342 // realtime statistics, and only used in snapshots and aggregatinos. 247 // realtime statistics, and only used in snapshots and aggregatinos.
343 void AddDeathData(const DeathData& other); 248 void AddDeathData(const DeathData& other);
344 249
345 // Simple print of internal state for use in line of HTML. 250 // Simple print of internal state for use in line of HTML.
346 void WriteHTML(std::string* output) const; 251 void WriteHTML(std::string* output) const;
347 252
348 // Construct a DictionaryValue instance containing all our stats. The caller 253 // Constructe a DictionaryValue instance containing all our stats. The caller
349 // assumes ownership of the returned instance. 254 // assumes ownership of the returned instance.
350 base::DictionaryValue* ToValue() const; 255 base::DictionaryValue* ToValue() const;
351 256
352 // Reset all tallies to zero. This is used as a hack on realtime data. 257 // Reset all tallies to zero. This is used as a hack on realtime data.
353 void Clear(); 258 void Clear();
354 259
355 private: 260 private:
356 int count_; // Number of destructions. 261 int count_; // Number of destructions.
357 Duration run_duration_; // Sum of all Run()time durations. 262 base::TimeDelta run_duration_; // Sum of all Run()time durations.
358 Duration queue_duration_; // Sum of all queue time durations. 263 base::TimeDelta queue_duration_; // Sum of all queue time durations.
359 }; 264 };
360 265
361 //------------------------------------------------------------------------------ 266 //------------------------------------------------------------------------------
362 // A temporary collection of data that can be sorted and summarized. It is 267 // A temporary collection of data that can be sorted and summarized. It is
363 // gathered (carefully) from many threads. Instances are held in arrays and 268 // gathered (carefully) from many threads. Instances are held in arrays and
364 // processed, filtered, and rendered. 269 // processed, filtered, and rendered.
365 // The source of this data was collected on many threads, and is asynchronously 270 // The source of this data was collected on many threads, and is asynchronously
366 // changing. The data in this instance is not asynchronously changing. 271 // changing. The data in this instance is not asynchronously changing.
367 272
368 class BASE_EXPORT Snapshot { 273 class BASE_EXPORT Snapshot {
369 public: 274 public:
370 // When snapshotting a full life cycle set (birth-to-death), use this: 275 // When snapshotting a full life cycle set (birth-to-death), use this:
371 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, 276 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread,
372 const DeathData& death_data); 277 const DeathData& death_data);
373 278
374 // When snapshotting a birth, with no death yet, use this: 279 // When snapshotting a birth, with no death yet, use this:
375 Snapshot(const BirthOnThread& birth_on_thread, int count); 280 Snapshot(const BirthOnThread& birth_on_thread, int count);
376 281
377 const ThreadData* birth_thread() const { return birth_->birth_thread(); } 282 const ThreadData* birth_thread() const { return birth_->birth_thread(); }
378 const Location location() const { return birth_->location(); } 283 const Location location() const { return birth_->location(); }
379 const BirthOnThread& birth() const { return *birth_; } 284 const BirthOnThread& birth() const { return *birth_; }
380 const ThreadData* death_thread() const {return death_thread_; } 285 const ThreadData* death_thread() const {return death_thread_; }
381 const DeathData& death_data() const { return death_data_; } 286 const DeathData& death_data() const { return death_data_; }
382 const std::string DeathThreadName() const; 287 const std::string DeathThreadName() const;
383 288
384 int count() const { return death_data_.count(); } 289 int count() const { return death_data_.count(); }
385 Duration run_duration() const { return death_data_.run_duration(); } 290 base::TimeDelta run_duration() const { return death_data_.run_duration(); }
386 Duration queue_duration() const { return death_data_.queue_duration(); }
387 int AverageMsRunDuration() const { 291 int AverageMsRunDuration() const {
388 return death_data_.AverageMsRunDuration(); 292 return death_data_.AverageMsRunDuration();
389 } 293 }
294 base::TimeDelta queue_duration() const {
295 return death_data_.queue_duration();
296 }
390 int AverageMsQueueDuration() const { 297 int AverageMsQueueDuration() const {
391 return death_data_.AverageMsQueueDuration(); 298 return death_data_.AverageMsQueueDuration();
392 } 299 }
393 300
394 // Emit contents for use in a line of HTML 301 // Emit contents for use in a line of HTML
395 void WriteHTML(std::string* output) const; 302 void WriteHTML(std::string* output) const;
396 303
397 // Construct a DictionaryValue instance containing all our data recursively. 304 // Construct a DictionaryValue instance containing all our data recursively.
398 // The caller assumes ownership of the memory in the returned instance. 305 // The caller assumes ownership of the memory in the returned instance.
399 base::DictionaryValue* ToValue() const; 306 base::DictionaryValue* ToValue() const;
400 307
308 void Add(const Snapshot& other);
309
401 private: 310 private:
402 const BirthOnThread* birth_; // Includes Location and birth_thread. 311 const BirthOnThread* birth_; // Includes Location and birth_thread.
403 const ThreadData* death_thread_; 312 const ThreadData* death_thread_;
404 DeathData death_data_; 313 DeathData death_data_;
405 }; 314 };
406 315
407 //------------------------------------------------------------------------------ 316 //------------------------------------------------------------------------------
408 // DataCollector is a container class for Snapshot and BirthOnThread count 317 // DataCollector is a container class for Snapshot and BirthOnThread count
409 // items. 318 // items.
410 319
411 class BASE_EXPORT DataCollector { 320 class BASE_EXPORT DataCollector {
412 public: 321 public:
413 typedef std::vector<Snapshot> Collection; 322 typedef std::vector<Snapshot> Collection;
414 323
415 // Construct with a list of how many threads should contribute. This helps us 324 // Construct with a list of how many threads should contribute. This helps us
416 // determine (in the async case) when we are done with all contributions. 325 // determine (in the async case) when we are done with all contributions.
417 DataCollector(); 326 DataCollector();
418 ~DataCollector(); 327 ~DataCollector();
419 328
420 // Adds all stats from the indicated thread into our arrays. This function 329 // Add all stats from the indicated thread into our arrays. This function is
421 // uses locks at the lowest level (when accessing the underlying maps which 330 // mutex protected, and *could* be called from any threads (although current
422 // could change when not locked), and can be called from any threads. 331 // implementation serialized calls to Append).
423 void Append(const ThreadData& thread_data); 332 void Append(const ThreadData& thread_data);
424 333
425 // After the accumulation phase, the following accessor is used to process the 334 // After the accumulation phase, the following accessor is used to process the
426 // data (i.e., sort it, filter it, etc.). 335 // data.
427 Collection* collection(); 336 Collection* collection();
428 337
429 // Adds entries for all the remaining living objects (objects that have 338 // After collection of death data is complete, we can add entries for all the
430 // tallied a birth, but have not yet tallied a matching death, and hence must 339 // remaining living objects.
431 // be either running, queued up, or being held in limbo for future posting).
432 // This should be called after all known ThreadData instances have been
433 // processed using Append().
434 void AddListOfLivingObjects(); 340 void AddListOfLivingObjects();
435 341
436 // Generates a ListValue representation of the vector of snapshots. The caller 342 // Generate a ListValue representation of the vector of snapshots. The caller
437 // assumes ownership of the memory in the returned instance. 343 // assumes ownership of the memory in the returned instance.
438 base::ListValue* ToValue() const; 344 base::ListValue* ToValue() const;
439 345
440 private: 346 private:
441 typedef std::map<const BirthOnThread*, int> BirthCount; 347 typedef std::map<const BirthOnThread*, int> BirthCount;
442 348
443 // The array that we collect data into. 349 // The array that we collect data into.
444 Collection collection_; 350 Collection collection_;
445 351
446 // The total number of births recorded at each location for which we have not 352 // The total number of births recorded at each location for which we have not
447 // seen a death count. This map changes as we do Append() calls, and is later 353 // seen a death count.
448 // used by AddListOfLivingObjects() to gather up unaccounted for births.
449 BirthCount global_birth_count_; 354 BirthCount global_birth_count_;
450 355
451 DISALLOW_COPY_AND_ASSIGN(DataCollector); 356 DISALLOW_COPY_AND_ASSIGN(DataCollector);
452 }; 357 };
453 358
454 //------------------------------------------------------------------------------ 359 //------------------------------------------------------------------------------
455 // Aggregation contains summaries (totals and subtotals) of groups of Snapshot 360 // Aggregation contains summaries (totals and subtotals) of groups of Snapshot
456 // instances to provide printing of these collections on a single line. 361 // instances to provide printing of these collections on a single line.
457 // We generally provide an aggregate total for the entire list, as well as
458 // aggregate subtotals for groups of stats (example: group of all lives that
459 // died on the specific thread).
460 362
461 class BASE_EXPORT Aggregation: public DeathData { 363 class BASE_EXPORT Aggregation: public DeathData {
462 public: 364 public:
463 Aggregation(); 365 Aggregation();
464 ~Aggregation(); 366 ~Aggregation();
465 367
466 void AddDeathSnapshot(const Snapshot& snapshot); 368 void AddDeathSnapshot(const Snapshot& snapshot);
467 void AddBirths(const Births& births); 369 void AddBirths(const Births& births);
468 void AddBirth(const BirthOnThread& birth); 370 void AddBirth(const BirthOnThread& birth);
469 void AddBirthPlace(const Location& location); 371 void AddBirthPlace(const Location& location);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 BIRTH_FUNCTION = 8, 407 BIRTH_FUNCTION = 8,
506 BIRTH_LINE = 16, 408 BIRTH_LINE = 16,
507 COUNT = 32, 409 COUNT = 32,
508 AVERAGE_RUN_DURATION = 64, 410 AVERAGE_RUN_DURATION = 64,
509 TOTAL_RUN_DURATION = 128, 411 TOTAL_RUN_DURATION = 128,
510 AVERAGE_QUEUE_DURATION = 256, 412 AVERAGE_QUEUE_DURATION = 256,
511 TOTAL_QUEUE_DURATION = 512, 413 TOTAL_QUEUE_DURATION = 512,
512 414
513 // Imediate action keywords. 415 // Imediate action keywords.
514 RESET_ALL_DATA = -1, 416 RESET_ALL_DATA = -1,
515 UNKNOWN_KEYWORD = -2,
516 }; 417 };
517 418
518 explicit Comparator(); 419 explicit Comparator();
519 420
520 // Reset the comparator to a NIL selector. Clear() and recursively delete any 421 // Reset the comparator to a NIL selector. Clear() and recursively delete any
521 // tiebreaker_ entries. NOTE: We can't use a standard destructor, because 422 // tiebreaker_ entries. NOTE: We can't use a standard destructor, because
522 // the sort algorithm makes copies of this object, and then deletes them, 423 // the sort algorithm makes copies of this object, and then deletes them,
523 // which would cause problems (either we'd make expensive deep copies, or we'd 424 // which would cause problems (either we'd make expensive deep copies, or we'd
524 // do more thna one delete on a tiebreaker_. 425 // do more thna one delete on a tiebreaker_.
525 void Clear(); 426 void Clear();
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 bool WriteSortGrouping(const Snapshot& sample, std::string* output) const; 463 bool WriteSortGrouping(const Snapshot& sample, std::string* output) const;
563 464
564 // Output a sample, with SortGroup details not displayed. 465 // Output a sample, with SortGroup details not displayed.
565 void WriteSnapshotHTML(const Snapshot& sample, std::string* output) const; 466 void WriteSnapshotHTML(const Snapshot& sample, std::string* output) const;
566 467
567 private: 468 private:
568 // The selector directs this instance to compare based on the specified 469 // The selector directs this instance to compare based on the specified
569 // members of the tested elements. 470 // members of the tested elements.
570 enum Selector selector_; 471 enum Selector selector_;
571 472
572 // Translate a path keyword into a selector. This is a slow implementation,
573 // but this is rarely done, and only for HTML presentations.
574 static Selector FindSelector(const std::string& keyword);
575
576 // For filtering into acceptable and unacceptable snapshot instance, the 473 // For filtering into acceptable and unacceptable snapshot instance, the
577 // following is required to be a substring of the selector_ field. 474 // following is required to be a substring of the selector_ field.
578 std::string required_; 475 std::string required_;
579 476
580 // If this instance can't decide on an ordering, we can consult a tie-breaker 477 // If this instance can't decide on an ordering, we can consult a tie-breaker
581 // which may have a different basis of comparison. 478 // which may have a different basis of comparison.
582 Comparator* tiebreaker_; 479 Comparator* tiebreaker_;
583 480
584 // We or together all the selectors we sort on (not counting sub-group 481 // We or together all the selectors we sort on (not counting sub-group
585 // selectors), so that we can tell if we've decided to group on any given 482 // selectors), so that we can tell if we've decided to group on any given
586 // criteria. 483 // criteria.
587 int combined_selectors_; 484 int combined_selectors_;
588 485
589 // Some tiebreakrs are for subgroup ordering, and not for basic ordering (in 486 // Some tiebreakrs are for subgroup ordering, and not for basic ordering (in
590 // preparation for aggregation). The subgroup tiebreakers are not consulted 487 // preparation for aggregation). The subgroup tiebreakers are not consulted
591 // when deciding if two items are in equivalent groups. This flag tells us 488 // when deciding if two items are in equivalent groups. This flag tells us
592 // to ignore the tiebreaker when doing Equivalent() testing. 489 // to ignore the tiebreaker when doing Equivalent() testing.
593 bool use_tiebreaker_for_sort_only_; 490 bool use_tiebreaker_for_sort_only_;
594 }; 491 };
595 492
596 //------------------------------------------------------------------------------ 493 //------------------------------------------------------------------------------
597 // For each thread, we have a ThreadData that stores all tracking info generated 494 // For each thread, we have a ThreadData that stores all tracking info generated
598 // on this thread. This prevents the need for locking as data accumulates. 495 // on this thread. This prevents the need for locking as data accumulates.
599 // We use ThreadLocalStorage to quickly identfy the current ThreadData context.
600 // We also have a linked list of ThreadData instances, and that list is used to
601 // harvest data from all existing instances.
602 496
603 class BASE_EXPORT ThreadData { 497 class BASE_EXPORT ThreadData {
604 public: 498 public:
605 // Current allowable states of the tracking system. The states can vary
606 // between ACTIVE and DEACTIVATED, but can never go back to UNINITIALIZED.
607 enum Status {
608 UNINITIALIZED,
609 ACTIVE,
610 DEACTIVATED,
611 };
612
613 typedef std::map<Location, Births*> BirthMap; 499 typedef std::map<Location, Births*> BirthMap;
614 typedef std::map<const Births*, DeathData> DeathMap; 500 typedef std::map<const Births*, DeathData> DeathMap;
615 501
616 // Initialize the current thread context with a new instance of ThreadData. 502 // Initialize the current thread context with a new instance of ThreadData.
617 // This is used by all threads that have names, and should be explicitly 503 // This is used by all threads that have names, and can be explicitly
618 // set *before* any births on the threads have taken place. It is generally 504 // set *before* any births are threads have taken place. It is generally
619 // only used by the message loop, which has a well defined thread name. 505 // only used by the message loop, which has a well defined name.
620 static void InitializeThreadContext(const std::string& suggested_name); 506 static void InitializeThreadContext(const std::string& suggested_name);
621 507
622 // Using Thread Local Store, find the current instance for collecting data. 508 // Using Thread Local Store, find the current instance for collecting data.
623 // If an instance does not exist, construct one (and remember it for use on 509 // If an instance does not exist, construct one (and remember it for use on
624 // this thread. 510 // this thread.
625 // This may return NULL if the system is disabled for any reason. 511 // This may return NULL if the system is disabled for any reason.
626 static ThreadData* Get(); 512 static ThreadData* Get();
627 513
628 // For a given (unescaped) about:tracking query, develop resulting HTML, and 514 // For a given (unescaped) about:tracking query, develop resulting HTML, and
629 // append to output. 515 // append to output.
630 static void WriteHTML(const std::string& query, std::string* output); 516 static void WriteHTML(const std::string& query, std::string* output);
631 517
518 // Constructe a ListValue instance containing all recursive results in our
519 // process. The caller assumes ownership of the memory in the returned
520 // instance. The |process_type| should become an enum, which corresponds
521 // to all possible process types. I'm using an int as a placeholder.
522 static base::Value* ToValue(int process_type);
523
632 // For a given accumulated array of results, use the comparator to sort and 524 // For a given accumulated array of results, use the comparator to sort and
633 // subtotal, writing the results to the output. 525 // subtotal, writing the results to the output.
634 static void WriteHTMLTotalAndSubtotals( 526 static void WriteHTMLTotalAndSubtotals(
635 const DataCollector::Collection& match_array, 527 const DataCollector::Collection& match_array,
636 const Comparator& comparator, std::string* output); 528 const Comparator& comparator, std::string* output);
637 529
638 // Constructs a DictionaryValue instance containing all recursive results in 530 // Find (or create) a place to count births from the given location in this
639 // our process. The caller assumes ownership of the memory in the returned
640 // instance.
641 static base::DictionaryValue* ToValue();
642
643 // Finds (or creates) a place to count births from the given location in this
644 // thread, and increment that tally. 531 // thread, and increment that tally.
645 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. 532 // TallyABirthIfActive will returns NULL if the birth cannot be tallied.
646 static Births* TallyABirthIfActive(const Location& location); 533 static Births* TallyABirthIfActive(const Location& location);
647 534
648 // Records the end of a timed run of an object. The |completed_task| contains
649 // a pointer to a Births, the time_posted, and a delayed_start_time if any.
650 // The |start_of_run| indicates when we started to perform the run of the
651 // task. The delayed_start_time is non-null for tasks that were posted as
652 // delayed tasks, and it indicates when the task should have run (i.e., when
653 // it should have posted out of the timer queue, and into the work queue.
654 // The |end_of_run| was just obtained by a call to Now() (just after the task
655 // finished). It is provided as an argument to help with testing.
656 static void TallyRunOnNamedThreadIfTracking(
657 const base::TrackingInfo& completed_task,
658 const TrackedTime& start_of_run,
659 const TrackedTime& end_of_run);
660
661 // Record the end of a timed run of an object. The |birth| is the record for 535 // Record the end of a timed run of an object. The |birth| is the record for
662 // the instance, the |time_posted| records that instant, which is presumed to 536 // the instance, the |time_posted| and |start_of_run| are times of posting
663 // be when the task was posted into a queue to run on a worker thread. 537 // into a message loop queue, and of starting to perform the run of the task.
664 // The |start_of_run| is when the worker thread started to perform the run of
665 // the task.
666 // The |end_of_run| was just obtained by a call to Now() (just after the task 538 // The |end_of_run| was just obtained by a call to Now() (just after the task
667 // finished). 539 // finished).
668 static void TallyRunOnWorkerThreadIfTracking( 540 static void TallyADeathIfActive(const Births* birth,
669 const Births* birth, 541 const base::TimeTicks& time_posted,
670 const TrackedTime& time_posted, 542 const base::TimeTicks& delayed_start_time,
671 const TrackedTime& start_of_run, 543 const base::TimeTicks& start_of_run,
672 const TrackedTime& end_of_run); 544 const base::TimeTicks& end_of_run);
673 545
674 const std::string thread_name() const { return thread_name_; } 546 const std::string thread_name() const { return thread_name_; }
675 547
676 // --------------------- 548 // ---------------------
677 // The following functions should all be private, and are only public because 549 // The following functions should all be private, and are only public because
678 // the collection is done externally. We need to relocate that code from the 550 // the collection is done externally. We need to relocate that code from the
679 // collection class into this class, and then all these methods can be made 551 // collection class into this class, and then all these methods can be made
680 // private. 552 // private.
681 // (Thread safe) Get start of list of all ThreadData instances. 553 // (Thread safe) Get start of list of all ThreadData instances.
682 static ThreadData* first(); 554 static ThreadData* first();
683 // Iterate through the null terminated list of ThreadData instances. 555 // Iterate through the null terminated list of ThreadData instances.
684 ThreadData* next() const { return next_; } 556 ThreadData* next() const { return next_; }
685 // Using our lock, make a copy of the specified maps. These calls may arrive 557 // Using our lock, make a copy of the specified maps. These calls may arrive
686 // from non-local threads, and are used to quickly scan data from all threads 558 // from non-local threads, and are used to quickly scan data from all threads
687 // in order to build an HTML page for about:tracking. 559 // in order to build an HTML page for about:tracking.
688 void SnapshotBirthMap(BirthMap *output) const; 560 void SnapshotBirthMap(BirthMap *output) const;
689 void SnapshotDeathMap(DeathMap *output) const; 561 void SnapshotDeathMap(DeathMap *output) const;
690 // -------- end of should be private methods. 562 // -------- end of should be private methods.
691 563
692 // Hack: asynchronously clear all birth counts and death tallies data values 564 // Hack: asynchronously clear all birth counts and death tallies data values
693 // in all ThreadData instances. The numerical (zeroing) part is done without 565 // in all ThreadData instances. The numerical (zeroing) part is done without
694 // use of a locks or atomics exchanges, and may (for int64 values) produce 566 // use of a locks or atomics exchanges, and may (for int64 values) produce
695 // bogus counts VERY rarely. 567 // bogus counts VERY rarely.
696 static void ResetAllThreadData(); 568 static void ResetAllThreadData();
697 569
698 // Initializes all statics if needed (this initialization call should be made 570 // Set internal status_ to either become ACTIVE, or later, to be SHUTDOWN,
699 // while we are single threaded). Returns false if unable to initialize.
700 static bool Initialize();
701
702 // Sets internal status_ to either become ACTIVE, or DEACTIVATED,
703 // based on argument being true or false respectively. 571 // based on argument being true or false respectively.
704 // If tracking is not compiled in, this function will return false. 572 // IF tracking is not compiled in, this function will return false.
705 static bool InitializeAndSetTrackingStatus(bool status); 573 static bool StartTracking(bool status);
706 static bool tracking_status(); 574 static bool IsActive();
707 575
708 // Provide a time function that does nothing (runs fast) when we don't have 576 // Provide a time function that does nothing (runs fast) when we don't have
709 // the profiler enabled. It will generally be optimized away when it is 577 // the profiler enabled. It will generally be optimized away when it is
710 // ifdef'ed to be small enough (allowing the profiler to be "compiled out" of 578 // ifdef'ed to be small enough (allowing the profiler to be "compiled out" of
711 // the code). 579 // the code).
712 static TrackedTime Now(); 580 static base::TimeTicks Now();
713 581
714 // WARNING: ONLY call this function when you are running single threaded 582 // WARNING: ONLY call this function when you are running single threaded
715 // (again) and all message loops and threads have terminated. Until that 583 // (again) and all message loops and threads have terminated. Until that
716 // point some threads may still attempt to write into our data structures. 584 // point some threads may still attempt to write into our data structures.
717 // Delete recursively all data structures, starting with the list of 585 // Delete recursively all data structures, starting with the list of
718 // ThreadData instances. 586 // ThreadData instances.
719 static void ShutdownSingleThreadedCleanup(); 587 static void ShutdownSingleThreadedCleanup();
720 588
721 private: 589 private:
590 // Current allowable states of the tracking system. The states always
591 // proceed towards SHUTDOWN, and never go backwards.
592 enum Status {
593 UNINITIALIZED,
594 ACTIVE,
595 SHUTDOWN,
596 };
597
722 typedef std::stack<const ThreadData*> ThreadDataPool; 598 typedef std::stack<const ThreadData*> ThreadDataPool;
723 599
724 // Worker thread construction creates a name since there is none. 600 // Worker thread construction creates a name since there is none.
725 ThreadData(); 601 ThreadData();
726 // Message loop based construction should provide a name. 602 // Message loop based construction should provide a name.
727 explicit ThreadData(const std::string& suggested_name); 603 explicit ThreadData(const std::string& suggested_name);
728 604
729 ~ThreadData(); 605 ~ThreadData();
730 606
731 // Push this instance to the head of all_thread_data_list_head_, linking it to 607 // Push this instance to the head of all_thread_data_list_head_, linking it to
732 // the previous head. This is performed after each construction, and leaves 608 // the previous head. This is performed after each construction, and leaves
733 // the instance permanently on that list. 609 // the instance permanently on that list.
734 void PushToHeadOfList(); 610 void PushToHeadOfList();
735 611
736 // In this thread's data, record a new birth. 612 // In this thread's data, record a new birth.
737 Births* TallyABirth(const Location& location); 613 Births* TallyABirth(const Location& location);
738 614
739 // Find a place to record a death on this thread. 615 // Find a place to record a death on this thread.
740 void TallyADeath(const Births& birth, 616 void TallyADeath(const Births& birth,
741 const Duration& queue_duration, 617 const base::TimeDelta& queue_duration,
742 const Duration& duration); 618 const base::TimeDelta& duration);
743 619
744 // Using our lock to protect the iteration, Clear all birth and death data. 620 // Using our lock to protect the iteration, Clear all birth and death data.
745 void Reset(); 621 void Reset();
746 622
747 // This method is called by the TLS system when a thread terminates. 623 // This method is called by the TLS system when a thread terminates.
748 // The argument may be NULL if this thread has never tracked a birth or death. 624 // The argument may be NULL if this thread has never tracked a birth or death.
749 static void OnThreadTermination(void* thread_data); 625 static void OnThreadTermination(void* thread_data);
750 626
751 // This method should be called when a worker thread terminates, so that we 627 // This method should be called when a worker thread terminates, so that we
752 // can save all the thread data into a cache of reusable ThreadData instances. 628 // can save all the thread data into a cache of reusable ThreadData instances.
753 void OnThreadTerminationCleanup() const; 629 void OnThreadTerminationCleanup() const;
754 630
755 // We use thread local store to identify which ThreadData to interact with. 631 // We use thread local store to identify which ThreadData to interact with.
756 static base::ThreadLocalStorage::Slot tls_index_; 632 static base::ThreadLocalStorage::Slot tls_index_;
757 633
758 // Link to the most recently created instance (starts a null terminated list). 634 // Link to the most recently created instance (starts a null terminated list).
759 // The list is traversed by about:tracking when it needs to snapshot data. 635 // The list is traversed by about:tracking when it needs to snapshot data.
760 static ThreadData* all_thread_data_list_head_; 636 static ThreadData* all_thread_data_list_head_;
761 // Set of ThreadData instances for use with worker threads. When a worker 637 // Set of ThreadData instances for use with worker threads. When a worker
762 // thread is done (terminating), we push it into this pool. When a new worker 638 // thread is done (terminating), we push it into this pool. When a new worker
763 // thread is created, we first try to re-use a ThreadData instance from the 639 // thread is created, we first try to re-use a ThreadData instance from the
764 // pool, and if none are available, construct a new one. 640 // pool, and if none are available, construct a new one.
765 static ThreadDataPool* unregistered_thread_data_pool_; 641 static ThreadDataPool* unregistered_thread_data_pool_;
766 // Protection for access to all_thread_data_list_head_, and to 642 // Protection for access to all_thread_data_list_head_, and to
767 // unregistered_thread_data_pool_. This lock is leaked at shutdown. 643 // unregistered_thread_data_pool_.
768 static base::Lock* list_lock_; 644 static base::Lock list_lock_;
769 645
770 // We set status_ to SHUTDOWN when we shut down the tracking service. 646 // We set status_ to SHUTDOWN when we shut down the tracking service.
771 static Status status_; 647 static Status status_;
772 648
773 // Link to next instance (null terminated list). Used to globally track all 649 // Link to next instance (null terminated list). Used to globally track all
774 // registered instances (corresponds to all registered threads where we keep 650 // registered instances (corresponds to all registered threads where we keep
775 // data). 651 // data).
776 ThreadData* next_; 652 ThreadData* next_;
777 653
778 // The name of the thread that is being recorded. If this thread has no 654 // The name of the thread that is being recorded. If this thread has no
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 688
813 //------------------------------------------------------------------------------ 689 //------------------------------------------------------------------------------
814 // Provide simple way to to start global tracking, and to tear down tracking 690 // Provide simple way to to start global tracking, and to tear down tracking
815 // when done. The design has evolved to *not* do any teardown (and just leak 691 // when done. The design has evolved to *not* do any teardown (and just leak
816 // all allocated data structures). As a result, we don't have any code in this 692 // all allocated data structures). As a result, we don't have any code in this
817 // destructor, and perhaps this whole class should go away. 693 // destructor, and perhaps this whole class should go away.
818 694
819 class BASE_EXPORT AutoTracking { 695 class BASE_EXPORT AutoTracking {
820 public: 696 public:
821 AutoTracking() { 697 AutoTracking() {
822 ThreadData::Initialize(); 698 if (state_ != kNeverBeenRun)
699 return;
700 ThreadData::StartTracking(true);
701 state_ = kRunning;
823 } 702 }
824 703
825 ~AutoTracking() { 704 ~AutoTracking() {
826 // TODO(jar): Consider emitting a CSV dump of the data at this point. This
827 // should be called after the message loops have all terminated (or at least
828 // the main message loop is gone), so there is little chance for additional
829 // tasks to be Run.
830 } 705 }
831 706
832 private: 707 private:
708 enum State {
709 kNeverBeenRun,
710 kRunning,
711 };
712 static State state_;
833 713
834 DISALLOW_COPY_AND_ASSIGN(AutoTracking); 714 DISALLOW_COPY_AND_ASSIGN(AutoTracking);
835 }; 715 };
836 716
837 } // namespace tracked_objects 717 } // namespace tracked_objects
838 718
839 #endif // BASE_TRACKED_OBJECTS_H_ 719 #endif // BASE_TRACKED_OBJECTS_H_
OLDNEW
« no previous file with comments | « base/threading/worker_pool_win.cc ('k') | base/tracked_objects.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698