[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_TRACKED_OBJECTS_H_ |
| 6 | #define BASE_TRACKED_OBJECTS_H_ |
| 7 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include <map> |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 9 | #include <set> |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 10 | #include <stack> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include <string> |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 12 | #include <utility> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
amistry | 42d1688 | 2015-07-17 03:58:06 | [diff] [blame] | 15 | #include "base/atomicops.h" |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 16 | #include "base/base_export.h" |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 17 | #include "base/basictypes.h" |
sque | e3b16f1 | 2015-03-14 02:39:03 | [diff] [blame] | 18 | #include "base/containers/hash_tables.h" |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 19 | #include "base/gtest_prod_util.h" |
[email protected] | 77169a6 | 2011-11-14 20:36:46 | [diff] [blame] | 20 | #include "base/lazy_instance.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 21 | #include "base/location.h" |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 22 | #include "base/process/process_handle.h" |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 23 | #include "base/profiler/alternate_timer.h" |
[email protected] | dbe5d207 | 2011-11-08 17:09:21 | [diff] [blame] | 24 | #include "base/profiler/tracked_time.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 25 | #include "base/synchronization/lock.h" |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 26 | #include "base/threading/thread_checker.h" |
[email protected] | 1357c32 | 2010-12-30 22:18:56 | [diff] [blame] | 27 | #include "base/threading/thread_local_storage.h" |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 28 | |
| 29 | namespace base { |
| 30 | struct TrackingInfo; |
| 31 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 33 | // TrackedObjects provides a database of stats about objects (generally Tasks) |
| 34 | // that are tracked. Tracking means their birth, death, duration, birth thread, |
| 35 | // death thread, and birth place are recorded. This data is carefully spread |
| 36 | // across a series of objects so that the counts and times can be rapidly |
| 37 | // updated without (usually) having to lock the data, and hence there is usually |
| 38 | // very little contention caused by the tracking. The data can be viewed via |
[email protected] | dda9768 | 2011-11-14 05:24:07 | [diff] [blame] | 39 | // the about:profiler URL, with a variety of sorting and filtering choices. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 40 | // |
[email protected] | ea319e4 | 2010-11-08 21:47:24 | [diff] [blame] | 41 | // These classes serve as the basis of a profiler of sorts for the Tasks system. |
| 42 | // As a result, design decisions were made to maximize speed, by minimizing |
| 43 | // recurring allocation/deallocation, lock contention and data copying. In the |
| 44 | // "stable" state, which is reached relatively quickly, there is no separate |
| 45 | // marginal allocation cost associated with construction or destruction of |
| 46 | // tracked objects, no locks are generally employed, and probably the largest |
| 47 | // computational cost is associated with obtaining start and stop times for |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 48 | // instances as they are created and destroyed. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 49 | // |
mithro | 5eb5850 | 2014-11-19 18:55:58 | [diff] [blame] | 50 | // The following describes the life cycle of tracking an instance. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 51 | // |
| 52 | // First off, when the instance is created, the FROM_HERE macro is expanded |
| 53 | // to specify the birth place (file, line, function) where the instance was |
| 54 | // created. That data is used to create a transient Location instance |
| 55 | // encapsulating the above triple of information. The strings (like __FILE__) |
| 56 | // are passed around by reference, with the assumption that they are static, and |
| 57 | // will never go away. This ensures that the strings can be dealt with as atoms |
| 58 | // with great efficiency (i.e., copying of strings is never needed, and |
| 59 | // comparisons for equality can be based on pointer comparisons). |
| 60 | // |
| 61 | // Next, a Births instance is created for use ONLY on the thread where this |
| 62 | // instance was created. That Births instance records (in a base class |
| 63 | // BirthOnThread) references to the static data provided in a Location instance, |
| 64 | // as well as a pointer specifying the thread on which the birth takes place. |
| 65 | // Hence there is at most one Births instance for each Location on each thread. |
| 66 | // The derived Births class contains slots for recording statistics about all |
| 67 | // instances born at the same location. Statistics currently include only the |
| 68 | // count of instances constructed. |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 69 | // |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 70 | // Since the base class BirthOnThread contains only constant data, it can be |
| 71 | // freely accessed by any thread at any time (i.e., only the statistic needs to |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 72 | // be handled carefully, and stats are updated exclusively on the birth thread). |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 73 | // |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 74 | // For Tasks, having now either constructed or found the Births instance |
| 75 | // described above, a pointer to the Births instance is then recorded into the |
| 76 | // PendingTask structure in MessageLoop. This fact alone is very useful in |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 77 | // debugging, when there is a question of where an instance came from. In |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 78 | // addition, the birth time is also recorded and used to later evaluate the |
| 79 | // lifetime duration of the whole Task. As a result of the above embedding, we |
| 80 | // can find out a Task's location of birth, and thread of birth, without using |
| 81 | // any locks, as all that data is constant across the life of the process. |
| 82 | // |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 83 | // The above work *could* also be done for any other object as well by calling |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 84 | // TallyABirthIfActive() and TallyRunOnNamedThreadIfTracking() as appropriate. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 85 | // |
| 86 | // The amount of memory used in the above data structures depends on how many |
| 87 | // threads there are, and how many Locations of construction there are. |
| 88 | // Fortunately, we don't use memory that is the product of those two counts, but |
| 89 | // rather we only need one Births instance for each thread that constructs an |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 90 | // instance at a Location. In many cases, instances are only created on one |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 91 | // thread, so the memory utilization is actually fairly restrained. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 92 | // |
| 93 | // Lastly, when an instance is deleted, the final tallies of statistics are |
[email protected] | c7dbf30 | 2011-11-08 07:57:05 | [diff] [blame] | 94 | // carefully accumulated. That tallying writes into slots (members) in a |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 95 | // collection of DeathData instances. For each birth place Location that is |
| 96 | // destroyed on a thread, there is a DeathData instance to record the additional |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 97 | // death count, as well as accumulate the run-time and queue-time durations for |
| 98 | // the instance as it is destroyed (dies). By maintaining a single place to |
| 99 | // aggregate this running sum *only* for the given thread, we avoid the need to |
| 100 | // lock such DeathData instances. (i.e., these accumulated stats in a DeathData |
| 101 | // instance are exclusively updated by the singular owning thread). |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 102 | // |
mithro | 5eb5850 | 2014-11-19 18:55:58 | [diff] [blame] | 103 | // With the above life cycle description complete, the major remaining detail |
| 104 | // is explaining how each thread maintains a list of DeathData instances, and |
| 105 | // of Births instances, and is able to avoid additional (redundant/unnecessary) |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 106 | // allocations. |
| 107 | // |
| 108 | // Each thread maintains a list of data items specific to that thread in a |
| 109 | // ThreadData instance (for that specific thread only). The two critical items |
| 110 | // are lists of DeathData and Births instances. These lists are maintained in |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 111 | // STL maps, which are indexed by Location. As noted earlier, we can compare |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 112 | // locations very efficiently as we consider the underlying data (file, |
| 113 | // function, line) to be atoms, and hence pointer comparison is used rather than |
| 114 | // (slow) string comparisons. |
| 115 | // |
| 116 | // To provide a mechanism for iterating over all "known threads," which means |
| 117 | // threads that have recorded a birth or a death, we create a singly linked list |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 118 | // of ThreadData instances. Each such instance maintains a pointer to the next |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 119 | // one. A static member of ThreadData provides a pointer to the first item on |
| 120 | // this global list, and access via that all_thread_data_list_head_ item |
| 121 | // requires the use of the list_lock_. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 122 | // When new ThreadData instances is added to the global list, it is pre-pended, |
| 123 | // which ensures that any prior acquisition of the list is valid (i.e., the |
| 124 | // holder can iterate over it without fear of it changing, or the necessity of |
| 125 | // using an additional lock. Iterations are actually pretty rare (used |
mithro | 5eb5850 | 2014-11-19 18:55:58 | [diff] [blame] | 126 | // primarily for cleanup, or snapshotting data for display), so this lock has |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 127 | // very little global performance impact. |
| 128 | // |
| 129 | // The above description tries to define the high performance (run time) |
| 130 | // portions of these classes. After gathering statistics, calls instigated |
[email protected] | dda9768 | 2011-11-14 05:24:07 | [diff] [blame] | 131 | // by visiting about:profiler will assemble and aggregate data for display. The |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 132 | // following data structures are used for producing such displays. They are |
| 133 | // not performance critical, and their only major constraint is that they should |
| 134 | // be able to run concurrently with ongoing augmentation of the birth and death |
| 135 | // data. |
| 136 | // |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 137 | // This header also exports collection of classes that provide "snapshotted" |
| 138 | // representations of the core tracked_objects:: classes. These snapshotted |
| 139 | // representations are designed for safe transmission of the tracked_objects:: |
| 140 | // data across process boundaries. Each consists of: |
| 141 | // (1) a default constructor, to support the IPC serialization macros, |
| 142 | // (2) a constructor that extracts data from the type being snapshotted, and |
| 143 | // (3) the snapshotted data. |
| 144 | // |
[email protected] | c7dbf30 | 2011-11-08 07:57:05 | [diff] [blame] | 145 | // For a given birth location, information about births is spread across data |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 146 | // structures that are asynchronously changing on various threads. For |
| 147 | // serialization and display purposes, we need to construct TaskSnapshot |
| 148 | // instances for each combination of birth thread, death thread, and location, |
| 149 | // along with the count of such lifetimes. We gather such data into a |
| 150 | // TaskSnapshot instances, so that such instances can be sorted and |
| 151 | // aggregated (and remain frozen during our processing). |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 152 | // |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 153 | // Profiling consists of phases. The concrete phase in the sequence of phases |
| 154 | // is identified by its 0-based index. |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 155 | // |
| 156 | // The ProcessDataPhaseSnapshot struct is a serialized representation of the |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 157 | // list of ThreadData objects for a process for a concrete profiling phase. It |
vadimt | cf8983e | 2015-05-01 19:13:01 | [diff] [blame] | 158 | // holds a set of TaskSnapshots. The statistics in a snapshot are gathered |
| 159 | // asynhcronously relative to their ongoing updates. |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 160 | // It is possible, though highly unlikely, that stats could be incorrectly |
| 161 | // recorded by this process (all data is held in 32 bit ints, but we are not |
| 162 | // atomically collecting all data, so we could have count that does not, for |
| 163 | // example, match with the number of durations we accumulated). The advantage |
| 164 | // to having fast (non-atomic) updates of the data outweighs the minimal risk of |
| 165 | // a singular corrupt statistic snapshot (only the snapshot could be corrupt, |
mithro | 5eb5850 | 2014-11-19 18:55:58 | [diff] [blame] | 166 | // not the underlying and ongoing statistic). In contrast, pointer data that |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 167 | // is accessed during snapshotting is completely invariant, and hence is |
| 168 | // perfectly acquired (i.e., no potential corruption, and no risk of a bad |
| 169 | // memory reference). |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 170 | // |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 171 | // TODO(jar): We can implement a Snapshot system that *tries* to grab the |
| 172 | // snapshots on the source threads *when* they have MessageLoops available |
| 173 | // (worker threads don't have message loops generally, and hence gathering from |
| 174 | // them will continue to be asynchronous). We had an implementation of this in |
| 175 | // the past, but the difficulty is dealing with message loops being terminated. |
| 176 | // We can *try* to spam the available threads via some message loop proxy to |
mithro | 5eb5850 | 2014-11-19 18:55:58 | [diff] [blame] | 177 | // achieve this feat, and it *might* be valuable when we are collecting data |
| 178 | // for upload via UMA (where correctness of data may be more significant than |
| 179 | // for a single screen of about:profiler). |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 180 | // |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 181 | // TODO(jar): We need to store DataCollections, and provide facilities for |
| 182 | // taking the difference between two gathered DataCollections. For now, we're |
| 183 | // just adding a hack that Reset()s to zero all counts and stats. This is also |
mithro | 5eb5850 | 2014-11-19 18:55:58 | [diff] [blame] | 184 | // done in a slightly thread-unsafe fashion, as the resetting is done |
[email protected] | eab79c38 | 2011-11-06 19:14:48 | [diff] [blame] | 185 | // asynchronously relative to ongoing updates (but all data is 32 bit in size). |
| 186 | // For basic profiling, this will work "most of the time," and should be |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 187 | // sufficient... but storing away DataCollections is the "right way" to do this. |
[email protected] | eab79c38 | 2011-11-06 19:14:48 | [diff] [blame] | 188 | // We'll accomplish this via JavaScript storage of snapshots, and then we'll |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 189 | // remove the Reset() methods. We may also need a short-term-max value in |
| 190 | // DeathData that is reset (as synchronously as possible) during each snapshot. |
| 191 | // This will facilitate displaying a max value for each snapshot period. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 192 | |
| 193 | namespace tracked_objects { |
| 194 | |
| 195 | //------------------------------------------------------------------------------ |
| 196 | // For a specific thread, and a specific birth place, the collection of all |
| 197 | // death info (with tallies for each death thread, to prevent access conflicts). |
| 198 | class ThreadData; |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 199 | class BASE_EXPORT BirthOnThread { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 200 | public: |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 201 | BirthOnThread(const Location& location, const ThreadData& current); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 202 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 203 | const Location location() const { return location_; } |
| 204 | const ThreadData* birth_thread() const { return birth_thread_; } |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 205 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 206 | private: |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 207 | // File/lineno of birth. This defines the essence of the task, as the context |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 208 | // of the birth (construction) often tell what the item is for. This field |
| 209 | // is const, and hence safe to access from any thread. |
| 210 | const Location location_; |
| 211 | |
| 212 | // The thread that records births into this object. Only this thread is |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 213 | // allowed to update birth_count_ (which changes over time). |
| 214 | const ThreadData* const birth_thread_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 216 | DISALLOW_COPY_AND_ASSIGN(BirthOnThread); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | //------------------------------------------------------------------------------ |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 220 | // A "snapshotted" representation of the BirthOnThread class. |
| 221 | |
| 222 | struct BASE_EXPORT BirthOnThreadSnapshot { |
| 223 | BirthOnThreadSnapshot(); |
| 224 | explicit BirthOnThreadSnapshot(const BirthOnThread& birth); |
| 225 | ~BirthOnThreadSnapshot(); |
| 226 | |
| 227 | LocationSnapshot location; |
| 228 | std::string thread_name; |
| 229 | }; |
| 230 | |
| 231 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 232 | // A class for accumulating counts of births (without bothering with a map<>). |
| 233 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 234 | class BASE_EXPORT Births: public BirthOnThread { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | public: |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 236 | Births(const Location& location, const ThreadData& current); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 237 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 238 | int birth_count() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 239 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 240 | // When we have a birth we update the count for this birthplace. |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 241 | void RecordBirth(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 243 | private: |
| 244 | // The number of births on this thread for our location_. |
| 245 | int birth_count_; |
| 246 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 247 | DISALLOW_COPY_AND_ASSIGN(Births); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | //------------------------------------------------------------------------------ |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 251 | // A "snapshotted" representation of the DeathData class. |
| 252 | |
| 253 | struct BASE_EXPORT DeathDataSnapshot { |
| 254 | DeathDataSnapshot(); |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 255 | |
| 256 | // Constructs the snapshot from individual values. |
| 257 | // The alternative would be taking a DeathData parameter, but this would |
| 258 | // create a loop since DeathData indirectly refers DeathDataSnapshot. Passing |
| 259 | // a wrapper structure as a param or using an empty constructor for |
| 260 | // snapshotting DeathData would be less efficient. |
| 261 | DeathDataSnapshot(int count, |
| 262 | int32 run_duration_sum, |
| 263 | int32 run_duration_max, |
| 264 | int32 run_duration_sample, |
| 265 | int32 queue_duration_sum, |
| 266 | int32 queue_duration_max, |
| 267 | int32 queue_duration_sample); |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 268 | ~DeathDataSnapshot(); |
| 269 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 270 | // Calculates and returns the delta between this snapshot and an earlier |
| 271 | // snapshot of the same task |older|. |
| 272 | DeathDataSnapshot Delta(const DeathDataSnapshot& older) const; |
| 273 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 274 | int count; |
| 275 | int32 run_duration_sum; |
| 276 | int32 run_duration_max; |
| 277 | int32 run_duration_sample; |
| 278 | int32 queue_duration_sum; |
| 279 | int32 queue_duration_max; |
| 280 | int32 queue_duration_sample; |
| 281 | }; |
| 282 | |
| 283 | //------------------------------------------------------------------------------ |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 284 | // 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. |
| 286 | |
| 287 | struct DeathDataPhaseSnapshot { |
| 288 | DeathDataPhaseSnapshot(int profiling_phase, |
| 289 | int count, |
| 290 | int32 run_duration_sum, |
| 291 | int32 run_duration_max, |
| 292 | int32 run_duration_sample, |
| 293 | int32 queue_duration_sum, |
| 294 | int32 queue_duration_max, |
| 295 | int32 queue_duration_sample, |
| 296 | const DeathDataPhaseSnapshot* prev); |
| 297 | |
| 298 | // Profiling phase at which completion this snapshot was taken. |
| 299 | int profiling_phase; |
| 300 | |
| 301 | // Death data snapshot. |
| 302 | DeathDataSnapshot death_data; |
| 303 | |
| 304 | // Pointer to a snapshot from the previous phase. |
| 305 | const DeathDataPhaseSnapshot* prev; |
| 306 | }; |
| 307 | |
| 308 | //------------------------------------------------------------------------------ |
| 309 | // Information about deaths of a task on a given thread, called "death thread". |
| 310 | // Access to members of this class is never protected by a lock. The fields |
| 311 | // are accessed in such a way that corruptions resulting from race conditions |
| 312 | // are not significant, and don't accumulate as a result of multiple accesses. |
| 313 | // All invocations of DeathData::OnProfilingPhaseCompleted and |
| 314 | // ThreadData::SnapshotMaps (which takes DeathData snapshot) in a given process |
| 315 | // must be called from the same thread. It doesn't matter what thread it is, but |
| 316 | // it's important the same thread is used as a snapshot thread during the whole |
| 317 | // process lifetime. All fields except sample_probability_count_ can be |
| 318 | // snapshotted. |
| 319 | |
| 320 | class BASE_EXPORT DeathData { |
| 321 | public: |
| 322 | DeathData(); |
| 323 | DeathData(const DeathData& other); |
| 324 | ~DeathData(); |
| 325 | |
| 326 | // Update stats for a task destruction (death) that had a Run() time of |
| 327 | // |duration|, and has had a queueing delay of |queue_duration|. |
| 328 | void RecordDeath(const int32 queue_duration, |
| 329 | const int32 run_duration, |
| 330 | const uint32 random_number); |
| 331 | |
| 332 | // Metrics and past snapshots accessors, used only for serialization and in |
| 333 | // tests. |
vadimt | b9d054ae28 | 2015-05-06 22:14:42 | [diff] [blame] | 334 | int count() const { return count_; } |
| 335 | int32 run_duration_sum() const { return run_duration_sum_; } |
| 336 | int32 run_duration_max() const { return run_duration_max_; } |
| 337 | int32 run_duration_sample() const { return run_duration_sample_; } |
| 338 | int32 queue_duration_sum() const { return queue_duration_sum_; } |
| 339 | int32 queue_duration_max() const { return queue_duration_max_; } |
| 340 | int32 queue_duration_sample() const { return queue_duration_sample_; } |
| 341 | const DeathDataPhaseSnapshot* last_phase_snapshot() const { |
| 342 | return last_phase_snapshot_; |
| 343 | } |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 344 | |
| 345 | // Called when the current profiling phase, identified by |profiling_phase|, |
| 346 | // ends. |
| 347 | // Must be called only on the snapshot thread. |
| 348 | void OnProfilingPhaseCompleted(int profiling_phase); |
| 349 | |
| 350 | private: |
| 351 | // Members are ordered from most regularly read and updated, to least |
| 352 | // frequently used. This might help a bit with cache lines. |
| 353 | // Number of runs seen (divisor for calculating averages). |
| 354 | // Can be incremented only on the death thread. |
| 355 | int count_; |
| 356 | |
| 357 | // Count used in determining probability of selecting exec/queue times from a |
| 358 | // recorded death as samples. |
| 359 | // Gets incremented only on the death thread, but can be set to 0 by |
| 360 | // OnProfilingPhaseCompleted() on the snapshot thread. |
| 361 | int sample_probability_count_; |
| 362 | |
| 363 | // Basic tallies, used to compute averages. Can be incremented only on the |
| 364 | // death thread. |
| 365 | int32 run_duration_sum_; |
| 366 | int32 queue_duration_sum_; |
| 367 | // 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, |
| 369 | // but these fields can be set to 0 by OnProfilingPhaseCompleted() on the |
| 370 | // snapshot thread. |
| 371 | int32 run_duration_max_; |
| 372 | int32 queue_duration_max_; |
| 373 | // Samples, used by crowd sourcing gatherers. These are almost never read, |
| 374 | // and rarely updated. They can be modified only on the death thread. |
| 375 | int32 run_duration_sample_; |
| 376 | int32 queue_duration_sample_; |
| 377 | |
| 378 | // Snapshot of this death data made at the last profiling phase completion, if |
| 379 | // any. DeathData owns the whole list starting with this pointer. |
| 380 | // Can be accessed only on the snapshot thread. |
| 381 | const DeathDataPhaseSnapshot* last_phase_snapshot_; |
| 382 | |
| 383 | DISALLOW_ASSIGN(DeathData); |
| 384 | }; |
| 385 | |
| 386 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 387 | // A temporary collection of data that can be sorted and summarized. It is |
| 388 | // gathered (carefully) from many threads. Instances are held in arrays and |
| 389 | // processed, filtered, and rendered. |
| 390 | // The source of this data was collected on many threads, and is asynchronously |
| 391 | // changing. The data in this instance is not asynchronously changing. |
| 392 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 393 | struct BASE_EXPORT TaskSnapshot { |
| 394 | TaskSnapshot(); |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 395 | TaskSnapshot(const BirthOnThreadSnapshot& birth, |
| 396 | const DeathDataSnapshot& death_data, |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 397 | const std::string& death_thread_name); |
| 398 | ~TaskSnapshot(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 399 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 400 | BirthOnThreadSnapshot birth; |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 401 | // Delta between death data for a thread for a certain profiling phase and the |
| 402 | // snapshot for the pervious phase, if any. Otherwise, just a snapshot. |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 403 | DeathDataSnapshot death_data; |
| 404 | std::string death_thread_name; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 405 | }; |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 406 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 407 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 408 | // For each thread, we have a ThreadData that stores all tracking info generated |
| 409 | // on this thread. This prevents the need for locking as data accumulates. |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 410 | // We use ThreadLocalStorage to quickly identfy the current ThreadData context. |
| 411 | // We also have a linked list of ThreadData instances, and that list is used to |
| 412 | // harvest data from all existing instances. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 413 | |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 414 | struct ProcessDataPhaseSnapshot; |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 415 | struct ProcessDataSnapshot; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 416 | class BASE_EXPORT TaskStopwatch; |
| 417 | |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 418 | // Map from profiling phase number to the process-wide snapshotted |
| 419 | // representation of the list of ThreadData objects that died during the given |
| 420 | // phase. |
| 421 | typedef std::map<int, ProcessDataPhaseSnapshot> PhasedProcessDataSnapshotMap; |
| 422 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 423 | class BASE_EXPORT ThreadData { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 424 | public: |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 425 | // Current allowable states of the tracking system. The states can vary |
| 426 | // between ACTIVE and DEACTIVATED, but can never go back to UNINITIALIZED. |
| 427 | enum Status { |
vadimt | cf8983e | 2015-05-01 19:13:01 | [diff] [blame] | 428 | UNINITIALIZED, // Pristine, link-time state before running. |
| 429 | DORMANT_DURING_TESTS, // Only used during testing. |
| 430 | DEACTIVATED, // No longer recording profiling. |
| 431 | PROFILING_ACTIVE, // Recording profiles. |
| 432 | STATUS_LAST = PROFILING_ACTIVE |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 433 | }; |
| 434 | |
sque | e3b16f1 | 2015-03-14 02:39:03 | [diff] [blame] | 435 | typedef base::hash_map<Location, Births*, Location::Hash> BirthMap; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 436 | typedef std::map<const Births*, DeathData> DeathMap; |
| 437 | |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 438 | // Initialize the current thread context with a new instance of ThreadData. |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 439 | // This is used by all threads that have names, and should be explicitly |
| 440 | // set *before* any births on the threads have taken place. It is generally |
| 441 | // only used by the message loop, which has a well defined thread name. |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 442 | static void InitializeThreadContext(const std::string& suggested_name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 443 | |
| 444 | // Using Thread Local Store, find the current instance for collecting data. |
| 445 | // If an instance does not exist, construct one (and remember it for use on |
| 446 | // this thread. |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 447 | // This may return NULL if the system is disabled for any reason. |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 448 | static ThreadData* Get(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 449 | |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 450 | // Fills |process_data_snapshot| with phased snapshots of all profiling |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 451 | // phases, including the current one, identified by |current_profiling_phase|. |
| 452 | // |current_profiling_phase| is necessary because a child process can start |
| 453 | // after several phase-changing events, so it needs to receive the current |
| 454 | // phase number from the browser process to fill the correct entry for the |
| 455 | // current phase in the |process_data_snapshot| map. |
| 456 | static void Snapshot(int current_profiling_phase, |
| 457 | ProcessDataSnapshot* process_data_snapshot); |
| 458 | |
| 459 | // Called when the current profiling phase, identified by |profiling_phase|, |
| 460 | // ends. |
| 461 | // |profiling_phase| is necessary because a child process can start after |
| 462 | // several phase-changing events, so it needs to receive the phase number from |
| 463 | // the browser process to fill the correct entry in the |
| 464 | // completed_phases_snapshots_ map. |
| 465 | static void OnProfilingPhaseCompleted(int profiling_phase); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 466 | |
| 467 | // Finds (or creates) a place to count births from the given location in this |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 468 | // thread, and increment that tally. |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 469 | // TallyABirthIfActive will returns NULL if the birth cannot be tallied. |
| 470 | static Births* TallyABirthIfActive(const Location& location); |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 471 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 472 | // Records the end of a timed run of an object. The |completed_task| contains |
| 473 | // a pointer to a Births, the time_posted, and a delayed_start_time if any. |
| 474 | // The |start_of_run| indicates when we started to perform the run of the |
| 475 | // task. The delayed_start_time is non-null for tasks that were posted as |
| 476 | // delayed tasks, and it indicates when the task should have run (i.e., when |
| 477 | // it should have posted out of the timer queue, and into the work queue. |
| 478 | // The |end_of_run| was just obtained by a call to Now() (just after the task |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 479 | // finished). It is provided as an argument to help with testing. |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 480 | static void TallyRunOnNamedThreadIfTracking( |
| 481 | const base::TrackingInfo& completed_task, |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 482 | const TaskStopwatch& stopwatch); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 483 | |
[email protected] | 6b26b9601 | 2011-10-28 21:41:50 | [diff] [blame] | 484 | // Record the end of a timed run of an object. The |birth| is the record for |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 485 | // the instance, the |time_posted| records that instant, which is presumed to |
| 486 | // be when the task was posted into a queue to run on a worker thread. |
| 487 | // The |start_of_run| is when the worker thread started to perform the run of |
| 488 | // the task. |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 489 | // The |end_of_run| was just obtained by a call to Now() (just after the task |
| 490 | // finished). |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 491 | static void TallyRunOnWorkerThreadIfTracking(const Births* births, |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 492 | const TrackedTime& time_posted, |
| 493 | const TaskStopwatch& stopwatch); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 494 | |
[email protected] | dbe5d207 | 2011-11-08 17:09:21 | [diff] [blame] | 495 | // Record the end of execution in region, generally corresponding to a scope |
| 496 | // being exited. |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 497 | static void TallyRunInAScopedRegionIfTracking(const Births* births, |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 498 | const TaskStopwatch& stopwatch); |
[email protected] | dbe5d207 | 2011-11-08 17:09:21 | [diff] [blame] | 499 | |
[email protected] | f42482e | 2012-11-05 06:25:55 | [diff] [blame] | 500 | const std::string& thread_name() const { return thread_name_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 501 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 502 | // Initializes all statics if needed (this initialization call should be made |
asvitkine | d0abaa3 | 2015-05-07 16:27:17 | [diff] [blame] | 503 | // while we are single threaded). |
| 504 | static void Initialize(); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 505 | |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 506 | // Sets internal status_. |
| 507 | // If |status| is false, then status_ is set to DEACTIVATED. |
vadimt | cf8983e | 2015-05-01 19:13:01 | [diff] [blame] | 508 | // If |status| is true, then status_ is set to PROFILING_ACTIVE. |
asvitkine | d0abaa3 | 2015-05-07 16:27:17 | [diff] [blame] | 509 | static void InitializeAndSetTrackingStatus(Status status); |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 510 | |
| 511 | static Status status(); |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 512 | |
| 513 | // Indicate if any sort of profiling is being done (i.e., we are more than |
| 514 | // DEACTIVATED). |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 515 | static bool TrackingStatus(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 516 | |
vadimt | a156831 | 2014-11-06 22:27:43 | [diff] [blame] | 517 | // Enables profiler timing. |
| 518 | static void EnableProfilerTiming(); |
| 519 | |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 520 | // Provide a time function that does nothing (runs fast) when we don't have |
| 521 | // the profiler enabled. It will generally be optimized away when it is |
| 522 | // ifdef'ed to be small enough (allowing the profiler to be "compiled out" of |
| 523 | // the code). |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 524 | static TrackedTime Now(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 525 | |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 526 | // Use the function |now| to provide current times, instead of calling the |
| 527 | // TrackedTime::Now() function. Since this alternate function is being used, |
| 528 | // the other time arguments (used for calculating queueing delay) will be |
| 529 | // ignored. |
| 530 | static void SetAlternateTimeSource(NowFunction* now); |
| 531 | |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 532 | // This function can be called at process termination to validate that thread |
| 533 | // cleanup routines have been called for at least some number of named |
| 534 | // threads. |
| 535 | static void EnsureCleanupWasCalled(int major_threads_shutdown_count); |
| 536 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 537 | private: |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 538 | friend class TaskStopwatch; |
[email protected] | eab79c38 | 2011-11-06 19:14:48 | [diff] [blame] | 539 | // Allow only tests to call ShutdownSingleThreadedCleanup. We NEVER call it |
| 540 | // in production code. |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 541 | // TODO(jar): Make this a friend in DEBUG only, so that the optimizer has a |
| 542 | // better change of optimizing (inlining? etc.) private methods (knowing that |
| 543 | // there will be no need for an external entry point). |
[email protected] | eab79c38 | 2011-11-06 19:14:48 | [diff] [blame] | 544 | friend class TrackedObjectsTest; |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 545 | FRIEND_TEST_ALL_PREFIXES(TrackedObjectsTest, MinimalStartupShutdown); |
| 546 | FRIEND_TEST_ALL_PREFIXES(TrackedObjectsTest, TinyStartupShutdown); |
[email protected] | eab79c38 | 2011-11-06 19:14:48 | [diff] [blame] | 547 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 548 | typedef std::map<const BirthOnThread*, int> BirthCountMap; |
| 549 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 550 | typedef std::vector<std::pair<const Births*, DeathDataPhaseSnapshot>> |
| 551 | DeathsSnapshot; |
| 552 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 553 | // Worker thread construction creates a name since there is none. |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 554 | explicit ThreadData(int thread_number); |
[email protected] | 445029fb | 2011-11-18 17:03:33 | [diff] [blame] | 555 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 556 | // Message loop based construction should provide a name. |
| 557 | explicit ThreadData(const std::string& suggested_name); |
| 558 | |
| 559 | ~ThreadData(); |
| 560 | |
| 561 | // Push this instance to the head of all_thread_data_list_head_, linking it to |
| 562 | // the previous head. This is performed after each construction, and leaves |
| 563 | // the instance permanently on that list. |
| 564 | void PushToHeadOfList(); |
| 565 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 566 | // (Thread safe) Get start of list of all ThreadData instances using the lock. |
| 567 | static ThreadData* first(); |
| 568 | |
| 569 | // Iterate through the null terminated list of ThreadData instances. |
| 570 | ThreadData* next() const; |
| 571 | |
| 572 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 573 | // In this thread's data, record a new birth. |
| 574 | Births* TallyABirth(const Location& location); |
| 575 | |
| 576 | // Find a place to record a death on this thread. |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 577 | void TallyADeath(const Births& births, |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 578 | int32 queue_duration, |
| 579 | const TaskStopwatch& stopwatch); |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 580 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 581 | // Snapshots (under a lock) the profiled data for the tasks for this thread |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 582 | // 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 |
| 584 | // 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 |
| 586 | // 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. |
| 588 | void SnapshotExecutedTasks(int current_profiling_phase, |
| 589 | PhasedProcessDataSnapshotMap* phased_snapshots, |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 590 | BirthCountMap* birth_counts); |
| 591 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 592 | // Using our lock, make a copy of the specified maps. This call may be made |
| 593 | // on non-local threads, which necessitate the use of the lock to prevent |
vadimt | a01ec209 | 2015-03-06 16:26:55 | [diff] [blame] | 594 | // the map(s) from being reallocated while they are copied. |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 595 | void SnapshotMaps(int profiling_phase, |
| 596 | BirthMap* birth_map, |
vadimt | cf8983e | 2015-05-01 19:13:01 | [diff] [blame] | 597 | DeathsSnapshot* deaths); |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 598 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 599 | // Called for this thread when the current profiling phase, identified by |
| 600 | // |profiling_phase|, ends. |
| 601 | void OnProfilingPhaseCompletedOnThread(int profiling_phase); |
| 602 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 603 | // This method is called by the TLS system when a thread terminates. |
| 604 | // The argument may be NULL if this thread has never tracked a birth or death. |
| 605 | static void OnThreadTermination(void* thread_data); |
| 606 | |
| 607 | // This method should be called when a worker thread terminates, so that we |
| 608 | // can save all the thread data into a cache of reusable ThreadData instances. |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 609 | void OnThreadTerminationCleanup(); |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 610 | |
[email protected] | eab79c38 | 2011-11-06 19:14:48 | [diff] [blame] | 611 | // Cleans up data structures, and returns statics to near pristine (mostly |
| 612 | // uninitialized) state. If there is any chance that other threads are still |
| 613 | // using the data structures, then the |leak| argument should be passed in as |
| 614 | // true, and the data structures (birth maps, death maps, ThreadData |
| 615 | // insntances, etc.) will be leaked and not deleted. If you have joined all |
| 616 | // threads since the time that InitializeAndSetTrackingStatus() was called, |
| 617 | // then you can pass in a |leak| value of false, and this function will |
| 618 | // delete recursively all data structures, starting with the list of |
| 619 | // ThreadData instances. |
| 620 | static void ShutdownSingleThreadedCleanup(bool leak); |
| 621 | |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 622 | // When non-null, this specifies an external function that supplies monotone |
| 623 | // increasing time functcion. |
| 624 | static NowFunction* now_function_; |
| 625 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 626 | // If true, now_function_ returns values that can be used to calculate queue |
| 627 | // time. |
| 628 | static bool now_function_is_time_; |
| 629 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 630 | // We use thread local store to identify which ThreadData to interact with. |
[email protected] | 444b8a3c | 2012-01-30 16:52:09 | [diff] [blame] | 631 | static base::ThreadLocalStorage::StaticSlot tls_index_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 632 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 633 | // List of ThreadData instances for use with worker threads. When a worker |
mithro | 5eb5850 | 2014-11-19 18:55:58 | [diff] [blame] | 634 | // thread is done (terminated), we push it onto this list. When a new worker |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 635 | // thread is created, we first try to re-use a ThreadData instance from the |
| 636 | // list, and if none are available, construct a new one. |
| 637 | // This is only accessed while list_lock_ is held. |
| 638 | static ThreadData* first_retired_worker_; |
| 639 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 640 | // Link to the most recently created instance (starts a null terminated list). |
[email protected] | dda9768 | 2011-11-14 05:24:07 | [diff] [blame] | 641 | // The list is traversed by about:profiler when it needs to snapshot data. |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 642 | // This is only accessed while list_lock_ is held. |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 643 | static ThreadData* all_thread_data_list_head_; |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 644 | |
| 645 | // The next available worker thread number. This should only be accessed when |
| 646 | // the list_lock_ is held. |
| 647 | static int worker_thread_data_creation_count_; |
| 648 | |
| 649 | // The number of times TLS has called us back to cleanup a ThreadData |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 650 | // instance. This is only accessed while list_lock_ is held. |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 651 | static int cleanup_count_; |
| 652 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 653 | // Incarnation sequence number, indicating how many times (during unittests) |
| 654 | // we've either transitioned out of UNINITIALIZED, or into that state. This |
| 655 | // value is only accessed while the list_lock_ is held. |
| 656 | static int incarnation_counter_; |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 657 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 658 | // Protection for access to all_thread_data_list_head_, and to |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 659 | // unregistered_thread_data_pool_. This lock is leaked at shutdown. |
[email protected] | 77169a6 | 2011-11-14 20:36:46 | [diff] [blame] | 660 | // The lock is very infrequently used, so we can afford to just make a lazy |
| 661 | // instance and be safe. |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 662 | static base::LazyInstance<base::Lock>::Leaky list_lock_; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 663 | |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 664 | // We set status_ to SHUTDOWN when we shut down the tracking service. |
amistry | 42d1688 | 2015-07-17 03:58:06 | [diff] [blame] | 665 | static base::subtle::Atomic32 status_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 666 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 667 | // Link to next instance (null terminated list). Used to globally track all |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 668 | // registered instances (corresponds to all registered threads where we keep |
| 669 | // data). |
| 670 | ThreadData* next_; |
| 671 | |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 672 | // Pointer to another ThreadData instance for a Worker-Thread that has been |
| 673 | // retired (its thread was terminated). This value is non-NULL only for a |
| 674 | // retired ThreadData associated with a Worker-Thread. |
| 675 | ThreadData* next_retired_worker_; |
| 676 | |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 677 | // The name of the thread that is being recorded. If this thread has no |
| 678 | // message_loop, then this is a worker thread, with a sequence number postfix. |
| 679 | std::string thread_name_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 680 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 681 | // Indicate if this is a worker thread, and the ThreadData contexts should be |
| 682 | // stored in the unregistered_thread_data_pool_ when not in use. |
[email protected] | 445029fb | 2011-11-18 17:03:33 | [diff] [blame] | 683 | // Value is zero when it is not a worker thread. Value is a positive integer |
| 684 | // corresponding to the created thread name if it is a worker thread. |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 685 | int worker_thread_number_; |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 686 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 687 | // A map used on each thread to keep track of Births on this thread. |
| 688 | // This map should only be accessed on the thread it was constructed on. |
| 689 | // When a snapshot is needed, this structure can be locked in place for the |
| 690 | // duration of the snapshotting activity. |
| 691 | BirthMap birth_map_; |
| 692 | |
| 693 | // Similar to birth_map_, this records informations about death of tracked |
| 694 | // instances (i.e., when a tracked instance was destroyed on this thread). |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 695 | // It is locked before changing, and hence other threads may access it by |
| 696 | // locking before reading it. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 697 | DeathMap death_map_; |
| 698 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 699 | // Lock to protect *some* access to BirthMap and DeathMap. The maps are |
| 700 | // regularly read and written on this thread, but may only be read from other |
| 701 | // 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 |
| 703 | // don't need a lock, as there is no potential for a conflict since the |
| 704 | // writing is only done from this thread. |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 705 | mutable base::Lock map_lock_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 706 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 707 | // 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 |
| 709 | // much randomness (because we can't call RandInt() on all our threads), so |
| 710 | // we stir in more and more as we go. |
glider | f7812575 | 2014-11-12 21:05:33 | [diff] [blame] | 711 | uint32 random_number_; |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 712 | |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 713 | // 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 |
| 715 | // pool (this is only critical in tests which go through multiple |
| 716 | // incarnations). |
| 717 | int incarnation_count_for_pool_; |
| 718 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 719 | // Most recently started (i.e. most nested) stopwatch on the current thread, |
| 720 | // if it exists; NULL otherwise. |
| 721 | TaskStopwatch* current_stopwatch_; |
| 722 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 723 | DISALLOW_COPY_AND_ASSIGN(ThreadData); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 724 | }; |
| 725 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 726 | //------------------------------------------------------------------------------ |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 727 | // Stopwatch to measure task run time or simply create a time interval that will |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 728 | // be subtracted from the current most nested task's run time. Stopwatches |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 729 | // coordinate with the stopwatches in which they are nested to avoid |
| 730 | // double-counting nested tasks run times. |
| 731 | |
| 732 | class BASE_EXPORT TaskStopwatch { |
| 733 | public: |
| 734 | // Starts the stopwatch. |
| 735 | TaskStopwatch(); |
| 736 | ~TaskStopwatch(); |
| 737 | |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 738 | // Starts stopwatch. |
| 739 | void Start(); |
| 740 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 741 | // Stops stopwatch. |
| 742 | void Stop(); |
| 743 | |
| 744 | // Returns the start time. |
| 745 | TrackedTime StartTime() const; |
| 746 | |
| 747 | // Task's duration is calculated as the wallclock duration between starting |
| 748 | // and stopping this stopwatch, minus the wallclock durations of any other |
| 749 | // instances that are immediately nested in this one, started and stopped on |
| 750 | // this thread during that period. |
| 751 | int32 RunDurationMs() const; |
| 752 | |
| 753 | // Returns tracking info for the current thread. |
| 754 | ThreadData* GetThreadData() const; |
| 755 | |
| 756 | private: |
| 757 | // Time when the stopwatch was started. |
| 758 | TrackedTime start_time_; |
| 759 | |
| 760 | // Wallclock duration of the task. |
| 761 | int32 wallclock_duration_ms_; |
| 762 | |
| 763 | // Tracking info for the current thread. |
| 764 | ThreadData* current_thread_data_; |
| 765 | |
| 766 | // Sum of wallclock durations of all stopwatches that were directly nested in |
| 767 | // this one. |
| 768 | int32 excluded_duration_ms_; |
| 769 | |
| 770 | // Stopwatch which was running on our thread when this stopwatch was started. |
| 771 | // That preexisting stopwatch must be adjusted to the exclude the wallclock |
| 772 | // duration of this stopwatch. |
| 773 | TaskStopwatch* parent_; |
| 774 | |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 775 | #if DCHECK_IS_ON() |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 776 | // State of the stopwatch. Stopwatch is first constructed in a created state |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 777 | // state, then is optionally started/stopped, then destructed. |
| 778 | enum { CREATED, RUNNING, STOPPED } state_; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 779 | |
| 780 | // Currently running stopwatch that is directly nested in this one, if such |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 781 | // stopwatch exists. NULL otherwise. |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 782 | TaskStopwatch* child_; |
| 783 | #endif |
| 784 | }; |
| 785 | |
| 786 | //------------------------------------------------------------------------------ |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 787 | // A snapshotted representation of the list of ThreadData objects for a process, |
| 788 | // for a single profiling phase. |
| 789 | |
| 790 | struct BASE_EXPORT ProcessDataPhaseSnapshot { |
| 791 | public: |
| 792 | ProcessDataPhaseSnapshot(); |
| 793 | ~ProcessDataPhaseSnapshot(); |
| 794 | |
| 795 | std::vector<TaskSnapshot> tasks; |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 796 | }; |
| 797 | |
| 798 | //------------------------------------------------------------------------------ |
| 799 | // A snapshotted representation of the list of ThreadData objects for a process, |
| 800 | // for all profiling phases, including the current one. |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 801 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 802 | struct BASE_EXPORT ProcessDataSnapshot { |
| 803 | public: |
| 804 | ProcessDataSnapshot(); |
| 805 | ~ProcessDataSnapshot(); |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 806 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 807 | PhasedProcessDataSnapshotMap phased_snapshots; |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 808 | base::ProcessId process_id; |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 809 | }; |
| 810 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 811 | } // namespace tracked_objects |
| 812 | |
| 813 | #endif // BASE_TRACKED_OBJECTS_H_ |