1 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "base/tracked_objects.h"
6 :
7 : #include <math.h>
8 :
9 : #include "base/string_util.h"
10 :
11 : using base::TimeDelta;
12 :
13 : namespace tracked_objects {
14 :
15 : // A TLS slot to the TrackRegistry for the current thread.
16 : // static
17 1464 : TLSSlot ThreadData::tls_index_(base::LINKER_INITIALIZED);
18 :
19 : //------------------------------------------------------------------------------
20 : // Death data tallies durations when a death takes place.
21 :
22 0 : void DeathData::RecordDeath(const TimeDelta& duration) {
23 0 : ++count_;
24 0 : life_duration_ += duration;
25 0 : int64 milliseconds = duration.InMilliseconds();
26 0 : square_duration_ += milliseconds * milliseconds;
27 0 : }
28 :
29 0 : int DeathData::AverageMsDuration() const {
30 0 : return static_cast<int>(life_duration_.InMilliseconds() / count_);
31 : }
32 :
33 0 : double DeathData::StandardDeviation() const {
34 0 : double average = AverageMsDuration();
35 : double variance = static_cast<float>(square_duration_)/count_
36 0 : - average * average;
37 0 : return sqrt(variance);
38 : }
39 :
40 :
41 0 : void DeathData::AddDeathData(const DeathData& other) {
42 0 : count_ += other.count_;
43 0 : life_duration_ += other.life_duration_;
44 0 : square_duration_ += other.square_duration_;
45 0 : }
46 :
47 0 : void DeathData::Write(std::string* output) const {
48 0 : if (!count_)
49 0 : return;
50 0 : if (1 == count_)
51 0 : StringAppendF(output, "(1)Life in %dms ", AverageMsDuration());
52 : else
53 0 : StringAppendF(output, "(%d)Lives %dms/life ", count_, AverageMsDuration());
54 : }
55 :
56 0 : void DeathData::Clear() {
57 0 : count_ = 0;
58 0 : life_duration_ = TimeDelta();
59 0 : square_duration_ = 0;
60 0 : }
61 :
62 : //------------------------------------------------------------------------------
63 :
64 0 : BirthOnThread::BirthOnThread(const Location& location)
65 : : location_(location),
66 0 : birth_thread_(ThreadData::current()) { }
67 :
68 : //------------------------------------------------------------------------------
69 0 : Births::Births(const Location& location)
70 : : BirthOnThread(location),
71 0 : birth_count_(0) { }
72 :
73 : //------------------------------------------------------------------------------
74 : // ThreadData maintains the central data for all births and death.
75 :
76 : // static
77 : ThreadData* ThreadData::first_ = NULL;
78 : // static
79 1464 : Lock ThreadData::list_lock_;
80 :
81 : // static
82 : ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
83 :
84 0 : ThreadData::ThreadData() : next_(NULL), message_loop_(MessageLoop::current()) {}
85 :
86 : // static
87 0 : ThreadData* ThreadData::current() {
88 0 : if (!tls_index_.initialized())
89 0 : return NULL;
90 :
91 0 : ThreadData* registry = static_cast<ThreadData*>(tls_index_.Get());
92 0 : if (!registry) {
93 : // We have to create a new registry for ThreadData.
94 0 : bool too_late_to_create = false;
95 : {
96 0 : registry = new ThreadData;
97 0 : AutoLock lock(list_lock_);
98 : // Use lock to insure we have most recent status.
99 0 : if (!IsActive()) {
100 0 : too_late_to_create = true;
101 : } else {
102 : // Use lock to insert into list.
103 0 : registry->next_ = first_;
104 0 : first_ = registry;
105 : }
106 : } // Release lock.
107 0 : if (too_late_to_create) {
108 0 : delete registry;
109 0 : registry = NULL;
110 : } else {
111 0 : tls_index_.Set(registry);
112 : }
113 : }
114 0 : return registry;
115 : }
116 :
117 : // Do mininimal fixups for searching function names.
118 0 : static std::string UnescapeQuery(const std::string& query) {
119 0 : std::string result;
120 0 : for (size_t i = 0; i < query.size(); i++) {
121 0 : char next = query[i];
122 0 : if ('%' == next && i + 2 < query.size()) {
123 0 : std::string hex = query.substr(i + 1, 2);
124 0 : char replacement = '\0';
125 : // Only bother with "<", ">", and " ".
126 0 : if (LowerCaseEqualsASCII(hex, "3c"))
127 0 : replacement ='<';
128 0 : else if (LowerCaseEqualsASCII(hex, "3e"))
129 0 : replacement = '>';
130 0 : else if (hex == "20")
131 0 : replacement = ' ';
132 0 : if (replacement) {
133 0 : next = replacement;
134 0 : i += 2;
135 : }
136 : }
137 0 : result.push_back(next);
138 : }
139 : return result;
140 : }
141 :
142 : // static
143 0 : void ThreadData::WriteHTML(const std::string& query, std::string* output) {
144 0 : if (!ThreadData::IsActive())
145 0 : return; // Not yet initialized.
146 :
147 0 : DCHECK(ThreadData::current());
148 :
149 0 : output->append("<html><head><title>About Objects");
150 0 : std::string escaped_query = UnescapeQuery(query);
151 0 : if (!escaped_query.empty())
152 0 : output->append(" - " + escaped_query);
153 0 : output->append("</title></head><body><pre>");
154 :
155 0 : DataCollector collected_data; // Gather data.
156 0 : collected_data.AddListOfLivingObjects(); // Add births that are still alive.
157 :
158 : // Data Gathering is complete. Now to sort/process/render.
159 0 : DataCollector::Collection* collection = collected_data.collection();
160 :
161 : // Create filtering and sort comparison object.
162 0 : Comparator comparator;
163 0 : comparator.ParseQuery(escaped_query);
164 :
165 : // Filter out acceptable (matching) instances.
166 0 : DataCollector::Collection match_array;
167 0 : for (DataCollector::Collection::iterator it = collection->begin();
168 0 : it != collection->end(); ++it) {
169 0 : if (comparator.Acceptable(*it))
170 0 : match_array.push_back(*it);
171 : }
172 :
173 0 : comparator.Sort(&match_array);
174 :
175 0 : WriteHTMLTotalAndSubtotals(match_array, comparator, output);
176 :
177 0 : comparator.Clear(); // Delete tiebreaker_ instances.
178 :
179 0 : output->append("</pre></body></html>");
180 : }
181 :
182 : // static
183 0 : void ThreadData::WriteHTMLTotalAndSubtotals(
184 : const DataCollector::Collection& match_array,
185 : const Comparator& comparator,
186 : std::string* output) {
187 0 : if (!match_array.size()) {
188 0 : output->append("There were no tracked matches.");
189 : } else {
190 : // Aggregate during printing
191 0 : Aggregation totals;
192 0 : for (size_t i = 0; i < match_array.size(); ++i) {
193 0 : totals.AddDeathSnapshot(match_array[i]);
194 : }
195 0 : output->append("Aggregate Stats: ");
196 0 : totals.Write(output);
197 0 : output->append("<hr><hr>");
198 :
199 0 : Aggregation subtotals;
200 0 : for (size_t i = 0; i < match_array.size(); ++i) {
201 0 : if (0 == i || !comparator.Equivalent(match_array[i - 1],
202 0 : match_array[i])) {
203 : // Print group's defining characteristics.
204 0 : comparator.WriteSortGrouping(match_array[i], output);
205 0 : output->append("<br><br>");
206 : }
207 0 : comparator.WriteSnapshot(match_array[i], output);
208 0 : output->append("<br>");
209 0 : subtotals.AddDeathSnapshot(match_array[i]);
210 0 : if (i + 1 >= match_array.size() ||
211 0 : !comparator.Equivalent(match_array[i],
212 0 : match_array[i + 1])) {
213 : // Print aggregate stats for the group.
214 0 : output->append("<br>");
215 0 : subtotals.Write(output);
216 0 : output->append("<br><hr><br>");
217 0 : subtotals.Clear();
218 : }
219 : }
220 : }
221 0 : }
222 :
223 0 : Births* ThreadData::FindLifetime(const Location& location) {
224 0 : if (!message_loop_) // In case message loop wasn't yet around...
225 0 : message_loop_ = MessageLoop::current(); // Find it now.
226 :
227 0 : BirthMap::iterator it = birth_map_.find(location);
228 0 : if (it != birth_map_.end())
229 0 : return it->second;
230 0 : Births* tracker = new Births(location);
231 :
232 : // Lock since the map may get relocated now, and other threads sometimes
233 : // snapshot it (but they lock before copying it).
234 0 : AutoLock lock(lock_);
235 0 : birth_map_[location] = tracker;
236 0 : return tracker;
237 : }
238 :
239 0 : void ThreadData::TallyADeath(const Births& lifetimes,
240 : const TimeDelta& duration) {
241 0 : if (!message_loop_) // In case message loop wasn't yet around...
242 0 : message_loop_ = MessageLoop::current(); // Find it now.
243 :
244 0 : DeathMap::iterator it = death_map_.find(&lifetimes);
245 0 : if (it != death_map_.end()) {
246 0 : it->second.RecordDeath(duration);
247 0 : return;
248 : }
249 :
250 0 : AutoLock lock(lock_); // Lock since the map may get relocated now.
251 0 : death_map_[&lifetimes].RecordDeath(duration);
252 : }
253 :
254 : // static
255 0 : ThreadData* ThreadData::first() {
256 0 : AutoLock lock(list_lock_);
257 0 : return first_;
258 : }
259 :
260 0 : const std::string ThreadData::ThreadName() const {
261 0 : if (message_loop_)
262 0 : return message_loop_->thread_name();
263 0 : return "ThreadWithoutMessageLoop";
264 : }
265 :
266 : // This may be called from another thread.
267 0 : void ThreadData::SnapshotBirthMap(BirthMap *output) const {
268 0 : AutoLock lock(*const_cast<Lock*>(&lock_));
269 0 : for (BirthMap::const_iterator it = birth_map_.begin();
270 0 : it != birth_map_.end(); ++it)
271 0 : (*output)[it->first] = it->second;
272 0 : }
273 :
274 : // This may be called from another thread.
275 0 : void ThreadData::SnapshotDeathMap(DeathMap *output) const {
276 0 : AutoLock lock(*const_cast<Lock*>(&lock_));
277 0 : for (DeathMap::const_iterator it = death_map_.begin();
278 0 : it != death_map_.end(); ++it)
279 0 : (*output)[it->first] = it->second;
280 0 : }
281 :
282 : #ifdef OS_WIN
283 : void ThreadData::RunOnAllThreads(void (*function)()) {
284 : ThreadData* list = first(); // Get existing list.
285 :
286 : std::vector<MessageLoop*> message_loops;
287 : for (ThreadData* it = list; it; it = it->next()) {
288 : if (current() != it && it->message_loop())
289 : message_loops.push_back(it->message_loop());
290 : }
291 :
292 : ThreadSafeDownCounter* counter =
293 : new ThreadSafeDownCounter(message_loops.size() + 1); // Extra one for us!
294 :
295 : HANDLE completion_handle = CreateEvent(NULL, false, false, NULL);
296 : // Tell all other threads to run.
297 : for (size_t i = 0; i < message_loops.size(); ++i)
298 : message_loops[i]->PostTask(FROM_HERE,
299 : new RunTheStatic(function, completion_handle, counter));
300 :
301 : // Also run Task on our thread.
302 : RunTheStatic local_task(function, completion_handle, counter);
303 : local_task.Run();
304 :
305 : WaitForSingleObject(completion_handle, INFINITE);
306 : int ret_val = CloseHandle(completion_handle);
307 : DCHECK(ret_val);
308 : }
309 : #endif
310 :
311 : // static
312 0 : bool ThreadData::StartTracking(bool status) {
313 : #ifndef TRACK_ALL_TASK_OBJECTS
314 : return false; // Not compiled in.
315 : #else
316 0 : if (!status) {
317 0 : AutoLock lock(list_lock_);
318 0 : DCHECK(status_ == ACTIVE || status_ == SHUTDOWN);
319 0 : status_ = SHUTDOWN;
320 0 : return true;
321 : }
322 0 : AutoLock lock(list_lock_);
323 0 : DCHECK(status_ == UNINITIALIZED);
324 0 : CHECK(tls_index_.Initialize(NULL));
325 0 : status_ = ACTIVE;
326 0 : return true;
327 : #endif
328 : }
329 :
330 : // static
331 4258 : bool ThreadData::IsActive() {
332 4258 : return status_ == ACTIVE;
333 : }
334 :
335 : #ifdef OS_WIN
336 : // static
337 : void ThreadData::ShutdownMultiThreadTracking() {
338 : // Using lock, guarantee that no new ThreadData instances will be created.
339 : if (!StartTracking(false))
340 : return;
341 :
342 : RunOnAllThreads(ShutdownDisablingFurtherTracking);
343 :
344 : // Now the *only* threads that might change the database are the threads with
345 : // no messages loops. They might still be adding data to their birth records,
346 : // but since no objects are deleted on those threads, there will be no further
347 : // access to to cross-thread data.
348 : // We could do a cleanup on all threads except for the ones without
349 : // MessageLoops, but we won't bother doing cleanup (destruction of data) yet.
350 : return;
351 : }
352 : #endif
353 :
354 : // static
355 0 : void ThreadData::ShutdownSingleThreadedCleanup() {
356 : // We must be single threaded... but be careful anyway.
357 0 : if (!StartTracking(false))
358 0 : return;
359 : ThreadData* thread_data_list;
360 : {
361 0 : AutoLock lock(list_lock_);
362 0 : thread_data_list = first_;
363 0 : first_ = NULL;
364 : }
365 :
366 0 : while (thread_data_list) {
367 0 : ThreadData* next_thread_data = thread_data_list;
368 0 : thread_data_list = thread_data_list->next();
369 :
370 0 : for (BirthMap::iterator it = next_thread_data->birth_map_.begin();
371 0 : next_thread_data->birth_map_.end() != it; ++it)
372 0 : delete it->second; // Delete the Birth Records.
373 0 : next_thread_data->birth_map_.clear();
374 0 : next_thread_data->death_map_.clear();
375 0 : delete next_thread_data; // Includes all Death Records.
376 : }
377 :
378 0 : CHECK(tls_index_.initialized());
379 0 : tls_index_.Free();
380 0 : DCHECK(!tls_index_.initialized());
381 0 : status_ = UNINITIALIZED;
382 : }
383 :
384 : // static
385 0 : void ThreadData::ShutdownDisablingFurtherTracking() {
386 : // Redundantly set status SHUTDOWN on this thread.
387 0 : if (!StartTracking(false))
388 0 : return;
389 : }
390 :
391 :
392 : //------------------------------------------------------------------------------
393 :
394 0 : ThreadData::ThreadSafeDownCounter::ThreadSafeDownCounter(size_t count)
395 0 : : remaining_count_(count) {
396 0 : DCHECK(remaining_count_ > 0);
397 0 : }
398 :
399 0 : bool ThreadData::ThreadSafeDownCounter::LastCaller() {
400 : {
401 0 : AutoLock lock(lock_);
402 0 : if (--remaining_count_)
403 0 : return false;
404 : } // Release lock, so we can delete everything in this instance.
405 0 : delete this;
406 0 : return true;
407 : }
408 :
409 : //------------------------------------------------------------------------------
410 : #ifdef OS_WIN
411 : ThreadData::RunTheStatic::RunTheStatic(FunctionPointer function,
412 : HANDLE completion_handle,
413 : ThreadSafeDownCounter* counter)
414 : : function_(function),
415 : completion_handle_(completion_handle),
416 : counter_(counter) {
417 : }
418 :
419 : void ThreadData::RunTheStatic::Run() {
420 : function_();
421 : if (counter_->LastCaller())
422 : SetEvent(completion_handle_);
423 : }
424 : #endif
425 :
426 : //------------------------------------------------------------------------------
427 : // Individual 3-tuple of birth (place and thread) along with death thread, and
428 : // the accumulated stats for instances (DeathData).
429 :
430 0 : Snapshot::Snapshot(const BirthOnThread& birth_on_thread,
431 : const ThreadData& death_thread,
432 : const DeathData& death_data)
433 : : birth_(&birth_on_thread),
434 : death_thread_(&death_thread),
435 0 : death_data_(death_data) {
436 0 : }
437 :
438 0 : Snapshot::Snapshot(const BirthOnThread& birth_on_thread, int count)
439 : : birth_(&birth_on_thread),
440 : death_thread_(NULL),
441 0 : death_data_(DeathData(count)) {
442 0 : }
443 :
444 0 : const std::string Snapshot::DeathThreadName() const {
445 0 : if (death_thread_)
446 0 : return death_thread_->ThreadName();
447 0 : return "Still_Alive";
448 : }
449 :
450 0 : void Snapshot::Write(std::string* output) const {
451 0 : death_data_.Write(output);
452 : StringAppendF(output, "%s->%s ",
453 0 : birth_->birth_thread()->ThreadName().c_str(),
454 0 : death_thread_->ThreadName().c_str());
455 0 : birth_->location().Write(true, true, output);
456 0 : }
457 :
458 0 : void Snapshot::Add(const Snapshot& other) {
459 0 : death_data_.AddDeathData(other.death_data_);
460 0 : }
461 :
462 : //------------------------------------------------------------------------------
463 : // DataCollector
464 :
465 0 : DataCollector::DataCollector() {
466 0 : DCHECK(ThreadData::IsActive());
467 :
468 0 : ThreadData* my_list = ThreadData::current()->first();
469 :
470 0 : count_of_contributing_threads_ = 0;
471 0 : for (ThreadData* thread_data = my_list;
472 : thread_data;
473 : thread_data = thread_data->next()) {
474 0 : ++count_of_contributing_threads_;
475 : }
476 :
477 : // Gather data serially. A different constructor could be used to do in
478 : // parallel, and then invoke an OnCompletion task.
479 0 : for (ThreadData* thread_data = my_list;
480 : thread_data;
481 : thread_data = thread_data->next()) {
482 0 : Append(*thread_data);
483 : }
484 0 : }
485 :
486 0 : void DataCollector::Append(const ThreadData& thread_data) {
487 : // Get copy of data (which is done under ThreadData's lock).
488 0 : ThreadData::BirthMap birth_map;
489 0 : thread_data.SnapshotBirthMap(&birth_map);
490 0 : ThreadData::DeathMap death_map;
491 0 : thread_data.SnapshotDeathMap(&death_map);
492 :
493 : // Use our lock to protect our accumulation activity.
494 0 : AutoLock lock(accumulation_lock_);
495 :
496 0 : DCHECK(count_of_contributing_threads_);
497 :
498 0 : for (ThreadData::DeathMap::const_iterator it = death_map.begin();
499 0 : it != death_map.end(); ++it) {
500 0 : collection_.push_back(Snapshot(*it->first, thread_data, it->second));
501 0 : global_birth_count_[it->first] -= it->first->birth_count();
502 : }
503 :
504 0 : for (ThreadData::BirthMap::const_iterator it = birth_map.begin();
505 0 : it != birth_map.end(); ++it) {
506 0 : global_birth_count_[it->second] += it->second->birth_count();
507 : }
508 :
509 0 : --count_of_contributing_threads_;
510 0 : }
511 :
512 0 : DataCollector::Collection* DataCollector::collection() {
513 0 : DCHECK(!count_of_contributing_threads_);
514 0 : return &collection_;
515 : }
516 :
517 0 : void DataCollector::AddListOfLivingObjects() {
518 0 : DCHECK(!count_of_contributing_threads_);
519 0 : for (BirthCount::iterator it = global_birth_count_.begin();
520 0 : it != global_birth_count_.end(); ++it) {
521 0 : if (it->second > 0)
522 0 : collection_.push_back(Snapshot(*it->first, it->second));
523 : }
524 0 : }
525 :
526 : //------------------------------------------------------------------------------
527 : // Aggregation
528 :
529 0 : void Aggregation::AddDeathSnapshot(const Snapshot& snapshot) {
530 0 : AddBirth(snapshot.birth());
531 0 : death_threads_[snapshot.death_thread()]++;
532 0 : AddDeathData(snapshot.death_data());
533 0 : }
534 :
535 0 : void Aggregation::AddBirths(const Births& births) {
536 0 : AddBirth(births);
537 0 : birth_count_ += births.birth_count();
538 0 : }
539 0 : void Aggregation::AddBirth(const BirthOnThread& birth) {
540 0 : AddBirthPlace(birth.location());
541 0 : birth_threads_[birth.birth_thread()]++;
542 0 : }
543 :
544 0 : void Aggregation::AddBirthPlace(const Location& location) {
545 0 : locations_[location]++;
546 0 : birth_files_[location.file_name()]++;
547 0 : }
548 :
549 0 : void Aggregation::Write(std::string* output) const {
550 0 : if (locations_.size() == 1) {
551 0 : locations_.begin()->first.Write(true, true, output);
552 : } else {
553 0 : StringAppendF(output, "%d Locations. ", locations_.size());
554 0 : if (birth_files_.size() > 1)
555 0 : StringAppendF(output, "%d Files. ", birth_files_.size());
556 : else
557 : StringAppendF(output, "All born in %s. ",
558 0 : birth_files_.begin()->first.c_str());
559 : }
560 :
561 0 : if (birth_threads_.size() > 1)
562 0 : StringAppendF(output, "%d BirthingThreads. ", birth_threads_.size());
563 : else
564 : StringAppendF(output, "All born on %s. ",
565 0 : birth_threads_.begin()->first->ThreadName().c_str());
566 :
567 0 : if (death_threads_.size() > 1) {
568 0 : StringAppendF(output, "%d DeathThreads. ", death_threads_.size());
569 : } else {
570 0 : if (death_threads_.begin()->first)
571 : StringAppendF(output, "All deleted on %s. ",
572 0 : death_threads_.begin()->first->ThreadName().c_str());
573 : else
574 0 : output->append("All these objects are still alive.");
575 : }
576 :
577 0 : if (birth_count_ > 1)
578 0 : StringAppendF(output, "Births=%d ", birth_count_);
579 :
580 0 : DeathData::Write(output);
581 0 : }
582 :
583 0 : void Aggregation::Clear() {
584 0 : birth_count_ = 0;
585 0 : birth_files_.clear();
586 0 : locations_.clear();
587 0 : birth_threads_.clear();
588 0 : DeathData::Clear();
589 0 : death_threads_.clear();
590 0 : }
591 :
592 : //------------------------------------------------------------------------------
593 : // Comparison object for sorting.
594 :
595 0 : Comparator::Comparator()
596 : : selector_(NIL),
597 : tiebreaker_(NULL),
598 : combined_selectors_(0),
599 0 : use_tiebreaker_for_sort_only_(false) {}
600 :
601 0 : void Comparator::Clear() {
602 0 : if (tiebreaker_) {
603 0 : tiebreaker_->Clear();
604 0 : delete tiebreaker_;
605 0 : tiebreaker_ = NULL;
606 : }
607 0 : use_tiebreaker_for_sort_only_ = false;
608 0 : selector_ = NIL;
609 0 : }
610 :
611 0 : void Comparator::Sort(DataCollector::Collection* collection) const {
612 0 : std::sort(collection->begin(), collection->end(), *this);
613 0 : }
614 :
615 :
616 0 : bool Comparator::operator()(const Snapshot& left,
617 : const Snapshot& right) const {
618 0 : switch (selector_) {
619 : case BIRTH_THREAD:
620 0 : if (left.birth_thread() != right.birth_thread() &&
621 0 : left.birth_thread()->ThreadName() !=
622 0 : right.birth_thread()->ThreadName())
623 0 : return left.birth_thread()->ThreadName() <
624 0 : right.birth_thread()->ThreadName();
625 0 : break;
626 :
627 : case DEATH_THREAD:
628 0 : if (left.death_thread() != right.death_thread() &&
629 0 : left.DeathThreadName() !=
630 0 : right.DeathThreadName()) {
631 0 : if (!left.death_thread())
632 0 : return true;
633 0 : if (!right.death_thread())
634 0 : return false;
635 0 : return left.DeathThreadName() <
636 0 : right.DeathThreadName();
637 : }
638 0 : break;
639 :
640 : case BIRTH_FILE:
641 0 : if (left.location().file_name() != right.location().file_name()) {
642 : int comp = strcmp(left.location().file_name(),
643 0 : right.location().file_name());
644 0 : if (comp)
645 0 : return 0 > comp;
646 : }
647 0 : break;
648 :
649 : case BIRTH_FUNCTION:
650 0 : if (left.location().function_name() != right.location().function_name()) {
651 : int comp = strcmp(left.location().function_name(),
652 0 : right.location().function_name());
653 0 : if (comp)
654 0 : return 0 > comp;
655 : }
656 0 : break;
657 :
658 : case BIRTH_LINE:
659 0 : if (left.location().line_number() != right.location().line_number())
660 0 : return left.location().line_number() <
661 0 : right.location().line_number();
662 0 : break;
663 :
664 : case COUNT:
665 0 : if (left.count() != right.count())
666 0 : return left.count() > right.count(); // Sort large at front of vector.
667 0 : break;
668 :
669 : case AVERAGE_DURATION:
670 0 : if (left.AverageMsDuration() != right.AverageMsDuration())
671 0 : return left.AverageMsDuration() > right.AverageMsDuration();
672 0 : break;
673 :
674 : default:
675 0 : break;
676 : }
677 0 : if (tiebreaker_)
678 0 : return tiebreaker_->operator()(left, right);
679 0 : return false;
680 : }
681 :
682 0 : bool Comparator::Equivalent(const Snapshot& left,
683 : const Snapshot& right) const {
684 0 : switch (selector_) {
685 : case BIRTH_THREAD:
686 0 : if (left.birth_thread() != right.birth_thread() &&
687 0 : left.birth_thread()->ThreadName() !=
688 0 : right.birth_thread()->ThreadName())
689 0 : return false;
690 0 : break;
691 :
692 : case DEATH_THREAD:
693 0 : if (left.death_thread() != right.death_thread() &&
694 0 : left.DeathThreadName() != right.DeathThreadName())
695 0 : return false;
696 0 : break;
697 :
698 : case BIRTH_FILE:
699 0 : if (left.location().file_name() != right.location().file_name()) {
700 : int comp = strcmp(left.location().file_name(),
701 0 : right.location().file_name());
702 0 : if (comp)
703 0 : return false;
704 : }
705 0 : break;
706 :
707 : case BIRTH_FUNCTION:
708 0 : if (left.location().function_name() != right.location().function_name()) {
709 : int comp = strcmp(left.location().function_name(),
710 0 : right.location().function_name());
711 0 : if (comp)
712 0 : return false;
713 : }
714 0 : break;
715 :
716 : case COUNT:
717 0 : if (left.count() != right.count())
718 0 : return false;
719 0 : break;
720 :
721 : case AVERAGE_DURATION:
722 0 : if (left.life_duration() != right.life_duration())
723 0 : return false;
724 0 : break;
725 :
726 : default:
727 0 : break;
728 : }
729 0 : if (tiebreaker_ && !use_tiebreaker_for_sort_only_)
730 0 : return tiebreaker_->Equivalent(left, right);
731 0 : return true;
732 : }
733 :
734 0 : bool Comparator::Acceptable(const Snapshot& sample) const {
735 0 : if (required_.size()) {
736 0 : switch (selector_) {
737 : case BIRTH_THREAD:
738 0 : if (sample.birth_thread()->ThreadName().find(required_) ==
739 : std::string::npos)
740 0 : return false;
741 0 : break;
742 :
743 : case DEATH_THREAD:
744 0 : if (sample.DeathThreadName().find(required_) == std::string::npos)
745 0 : return false;
746 0 : break;
747 :
748 : case BIRTH_FILE:
749 0 : if (!strstr(sample.location().file_name(), required_.c_str()))
750 0 : return false;
751 0 : break;
752 :
753 : case BIRTH_FUNCTION:
754 0 : if (!strstr(sample.location().function_name(), required_.c_str()))
755 0 : return false;
756 0 : break;
757 :
758 : default:
759 0 : break;
760 : }
761 : }
762 0 : if (tiebreaker_ && !use_tiebreaker_for_sort_only_)
763 0 : return tiebreaker_->Acceptable(sample);
764 0 : return true;
765 : }
766 :
767 0 : void Comparator::SetTiebreaker(Selector selector, const std::string required) {
768 0 : if (selector == selector_ || NIL == selector)
769 0 : return;
770 0 : combined_selectors_ |= selector;
771 0 : if (NIL == selector_) {
772 0 : selector_ = selector;
773 0 : if (required.size())
774 0 : required_ = required;
775 0 : return;
776 : }
777 0 : if (tiebreaker_) {
778 0 : if (use_tiebreaker_for_sort_only_) {
779 0 : Comparator* temp = new Comparator;
780 0 : temp->tiebreaker_ = tiebreaker_;
781 0 : tiebreaker_ = temp;
782 : }
783 : } else {
784 0 : tiebreaker_ = new Comparator;
785 0 : DCHECK(!use_tiebreaker_for_sort_only_);
786 : }
787 0 : tiebreaker_->SetTiebreaker(selector, required);
788 : }
789 :
790 0 : bool Comparator::IsGroupedBy(Selector selector) const {
791 0 : return 0 != (selector & combined_selectors_);
792 : }
793 :
794 0 : void Comparator::SetSubgroupTiebreaker(Selector selector) {
795 0 : if (selector == selector_ || NIL == selector)
796 0 : return;
797 0 : if (!tiebreaker_) {
798 0 : use_tiebreaker_for_sort_only_ = true;
799 0 : tiebreaker_ = new Comparator;
800 0 : tiebreaker_->SetTiebreaker(selector, "");
801 : } else {
802 0 : tiebreaker_->SetSubgroupTiebreaker(selector);
803 : }
804 : }
805 :
806 0 : void Comparator::ParseKeyphrase(const std::string key_phrase) {
807 0 : static std::map<const std::string, Selector> key_map;
808 : static bool initialized = false;
809 0 : if (!initialized) {
810 0 : initialized = true;
811 0 : key_map["count"] = COUNT;
812 0 : key_map["duration"] = AVERAGE_DURATION;
813 0 : key_map["birth"] = BIRTH_THREAD;
814 0 : key_map["death"] = DEATH_THREAD;
815 0 : key_map["file"] = BIRTH_FILE;
816 0 : key_map["function"] = BIRTH_FUNCTION;
817 0 : key_map["line"] = BIRTH_LINE;
818 : }
819 :
820 0 : std::string required;
821 0 : size_t equal_offset = key_phrase.find('=', 0);
822 0 : if (key_phrase.npos != equal_offset)
823 0 : required = key_phrase.substr(equal_offset + 1, key_phrase.npos);
824 0 : std::string keyword(key_phrase.substr(0, equal_offset));
825 0 : keyword = StringToLowerASCII(keyword);
826 0 : if (key_map.end() == key_map.find(keyword))
827 : return;
828 0 : SetTiebreaker(key_map[keyword], required);
829 : }
830 :
831 0 : bool Comparator::ParseQuery(const std::string query) {
832 0 : for (size_t i = 0; i < query.size();) {
833 0 : size_t slash_offset = query.find('/', i);
834 0 : ParseKeyphrase(query.substr(i, slash_offset - i));
835 0 : if (query.npos == slash_offset)
836 0 : break;
837 0 : i = slash_offset + 1;
838 : }
839 :
840 : // Select subgroup ordering (if we want to display the subgroup)
841 0 : SetSubgroupTiebreaker(COUNT);
842 0 : SetSubgroupTiebreaker(AVERAGE_DURATION);
843 0 : SetSubgroupTiebreaker(BIRTH_THREAD);
844 0 : SetSubgroupTiebreaker(DEATH_THREAD);
845 0 : SetSubgroupTiebreaker(BIRTH_FUNCTION);
846 0 : SetSubgroupTiebreaker(BIRTH_FILE);
847 0 : SetSubgroupTiebreaker(BIRTH_LINE);
848 :
849 0 : return true;
850 : }
851 :
852 0 : bool Comparator::WriteSortGrouping(const Snapshot& sample,
853 : std::string* output) const {
854 0 : bool wrote_data = false;
855 0 : switch (selector_) {
856 : case BIRTH_THREAD:
857 : StringAppendF(output, "All new on %s ",
858 0 : sample.birth_thread()->ThreadName().c_str());
859 0 : wrote_data = true;
860 0 : break;
861 :
862 : case DEATH_THREAD:
863 0 : if (sample.death_thread())
864 : StringAppendF(output, "All deleted on %s ",
865 0 : sample.DeathThreadName().c_str());
866 : else
867 0 : output->append("All still alive ");
868 0 : wrote_data = true;
869 0 : break;
870 :
871 : case BIRTH_FILE:
872 : StringAppendF(output, "All born in %s ",
873 0 : sample.location().file_name());
874 0 : break;
875 :
876 : case BIRTH_FUNCTION:
877 0 : output->append("All born in ");
878 0 : sample.location().WriteFunctionName(output);
879 0 : output->push_back(' ');
880 0 : break;
881 :
882 : default:
883 0 : break;
884 : }
885 0 : if (tiebreaker_ && !use_tiebreaker_for_sort_only_) {
886 0 : wrote_data |= tiebreaker_->WriteSortGrouping(sample, output);
887 : }
888 0 : return wrote_data;
889 : }
890 :
891 0 : void Comparator::WriteSnapshot(const Snapshot& sample,
892 : std::string* output) const {
893 0 : sample.death_data().Write(output);
894 0 : if (!(combined_selectors_ & BIRTH_THREAD) ||
895 0 : !(combined_selectors_ & DEATH_THREAD))
896 : StringAppendF(output, "%s->%s ",
897 : (combined_selectors_ & BIRTH_THREAD) ? "*" :
898 0 : sample.birth().birth_thread()->ThreadName().c_str(),
899 : (combined_selectors_ & DEATH_THREAD) ? "*" :
900 0 : sample.DeathThreadName().c_str());
901 0 : sample.birth().location().Write(!(combined_selectors_ & BIRTH_FILE),
902 : !(combined_selectors_ & BIRTH_FUNCTION),
903 0 : output);
904 0 : }
905 :
906 4392 : } // namespace tracked_objects
|