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