| 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> |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 // freely accessed by any thread at any time (i.e., only the statistic needs to | 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). | 59 // be handled carefully, and it is ONLY read or written by the birth thread). |
| 60 // | 60 // |
| 61 // For Tasks, having now either constructed or found the Births instance | 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 | 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 | 63 // PendingTask structure in MessageLoop. This fact alone is very useful in |
| 64 // debugging, when there is a question of where an instance came from. In | 64 // debugging, when there is a question of where an instance came from. In |
| 65 // addition, the birth time is also recorded and used to later evaluate the | 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 | 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 | 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. | 68 // any locks, as all that data is constant across the life of the process. We |
| 69 // currently over-write the birth time with the time at which a Run() of the |
| 70 // task begins, so that we can calculate execution time independendent of |
| 71 // queueuing time (time spent in delayed task queues, or regular queues). |
| 69 // | 72 // |
| 70 // This can also be done for any other object as well by calling | 73 // This can also be done for any other object as well by calling |
| 71 // TallyABirthIfActive() and TallyADeathIfActive() as appropriate. | 74 // TallyABirthIfActive() and TallyADeathIfActive() as appropriate. |
| 72 // | 75 // |
| 73 // The amount of memory used in the above data structures depends on how many | 76 // 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. | 77 // 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 | 78 // 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 | 79 // rather we only need one Births instance for each thread that constructs an |
| 77 // instance at a Location. In many cases, instances are only created on one | 80 // instance at a Location. In many cases, instances are only created on one |
| 78 // thread, so the memory utilization is actually fairly restrained. | 81 // thread, so the memory utilization is actually fairly restrained. |
| 79 // | 82 // |
| 80 // Lastly, when an instance is deleted, the final tallies of statistics are | 83 // Lastly, when an instance is deleted, the final tallies of statistics are |
| 81 // carefully accumulated. That tallying wrties into slots (members) in a | 84 // carefully accumulated. That tallying wrties into slots (members) in a |
| 82 // collection of DeathData instances. For each birth place Location that is | 85 // 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 | 86 // 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 | 87 // death count, as well as accumulate the Run()time duration of the instance as |
| 85 // it is destroyed (dies). By maintaining a single place to aggregate this | 88 // 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 | 89 // addition *only* for the given thread, we avoid the need to lock such |
| 87 // DeathData instances. | 90 // DeathData instances. |
| 88 // | 91 // |
| 89 // With the above lifecycle description complete, the major remaining detail is | 92 // With the above lifecycle description complete, the major remaining detail is |
| 90 // explaining how each thread maintains a list of DeathData instances, and of | 93 // explaining how each thread maintains a list of DeathData instances, and of |
| 91 // Births instances, and is able to avoid additional (redundant/unnecessary) | 94 // Births instances, and is able to avoid additional (redundant/unnecessary) |
| 92 // allocations. | 95 // allocations. |
| 93 // | 96 // |
| 94 // Each thread maintains a list of data items specific to that thread in a | 97 // Each thread maintains a list of data items specific to that thread in a |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 }; | 209 }; |
| 207 | 210 |
| 208 //------------------------------------------------------------------------------ | 211 //------------------------------------------------------------------------------ |
| 209 // Basic info summarizing multiple destructions of an object with a single | 212 // Basic info summarizing multiple destructions of an object with a single |
| 210 // birthplace (fixed Location). Used both on specific threads, and also used | 213 // birthplace (fixed Location). Used both on specific threads, and also used |
| 211 // in snapshots when integrating assembled data. | 214 // in snapshots when integrating assembled data. |
| 212 | 215 |
| 213 class BASE_EXPORT DeathData { | 216 class BASE_EXPORT DeathData { |
| 214 public: | 217 public: |
| 215 // Default initializer. | 218 // Default initializer. |
| 216 DeathData() : count_(0), square_duration_(0) {} | 219 DeathData() : count_(0) {} |
| 217 | 220 |
| 218 // When deaths have not yet taken place, and we gather data from all the | 221 // When deaths have not yet taken place, and we gather data from all the |
| 219 // threads, we create DeathData stats that tally the number of births without | 222 // threads, we create DeathData stats that tally the number of births without |
| 220 // a corrosponding death. | 223 // a corrosponding death. |
| 221 explicit DeathData(int count) : count_(count), square_duration_(0) {} | 224 explicit DeathData(int count) : count_(count) {} |
| 222 | 225 |
| 226 // Update stats for a task destruction (death) that had a Run() time of |
| 227 // |duration|. |
| 223 void RecordDeath(const base::TimeDelta& duration); | 228 void RecordDeath(const base::TimeDelta& duration); |
| 224 | 229 |
| 225 // Metrics accessors. | 230 // Metrics accessors. |
| 226 int count() const { return count_; } | 231 int count() const { return count_; } |
| 227 base::TimeDelta life_duration() const { return life_duration_; } | 232 base::TimeDelta life_duration() const { return life_duration_; } |
| 228 int64 square_duration() const { return square_duration_; } | |
| 229 int AverageMsDuration() const; | 233 int AverageMsDuration() const; |
| 230 double StandardDeviation() const; | 234 double StandardDeviation() const; |
| 231 | 235 |
| 232 // Accumulate metrics from other into this. | 236 // Accumulate metrics from other into this. |
| 233 void AddDeathData(const DeathData& other); | 237 void AddDeathData(const DeathData& other); |
| 234 | 238 |
| 235 // Simple print of internal state. | 239 // Simple print of internal state. |
| 236 void Write(std::string* output) const; | 240 void Write(std::string* output) const; |
| 237 | 241 |
| 238 // Reset all tallies to zero. | 242 // Reset all tallies to zero. |
| 239 void Clear(); | 243 void Clear(); |
| 240 | 244 |
| 241 private: | 245 private: |
| 242 int count_; // Number of destructions. | 246 int count_; // Number of destructions. |
| 243 base::TimeDelta life_duration_; // Sum of all lifetime durations. | 247 base::TimeDelta life_duration_; // Sum of all Run()time durations. |
| 244 int64 square_duration_; // Sum of squares in milliseconds. | |
| 245 }; | 248 }; |
| 246 | 249 |
| 247 //------------------------------------------------------------------------------ | 250 //------------------------------------------------------------------------------ |
| 248 // A temporary collection of data that can be sorted and summarized. It is | 251 // A temporary collection of data that can be sorted and summarized. It is |
| 249 // gathered (carefully) from many threads. Instances are held in arrays and | 252 // gathered (carefully) from many threads. Instances are held in arrays and |
| 250 // processed, filtered, and rendered. | 253 // processed, filtered, and rendered. |
| 251 // The source of this data was collected on many threads, and is asynchronously | 254 // The source of this data was collected on many threads, and is asynchronously |
| 252 // changing. The data in this instance is not asynchronously changing. | 255 // changing. The data in this instance is not asynchronously changing. |
| 253 | 256 |
| 254 class BASE_EXPORT Snapshot { | 257 class BASE_EXPORT Snapshot { |
| 255 public: | 258 public: |
| 256 // When snapshotting a full life cycle set (birth-to-death), use this: | 259 // When snapshotting a full life cycle set (birth-to-death), use this: |
| 257 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, | 260 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, |
| 258 const DeathData& death_data); | 261 const DeathData& death_data); |
| 259 | 262 |
| 260 // When snapshotting a birth, with no death yet, use this: | 263 // When snapshotting a birth, with no death yet, use this: |
| 261 Snapshot(const BirthOnThread& birth_on_thread, int count); | 264 Snapshot(const BirthOnThread& birth_on_thread, int count); |
| 262 | 265 |
| 263 | 266 |
| 264 const ThreadData* birth_thread() const { return birth_->birth_thread(); } | 267 const ThreadData* birth_thread() const { return birth_->birth_thread(); } |
| 265 const Location location() const { return birth_->location(); } | 268 const Location location() const { return birth_->location(); } |
| 266 const BirthOnThread& birth() const { return *birth_; } | 269 const BirthOnThread& birth() const { return *birth_; } |
| 267 const ThreadData* death_thread() const {return death_thread_; } | 270 const ThreadData* death_thread() const {return death_thread_; } |
| 268 const DeathData& death_data() const { return death_data_; } | 271 const DeathData& death_data() const { return death_data_; } |
| 269 const std::string DeathThreadName() const; | 272 const std::string DeathThreadName() const; |
| 270 | 273 |
| 271 int count() const { return death_data_.count(); } | 274 int count() const { return death_data_.count(); } |
| 272 base::TimeDelta life_duration() const { return death_data_.life_duration(); } | 275 base::TimeDelta life_duration() const { return death_data_.life_duration(); } |
| 273 int64 square_duration() const { return death_data_.square_duration(); } | |
| 274 int AverageMsDuration() const { return death_data_.AverageMsDuration(); } | 276 int AverageMsDuration() const { return death_data_.AverageMsDuration(); } |
| 275 | 277 |
| 276 void Write(std::string* output) const; | 278 void Write(std::string* output) const; |
| 277 | 279 |
| 278 void Add(const Snapshot& other); | 280 void Add(const Snapshot& other); |
| 279 | 281 |
| 280 private: | 282 private: |
| 281 const BirthOnThread* birth_; // Includes Location and birth_thread. | 283 const BirthOnThread* birth_; // Includes Location and birth_thread. |
| 282 const ThreadData* death_thread_; | 284 const ThreadData* death_thread_; |
| 283 DeathData death_data_; | 285 DeathData death_data_; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 | 467 |
| 466 //------------------------------------------------------------------------------ | 468 //------------------------------------------------------------------------------ |
| 467 // For each thread, we have a ThreadData that stores all tracking info generated | 469 // For each thread, we have a ThreadData that stores all tracking info generated |
| 468 // on this thread. This prevents the need for locking as data accumulates. | 470 // on this thread. This prevents the need for locking as data accumulates. |
| 469 | 471 |
| 470 class BASE_EXPORT ThreadData { | 472 class BASE_EXPORT ThreadData { |
| 471 public: | 473 public: |
| 472 typedef std::map<Location, Births*> BirthMap; | 474 typedef std::map<Location, Births*> BirthMap; |
| 473 typedef std::map<const Births*, DeathData> DeathMap; | 475 typedef std::map<const Births*, DeathData> DeathMap; |
| 474 | 476 |
| 475 ThreadData(); | 477 explicit ThreadData(const char* suggested_name); |
| 476 ~ThreadData(); | 478 ~ThreadData(); |
| 477 | 479 |
| 478 // Using Thread Local Store, find the current instance for collecting data. | 480 // Using Thread Local Store, find the current instance for collecting data. |
| 479 // If an instance does not exist, construct one (and remember it for use on | 481 // If an instance does not exist, construct one (and remember it for use on |
| 480 // this thread. | 482 // this thread. |
| 481 // If shutdown has already started, and we don't yet have an instance, then | 483 // If shutdown has already started, and we don't yet have an instance, then |
| 482 // return null. | 484 // return null. |
| 483 static ThreadData* current(); | 485 static ThreadData* FactoryGet(const char* suggested_name); |
| 484 | 486 |
| 485 // For a given (unescaped) about:tracking query, develop resulting HTML, and | 487 // For a given (unescaped) about:tracking query, develop resulting HTML, and |
| 486 // append to output. | 488 // append to output. |
| 487 static void WriteHTML(const std::string& query, std::string* output); | 489 static void WriteHTML(const std::string& query, std::string* output); |
| 488 | 490 |
| 489 // For a given accumulated array of results, use the comparator to sort and | 491 // For a given accumulated array of results, use the comparator to sort and |
| 490 // subtotal, writing the results to the output. | 492 // subtotal, writing the results to the output. |
| 491 static void WriteHTMLTotalAndSubtotals( | 493 static void WriteHTMLTotalAndSubtotals( |
| 492 const DataCollector::Collection& match_array, | 494 const DataCollector::Collection& match_array, |
| 493 const Comparator& comparator, std::string* output); | 495 const Comparator& comparator, std::string* output); |
| 494 | 496 |
| 495 // In this thread's data, record a new birth. | 497 // In this thread's data, record a new birth. |
| 496 Births* TallyABirth(const Location& location); | 498 Births* TallyABirth(const Location& location); |
| 497 | 499 |
| 498 // Find a place to record a death on this thread. | 500 // Find a place to record a death on this thread. |
| 499 void TallyADeath(const Births& lifetimes, const base::TimeDelta& duration); | 501 void TallyADeath(const Births& the_birth, const base::TimeDelta& duration); |
| 500 | 502 |
| 501 // Helper methods to only tally if the current thread has tracking active. | 503 // Helper methods to only tally if the current thread has tracking active. |
| 502 // | 504 // |
| 503 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. | 505 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. |
| 504 static Births* TallyABirthIfActive(const Location& location); | 506 static Births* TallyABirthIfActive(const Location& location); |
| 505 static void TallyADeathIfActive(const Births* lifetimes, | 507 |
| 506 const base::TimeDelta& duration); | 508 // Record the end of a timed run of an object. The |the_birth| is the record |
| 509 // for the instance, the |time_posted| and |start_of_run| are times of posting |
| 510 // into a message loop queue, and of starting to perform the run of the task. |
| 511 // Implied is that the run just (Now()) ended. The current_message_loop is |
| 512 // optional, and only used in DEBUG mode (when supplied) to verify that the |
| 513 // ThreadData has a thread name that does indeed match the given loop's |
| 514 // associated thread name (in RELEASE mode, its use is compiled away). |
| 515 static void TallyADeathIfActive(const Births* the_birth, |
| 516 const base::TimeTicks& time_posted, |
| 517 const base::TimeTicks& start_of_run, |
| 518 const MessageLoop* current_message_loop); |
| 507 | 519 |
| 508 // (Thread safe) Get start of list of instances. | 520 // (Thread safe) Get start of list of instances. |
| 509 static ThreadData* first(); | 521 static ThreadData* first(); |
| 510 // Iterate through the null terminated list of instances. | 522 // Iterate through the null terminated list of instances. |
| 511 ThreadData* next() const { return next_; } | 523 ThreadData* next() const { return next_; } |
| 512 | 524 |
| 513 MessageLoop* message_loop() const { return message_loop_; } | 525 const std::string thread_name() const { return thread_name_; } |
| 514 const std::string ThreadName() const; | |
| 515 | 526 |
| 516 // Using our lock, make a copy of the specified maps. These calls may arrive | 527 // Using our lock, make a copy of the specified maps. These calls may arrive |
| 517 // from non-local threads, and are used to quickly scan data from all threads | 528 // from non-local threads, and are used to quickly scan data from all threads |
| 518 // in order to build an HTML page for about:tracking. | 529 // in order to build an HTML page for about:tracking. |
| 519 void SnapshotBirthMap(BirthMap *output) const; | 530 void SnapshotBirthMap(BirthMap *output) const; |
| 520 void SnapshotDeathMap(DeathMap *output) const; | 531 void SnapshotDeathMap(DeathMap *output) const; |
| 521 | 532 |
| 522 // Hack: asynchronously clear all birth counts and death tallies data values | 533 // Hack: asynchronously clear all birth counts and death tallies data values |
| 523 // in all ThreadData instances. The numerical (zeroing) part is done without | 534 // in all ThreadData instances. The numerical (zeroing) part is done without |
| 524 // use of a locks or atomics exchanges, and may (for int64 values) produce | 535 // use of a locks or atomics exchanges, and may (for int64 values) produce |
| 525 // bogus counts VERY rarely. | 536 // bogus counts VERY rarely. |
| 526 static void ResetAllThreadData(); | 537 static void ResetAllThreadData(); |
| 527 | 538 |
| 528 // Using our lock to protect the iteration, Clear all birth and death data. | 539 // Using our lock to protect the iteration, Clear all birth and death data. |
| 529 void Reset(); | 540 void Reset(); |
| 530 | 541 |
| 531 // Using the "known list of threads" gathered during births and deaths, the | 542 // Using the "known list of threads" gathered during births and deaths, the |
| 532 // following attempts to run the given function once all all such threads. | 543 // following attempts to run the given function once all all such threads. |
| 533 // Note that the function can only be run on threads which have a message | 544 // Note that the function can only be run on threads which have a message |
| 534 // loop! | 545 // loop! |
| 535 static void RunOnAllThreads(void (*Func)()); | 546 static void RunOnAllThreads(void (*Func)()); |
| 536 | 547 |
| 537 // Set internal status_ to either become ACTIVE, or later, to be SHUTDOWN, | 548 // Set internal status_ to either become ACTIVE, or later, to be SHUTDOWN, |
| 538 // based on argument being true or false respectively. | 549 // based on argument being true or false respectively. |
| 539 // IF tracking is not compiled in, this function will return false. | 550 // IF tracking is not compiled in, this function will return false. |
| 540 static bool StartTracking(bool status); | 551 static bool StartTracking(bool status); |
| 541 static bool IsActive(); | 552 static bool IsActive(); |
| 542 | 553 |
| 543 #ifdef OS_WIN | 554 // Provide a time function that does nothing (runs fast) when we don't have |
| 544 // WARNING: ONLY call this function when all MessageLoops are still intact for | 555 // the profiler enabled. It will generally be optimized away when it is |
| 545 // all registered threads. IF you call it later, you will crash. | 556 // ifdef'ed to be small enough (allowing the profiler to be "compiled out" of |
| 546 // Note: You don't need to call it at all, and you can wait till you are | 557 // the code). |
| 547 // single threaded (again) to do the cleanup via | 558 static base::TimeTicks Now(); |
| 548 // ShutdownSingleThreadedCleanup(). | |
| 549 // Start the teardown (shutdown) process in a multi-thread mode by disabling | |
| 550 // further additions to thread database on all threads. First it makes a | |
| 551 // local (locked) change to prevent any more threads from registering. Then | |
| 552 // it Posts a Task to all registered threads to be sure they are aware that no | |
| 553 // more accumulation can take place. | |
| 554 static void ShutdownMultiThreadTracking(); | |
| 555 #endif | |
| 556 | 559 |
| 557 // WARNING: ONLY call this function when you are running single threaded | 560 // WARNING: ONLY call this function when you are running single threaded |
| 558 // (again) and all message loops and threads have terminated. Until that | 561 // (again) and all message loops and threads have terminated. Until that |
| 559 // point some threads may still attempt to write into our data structures. | 562 // point some threads may still attempt to write into our data structures. |
| 560 // Delete recursively all data structures, starting with the list of | 563 // Delete recursively all data structures, starting with the list of |
| 561 // ThreadData instances. | 564 // ThreadData instances. |
| 562 static void ShutdownSingleThreadedCleanup(); | 565 static void ShutdownSingleThreadedCleanup(); |
| 563 | 566 |
| 564 private: | 567 private: |
| 565 // Current allowable states of the tracking system. The states always | 568 // Current allowable states of the tracking system. The states always |
| 566 // proceed towards SHUTDOWN, and never go backwards. | 569 // proceed towards SHUTDOWN, and never go backwards. |
| 567 enum Status { | 570 enum Status { |
| 568 UNINITIALIZED, | 571 UNINITIALIZED, |
| 569 ACTIVE, | 572 ACTIVE, |
| 570 SHUTDOWN, | 573 SHUTDOWN, |
| 571 }; | 574 }; |
| 572 | 575 |
| 573 #if defined(OS_WIN) | |
| 574 class ThreadSafeDownCounter; | |
| 575 class RunTheStatic; | |
| 576 #endif | |
| 577 | |
| 578 // Each registered thread is called to set status_ to SHUTDOWN. | 576 // Each registered thread is called to set status_ to SHUTDOWN. |
| 579 // This is done redundantly on every registered thread because it is not | 577 // This is done redundantly on every registered thread because it is not |
| 580 // protected by a mutex. Running on all threads guarantees we get the | 578 // protected by a mutex. Running on all threads guarantees we get the |
| 581 // notification into the memory cache of all possible threads. | 579 // notification into the memory cache of all possible threads. |
| 582 static void ShutdownDisablingFurtherTracking(); | 580 static void ShutdownDisablingFurtherTracking(); |
| 583 | 581 |
| 584 // We use thread local store to identify which ThreadData to interact with. | 582 // We use thread local store to identify which ThreadData to interact with. |
| 585 static base::ThreadLocalStorage::Slot tls_index_; | 583 static base::ThreadLocalStorage::Slot tls_index_; |
| 586 | 584 |
| 587 // Link to the most recently created instance (starts a null terminated list). | 585 // Link to the most recently created instance (starts a null terminated list). |
| 588 static ThreadData* first_; | 586 static ThreadData* first_; |
| 589 // Protection for access to first_. | 587 // Protection for access to first_. |
| 590 static base::Lock list_lock_; | 588 static base::Lock list_lock_; |
| 591 | 589 |
| 592 // We set status_ to SHUTDOWN when we shut down the tracking service. This | 590 // We set status_ to SHUTDOWN when we shut down the tracking service. This |
| 593 // setting is redundantly established by all participating threads so that we | 591 // setting is redundantly established by all participating threads so that we |
| 594 // are *guaranteed* (without locking) that all threads can "see" the status | 592 // are *guaranteed* (without locking) that all threads can "see" the status |
| 595 // and avoid additional calls into the service. | 593 // and avoid additional calls into the service. |
| 596 static Status status_; | 594 static Status status_; |
| 597 | 595 |
| 598 // Link to next instance (null terminated list). Used to globally track all | 596 // Link to next instance (null terminated list). Used to globally track all |
| 599 // registered instances (corresponds to all registered threads where we keep | 597 // registered instances (corresponds to all registered threads where we keep |
| 600 // data). | 598 // data). |
| 601 ThreadData* next_; | 599 ThreadData* next_; |
| 602 | 600 |
| 603 // The message loop where tasks needing to access this instance's private data | 601 // The name of the thread that is being recorded. If this is a worker thread, |
| 604 // should be directed. Since some threads have no message loop, some | 602 // or has no message_loop, then this string is empty. |
| 605 // instances have data that can't be (safely) modified externally. | 603 std::string thread_name_; |
| 606 MessageLoop* message_loop_; | 604 |
| 605 // A sequential number indicating when this thread context was created. If |
| 606 // the thread_name_ is empty, then this values will be used to form a unique |
| 607 // name. |
| 608 int thread_number_; |
| 607 | 609 |
| 608 // A map used on each thread to keep track of Births on this thread. | 610 // A map used on each thread to keep track of Births on this thread. |
| 609 // This map should only be accessed on the thread it was constructed on. | 611 // This map should only be accessed on the thread it was constructed on. |
| 610 // When a snapshot is needed, this structure can be locked in place for the | 612 // When a snapshot is needed, this structure can be locked in place for the |
| 611 // duration of the snapshotting activity. | 613 // duration of the snapshotting activity. |
| 612 BirthMap birth_map_; | 614 BirthMap birth_map_; |
| 613 | 615 |
| 614 // Similar to birth_map_, this records informations about death of tracked | 616 // Similar to birth_map_, this records informations about death of tracked |
| 615 // instances (i.e., when a tracked instance was destroyed on this thread). | 617 // instances (i.e., when a tracked instance was destroyed on this thread). |
| 616 // It is locked before changing, and hence other threads may access it by | 618 // It is locked before changing, and hence other threads may access it by |
| 617 // locking before reading it. | 619 // locking before reading it. |
| 618 DeathMap death_map_; | 620 DeathMap death_map_; |
| 619 | 621 |
| 620 // Lock to protect *some* access to BirthMap and DeathMap. The maps are | 622 // Lock to protect *some* access to BirthMap and DeathMap. The maps are |
| 621 // regularly read and written on this thread, but may only be read from other | 623 // regularly read and written on this thread, but may only be read from other |
| 622 // threads. To support this, we acquire this lock if we are writing from this | 624 // threads. To support this, we acquire this lock if we are writing from this |
| 623 // thread, or reading from another thread. For reading from this thread we | 625 // thread, or reading from another thread. For reading from this thread we |
| 624 // don't need a lock, as there is no potential for a conflict since the | 626 // don't need a lock, as there is no potential for a conflict since the |
| 625 // writing is only done from this thread. | 627 // writing is only done from this thread. |
| 626 mutable base::Lock lock_; | 628 mutable base::Lock lock_; |
| 627 | 629 |
| 630 // The next available thread number. This should only be accessed when the |
| 631 // list_lock_ is held. |
| 632 static int thread_number_counter; |
| 633 |
| 628 DISALLOW_COPY_AND_ASSIGN(ThreadData); | 634 DISALLOW_COPY_AND_ASSIGN(ThreadData); |
| 629 }; | 635 }; |
| 630 | 636 |
| 631 | 637 |
| 632 //------------------------------------------------------------------------------ | 638 //------------------------------------------------------------------------------ |
| 633 // Provide simple way to to start global tracking, and to tear down tracking | 639 // Provide simple way to to start global tracking, and to tear down tracking |
| 634 // when done. Note that construction and destruction of this object must be | 640 // when done. Note that construction and destruction of this object must be |
| 635 // done when running in threaded mode (before spawning a lot of threads | 641 // done when running in threaded mode (before spawning a lot of threads |
| 636 // for construction, and after shutting down all the threads for destruction). | 642 // for construction, and after shutting down all the threads for destruction). |
| 637 | 643 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 668 }; | 674 }; |
| 669 static State state_; | 675 static State state_; |
| 670 | 676 |
| 671 DISALLOW_COPY_AND_ASSIGN(AutoTracking); | 677 DISALLOW_COPY_AND_ASSIGN(AutoTracking); |
| 672 }; | 678 }; |
| 673 | 679 |
| 674 | 680 |
| 675 } // namespace tracked_objects | 681 } // namespace tracked_objects |
| 676 | 682 |
| 677 #endif // BASE_TRACKED_OBJECTS_H_ | 683 #endif // BASE_TRACKED_OBJECTS_H_ |
| OLD | NEW |