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* cur_name = PlatformThread::GetName(); |
| 517 if (!g_current_thread_name_captured.Pointer()->Get()) { | 519 if (cur_name && *cur_name && |
|
jbates
2011/11/08 18:44:38
Since the steady state is (cur_name == g_current_t
nduca
2011/11/09 04:02:24
If we do that, we might want to explain the logic,
joth
2011/11/09 13:17:58
Done.
joth
2011/11/09 13:17:58
Done.
| |
| 518 g_current_thread_name_captured.Pointer()->Set(true); | 520 cur_name != g_current_thread_name.Get().Get()) { |
| 519 const char* cur_name = PlatformThread::GetName(); | 521 // Benign const cast. |
| 522 g_current_thread_name.Get().Set(const_cast<char*>(cur_name)); | |
| 520 base::hash_map<PlatformThreadId, std::string>::iterator existing_name = | 523 base::hash_map<PlatformThreadId, std::string>::iterator existing_name = |
| 521 thread_names_.find(thread_id); | 524 thread_names_.find(thread_id); |
| 522 if (existing_name == thread_names_.end()) { | 525 if (existing_name == thread_names_.end()) { |
| 523 // This is a new thread id, and a new name. | 526 // This is a new thread id, and a new name. |
| 524 thread_names_[thread_id] = cur_name ? cur_name : ""; | 527 thread_names_[thread_id] = cur_name; |
| 525 } else if(cur_name != NULL) { | 528 } else { |
| 526 // This is a thread id that we've seen before, but potentially with a | 529 // This is a thread id that we've seen before, but potentially with a |
| 527 // new name. | 530 // new name. |
| 528 std::vector<std::string> existing_names; | 531 std::vector<base::StringPiece> existing_names; |
| 529 Tokenize(existing_name->second, std::string(","), &existing_names); | 532 Tokenize(existing_name->second, std::string(","), &existing_names); |
| 530 bool found = std::find(existing_names.begin(), | 533 bool found = std::find(existing_names.begin(), |
| 531 existing_names.end(), | 534 existing_names.end(), |
| 532 cur_name) != existing_names.end(); | 535 cur_name) != existing_names.end(); |
| 533 if (!found) { | 536 if (!found) { |
| 534 existing_names.push_back(cur_name); | 537 existing_name->second.push_back(','); |
| 535 thread_names_[thread_id] = | 538 existing_name->second.append(cur_name); |
| 536 JoinString(existing_names, ','); | |
| 537 } | 539 } |
| 538 } | 540 } |
| 539 } | 541 } |
| 540 | 542 |
| 541 if (threshold_begin_id > -1) { | 543 if (threshold_begin_id > -1) { |
| 542 DCHECK(phase == base::debug::TRACE_EVENT_PHASE_END); | 544 DCHECK(phase == base::debug::TRACE_EVENT_PHASE_END); |
| 543 size_t begin_i = static_cast<size_t>(threshold_begin_id); | 545 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. | 546 // Return now if there has been a flush since the begin event was posted. |
| 545 if (begin_i >= logged_events_.size()) | 547 if (begin_i >= logged_events_.size()) |
| 546 return -1; | 548 return -1; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 667 NULL, 0, NULL, 0, | 669 NULL, 0, NULL, 0, |
| 668 p_data_->threshold_begin_id, p_data_->threshold, | 670 p_data_->threshold_begin_id, p_data_->threshold, |
| 669 TraceLog::EVENT_FLAG_NONE); | 671 TraceLog::EVENT_FLAG_NONE); |
| 670 } | 672 } |
| 671 } | 673 } |
| 672 | 674 |
| 673 } // namespace internal | 675 } // namespace internal |
| 674 | 676 |
| 675 } // namespace debug | 677 } // namespace debug |
| 676 } // namespace base | 678 } // namespace base |
| OLD | NEW |