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

Side by Side Diff: base/threading/thread_id_name_manager.cc

Issue 1814083002: [tracing] Add thread name to the pseudo stack of heap profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@profiler
Patch Set: Add comment. Created 4 years, 8 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
« no previous file with comments | « no previous file | base/trace_event/heap_profiler_allocation_context_tracker.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/threading/thread_id_name_manager.h" 5 #include "base/threading/thread_id_name_manager.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/singleton.h" 11 #include "base/memory/singleton.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
13 14
14 namespace base { 15 namespace base {
15 namespace { 16 namespace {
16 17
17 static const char kDefaultName[] = ""; 18 static const char kDefaultName[] = "";
18 static std::string* g_default_name; 19 static std::string* g_default_name;
19 20
20 } 21 }
21 22
22 ThreadIdNameManager::ThreadIdNameManager() 23 ThreadIdNameManager::ThreadIdNameManager()
(...skipping 20 matching lines...) Expand all
43 void ThreadIdNameManager::RegisterThread(PlatformThreadHandle::Handle handle, 44 void ThreadIdNameManager::RegisterThread(PlatformThreadHandle::Handle handle,
44 PlatformThreadId id) { 45 PlatformThreadId id) {
45 AutoLock locked(lock_); 46 AutoLock locked(lock_);
46 thread_id_to_handle_[id] = handle; 47 thread_id_to_handle_[id] = handle;
47 thread_handle_to_interned_name_[handle] = 48 thread_handle_to_interned_name_[handle] =
48 name_to_interned_name_[kDefaultName]; 49 name_to_interned_name_[kDefaultName];
49 } 50 }
50 51
51 void ThreadIdNameManager::SetName(PlatformThreadId id, 52 void ThreadIdNameManager::SetName(PlatformThreadId id,
52 const std::string& name) { 53 const std::string& name) {
53 AutoLock locked(lock_);
54 NameToInternedNameMap::iterator iter = name_to_interned_name_.find(name);
55 std::string* leaked_str = NULL; 54 std::string* leaked_str = NULL;
56 if (iter != name_to_interned_name_.end()) { 55 {
57 leaked_str = iter->second; 56 AutoLock locked(lock_);
58 } else { 57 NameToInternedNameMap::iterator iter = name_to_interned_name_.find(name);
59 leaked_str = new std::string(name); 58 if (iter != name_to_interned_name_.end()) {
60 name_to_interned_name_[name] = leaked_str; 59 leaked_str = iter->second;
60 } else {
61 leaked_str = new std::string(name);
62 name_to_interned_name_[name] = leaked_str;
63 }
64
65 ThreadIdToHandleMap::iterator id_to_handle_iter =
66 thread_id_to_handle_.find(id);
67
68 // The main thread of a process will not be created as a Thread object which
69 // means there is no PlatformThreadHandler registered.
70 if (id_to_handle_iter == thread_id_to_handle_.end()) {
71 main_process_name_ = leaked_str;
72 main_process_id_ = id;
73 return;
74 }
75 thread_handle_to_interned_name_[id_to_handle_iter->second] = leaked_str;
61 } 76 }
62 77
63 ThreadIdToHandleMap::iterator id_to_handle_iter = 78 // Add the leaked thread name to heap profiler context tracker. The name added
64 thread_id_to_handle_.find(id); 79 // is valid for the lifetime of the process. AllocationContextTracker cannot
65 80 // call GetName(which holds a lock) during the first allocation because it can
66 // The main thread of a process will not be created as a Thread object which 81 // cause a deadlock when the first allocation happens in the
67 // means there is no PlatformThreadHandler registered. 82 // ThreadIdNameManager itself when holding the lock.
68 if (id_to_handle_iter == thread_id_to_handle_.end()) { 83 trace_event::AllocationContextTracker::SetCurrentThreadName(
69 main_process_name_ = leaked_str; 84 leaked_str->c_str());
70 main_process_id_ = id;
71 return;
72 }
73 thread_handle_to_interned_name_[id_to_handle_iter->second] = leaked_str;
74 } 85 }
75 86
76 const char* ThreadIdNameManager::GetName(PlatformThreadId id) { 87 const char* ThreadIdNameManager::GetName(PlatformThreadId id) {
77 AutoLock locked(lock_); 88 AutoLock locked(lock_);
78 89
79 if (id == main_process_id_) 90 if (id == main_process_id_)
80 return main_process_name_->c_str(); 91 return main_process_name_->c_str();
81 92
82 ThreadIdToHandleMap::iterator id_to_handle_iter = 93 ThreadIdToHandleMap::iterator id_to_handle_iter =
83 thread_id_to_handle_.find(id); 94 thread_id_to_handle_.find(id);
(...skipping 19 matching lines...) Expand all
103 DCHECK((id_to_handle_iter!= thread_id_to_handle_.end())); 114 DCHECK((id_to_handle_iter!= thread_id_to_handle_.end()));
104 // The given |id| may have been re-used by the system. Make sure the 115 // The given |id| may have been re-used by the system. Make sure the
105 // mapping points to the provided |handle| before removal. 116 // mapping points to the provided |handle| before removal.
106 if (id_to_handle_iter->second != handle) 117 if (id_to_handle_iter->second != handle)
107 return; 118 return;
108 119
109 thread_id_to_handle_.erase(id_to_handle_iter); 120 thread_id_to_handle_.erase(id_to_handle_iter);
110 } 121 }
111 122
112 } // namespace base 123 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/trace_event/heap_profiler_allocation_context_tracker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698