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