[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 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_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 13 | #include "base/base_export.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame^] | 14 | #include "base/location.h" |
| 15 | #include "base/time.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
[email protected] | 1357c32 | 2010-12-30 22:18:56 | [diff] [blame] | 17 | #include "base/threading/thread_local_storage.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 19 | // TrackedObjects provides a database of stats about objects (generally Tasks) |
| 20 | // that are tracked. Tracking means their birth, death, duration, birth thread, |
| 21 | // death thread, and birth place are recorded. This data is carefully spread |
| 22 | // across a series of objects so that the counts and times can be rapidly |
| 23 | // updated without (usually) having to lock the data, and hence there is usually |
| 24 | // very little contention caused by the tracking. The data can be viewed via |
[email protected] | ea319e4 | 2010-11-08 21:47:24 | [diff] [blame] | 25 | // the about:tasks URL, with a variety of sorting and filtering choices. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 26 | // |
[email protected] | ea319e4 | 2010-11-08 21:47:24 | [diff] [blame] | 27 | // These classes serve as the basis of a profiler of sorts for the Tasks system. |
| 28 | // As a result, design decisions were made to maximize speed, by minimizing |
| 29 | // recurring allocation/deallocation, lock contention and data copying. In the |
| 30 | // "stable" state, which is reached relatively quickly, there is no separate |
| 31 | // marginal allocation cost associated with construction or destruction of |
| 32 | // tracked objects, no locks are generally employed, and probably the largest |
| 33 | // computational cost is associated with obtaining start and stop times for |
| 34 | // instances as they are created and destroyed. The introduction of worker |
| 35 | // threads had a slight impact on this approach, and required use of some locks |
| 36 | // when accessing data from the worker threads. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 37 | // |
| 38 | // The following describes the lifecycle of tracking an instance. |
| 39 | // |
| 40 | // First off, when the instance is created, the FROM_HERE macro is expanded |
| 41 | // to specify the birth place (file, line, function) where the instance was |
| 42 | // created. That data is used to create a transient Location instance |
| 43 | // encapsulating the above triple of information. The strings (like __FILE__) |
| 44 | // are passed around by reference, with the assumption that they are static, and |
| 45 | // will never go away. This ensures that the strings can be dealt with as atoms |
| 46 | // with great efficiency (i.e., copying of strings is never needed, and |
| 47 | // comparisons for equality can be based on pointer comparisons). |
| 48 | // |
| 49 | // Next, a Births instance is created for use ONLY on the thread where this |
| 50 | // instance was created. That Births instance records (in a base class |
| 51 | // BirthOnThread) references to the static data provided in a Location instance, |
| 52 | // as well as a pointer specifying the thread on which the birth takes place. |
| 53 | // Hence there is at most one Births instance for each Location on each thread. |
| 54 | // The derived Births class contains slots for recording statistics about all |
| 55 | // instances born at the same location. Statistics currently include only the |
| 56 | // count of instances constructed. |
| 57 | // Since the base class BirthOnThread contains only constant data, it can be |
| 58 | // freely accessed by any thread at any time (i.e., only the statistic needs to |
| 59 | // be handled carefully, and it is ONLY read or written by the birth thread). |
| 60 | // |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame^] | 61 | // For Tasks, having now either constructed or found the Births instance |
| 62 | // described above, a pointer to the Births instance is then recorded into the |
| 63 | // PendingTask structure in MessageLoop. This fact alone is very useful in |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 64 | // debugging, when there is a question of where an instance came from. In |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame^] | 65 | // addition, the birth time is also recorded and used to later evaluate the |
| 66 | // lifetime duration of the whole Task. As a result of the above embedding, we |
| 67 | // can find out a Task's location of birth, and thread of birth, without using |
| 68 | // any locks, as all that data is constant across the life of the process. |
| 69 | // |
| 70 | // This can also be done for any other object as well by calling |
| 71 | // TallyABirthIfActive() and TallyADeathIfActive() as appropriate. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 72 | // |
| 73 | // The amount of memory used in the above data structures depends on how many |
| 74 | // threads there are, and how many Locations of construction there are. |
| 75 | // Fortunately, we don't use memory that is the product of those two counts, but |
| 76 | // rather we only need one Births instance for each thread that constructs an |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame^] | 77 | // instance at a Location. In many cases, instances are only created on one |
| 78 | // thread, so the memory utilization is actually fairly restrained. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 79 | // |
| 80 | // Lastly, when an instance is deleted, the final tallies of statistics are |
| 81 | // carefully accumulated. That tallying wrties into slots (members) in a |
| 82 | // collection of DeathData instances. For each birth place Location that is |
| 83 | // destroyed on a thread, there is a DeathData instance to record the additional |
| 84 | // death count, as well as accumulate the lifetime duration of the instance as |
| 85 | // it is destroyed (dies). By maintaining a single place to aggregate this |
| 86 | // addition *only* for the given thread, we avoid the need to lock such |
| 87 | // DeathData instances. |
| 88 | // |
| 89 | // With the above lifecycle description complete, the major remaining detail is |
| 90 | // explaining how each thread maintains a list of DeathData instances, and of |
| 91 | // Births instances, and is able to avoid additional (redundant/unnecessary) |
| 92 | // allocations. |
| 93 | // |
| 94 | // Each thread maintains a list of data items specific to that thread in a |
| 95 | // ThreadData instance (for that specific thread only). The two critical items |
| 96 | // are lists of DeathData and Births instances. These lists are maintained in |
| 97 | // STL maps, which are indexed by Location. As noted earlier, we can compare |
| 98 | // locations very efficiently as we consider the underlying data (file, |
| 99 | // function, line) to be atoms, and hence pointer comparison is used rather than |
| 100 | // (slow) string comparisons. |
| 101 | // |
| 102 | // To provide a mechanism for iterating over all "known threads," which means |
| 103 | // threads that have recorded a birth or a death, we create a singly linked list |
| 104 | // of ThreadData instances. Each such instance maintains a pointer to the next |
| 105 | // one. A static member of ThreadData provides a pointer to the first_ item on |
| 106 | // this global list, and access to that first_ item requires the use of a lock_. |
| 107 | // When new ThreadData instances is added to the global list, it is pre-pended, |
| 108 | // which ensures that any prior acquisition of the list is valid (i.e., the |
| 109 | // holder can iterate over it without fear of it changing, or the necessity of |
| 110 | // using an additional lock. Iterations are actually pretty rare (used |
| 111 | // primarilly for cleanup, or snapshotting data for display), so this lock has |
| 112 | // very little global performance impact. |
| 113 | // |
| 114 | // The above description tries to define the high performance (run time) |
| 115 | // portions of these classes. After gathering statistics, calls instigated |
[email protected] | ea319e4 | 2010-11-08 21:47:24 | [diff] [blame] | 116 | // by visiting about:tasks will assemble and aggregate data for display. The |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 117 | // following data structures are used for producing such displays. They are |
| 118 | // not performance critical, and their only major constraint is that they should |
| 119 | // be able to run concurrently with ongoing augmentation of the birth and death |
| 120 | // data. |
| 121 | // |
| 122 | // For a given birth location, information about births are spread across data |
| 123 | // structures that are asynchronously changing on various threads. For display |
| 124 | // purposes, we need to construct Snapshot instances for each combination of |
| 125 | // birth thread, death thread, and location, along with the count of such |
| 126 | // lifetimes. We gather such data into a Snapshot instances, so that such |
| 127 | // instances can be sorted and aggregated (and remain frozen during our |
| 128 | // processing). Snapshot instances use pointers to constant portions of the |
| 129 | // birth and death datastructures, but have local (frozen) copies of the actual |
| 130 | // statistics (birth count, durations, etc. etc.). |
| 131 | // |
| 132 | // A DataCollector is a container object that holds a set of Snapshots. A |
| 133 | // DataCollector can be passed from thread to thread, and each thread |
| 134 | // contributes to it by adding or updating Snapshot instances. DataCollector |
| 135 | // instances are thread safe containers which are passed to various threads to |
| 136 | // accumulate all Snapshot instances. |
| 137 | // |
| 138 | // After an array of Snapshots instances are colleted into a DataCollector, they |
| 139 | // need to be sorted, and possibly aggregated (example: how many threads are in |
| 140 | // a specific consecutive set of Snapshots? What was the total birth count for |
| 141 | // that set? etc.). Aggregation instances collect running sums of any set of |
[email protected] | ea319e4 | 2010-11-08 21:47:24 | [diff] [blame] | 142 | // snapshot instances, and are used to print sub-totals in an about:tasks page. |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 143 | // |
| 144 | // TODO(jar): I need to store DataCollections, and provide facilities for taking |
| 145 | // the difference between two gathered DataCollections. For now, I'm just |
| 146 | // adding a hack that Reset()'s to zero all counts and stats. This is also |
| 147 | // done in a slighly thread-unsafe fashion, as the reseting is done |
| 148 | // asynchronously relative to ongoing updates, and worse yet, some data fields |
| 149 | // are 64bit quantities, and are not atomicly accessed (reset or incremented |
| 150 | // etc.). For basic profiling, this will work "most of the time," and should be |
| 151 | // sufficient... but storing away DataCollections is the "right way" to do this. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame^] | 153 | class MessageLoop; |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 154 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 155 | namespace tracked_objects { |
| 156 | |
| 157 | //------------------------------------------------------------------------------ |
| 158 | // For a specific thread, and a specific birth place, the collection of all |
| 159 | // death info (with tallies for each death thread, to prevent access conflicts). |
| 160 | class ThreadData; |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 161 | class BASE_EXPORT BirthOnThread { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | public: |
| 163 | explicit BirthOnThread(const Location& location); |
| 164 | |
| 165 | const Location location() const { return location_; } |
| 166 | const ThreadData* birth_thread() const { return birth_thread_; } |
| 167 | |
| 168 | private: |
| 169 | // File/lineno of birth. This defines the essence of the type, as the context |
| 170 | // of the birth (construction) often tell what the item is for. This field |
| 171 | // is const, and hence safe to access from any thread. |
| 172 | const Location location_; |
| 173 | |
| 174 | // The thread that records births into this object. Only this thread is |
| 175 | // allowed to access birth_count_ (which changes over time). |
| 176 | const ThreadData* birth_thread_; // The thread this birth took place on. |
| 177 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 178 | DISALLOW_COPY_AND_ASSIGN(BirthOnThread); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | //------------------------------------------------------------------------------ |
| 182 | // A class for accumulating counts of births (without bothering with a map<>). |
| 183 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 184 | class BASE_EXPORT Births: public BirthOnThread { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 185 | public: |
| 186 | explicit Births(const Location& location); |
| 187 | |
| 188 | int birth_count() const { return birth_count_; } |
| 189 | |
| 190 | // When we have a birth we update the count for this BirhPLace. |
| 191 | void RecordBirth() { ++birth_count_; } |
| 192 | |
| 193 | // When a birthplace is changed (updated), we need to decrement the counter |
| 194 | // for the old instance. |
| 195 | void ForgetBirth() { --birth_count_; } // We corrected a birth place. |
| 196 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 197 | // Hack to quickly reset all counts to zero. |
| 198 | void Clear() { birth_count_ = 0; } |
| 199 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 200 | private: |
| 201 | // The number of births on this thread for our location_. |
| 202 | int birth_count_; |
| 203 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 204 | DISALLOW_COPY_AND_ASSIGN(Births); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | //------------------------------------------------------------------------------ |
| 208 | // Basic info summarizing multiple destructions of an object with a single |
| 209 | // birthplace (fixed Location). Used both on specific threads, and also used |
| 210 | // in snapshots when integrating assembled data. |
| 211 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 212 | class BASE_EXPORT DeathData { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | public: |
| 214 | // Default initializer. |
| 215 | DeathData() : count_(0), square_duration_(0) {} |
| 216 | |
| 217 | // When deaths have not yet taken place, and we gather data from all the |
| 218 | // threads, we create DeathData stats that tally the number of births without |
| 219 | // a corrosponding death. |
| 220 | explicit DeathData(int count) : count_(count), square_duration_(0) {} |
| 221 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 222 | void RecordDeath(const base::TimeDelta& duration); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 223 | |
| 224 | // Metrics accessors. |
| 225 | int count() const { return count_; } |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 226 | base::TimeDelta life_duration() const { return life_duration_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 227 | int64 square_duration() const { return square_duration_; } |
| 228 | int AverageMsDuration() const; |
| 229 | double StandardDeviation() const; |
| 230 | |
| 231 | // Accumulate metrics from other into this. |
| 232 | void AddDeathData(const DeathData& other); |
| 233 | |
| 234 | // Simple print of internal state. |
| 235 | void Write(std::string* output) const; |
| 236 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 237 | // Reset all tallies to zero. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 238 | void Clear(); |
| 239 | |
| 240 | private: |
| 241 | int count_; // Number of destructions. |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 242 | base::TimeDelta life_duration_; // Sum of all lifetime durations. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 243 | int64 square_duration_; // Sum of squares in milliseconds. |
| 244 | }; |
| 245 | |
| 246 | //------------------------------------------------------------------------------ |
| 247 | // A temporary collection of data that can be sorted and summarized. It is |
| 248 | // gathered (carefully) from many threads. Instances are held in arrays and |
| 249 | // processed, filtered, and rendered. |
| 250 | // The source of this data was collected on many threads, and is asynchronously |
| 251 | // changing. The data in this instance is not asynchronously changing. |
| 252 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 253 | class BASE_EXPORT Snapshot { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 254 | public: |
| 255 | // When snapshotting a full life cycle set (birth-to-death), use this: |
| 256 | Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, |
| 257 | const DeathData& death_data); |
| 258 | |
| 259 | // When snapshotting a birth, with no death yet, use this: |
| 260 | Snapshot(const BirthOnThread& birth_on_thread, int count); |
| 261 | |
| 262 | |
| 263 | const ThreadData* birth_thread() const { return birth_->birth_thread(); } |
| 264 | const Location location() const { return birth_->location(); } |
| 265 | const BirthOnThread& birth() const { return *birth_; } |
| 266 | const ThreadData* death_thread() const {return death_thread_; } |
| 267 | const DeathData& death_data() const { return death_data_; } |
| 268 | const std::string DeathThreadName() const; |
| 269 | |
| 270 | int count() const { return death_data_.count(); } |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 271 | base::TimeDelta life_duration() const { return death_data_.life_duration(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 272 | int64 square_duration() const { return death_data_.square_duration(); } |
| 273 | int AverageMsDuration() const { return death_data_.AverageMsDuration(); } |
| 274 | |
[email protected] | f0d930a | 2008-08-13 19:38:25 | [diff] [blame] | 275 | void Write(std::string* output) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 276 | |
| 277 | void Add(const Snapshot& other); |
| 278 | |
| 279 | private: |
| 280 | const BirthOnThread* birth_; // Includes Location and birth_thread. |
| 281 | const ThreadData* death_thread_; |
| 282 | DeathData death_data_; |
| 283 | }; |
| 284 | //------------------------------------------------------------------------------ |
| 285 | // DataCollector is a container class for Snapshot and BirthOnThread count |
| 286 | // items. It protects the gathering under locks, so that it could be called via |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 287 | // Posttask on any threads, or passed to all the target threads in parallel. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 288 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 289 | class BASE_EXPORT DataCollector { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 290 | public: |
[email protected] | 764be58b | 2008-08-08 20:03:42 | [diff] [blame] | 291 | typedef std::vector<Snapshot> Collection; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 292 | |
| 293 | // Construct with a list of how many threads should contribute. This helps us |
| 294 | // determine (in the async case) when we are done with all contributions. |
| 295 | DataCollector(); |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 296 | ~DataCollector(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 297 | |
| 298 | // Add all stats from the indicated thread into our arrays. This function is |
| 299 | // mutex protected, and *could* be called from any threads (although current |
| 300 | // implementation serialized calls to Append). |
| 301 | void Append(const ThreadData& thread_data); |
| 302 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 303 | // After the accumulation phase, the following accessor is used to process the |
| 304 | // data. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 305 | Collection* collection(); |
| 306 | |
| 307 | // After collection of death data is complete, we can add entries for all the |
| 308 | // remaining living objects. |
| 309 | void AddListOfLivingObjects(); |
| 310 | |
| 311 | private: |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 312 | typedef std::map<const BirthOnThread*, int> BirthCount; |
| 313 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 314 | // This instance may be provided to several threads to contribute data. The |
| 315 | // following counter tracks how many more threads will contribute. When it is |
| 316 | // zero, then all asynchronous contributions are complete, and locked access |
| 317 | // is no longer needed. |
| 318 | int count_of_contributing_threads_; |
| 319 | |
| 320 | // The array that we collect data into. |
| 321 | Collection collection_; |
| 322 | |
| 323 | // The total number of births recorded at each location for which we have not |
| 324 | // seen a death count. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 325 | BirthCount global_birth_count_; |
| 326 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 327 | base::Lock accumulation_lock_; // Protects access during accumulation phase. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 328 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 329 | DISALLOW_COPY_AND_ASSIGN(DataCollector); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | //------------------------------------------------------------------------------ |
| 333 | // Aggregation contains summaries (totals and subtotals) of groups of Snapshot |
| 334 | // instances to provide printing of these collections on a single line. |
| 335 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 336 | class BASE_EXPORT Aggregation: public DeathData { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 337 | public: |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 338 | Aggregation(); |
| 339 | ~Aggregation(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 340 | |
| 341 | void AddDeathSnapshot(const Snapshot& snapshot); |
| 342 | void AddBirths(const Births& births); |
| 343 | void AddBirth(const BirthOnThread& birth); |
| 344 | void AddBirthPlace(const Location& location); |
| 345 | void Write(std::string* output) const; |
| 346 | void Clear(); |
| 347 | |
| 348 | private: |
| 349 | int birth_count_; |
| 350 | std::map<std::string, int> birth_files_; |
| 351 | std::map<Location, int> locations_; |
| 352 | std::map<const ThreadData*, int> birth_threads_; |
| 353 | DeathData death_data_; |
| 354 | std::map<const ThreadData*, int> death_threads_; |
| 355 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 356 | DISALLOW_COPY_AND_ASSIGN(Aggregation); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | //------------------------------------------------------------------------------ |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 360 | // Comparator is a class that supports the comparison of Snapshot instances. |
| 361 | // An instance is actually a list of chained Comparitors, that can provide for |
[email protected] | 9958a32 | 2011-03-08 20:04:17 | [diff] [blame] | 362 | // arbitrary ordering. The path portion of an about:tasks URL is translated |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 363 | // into such a chain, which is then used to order Snapshot instances in a |
| 364 | // vector. It orders them into groups (for aggregation), and can also order |
| 365 | // instances within the groups (for detailed rendering of the instances in an |
| 366 | // aggregation). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 367 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 368 | class BASE_EXPORT Comparator { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 369 | public: |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 370 | // Selector enum is the token identifier for each parsed keyword, most of |
| 371 | // which specify a sort order. |
| 372 | // Since it is not meaningful to sort more than once on a specific key, we |
| 373 | // use bitfields to accumulate what we have sorted on so far. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 374 | enum Selector { |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 375 | // Sort orders. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 376 | NIL = 0, |
| 377 | BIRTH_THREAD = 1, |
| 378 | DEATH_THREAD = 2, |
| 379 | BIRTH_FILE = 4, |
| 380 | BIRTH_FUNCTION = 8, |
| 381 | BIRTH_LINE = 16, |
| 382 | COUNT = 32, |
| 383 | AVERAGE_DURATION = 64, |
| 384 | TOTAL_DURATION = 128, |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 385 | |
| 386 | // Imediate action keywords. |
| 387 | RESET_ALL_DATA = -1, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 388 | }; |
| 389 | |
| 390 | explicit Comparator(); |
| 391 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 392 | // Reset the comparator to a NIL selector. Clear() and recursively delete any |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 393 | // tiebreaker_ entries. NOTE: We can't use a standard destructor, because |
| 394 | // the sort algorithm makes copies of this object, and then deletes them, |
| 395 | // which would cause problems (either we'd make expensive deep copies, or we'd |
| 396 | // do more thna one delete on a tiebreaker_. |
| 397 | void Clear(); |
| 398 | |
| 399 | // The less() operator for sorting the array via std::sort(). |
| 400 | bool operator()(const Snapshot& left, const Snapshot& right) const; |
| 401 | |
| 402 | void Sort(DataCollector::Collection* collection) const; |
| 403 | |
| 404 | // Check to see if the items are sort equivalents (should be aggregated). |
| 405 | bool Equivalent(const Snapshot& left, const Snapshot& right) const; |
| 406 | |
| 407 | // Check to see if all required fields are present in the given sample. |
| 408 | bool Acceptable(const Snapshot& sample) const; |
| 409 | |
| 410 | // A comparator can be refined by specifying what to do if the selected basis |
| 411 | // for comparison is insufficient to establish an ordering. This call adds |
| 412 | // the indicated attribute as the new "least significant" basis of comparison. |
[email protected] | 2bce035 | 2009-07-06 20:11:00 | [diff] [blame] | 413 | void SetTiebreaker(Selector selector, const std::string& required); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 414 | |
| 415 | // Indicate if this instance is set up to sort by the given Selector, thereby |
| 416 | // putting that information in the SortGrouping, so it is not needed in each |
| 417 | // printed line. |
| 418 | bool IsGroupedBy(Selector selector) const; |
| 419 | |
| 420 | // Using the tiebreakers as set above, we mostly get an ordering, which |
| 421 | // equivalent groups. If those groups are displayed (rather than just being |
| 422 | // aggregated, then the following is used to order them (within the group). |
| 423 | void SetSubgroupTiebreaker(Selector selector); |
| 424 | |
| 425 | // Translate a keyword and restriction in URL path to a selector for sorting. |
[email protected] | 2bce035 | 2009-07-06 20:11:00 | [diff] [blame] | 426 | void ParseKeyphrase(const std::string& key_phrase); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 427 | |
[email protected] | be843e2 | 2011-06-28 17:35:18 | [diff] [blame] | 428 | // Parse a query to decide on sort ordering. |
[email protected] | 2bce035 | 2009-07-06 20:11:00 | [diff] [blame] | 429 | bool ParseQuery(const std::string& query); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 430 | |
| 431 | // Output a header line that can be used to indicated what items will be |
| 432 | // collected in the group. It lists all (potentially) tested attributes and |
| 433 | // their values (in the sample item). |
| 434 | bool WriteSortGrouping(const Snapshot& sample, std::string* output) const; |
| 435 | |
| 436 | // Output a sample, with SortGroup details not displayed. |
| 437 | void WriteSnapshot(const Snapshot& sample, std::string* output) const; |
| 438 | |
| 439 | private: |
| 440 | // The selector directs this instance to compare based on the specified |
| 441 | // members of the tested elements. |
| 442 | enum Selector selector_; |
| 443 | |
| 444 | // For filtering into acceptable and unacceptable snapshot instance, the |
| 445 | // following is required to be a substring of the selector_ field. |
| 446 | std::string required_; |
| 447 | |
| 448 | // If this instance can't decide on an ordering, we can consult a tie-breaker |
| 449 | // which may have a different basis of comparison. |
| 450 | Comparator* tiebreaker_; |
| 451 | |
| 452 | // We or together all the selectors we sort on (not counting sub-group |
| 453 | // selectors), so that we can tell if we've decided to group on any given |
| 454 | // criteria. |
| 455 | int combined_selectors_; |
| 456 | |
| 457 | // Some tiebreakrs are for subgroup ordering, and not for basic ordering (in |
| 458 | // preparation for aggregation). The subgroup tiebreakers are not consulted |
| 459 | // when deciding if two items are in equivalent groups. This flag tells us |
| 460 | // to ignore the tiebreaker when doing Equivalent() testing. |
| 461 | bool use_tiebreaker_for_sort_only_; |
| 462 | }; |
| 463 | |
| 464 | |
| 465 | //------------------------------------------------------------------------------ |
| 466 | // For each thread, we have a ThreadData that stores all tracking info generated |
| 467 | // on this thread. This prevents the need for locking as data accumulates. |
| 468 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 469 | class BASE_EXPORT ThreadData { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 470 | public: |
| 471 | typedef std::map<Location, Births*> BirthMap; |
| 472 | typedef std::map<const Births*, DeathData> DeathMap; |
| 473 | |
| 474 | ThreadData(); |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 475 | ~ThreadData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 476 | |
| 477 | // Using Thread Local Store, find the current instance for collecting data. |
| 478 | // If an instance does not exist, construct one (and remember it for use on |
| 479 | // this thread. |
| 480 | // If shutdown has already started, and we don't yet have an instance, then |
| 481 | // return null. |
| 482 | static ThreadData* current(); |
| 483 | |
[email protected] | be843e2 | 2011-06-28 17:35:18 | [diff] [blame] | 484 | // For a given (unescaped) about:tasks query, develop resulting HTML, and |
| 485 | // append to output. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 486 | static void WriteHTML(const std::string& query, std::string* output); |
| 487 | |
| 488 | // For a given accumulated array of results, use the comparator to sort and |
| 489 | // subtotal, writing the results to the output. |
| 490 | static void WriteHTMLTotalAndSubtotals( |
| 491 | const DataCollector::Collection& match_array, |
| 492 | const Comparator& comparator, std::string* output); |
| 493 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 494 | // In this thread's data, record a new birth. |
| 495 | Births* TallyABirth(const Location& location); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 496 | |
| 497 | // Find a place to record a death on this thread. |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 498 | void TallyADeath(const Births& lifetimes, const base::TimeDelta& duration); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 499 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 500 | // Helper methods to only tally if the current thread has tracking active. |
| 501 | // |
| 502 | // TallyABirthIfActive will returns NULL if the birth cannot be tallied. |
| 503 | static Births* TallyABirthIfActive(const Location& location); |
| 504 | static void TallyADeathIfActive(const Births* lifetimes, |
| 505 | const base::TimeDelta& duration); |
| 506 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 507 | // (Thread safe) Get start of list of instances. |
| 508 | static ThreadData* first(); |
| 509 | // Iterate through the null terminated list of instances. |
| 510 | ThreadData* next() const { return next_; } |
| 511 | |
| 512 | MessageLoop* message_loop() const { return message_loop_; } |
| 513 | const std::string ThreadName() const; |
| 514 | |
| 515 | // Using our lock, make a copy of the specified maps. These calls may arrive |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 516 | // from non-local threads, and are used to quickly scan data from all threads |
[email protected] | 9958a32 | 2011-03-08 20:04:17 | [diff] [blame] | 517 | // in order to build an HTML page for about:tasks. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 518 | void SnapshotBirthMap(BirthMap *output) const; |
| 519 | void SnapshotDeathMap(DeathMap *output) const; |
| 520 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 521 | // Hack: asynchronously clear all birth counts and death tallies data values |
| 522 | // in all ThreadData instances. The numerical (zeroing) part is done without |
| 523 | // use of a locks or atomics exchanges, and may (for int64 values) produce |
| 524 | // bogus counts VERY rarely. |
| 525 | static void ResetAllThreadData(); |
| 526 | |
| 527 | // Using our lock to protect the iteration, Clear all birth and death data. |
| 528 | void Reset(); |
| 529 | |
| 530 | // Using the "known list of threads" gathered during births and deaths, the |
| 531 | // following attempts to run the given function once all all such threads. |
| 532 | // Note that the function can only be run on threads which have a message |
| 533 | // loop! |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 534 | static void RunOnAllThreads(void (*Func)()); |
| 535 | |
| 536 | // Set internal status_ to either become ACTIVE, or later, to be SHUTDOWN, |
| 537 | // based on argument being true or false respectively. |
| 538 | // IF tracking is not compiled in, this function will return false. |
| 539 | static bool StartTracking(bool status); |
| 540 | static bool IsActive(); |
| 541 | |
[email protected] | 764be58b | 2008-08-08 20:03:42 | [diff] [blame] | 542 | #ifdef OS_WIN |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 543 | // WARNING: ONLY call this function when all MessageLoops are still intact for |
| 544 | // all registered threads. IF you call it later, you will crash. |
| 545 | // Note: You don't need to call it at all, and you can wait till you are |
| 546 | // single threaded (again) to do the cleanup via |
| 547 | // ShutdownSingleThreadedCleanup(). |
| 548 | // Start the teardown (shutdown) process in a multi-thread mode by disabling |
| 549 | // further additions to thread database on all threads. First it makes a |
| 550 | // local (locked) change to prevent any more threads from registering. Then |
| 551 | // it Posts a Task to all registered threads to be sure they are aware that no |
| 552 | // more accumulation can take place. |
| 553 | static void ShutdownMultiThreadTracking(); |
[email protected] | 764be58b | 2008-08-08 20:03:42 | [diff] [blame] | 554 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 555 | |
| 556 | // WARNING: ONLY call this function when you are running single threaded |
| 557 | // (again) and all message loops and threads have terminated. Until that |
| 558 | // point some threads may still attempt to write into our data structures. |
| 559 | // Delete recursively all data structures, starting with the list of |
| 560 | // ThreadData instances. |
| 561 | static void ShutdownSingleThreadedCleanup(); |
| 562 | |
| 563 | private: |
| 564 | // Current allowable states of the tracking system. The states always |
| 565 | // proceed towards SHUTDOWN, and never go backwards. |
| 566 | enum Status { |
| 567 | UNINITIALIZED, |
| 568 | ACTIVE, |
| 569 | SHUTDOWN, |
| 570 | }; |
| 571 | |
[email protected] | e7af596 | 2010-08-05 22:36:04 | [diff] [blame] | 572 | #if defined(OS_WIN) |
| 573 | class ThreadSafeDownCounter; |
| 574 | class RunTheStatic; |
[email protected] | 764be58b | 2008-08-08 20:03:42 | [diff] [blame] | 575 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 576 | |
| 577 | // Each registered thread is called to set status_ to SHUTDOWN. |
| 578 | // This is done redundantly on every registered thread because it is not |
| 579 | // protected by a mutex. Running on all threads guarantees we get the |
| 580 | // notification into the memory cache of all possible threads. |
| 581 | static void ShutdownDisablingFurtherTracking(); |
| 582 | |
| 583 | // We use thread local store to identify which ThreadData to interact with. |
[email protected] | 1357c32 | 2010-12-30 22:18:56 | [diff] [blame] | 584 | static base::ThreadLocalStorage::Slot tls_index_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 585 | |
| 586 | // Link to the most recently created instance (starts a null terminated list). |
| 587 | static ThreadData* first_; |
| 588 | // Protection for access to first_. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 589 | static base::Lock list_lock_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 590 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 591 | // We set status_ to SHUTDOWN when we shut down the tracking service. This |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 592 | // setting is redundantly established by all participating threads so that we |
| 593 | // are *guaranteed* (without locking) that all threads can "see" the status |
| 594 | // and avoid additional calls into the service. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 595 | static Status status_; |
| 596 | |
| 597 | // Link to next instance (null terminated list). Used to globally track all |
| 598 | // registered instances (corresponds to all registered threads where we keep |
| 599 | // data). |
| 600 | ThreadData* next_; |
| 601 | |
| 602 | // The message loop where tasks needing to access this instance's private data |
| 603 | // should be directed. Since some threads have no message loop, some |
| 604 | // instances have data that can't be (safely) modified externally. |
| 605 | MessageLoop* message_loop_; |
| 606 | |
| 607 | // A map used on each thread to keep track of Births on this thread. |
| 608 | // This map should only be accessed on the thread it was constructed on. |
| 609 | // When a snapshot is needed, this structure can be locked in place for the |
| 610 | // duration of the snapshotting activity. |
| 611 | BirthMap birth_map_; |
| 612 | |
| 613 | // Similar to birth_map_, this records informations about death of tracked |
| 614 | // instances (i.e., when a tracked instance was destroyed on this thread). |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 615 | // It is locked before changing, and hence other threads may access it by |
| 616 | // locking before reading it. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 617 | DeathMap death_map_; |
| 618 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 619 | // Lock to protect *some* access to BirthMap and DeathMap. The maps are |
| 620 | // regularly read and written on this thread, but may only be read from other |
| 621 | // threads. To support this, we acquire this lock if we are writing from this |
| 622 | // thread, or reading from another thread. For reading from this thread we |
| 623 | // don't need a lock, as there is no potential for a conflict since the |
| 624 | // writing is only done from this thread. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 625 | mutable base::Lock lock_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 626 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 627 | DISALLOW_COPY_AND_ASSIGN(ThreadData); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 628 | }; |
| 629 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 630 | |
| 631 | //------------------------------------------------------------------------------ |
| 632 | // Provide simple way to to start global tracking, and to tear down tracking |
| 633 | // when done. Note that construction and destruction of this object must be |
[email protected] | 862aa2f0 | 2009-12-31 07:26:16 | [diff] [blame] | 634 | // done when running in threaded mode (before spawning a lot of threads |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 635 | // for construction, and after shutting down all the threads for destruction). |
| 636 | |
[email protected] | 862aa2f0 | 2009-12-31 07:26:16 | [diff] [blame] | 637 | // To prevent grabbing thread local store resources time and again if someone |
| 638 | // chooses to try to re-run the browser many times, we maintain global state and |
| 639 | // only allow the tracking system to be started up at most once, and shutdown |
| 640 | // at most once. See bug 31344 for an example. |
| 641 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 642 | class BASE_EXPORT AutoTracking { |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 643 | public: |
[email protected] | 862aa2f0 | 2009-12-31 07:26:16 | [diff] [blame] | 644 | AutoTracking() { |
| 645 | if (state_ != kNeverBeenRun) |
| 646 | return; |
| 647 | ThreadData::StartTracking(true); |
| 648 | state_ = kRunning; |
| 649 | } |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 650 | |
| 651 | ~AutoTracking() { |
[email protected] | 862aa2f0 | 2009-12-31 07:26:16 | [diff] [blame] | 652 | #ifndef NDEBUG |
| 653 | if (state_ != kRunning) |
| 654 | return; |
[email protected] | 23751182 | 2011-03-01 21:56:30 | [diff] [blame] | 655 | // We don't do cleanup of any sort in Release build because it is a |
| 656 | // complete waste of time. Since Chromium doesn't join all its thread and |
| 657 | // guarantee we're in a single threaded mode, we don't even do cleanup in |
| 658 | // debug mode, as it will generate race-checker warnings. |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 659 | #endif |
| 660 | } |
| 661 | |
| 662 | private: |
[email protected] | 862aa2f0 | 2009-12-31 07:26:16 | [diff] [blame] | 663 | enum State { |
| 664 | kNeverBeenRun, |
| 665 | kRunning, |
| 666 | kTornDownAndStopped, |
| 667 | }; |
| 668 | static State state_; |
| 669 | |
[email protected] | 022614ef9 | 2008-12-30 20:50:01 | [diff] [blame] | 670 | DISALLOW_COPY_AND_ASSIGN(AutoTracking); |
| 671 | }; |
| 672 | |
| 673 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 674 | } // namespace tracked_objects |
| 675 | |
| 676 | #endif // BASE_TRACKED_OBJECTS_H_ |