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

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: rebase. Created 4 years, 9 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
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
petrcermak 2016/03/29 13:07:46 Shouldn't you also do this for the main thread?
ssid 2016/03/29 18:19:11 replied below.
64 thread_id_to_handle_.find(id); 79 // is valid for the lifetime of the process.
65 80 trace_event::AllocationContextTracker::SetCurrentThreadName(
danakj 2016/03/29 00:47:36 Why not call GetName() when you need the name?
ssid 2016/03/29 18:19:11 I cannot call GetName from AllocationTracker becau
danakj 2016/03/29 20:12:41 Sorry I'm not understanding this, but why is it a
Primiano Tucci (use gerrit) 2016/03/31 15:48:28 Yeah that was my initial proposal, unfortunately w
66 // The main thread of a process will not be created as a Thread object which 81 leaked_str->c_str());
67 // means there is no PlatformThreadHandler registered.
68 if (id_to_handle_iter == thread_id_to_handle_.end()) {
69 main_process_name_ = leaked_str;
70 main_process_id_ = id;
71 return;
72 }
73 thread_handle_to_interned_name_[id_to_handle_iter->second] = leaked_str;
74 } 82 }
75 83
76 const char* ThreadIdNameManager::GetName(PlatformThreadId id) { 84 const char* ThreadIdNameManager::GetName(PlatformThreadId id) {
77 AutoLock locked(lock_); 85 AutoLock locked(lock_);
78 86
79 if (id == main_process_id_) 87 if (id == main_process_id_)
80 return main_process_name_->c_str(); 88 return main_process_name_->c_str();
81 89
82 ThreadIdToHandleMap::iterator id_to_handle_iter = 90 ThreadIdToHandleMap::iterator id_to_handle_iter =
83 thread_id_to_handle_.find(id); 91 thread_id_to_handle_.find(id);
(...skipping 19 matching lines...) Expand all
103 DCHECK((id_to_handle_iter!= thread_id_to_handle_.end())); 111 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 112 // The given |id| may have been re-used by the system. Make sure the
105 // mapping points to the provided |handle| before removal. 113 // mapping points to the provided |handle| before removal.
106 if (id_to_handle_iter->second != handle) 114 if (id_to_handle_iter->second != handle)
107 return; 115 return;
108 116
109 thread_id_to_handle_.erase(id_to_handle_iter); 117 thread_id_to_handle_.erase(id_to_handle_iter);
110 } 118 }
111 119
112 } // namespace base 120 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698