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 #include "base/tracked_objects.h" | 5 #include "base/tracked_objects.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 if (!kTrackAllTaskObjects) | 204 if (!kTrackAllTaskObjects) |
205 return; // Not compiled in. | 205 return; // Not compiled in. |
206 DCHECK(tls_index_.initialized()); | 206 DCHECK(tls_index_.initialized()); |
207 if (!thread_data) | 207 if (!thread_data) |
208 return; | 208 return; |
209 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup(); | 209 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup(); |
210 DCHECK_EQ(tls_index_.Get(), reinterpret_cast<ThreadData*>(NULL)); | 210 DCHECK_EQ(tls_index_.Get(), reinterpret_cast<ThreadData*>(NULL)); |
211 } | 211 } |
212 | 212 |
213 void ThreadData::OnThreadTerminationCleanup() const { | 213 void ThreadData::OnThreadTerminationCleanup() const { |
| 214 tls_index_.Set(NULL); |
214 if (!is_a_worker_thread_) | 215 if (!is_a_worker_thread_) |
215 return; | 216 return; |
216 base::AutoLock lock(*list_lock_); | 217 base::AutoLock lock(*list_lock_); |
217 unregistered_thread_data_pool_->push(this); | 218 unregistered_thread_data_pool_->push(this); |
218 } | 219 } |
219 | 220 |
220 // static | 221 // static |
221 void ThreadData::WriteHTML(const std::string& query, std::string* output) { | 222 void ThreadData::WriteHTML(const std::string& query, std::string* output) { |
222 if (status_ == UNINITIALIZED) | 223 if (!ThreadData::tracking_status()) |
223 return; // Not yet initialized. | 224 return; // Not yet initialized. |
224 | 225 |
225 DataCollector collected_data; // Gather data. | 226 DataCollector collected_data; // Gather data. |
226 collected_data.AddListOfLivingObjects(); // Add births that are still alive. | 227 collected_data.AddListOfLivingObjects(); // Add births that are still alive. |
227 | 228 |
228 // Data Gathering is complete. Now to sort/process/render. | 229 // Data Gathering is complete. Now to sort/process/render. |
229 DataCollector::Collection* collection = collected_data.collection(); | 230 DataCollector::Collection* collection = collected_data.collection(); |
230 | 231 |
231 // Create filtering and sort comparison object. | 232 // Create filtering and sort comparison object. |
232 Comparator comparator; | 233 Comparator comparator; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 // To avoid conflating our stats with the delay duration in a PostDelayedTask, | 399 // To avoid conflating our stats with the delay duration in a PostDelayedTask, |
399 // we identify such tasks, and replace their post_time with the time they | 400 // we identify such tasks, and replace their post_time with the time they |
400 // were scheduled (requested?) to emerge from the delayed task queue. This | 401 // were scheduled (requested?) to emerge from the delayed task queue. This |
401 // means that queueing delay for such tasks will show how long they went | 402 // means that queueing delay for such tasks will show how long they went |
402 // unserviced, after they *could* be serviced. This is the same stat as we | 403 // unserviced, after they *could* be serviced. This is the same stat as we |
403 // have for non-delayed tasks, and we consistently call it queueing delay. | 404 // have for non-delayed tasks, and we consistently call it queueing delay. |
404 TrackedTime effective_post_time = completed_task.delayed_run_time.is_null() | 405 TrackedTime effective_post_time = completed_task.delayed_run_time.is_null() |
405 ? tracked_objects::TrackedTime(completed_task.time_posted) | 406 ? tracked_objects::TrackedTime(completed_task.time_posted) |
406 : tracked_objects::TrackedTime(completed_task.delayed_run_time); | 407 : tracked_objects::TrackedTime(completed_task.delayed_run_time); |
407 | 408 |
408 // Watch out for a race where status_ is changing, and hence one or both | 409 Duration queue_duration = start_of_run - effective_post_time; |
409 // of start_of_run or end_of_run is zero. IN that case, we didn't bother to | 410 Duration run_duration = end_of_run - start_of_run; |
410 // get a time value since we "weren't tracking" and we were trying to be | |
411 // efficient by not calling for a genuine time value. For simplicity, we'll | |
412 // use a default zero duration when we can't calculate a true value. | |
413 Duration queue_duration; | |
414 Duration run_duration; | |
415 if (!start_of_run.is_null()) { | |
416 queue_duration = start_of_run - effective_post_time; | |
417 if (!end_of_run.is_null()) | |
418 run_duration = end_of_run - start_of_run; | |
419 } | |
420 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); | 411 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); |
421 } | 412 } |
422 | 413 |
423 // static | 414 // static |
424 void ThreadData::TallyRunOnWorkerThreadIfTracking( | 415 void ThreadData::TallyRunOnWorkerThreadIfTracking( |
425 const Births* birth, | 416 const Births* birth, |
426 const TrackedTime& time_posted, | 417 const TrackedTime& time_posted, |
427 const TrackedTime& start_of_run, | 418 const TrackedTime& start_of_run, |
428 const TrackedTime& end_of_run) { | 419 const TrackedTime& end_of_run) { |
429 if (!kTrackAllTaskObjects) | 420 if (!kTrackAllTaskObjects) |
(...skipping 11 matching lines...) Expand all Loading... |
441 // (since we'll use locks on TallyBirth and TallyDeath). The good news is | 432 // (since we'll use locks on TallyBirth and TallyDeath). The good news is |
442 // that the locks on TallyDeath will be *after* the worker thread has run, and | 433 // that the locks on TallyDeath will be *after* the worker thread has run, and |
443 // hence nothing will be waiting for the completion (... besides some other | 434 // hence nothing will be waiting for the completion (... besides some other |
444 // thread that might like to run). Also, the worker threads tasks are | 435 // thread that might like to run). Also, the worker threads tasks are |
445 // generally longer, and hence the cost of the lock may perchance be amortized | 436 // generally longer, and hence the cost of the lock may perchance be amortized |
446 // over the long task's lifetime. | 437 // over the long task's lifetime. |
447 ThreadData* current_thread_data = Get(); | 438 ThreadData* current_thread_data = Get(); |
448 if (!current_thread_data) | 439 if (!current_thread_data) |
449 return; | 440 return; |
450 | 441 |
451 Duration queue_duration; | 442 Duration queue_duration = start_of_run - time_posted; |
452 Duration run_duration; | 443 Duration run_duration = end_of_run - start_of_run; |
453 if (!start_of_run.is_null()) { | |
454 queue_duration = start_of_run - time_posted; | |
455 if (!end_of_run.is_null()) | |
456 run_duration = end_of_run - start_of_run; | |
457 } | |
458 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); | 444 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); |
459 } | 445 } |
460 | 446 |
461 // static | 447 // static |
462 ThreadData* ThreadData::first() { | 448 ThreadData* ThreadData::first() { |
463 base::AutoLock lock(*list_lock_); | 449 base::AutoLock lock(*list_lock_); |
464 return all_thread_data_list_head_; | 450 return all_thread_data_list_head_; |
465 } | 451 } |
466 | 452 |
467 // This may be called from another thread. | 453 // This may be called from another thread. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 return true; | 517 return true; |
532 } | 518 } |
533 | 519 |
534 // static | 520 // static |
535 bool ThreadData::tracking_status() { | 521 bool ThreadData::tracking_status() { |
536 return status_ == ACTIVE; | 522 return status_ == ACTIVE; |
537 } | 523 } |
538 | 524 |
539 // static | 525 // static |
540 TrackedTime ThreadData::Now() { | 526 TrackedTime ThreadData::Now() { |
541 if (kTrackAllTaskObjects && tracking_status()) | 527 if (!kTrackAllTaskObjects || status_ != ACTIVE) |
542 return TrackedTime::Now(); | 528 return TrackedTime(); // Super fast when disabled, or not compiled. |
543 return TrackedTime(); // Super fast when disabled, or not compiled. | 529 return TrackedTime::Now(); |
544 } | 530 } |
545 | 531 |
546 // static | 532 // static |
547 void ThreadData::ShutdownSingleThreadedCleanup() { | 533 void ThreadData::ShutdownSingleThreadedCleanup() { |
548 // This is only called from test code, where we need to cleanup so that | 534 // This is only called from test code, where we need to cleanup so that |
549 // additional tests can be run. | 535 // additional tests can be run. |
550 // We must be single threaded... but be careful anyway. | 536 // We must be single threaded... but be careful anyway. |
551 if (!InitializeAndSetTrackingStatus(false)) | 537 if (!InitializeAndSetTrackingStatus(false)) |
552 return; | 538 return; |
553 ThreadData* thread_data_list; | 539 ThreadData* thread_data_list; |
554 ThreadDataPool* final_pool; | 540 ThreadDataPool* final_pool; |
555 { | 541 { |
556 base::AutoLock lock(*list_lock_); | 542 base::AutoLock lock(*list_lock_); |
557 thread_data_list = all_thread_data_list_head_; | 543 thread_data_list = all_thread_data_list_head_; |
558 all_thread_data_list_head_ = NULL; | 544 all_thread_data_list_head_ = NULL; |
559 final_pool = unregistered_thread_data_pool_; | 545 final_pool = unregistered_thread_data_pool_; |
560 unregistered_thread_data_pool_ = NULL; | 546 unregistered_thread_data_pool_ = NULL; |
561 } | 547 } |
562 | 548 |
563 // Put most global static back in pristine shape. | |
564 thread_number_counter_ = 0; | |
565 tls_index_.Set(NULL); | |
566 status_ = UNINITIALIZED; | |
567 | |
568 // To avoid any chance of racing in unit tests, which is the only place we | |
569 // call this function, we will leak all the data structures we recovered. | |
570 // These structures could plausibly be used by other threads in earlier tests | |
571 // that are still running. | |
572 return; | |
573 | |
574 // If we wanted to cleanup (on a single thread), here is what we would do. | |
575 | |
576 if (final_pool) { | 549 if (final_pool) { |
577 // The thread_data_list contains *all* the instances, and we'll use it to | 550 // The thread_data_list contains *all* the instances, and we'll use it to |
578 // delete them. This pool has pointers to some instances, and we just | 551 // delete them. This pool has pointers to some instances, and we just |
579 // have to drop those pointers (and not do the deletes here). | 552 // have to drop those pointers (and not do the deletes here). |
580 while (!final_pool->empty()) | 553 while (!final_pool->empty()) |
581 final_pool->pop(); | 554 final_pool->pop(); |
582 delete final_pool; | 555 delete final_pool; |
583 } | 556 } |
584 | 557 |
585 // Do actual recursive delete in all ThreadData instances. | 558 // Do actual recursive delete in all ThreadData instances. |
586 while (thread_data_list) { | 559 while (thread_data_list) { |
587 ThreadData* next_thread_data = thread_data_list; | 560 ThreadData* next_thread_data = thread_data_list; |
588 thread_data_list = thread_data_list->next(); | 561 thread_data_list = thread_data_list->next(); |
589 | 562 |
590 for (BirthMap::iterator it = next_thread_data->birth_map_.begin(); | 563 for (BirthMap::iterator it = next_thread_data->birth_map_.begin(); |
591 next_thread_data->birth_map_.end() != it; ++it) | 564 next_thread_data->birth_map_.end() != it; ++it) |
592 delete it->second; // Delete the Birth Records. | 565 delete it->second; // Delete the Birth Records. |
593 next_thread_data->birth_map_.clear(); | 566 next_thread_data->birth_map_.clear(); |
594 next_thread_data->death_map_.clear(); | 567 next_thread_data->death_map_.clear(); |
595 delete next_thread_data; // Includes all Death Records. | 568 delete next_thread_data; // Includes all Death Records. |
596 } | 569 } |
| 570 // Put most global static back in pristine shape. |
| 571 thread_number_counter_ = 0; |
| 572 tls_index_.Set(NULL); |
| 573 status_ = UNINITIALIZED; |
597 } | 574 } |
598 | 575 |
599 //------------------------------------------------------------------------------ | 576 //------------------------------------------------------------------------------ |
600 // Individual 3-tuple of birth (place and thread) along with death thread, and | 577 // Individual 3-tuple of birth (place and thread) along with death thread, and |
601 // the accumulated stats for instances (DeathData). | 578 // the accumulated stats for instances (DeathData). |
602 | 579 |
603 Snapshot::Snapshot(const BirthOnThread& birth_on_thread, | 580 Snapshot::Snapshot(const BirthOnThread& birth_on_thread, |
604 const ThreadData& death_thread, | 581 const ThreadData& death_thread, |
605 const DeathData& death_data) | 582 const DeathData& death_data) |
606 : birth_(&birth_on_thread), | 583 : birth_(&birth_on_thread), |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1137 (combined_selectors_ & BIRTH_THREAD) ? "*" : | 1114 (combined_selectors_ & BIRTH_THREAD) ? "*" : |
1138 sample.birth().birth_thread()->thread_name().c_str(), | 1115 sample.birth().birth_thread()->thread_name().c_str(), |
1139 (combined_selectors_ & DEATH_THREAD) ? "*" : | 1116 (combined_selectors_ & DEATH_THREAD) ? "*" : |
1140 sample.DeathThreadName().c_str()); | 1117 sample.DeathThreadName().c_str()); |
1141 sample.birth().location().Write(!(combined_selectors_ & BIRTH_FILE), | 1118 sample.birth().location().Write(!(combined_selectors_ & BIRTH_FILE), |
1142 !(combined_selectors_ & BIRTH_FUNCTION), | 1119 !(combined_selectors_ & BIRTH_FUNCTION), |
1143 output); | 1120 output); |
1144 } | 1121 } |
1145 | 1122 |
1146 } // namespace tracked_objects | 1123 } // namespace tracked_objects |
OLD | NEW |