OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 #include <map> | 10 #include <map> |
9 #include <set> | 11 #include <set> |
10 #include <stack> | 12 #include <stack> |
11 #include <string> | 13 #include <string> |
12 #include <utility> | 14 #include <utility> |
13 #include <vector> | 15 #include <vector> |
14 | 16 |
15 #include "base/atomicops.h" | 17 #include "base/atomicops.h" |
16 #include "base/base_export.h" | 18 #include "base/base_export.h" |
17 #include "base/basictypes.h" | |
18 #include "base/containers/hash_tables.h" | 19 #include "base/containers/hash_tables.h" |
19 #include "base/gtest_prod_util.h" | 20 #include "base/gtest_prod_util.h" |
20 #include "base/lazy_instance.h" | 21 #include "base/lazy_instance.h" |
21 #include "base/location.h" | 22 #include "base/location.h" |
| 23 #include "base/macros.h" |
22 #include "base/process/process_handle.h" | 24 #include "base/process/process_handle.h" |
23 #include "base/profiler/alternate_timer.h" | 25 #include "base/profiler/alternate_timer.h" |
24 #include "base/profiler/tracked_time.h" | 26 #include "base/profiler/tracked_time.h" |
25 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
26 #include "base/threading/thread_checker.h" | 28 #include "base/threading/thread_checker.h" |
27 #include "base/threading/thread_local_storage.h" | 29 #include "base/threading/thread_local_storage.h" |
28 | 30 |
29 namespace base { | 31 namespace base { |
30 struct TrackingInfo; | 32 struct TrackingInfo; |
31 } | 33 } |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 254 |
253 struct BASE_EXPORT DeathDataSnapshot { | 255 struct BASE_EXPORT DeathDataSnapshot { |
254 DeathDataSnapshot(); | 256 DeathDataSnapshot(); |
255 | 257 |
256 // Constructs the snapshot from individual values. | 258 // Constructs the snapshot from individual values. |
257 // The alternative would be taking a DeathData parameter, but this would | 259 // The alternative would be taking a DeathData parameter, but this would |
258 // create a loop since DeathData indirectly refers DeathDataSnapshot. Passing | 260 // create a loop since DeathData indirectly refers DeathDataSnapshot. Passing |
259 // a wrapper structure as a param or using an empty constructor for | 261 // a wrapper structure as a param or using an empty constructor for |
260 // snapshotting DeathData would be less efficient. | 262 // snapshotting DeathData would be less efficient. |
261 DeathDataSnapshot(int count, | 263 DeathDataSnapshot(int count, |
262 int32 run_duration_sum, | 264 int32_t run_duration_sum, |
263 int32 run_duration_max, | 265 int32_t run_duration_max, |
264 int32 run_duration_sample, | 266 int32_t run_duration_sample, |
265 int32 queue_duration_sum, | 267 int32_t queue_duration_sum, |
266 int32 queue_duration_max, | 268 int32_t queue_duration_max, |
267 int32 queue_duration_sample); | 269 int32_t queue_duration_sample); |
268 ~DeathDataSnapshot(); | 270 ~DeathDataSnapshot(); |
269 | 271 |
270 // Calculates and returns the delta between this snapshot and an earlier | 272 // Calculates and returns the delta between this snapshot and an earlier |
271 // snapshot of the same task |older|. | 273 // snapshot of the same task |older|. |
272 DeathDataSnapshot Delta(const DeathDataSnapshot& older) const; | 274 DeathDataSnapshot Delta(const DeathDataSnapshot& older) const; |
273 | 275 |
274 int count; | 276 int count; |
275 int32 run_duration_sum; | 277 int32_t run_duration_sum; |
276 int32 run_duration_max; | 278 int32_t run_duration_max; |
277 int32 run_duration_sample; | 279 int32_t run_duration_sample; |
278 int32 queue_duration_sum; | 280 int32_t queue_duration_sum; |
279 int32 queue_duration_max; | 281 int32_t queue_duration_max; |
280 int32 queue_duration_sample; | 282 int32_t queue_duration_sample; |
281 }; | 283 }; |
282 | 284 |
283 //------------------------------------------------------------------------------ | 285 //------------------------------------------------------------------------------ |
284 // A "snapshotted" representation of the DeathData for a particular profiling | 286 // A "snapshotted" representation of the DeathData for a particular profiling |
285 // phase. Used as an element of the list of phase snapshots owned by DeathData. | 287 // phase. Used as an element of the list of phase snapshots owned by DeathData. |
286 | 288 |
287 struct DeathDataPhaseSnapshot { | 289 struct DeathDataPhaseSnapshot { |
288 DeathDataPhaseSnapshot(int profiling_phase, | 290 DeathDataPhaseSnapshot(int profiling_phase, |
289 int count, | 291 int count, |
290 int32 run_duration_sum, | 292 int32_t run_duration_sum, |
291 int32 run_duration_max, | 293 int32_t run_duration_max, |
292 int32 run_duration_sample, | 294 int32_t run_duration_sample, |
293 int32 queue_duration_sum, | 295 int32_t queue_duration_sum, |
294 int32 queue_duration_max, | 296 int32_t queue_duration_max, |
295 int32 queue_duration_sample, | 297 int32_t queue_duration_sample, |
296 const DeathDataPhaseSnapshot* prev); | 298 const DeathDataPhaseSnapshot* prev); |
297 | 299 |
298 // Profiling phase at which completion this snapshot was taken. | 300 // Profiling phase at which completion this snapshot was taken. |
299 int profiling_phase; | 301 int profiling_phase; |
300 | 302 |
301 // Death data snapshot. | 303 // Death data snapshot. |
302 DeathDataSnapshot death_data; | 304 DeathDataSnapshot death_data; |
303 | 305 |
304 // Pointer to a snapshot from the previous phase. | 306 // Pointer to a snapshot from the previous phase. |
305 const DeathDataPhaseSnapshot* prev; | 307 const DeathDataPhaseSnapshot* prev; |
(...skipping 12 matching lines...) Expand all Loading... |
318 // snapshotted. | 320 // snapshotted. |
319 | 321 |
320 class BASE_EXPORT DeathData { | 322 class BASE_EXPORT DeathData { |
321 public: | 323 public: |
322 DeathData(); | 324 DeathData(); |
323 DeathData(const DeathData& other); | 325 DeathData(const DeathData& other); |
324 ~DeathData(); | 326 ~DeathData(); |
325 | 327 |
326 // Update stats for a task destruction (death) that had a Run() time of | 328 // Update stats for a task destruction (death) that had a Run() time of |
327 // |duration|, and has had a queueing delay of |queue_duration|. | 329 // |duration|, and has had a queueing delay of |queue_duration|. |
328 void RecordDeath(const int32 queue_duration, | 330 void RecordDeath(const int32_t queue_duration, |
329 const int32 run_duration, | 331 const int32_t run_duration, |
330 const uint32 random_number); | 332 const uint32_t random_number); |
331 | 333 |
332 // Metrics and past snapshots accessors, used only for serialization and in | 334 // Metrics and past snapshots accessors, used only for serialization and in |
333 // tests. | 335 // tests. |
334 int count() const { return count_; } | 336 int count() const { return count_; } |
335 int32 run_duration_sum() const { return run_duration_sum_; } | 337 int32_t run_duration_sum() const { return run_duration_sum_; } |
336 int32 run_duration_max() const { return run_duration_max_; } | 338 int32_t run_duration_max() const { return run_duration_max_; } |
337 int32 run_duration_sample() const { return run_duration_sample_; } | 339 int32_t run_duration_sample() const { return run_duration_sample_; } |
338 int32 queue_duration_sum() const { return queue_duration_sum_; } | 340 int32_t queue_duration_sum() const { return queue_duration_sum_; } |
339 int32 queue_duration_max() const { return queue_duration_max_; } | 341 int32_t queue_duration_max() const { return queue_duration_max_; } |
340 int32 queue_duration_sample() const { return queue_duration_sample_; } | 342 int32_t queue_duration_sample() const { return queue_duration_sample_; } |
341 const DeathDataPhaseSnapshot* last_phase_snapshot() const { | 343 const DeathDataPhaseSnapshot* last_phase_snapshot() const { |
342 return last_phase_snapshot_; | 344 return last_phase_snapshot_; |
343 } | 345 } |
344 | 346 |
345 // Called when the current profiling phase, identified by |profiling_phase|, | 347 // Called when the current profiling phase, identified by |profiling_phase|, |
346 // ends. | 348 // ends. |
347 // Must be called only on the snapshot thread. | 349 // Must be called only on the snapshot thread. |
348 void OnProfilingPhaseCompleted(int profiling_phase); | 350 void OnProfilingPhaseCompleted(int profiling_phase); |
349 | 351 |
350 private: | 352 private: |
351 // Members are ordered from most regularly read and updated, to least | 353 // Members are ordered from most regularly read and updated, to least |
352 // frequently used. This might help a bit with cache lines. | 354 // frequently used. This might help a bit with cache lines. |
353 // Number of runs seen (divisor for calculating averages). | 355 // Number of runs seen (divisor for calculating averages). |
354 // Can be incremented only on the death thread. | 356 // Can be incremented only on the death thread. |
355 int count_; | 357 int count_; |
356 | 358 |
357 // Count used in determining probability of selecting exec/queue times from a | 359 // Count used in determining probability of selecting exec/queue times from a |
358 // recorded death as samples. | 360 // recorded death as samples. |
359 // Gets incremented only on the death thread, but can be set to 0 by | 361 // Gets incremented only on the death thread, but can be set to 0 by |
360 // OnProfilingPhaseCompleted() on the snapshot thread. | 362 // OnProfilingPhaseCompleted() on the snapshot thread. |
361 int sample_probability_count_; | 363 int sample_probability_count_; |
362 | 364 |
363 // Basic tallies, used to compute averages. Can be incremented only on the | 365 // Basic tallies, used to compute averages. Can be incremented only on the |
364 // death thread. | 366 // death thread. |
365 int32 run_duration_sum_; | 367 int32_t run_duration_sum_; |
366 int32 queue_duration_sum_; | 368 int32_t queue_duration_sum_; |
367 // Max values, used by local visualization routines. These are often read, | 369 // Max values, used by local visualization routines. These are often read, |
368 // but rarely updated. The max values get assigned only on the death thread, | 370 // but rarely updated. The max values get assigned only on the death thread, |
369 // but these fields can be set to 0 by OnProfilingPhaseCompleted() on the | 371 // but these fields can be set to 0 by OnProfilingPhaseCompleted() on the |
370 // snapshot thread. | 372 // snapshot thread. |
371 int32 run_duration_max_; | 373 int32_t run_duration_max_; |
372 int32 queue_duration_max_; | 374 int32_t queue_duration_max_; |
373 // Samples, used by crowd sourcing gatherers. These are almost never read, | 375 // Samples, used by crowd sourcing gatherers. These are almost never read, |
374 // and rarely updated. They can be modified only on the death thread. | 376 // and rarely updated. They can be modified only on the death thread. |
375 int32 run_duration_sample_; | 377 int32_t run_duration_sample_; |
376 int32 queue_duration_sample_; | 378 int32_t queue_duration_sample_; |
377 | 379 |
378 // Snapshot of this death data made at the last profiling phase completion, if | 380 // Snapshot of this death data made at the last profiling phase completion, if |
379 // any. DeathData owns the whole list starting with this pointer. | 381 // any. DeathData owns the whole list starting with this pointer. |
380 // Can be accessed only on the snapshot thread. | 382 // Can be accessed only on the snapshot thread. |
381 const DeathDataPhaseSnapshot* last_phase_snapshot_; | 383 const DeathDataPhaseSnapshot* last_phase_snapshot_; |
382 | 384 |
383 DISALLOW_ASSIGN(DeathData); | 385 DISALLOW_ASSIGN(DeathData); |
384 }; | 386 }; |
385 | 387 |
386 //------------------------------------------------------------------------------ | 388 //------------------------------------------------------------------------------ |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 | 570 |
569 // Iterate through the null terminated list of ThreadData instances. | 571 // Iterate through the null terminated list of ThreadData instances. |
570 ThreadData* next() const; | 572 ThreadData* next() const; |
571 | 573 |
572 | 574 |
573 // In this thread's data, record a new birth. | 575 // In this thread's data, record a new birth. |
574 Births* TallyABirth(const Location& location); | 576 Births* TallyABirth(const Location& location); |
575 | 577 |
576 // Find a place to record a death on this thread. | 578 // Find a place to record a death on this thread. |
577 void TallyADeath(const Births& births, | 579 void TallyADeath(const Births& births, |
578 int32 queue_duration, | 580 int32_t queue_duration, |
579 const TaskStopwatch& stopwatch); | 581 const TaskStopwatch& stopwatch); |
580 | 582 |
581 // Snapshots (under a lock) the profiled data for the tasks for this thread | 583 // Snapshots (under a lock) the profiled data for the tasks for this thread |
582 // and writes all of the executed tasks' data -- i.e. the data for all | 584 // and writes all of the executed tasks' data -- i.e. the data for all |
583 // profiling phases (including the current one: |current_profiling_phase|) for | 585 // profiling phases (including the current one: |current_profiling_phase|) for |
584 // the tasks with with entries in the death_map_ -- into |phased_snapshots|. | 586 // the tasks with with entries in the death_map_ -- into |phased_snapshots|. |
585 // Also updates the |birth_counts| tally for each task to keep track of the | 587 // Also updates the |birth_counts| tally for each task to keep track of the |
586 // number of living instances of the task -- that is, each task maps to the | 588 // number of living instances of the task -- that is, each task maps to the |
587 // number of births for the task that have not yet been balanced by a death. | 589 // number of births for the task that have not yet been balanced by a death. |
588 void SnapshotExecutedTasks(int current_profiling_phase, | 590 void SnapshotExecutedTasks(int current_profiling_phase, |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 // threads. To support this, we acquire this lock if we are writing from this | 703 // threads. To support this, we acquire this lock if we are writing from this |
702 // thread, or reading from another thread. For reading from this thread we | 704 // thread, or reading from another thread. For reading from this thread we |
703 // don't need a lock, as there is no potential for a conflict since the | 705 // don't need a lock, as there is no potential for a conflict since the |
704 // writing is only done from this thread. | 706 // writing is only done from this thread. |
705 mutable base::Lock map_lock_; | 707 mutable base::Lock map_lock_; |
706 | 708 |
707 // A random number that we used to select decide which sample to keep as a | 709 // A random number that we used to select decide which sample to keep as a |
708 // representative sample in each DeathData instance. We can't start off with | 710 // representative sample in each DeathData instance. We can't start off with |
709 // much randomness (because we can't call RandInt() on all our threads), so | 711 // much randomness (because we can't call RandInt() on all our threads), so |
710 // we stir in more and more as we go. | 712 // we stir in more and more as we go. |
711 uint32 random_number_; | 713 uint32_t random_number_; |
712 | 714 |
713 // Record of what the incarnation_counter_ was when this instance was created. | 715 // Record of what the incarnation_counter_ was when this instance was created. |
714 // If the incarnation_counter_ has changed, then we avoid pushing into the | 716 // If the incarnation_counter_ has changed, then we avoid pushing into the |
715 // pool (this is only critical in tests which go through multiple | 717 // pool (this is only critical in tests which go through multiple |
716 // incarnations). | 718 // incarnations). |
717 int incarnation_count_for_pool_; | 719 int incarnation_count_for_pool_; |
718 | 720 |
719 // Most recently started (i.e. most nested) stopwatch on the current thread, | 721 // Most recently started (i.e. most nested) stopwatch on the current thread, |
720 // if it exists; NULL otherwise. | 722 // if it exists; NULL otherwise. |
721 TaskStopwatch* current_stopwatch_; | 723 TaskStopwatch* current_stopwatch_; |
(...skipping 19 matching lines...) Expand all Loading... |
741 // Stops stopwatch. | 743 // Stops stopwatch. |
742 void Stop(); | 744 void Stop(); |
743 | 745 |
744 // Returns the start time. | 746 // Returns the start time. |
745 TrackedTime StartTime() const; | 747 TrackedTime StartTime() const; |
746 | 748 |
747 // Task's duration is calculated as the wallclock duration between starting | 749 // Task's duration is calculated as the wallclock duration between starting |
748 // and stopping this stopwatch, minus the wallclock durations of any other | 750 // and stopping this stopwatch, minus the wallclock durations of any other |
749 // instances that are immediately nested in this one, started and stopped on | 751 // instances that are immediately nested in this one, started and stopped on |
750 // this thread during that period. | 752 // this thread during that period. |
751 int32 RunDurationMs() const; | 753 int32_t RunDurationMs() const; |
752 | 754 |
753 // Returns tracking info for the current thread. | 755 // Returns tracking info for the current thread. |
754 ThreadData* GetThreadData() const; | 756 ThreadData* GetThreadData() const; |
755 | 757 |
756 private: | 758 private: |
757 // Time when the stopwatch was started. | 759 // Time when the stopwatch was started. |
758 TrackedTime start_time_; | 760 TrackedTime start_time_; |
759 | 761 |
760 // Wallclock duration of the task. | 762 // Wallclock duration of the task. |
761 int32 wallclock_duration_ms_; | 763 int32_t wallclock_duration_ms_; |
762 | 764 |
763 // Tracking info for the current thread. | 765 // Tracking info for the current thread. |
764 ThreadData* current_thread_data_; | 766 ThreadData* current_thread_data_; |
765 | 767 |
766 // Sum of wallclock durations of all stopwatches that were directly nested in | 768 // Sum of wallclock durations of all stopwatches that were directly nested in |
767 // this one. | 769 // this one. |
768 int32 excluded_duration_ms_; | 770 int32_t excluded_duration_ms_; |
769 | 771 |
770 // Stopwatch which was running on our thread when this stopwatch was started. | 772 // Stopwatch which was running on our thread when this stopwatch was started. |
771 // That preexisting stopwatch must be adjusted to the exclude the wallclock | 773 // That preexisting stopwatch must be adjusted to the exclude the wallclock |
772 // duration of this stopwatch. | 774 // duration of this stopwatch. |
773 TaskStopwatch* parent_; | 775 TaskStopwatch* parent_; |
774 | 776 |
775 #if DCHECK_IS_ON() | 777 #if DCHECK_IS_ON() |
776 // State of the stopwatch. Stopwatch is first constructed in a created state | 778 // State of the stopwatch. Stopwatch is first constructed in a created state |
777 // state, then is optionally started/stopped, then destructed. | 779 // state, then is optionally started/stopped, then destructed. |
778 enum { CREATED, RUNNING, STOPPED } state_; | 780 enum { CREATED, RUNNING, STOPPED } state_; |
(...skipping 25 matching lines...) Expand all Loading... |
804 ProcessDataSnapshot(); | 806 ProcessDataSnapshot(); |
805 ~ProcessDataSnapshot(); | 807 ~ProcessDataSnapshot(); |
806 | 808 |
807 PhasedProcessDataSnapshotMap phased_snapshots; | 809 PhasedProcessDataSnapshotMap phased_snapshots; |
808 base::ProcessId process_id; | 810 base::ProcessId process_id; |
809 }; | 811 }; |
810 | 812 |
811 } // namespace tracked_objects | 813 } // namespace tracked_objects |
812 | 814 |
813 #endif // BASE_TRACKED_OBJECTS_H_ | 815 #endif // BASE_TRACKED_OBJECTS_H_ |
OLD | NEW |