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

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: Store a version number for the current name for fast comparisons. Created 8 years 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"
14 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
15 #include "base/process_util.h" 14 #include "base/process_util.h"
16 #include "base/stl_util.h" 15 #include "base/stl_util.h"
17 #include "base/stringprintf.h" 16 #include "base/stringprintf.h"
18 #include "base/string_tokenizer.h" 17 #include "base/string_tokenizer.h"
19 #include "base/string_util.h" 18 #include "base/string_util.h"
20 #include "base/sys_info.h" 19 #include "base/sys_info.h"
21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 20 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
22 #include "base/threading/platform_thread.h" 21 #include "base/threading/platform_thread.h"
23 #include "base/threading/thread_local.h" 22 #include "base/threading/thread_local.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 "tracing categories exhausted; must increase TRACE_EVENT_MAX_CATEGORIES", 57 "tracing categories exhausted; must increase TRACE_EVENT_MAX_CATEGORIES",
59 "__metadata", 58 "__metadata",
60 }; 59 };
61 // The enabled flag is char instead of bool so that the API can be used from C. 60 // 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 }; 61 unsigned char g_category_enabled[TRACE_EVENT_MAX_CATEGORIES] = { 0 };
63 const int g_category_already_shutdown = 0; 62 const int g_category_already_shutdown = 0;
64 const int g_category_categories_exhausted = 1; 63 const int g_category_categories_exhausted = 1;
65 const int g_category_metadata = 2; 64 const int g_category_metadata = 2;
66 int g_category_index = 3; // skip initial 3 categories 65 int g_category_index = 3; // skip initial 3 categories
67 66
68 // The most-recently captured name of the current thread 67 // The most-recently captured name version of the current thread
jar (doing other things) 2012/12/14 18:23:07 Please explain what a "name version" is for a thre
dsinclair 2012/12/14 19:13:46 Done.
jar (doing other things) 2012/12/21 00:16:59 The comment change was helpful, as I now am able t
dsinclair 2012/12/21 16:28:59 Done.
69 LazyInstance<ThreadLocalPointer<const char> >::Leaky 68 uint32 g_current_thread_name_version = 0;
70 g_current_thread_name = LAZY_INSTANCE_INITIALIZER;
71 69
72 } // namespace 70 } // namespace
73 71
74 //////////////////////////////////////////////////////////////////////////////// 72 ////////////////////////////////////////////////////////////////////////////////
75 // 73 //
76 // TraceEvent 74 // TraceEvent
77 // 75 //
78 //////////////////////////////////////////////////////////////////////////////// 76 ////////////////////////////////////////////////////////////////////////////////
79 77
80 namespace { 78 namespace {
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 { 628 {
631 AutoLock lock(lock_); 629 AutoLock lock(lock_);
632 if (*category_enabled != CATEGORY_ENABLED) 630 if (*category_enabled != CATEGORY_ENABLED)
633 return; 631 return;
634 if (logged_events_.size() >= kTraceEventBufferSize) 632 if (logged_events_.size() >= kTraceEventBufferSize)
635 return; 633 return;
636 634
637 int thread_id = static_cast<int>(PlatformThread::CurrentId()); 635 int thread_id = static_cast<int>(PlatformThread::CurrentId());
638 636
639 const char* new_name = PlatformThread::GetName(); 637 const char* new_name = PlatformThread::GetName();
638 uint32 new_version = PlatformThread::GetVersionForName();
640 // Check if the thread name has been set or changed since the previous 639 // 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 640 // 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 641 // not detect a thread name change within the same char* buffer address: we
643 // favor common case performance over corner case correctness. 642 // favor common case performance over corner case correctness.
644 if (new_name != g_current_thread_name.Get().Get() && 643 if (new_version != g_current_thread_name_version &&
645 new_name && *new_name) { 644 new_name && *new_name) {
646 g_current_thread_name.Get().Set(new_name); 645 g_current_thread_name_version = new_version;
646
647 base::hash_map<int, std::string>::iterator existing_name = 647 base::hash_map<int, std::string>::iterator existing_name =
648 thread_names_.find(thread_id); 648 thread_names_.find(thread_id);
649 if (existing_name == thread_names_.end()) { 649 if (existing_name == thread_names_.end()) {
650 // This is a new thread id, and a new name. 650 // This is a new thread id, and a new name.
651 thread_names_[thread_id] = new_name; 651 thread_names_[thread_id] = new_name;
652 } else { 652 } else {
653 // This is a thread id that we've seen before, but potentially with a 653 // This is a thread id that we've seen before, but potentially with a
654 // new name. 654 // new name.
655 std::vector<base::StringPiece> existing_names; 655 std::vector<base::StringPiece> existing_names;
656 Tokenize(existing_name->second, ",", &existing_names); 656 Tokenize(existing_name->second, ",", &existing_names);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 unsigned long long pid = static_cast<unsigned long long>(process_id_); 780 unsigned long long pid = static_cast<unsigned long long>(process_id_);
781 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; 781 process_id_hash_ = (offset_basis ^ pid) * fnv_prime;
782 } 782 }
783 783
784 void TraceLog::SetTimeOffset(TimeDelta offset) { 784 void TraceLog::SetTimeOffset(TimeDelta offset) {
785 time_offset_ = offset; 785 time_offset_ = offset;
786 } 786 }
787 787
788 } // namespace debug 788 } // namespace debug
789 } // namespace base 789 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698