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

Side by Side Diff: base/tracked_objects.cc

Issue 1021053003: Delivering the FIRST_NONEMPTY_PAINT phase changing event to base/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@phase_splitting
Patch Set: Another platform-dependent error: no 'override' for virtual destructor. Created 5 years, 8 months 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
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"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 current_timing_enabled); 85 current_timing_enabled);
86 } 86 }
87 return current_timing_enabled == ENABLED_TIMING; 87 return current_timing_enabled == ENABLED_TIMING;
88 } 88 }
89 89
90 } // namespace 90 } // namespace
91 91
92 //------------------------------------------------------------------------------ 92 //------------------------------------------------------------------------------
93 // DeathData tallies durations when a death takes place. 93 // DeathData tallies durations when a death takes place.
94 94
95 DeathData::DeathData() { 95 DeathData::DeathData()
96 Clear(); 96 : count_(0),
97 sample_probability_count_(0),
98 run_duration_sum_(0),
99 queue_duration_sum_(0),
100 run_duration_max_(0),
101 queue_duration_max_(0),
102 run_duration_sample_(0),
103 queue_duration_sample_(0),
104 last_phase_snapshot_(nullptr) {
97 } 105 }
98 106
99 DeathData::DeathData(int count) { 107 DeathData::DeathData(const DeathData& other)
100 Clear(); 108 : count_(other.count_),
101 count_ = count; 109 sample_probability_count_(other.sample_probability_count_),
110 run_duration_sum_(other.run_duration_sum_),
111 queue_duration_sum_(other.queue_duration_sum_),
112 run_duration_max_(other.run_duration_max_),
113 queue_duration_max_(other.queue_duration_max_),
114 run_duration_sample_(other.run_duration_sample_),
115 queue_duration_sample_(other.queue_duration_sample_),
116 last_phase_snapshot_(nullptr) {
117 // This constructor will be used by std::map when adding new DeathData values
118 // to the map. At that point, last_phase_snapshot_ is still NULL, so we don't
119 // need to worry about ownership transfer.
120 DCHECK(other.last_phase_snapshot_ == nullptr);
Ilya Sherman 2015/04/17 02:14:26 nit: DCHECK_EQ
vadimt 2015/04/17 16:15:51 That's what I originally tried. Got an error. DCHE
121 }
122
123 DeathData::~DeathData() {
124 while (last_phase_snapshot_) {
125 DeathDataPhaseSnapshot* snapshot = last_phase_snapshot_;
126 last_phase_snapshot_ = snapshot->prev;
127 delete snapshot;
128 }
102 } 129 }
103 130
104 // 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.
105 // 132 //
106 // 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:
107 // if (assign_it) 134 // if (assign_it)
108 // target = source; 135 // target = source;
109 // 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.
110 // Related code for calculating max is discussed on the web. 137 // Related code for calculating max is discussed on the web.
111 #define CONDITIONAL_ASSIGN(assign_it, target, source) \ 138 #define CONDITIONAL_ASSIGN(assign_it, target, source) \
112 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it)) 139 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it))
113 140
114 void DeathData::RecordDeath(const int32 queue_duration, 141 void DeathData::RecordDeath(const int32 queue_duration,
115 const int32 run_duration, 142 const int32 run_duration,
116 const uint32 random_number) { 143 const uint32 random_number) {
117 // 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.
118 if (count_ < INT_MAX) 145 if (count_ < INT_MAX)
119 ++count_; 146 ++count_;
147 if (sample_probability_count_ < INT_MAX)
148 ++sample_probability_count_;
120 queue_duration_sum_ += queue_duration; 149 queue_duration_sum_ += queue_duration;
121 run_duration_sum_ += run_duration; 150 run_duration_sum_ += run_duration;
122 151
123 if (queue_duration_max_ < queue_duration) 152 if (queue_duration_max_ < queue_duration)
124 queue_duration_max_ = queue_duration; 153 queue_duration_max_ = queue_duration;
125 if (run_duration_max_ < run_duration) 154 if (run_duration_max_ < run_duration)
126 run_duration_max_ = run_duration; 155 run_duration_max_ = run_duration;
127 156
128 // Take a uniformly distributed sample over all durations ever supplied. 157 // Take a uniformly distributed sample over all durations ever supplied during
129 // The probability that we (instead) use this new sample is 1/count_. This 158 // the current profiling phase.
130 // results in a completely uniform selection of the sample (at least when we 159 // The probability that we (instead) use this new sample is
131 // don't clamp count_... but that should be inconsequentially likely). 160 // 1/sample_probability_count_. This results in a completely uniform selection
132 // We ignore the fact that we correlated our selection of a sample to the run 161 // of the sample (at least when we don't clamp sample_probability_count_...
133 // and queue times (i.e., we used them to generate random_number). 162 // but that should be inconsequentially likely). We ignore the fact that we
134 CHECK_GT(count_, 0); 163 // correlated our selection of a sample to the run and queue times (i.e., we
135 if (0 == (random_number % count_)) { 164 // used them to generate random_number).
165 CHECK_GT(sample_probability_count_, 0);
166 if (0 == (random_number % sample_probability_count_)) {
136 queue_duration_sample_ = queue_duration; 167 queue_duration_sample_ = queue_duration;
137 run_duration_sample_ = run_duration; 168 run_duration_sample_ = run_duration;
138 } 169 }
139 } 170 }
140 171
141 int DeathData::count() const { return count_; } 172 int DeathData::count() const { return count_; }
142 173
143 int32 DeathData::run_duration_sum() const { return run_duration_sum_; } 174 int32 DeathData::run_duration_sum() const { return run_duration_sum_; }
144 175
145 int32 DeathData::run_duration_max() const { return run_duration_max_; } 176 int32 DeathData::run_duration_max() const { return run_duration_max_; }
146 177
147 int32 DeathData::run_duration_sample() const { 178 int32 DeathData::run_duration_sample() const {
148 return run_duration_sample_; 179 return run_duration_sample_;
149 } 180 }
150 181
151 int32 DeathData::queue_duration_sum() const { 182 int32 DeathData::queue_duration_sum() const {
152 return queue_duration_sum_; 183 return queue_duration_sum_;
153 } 184 }
154 185
155 int32 DeathData::queue_duration_max() const { 186 int32 DeathData::queue_duration_max() const {
156 return queue_duration_max_; 187 return queue_duration_max_;
157 } 188 }
158 189
159 int32 DeathData::queue_duration_sample() const { 190 int32 DeathData::queue_duration_sample() const {
160 return queue_duration_sample_; 191 return queue_duration_sample_;
161 } 192 }
162 193
163 void DeathData::Clear() { 194 DeathDataPhaseSnapshot* DeathData::last_phase_snapshot() const {
164 count_ = 0; 195 return last_phase_snapshot_;
165 run_duration_sum_ = 0; 196 }
Ilya Sherman 2015/04/17 02:14:26 nit: Hmm, all of these accessors should be inlined
vadimt 2015/04/17 16:15:51 Will do.
197
198 void DeathData::OnProfilingPhaseCompleted(int profiling_phase) {
199 // Snapshotting and storing current state.
200 last_phase_snapshot_ = new DeathDataPhaseSnapshot(
201 profiling_phase, count_, run_duration_sum_, run_duration_max_,
202 run_duration_sample_, queue_duration_sum_, queue_duration_max_,
203 queue_duration_sample_, last_phase_snapshot_);
Ilya Sherman 2015/04/17 02:14:26 Man, I really disagree with Alexei about not just
vadimt 2015/04/17 16:15:51 Will do.
204
205 // Not touching fields for which a delta can be computed by comparing with a
206 // snapshot from previos phase. Resetting other fields. Sample values will be
Ilya Sherman 2015/04/17 02:14:26 nit: "from previos" -> "from the previous"
vadimt 2015/04/17 16:15:51 Done.
207 // reset upon next death recording because sample_probability_count_ is set to
208 // 0.
209 // We avoid resetting to 0 in favor of deltas whenever possible. The reason is
210 // that for incrementable fields, resetting to 0 from the snapshot thread
211 // potentially in parallel with incrementing in the death thread may result in
212 // significant data corruption that has a potential to grow with time. Not
213 // resetting incrementable fields and using deltas will cause any
214 // off-by-little corruptions to be likely fixed at the next snapshot.
215 // The max values are not incrementable, and cannot be deduced using deltas
216 // for a given phase. Hence, we have to reset them to 0. But the potential
217 // damage is limited to getting the previous phase's max to apply for the next
218 // phase, and the error doesn't have a potential to keep growing with new
219 // resets.
220 // sample_probability_count_ is incrementable, but must be reset to 0 at the
221 // phase end, so that we start a new uniformly randomized sample selection
222 // after the reset. Corruptions due to race conditions are possible, but the
223 // damage is limited to selecting a wrong sample, which is not something that
224 // can cause accumulating or cascading effects.
225 // If there were no corruptions caused by race conditions, we never send a
226 // sample for the previous phase in the next phase's snapshot because
227 // ThreadData::SnapshotExecutedTasks doesn't send deltas with 0 count.
228 sample_probability_count_ = 0;
166 run_duration_max_ = 0; 229 run_duration_max_ = 0;
167 run_duration_sample_ = 0;
168 queue_duration_sum_ = 0;
169 queue_duration_max_ = 0; 230 queue_duration_max_ = 0;
170 queue_duration_sample_ = 0;
171 } 231 }
172 232
173 //------------------------------------------------------------------------------ 233 //------------------------------------------------------------------------------
174 DeathDataSnapshot::DeathDataSnapshot() 234 DeathDataSnapshot::DeathDataSnapshot()
175 : count(-1), 235 : count(-1),
176 run_duration_sum(-1), 236 run_duration_sum(-1),
177 run_duration_max(-1), 237 run_duration_max(-1),
178 run_duration_sample(-1), 238 run_duration_sample(-1),
179 queue_duration_sum(-1), 239 queue_duration_sum(-1),
180 queue_duration_max(-1), 240 queue_duration_max(-1),
181 queue_duration_sample(-1) { 241 queue_duration_sample(-1) {
182 } 242 }
183 243
184 DeathDataSnapshot::DeathDataSnapshot( 244 DeathDataSnapshot::DeathDataSnapshot(int count,
185 const tracked_objects::DeathData& death_data) 245 int32 run_duration_sum,
186 : count(death_data.count()), 246 int32 run_duration_max,
187 run_duration_sum(death_data.run_duration_sum()), 247 int32 run_duration_sample,
188 run_duration_max(death_data.run_duration_max()), 248 int32 queue_duration_sum,
189 run_duration_sample(death_data.run_duration_sample()), 249 int32 queue_duration_max,
190 queue_duration_sum(death_data.queue_duration_sum()), 250 int32 queue_duration_sample)
191 queue_duration_max(death_data.queue_duration_max()), 251 : count(count),
192 queue_duration_sample(death_data.queue_duration_sample()) { 252 run_duration_sum(run_duration_sum),
253 run_duration_max(run_duration_max),
254 run_duration_sample(run_duration_sample),
255 queue_duration_sum(queue_duration_sum),
256 queue_duration_max(queue_duration_max),
257 queue_duration_sample(queue_duration_sample) {
193 } 258 }
194 259
195 DeathDataSnapshot::~DeathDataSnapshot() { 260 DeathDataSnapshot::~DeathDataSnapshot() {
196 } 261 }
197 262
263 DeathDataSnapshot DeathDataSnapshot::Delta(
264 const DeathDataSnapshot& older) const {
265 return DeathDataSnapshot(count - older.count,
266 run_duration_sum - older.run_duration_sum,
267 run_duration_max, run_duration_sample,
268 queue_duration_sum - older.queue_duration_sum,
269 queue_duration_max, queue_duration_sample);
270 }
271
198 //------------------------------------------------------------------------------ 272 //------------------------------------------------------------------------------
199 BirthOnThread::BirthOnThread(const Location& location, 273 BirthOnThread::BirthOnThread(const Location& location,
200 const ThreadData& current) 274 const ThreadData& current)
201 : location_(location), 275 : location_(location),
202 birth_thread_(&current) { 276 birth_thread_(&current) {
203 } 277 }
204 278
205 //------------------------------------------------------------------------------ 279 //------------------------------------------------------------------------------
206 BirthOnThreadSnapshot::BirthOnThreadSnapshot() { 280 BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
207 } 281 }
208 282
209 BirthOnThreadSnapshot::BirthOnThreadSnapshot( 283 BirthOnThreadSnapshot::BirthOnThreadSnapshot(const BirthOnThread& birth)
210 const tracked_objects::BirthOnThread& birth)
211 : location(birth.location()), 284 : location(birth.location()),
212 thread_name(birth.birth_thread()->thread_name()) { 285 thread_name(birth.birth_thread()->thread_name()) {
213 } 286 }
214 287
215 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() { 288 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
216 } 289 }
217 290
218 //------------------------------------------------------------------------------ 291 //------------------------------------------------------------------------------
219 Births::Births(const Location& location, const ThreadData& current) 292 Births::Births(const Location& location, const ThreadData& current)
220 : BirthOnThread(location, current), 293 : BirthOnThread(location, current),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 ThreadData* ThreadData::all_thread_data_list_head_ = NULL; 330 ThreadData* ThreadData::all_thread_data_list_head_ = NULL;
258 331
259 // static 332 // static
260 ThreadData* ThreadData::first_retired_worker_ = NULL; 333 ThreadData* ThreadData::first_retired_worker_ = NULL;
261 334
262 // static 335 // static
263 base::LazyInstance<base::Lock>::Leaky 336 base::LazyInstance<base::Lock>::Leaky
264 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER; 337 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER;
265 338
266 // static 339 // static
340 base::LazyInstance<base::ThreadChecker>::Leaky
341 ThreadData::snapshot_thread_checker_ = LAZY_INSTANCE_INITIALIZER;
342
343 // static
267 ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED; 344 ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
268 345
269 ThreadData::ThreadData(const std::string& suggested_name) 346 ThreadData::ThreadData(const std::string& suggested_name)
270 : next_(NULL), 347 : next_(NULL),
271 next_retired_worker_(NULL), 348 next_retired_worker_(NULL),
272 worker_thread_number_(0), 349 worker_thread_number_(0),
273 incarnation_count_for_pool_(-1), 350 incarnation_count_for_pool_(-1),
274 current_stopwatch_(NULL) { 351 current_stopwatch_(NULL) {
275 DCHECK_GE(suggested_name.size(), 0u); 352 DCHECK_GE(suggested_name.size(), 0u);
276 thread_name_ = suggested_name; 353 thread_name_ = suggested_name;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 return; 455 return;
379 } 456 }
380 // We must NOT do any allocations during this callback. 457 // We must NOT do any allocations during this callback.
381 // Using the simple linked lists avoids all allocations. 458 // Using the simple linked lists avoids all allocations.
382 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); 459 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL));
383 this->next_retired_worker_ = first_retired_worker_; 460 this->next_retired_worker_ = first_retired_worker_;
384 first_retired_worker_ = this; 461 first_retired_worker_ = this;
385 } 462 }
386 463
387 // static 464 // static
388 void ThreadData::Snapshot(ProcessDataSnapshot* process_data_snapshot) { 465 void ThreadData::Snapshot(int current_profiling_phase,
389 ThreadData::SnapshotCurrentPhase( 466 ProcessDataSnapshot* process_data_snapshot) {
390 &process_data_snapshot->phased_process_data_snapshots[0]); 467 DCHECK(snapshot_thread_checker_.Get().CalledOnValidThread());
468
469 // Get an unchanging copy of a ThreadData list.
470 ThreadData* my_list = ThreadData::first();
471
472 // Gather data serially.
473 // This hackish approach *can* get some slightly corrupt tallies, as we are
474 // grabbing values without the protection of a lock, but it has the advantage
475 // of working even with threads that don't have message loops. If a user
476 // sees any strangeness, they can always just run their stats gathering a
477 // second time.
478 BirthCountMap birth_counts;
479
Ilya Sherman 2015/04/17 02:14:26 nit: Please omit this newline.
vadimt 2015/04/17 16:15:51 Done.
480 for (ThreadData* thread_data = my_list; thread_data;
481 thread_data = thread_data->next()) {
482 thread_data->SnapshotExecutedTasks(current_profiling_phase,
483 &process_data_snapshot->phased_snapshots,
484 &birth_counts);
485 }
486
487 // Add births that are still active -- i.e. objects that have tallied a birth,
488 // but have not yet tallied a matching death, and hence must be either
489 // running, queued up, or being held in limbo for future posting.
490 auto current_phase_tasks =
491 &process_data_snapshot->phased_snapshots[current_profiling_phase].tasks;
492 for (const auto& birth_count : birth_counts) {
493 if (birth_count.second > 0) {
494 current_phase_tasks->push_back(
495 TaskSnapshot(BirthOnThreadSnapshot(*birth_count.first),
496 DeathDataSnapshot(birth_count.second, 0, 0, 0, 0, 0, 0),
497 "Still_Alive"));
498 }
499 }
500 }
501
502 // static
503 void ThreadData::OnProfilingPhaseCompleted(int profiling_phase) {
504 DCHECK(snapshot_thread_checker_.Get().CalledOnValidThread());
505 // Get an unchanging copy of a ThreadData list.
506 ThreadData* my_list = ThreadData::first();
507
508 // Add snapshots for all instances of death data in all threads serially.
509 // This hackish approach *can* get some slightly corrupt tallies, as we are
510 // grabbing values without the protection of a lock, but it has the advantage
511 // of working even with threads that don't have message loops. Any corruption
512 // shouldn't cause "cascading damage" to anything else (in later phases).
513 for (ThreadData* thread_data = my_list; thread_data;
514 thread_data = thread_data->next()) {
515 thread_data->OnProfilingPhaseCompletedOnThread(profiling_phase);
516 }
391 } 517 }
392 518
393 Births* ThreadData::TallyABirth(const Location& location) { 519 Births* ThreadData::TallyABirth(const Location& location) {
394 BirthMap::iterator it = birth_map_.find(location); 520 BirthMap::iterator it = birth_map_.find(location);
395 Births* child; 521 Births* child;
396 if (it != birth_map_.end()) { 522 if (it != birth_map_.end()) {
397 child = it->second; 523 child = it->second;
398 child->RecordBirth(); 524 child->RecordBirth();
399 } else { 525 } else {
400 child = new Births(location, *this); // Leak this. 526 child = new Births(location, *this); // Leak this.
(...skipping 11 matching lines...) Expand all
412 // Lock since the map may get relocated now, and other threads sometimes 538 // Lock since the map may get relocated now, and other threads sometimes
413 // snapshot it (but they lock before copying it). 539 // snapshot it (but they lock before copying it).
414 base::AutoLock lock(map_lock_); 540 base::AutoLock lock(map_lock_);
415 parent_child_set_.insert(pair); 541 parent_child_set_.insert(pair);
416 } 542 }
417 } 543 }
418 544
419 return child; 545 return child;
420 } 546 }
421 547
422 void ThreadData::TallyADeath(const Births& birth, 548 void ThreadData::TallyADeath(const Births& births,
423 int32 queue_duration, 549 int32 queue_duration,
424 const TaskStopwatch& stopwatch) { 550 const TaskStopwatch& stopwatch) {
425 int32 run_duration = stopwatch.RunDurationMs(); 551 int32 run_duration = stopwatch.RunDurationMs();
426 552
427 // Stir in some randomness, plus add constant in case durations are zero. 553 // Stir in some randomness, plus add constant in case durations are zero.
428 const uint32 kSomePrimeNumber = 2147483647; 554 const uint32 kSomePrimeNumber = 2147483647;
429 random_number_ += queue_duration + run_duration + kSomePrimeNumber; 555 random_number_ += queue_duration + run_duration + kSomePrimeNumber;
430 // An address is going to have some randomness to it as well ;-). 556 // An address is going to have some randomness to it as well ;-).
431 random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0)); 557 random_number_ ^= static_cast<uint32>(&births - reinterpret_cast<Births*>(0));
432 558
433 // We don't have queue durations without OS timer. OS timer is automatically 559 // We don't have queue durations without OS timer. OS timer is automatically
434 // used for task-post-timing, so the use of an alternate timer implies all 560 // used for task-post-timing, so the use of an alternate timer implies all
435 // queue times are invalid, unless it was explicitly said that we can trust 561 // queue times are invalid, unless it was explicitly said that we can trust
436 // the alternate timer. 562 // the alternate timer.
437 if (kAllowAlternateTimeSourceHandling && 563 if (kAllowAlternateTimeSourceHandling &&
438 now_function_ && 564 now_function_ &&
439 !now_function_is_time_) { 565 !now_function_is_time_) {
440 queue_duration = 0; 566 queue_duration = 0;
441 } 567 }
442 568
443 DeathMap::iterator it = death_map_.find(&birth); 569 DeathMap::iterator it = death_map_.find(&births);
444 DeathData* death_data; 570 DeathData* death_data;
445 if (it != death_map_.end()) { 571 if (it != death_map_.end()) {
446 death_data = &it->second; 572 death_data = &it->second;
447 } else { 573 } else {
448 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now. 574 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now.
449 death_data = &death_map_[&birth]; 575 death_data = &death_map_[&births];
450 } // Release lock ASAP. 576 } // Release lock ASAP.
451 death_data->RecordDeath(queue_duration, run_duration, random_number_); 577 death_data->RecordDeath(queue_duration, run_duration, random_number_);
452 578
453 if (!kTrackParentChildLinks) 579 if (!kTrackParentChildLinks)
454 return; 580 return;
455 if (!parent_stack_.empty()) { // We might get turned off. 581 if (!parent_stack_.empty()) { // We might get turned off.
456 DCHECK_EQ(parent_stack_.top(), &birth); 582 DCHECK_EQ(parent_stack_.top(), &births);
457 parent_stack_.pop(); 583 parent_stack_.pop();
458 } 584 }
459 } 585 }
460 586
461 // static 587 // static
462 Births* ThreadData::TallyABirthIfActive(const Location& location) { 588 Births* ThreadData::TallyABirthIfActive(const Location& location) {
463 if (!TrackingStatus()) 589 if (!TrackingStatus())
464 return NULL; 590 return NULL;
465 ThreadData* current_thread_data = Get(); 591 ThreadData* current_thread_data = Get();
466 if (!current_thread_data) 592 if (!current_thread_data)
467 return NULL; 593 return NULL;
468 return current_thread_data->TallyABirth(location); 594 return current_thread_data->TallyABirth(location);
469 } 595 }
470 596
471 // static 597 // static
472 void ThreadData::TallyRunOnNamedThreadIfTracking( 598 void ThreadData::TallyRunOnNamedThreadIfTracking(
473 const base::TrackingInfo& completed_task, 599 const base::TrackingInfo& completed_task,
474 const TaskStopwatch& stopwatch) { 600 const TaskStopwatch& stopwatch) {
475 // Even if we have been DEACTIVATED, we will process any pending births so 601 // Even if we have been DEACTIVATED, we will process any pending births so
476 // that our data structures (which counted the outstanding births) remain 602 // that our data structures (which counted the outstanding births) remain
477 // consistent. 603 // consistent.
478 const Births* birth = completed_task.birth_tally; 604 const Births* births = completed_task.birth_tally;
479 if (!birth) 605 if (!births)
480 return; 606 return;
481 ThreadData* current_thread_data = stopwatch.GetThreadData(); 607 ThreadData* current_thread_data = stopwatch.GetThreadData();
482 if (!current_thread_data) 608 if (!current_thread_data)
483 return; 609 return;
484 610
485 // Watch out for a race where status_ is changing, and hence one or both 611 // Watch out for a race where status_ is changing, and hence one or both
486 // of start_of_run or end_of_run is zero. In that case, we didn't bother to 612 // of start_of_run or end_of_run is zero. In that case, we didn't bother to
487 // get a time value since we "weren't tracking" and we were trying to be 613 // get a time value since we "weren't tracking" and we were trying to be
488 // efficient by not calling for a genuine time value. For simplicity, we'll 614 // efficient by not calling for a genuine time value. For simplicity, we'll
489 // use a default zero duration when we can't calculate a true value. 615 // use a default zero duration when we can't calculate a true value.
490 TrackedTime start_of_run = stopwatch.StartTime(); 616 TrackedTime start_of_run = stopwatch.StartTime();
491 int32 queue_duration = 0; 617 int32 queue_duration = 0;
492 if (!start_of_run.is_null()) { 618 if (!start_of_run.is_null()) {
493 queue_duration = (start_of_run - completed_task.EffectiveTimePosted()) 619 queue_duration = (start_of_run - completed_task.EffectiveTimePosted())
494 .InMilliseconds(); 620 .InMilliseconds();
495 } 621 }
496 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); 622 current_thread_data->TallyADeath(*births, queue_duration, stopwatch);
497 } 623 }
498 624
499 // static 625 // static
500 void ThreadData::TallyRunOnWorkerThreadIfTracking( 626 void ThreadData::TallyRunOnWorkerThreadIfTracking(
501 const Births* birth, 627 const Births* births,
502 const TrackedTime& time_posted, 628 const TrackedTime& time_posted,
503 const TaskStopwatch& stopwatch) { 629 const TaskStopwatch& stopwatch) {
504 // Even if we have been DEACTIVATED, we will process any pending births so 630 // Even if we have been DEACTIVATED, we will process any pending births so
505 // that our data structures (which counted the outstanding births) remain 631 // that our data structures (which counted the outstanding births) remain
506 // consistent. 632 // consistent.
507 if (!birth) 633 if (!births)
508 return; 634 return;
509 635
510 // TODO(jar): Support the option to coalesce all worker-thread activity under 636 // TODO(jar): Support the option to coalesce all worker-thread activity under
511 // one ThreadData instance that uses locks to protect *all* access. This will 637 // one ThreadData instance that uses locks to protect *all* access. This will
512 // reduce memory (making it provably bounded), but run incrementally slower 638 // reduce memory (making it provably bounded), but run incrementally slower
513 // (since we'll use locks on TallyABirth and TallyADeath). The good news is 639 // (since we'll use locks on TallyABirth and TallyADeath). The good news is
514 // that the locks on TallyADeath will be *after* the worker thread has run, 640 // that the locks on TallyADeath will be *after* the worker thread has run,
515 // and hence nothing will be waiting for the completion (... besides some 641 // and hence nothing will be waiting for the completion (... besides some
516 // other thread that might like to run). Also, the worker threads tasks are 642 // other thread that might like to run). Also, the worker threads tasks are
517 // generally longer, and hence the cost of the lock may perchance be amortized 643 // generally longer, and hence the cost of the lock may perchance be amortized
518 // over the long task's lifetime. 644 // over the long task's lifetime.
519 ThreadData* current_thread_data = stopwatch.GetThreadData(); 645 ThreadData* current_thread_data = stopwatch.GetThreadData();
520 if (!current_thread_data) 646 if (!current_thread_data)
521 return; 647 return;
522 648
523 TrackedTime start_of_run = stopwatch.StartTime(); 649 TrackedTime start_of_run = stopwatch.StartTime();
524 int32 queue_duration = 0; 650 int32 queue_duration = 0;
525 if (!start_of_run.is_null()) { 651 if (!start_of_run.is_null()) {
526 queue_duration = (start_of_run - time_posted).InMilliseconds(); 652 queue_duration = (start_of_run - time_posted).InMilliseconds();
527 } 653 }
528 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); 654 current_thread_data->TallyADeath(*births, queue_duration, stopwatch);
529 } 655 }
530 656
531 // static 657 // static
532 void ThreadData::TallyRunInAScopedRegionIfTracking( 658 void ThreadData::TallyRunInAScopedRegionIfTracking(
533 const Births* birth, 659 const Births* births,
534 const TaskStopwatch& stopwatch) { 660 const TaskStopwatch& stopwatch) {
535 // Even if we have been DEACTIVATED, we will process any pending births so 661 // Even if we have been DEACTIVATED, we will process any pending births so
536 // that our data structures (which counted the outstanding births) remain 662 // that our data structures (which counted the outstanding births) remain
537 // consistent. 663 // consistent.
538 if (!birth) 664 if (!births)
539 return; 665 return;
540 666
541 ThreadData* current_thread_data = stopwatch.GetThreadData(); 667 ThreadData* current_thread_data = stopwatch.GetThreadData();
542 if (!current_thread_data) 668 if (!current_thread_data)
543 return; 669 return;
544 670
545 int32 queue_duration = 0; 671 int32 queue_duration = 0;
546 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); 672 current_thread_data->TallyADeath(*births, queue_duration, stopwatch);
547 }
548
549 // static
550 void ThreadData::SnapshotAllExecutedTasks(
551 ProcessDataPhaseSnapshot* process_data_phase,
552 BirthCountMap* birth_counts) {
553 // Get an unchanging copy of a ThreadData list.
554 ThreadData* my_list = ThreadData::first();
555
556 // Gather data serially.
557 // This hackish approach *can* get some slighly corrupt tallies, as we are
558 // grabbing values without the protection of a lock, but it has the advantage
559 // of working even with threads that don't have message loops. If a user
560 // sees any strangeness, they can always just run their stats gathering a
561 // second time.
562 for (ThreadData* thread_data = my_list;
563 thread_data;
564 thread_data = thread_data->next()) {
565 thread_data->SnapshotExecutedTasks(process_data_phase, birth_counts);
566 }
567 }
568
569 // static
570 void ThreadData::SnapshotCurrentPhase(
571 ProcessDataPhaseSnapshot* process_data_phase) {
572 // Add births that have run to completion to |collected_data|.
573 // |birth_counts| tracks the total number of births recorded at each location
574 // for which we have not seen a death count.
575 BirthCountMap birth_counts;
576 ThreadData::SnapshotAllExecutedTasks(process_data_phase, &birth_counts);
577
578 // Add births that are still active -- i.e. objects that have tallied a birth,
579 // but have not yet tallied a matching death, and hence must be either
580 // running, queued up, or being held in limbo for future posting.
581 for (const auto& birth_count : birth_counts) {
582 if (birth_count.second > 0) {
583 process_data_phase->tasks.push_back(TaskSnapshot(
584 *birth_count.first, DeathData(birth_count.second), "Still_Alive"));
585 }
586 }
587 } 673 }
588 674
589 void ThreadData::SnapshotExecutedTasks( 675 void ThreadData::SnapshotExecutedTasks(
590 ProcessDataPhaseSnapshot* process_data_phase, 676 int current_profiling_phase,
677 PhasedProcessDataSnapshotMap* phased_snapshots,
591 BirthCountMap* birth_counts) { 678 BirthCountMap* birth_counts) {
592 // Get copy of data, so that the data will not change during the iterations 679 // Get copy of data, so that the data will not change during the iterations
593 // and processing. 680 // and processing.
594 ThreadData::BirthMap birth_map; 681 BirthMap birth_map;
595 ThreadData::DeathMap death_map; 682 DeathsSnapshot deaths;
596 ThreadData::ParentChildSet parent_child_set; 683 ParentChildSet parent_child_set;
597 SnapshotMaps(&birth_map, &death_map, &parent_child_set); 684 SnapshotMaps(current_profiling_phase, &birth_map, &deaths, &parent_child_set);
598
599 for (const auto& death : death_map) {
600 process_data_phase->tasks.push_back(
601 TaskSnapshot(*death.first, death.second, thread_name()));
602 (*birth_counts)[death.first] -= death.first->birth_count();
603 }
604 685
605 for (const auto& birth : birth_map) { 686 for (const auto& birth : birth_map) {
606 (*birth_counts)[birth.second] += birth.second->birth_count(); 687 (*birth_counts)[birth.second] += birth.second->birth_count();
607 } 688 }
608 689
609 if (!kTrackParentChildLinks) 690 for (const auto& death : deaths) {
610 return; 691 (*birth_counts)[death.first] -= death.first->birth_count();
611 692
612 for (const auto& parent_child : parent_child_set) { 693 // For the current death data, walk through all its snapshots, starting from
613 process_data_phase->descendants.push_back( 694 // the current one, then from the previous profiling phase etc., and for
614 ParentChildPairSnapshot(parent_child)); 695 // each snapshot calculate the delta between the snapshot and the previous
696 // phase, if any. Store the deltas in the result.
697 for (const DeathDataPhaseSnapshot* phase = &death.second; phase;
698 phase = phase->prev) {
699 const DeathDataSnapshot& death_data =
700 phase->prev ? phase->death_data.Delta(phase->prev->death_data)
701 : phase->death_data;
702
703 if (death_data.count > 0) {
704 (*phased_snapshots)[phase->profiling_phase].tasks.push_back(
705 TaskSnapshot(BirthOnThreadSnapshot(*death.first), death_data,
706 thread_name()));
707 }
708 }
615 } 709 }
616 } 710 }
617 711
618 // This may be called from another thread. 712 // This may be called from another thread.
619 void ThreadData::SnapshotMaps(BirthMap* birth_map, 713 void ThreadData::SnapshotMaps(int profiling_phase,
620 DeathMap* death_map, 714 BirthMap* birth_map,
715 DeathsSnapshot* deaths,
621 ParentChildSet* parent_child_set) { 716 ParentChildSet* parent_child_set) {
622 base::AutoLock lock(map_lock_); 717 base::AutoLock lock(map_lock_);
718
623 for (const auto& birth : birth_map_) 719 for (const auto& birth : birth_map_)
624 (*birth_map)[birth.first] = birth.second; 720 (*birth_map)[birth.first] = birth.second;
625 for (const auto& death : death_map_) 721
626 (*death_map)[death.first] = death.second; 722 for (const auto& death : death_map_) {
723 deaths->push_back(DeathsSnapshot::value_type(
724 death.first,
725 DeathDataPhaseSnapshot(profiling_phase, death.second.count(),
726 death.second.run_duration_sum(),
727 death.second.run_duration_max(),
728 death.second.run_duration_sample(),
729 death.second.queue_duration_sum(),
730 death.second.queue_duration_max(),
731 death.second.queue_duration_sample(),
732 death.second.last_phase_snapshot())));
733 }
627 734
628 if (!kTrackParentChildLinks) 735 if (!kTrackParentChildLinks)
629 return; 736 return;
630 737
631 for (const auto& parent_child : parent_child_set_) 738 for (const auto& parent_child : parent_child_set_)
632 parent_child_set->insert(parent_child); 739 parent_child_set->insert(parent_child);
633 } 740 }
634 741
742 void ThreadData::OnProfilingPhaseCompletedOnThread(int profiling_phase) {
743 base::AutoLock lock(map_lock_);
744
745 for (auto& death : death_map_) {
746 death.second.OnProfilingPhaseCompleted(profiling_phase);
747 }
748 }
749
635 static void OptionallyInitializeAlternateTimer() { 750 static void OptionallyInitializeAlternateTimer() {
636 NowFunction* alternate_time_source = GetAlternateTimeSource(); 751 NowFunction* alternate_time_source = GetAlternateTimeSource();
637 if (alternate_time_source) 752 if (alternate_time_source)
638 ThreadData::SetAlternateTimeSource(alternate_time_source); 753 ThreadData::SetAlternateTimeSource(alternate_time_source);
639 } 754 }
640 755
641 bool ThreadData::Initialize() { 756 bool ThreadData::Initialize() {
642 if (status_ >= DEACTIVATED) 757 if (status_ >= DEACTIVATED)
643 return true; // Someone else did the initialization. 758 return true; // Someone else did the initialization.
644 // Due to racy lazy initialization in tests, we'll need to recheck status_ 759 // Due to racy lazy initialization in tests, we'll need to recheck status_
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
902 1017
903 ThreadData* TaskStopwatch::GetThreadData() const { 1018 ThreadData* TaskStopwatch::GetThreadData() const {
904 #if DCHECK_IS_ON() 1019 #if DCHECK_IS_ON()
905 DCHECK(state_ != CREATED); 1020 DCHECK(state_ != CREATED);
906 #endif 1021 #endif
907 1022
908 return current_thread_data_; 1023 return current_thread_data_;
909 } 1024 }
910 1025
911 //------------------------------------------------------------------------------ 1026 //------------------------------------------------------------------------------
1027 // DeathDataPhaseSnapshot
1028
1029 DeathDataPhaseSnapshot::DeathDataPhaseSnapshot(int profiling_phase,
1030 int count,
1031 int32 run_duration_sum,
1032 int32 run_duration_max,
1033 int32 run_duration_sample,
1034 int32 queue_duration_sum,
1035 int32 queue_duration_max,
1036 int32 queue_duration_sample,
1037 DeathDataPhaseSnapshot* prev)
1038 : profiling_phase(profiling_phase),
1039 death_data(count,
1040 run_duration_sum,
1041 run_duration_max,
1042 run_duration_sample,
1043 queue_duration_sum,
1044 queue_duration_max,
1045 queue_duration_sample),
1046 prev(prev) {
1047 }
1048
1049 //------------------------------------------------------------------------------
1050 // TaskSnapshot
1051
912 TaskSnapshot::TaskSnapshot() { 1052 TaskSnapshot::TaskSnapshot() {
913 } 1053 }
914 1054
915 TaskSnapshot::TaskSnapshot(const BirthOnThread& birth, 1055 TaskSnapshot::TaskSnapshot(const BirthOnThreadSnapshot& birth,
916 const DeathData& death_data, 1056 const DeathDataSnapshot& death_data,
917 const std::string& death_thread_name) 1057 const std::string& death_thread_name)
918 : birth(birth), 1058 : birth(birth),
919 death_data(death_data), 1059 death_data(death_data),
920 death_thread_name(death_thread_name) { 1060 death_thread_name(death_thread_name) {
921 } 1061 }
922 1062
923 TaskSnapshot::~TaskSnapshot() { 1063 TaskSnapshot::~TaskSnapshot() {
924 } 1064 }
925 1065
926 //------------------------------------------------------------------------------ 1066 //------------------------------------------------------------------------------
(...skipping 28 matching lines...) Expand all
955 : process_id(base::GetCurrentProcId()) { 1095 : process_id(base::GetCurrentProcId()) {
956 #else 1096 #else
957 : process_id(base::kNullProcessId) { 1097 : process_id(base::kNullProcessId) {
958 #endif 1098 #endif
959 } 1099 }
960 1100
961 ProcessDataSnapshot::~ProcessDataSnapshot() { 1101 ProcessDataSnapshot::~ProcessDataSnapshot() {
962 } 1102 }
963 1103
964 } // namespace tracked_objects 1104 } // namespace tracked_objects
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698