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

Side by Side Diff: base/debug/trace_event_impl.cc

Issue 11438022: Add ability to retrieve a thread_name given a thread_id. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ThreadLocal for the current interned string value. Created 7 years, 11 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 | Annotate | Revision Log
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/debug/trace_event_impl.h" 5 #include "base/debug/trace_event_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/debug/leak_annotations.h" 10 #include "base/debug/leak_annotations.h"
11 #include "base/debug/trace_event.h" 11 #include "base/debug/trace_event.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/memory/singleton.h" 14 #include "base/memory/singleton.h"
15 #include "base/process_util.h" 15 #include "base/process_util.h"
16 #include "base/stl_util.h" 16 #include "base/stl_util.h"
17 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "base/string_tokenizer.h" 18 #include "base/string_tokenizer.h"
19 #include "base/string_util.h" 19 #include "base/string_util.h"
20 #include "base/sys_info.h" 20 #include "base/sys_info.h"
21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
22 #include "base/threading/platform_thread.h" 22 #include "base/threading/platform_thread.h"
23 #include "base/threading/thread_id_name_manager.h"
23 #include "base/threading/thread_local.h" 24 #include "base/threading/thread_local.h"
24 #include "base/time.h" 25 #include "base/time.h"
25 #include "base/utf_string_conversions.h" 26 #include "base/utf_string_conversions.h"
26 27
27 #if defined(OS_WIN) 28 #if defined(OS_WIN)
28 #include "base/debug/trace_event_win.h" 29 #include "base/debug/trace_event_win.h"
29 #endif 30 #endif
30 31
31 class DeleteTraceLogForTesting { 32 class DeleteTraceLogForTesting {
32 public: 33 public:
(...skipping 25 matching lines...) Expand all
58 "tracing categories exhausted; must increase TRACE_EVENT_MAX_CATEGORIES", 59 "tracing categories exhausted; must increase TRACE_EVENT_MAX_CATEGORIES",
59 "__metadata", 60 "__metadata",
60 }; 61 };
61 // The enabled flag is char instead of bool so that the API can be used from C. 62 // The enabled flag is char instead of bool so that the API can be used from C.
62 unsigned char g_category_enabled[TRACE_EVENT_MAX_CATEGORIES] = { 0 }; 63 unsigned char g_category_enabled[TRACE_EVENT_MAX_CATEGORIES] = { 0 };
63 const int g_category_already_shutdown = 0; 64 const int g_category_already_shutdown = 0;
64 const int g_category_categories_exhausted = 1; 65 const int g_category_categories_exhausted = 1;
65 const int g_category_metadata = 2; 66 const int g_category_metadata = 2;
66 int g_category_index = 3; // skip initial 3 categories 67 int g_category_index = 3; // skip initial 3 categories
67 68
68 // The most-recently captured name of the current thread 69 // The name of the current thread. This is used to decide if the current
69 LazyInstance<ThreadLocalPointer<const char> >::Leaky 70 // thread name has changed. We combine all the seen thread names into the
70 g_current_thread_name = LAZY_INSTANCE_INITIALIZER; 71 // output name for the thread.
72 LazyInstance<ThreadLocalPointer<base::InternedString> >::Leaky
73 g_current_thread_interned_name = LAZY_INSTANCE_INITIALIZER;
71 74
72 } // namespace 75 } // namespace
73 76
74 //////////////////////////////////////////////////////////////////////////////// 77 ////////////////////////////////////////////////////////////////////////////////
75 // 78 //
76 // TraceEvent 79 // TraceEvent
77 // 80 //
78 //////////////////////////////////////////////////////////////////////////////// 81 ////////////////////////////////////////////////////////////////////////////////
79 82
80 namespace { 83 namespace {
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 TimeTicks now = TimeTicks::NowFromSystemTraceTime() - time_offset_; 631 TimeTicks now = TimeTicks::NowFromSystemTraceTime() - time_offset_;
629 NotificationHelper notifier(this); 632 NotificationHelper notifier(this);
630 { 633 {
631 AutoLock lock(lock_); 634 AutoLock lock(lock_);
632 if (*category_enabled != CATEGORY_ENABLED) 635 if (*category_enabled != CATEGORY_ENABLED)
633 return; 636 return;
634 if (logged_events_.size() >= kTraceEventBufferSize) 637 if (logged_events_.size() >= kTraceEventBufferSize)
635 return; 638 return;
636 639
637 int thread_id = static_cast<int>(PlatformThread::CurrentId()); 640 int thread_id = static_cast<int>(PlatformThread::CurrentId());
641 ThreadIdNameManager* manager = ThreadIdNameManager::GetInstance();
642 InternedString interned_name = manager->GetInternedName(thread_id);
jar (doing other things) 2013/01/17 23:18:39 nit: declare/define vars as close as possible to f
dsinclair 2013/01/21 19:14:18 Done.
638 643
639 const char* new_name = PlatformThread::GetName(); 644
645 if (!g_current_thread_interned_name.Get().Get())
jar (doing other things) 2013/01/17 23:18:39 nit: not sure about perf impact... but I suspect y
dsinclair 2013/01/21 19:14:18 Done.
646 g_current_thread_interned_name.Get().Set(
647 new InternedString(base::kDefaultInternedString));
jar (doing other things) 2013/01/17 23:18:39 nit: use curlies around any if when you span more
dsinclair 2013/01/21 19:14:18 Done.
640 // Check if the thread name has been set or changed since the previous 648 // Check if the thread name has been set or changed since the previous
641 // call (if any), but don't bother if the new name is empty. Note this will 649 // call (if any), but don't bother if the new name is empty. Note this will
642 // not detect a thread name change within the same char* buffer address: we 650 // not detect a thread name change within the same char* buffer address: we
643 // favor common case performance over corner case correctness. 651 // favor common case performance over corner case correctness.
644 if (new_name != g_current_thread_name.Get().Get() && 652 if (interned_name != *(g_current_thread_interned_name.Get().Get())) {
645 new_name && *new_name) { 653 const char* new_name = manager->GetInternedStringValue(interned_name);
646 g_current_thread_name.Get().Set(new_name); 654 if (new_name && *new_name) {
jar (doing other things) 2013/01/17 23:18:39 See comments about GetInternedStringValue(). I do
dsinclair 2013/01/21 19:14:18 Done.
647 base::hash_map<int, std::string>::iterator existing_name = 655 *(g_current_thread_interned_name.Get().Get()) = interned_name;
648 thread_names_.find(thread_id); 656
649 if (existing_name == thread_names_.end()) { 657 base::hash_map<int, std::string>::iterator existing_name =
650 // This is a new thread id, and a new name. 658 thread_names_.find(thread_id);
651 thread_names_[thread_id] = new_name; 659 if (existing_name == thread_names_.end()) {
652 } else { 660 // This is a new thread id, and a new name.
653 // This is a thread id that we've seen before, but potentially with a 661 thread_names_[thread_id] = new_name;
654 // new name. 662 } else {
655 std::vector<base::StringPiece> existing_names; 663 // This is a thread id that we've seen before, but potentially with a
656 Tokenize(existing_name->second, ",", &existing_names); 664 // new name.
657 bool found = std::find(existing_names.begin(), 665 std::vector<base::StringPiece> existing_names;
658 existing_names.end(), 666 Tokenize(existing_name->second, ",", &existing_names);
659 new_name) != existing_names.end(); 667 bool found = std::find(existing_names.begin(),
660 if (!found) { 668 existing_names.end(),
661 existing_name->second.push_back(','); 669 new_name) != existing_names.end();
662 existing_name->second.append(new_name); 670 if (!found) {
671 existing_name->second.push_back(',');
672 existing_name->second.append(new_name);
673 }
663 } 674 }
664 } 675 }
665 } 676 }
666 677
667 if (flags & TRACE_EVENT_FLAG_MANGLE_ID) 678 if (flags & TRACE_EVENT_FLAG_MANGLE_ID)
668 id ^= process_id_hash_; 679 id ^= process_id_hash_;
669 680
670 logged_events_.push_back( 681 logged_events_.push_back(
671 TraceEvent(thread_id, 682 TraceEvent(thread_id,
672 now, phase, category_enabled, name, id, 683 now, phase, category_enabled, name, id,
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 unsigned long long pid = static_cast<unsigned long long>(process_id_); 791 unsigned long long pid = static_cast<unsigned long long>(process_id_);
781 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; 792 process_id_hash_ = (offset_basis ^ pid) * fnv_prime;
782 } 793 }
783 794
784 void TraceLog::SetTimeOffset(TimeDelta offset) { 795 void TraceLog::SetTimeOffset(TimeDelta offset) {
785 time_offset_ = offset; 796 time_offset_ = offset;
786 } 797 }
787 798
788 } // namespace debug 799 } // namespace debug
789 } // namespace base 800 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698