| 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 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/memory/singleton.h" |
| 12 #include "base/process_util.h" | 13 #include "base/process_util.h" |
| 13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 14 #include "base/string_tokenizer.h" | 15 #include "base/string_tokenizer.h" |
| 16 #include "base/threading/platform_thread.h" |
| 15 #include "base/threading/thread_local.h" | 17 #include "base/threading/thread_local.h" |
| 16 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 17 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 18 #include "base/time.h" | 20 #include "base/time.h" |
| 19 | 21 |
| 20 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
| 21 #include "base/debug/trace_event_win.h" | 23 #include "base/debug/trace_event_win.h" |
| 22 #endif | 24 #endif |
| 23 | 25 |
| 24 class DeleteTraceLogForTesting { | 26 class DeleteTraceLogForTesting { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 49 false } | 51 false } |
| 50 }; | 52 }; |
| 51 const TraceCategory* const g_category_already_shutdown = | 53 const TraceCategory* const g_category_already_shutdown = |
| 52 &g_categories[0]; | 54 &g_categories[0]; |
| 53 const TraceCategory* const g_category_categories_exhausted = | 55 const TraceCategory* const g_category_categories_exhausted = |
| 54 &g_categories[1]; | 56 &g_categories[1]; |
| 55 const TraceCategory* const g_category_metadata = | 57 const TraceCategory* const g_category_metadata = |
| 56 &g_categories[2]; | 58 &g_categories[2]; |
| 57 int g_category_index = 3; // skip initial 3 categories | 59 int g_category_index = 3; // skip initial 3 categories |
| 58 | 60 |
| 59 // Flag to indicate whether we captured the current thread name | 61 // The most-recently captured name of the current thread |
| 60 LazyInstance<ThreadLocalBoolean, | 62 LazyInstance<ThreadLocalPointer<char>, |
| 61 LeakyLazyInstanceTraits<ThreadLocalBoolean> > | 63 LeakyLazyInstanceTraits<ThreadLocalPointer<char> > > |
| 62 g_current_thread_name_captured(LINKER_INITIALIZED); | 64 g_current_thread_name(LINKER_INITIALIZED); |
| 63 | 65 |
| 64 } // namespace | 66 } // namespace |
| 65 | 67 |
| 66 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
| 67 // | 69 // |
| 68 // TraceValue | 70 // TraceValue |
| 69 // | 71 // |
| 70 //////////////////////////////////////////////////////////////////////////////// | 72 //////////////////////////////////////////////////////////////////////////////// |
| 71 | 73 |
| 72 void TraceValue::AppendAsJSON(std::string* out) const { | 74 void TraceValue::AppendAsJSON(std::string* out) const { |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 int ret_begin_id = -1; | 540 int ret_begin_id = -1; |
| 539 { | 541 { |
| 540 AutoLock lock(lock_); | 542 AutoLock lock(lock_); |
| 541 if (!category->enabled) | 543 if (!category->enabled) |
| 542 return -1; | 544 return -1; |
| 543 if (logged_events_.size() >= kTraceEventBufferSize) | 545 if (logged_events_.size() >= kTraceEventBufferSize) |
| 544 return -1; | 546 return -1; |
| 545 | 547 |
| 546 PlatformThreadId thread_id = PlatformThread::CurrentId(); | 548 PlatformThreadId thread_id = PlatformThread::CurrentId(); |
| 547 | 549 |
| 548 // Record the name of the calling thread, if not done already. | 550 const char* new_name = PlatformThread::GetName(); |
| 549 if (!g_current_thread_name_captured.Pointer()->Get()) { | 551 // Check if the thread name has been set or changed since the previous |
| 550 g_current_thread_name_captured.Pointer()->Set(true); | 552 // call (if any), but don't bother if the new name is empty. Note this will |
| 551 const char* cur_name = PlatformThread::GetName(); | 553 // not detect a thread name change within the same char* buffer address: we |
| 554 // favor common case performance over corner case correctness. |
| 555 if (new_name != g_current_thread_name.Get().Get() && |
| 556 new_name && *new_name) { |
| 557 // Benign const cast. |
| 558 g_current_thread_name.Get().Set(const_cast<char*>(new_name)); |
| 552 base::hash_map<PlatformThreadId, std::string>::iterator existing_name = | 559 base::hash_map<PlatformThreadId, std::string>::iterator existing_name = |
| 553 thread_names_.find(thread_id); | 560 thread_names_.find(thread_id); |
| 554 if (existing_name == thread_names_.end()) { | 561 if (existing_name == thread_names_.end()) { |
| 555 // This is a new thread id, and a new name. | 562 // This is a new thread id, and a new name. |
| 556 thread_names_[thread_id] = cur_name ? cur_name : ""; | 563 thread_names_[thread_id] = new_name; |
| 557 } else if(cur_name != NULL) { | 564 } else { |
| 558 // This is a thread id that we've seen before, but potentially with a | 565 // This is a thread id that we've seen before, but potentially with a |
| 559 // new name. | 566 // new name. |
| 560 std::vector<std::string> existing_names; | 567 std::vector<base::StringPiece> existing_names; |
| 561 Tokenize(existing_name->second, std::string(","), &existing_names); | 568 Tokenize(existing_name->second, ",", &existing_names); |
| 562 bool found = std::find(existing_names.begin(), | 569 bool found = std::find(existing_names.begin(), |
| 563 existing_names.end(), | 570 existing_names.end(), |
| 564 cur_name) != existing_names.end(); | 571 new_name) != existing_names.end(); |
| 565 if (!found) { | 572 if (!found) { |
| 566 existing_names.push_back(cur_name); | 573 existing_name->second.push_back(','); |
| 567 thread_names_[thread_id] = | 574 existing_name->second.append(new_name); |
| 568 JoinString(existing_names, ','); | |
| 569 } | 575 } |
| 570 } | 576 } |
| 571 } | 577 } |
| 572 | 578 |
| 573 if (threshold_begin_id > -1) { | 579 if (threshold_begin_id > -1) { |
| 574 DCHECK(phase == base::debug::TRACE_EVENT_PHASE_END); | 580 DCHECK(phase == base::debug::TRACE_EVENT_PHASE_END); |
| 575 size_t begin_i = static_cast<size_t>(threshold_begin_id); | 581 size_t begin_i = static_cast<size_t>(threshold_begin_id); |
| 576 // Return now if there has been a flush since the begin event was posted. | 582 // Return now if there has been a flush since the begin event was posted. |
| 577 if (begin_i >= logged_events_.size()) | 583 if (begin_i >= logged_events_.size()) |
| 578 return -1; | 584 return -1; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 NULL, 0, NULL, 0, | 705 NULL, 0, NULL, 0, |
| 700 p_data_->threshold_begin_id, p_data_->threshold, | 706 p_data_->threshold_begin_id, p_data_->threshold, |
| 701 TraceLog::EVENT_FLAG_NONE); | 707 TraceLog::EVENT_FLAG_NONE); |
| 702 } | 708 } |
| 703 } | 709 } |
| 704 | 710 |
| 705 } // namespace internal | 711 } // namespace internal |
| 706 | 712 |
| 707 } // namespace debug | 713 } // namespace debug |
| 708 } // namespace base | 714 } // namespace base |
| OLD | NEW |