Chromium Code Reviews| 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 <stack> | 10 #include <stack> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 15 #include "base/gtest_prod_util.h" | |
|
ramant (doing other things)
2011/12/03 17:36:26
hi Jim,
In the tip this file is deleted. New cod
ramant (doing other things)
2011/12/03 22:48:39
This comment was in the wrong file. I meant to add
jar (doing other things)
2011/12/04 00:46:22
No problem... I understood.
On 2011/12/03 22:48:3
| |
| 15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 16 #include "base/location.h" | 17 #include "base/location.h" |
| 17 #include "base/profiler/tracked_time.h" | 18 #include "base/profiler/tracked_time.h" |
| 18 #include "base/time.h" | 19 #include "base/time.h" |
| 19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 20 #include "base/threading/thread_local_storage.h" | 21 #include "base/threading/thread_local_storage.h" |
| 21 #include "base/tracking_info.h" | 22 #include "base/tracking_info.h" |
| 22 #include "base/values.h" | 23 #include "base/values.h" |
| 23 | 24 |
| 24 // TrackedObjects provides a database of stats about objects (generally Tasks) | 25 // TrackedObjects provides a database of stats about objects (generally Tasks) |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 namespace tracked_objects { | 191 namespace tracked_objects { |
| 191 | 192 |
| 192 //------------------------------------------------------------------------------ | 193 //------------------------------------------------------------------------------ |
| 193 // For a specific thread, and a specific birth place, the collection of all | 194 // For a specific thread, and a specific birth place, the collection of all |
| 194 // death info (with tallies for each death thread, to prevent access conflicts). | 195 // death info (with tallies for each death thread, to prevent access conflicts). |
| 195 class ThreadData; | 196 class ThreadData; |
| 196 class BASE_EXPORT BirthOnThread { | 197 class BASE_EXPORT BirthOnThread { |
| 197 public: | 198 public: |
| 198 BirthOnThread(const Location& location, const ThreadData& current); | 199 BirthOnThread(const Location& location, const ThreadData& current); |
| 199 | 200 |
| 200 const Location location() const { return location_; } | 201 const Location location() const; |
| 201 const ThreadData* birth_thread() const { return birth_thread_; } | 202 const ThreadData* birth_thread() const; |
| 202 | 203 |
| 203 private: | 204 private: |
| 204 // File/lineno of birth. This defines the essence of the task, as the context | 205 // File/lineno of birth. This defines the essence of the task, as the context |
| 205 // of the birth (construction) often tell what the item is for. This field | 206 // of the birth (construction) often tell what the item is for. This field |
| 206 // is const, and hence safe to access from any thread. | 207 // is const, and hence safe to access from any thread. |
| 207 const Location location_; | 208 const Location location_; |
| 208 | 209 |
| 209 // The thread that records births into this object. Only this thread is | 210 // The thread that records births into this object. Only this thread is |
| 210 // allowed to update birth_count_ (which changes over time). | 211 // allowed to update birth_count_ (which changes over time). |
| 211 const ThreadData* const birth_thread_; | 212 const ThreadData* const birth_thread_; |
| 212 | 213 |
| 213 DISALLOW_COPY_AND_ASSIGN(BirthOnThread); | 214 DISALLOW_COPY_AND_ASSIGN(BirthOnThread); |
| 214 }; | 215 }; |
| 215 | 216 |
| 216 //------------------------------------------------------------------------------ | 217 //------------------------------------------------------------------------------ |
| 217 // A class for accumulating counts of births (without bothering with a map<>). | 218 // A class for accumulating counts of births (without bothering with a map<>). |
| 218 | 219 |
| 219 class BASE_EXPORT Births: public BirthOnThread { | 220 class BASE_EXPORT Births: public BirthOnThread { |
| 220 public: | 221 public: |
| 221 Births(const Location& location, const ThreadData& current); | 222 Births(const Location& location, const ThreadData& current); |
| 222 | 223 |
| 223 int birth_count() const { return birth_count_; } | 224 int birth_count() const; |
| 224 | 225 |
| 225 // When we have a birth we update the count for this BirhPLace. | 226 // When we have a birth we update the count for this BirhPLace. |
| 226 void RecordBirth() { ++birth_count_; } | 227 void RecordBirth(); |
| 227 | 228 |
| 228 // When a birthplace is changed (updated), we need to decrement the counter | 229 // When a birthplace is changed (updated), we need to decrement the counter |
| 229 // for the old instance. | 230 // for the old instance. |
| 230 void ForgetBirth() { --birth_count_; } // We corrected a birth place. | 231 void ForgetBirth(); |
| 231 | 232 |
| 232 // Hack to quickly reset all counts to zero. | 233 // Hack to quickly reset all counts to zero. |
| 233 void Clear() { birth_count_ = 0; } | 234 void Clear(); |
| 234 | 235 |
| 235 private: | 236 private: |
| 236 // The number of births on this thread for our location_. | 237 // The number of births on this thread for our location_. |
| 237 int birth_count_; | 238 int birth_count_; |
| 238 | 239 |
| 239 DISALLOW_COPY_AND_ASSIGN(Births); | 240 DISALLOW_COPY_AND_ASSIGN(Births); |
| 240 }; | 241 }; |
| 241 | 242 |
| 242 //------------------------------------------------------------------------------ | 243 //------------------------------------------------------------------------------ |
| 243 // Basic info summarizing multiple destructions of a tracked object with a | 244 // Basic info summarizing multiple destructions of a tracked object with a |
| 244 // single birthplace (fixed Location). Used both on specific threads, and also | 245 // single birthplace (fixed Location). Used both on specific threads, and also |
| 245 // in snapshots when integrating assembled data. | 246 // in snapshots when integrating assembled data. |
| 246 | 247 |
| 247 class BASE_EXPORT DeathData { | 248 class BASE_EXPORT DeathData { |
| 248 public: | 249 public: |
| 249 // Default initializer. | 250 // Default initializer. |
| 250 DeathData() : count_(0) {} | 251 DeathData(); |
| 251 | 252 |
| 252 // When deaths have not yet taken place, and we gather data from all the | 253 // When deaths have not yet taken place, and we gather data from all the |
| 253 // threads, we create DeathData stats that tally the number of births without | 254 // threads, we create DeathData stats that tally the number of births without |
| 254 // a corrosponding death. | 255 // a corresponding death. |
| 255 explicit DeathData(int count) | 256 explicit DeathData(int count); |
| 256 : count_(count) {} | |
| 257 | 257 |
| 258 // Update stats for a task destruction (death) that had a Run() time of | 258 // Update stats for a task destruction (death) that had a Run() time of |
| 259 // |duration|, and has had a queueing delay of |queue_duration|. | 259 // |duration|, and has had a queueing delay of |queue_duration|. |
| 260 void RecordDeath(DurationInt queue_duration, | 260 void RecordDeath(const DurationInt queue_duration, |
| 261 DurationInt run_duration); | 261 const DurationInt run_duration, |
| 262 int random_number); | |
| 262 | 263 |
| 263 // Metrics accessors. | 264 // Metrics accessors, used only in tests. |
| 264 int count() const { return count_; } | 265 int count() const; |
| 265 DurationInt run_duration() const { return run_time_.duration(); } | 266 DurationInt run_duration_sum() const; |
| 266 DurationInt AverageMsRunDuration() const; | 267 DurationInt run_duration_max() const; |
| 267 DurationInt run_duration_max() const { return run_time_.max(); } | 268 DurationInt run_duration_sample() const; |
| 268 DurationInt queue_duration() const { return queue_time_.duration(); } | 269 DurationInt queue_duration_sum() const; |
| 269 DurationInt AverageMsQueueDuration() const; | 270 DurationInt queue_duration_max() const; |
| 270 DurationInt queue_duration_max() const { return queue_time_.max(); } | 271 DurationInt queue_duration_sample() const; |
| 271 | |
| 272 // Accumulate metrics from other into this. This method is never used on | |
| 273 // realtime statistics, and only used in snapshots and aggregatinos. | |
| 274 void AddDeathData(const DeathData& other); | |
| 275 | 272 |
| 276 // Construct a DictionaryValue instance containing all our stats. The caller | 273 // Construct a DictionaryValue instance containing all our stats. The caller |
| 277 // assumes ownership of the returned instance. | 274 // assumes ownership of the returned instance. |
| 278 base::DictionaryValue* ToValue() const; | 275 base::DictionaryValue* ToValue() const; |
| 279 | 276 |
| 277 // Reset the max values to zero. | |
| 278 void ResetMax(); | |
| 279 | |
| 280 // Reset all tallies to zero. This is used as a hack on realtime data. | 280 // Reset all tallies to zero. This is used as a hack on realtime data. |
| 281 void Clear(); | 281 void Clear(); |
| 282 | 282 |
| 283 private: | 283 private: |
| 284 // DeathData::Data is a helper class, useful when different metrics need to be | 284 // Number of runs seen. |
| 285 // aggregated, such as queueing times, or run times. | 285 int count_; |
| 286 class Data { | 286 // Data about run time durations. |
| 287 public: | 287 DurationInt run_duration_sum_; |
| 288 Data() : duration_(0), max_(0) {} | 288 DurationInt run_duration_max_; |
| 289 ~Data() {} | 289 DurationInt run_duration_sample_; |
| 290 | 290 // Data about queueing times durations. |
| 291 DurationInt duration() const { return duration_; } | 291 DurationInt queue_duration_sum_; |
| 292 DurationInt max() const { return max_; } | 292 DurationInt queue_duration_max_; |
| 293 | 293 DurationInt queue_duration_sample_; |
| 294 // Agggegate data into our state. | |
| 295 void AddData(const Data& other); | |
| 296 void AddDuration(DurationInt duration); | |
| 297 | |
| 298 // Central helper function for calculating averages (correctly, in only one | |
| 299 // place). | |
| 300 DurationInt AverageMsDuration(int count) const; | |
| 301 | |
| 302 // Resets all members to zero. | |
| 303 void Clear(); | |
| 304 | |
| 305 private: | |
| 306 DurationInt duration_; // Sum of all durations seen. | |
| 307 DurationInt max_; // Largest singular duration seen. | |
| 308 }; | |
| 309 | |
| 310 | |
| 311 int count_; // Number of deaths seen. | |
| 312 Data run_time_; // Data about run time durations. | |
| 313 Data queue_time_; // Data about queueing times durations. | |
| 314 }; | 294 }; |
| 315 | 295 |
| 316 //------------------------------------------------------------------------------ | 296 //------------------------------------------------------------------------------ |
| 317 // A temporary collection of data that can be sorted and summarized. It is | 297 // A temporary collection of data that can be sorted and summarized. It is |
| 318 // gathered (carefully) from many threads. Instances are held in arrays and | 298 // gathered (carefully) from many threads. Instances are held in arrays and |
| 319 // processed, filtered, and rendered. | 299 // processed, filtered, and rendered. |
| 320 // The source of this data was collected on many threads, and is asynchronously | 300 // The source of this data was collected on many threads, and is asynchronously |
| 321 // changing. The data in this instance is not asynchronously changing. | 301 // changing. The data in this instance is not asynchronously changing. |
| 322 | 302 |
| 323 class BASE_EXPORT Snapshot { | 303 class BASE_EXPORT Snapshot { |
| 324 public: | 304 public: |
| 325 // When snapshotting a full life cycle set (birth-to-death), use this: | 305 // When snapshotting a full life cycle set (birth-to-death), use this: |
| 326 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, | 306 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, |
| 327 const DeathData& death_data); | 307 const DeathData& death_data); |
| 328 | 308 |
| 329 // When snapshotting a birth, with no death yet, use this: | 309 // When snapshotting a birth, with no death yet, use this: |
| 330 Snapshot(const BirthOnThread& birth_on_thread, int count); | 310 Snapshot(const BirthOnThread& birth_on_thread, int count); |
| 331 | 311 |
| 332 const ThreadData* birth_thread() const { return birth_->birth_thread(); } | 312 // Accessor, that provides default value when there is no death thread. |
| 333 const Location location() const { return birth_->location(); } | |
| 334 const BirthOnThread& birth() const { return *birth_; } | |
| 335 const ThreadData* death_thread() const {return death_thread_; } | |
| 336 const DeathData& death_data() const { return death_data_; } | |
| 337 const std::string DeathThreadName() const; | 313 const std::string DeathThreadName() const; |
| 338 | 314 |
| 339 int count() const { return death_data_.count(); } | |
| 340 DurationInt run_duration() const { return death_data_.run_duration(); } | |
| 341 DurationInt AverageMsRunDuration() const { | |
| 342 return death_data_.AverageMsRunDuration(); | |
| 343 } | |
| 344 DurationInt run_duration_max() const { | |
| 345 return death_data_.run_duration_max(); | |
| 346 } | |
| 347 DurationInt queue_duration() const { return death_data_.queue_duration(); } | |
| 348 DurationInt AverageMsQueueDuration() const { | |
| 349 return death_data_.AverageMsQueueDuration(); | |
| 350 } | |
| 351 DurationInt queue_duration_max() const { | |
| 352 return death_data_.queue_duration_max(); | |
| 353 } | |
| 354 | |
| 355 // Construct a DictionaryValue instance containing all our data recursively. | 315 // Construct a DictionaryValue instance containing all our data recursively. |
| 356 // The caller assumes ownership of the memory in the returned instance. | 316 // The caller assumes ownership of the memory in the returned instance. |
| 357 base::DictionaryValue* ToValue() const; | 317 base::DictionaryValue* ToValue() const; |
| 358 | 318 |
| 359 private: | 319 private: |
| 360 const BirthOnThread* birth_; // Includes Location and birth_thread. | 320 const BirthOnThread* birth_; // Includes Location and birth_thread. |
| 361 const ThreadData* death_thread_; | 321 const ThreadData* death_thread_; |
| 362 DeathData death_data_; | 322 DeathData death_data_; |
| 363 }; | 323 }; |
| 364 | 324 |
| 365 //------------------------------------------------------------------------------ | 325 //------------------------------------------------------------------------------ |
| 366 // DataCollector is a container class for Snapshot and BirthOnThread count | |
| 367 // items. | |
| 368 | |
| 369 class BASE_EXPORT DataCollector { | |
| 370 public: | |
| 371 typedef std::vector<Snapshot> Collection; | |
| 372 | |
| 373 // Construct with a list of how many threads should contribute. This helps us | |
| 374 // determine (in the async case) when we are done with all contributions. | |
| 375 DataCollector(); | |
| 376 ~DataCollector(); | |
| 377 | |
| 378 // Adds all stats from the indicated thread into our arrays. This function | |
| 379 // uses locks at the lowest level (when accessing the underlying maps which | |
| 380 // could change when not locked), and can be called from any threads. | |
| 381 void Append(const ThreadData& thread_data); | |
| 382 | |
| 383 // After the accumulation phase, the following accessor is used to process the | |
| 384 // data (i.e., sort it, filter it, etc.). | |
| 385 Collection* collection(); | |
| 386 | |
| 387 // Adds entries for all the remaining living objects (objects that have | |
| 388 // tallied a birth, but have not yet tallied a matching death, and hence must | |
| 389 // be either running, queued up, or being held in limbo for future posting). | |
| 390 // This should be called after all known ThreadData instances have been | |
| 391 // processed using Append(). | |
| 392 void AddListOfLivingObjects(); | |
| 393 | |
| 394 // Generates a ListValue representation of the vector of snapshots. The caller | |
| 395 // assumes ownership of the memory in the returned instance. | |
| 396 base::ListValue* ToValue() const; | |
| 397 | |
| 398 private: | |
| 399 typedef std::map<const BirthOnThread*, int> BirthCount; | |
| 400 | |
| 401 // The array that we collect data into. | |
| 402 Collection collection_; | |
| 403 | |
| 404 // The total number of births recorded at each location for which we have not | |
| 405 // seen a death count. This map changes as we do Append() calls, and is later | |
| 406 // used by AddListOfLivingObjects() to gather up unaccounted for births. | |
| 407 BirthCount global_birth_count_; | |
| 408 | |
| 409 DISALLOW_COPY_AND_ASSIGN(DataCollector); | |
| 410 }; | |
| 411 | |
| 412 //------------------------------------------------------------------------------ | |
| 413 // For each thread, we have a ThreadData that stores all tracking info generated | 326 // For each thread, we have a ThreadData that stores all tracking info generated |
| 414 // on this thread. This prevents the need for locking as data accumulates. | 327 // on this thread. This prevents the need for locking as data accumulates. |
| 415 // We use ThreadLocalStorage to quickly identfy the current ThreadData context. | 328 // We use ThreadLocalStorage to quickly identfy the current ThreadData context. |
| 416 // We also have a linked list of ThreadData instances, and that list is used to | 329 // We also have a linked list of ThreadData instances, and that list is used to |
| 417 // harvest data from all existing instances. | 330 // harvest data from all existing instances. |
| 418 | 331 |
| 419 class BASE_EXPORT ThreadData { | 332 class BASE_EXPORT ThreadData { |
| 420 public: | 333 public: |
| 421 // Current allowable states of the tracking system. The states can vary | 334 // Current allowable states of the tracking system. The states can vary |
| 422 // between ACTIVE and DEACTIVATED, but can never go back to UNINITIALIZED. | 335 // between ACTIVE and DEACTIVATED, but can never go back to UNINITIALIZED. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 436 static void InitializeThreadContext(const std::string& suggested_name); | 349 static void InitializeThreadContext(const std::string& suggested_name); |
| 437 | 350 |
| 438 // Using Thread Local Store, find the current instance for collecting data. | 351 // Using Thread Local Store, find the current instance for collecting data. |
| 439 // If an instance does not exist, construct one (and remember it for use on | 352 // If an instance does not exist, construct one (and remember it for use on |
| 440 // this thread. | 353 // this thread. |
| 441 // This may return NULL if the system is disabled for any reason. | 354 // This may return NULL if the system is disabled for any reason. |
| 442 static ThreadData* Get(); | 355 static ThreadData* Get(); |
| 443 | 356 |
| 444 // Constructs a DictionaryValue instance containing all recursive results in | 357 // Constructs a DictionaryValue instance containing all recursive results in |
| 445 // our process. The caller assumes ownership of the memory in the returned | 358 // our process. The caller assumes ownership of the memory in the returned |
| 446 // instance. | 359 // instance. During the scavenging, if |reset_value| is true, then the |
|
ramant (doing other things)
2011/12/03 22:48:39
reset_value -> reset_max
jar (doing other things)
2011/12/04 00:46:22
Done.
| |
| 447 static base::DictionaryValue* ToValue(); | 360 // DeathData instances max-values are reset to zero during this scan. |
| 361 static base::DictionaryValue* ToValue(bool reset_max); | |
| 448 | 362 |
| 449 // Finds (or creates) a place to count births from the given location in this | 363 // Finds (or creates) a place to count births from the given location in this |
| 450 // thread, and increment that tally. | 364 // thread, and increment that tally. |
| 451 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. | 365 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. |
| 452 static Births* TallyABirthIfActive(const Location& location); | 366 static Births* TallyABirthIfActive(const Location& location); |
| 453 | 367 |
| 454 // Records the end of a timed run of an object. The |completed_task| contains | 368 // Records the end of a timed run of an object. The |completed_task| contains |
| 455 // a pointer to a Births, the time_posted, and a delayed_start_time if any. | 369 // a pointer to a Births, the time_posted, and a delayed_start_time if any. |
| 456 // The |start_of_run| indicates when we started to perform the run of the | 370 // The |start_of_run| indicates when we started to perform the run of the |
| 457 // task. The delayed_start_time is non-null for tasks that were posted as | 371 // task. The delayed_start_time is non-null for tasks that were posted as |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 477 const TrackedTime& start_of_run, | 391 const TrackedTime& start_of_run, |
| 478 const TrackedTime& end_of_run); | 392 const TrackedTime& end_of_run); |
| 479 | 393 |
| 480 // Record the end of execution in region, generally corresponding to a scope | 394 // Record the end of execution in region, generally corresponding to a scope |
| 481 // being exited. | 395 // being exited. |
| 482 static void TallyRunInAScopedRegionIfTracking( | 396 static void TallyRunInAScopedRegionIfTracking( |
| 483 const Births* birth, | 397 const Births* birth, |
| 484 const TrackedTime& start_of_run, | 398 const TrackedTime& start_of_run, |
| 485 const TrackedTime& end_of_run); | 399 const TrackedTime& end_of_run); |
| 486 | 400 |
| 487 const std::string thread_name() const { return thread_name_; } | 401 const std::string thread_name() const; |
| 488 | 402 |
| 489 // --------------------- | 403 // Snapshot (under a lock) copies of the maps in each ThreadData instance. For |
| 490 // TODO(jar): | 404 // each set of maps (BirthMap and DeathMap) call the Append() method of the |
| 491 // The following functions should all be private, and are only public because | 405 // |target| DataCollector. If |reset_max| is true, then the max values in |
| 492 // the collection is done externally. We need to relocate that code from the | 406 // each DeathData instance should be reset during the scan. |
| 493 // collection class into this class, and then all these methods can be made | 407 static void SendAllMaps(bool reset_max, class DataCollector* target); |
| 494 // private. | |
| 495 // (Thread safe) Get start of list of all ThreadData instances. | |
| 496 static ThreadData* first(); | |
| 497 // Iterate through the null terminated list of ThreadData instances. | |
| 498 ThreadData* next() const { return next_; } | |
| 499 // Using our lock, make a copy of the specified maps. These calls may arrive | |
| 500 // from non-local threads, and are used to quickly scan data from all threads | |
| 501 // in order to build JSON for about:profiler. | |
| 502 void SnapshotBirthMap(BirthMap *output) const; | |
| 503 void SnapshotDeathMap(DeathMap *output) const; | |
| 504 // -------- end of should be private methods. | |
| 505 | 408 |
| 506 // Hack: asynchronously clear all birth counts and death tallies data values | 409 // Hack: asynchronously clear all birth counts and death tallies data values |
| 507 // in all ThreadData instances. The numerical (zeroing) part is done without | 410 // in all ThreadData instances. The numerical (zeroing) part is done without |
| 508 // use of a locks or atomics exchanges, and may (for int64 values) produce | 411 // use of a locks or atomics exchanges, and may (for int64 values) produce |
| 509 // bogus counts VERY rarely. | 412 // bogus counts VERY rarely. |
| 510 static void ResetAllThreadData(); | 413 static void ResetAllThreadData(); |
| 511 | 414 |
| 512 // Initializes all statics if needed (this initialization call should be made | 415 // Initializes all statics if needed (this initialization call should be made |
| 513 // while we are single threaded). Returns false if unable to initialize. | 416 // while we are single threaded). Returns false if unable to initialize. |
| 514 static bool Initialize(); | 417 static bool Initialize(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 533 static TrackedTime Now(); | 436 static TrackedTime Now(); |
| 534 | 437 |
| 535 // This function can be called at process termination to validate that thread | 438 // This function can be called at process termination to validate that thread |
| 536 // cleanup routines have been called for at least some number of named | 439 // cleanup routines have been called for at least some number of named |
| 537 // threads. | 440 // threads. |
| 538 static void EnsureCleanupWasCalled(int major_threads_shutdown_count); | 441 static void EnsureCleanupWasCalled(int major_threads_shutdown_count); |
| 539 | 442 |
| 540 private: | 443 private: |
| 541 // Allow only tests to call ShutdownSingleThreadedCleanup. We NEVER call it | 444 // Allow only tests to call ShutdownSingleThreadedCleanup. We NEVER call it |
| 542 // in production code. | 445 // in production code. |
| 446 // TODO(jar): Make this a friend in DEBUG only, so that the optimizer has a | |
| 447 // better change of optimizing (inlining? etc.) private methods (knowing that | |
| 448 // there will be no need for an external entry point). | |
| 543 friend class TrackedObjectsTest; | 449 friend class TrackedObjectsTest; |
| 450 FRIEND_TEST_ALL_PREFIXES(TrackedObjectsTest, MinimalStartupShutdown); | |
| 451 FRIEND_TEST_ALL_PREFIXES(TrackedObjectsTest, TinyStartupShutdown); | |
| 544 | 452 |
| 545 // Worker thread construction creates a name since there is none. | 453 // Worker thread construction creates a name since there is none. |
| 546 explicit ThreadData(int thread_number); | 454 explicit ThreadData(int thread_number); |
| 547 | 455 |
| 548 // Message loop based construction should provide a name. | 456 // Message loop based construction should provide a name. |
| 549 explicit ThreadData(const std::string& suggested_name); | 457 explicit ThreadData(const std::string& suggested_name); |
| 550 | 458 |
| 551 ~ThreadData(); | 459 ~ThreadData(); |
| 552 | 460 |
| 553 // Push this instance to the head of all_thread_data_list_head_, linking it to | 461 // Push this instance to the head of all_thread_data_list_head_, linking it to |
| 554 // the previous head. This is performed after each construction, and leaves | 462 // the previous head. This is performed after each construction, and leaves |
| 555 // the instance permanently on that list. | 463 // the instance permanently on that list. |
| 556 void PushToHeadOfList(); | 464 void PushToHeadOfList(); |
| 557 | 465 |
| 466 // (Thread safe) Get start of list of all ThreadData instances using the lock. | |
| 467 static ThreadData* first(); | |
| 468 | |
| 469 // Iterate through the null terminated list of ThreadData instances. | |
| 470 ThreadData* next() const; | |
| 471 | |
| 472 | |
| 558 // In this thread's data, record a new birth. | 473 // In this thread's data, record a new birth. |
| 559 Births* TallyABirth(const Location& location); | 474 Births* TallyABirth(const Location& location); |
| 560 | 475 |
| 561 // Find a place to record a death on this thread. | 476 // Find a place to record a death on this thread. |
| 562 void TallyADeath(const Births& birth, | 477 void TallyADeath(const Births& birth, |
| 563 DurationInt queue_duration, | 478 DurationInt queue_duration, |
| 564 DurationInt duration); | 479 DurationInt duration); |
| 565 | 480 |
| 481 // Using our lock, make a copy of the specified maps. This call may be made | |
| 482 // on non-local threads, which necessitate the use of the lock to prevent | |
| 483 // the map(s) from being reallocaed while they are copied. If |reset_max| is | |
| 484 // true, then, just after we copy the DeathMap, we will set the max values to | |
| 485 // zero in the active DeathMap (not the snapshot). | |
| 486 void SnapshotMaps(bool reset_max, | |
| 487 BirthMap* birth_map, | |
| 488 DeathMap* death_map); | |
| 489 | |
| 566 // Using our lock to protect the iteration, Clear all birth and death data. | 490 // Using our lock to protect the iteration, Clear all birth and death data. |
| 567 void Reset(); | 491 void Reset(); |
| 568 | 492 |
| 569 // This method is called by the TLS system when a thread terminates. | 493 // This method is called by the TLS system when a thread terminates. |
| 570 // The argument may be NULL if this thread has never tracked a birth or death. | 494 // The argument may be NULL if this thread has never tracked a birth or death. |
| 571 static void OnThreadTermination(void* thread_data); | 495 static void OnThreadTermination(void* thread_data); |
| 572 | 496 |
| 573 // This method should be called when a worker thread terminates, so that we | 497 // This method should be called when a worker thread terminates, so that we |
| 574 // can save all the thread data into a cache of reusable ThreadData instances. | 498 // can save all the thread data into a cache of reusable ThreadData instances. |
| 575 void OnThreadTerminationCleanup(); | 499 void OnThreadTerminationCleanup(); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 662 DeathMap death_map_; | 586 DeathMap death_map_; |
| 663 | 587 |
| 664 // Lock to protect *some* access to BirthMap and DeathMap. The maps are | 588 // Lock to protect *some* access to BirthMap and DeathMap. The maps are |
| 665 // regularly read and written on this thread, but may only be read from other | 589 // regularly read and written on this thread, but may only be read from other |
| 666 // threads. To support this, we acquire this lock if we are writing from this | 590 // threads. To support this, we acquire this lock if we are writing from this |
| 667 // thread, or reading from another thread. For reading from this thread we | 591 // thread, or reading from another thread. For reading from this thread we |
| 668 // don't need a lock, as there is no potential for a conflict since the | 592 // don't need a lock, as there is no potential for a conflict since the |
| 669 // writing is only done from this thread. | 593 // writing is only done from this thread. |
| 670 mutable base::Lock map_lock_; | 594 mutable base::Lock map_lock_; |
| 671 | 595 |
| 596 // A random number that we used to select decide which sample to keep as a | |
| 597 // representative sample in each DeathData instance. We start it off "very | |
| 598 // random" (which is expensive), and then stir in some data gently as we see | |
| 599 // various events. | |
| 600 int32 random_number_; | |
| 601 | |
| 672 DISALLOW_COPY_AND_ASSIGN(ThreadData); | 602 DISALLOW_COPY_AND_ASSIGN(ThreadData); |
| 673 }; | 603 }; |
| 674 | 604 |
| 675 //------------------------------------------------------------------------------ | 605 //------------------------------------------------------------------------------ |
| 606 // DataCollector is a container class for Snapshot and BirthOnThread count | |
| 607 // items. | |
| 608 | |
| 609 class BASE_EXPORT DataCollector { | |
| 610 public: | |
| 611 typedef std::vector<Snapshot> Collection; | |
| 612 | |
| 613 // Construct with a list of how many threads should contribute. This helps us | |
| 614 // determine (in the async case) when we are done with all contributions. | |
| 615 DataCollector(); | |
| 616 ~DataCollector(); | |
| 617 | |
| 618 // Adds all stats from the indicated thread into our arrays. Accepts copies | |
| 619 // of the birth_map and death_map, so that the data will not change during the | |
| 620 // iterations and processing. | |
| 621 void Append(const ThreadData &thread_data, | |
| 622 const ThreadData::BirthMap &birth_map, | |
| 623 const ThreadData::DeathMap &death_map); | |
| 624 | |
| 625 // After the accumulation phase, the following accessor is used to process the | |
| 626 // data (i.e., sort it, filter it, etc.). | |
| 627 Collection* collection(); | |
| 628 | |
| 629 // Adds entries for all the remaining living objects (objects that have | |
| 630 // tallied a birth, but have not yet tallied a matching death, and hence must | |
| 631 // be either running, queued up, or being held in limbo for future posting). | |
| 632 // This should be called after all known ThreadData instances have been | |
| 633 // processed using Append(). | |
| 634 void AddListOfLivingObjects(); | |
| 635 | |
| 636 // Generates a ListValue representation of the vector of snapshots. The caller | |
| 637 // assumes ownership of the memory in the returned instance. | |
| 638 base::ListValue* ToValue() const; | |
| 639 | |
| 640 private: | |
| 641 typedef std::map<const BirthOnThread*, int> BirthCount; | |
| 642 | |
| 643 // The array that we collect data into. | |
| 644 Collection collection_; | |
| 645 | |
| 646 // The total number of births recorded at each location for which we have not | |
| 647 // seen a death count. This map changes as we do Append() calls, and is later | |
| 648 // used by AddListOfLivingObjects() to gather up unaccounted for births. | |
| 649 BirthCount global_birth_count_; | |
| 650 | |
| 651 DISALLOW_COPY_AND_ASSIGN(DataCollector); | |
| 652 }; | |
| 653 | |
| 654 //------------------------------------------------------------------------------ | |
| 676 // Provide simple way to to start global tracking, and to tear down tracking | 655 // Provide simple way to to start global tracking, and to tear down tracking |
| 677 // when done. The design has evolved to *not* do any teardown (and just leak | 656 // when done. The design has evolved to *not* do any teardown (and just leak |
| 678 // all allocated data structures). As a result, we don't have any code in this | 657 // all allocated data structures). As a result, we don't have any code in this |
| 679 // destructor, and perhaps this whole class should go away. | 658 // destructor, and perhaps this whole class should go away. |
| 680 | 659 |
| 681 class BASE_EXPORT AutoTracking { | 660 class BASE_EXPORT AutoTracking { |
| 682 public: | 661 public: |
| 683 AutoTracking() { | 662 AutoTracking() { |
| 684 ThreadData::Initialize(); | 663 ThreadData::Initialize(); |
| 685 } | 664 } |
| 686 | 665 |
| 687 ~AutoTracking() { | 666 ~AutoTracking() { |
| 688 // TODO(jar): Consider emitting a CSV dump of the data at this point. This | 667 // TODO(jar): Consider emitting a CSV dump of the data at this point. This |
| 689 // should be called after the message loops have all terminated (or at least | 668 // should be called after the message loops have all terminated (or at least |
| 690 // the main message loop is gone), so there is little chance for additional | 669 // the main message loop is gone), so there is little chance for additional |
| 691 // tasks to be Run. | 670 // tasks to be Run. |
| 692 } | 671 } |
| 693 | 672 |
| 694 private: | 673 private: |
| 695 | 674 |
| 696 DISALLOW_COPY_AND_ASSIGN(AutoTracking); | 675 DISALLOW_COPY_AND_ASSIGN(AutoTracking); |
| 697 }; | 676 }; |
| 698 | 677 |
| 699 } // namespace tracked_objects | 678 } // namespace tracked_objects |
| 700 | 679 |
| 701 #endif // BASE_TRACKED_OBJECTS_H_ | 680 #endif // BASE_TRACKED_OBJECTS_H_ |
| OLD | NEW |