Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: base/tracked_objects.cc

Issue 2386123003: Add heap allocator usage to task profiler. (Closed)
Patch Set: Now fix snapshots, not merely unbreak them. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/tracked_objects.h ('k') | base/tracked_objects_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <limits.h> 7 #include <limits.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 9
10 #include "base/atomicops.h" 10 #include "base/atomicops.h"
11 #include "base/base_switches.h" 11 #include "base/base_switches.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/debug/leak_annotations.h" 14 #include "base/debug/leak_annotations.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/numerics/safe_conversions.h"
17 #include "base/numerics/safe_math.h"
16 #include "base/process/process_handle.h" 18 #include "base/process/process_handle.h"
17 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
18 #include "base/third_party/valgrind/memcheck.h" 20 #include "base/third_party/valgrind/memcheck.h"
19 #include "base/threading/worker_pool.h" 21 #include "base/threading/worker_pool.h"
20 #include "base/tracking_info.h" 22 #include "base/tracking_info.h"
21 #include "build/build_config.h" 23 #include "build/build_config.h"
22 24
23 using base::TimeDelta; 25 using base::TimeDelta;
24 26
25 namespace base { 27 namespace base {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 //------------------------------------------------------------------------------ 81 //------------------------------------------------------------------------------
80 // DeathData tallies durations when a death takes place. 82 // DeathData tallies durations when a death takes place.
81 83
82 DeathData::DeathData() 84 DeathData::DeathData()
83 : count_(0), 85 : count_(0),
84 sample_probability_count_(0), 86 sample_probability_count_(0),
85 run_duration_sum_(0), 87 run_duration_sum_(0),
86 queue_duration_sum_(0), 88 queue_duration_sum_(0),
87 run_duration_max_(0), 89 run_duration_max_(0),
88 queue_duration_max_(0), 90 queue_duration_max_(0),
91 alloc_ops_(0),
92 free_ops_(0),
93 allocated_bytes_(0),
94 freed_bytes_(0),
95 alloc_overhead_bytes_(0),
96 max_allocated_bytes_(0),
89 run_duration_sample_(0), 97 run_duration_sample_(0),
90 queue_duration_sample_(0), 98 queue_duration_sample_(0),
91 last_phase_snapshot_(nullptr) { 99 last_phase_snapshot_(nullptr) {}
92 }
93 100
94 DeathData::DeathData(const DeathData& other) 101 DeathData::DeathData(const DeathData& other)
95 : count_(other.count_), 102 : count_(other.count_),
96 sample_probability_count_(other.sample_probability_count_), 103 sample_probability_count_(other.sample_probability_count_),
97 run_duration_sum_(other.run_duration_sum_), 104 run_duration_sum_(other.run_duration_sum_),
98 queue_duration_sum_(other.queue_duration_sum_), 105 queue_duration_sum_(other.queue_duration_sum_),
99 run_duration_max_(other.run_duration_max_), 106 run_duration_max_(other.run_duration_max_),
100 queue_duration_max_(other.queue_duration_max_), 107 queue_duration_max_(other.queue_duration_max_),
108 alloc_ops_(other.alloc_ops_),
109 free_ops_(other.free_ops_),
110 allocated_bytes_(other.allocated_bytes_),
111 freed_bytes_(other.freed_bytes_),
112 alloc_overhead_bytes_(other.alloc_overhead_bytes_),
113 max_allocated_bytes_(other.max_allocated_bytes_),
101 run_duration_sample_(other.run_duration_sample_), 114 run_duration_sample_(other.run_duration_sample_),
102 queue_duration_sample_(other.queue_duration_sample_), 115 queue_duration_sample_(other.queue_duration_sample_),
103 last_phase_snapshot_(nullptr) { 116 last_phase_snapshot_(nullptr) {
104 // This constructor will be used by std::map when adding new DeathData values 117 // This constructor will be used by std::map when adding new DeathData values
105 // to the map. At that point, last_phase_snapshot_ is still NULL, so we don't 118 // to the map. At that point, last_phase_snapshot_ is still NULL, so we don't
106 // need to worry about ownership transfer. 119 // need to worry about ownership transfer.
107 DCHECK(other.last_phase_snapshot_ == nullptr); 120 DCHECK(other.last_phase_snapshot_ == nullptr);
108 } 121 }
109 122
110 DeathData::~DeathData() { 123 DeathData::~DeathData() {
111 while (last_phase_snapshot_) { 124 while (last_phase_snapshot_) {
112 const DeathDataPhaseSnapshot* snapshot = last_phase_snapshot_; 125 const DeathDataPhaseSnapshot* snapshot = last_phase_snapshot_;
113 last_phase_snapshot_ = snapshot->prev; 126 last_phase_snapshot_ = snapshot->prev;
114 delete snapshot; 127 delete snapshot;
115 } 128 }
116 } 129 }
117 130
118 // TODO(jar): I need to see if this macro to optimize branching is worth using. 131 // TODO(jar): I need to see if this macro to optimize branching is worth using.
119 // 132 //
120 // This macro has no branching, so it is surely fast, and is equivalent to: 133 // This macro has no branching, so it is surely fast, and is equivalent to:
121 // if (assign_it) 134 // if (assign_it)
122 // target = source; 135 // target = source;
123 // We use a macro rather than a template to force this to inline. 136 // We use a macro rather than a template to force this to inline.
124 // Related code for calculating max is discussed on the web. 137 // Related code for calculating max is discussed on the web.
125 #define CONDITIONAL_ASSIGN(assign_it, target, source) \ 138 #define CONDITIONAL_ASSIGN(assign_it, target, source) \
126 ((target) ^= ((target) ^ (source)) & -static_cast<int32_t>(assign_it)) 139 ((target) ^= ((target) ^ (source)) & -static_cast<int32_t>(assign_it))
127 140
128 void DeathData::RecordDeath(const int32_t queue_duration, 141 void DeathData::RecordDurations(const int32_t queue_duration,
129 const int32_t run_duration, 142 const int32_t run_duration,
130 const uint32_t random_number) { 143 const uint32_t random_number) {
131 // We'll just clamp at INT_MAX, but we should note this in the UI as such. 144 // We'll just clamp at INT_MAX, but we should note this in the UI as such.
132 if (count_ < INT_MAX) 145 if (count_ < INT_MAX)
133 base::subtle::NoBarrier_Store(&count_, count_ + 1); 146 base::subtle::NoBarrier_Store(&count_, count_ + 1);
134 147
135 int sample_probability_count = 148 int sample_probability_count =
136 base::subtle::NoBarrier_Load(&sample_probability_count_); 149 base::subtle::NoBarrier_Load(&sample_probability_count_);
137 if (sample_probability_count < INT_MAX) 150 if (sample_probability_count < INT_MAX)
138 ++sample_probability_count; 151 ++sample_probability_count;
139 base::subtle::NoBarrier_Store(&sample_probability_count_, 152 base::subtle::NoBarrier_Store(&sample_probability_count_,
140 sample_probability_count); 153 sample_probability_count);
(...skipping 16 matching lines...) Expand all
157 // but that should be inconsequentially likely). We ignore the fact that we 170 // but that should be inconsequentially likely). We ignore the fact that we
158 // correlated our selection of a sample to the run and queue times (i.e., we 171 // correlated our selection of a sample to the run and queue times (i.e., we
159 // used them to generate random_number). 172 // used them to generate random_number).
160 CHECK_GT(sample_probability_count, 0); 173 CHECK_GT(sample_probability_count, 0);
161 if (0 == (random_number % sample_probability_count)) { 174 if (0 == (random_number % sample_probability_count)) {
162 base::subtle::NoBarrier_Store(&queue_duration_sample_, queue_duration); 175 base::subtle::NoBarrier_Store(&queue_duration_sample_, queue_duration);
163 base::subtle::NoBarrier_Store(&run_duration_sample_, run_duration); 176 base::subtle::NoBarrier_Store(&run_duration_sample_, run_duration);
164 } 177 }
165 } 178 }
166 179
180 // This method is out of order with respect to declaration order so as to
181 // give the compiler the maximal opportunity to inline it, if appropriate.
Nico 2016/12/02 01:45:12 This comment only makes sense for compilers that d
Sigurður Ásgeirsson 2016/12/02 15:34:28 I don't know much of modern compilers - should I m
Nico 2016/12/02 15:35:57 I'd do that unless there's evidence that shows put
Sigurður Ásgeirsson 2016/12/02 16:45:11 Done.
182 void DeathData::SaturatingMemberAdd(const uint32_t addend,
183 base::subtle::Atomic32* sum) {
184 // Bail quick if no work or already saturated.
185 if (addend == 0U || *sum == INT_MAX)
186 return;
187
188 base::CheckedNumeric<int32_t> new_sum = *sum;
189 new_sum += addend;
190 base::subtle::NoBarrier_Store(sum, new_sum.ValueOrDefault(INT_MAX));
191 }
192
193 void DeathData::RecordAllocations(const uint32_t alloc_ops,
194 const uint32_t free_ops,
195 const uint32_t allocated_bytes,
196 const uint32_t freed_bytes,
197 const uint32_t alloc_overhead_bytes,
198 const uint32_t max_allocated_bytes) {
199 // Use saturating arithmetic.
200 SaturatingMemberAdd(alloc_ops, &alloc_ops_);
201 SaturatingMemberAdd(free_ops, &free_ops_);
202 SaturatingMemberAdd(allocated_bytes, &allocated_bytes_);
203 SaturatingMemberAdd(freed_bytes, &freed_bytes_);
204 SaturatingMemberAdd(alloc_overhead_bytes, &alloc_overhead_bytes_);
205
206 int32_t max = base::saturated_cast<int32_t>(max_allocated_bytes);
207 if (max > max_allocated_bytes_)
208 base::subtle::NoBarrier_Store(&max_allocated_bytes_, max);
209 }
210
167 void DeathData::OnProfilingPhaseCompleted(int profiling_phase) { 211 void DeathData::OnProfilingPhaseCompleted(int profiling_phase) {
168 // Snapshotting and storing current state. 212 // Snapshotting and storing current state.
169 last_phase_snapshot_ = new DeathDataPhaseSnapshot( 213 last_phase_snapshot_ =
170 profiling_phase, count(), run_duration_sum(), run_duration_max(), 214 new DeathDataPhaseSnapshot(profiling_phase, *this, last_phase_snapshot_);
171 run_duration_sample(), queue_duration_sum(), queue_duration_max(),
172 queue_duration_sample(), last_phase_snapshot_);
173 215
174 // Not touching fields for which a delta can be computed by comparing with a 216 // Not touching fields for which a delta can be computed by comparing with a
175 // snapshot from the previous phase. Resetting other fields. Sample values 217 // snapshot from the previous phase. Resetting other fields. Sample values
176 // will be reset upon next death recording because sample_probability_count_ 218 // will be reset upon next death recording because sample_probability_count_
177 // is set to 0. 219 // is set to 0.
178 // We avoid resetting to 0 in favor of deltas whenever possible. The reason 220 // We avoid resetting to 0 in favor of deltas whenever possible. The reason
179 // is that for incrementable fields, resetting to 0 from the snapshot thread 221 // is that for incrementable fields, resetting to 0 from the snapshot thread
180 // potentially in parallel with incrementing in the death thread may result in 222 // potentially in parallel with incrementing in the death thread may result in
181 // significant data corruption that has a potential to grow with time. Not 223 // significant data corruption that has a potential to grow with time. Not
182 // resetting incrementable fields and using deltas will cause any 224 // resetting incrementable fields and using deltas will cause any
(...skipping 19 matching lines...) Expand all
202 } 244 }
203 245
204 //------------------------------------------------------------------------------ 246 //------------------------------------------------------------------------------
205 DeathDataSnapshot::DeathDataSnapshot() 247 DeathDataSnapshot::DeathDataSnapshot()
206 : count(-1), 248 : count(-1),
207 run_duration_sum(-1), 249 run_duration_sum(-1),
208 run_duration_max(-1), 250 run_duration_max(-1),
209 run_duration_sample(-1), 251 run_duration_sample(-1),
210 queue_duration_sum(-1), 252 queue_duration_sum(-1),
211 queue_duration_max(-1), 253 queue_duration_max(-1),
212 queue_duration_sample(-1) { 254 queue_duration_sample(-1),
213 } 255 alloc_ops(-1),
256 free_ops(-1),
257 allocated_bytes(-1),
258 freed_bytes(-1),
259 alloc_overhead_bytes(-1),
260 max_allocated_bytes(-1) {}
214 261
215 DeathDataSnapshot::DeathDataSnapshot(int count, 262 DeathDataSnapshot::DeathDataSnapshot(int count,
216 int32_t run_duration_sum, 263 int32_t run_duration_sum,
217 int32_t run_duration_max, 264 int32_t run_duration_max,
218 int32_t run_duration_sample, 265 int32_t run_duration_sample,
219 int32_t queue_duration_sum, 266 int32_t queue_duration_sum,
220 int32_t queue_duration_max, 267 int32_t queue_duration_max,
221 int32_t queue_duration_sample) 268 int32_t queue_duration_sample,
269 int32_t alloc_ops,
270 int32_t free_ops,
271 int32_t allocated_bytes,
272 int32_t freed_bytes,
273 int32_t alloc_overhead_bytes,
274 int32_t max_allocated_bytes)
222 : count(count), 275 : count(count),
223 run_duration_sum(run_duration_sum), 276 run_duration_sum(run_duration_sum),
224 run_duration_max(run_duration_max), 277 run_duration_max(run_duration_max),
225 run_duration_sample(run_duration_sample), 278 run_duration_sample(run_duration_sample),
226 queue_duration_sum(queue_duration_sum), 279 queue_duration_sum(queue_duration_sum),
227 queue_duration_max(queue_duration_max), 280 queue_duration_max(queue_duration_max),
228 queue_duration_sample(queue_duration_sample) {} 281 queue_duration_sample(queue_duration_sample),
282 alloc_ops(alloc_ops),
283 free_ops(free_ops),
284 allocated_bytes(allocated_bytes),
285 freed_bytes(freed_bytes),
286 alloc_overhead_bytes(alloc_overhead_bytes),
287 max_allocated_bytes(max_allocated_bytes) {}
288
289 DeathDataSnapshot::DeathDataSnapshot(const DeathData& death_data)
290 : count(death_data.count()),
291 run_duration_sum(death_data.run_duration_sum()),
292 run_duration_max(death_data.run_duration_max()),
293 run_duration_sample(death_data.run_duration_sample()),
294 queue_duration_sum(death_data.queue_duration_sum()),
295 queue_duration_max(death_data.queue_duration_max()),
296 queue_duration_sample(death_data.queue_duration_sample()),
297 alloc_ops(death_data.alloc_ops()),
298 free_ops(death_data.free_ops()),
299 allocated_bytes(death_data.allocated_bytes()),
300 freed_bytes(death_data.freed_bytes()),
301 alloc_overhead_bytes(death_data.alloc_overhead_bytes()),
302 max_allocated_bytes(death_data.max_allocated_bytes()) {}
303
304 DeathDataSnapshot::DeathDataSnapshot(const DeathDataSnapshot& death_data) =
305 default;
229 306
230 DeathDataSnapshot::~DeathDataSnapshot() { 307 DeathDataSnapshot::~DeathDataSnapshot() {
231 } 308 }
232 309
233 DeathDataSnapshot DeathDataSnapshot::Delta( 310 DeathDataSnapshot DeathDataSnapshot::Delta(
234 const DeathDataSnapshot& older) const { 311 const DeathDataSnapshot& older) const {
235 return DeathDataSnapshot(count - older.count, 312 return DeathDataSnapshot(
236 run_duration_sum - older.run_duration_sum, 313 count - older.count, run_duration_sum - older.run_duration_sum,
237 run_duration_max, run_duration_sample, 314 run_duration_max, run_duration_sample,
238 queue_duration_sum - older.queue_duration_sum, 315 queue_duration_sum - older.queue_duration_sum, queue_duration_max,
239 queue_duration_max, queue_duration_sample); 316 queue_duration_sample, alloc_ops - older.alloc_ops,
317 free_ops - older.free_ops, allocated_bytes - older.allocated_bytes,
318 freed_bytes - older.freed_bytes,
319 alloc_overhead_bytes - older.alloc_overhead_bytes, max_allocated_bytes);
240 } 320 }
241 321
242 //------------------------------------------------------------------------------ 322 //------------------------------------------------------------------------------
243 BirthOnThread::BirthOnThread(const Location& location, 323 BirthOnThread::BirthOnThread(const Location& location,
244 const ThreadData& current) 324 const ThreadData& current)
245 : location_(location), 325 : location_(location),
246 birth_thread_(&current) { 326 birth_thread_(&current) {
247 } 327 }
248 328
249 //------------------------------------------------------------------------------ 329 //------------------------------------------------------------------------------
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 528
449 // Add births that are still active -- i.e. objects that have tallied a birth, 529 // Add births that are still active -- i.e. objects that have tallied a birth,
450 // but have not yet tallied a matching death, and hence must be either 530 // but have not yet tallied a matching death, and hence must be either
451 // running, queued up, or being held in limbo for future posting. 531 // running, queued up, or being held in limbo for future posting.
452 auto* current_phase_tasks = 532 auto* current_phase_tasks =
453 &process_data_snapshot->phased_snapshots[current_profiling_phase].tasks; 533 &process_data_snapshot->phased_snapshots[current_profiling_phase].tasks;
454 for (const auto& birth_count : birth_counts) { 534 for (const auto& birth_count : birth_counts) {
455 if (birth_count.second > 0) { 535 if (birth_count.second > 0) {
456 current_phase_tasks->push_back( 536 current_phase_tasks->push_back(
457 TaskSnapshot(BirthOnThreadSnapshot(*birth_count.first), 537 TaskSnapshot(BirthOnThreadSnapshot(*birth_count.first),
458 DeathDataSnapshot(birth_count.second, 0, 0, 0, 0, 0, 0), 538 DeathDataSnapshot(birth_count.second, 0, 0, 0, 0, 0, 0,
539 0, 0, 0, 0, 0, 0),
459 "Still_Alive")); 540 "Still_Alive"));
460 } 541 }
461 } 542 }
462 } 543 }
463 544
464 // static 545 // static
465 void ThreadData::OnProfilingPhaseCompleted(int profiling_phase) { 546 void ThreadData::OnProfilingPhaseCompleted(int profiling_phase) {
466 // Get an unchanging copy of a ThreadData list. 547 // Get an unchanging copy of a ThreadData list.
467 ThreadData* my_list = ThreadData::first(); 548 ThreadData* my_list = ThreadData::first();
468 549
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 static_cast<uint32_t>(&births - reinterpret_cast<Births*>(0)); 588 static_cast<uint32_t>(&births - reinterpret_cast<Births*>(0));
508 589
509 DeathMap::iterator it = death_map_.find(&births); 590 DeathMap::iterator it = death_map_.find(&births);
510 DeathData* death_data; 591 DeathData* death_data;
511 if (it != death_map_.end()) { 592 if (it != death_map_.end()) {
512 death_data = &it->second; 593 death_data = &it->second;
513 } else { 594 } else {
514 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now. 595 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now.
515 death_data = &death_map_[&births]; 596 death_data = &death_map_[&births];
516 } // Release lock ASAP. 597 } // Release lock ASAP.
517 death_data->RecordDeath(queue_duration, run_duration, random_number_); 598 death_data->RecordDurations(queue_duration, run_duration, random_number_);
599
600 #if BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
601 if (stopwatch.heap_tracking_enabled()) {
602 base::debug::ThreadHeapUsage heap_usage = stopwatch.heap_usage().usage();
603 // Saturate the 64 bit counts on conversion to 32 bit storage.
604 death_data->RecordAllocations(
605 base::saturated_cast<int32_t>(heap_usage.alloc_ops),
606 base::saturated_cast<int32_t>(heap_usage.free_ops),
607 base::saturated_cast<int32_t>(heap_usage.alloc_bytes),
608 base::saturated_cast<int32_t>(heap_usage.free_bytes),
609 base::saturated_cast<int32_t>(heap_usage.alloc_overhead_bytes),
610 base::saturated_cast<int32_t>(heap_usage.max_allocated_bytes));
611 }
612 #endif
518 } 613 }
519 614
520 // static 615 // static
521 Births* ThreadData::TallyABirthIfActive(const Location& location) { 616 Births* ThreadData::TallyABirthIfActive(const Location& location) {
522 if (!TrackingStatus()) 617 if (!TrackingStatus())
523 return NULL; 618 return NULL;
524 ThreadData* current_thread_data = Get(); 619 ThreadData* current_thread_data = Get();
525 if (!current_thread_data) 620 if (!current_thread_data)
526 return NULL; 621 return NULL;
527 return current_thread_data->TallyABirth(location); 622 return current_thread_data->TallyABirth(location);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 BirthMap* birth_map, 741 BirthMap* birth_map,
647 DeathsSnapshot* deaths) { 742 DeathsSnapshot* deaths) {
648 base::AutoLock lock(map_lock_); 743 base::AutoLock lock(map_lock_);
649 744
650 for (const auto& birth : birth_map_) 745 for (const auto& birth : birth_map_)
651 (*birth_map)[birth.first] = birth.second; 746 (*birth_map)[birth.first] = birth.second;
652 747
653 for (const auto& death : death_map_) { 748 for (const auto& death : death_map_) {
654 deaths->push_back(std::make_pair( 749 deaths->push_back(std::make_pair(
655 death.first, 750 death.first,
656 DeathDataPhaseSnapshot(profiling_phase, death.second.count(), 751 DeathDataPhaseSnapshot(profiling_phase, death.second,
657 death.second.run_duration_sum(),
658 death.second.run_duration_max(),
659 death.second.run_duration_sample(),
660 death.second.queue_duration_sum(),
661 death.second.queue_duration_max(),
662 death.second.queue_duration_sample(),
663 death.second.last_phase_snapshot()))); 752 death.second.last_phase_snapshot())));
664 } 753 }
665 } 754 }
666 755
667 void ThreadData::OnProfilingPhaseCompletedOnThread(int profiling_phase) { 756 void ThreadData::OnProfilingPhaseCompletedOnThread(int profiling_phase) {
668 base::AutoLock lock(map_lock_); 757 base::AutoLock lock(map_lock_);
669 758
670 for (auto& death : death_map_) { 759 for (auto& death : death_map_) {
671 death.second.OnProfilingPhaseCompleted(profiling_phase); 760 death.second.OnProfilingPhaseCompleted(profiling_phase);
672 } 761 }
(...skipping 25 matching lines...) Expand all
698 787
699 // Incarnation counter is only significant to testing, as it otherwise will 788 // Incarnation counter is only significant to testing, as it otherwise will
700 // never again change in this process. 789 // never again change in this process.
701 ++incarnation_counter_; 790 ++incarnation_counter_;
702 791
703 // The lock is not critical for setting status_, but it doesn't hurt. It also 792 // The lock is not critical for setting status_, but it doesn't hurt. It also
704 // ensures that if we have a racy initialization, that we'll bail as soon as 793 // ensures that if we have a racy initialization, that we'll bail as soon as
705 // we get the lock earlier in this method. 794 // we get the lock earlier in this method.
706 base::subtle::Release_Store(&status_, kInitialStartupState); 795 base::subtle::Release_Store(&status_, kInitialStartupState);
707 DCHECK(base::subtle::NoBarrier_Load(&status_) != UNINITIALIZED); 796 DCHECK(base::subtle::NoBarrier_Load(&status_) != UNINITIALIZED);
797
798 #if BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
799 // Make sure heap tracking is enabled ASAP if the default state is active.
800 if (kInitialStartupState == PROFILING_ACTIVE &&
801 !base::debug::ThreadHeapUsageTracker::IsHeapTrackingEnabled()) {
802 base::debug::ThreadHeapUsageTracker::EnableHeapTracking();
803 }
804 #endif // BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
708 } 805 }
709 806
710 // static 807 // static
711 void ThreadData::InitializeAndSetTrackingStatus(Status status) { 808 void ThreadData::InitializeAndSetTrackingStatus(Status status) {
712 DCHECK_GE(status, DEACTIVATED); 809 DCHECK_GE(status, DEACTIVATED);
713 DCHECK_LE(status, PROFILING_ACTIVE); 810 DCHECK_LE(status, PROFILING_ACTIVE);
714 811
715 EnsureTlsInitialization(); // No-op if already initialized. 812 EnsureTlsInitialization(); // No-op if already initialized.
716 813
717 if (status > DEACTIVATED) 814 if (status > DEACTIVATED) {
718 status = PROFILING_ACTIVE; 815 status = PROFILING_ACTIVE;
816
817 #if BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
818 if (!base::debug::ThreadHeapUsageTracker::IsHeapTrackingEnabled())
819 base::debug::ThreadHeapUsageTracker::EnableHeapTracking();
820 #endif // BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
821 }
719 base::subtle::Release_Store(&status_, status); 822 base::subtle::Release_Store(&status_, status);
720 } 823 }
721 824
722 // static 825 // static
723 ThreadData::Status ThreadData::status() { 826 ThreadData::Status ThreadData::status() {
724 return static_cast<ThreadData::Status>(base::subtle::Acquire_Load(&status_)); 827 return static_cast<ThreadData::Status>(base::subtle::Acquire_Load(&status_));
725 } 828 }
726 829
727 // static 830 // static
728 bool ThreadData::TrackingStatus() { 831 bool ThreadData::TrackingStatus() {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 //------------------------------------------------------------------------------ 919 //------------------------------------------------------------------------------
817 TaskStopwatch::TaskStopwatch() 920 TaskStopwatch::TaskStopwatch()
818 : wallclock_duration_ms_(0), 921 : wallclock_duration_ms_(0),
819 current_thread_data_(NULL), 922 current_thread_data_(NULL),
820 excluded_duration_ms_(0), 923 excluded_duration_ms_(0),
821 parent_(NULL) { 924 parent_(NULL) {
822 #if DCHECK_IS_ON() 925 #if DCHECK_IS_ON()
823 state_ = CREATED; 926 state_ = CREATED;
824 child_ = NULL; 927 child_ = NULL;
825 #endif 928 #endif
929 #if BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
930 heap_tracking_enabled_ =
931 base::debug::ThreadHeapUsageTracker::IsHeapTrackingEnabled();
932 #endif
826 } 933 }
827 934
828 TaskStopwatch::~TaskStopwatch() { 935 TaskStopwatch::~TaskStopwatch() {
829 #if DCHECK_IS_ON() 936 #if DCHECK_IS_ON()
830 DCHECK(state_ != RUNNING); 937 DCHECK(state_ != RUNNING);
831 DCHECK(child_ == NULL); 938 DCHECK(child_ == NULL);
832 #endif 939 #endif
833 } 940 }
834 941
835 void TaskStopwatch::Start() { 942 void TaskStopwatch::Start() {
836 #if DCHECK_IS_ON() 943 #if DCHECK_IS_ON()
837 DCHECK(state_ == CREATED); 944 DCHECK(state_ == CREATED);
838 state_ = RUNNING; 945 state_ = RUNNING;
839 #endif 946 #endif
840 947
841 start_time_ = ThreadData::Now(); 948 start_time_ = ThreadData::Now();
949 #if BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
950 if (heap_tracking_enabled_)
951 heap_usage_.Start();
952 #endif
842 953
843 current_thread_data_ = ThreadData::Get(); 954 current_thread_data_ = ThreadData::Get();
844 if (!current_thread_data_) 955 if (!current_thread_data_)
845 return; 956 return;
846 957
847 parent_ = current_thread_data_->current_stopwatch_; 958 parent_ = current_thread_data_->current_stopwatch_;
848 #if DCHECK_IS_ON() 959 #if DCHECK_IS_ON()
849 if (parent_) { 960 if (parent_) {
850 DCHECK(parent_->state_ == RUNNING); 961 DCHECK(parent_->state_ == RUNNING);
851 DCHECK(parent_->child_ == NULL); 962 DCHECK(parent_->child_ == NULL);
852 parent_->child_ = this; 963 parent_->child_ = this;
853 } 964 }
854 #endif 965 #endif
855 current_thread_data_->current_stopwatch_ = this; 966 current_thread_data_->current_stopwatch_ = this;
856 } 967 }
857 968
858 void TaskStopwatch::Stop() { 969 void TaskStopwatch::Stop() {
859 const TrackedTime end_time = ThreadData::Now(); 970 const TrackedTime end_time = ThreadData::Now();
860 #if DCHECK_IS_ON() 971 #if DCHECK_IS_ON()
861 DCHECK(state_ == RUNNING); 972 DCHECK(state_ == RUNNING);
862 state_ = STOPPED; 973 state_ = STOPPED;
863 DCHECK(child_ == NULL); 974 DCHECK(child_ == NULL);
864 #endif 975 #endif
976 #if BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
977 if (heap_tracking_enabled_)
978 heap_usage_.Stop(true);
979 #endif
865 980
866 if (!start_time_.is_null() && !end_time.is_null()) { 981 if (!start_time_.is_null() && !end_time.is_null()) {
867 wallclock_duration_ms_ = (end_time - start_time_).InMilliseconds(); 982 wallclock_duration_ms_ = (end_time - start_time_).InMilliseconds();
868 } 983 }
869 984
870 if (!current_thread_data_) 985 if (!current_thread_data_)
871 return; 986 return;
872 987
873 DCHECK(current_thread_data_->current_stopwatch_ == this); 988 DCHECK(current_thread_data_->current_stopwatch_ == this);
874 current_thread_data_->current_stopwatch_ = parent_; 989 current_thread_data_->current_stopwatch_ = parent_;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 #endif 1021 #endif
907 1022
908 return current_thread_data_; 1023 return current_thread_data_;
909 } 1024 }
910 1025
911 //------------------------------------------------------------------------------ 1026 //------------------------------------------------------------------------------
912 // DeathDataPhaseSnapshot 1027 // DeathDataPhaseSnapshot
913 1028
914 DeathDataPhaseSnapshot::DeathDataPhaseSnapshot( 1029 DeathDataPhaseSnapshot::DeathDataPhaseSnapshot(
915 int profiling_phase, 1030 int profiling_phase,
916 int count, 1031 const DeathData& death,
917 int32_t run_duration_sum,
918 int32_t run_duration_max,
919 int32_t run_duration_sample,
920 int32_t queue_duration_sum,
921 int32_t queue_duration_max,
922 int32_t queue_duration_sample,
923 const DeathDataPhaseSnapshot* prev) 1032 const DeathDataPhaseSnapshot* prev)
924 : profiling_phase(profiling_phase), 1033 : profiling_phase(profiling_phase), death_data(death), prev(prev) {}
925 death_data(count,
926 run_duration_sum,
927 run_duration_max,
928 run_duration_sample,
929 queue_duration_sum,
930 queue_duration_max,
931 queue_duration_sample),
932 prev(prev) {}
933 1034
934 //------------------------------------------------------------------------------ 1035 //------------------------------------------------------------------------------
935 // TaskSnapshot 1036 // TaskSnapshot
936 1037
937 TaskSnapshot::TaskSnapshot() { 1038 TaskSnapshot::TaskSnapshot() {
938 } 1039 }
939 1040
940 TaskSnapshot::TaskSnapshot(const BirthOnThreadSnapshot& birth, 1041 TaskSnapshot::TaskSnapshot(const BirthOnThreadSnapshot& birth,
941 const DeathDataSnapshot& death_data, 1042 const DeathDataSnapshot& death_data,
942 const std::string& death_thread_name) 1043 const std::string& death_thread_name)
(...skipping 28 matching lines...) Expand all
971 #endif 1072 #endif
972 } 1073 }
973 1074
974 ProcessDataSnapshot::ProcessDataSnapshot(const ProcessDataSnapshot& other) = 1075 ProcessDataSnapshot::ProcessDataSnapshot(const ProcessDataSnapshot& other) =
975 default; 1076 default;
976 1077
977 ProcessDataSnapshot::~ProcessDataSnapshot() { 1078 ProcessDataSnapshot::~ProcessDataSnapshot() {
978 } 1079 }
979 1080
980 } // namespace tracked_objects 1081 } // namespace tracked_objects
OLDNEW
« no previous file with comments | « base/tracked_objects.h ('k') | base/tracked_objects_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698