Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/debug/trace_event.h" | 5 #include "base/debug/trace_event.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include "base/debug/trace_event_win.h" | 10 #include "base/debug/trace_event_win.h" |
| 11 #endif | 11 #endif |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/memory/singleton.h" | |
| 15 #include "base/process_util.h" | 16 #include "base/process_util.h" |
| 16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 18 #include "base/threading/platform_thread.h" | |
| 17 #include "base/threading/thread_local.h" | 19 #include "base/threading/thread_local.h" |
| 18 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 19 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
| 20 #include "base/time.h" | 22 #include "base/time.h" |
| 21 | 23 |
| 22 class DeleteTraceLogForTesting { | 24 class DeleteTraceLogForTesting { |
| 23 public: | 25 public: |
| 24 static void Delete() { | 26 static void Delete() { |
| 25 Singleton<base::debug::TraceLog, | 27 Singleton<base::debug::TraceLog, |
| 26 StaticMemorySingletonTraits<base::debug::TraceLog> >::OnExit(0); | 28 StaticMemorySingletonTraits<base::debug::TraceLog> >::OnExit(0); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 47 false } | 49 false } |
| 48 }; | 50 }; |
| 49 const TraceCategory* const g_category_already_shutdown = | 51 const TraceCategory* const g_category_already_shutdown = |
| 50 &g_categories[0]; | 52 &g_categories[0]; |
| 51 const TraceCategory* const g_category_categories_exhausted = | 53 const TraceCategory* const g_category_categories_exhausted = |
| 52 &g_categories[1]; | 54 &g_categories[1]; |
| 53 const TraceCategory* const g_category_metadata = | 55 const TraceCategory* const g_category_metadata = |
| 54 &g_categories[2]; | 56 &g_categories[2]; |
| 55 int g_category_index = 3; // skip initial 3 categories | 57 int g_category_index = 3; // skip initial 3 categories |
| 56 | 58 |
| 57 // Flag to indicate whether we captured the current thread name | 59 // The most-recently captured name of the current thread |
| 58 LazyInstance<ThreadLocalBoolean, | 60 LazyInstance<ThreadLocalPointer<char>, |
| 59 LeakyLazyInstanceTraits<ThreadLocalBoolean> > | 61 LeakyLazyInstanceTraits<ThreadLocalPointer<char> > > |
| 60 g_current_thread_name_captured(LINKER_INITIALIZED); | 62 g_current_thread_name(LINKER_INITIALIZED); |
| 61 | 63 |
| 62 } // namespace | 64 } // namespace |
| 63 | 65 |
| 64 //////////////////////////////////////////////////////////////////////////////// | 66 //////////////////////////////////////////////////////////////////////////////// |
| 65 // | 67 // |
| 66 // TraceValue | 68 // TraceValue |
| 67 // | 69 // |
| 68 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
| 69 | 71 |
| 70 void TraceValue::AppendAsJSON(std::string* out) const { | 72 void TraceValue::AppendAsJSON(std::string* out) const { |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 int ret_begin_id = -1; | 508 int ret_begin_id = -1; |
| 507 { | 509 { |
| 508 AutoLock lock(lock_); | 510 AutoLock lock(lock_); |
| 509 if (!category->enabled) | 511 if (!category->enabled) |
| 510 return -1; | 512 return -1; |
| 511 if (logged_events_.size() >= kTraceEventBufferSize) | 513 if (logged_events_.size() >= kTraceEventBufferSize) |
| 512 return -1; | 514 return -1; |
| 513 | 515 |
| 514 PlatformThreadId thread_id = PlatformThread::CurrentId(); | 516 PlatformThreadId thread_id = PlatformThread::CurrentId(); |
| 515 | 517 |
| 516 // Record the name of the calling thread, if not done already. | 518 const char* new_name = PlatformThread::GetName(); |
| 517 if (!g_current_thread_name_captured.Pointer()->Get()) { | 519 // Check if the thread name has been set or changed since th previous |
|
jar (doing other things)
2011/11/10 00:18:35
nit typo: th--> the
joth
2011/11/10 10:15:26
Done.
| |
| 518 g_current_thread_name_captured.Pointer()->Set(true); | 520 // call (if any), but don't bother if the new name is empty. Note this will |
| 519 const char* cur_name = PlatformThread::GetName(); | 521 // not detect a thread name change within the same char* buffer address: we |
| 522 // favor common case performance over corner case correctness. | |
| 523 if (new_name != g_current_thread_name.Get().Get() && | |
| 524 new_name && *new_name) { | |
| 525 // Benign const cast. | |
| 526 g_current_thread_name.Get().Set(const_cast<char*>(new_name)); | |
| 520 base::hash_map<PlatformThreadId, std::string>::iterator existing_name = | 527 base::hash_map<PlatformThreadId, std::string>::iterator existing_name = |
| 521 thread_names_.find(thread_id); | 528 thread_names_.find(thread_id); |
| 522 if (existing_name == thread_names_.end()) { | 529 if (existing_name == thread_names_.end()) { |
| 523 // This is a new thread id, and a new name. | 530 // This is a new thread id, and a new name. |
| 524 thread_names_[thread_id] = cur_name ? cur_name : ""; | 531 thread_names_[thread_id] = new_name; |
| 525 } else if(cur_name != NULL) { | 532 } else { |
| 526 // This is a thread id that we've seen before, but potentially with a | 533 // This is a thread id that we've seen before, but potentially with a |
| 527 // new name. | 534 // new name. |
| 528 std::vector<std::string> existing_names; | 535 std::vector<base::StringPiece> existing_names; |
| 529 Tokenize(existing_name->second, std::string(","), &existing_names); | 536 Tokenize(existing_name->second, std::string(","), &existing_names); |
| 530 bool found = std::find(existing_names.begin(), | 537 bool found = std::find(existing_names.begin(), |
| 531 existing_names.end(), | 538 existing_names.end(), |
| 532 cur_name) != existing_names.end(); | 539 new_name) != existing_names.end(); |
| 533 if (!found) { | 540 if (!found) { |
| 534 existing_names.push_back(cur_name); | 541 existing_name->second.push_back(','); |
| 535 thread_names_[thread_id] = | 542 existing_name->second.append(new_name); |
| 536 JoinString(existing_names, ','); | |
| 537 } | 543 } |
| 538 } | 544 } |
| 539 } | 545 } |
| 540 | 546 |
| 541 if (threshold_begin_id > -1) { | 547 if (threshold_begin_id > -1) { |
| 542 DCHECK(phase == base::debug::TRACE_EVENT_PHASE_END); | 548 DCHECK(phase == base::debug::TRACE_EVENT_PHASE_END); |
| 543 size_t begin_i = static_cast<size_t>(threshold_begin_id); | 549 size_t begin_i = static_cast<size_t>(threshold_begin_id); |
| 544 // Return now if there has been a flush since the begin event was posted. | 550 // Return now if there has been a flush since the begin event was posted. |
| 545 if (begin_i >= logged_events_.size()) | 551 if (begin_i >= logged_events_.size()) |
| 546 return -1; | 552 return -1; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 667 NULL, 0, NULL, 0, | 673 NULL, 0, NULL, 0, |
| 668 p_data_->threshold_begin_id, p_data_->threshold, | 674 p_data_->threshold_begin_id, p_data_->threshold, |
| 669 TraceLog::EVENT_FLAG_NONE); | 675 TraceLog::EVENT_FLAG_NONE); |
| 670 } | 676 } |
| 671 } | 677 } |
| 672 | 678 |
| 673 } // namespace internal | 679 } // namespace internal |
| 674 | 680 |
| 675 } // namespace debug | 681 } // namespace debug |
| 676 } // namespace base | 682 } // namespace base |
| OLD | NEW |