| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TRACKED_OBJECTS_H_ | 5 #ifndef BASE_TRACKED_OBJECTS_H_ |
| 6 #define BASE_TRACKED_OBJECTS_H_ | 6 #define BASE_TRACKED_OBJECTS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/base_api.h" |
| 13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 14 #include "base/tracked.h" | 15 #include "base/tracked.h" |
| 15 #include "base/threading/thread_local_storage.h" | 16 #include "base/threading/thread_local_storage.h" |
| 16 | 17 |
| 17 // TrackedObjects provides a database of stats about objects (generally Tasks) | 18 // TrackedObjects provides a database of stats about objects (generally Tasks) |
| 18 // that are tracked. Tracking means their birth, death, duration, birth thread, | 19 // that are tracked. Tracking means their birth, death, duration, birth thread, |
| 19 // death thread, and birth place are recorded. This data is carefully spread | 20 // death thread, and birth place are recorded. This data is carefully spread |
| 20 // across a series of objects so that the counts and times can be rapidly | 21 // across a series of objects so that the counts and times can be rapidly |
| 21 // updated without (usually) having to lock the data, and hence there is usually | 22 // updated without (usually) having to lock the data, and hence there is usually |
| 22 // very little contention caused by the tracking. The data can be viewed via | 23 // very little contention caused by the tracking. The data can be viewed via |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 // | 150 // |
| 150 class MessageLoop; | 151 class MessageLoop; |
| 151 | 152 |
| 152 | 153 |
| 153 namespace tracked_objects { | 154 namespace tracked_objects { |
| 154 | 155 |
| 155 //------------------------------------------------------------------------------ | 156 //------------------------------------------------------------------------------ |
| 156 // For a specific thread, and a specific birth place, the collection of all | 157 // For a specific thread, and a specific birth place, the collection of all |
| 157 // death info (with tallies for each death thread, to prevent access conflicts). | 158 // death info (with tallies for each death thread, to prevent access conflicts). |
| 158 class ThreadData; | 159 class ThreadData; |
| 159 class BirthOnThread { | 160 class BASE_API BirthOnThread { |
| 160 public: | 161 public: |
| 161 explicit BirthOnThread(const Location& location); | 162 explicit BirthOnThread(const Location& location); |
| 162 | 163 |
| 163 const Location location() const { return location_; } | 164 const Location location() const { return location_; } |
| 164 const ThreadData* birth_thread() const { return birth_thread_; } | 165 const ThreadData* birth_thread() const { return birth_thread_; } |
| 165 | 166 |
| 166 private: | 167 private: |
| 167 // File/lineno of birth. This defines the essence of the type, as the context | 168 // File/lineno of birth. This defines the essence of the type, as the context |
| 168 // of the birth (construction) often tell what the item is for. This field | 169 // of the birth (construction) often tell what the item is for. This field |
| 169 // is const, and hence safe to access from any thread. | 170 // is const, and hence safe to access from any thread. |
| 170 const Location location_; | 171 const Location location_; |
| 171 | 172 |
| 172 // The thread that records births into this object. Only this thread is | 173 // The thread that records births into this object. Only this thread is |
| 173 // allowed to access birth_count_ (which changes over time). | 174 // allowed to access birth_count_ (which changes over time). |
| 174 const ThreadData* birth_thread_; // The thread this birth took place on. | 175 const ThreadData* birth_thread_; // The thread this birth took place on. |
| 175 | 176 |
| 176 DISALLOW_COPY_AND_ASSIGN(BirthOnThread); | 177 DISALLOW_COPY_AND_ASSIGN(BirthOnThread); |
| 177 }; | 178 }; |
| 178 | 179 |
| 179 //------------------------------------------------------------------------------ | 180 //------------------------------------------------------------------------------ |
| 180 // A class for accumulating counts of births (without bothering with a map<>). | 181 // A class for accumulating counts of births (without bothering with a map<>). |
| 181 | 182 |
| 182 class Births: public BirthOnThread { | 183 class BASE_API Births: public BirthOnThread { |
| 183 public: | 184 public: |
| 184 explicit Births(const Location& location); | 185 explicit Births(const Location& location); |
| 185 | 186 |
| 186 int birth_count() const { return birth_count_; } | 187 int birth_count() const { return birth_count_; } |
| 187 | 188 |
| 188 // When we have a birth we update the count for this BirhPLace. | 189 // When we have a birth we update the count for this BirhPLace. |
| 189 void RecordBirth() { ++birth_count_; } | 190 void RecordBirth() { ++birth_count_; } |
| 190 | 191 |
| 191 // When a birthplace is changed (updated), we need to decrement the counter | 192 // When a birthplace is changed (updated), we need to decrement the counter |
| 192 // for the old instance. | 193 // for the old instance. |
| 193 void ForgetBirth() { --birth_count_; } // We corrected a birth place. | 194 void ForgetBirth() { --birth_count_; } // We corrected a birth place. |
| 194 | 195 |
| 195 // Hack to quickly reset all counts to zero. | 196 // Hack to quickly reset all counts to zero. |
| 196 void Clear() { birth_count_ = 0; } | 197 void Clear() { birth_count_ = 0; } |
| 197 | 198 |
| 198 private: | 199 private: |
| 199 // The number of births on this thread for our location_. | 200 // The number of births on this thread for our location_. |
| 200 int birth_count_; | 201 int birth_count_; |
| 201 | 202 |
| 202 DISALLOW_COPY_AND_ASSIGN(Births); | 203 DISALLOW_COPY_AND_ASSIGN(Births); |
| 203 }; | 204 }; |
| 204 | 205 |
| 205 //------------------------------------------------------------------------------ | 206 //------------------------------------------------------------------------------ |
| 206 // Basic info summarizing multiple destructions of an object with a single | 207 // Basic info summarizing multiple destructions of an object with a single |
| 207 // birthplace (fixed Location). Used both on specific threads, and also used | 208 // birthplace (fixed Location). Used both on specific threads, and also used |
| 208 // in snapshots when integrating assembled data. | 209 // in snapshots when integrating assembled data. |
| 209 | 210 |
| 210 class DeathData { | 211 class BASE_API DeathData { |
| 211 public: | 212 public: |
| 212 // Default initializer. | 213 // Default initializer. |
| 213 DeathData() : count_(0), square_duration_(0) {} | 214 DeathData() : count_(0), square_duration_(0) {} |
| 214 | 215 |
| 215 // When deaths have not yet taken place, and we gather data from all the | 216 // When deaths have not yet taken place, and we gather data from all the |
| 216 // threads, we create DeathData stats that tally the number of births without | 217 // threads, we create DeathData stats that tally the number of births without |
| 217 // a corrosponding death. | 218 // a corrosponding death. |
| 218 explicit DeathData(int count) : count_(count), square_duration_(0) {} | 219 explicit DeathData(int count) : count_(count), square_duration_(0) {} |
| 219 | 220 |
| 220 void RecordDeath(const base::TimeDelta& duration); | 221 void RecordDeath(const base::TimeDelta& duration); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 241 int64 square_duration_; // Sum of squares in milliseconds. | 242 int64 square_duration_; // Sum of squares in milliseconds. |
| 242 }; | 243 }; |
| 243 | 244 |
| 244 //------------------------------------------------------------------------------ | 245 //------------------------------------------------------------------------------ |
| 245 // A temporary collection of data that can be sorted and summarized. It is | 246 // A temporary collection of data that can be sorted and summarized. It is |
| 246 // gathered (carefully) from many threads. Instances are held in arrays and | 247 // gathered (carefully) from many threads. Instances are held in arrays and |
| 247 // processed, filtered, and rendered. | 248 // processed, filtered, and rendered. |
| 248 // The source of this data was collected on many threads, and is asynchronously | 249 // The source of this data was collected on many threads, and is asynchronously |
| 249 // changing. The data in this instance is not asynchronously changing. | 250 // changing. The data in this instance is not asynchronously changing. |
| 250 | 251 |
| 251 class Snapshot { | 252 class BASE_API Snapshot { |
| 252 public: | 253 public: |
| 253 // When snapshotting a full life cycle set (birth-to-death), use this: | 254 // When snapshotting a full life cycle set (birth-to-death), use this: |
| 254 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, | 255 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, |
| 255 const DeathData& death_data); | 256 const DeathData& death_data); |
| 256 | 257 |
| 257 // When snapshotting a birth, with no death yet, use this: | 258 // When snapshotting a birth, with no death yet, use this: |
| 258 Snapshot(const BirthOnThread& birth_on_thread, int count); | 259 Snapshot(const BirthOnThread& birth_on_thread, int count); |
| 259 | 260 |
| 260 | 261 |
| 261 const ThreadData* birth_thread() const { return birth_->birth_thread(); } | 262 const ThreadData* birth_thread() const { return birth_->birth_thread(); } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 277 private: | 278 private: |
| 278 const BirthOnThread* birth_; // Includes Location and birth_thread. | 279 const BirthOnThread* birth_; // Includes Location and birth_thread. |
| 279 const ThreadData* death_thread_; | 280 const ThreadData* death_thread_; |
| 280 DeathData death_data_; | 281 DeathData death_data_; |
| 281 }; | 282 }; |
| 282 //------------------------------------------------------------------------------ | 283 //------------------------------------------------------------------------------ |
| 283 // DataCollector is a container class for Snapshot and BirthOnThread count | 284 // DataCollector is a container class for Snapshot and BirthOnThread count |
| 284 // items. It protects the gathering under locks, so that it could be called via | 285 // items. It protects the gathering under locks, so that it could be called via |
| 285 // Posttask on any threads, or passed to all the target threads in parallel. | 286 // Posttask on any threads, or passed to all the target threads in parallel. |
| 286 | 287 |
| 287 class DataCollector { | 288 class BASE_API DataCollector { |
| 288 public: | 289 public: |
| 289 typedef std::vector<Snapshot> Collection; | 290 typedef std::vector<Snapshot> Collection; |
| 290 | 291 |
| 291 // Construct with a list of how many threads should contribute. This helps us | 292 // Construct with a list of how many threads should contribute. This helps us |
| 292 // determine (in the async case) when we are done with all contributions. | 293 // determine (in the async case) when we are done with all contributions. |
| 293 DataCollector(); | 294 DataCollector(); |
| 294 ~DataCollector(); | 295 ~DataCollector(); |
| 295 | 296 |
| 296 // Add all stats from the indicated thread into our arrays. This function is | 297 // Add all stats from the indicated thread into our arrays. This function is |
| 297 // mutex protected, and *could* be called from any threads (although current | 298 // mutex protected, and *could* be called from any threads (although current |
| (...skipping 26 matching lines...) Expand all Loading... |
| 324 | 325 |
| 325 base::Lock accumulation_lock_; // Protects access during accumulation phase. | 326 base::Lock accumulation_lock_; // Protects access during accumulation phase. |
| 326 | 327 |
| 327 DISALLOW_COPY_AND_ASSIGN(DataCollector); | 328 DISALLOW_COPY_AND_ASSIGN(DataCollector); |
| 328 }; | 329 }; |
| 329 | 330 |
| 330 //------------------------------------------------------------------------------ | 331 //------------------------------------------------------------------------------ |
| 331 // Aggregation contains summaries (totals and subtotals) of groups of Snapshot | 332 // Aggregation contains summaries (totals and subtotals) of groups of Snapshot |
| 332 // instances to provide printing of these collections on a single line. | 333 // instances to provide printing of these collections on a single line. |
| 333 | 334 |
| 334 class Aggregation: public DeathData { | 335 class BASE_API Aggregation: public DeathData { |
| 335 public: | 336 public: |
| 336 Aggregation(); | 337 Aggregation(); |
| 337 ~Aggregation(); | 338 ~Aggregation(); |
| 338 | 339 |
| 339 void AddDeathSnapshot(const Snapshot& snapshot); | 340 void AddDeathSnapshot(const Snapshot& snapshot); |
| 340 void AddBirths(const Births& births); | 341 void AddBirths(const Births& births); |
| 341 void AddBirth(const BirthOnThread& birth); | 342 void AddBirth(const BirthOnThread& birth); |
| 342 void AddBirthPlace(const Location& location); | 343 void AddBirthPlace(const Location& location); |
| 343 void Write(std::string* output) const; | 344 void Write(std::string* output) const; |
| 344 void Clear(); | 345 void Clear(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 356 | 357 |
| 357 //------------------------------------------------------------------------------ | 358 //------------------------------------------------------------------------------ |
| 358 // Comparator is a class that supports the comparison of Snapshot instances. | 359 // Comparator is a class that supports the comparison of Snapshot instances. |
| 359 // An instance is actually a list of chained Comparitors, that can provide for | 360 // An instance is actually a list of chained Comparitors, that can provide for |
| 360 // arbitrary ordering. The path portion of an about:tasks URL is translated | 361 // arbitrary ordering. The path portion of an about:tasks URL is translated |
| 361 // into such a chain, which is then used to order Snapshot instances in a | 362 // into such a chain, which is then used to order Snapshot instances in a |
| 362 // vector. It orders them into groups (for aggregation), and can also order | 363 // vector. It orders them into groups (for aggregation), and can also order |
| 363 // instances within the groups (for detailed rendering of the instances in an | 364 // instances within the groups (for detailed rendering of the instances in an |
| 364 // aggregation). | 365 // aggregation). |
| 365 | 366 |
| 366 class Comparator { | 367 class BASE_API Comparator { |
| 367 public: | 368 public: |
| 368 // Selector enum is the token identifier for each parsed keyword, most of | 369 // Selector enum is the token identifier for each parsed keyword, most of |
| 369 // which specify a sort order. | 370 // which specify a sort order. |
| 370 // Since it is not meaningful to sort more than once on a specific key, we | 371 // Since it is not meaningful to sort more than once on a specific key, we |
| 371 // use bitfields to accumulate what we have sorted on so far. | 372 // use bitfields to accumulate what we have sorted on so far. |
| 372 enum Selector { | 373 enum Selector { |
| 373 // Sort orders. | 374 // Sort orders. |
| 374 NIL = 0, | 375 NIL = 0, |
| 375 BIRTH_THREAD = 1, | 376 BIRTH_THREAD = 1, |
| 376 DEATH_THREAD = 2, | 377 DEATH_THREAD = 2, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 // when deciding if two items are in equivalent groups. This flag tells us | 458 // when deciding if two items are in equivalent groups. This flag tells us |
| 458 // to ignore the tiebreaker when doing Equivalent() testing. | 459 // to ignore the tiebreaker when doing Equivalent() testing. |
| 459 bool use_tiebreaker_for_sort_only_; | 460 bool use_tiebreaker_for_sort_only_; |
| 460 }; | 461 }; |
| 461 | 462 |
| 462 | 463 |
| 463 //------------------------------------------------------------------------------ | 464 //------------------------------------------------------------------------------ |
| 464 // For each thread, we have a ThreadData that stores all tracking info generated | 465 // For each thread, we have a ThreadData that stores all tracking info generated |
| 465 // on this thread. This prevents the need for locking as data accumulates. | 466 // on this thread. This prevents the need for locking as data accumulates. |
| 466 | 467 |
| 467 class ThreadData { | 468 class BASE_API ThreadData { |
| 468 public: | 469 public: |
| 469 typedef std::map<Location, Births*> BirthMap; | 470 typedef std::map<Location, Births*> BirthMap; |
| 470 typedef std::map<const Births*, DeathData> DeathMap; | 471 typedef std::map<const Births*, DeathData> DeathMap; |
| 471 | 472 |
| 472 ThreadData(); | 473 ThreadData(); |
| 473 ~ThreadData(); | 474 ~ThreadData(); |
| 474 | 475 |
| 475 // Using Thread Local Store, find the current instance for collecting data. | 476 // Using Thread Local Store, find the current instance for collecting data. |
| 476 // If an instance does not exist, construct one (and remember it for use on | 477 // If an instance does not exist, construct one (and remember it for use on |
| 477 // this thread. | 478 // this thread. |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 }; | 658 }; |
| 658 static State state_; | 659 static State state_; |
| 659 | 660 |
| 660 DISALLOW_COPY_AND_ASSIGN(AutoTracking); | 661 DISALLOW_COPY_AND_ASSIGN(AutoTracking); |
| 661 }; | 662 }; |
| 662 | 663 |
| 663 | 664 |
| 664 } // namespace tracked_objects | 665 } // namespace tracked_objects |
| 665 | 666 |
| 666 #endif // BASE_TRACKED_OBJECTS_H_ | 667 #endif // BASE_TRACKED_OBJECTS_H_ |
| OLD | NEW |