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

Side by Side Diff: base/metrics/stats_table.cc

Issue 22911027: Pass StatsTable shared memory via global descriptors on Posix rather than using named shared memory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Jar's comments. Created 7 years, 4 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 | « base/metrics/stats_table.h ('k') | content/browser/child_process_launcher.cc » ('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) 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/metrics/stats_table.h" 5 #include "base/metrics/stats_table.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/shared_memory.h" 9 #include "base/memory/shared_memory.h"
10 #include "base/process/process_handle.h" 10 #include "base/process/process_handle.h"
11 #include "base/strings/string_piece.h" 11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 #include "base/threading/thread_local_storage.h" 15 #include "base/threading/thread_local_storage.h"
16 16
17 #if defined(OS_POSIX) 17 #if defined(OS_POSIX)
18 #include "base/posix/global_descriptors.h"
18 #include "errno.h" 19 #include "errno.h"
20 #include "ipc/ipc_descriptors.h"
19 #endif 21 #endif
20 22
21 namespace base { 23 namespace base {
22 24
23 // The StatsTable uses a shared memory segment that is laid out as follows 25 // The StatsTable uses a shared memory segment that is laid out as follows
24 // 26 //
25 // +-------------------------------------------+ 27 // +-------------------------------------------+
26 // | Version | Size | MaxCounters | MaxThreads | 28 // | Version | Size | MaxCounters | MaxThreads |
27 // +-------------------------------------------+ 29 // +-------------------------------------------+
28 // | Thread names table | 30 // | Thread names table |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 struct TableHeader { 99 struct TableHeader {
98 int version; 100 int version;
99 int size; 101 int size;
100 int max_counters; 102 int max_counters;
101 int max_threads; 103 int max_threads;
102 }; 104 };
103 105
104 // Construct a new Private based on expected size parameters, or 106 // Construct a new Private based on expected size parameters, or
105 // return NULL on failure. 107 // return NULL on failure.
106 static Private* New(const std::string& name, int size, 108 static Private* New(const std::string& name, int size,
107 int max_threads, int max_counters); 109 int max_threads, int max_counters);
jar (doing other things) 2013/08/22 00:29:38 nit: fix indentation. Chromium standard for decla
rmcilroy 2013/08/22 10:03:55 Done.
108 110
109 SharedMemory* shared_memory() { return &shared_memory_; } 111 SharedMemory* shared_memory() { return shared_memory_.get(); }
110 112
111 // Accessors for our header pointers 113 // Accessors for our header pointers
112 TableHeader* table_header() const { return table_header_; } 114 TableHeader* table_header() const { return table_header_; }
113 int version() const { return table_header_->version; } 115 int version() const { return table_header_->version; }
114 int size() const { return table_header_->size; } 116 int size() const { return table_header_->size; }
115 int max_counters() const { return table_header_->max_counters; } 117 int max_counters() const { return table_header_->max_counters; }
116 int max_threads() const { return table_header_->max_threads; } 118 int max_threads() const { return table_header_->max_threads; }
117 119
118 // Accessors for our tables 120 // Accessors for our tables
119 char* thread_name(int slot_id) const { 121 char* thread_name(int slot_id) const {
120 return &thread_names_table_[ 122 return &thread_names_table_[
121 (slot_id-1) * (StatsTable::kMaxThreadNameLength)]; 123 (slot_id-1) * (StatsTable::kMaxThreadNameLength)];
122 } 124 }
123 PlatformThreadId* thread_tid(int slot_id) const { 125 PlatformThreadId* thread_tid(int slot_id) const {
124 return &(thread_tid_table_[slot_id-1]); 126 return &(thread_tid_table_[slot_id-1]);
125 } 127 }
126 int* thread_pid(int slot_id) const { 128 int* thread_pid(int slot_id) const {
127 return &(thread_pid_table_[slot_id-1]); 129 return &(thread_pid_table_[slot_id-1]);
128 } 130 }
129 char* counter_name(int counter_id) const { 131 char* counter_name(int counter_id) const {
130 return &counter_names_table_[ 132 return &counter_names_table_[
131 (counter_id-1) * (StatsTable::kMaxCounterNameLength)]; 133 (counter_id-1) * (StatsTable::kMaxCounterNameLength)];
132 } 134 }
133 int* row(int counter_id) const { 135 int* row(int counter_id) const {
134 return &data_table_[(counter_id-1) * max_threads()]; 136 return &data_table_[(counter_id-1) * max_threads()];
135 } 137 }
136 138
137 private: 139 private:
138 // Constructor is private because you should use New() instead. 140 // Constructor is private because you should use New() instead.
139 Private() 141 explicit Private(SharedMemory* shared_memory)
140 : table_header_(NULL), 142 : shared_memory_(shared_memory),
143 table_header_(NULL),
141 thread_names_table_(NULL), 144 thread_names_table_(NULL),
142 thread_tid_table_(NULL), 145 thread_tid_table_(NULL),
143 thread_pid_table_(NULL), 146 thread_pid_table_(NULL),
144 counter_names_table_(NULL), 147 counter_names_table_(NULL),
145 data_table_(NULL) { 148 data_table_(NULL) {
146 } 149 }
147 150
151 // Create or open the SharedMemory used by the stats table.
152 static SharedMemory* CreateSharedMemory(int size);
153
148 // Initializes the table on first access. Sets header values 154 // Initializes the table on first access. Sets header values
149 // appropriately and zeroes all counters. 155 // appropriately and zeroes all counters.
150 void InitializeTable(void* memory, int size, int max_counters, 156 void InitializeTable(void* memory, int size, int max_counters,
151 int max_threads); 157 int max_threads);
152 158
153 // Initializes our in-memory pointers into a pre-created StatsTable. 159 // Initializes our in-memory pointers into a pre-created StatsTable.
154 void ComputeMappedPointers(void* memory); 160 void ComputeMappedPointers(void* memory);
155 161
156 SharedMemory shared_memory_; 162 scoped_ptr<SharedMemory> shared_memory_;
157 TableHeader* table_header_; 163 TableHeader* table_header_;
158 char* thread_names_table_; 164 char* thread_names_table_;
159 PlatformThreadId* thread_tid_table_; 165 PlatformThreadId* thread_tid_table_;
160 int* thread_pid_table_; 166 int* thread_pid_table_;
161 char* counter_names_table_; 167 char* counter_names_table_;
162 int* data_table_; 168 int* data_table_;
163 }; 169 };
jar (doing other things) 2013/08/22 00:29:38 nit: Probably NO_DEFAULT_COPY_OR_ASSIGN(...)
rmcilroy 2013/08/22 10:03:55 Done (DISALLOW_COPY_AND_ASSIGN)
164 170
165 // static 171 // static
166 StatsTable::Private* StatsTable::Private::New(const std::string& name, 172 StatsTable::Private* StatsTable::Private::New(const std::string& name,
167 int size, 173 int size,
168 int max_threads, 174 int max_threads,
169 int max_counters) { 175 int max_counters) {
170 scoped_ptr<Private> priv(new Private()); 176 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(size));
171 if (!priv->shared_memory_.CreateNamed(name, true, size)) 177 if (!shared_memory.get())
172 return NULL; 178 return NULL;
173 if (!priv->shared_memory_.Map(size)) 179 if (!shared_memory->Map(size))
174 return NULL; 180 return NULL;
175 void* memory = priv->shared_memory_.memory(); 181 void* memory = shared_memory->memory();
176 182
183 scoped_ptr<Private> priv(new Private(shared_memory.release()));
jar (doing other things) 2013/08/22 00:29:38 nit: Style: Don't abbreviate in names. Sadly, the
rmcilroy 2013/08/22 10:03:55 Done (went with Internal)
177 TableHeader* header = static_cast<TableHeader*>(memory); 184 TableHeader* header = static_cast<TableHeader*>(memory);
178 185
179 // If the version does not match, then assume the table needs 186 // If the version does not match, then assume the table needs
180 // to be initialized. 187 // to be initialized.
181 if (header->version != kTableVersion) 188 if (header->version != kTableVersion)
182 priv->InitializeTable(memory, size, max_counters, max_threads); 189 priv->InitializeTable(memory, size, max_counters, max_threads);
183 190
184 // We have a valid table, so compute our pointers. 191 // We have a valid table, so compute our pointers.
185 priv->ComputeMappedPointers(memory); 192 priv->ComputeMappedPointers(memory);
186 193
187 return priv.release(); 194 return priv.release();
188 } 195 }
189 196
197 // static
198 SharedMemory* StatsTable::Private::CreateSharedMemory(int size) {
199 #if defined(OS_POSIX)
200 GlobalDescriptors* global_descriptors = GlobalDescriptors::GetInstance();
201 if (global_descriptors->MaybeGet(kStatsTableSharedMemFd) != -1) {
202 // Open the shared memory file descriptor passed by the browser process.
203 FileDescriptor file_descriptor(
204 global_descriptors->Get(kStatsTableSharedMemFd), false);
205 return new SharedMemory(file_descriptor, false);
206 }
207 // Otherwise we need to create it.
208 scoped_ptr<SharedMemory> shared_memory(new SharedMemory());
209 if (!shared_memory->CreateAnonymous(size))
210 return NULL;
211 return shared_memory.release();
212 #elif defined(OS_WIN)
213 scoped_ptr<SharedMemory> shared_memory(new SharedMemory());
214 if (!shared_memory.CreateNamed(name, true, size))
215 return NULL;
216 return shared_memory.release();
217 #endif
218 }
219
190 void StatsTable::Private::InitializeTable(void* memory, int size, 220 void StatsTable::Private::InitializeTable(void* memory, int size,
191 int max_counters, 221 int max_counters,
192 int max_threads) { 222 int max_threads) {
193 // Zero everything. 223 // Zero everything.
194 memset(memory, 0, size); 224 memset(memory, 0, size);
195 225
196 // Initialize the header. 226 // Initialize the header.
197 TableHeader* header = static_cast<TableHeader*>(memory); 227 TableHeader* header = static_cast<TableHeader*>(memory);
198 header->version = kTableVersion; 228 header->version = kTableVersion;
199 header->size = size; 229 header->size = size;
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 TLSData* data = 588 TLSData* data =
559 static_cast<TLSData*>(tls_index_.Get()); 589 static_cast<TLSData*>(tls_index_.Get());
560 if (!data) 590 if (!data)
561 return NULL; 591 return NULL;
562 592
563 DCHECK(data->slot); 593 DCHECK(data->slot);
564 DCHECK_EQ(data->table, this); 594 DCHECK_EQ(data->table, this);
565 return data; 595 return data;
566 } 596 }
567 597
598 #if defined(OS_POSIX)
599 SharedMemoryHandle StatsTable::GetSharedMemoryHandle() const {
600 return impl_ ? impl_->shared_memory()->handle() : SharedMemory::NULLHandle();
601 }
602 #endif
603
568 } // namespace base 604 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/stats_table.h ('k') | content/browser/child_process_launcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698