OLD | NEW |
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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 inline int AlignOffset(int offset) { | 83 inline int AlignOffset(int offset) { |
82 return (sizeof(int) - (offset % sizeof(int))) % sizeof(int); | 84 return (sizeof(int) - (offset % sizeof(int))) % sizeof(int); |
83 } | 85 } |
84 | 86 |
85 inline int AlignedSize(int size) { | 87 inline int AlignedSize(int size) { |
86 return size + AlignOffset(size); | 88 return size + AlignOffset(size); |
87 } | 89 } |
88 | 90 |
89 } // namespace | 91 } // namespace |
90 | 92 |
91 // The StatsTable::Private maintains convenience pointers into the | 93 // The StatsTable::Internal maintains convenience pointers into the |
92 // shared memory segment. Use this class to keep the data structure | 94 // shared memory segment. Use this class to keep the data structure |
93 // clean and accessible. | 95 // clean and accessible. |
94 class StatsTable::Private { | 96 class StatsTable::Internal { |
95 public: | 97 public: |
96 // Various header information contained in the memory mapped segment. | 98 // Various header information contained in the memory mapped segment. |
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 Internal 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 Internal* New(const std::string& name, |
107 int max_threads, int max_counters); | 109 int size, |
| 110 int max_threads, |
| 111 int max_counters); |
108 | 112 |
109 SharedMemory* shared_memory() { return &shared_memory_; } | 113 SharedMemory* shared_memory() { return shared_memory_.get(); } |
110 | 114 |
111 // Accessors for our header pointers | 115 // Accessors for our header pointers |
112 TableHeader* table_header() const { return table_header_; } | 116 TableHeader* table_header() const { return table_header_; } |
113 int version() const { return table_header_->version; } | 117 int version() const { return table_header_->version; } |
114 int size() const { return table_header_->size; } | 118 int size() const { return table_header_->size; } |
115 int max_counters() const { return table_header_->max_counters; } | 119 int max_counters() const { return table_header_->max_counters; } |
116 int max_threads() const { return table_header_->max_threads; } | 120 int max_threads() const { return table_header_->max_threads; } |
117 | 121 |
118 // Accessors for our tables | 122 // Accessors for our tables |
119 char* thread_name(int slot_id) const { | 123 char* thread_name(int slot_id) const { |
120 return &thread_names_table_[ | 124 return &thread_names_table_[ |
121 (slot_id-1) * (StatsTable::kMaxThreadNameLength)]; | 125 (slot_id-1) * (StatsTable::kMaxThreadNameLength)]; |
122 } | 126 } |
123 PlatformThreadId* thread_tid(int slot_id) const { | 127 PlatformThreadId* thread_tid(int slot_id) const { |
124 return &(thread_tid_table_[slot_id-1]); | 128 return &(thread_tid_table_[slot_id-1]); |
125 } | 129 } |
126 int* thread_pid(int slot_id) const { | 130 int* thread_pid(int slot_id) const { |
127 return &(thread_pid_table_[slot_id-1]); | 131 return &(thread_pid_table_[slot_id-1]); |
128 } | 132 } |
129 char* counter_name(int counter_id) const { | 133 char* counter_name(int counter_id) const { |
130 return &counter_names_table_[ | 134 return &counter_names_table_[ |
131 (counter_id-1) * (StatsTable::kMaxCounterNameLength)]; | 135 (counter_id-1) * (StatsTable::kMaxCounterNameLength)]; |
132 } | 136 } |
133 int* row(int counter_id) const { | 137 int* row(int counter_id) const { |
134 return &data_table_[(counter_id-1) * max_threads()]; | 138 return &data_table_[(counter_id-1) * max_threads()]; |
135 } | 139 } |
136 | 140 |
137 private: | 141 private: |
138 // Constructor is private because you should use New() instead. | 142 // Constructor is private because you should use New() instead. |
139 Private() | 143 explicit Internal(SharedMemory* shared_memory) |
140 : table_header_(NULL), | 144 : shared_memory_(shared_memory), |
| 145 table_header_(NULL), |
141 thread_names_table_(NULL), | 146 thread_names_table_(NULL), |
142 thread_tid_table_(NULL), | 147 thread_tid_table_(NULL), |
143 thread_pid_table_(NULL), | 148 thread_pid_table_(NULL), |
144 counter_names_table_(NULL), | 149 counter_names_table_(NULL), |
145 data_table_(NULL) { | 150 data_table_(NULL) { |
146 } | 151 } |
147 | 152 |
| 153 // Create or open the SharedMemory used by the stats table. |
| 154 static SharedMemory* CreateSharedMemory(const std::string& name, |
| 155 int size); |
| 156 |
148 // Initializes the table on first access. Sets header values | 157 // Initializes the table on first access. Sets header values |
149 // appropriately and zeroes all counters. | 158 // appropriately and zeroes all counters. |
150 void InitializeTable(void* memory, int size, int max_counters, | 159 void InitializeTable(void* memory, int size, int max_counters, |
151 int max_threads); | 160 int max_threads); |
152 | 161 |
153 // Initializes our in-memory pointers into a pre-created StatsTable. | 162 // Initializes our in-memory pointers into a pre-created StatsTable. |
154 void ComputeMappedPointers(void* memory); | 163 void ComputeMappedPointers(void* memory); |
155 | 164 |
156 SharedMemory shared_memory_; | 165 scoped_ptr<SharedMemory> shared_memory_; |
157 TableHeader* table_header_; | 166 TableHeader* table_header_; |
158 char* thread_names_table_; | 167 char* thread_names_table_; |
159 PlatformThreadId* thread_tid_table_; | 168 PlatformThreadId* thread_tid_table_; |
160 int* thread_pid_table_; | 169 int* thread_pid_table_; |
161 char* counter_names_table_; | 170 char* counter_names_table_; |
162 int* data_table_; | 171 int* data_table_; |
| 172 |
| 173 DISALLOW_COPY_AND_ASSIGN(Internal); |
163 }; | 174 }; |
164 | 175 |
165 // static | 176 // static |
166 StatsTable::Private* StatsTable::Private::New(const std::string& name, | 177 StatsTable::Internal* StatsTable::Internal::New(const std::string& name, |
167 int size, | 178 int size, |
168 int max_threads, | 179 int max_threads, |
169 int max_counters) { | 180 int max_counters) { |
170 scoped_ptr<Private> priv(new Private()); | 181 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(name, size)); |
171 if (!priv->shared_memory_.CreateNamed(name, true, size)) | 182 if (!shared_memory.get()) |
172 return NULL; | 183 return NULL; |
173 if (!priv->shared_memory_.Map(size)) | 184 if (!shared_memory->Map(size)) |
174 return NULL; | 185 return NULL; |
175 void* memory = priv->shared_memory_.memory(); | 186 void* memory = shared_memory->memory(); |
176 | 187 |
| 188 scoped_ptr<Internal> internal(new Internal(shared_memory.release())); |
177 TableHeader* header = static_cast<TableHeader*>(memory); | 189 TableHeader* header = static_cast<TableHeader*>(memory); |
178 | 190 |
179 // If the version does not match, then assume the table needs | 191 // If the version does not match, then assume the table needs |
180 // to be initialized. | 192 // to be initialized. |
181 if (header->version != kTableVersion) | 193 if (header->version != kTableVersion) |
182 priv->InitializeTable(memory, size, max_counters, max_threads); | 194 internal->InitializeTable(memory, size, max_counters, max_threads); |
183 | 195 |
184 // We have a valid table, so compute our pointers. | 196 // We have a valid table, so compute our pointers. |
185 priv->ComputeMappedPointers(memory); | 197 internal->ComputeMappedPointers(memory); |
186 | 198 |
187 return priv.release(); | 199 return internal.release(); |
188 } | 200 } |
189 | 201 |
190 void StatsTable::Private::InitializeTable(void* memory, int size, | 202 // static |
| 203 SharedMemory* StatsTable::Internal::CreateSharedMemory(const std::string& name, |
| 204 int size) { |
| 205 #if defined(OS_POSIX) |
| 206 GlobalDescriptors* global_descriptors = GlobalDescriptors::GetInstance(); |
| 207 if (global_descriptors->MaybeGet(kStatsTableSharedMemFd) != -1) { |
| 208 // Open the shared memory file descriptor passed by the browser process. |
| 209 FileDescriptor file_descriptor( |
| 210 global_descriptors->Get(kStatsTableSharedMemFd), false); |
| 211 return new SharedMemory(file_descriptor, false); |
| 212 } |
| 213 // Otherwise we need to create it. |
| 214 scoped_ptr<SharedMemory> shared_memory(new SharedMemory()); |
| 215 if (!shared_memory->CreateAnonymous(size)) |
| 216 return NULL; |
| 217 return shared_memory.release(); |
| 218 #elif defined(OS_WIN) |
| 219 scoped_ptr<SharedMemory> shared_memory(new SharedMemory()); |
| 220 if (!shared_memory->CreateNamed(name, true, size)) |
| 221 return NULL; |
| 222 return shared_memory.release(); |
| 223 #endif |
| 224 } |
| 225 |
| 226 void StatsTable::Internal::InitializeTable(void* memory, int size, |
191 int max_counters, | 227 int max_counters, |
192 int max_threads) { | 228 int max_threads) { |
193 // Zero everything. | 229 // Zero everything. |
194 memset(memory, 0, size); | 230 memset(memory, 0, size); |
195 | 231 |
196 // Initialize the header. | 232 // Initialize the header. |
197 TableHeader* header = static_cast<TableHeader*>(memory); | 233 TableHeader* header = static_cast<TableHeader*>(memory); |
198 header->version = kTableVersion; | 234 header->version = kTableVersion; |
199 header->size = size; | 235 header->size = size; |
200 header->max_counters = max_counters; | 236 header->max_counters = max_counters; |
201 header->max_threads = max_threads; | 237 header->max_threads = max_threads; |
202 } | 238 } |
203 | 239 |
204 void StatsTable::Private::ComputeMappedPointers(void* memory) { | 240 void StatsTable::Internal::ComputeMappedPointers(void* memory) { |
205 char* data = static_cast<char*>(memory); | 241 char* data = static_cast<char*>(memory); |
206 int offset = 0; | 242 int offset = 0; |
207 | 243 |
208 table_header_ = reinterpret_cast<TableHeader*>(data); | 244 table_header_ = reinterpret_cast<TableHeader*>(data); |
209 offset += sizeof(*table_header_); | 245 offset += sizeof(*table_header_); |
210 offset += AlignOffset(offset); | 246 offset += AlignOffset(offset); |
211 | 247 |
212 // Verify we're looking at a valid StatsTable. | 248 // Verify we're looking at a valid StatsTable. |
213 DCHECK_EQ(table_header_->version, kTableVersion); | 249 DCHECK_EQ(table_header_->version, kTableVersion); |
214 | 250 |
(...skipping 30 matching lines...) Expand all Loading... |
245 struct StatsTable::TLSData { | 281 struct StatsTable::TLSData { |
246 StatsTable* table; | 282 StatsTable* table; |
247 int slot; | 283 int slot; |
248 }; | 284 }; |
249 | 285 |
250 // We keep a singleton table which can be easily accessed. | 286 // We keep a singleton table which can be easily accessed. |
251 StatsTable* global_table = NULL; | 287 StatsTable* global_table = NULL; |
252 | 288 |
253 StatsTable::StatsTable(const std::string& name, int max_threads, | 289 StatsTable::StatsTable(const std::string& name, int max_threads, |
254 int max_counters) | 290 int max_counters) |
255 : impl_(NULL), | 291 : internal_(NULL), |
256 tls_index_(SlotReturnFunction) { | 292 tls_index_(SlotReturnFunction) { |
257 int table_size = | 293 int table_size = |
258 AlignedSize(sizeof(Private::TableHeader)) + | 294 AlignedSize(sizeof(Internal::TableHeader)) + |
259 AlignedSize((max_counters * sizeof(char) * kMaxCounterNameLength)) + | 295 AlignedSize((max_counters * sizeof(char) * kMaxCounterNameLength)) + |
260 AlignedSize((max_threads * sizeof(char) * kMaxThreadNameLength)) + | 296 AlignedSize((max_threads * sizeof(char) * kMaxThreadNameLength)) + |
261 AlignedSize(max_threads * sizeof(int)) + | 297 AlignedSize(max_threads * sizeof(int)) + |
262 AlignedSize(max_threads * sizeof(int)) + | 298 AlignedSize(max_threads * sizeof(int)) + |
263 AlignedSize((sizeof(int) * (max_counters * max_threads))); | 299 AlignedSize((sizeof(int) * (max_counters * max_threads))); |
264 | 300 |
265 impl_ = Private::New(name, table_size, max_threads, max_counters); | 301 internal_ = Internal::New(name, table_size, max_threads, max_counters); |
266 | 302 |
267 if (!impl_) | 303 if (!internal_) |
268 DPLOG(ERROR) << "StatsTable did not initialize"; | 304 DPLOG(ERROR) << "StatsTable did not initialize"; |
269 } | 305 } |
270 | 306 |
271 StatsTable::~StatsTable() { | 307 StatsTable::~StatsTable() { |
272 // Before we tear down our copy of the table, be sure to | 308 // Before we tear down our copy of the table, be sure to |
273 // unregister our thread. | 309 // unregister our thread. |
274 UnregisterThread(); | 310 UnregisterThread(); |
275 | 311 |
276 // Return ThreadLocalStorage. At this point, if any registered threads | 312 // Return ThreadLocalStorage. At this point, if any registered threads |
277 // still exist, they cannot Unregister. | 313 // still exist, they cannot Unregister. |
278 tls_index_.Free(); | 314 tls_index_.Free(); |
279 | 315 |
280 // Cleanup our shared memory. | 316 // Cleanup our shared memory. |
281 delete impl_; | 317 delete internal_; |
282 | 318 |
283 // If we are the global table, unregister ourselves. | 319 // If we are the global table, unregister ourselves. |
284 if (global_table == this) | 320 if (global_table == this) |
285 global_table = NULL; | 321 global_table = NULL; |
286 } | 322 } |
287 | 323 |
288 StatsTable* StatsTable::current() { | 324 StatsTable* StatsTable::current() { |
289 return global_table; | 325 return global_table; |
290 } | 326 } |
291 | 327 |
292 void StatsTable::set_current(StatsTable* value) { | 328 void StatsTable::set_current(StatsTable* value) { |
293 global_table = value; | 329 global_table = value; |
294 } | 330 } |
295 | 331 |
296 int StatsTable::GetSlot() const { | 332 int StatsTable::GetSlot() const { |
297 TLSData* data = GetTLSData(); | 333 TLSData* data = GetTLSData(); |
298 if (!data) | 334 if (!data) |
299 return 0; | 335 return 0; |
300 return data->slot; | 336 return data->slot; |
301 } | 337 } |
302 | 338 |
303 int StatsTable::RegisterThread(const std::string& name) { | 339 int StatsTable::RegisterThread(const std::string& name) { |
304 int slot = 0; | 340 int slot = 0; |
305 if (!impl_) | 341 if (!internal_) |
306 return 0; | 342 return 0; |
307 | 343 |
308 // Registering a thread requires that we lock the shared memory | 344 // Registering a thread requires that we lock the shared memory |
309 // so that two threads don't grab the same slot. Fortunately, | 345 // so that two threads don't grab the same slot. Fortunately, |
310 // thread creation shouldn't happen in inner loops. | 346 // thread creation shouldn't happen in inner loops. |
311 { | 347 { |
312 SharedMemoryAutoLock lock(impl_->shared_memory()); | 348 SharedMemoryAutoLock lock(internal_->shared_memory()); |
313 slot = FindEmptyThread(); | 349 slot = FindEmptyThread(); |
314 if (!slot) { | 350 if (!slot) { |
315 return 0; | 351 return 0; |
316 } | 352 } |
317 | 353 |
318 // We have space, so consume a column in the table. | 354 // We have space, so consume a column in the table. |
319 std::string thread_name = name; | 355 std::string thread_name = name; |
320 if (name.empty()) | 356 if (name.empty()) |
321 thread_name = kUnknownName; | 357 thread_name = kUnknownName; |
322 strlcpy(impl_->thread_name(slot), thread_name.c_str(), | 358 strlcpy(internal_->thread_name(slot), thread_name.c_str(), |
323 kMaxThreadNameLength); | 359 kMaxThreadNameLength); |
324 *(impl_->thread_tid(slot)) = PlatformThread::CurrentId(); | 360 *(internal_->thread_tid(slot)) = PlatformThread::CurrentId(); |
325 *(impl_->thread_pid(slot)) = GetCurrentProcId(); | 361 *(internal_->thread_pid(slot)) = GetCurrentProcId(); |
326 } | 362 } |
327 | 363 |
328 // Set our thread local storage. | 364 // Set our thread local storage. |
329 TLSData* data = new TLSData; | 365 TLSData* data = new TLSData; |
330 data->table = this; | 366 data->table = this; |
331 data->slot = slot; | 367 data->slot = slot; |
332 tls_index_.Set(data); | 368 tls_index_.Set(data); |
333 return slot; | 369 return slot; |
334 } | 370 } |
335 | 371 |
336 int StatsTable::CountThreadsRegistered() const { | 372 int StatsTable::CountThreadsRegistered() const { |
337 if (!impl_) | 373 if (!internal_) |
338 return 0; | 374 return 0; |
339 | 375 |
340 // Loop through the shared memory and count the threads that are active. | 376 // Loop through the shared memory and count the threads that are active. |
341 // We intentionally do not lock the table during the operation. | 377 // We intentionally do not lock the table during the operation. |
342 int count = 0; | 378 int count = 0; |
343 for (int index = 1; index <= impl_->max_threads(); index++) { | 379 for (int index = 1; index <= internal_->max_threads(); index++) { |
344 char* name = impl_->thread_name(index); | 380 char* name = internal_->thread_name(index); |
345 if (*name != '\0') | 381 if (*name != '\0') |
346 count++; | 382 count++; |
347 } | 383 } |
348 return count; | 384 return count; |
349 } | 385 } |
350 | 386 |
351 int StatsTable::FindCounter(const std::string& name) { | 387 int StatsTable::FindCounter(const std::string& name) { |
352 // Note: the API returns counters numbered from 1..N, although | 388 // Note: the API returns counters numbered from 1..N, although |
353 // internally, the array is 0..N-1. This is so that we can return | 389 // internally, the array is 0..N-1. This is so that we can return |
354 // zero as "not found". | 390 // zero as "not found". |
355 if (!impl_) | 391 if (!internal_) |
356 return 0; | 392 return 0; |
357 | 393 |
358 // Create a scope for our auto-lock. | 394 // Create a scope for our auto-lock. |
359 { | 395 { |
360 AutoLock scoped_lock(counters_lock_); | 396 AutoLock scoped_lock(counters_lock_); |
361 | 397 |
362 // Attempt to find the counter. | 398 // Attempt to find the counter. |
363 CountersMap::const_iterator iter; | 399 CountersMap::const_iterator iter; |
364 iter = counters_.find(name); | 400 iter = counters_.find(name); |
365 if (iter != counters_.end()) | 401 if (iter != counters_.end()) |
366 return iter->second; | 402 return iter->second; |
367 } | 403 } |
368 | 404 |
369 // Counter does not exist, so add it. | 405 // Counter does not exist, so add it. |
370 return AddCounter(name); | 406 return AddCounter(name); |
371 } | 407 } |
372 | 408 |
373 int* StatsTable::GetLocation(int counter_id, int slot_id) const { | 409 int* StatsTable::GetLocation(int counter_id, int slot_id) const { |
374 if (!impl_) | 410 if (!internal_) |
375 return NULL; | 411 return NULL; |
376 if (slot_id > impl_->max_threads()) | 412 if (slot_id > internal_->max_threads()) |
377 return NULL; | 413 return NULL; |
378 | 414 |
379 int* row = impl_->row(counter_id); | 415 int* row = internal_->row(counter_id); |
380 return &(row[slot_id-1]); | 416 return &(row[slot_id-1]); |
381 } | 417 } |
382 | 418 |
383 const char* StatsTable::GetRowName(int index) const { | 419 const char* StatsTable::GetRowName(int index) const { |
384 if (!impl_) | 420 if (!internal_) |
385 return NULL; | 421 return NULL; |
386 | 422 |
387 return impl_->counter_name(index); | 423 return internal_->counter_name(index); |
388 } | 424 } |
389 | 425 |
390 int StatsTable::GetRowValue(int index) const { | 426 int StatsTable::GetRowValue(int index) const { |
391 return GetRowValue(index, 0); | 427 return GetRowValue(index, 0); |
392 } | 428 } |
393 | 429 |
394 int StatsTable::GetRowValue(int index, int pid) const { | 430 int StatsTable::GetRowValue(int index, int pid) const { |
395 if (!impl_) | 431 if (!internal_) |
396 return 0; | 432 return 0; |
397 | 433 |
398 int rv = 0; | 434 int rv = 0; |
399 int* row = impl_->row(index); | 435 int* row = internal_->row(index); |
400 for (int slot_id = 1; slot_id <= impl_->max_threads(); slot_id++) { | 436 for (int slot_id = 1; slot_id <= internal_->max_threads(); slot_id++) { |
401 if (pid == 0 || *impl_->thread_pid(slot_id) == pid) | 437 if (pid == 0 || *internal_->thread_pid(slot_id) == pid) |
402 rv += row[slot_id-1]; | 438 rv += row[slot_id-1]; |
403 } | 439 } |
404 return rv; | 440 return rv; |
405 } | 441 } |
406 | 442 |
407 int StatsTable::GetCounterValue(const std::string& name) { | 443 int StatsTable::GetCounterValue(const std::string& name) { |
408 return GetCounterValue(name, 0); | 444 return GetCounterValue(name, 0); |
409 } | 445 } |
410 | 446 |
411 int StatsTable::GetCounterValue(const std::string& name, int pid) { | 447 int StatsTable::GetCounterValue(const std::string& name, int pid) { |
412 if (!impl_) | 448 if (!internal_) |
413 return 0; | 449 return 0; |
414 | 450 |
415 int row = FindCounter(name); | 451 int row = FindCounter(name); |
416 if (!row) | 452 if (!row) |
417 return 0; | 453 return 0; |
418 return GetRowValue(row, pid); | 454 return GetRowValue(row, pid); |
419 } | 455 } |
420 | 456 |
421 int StatsTable::GetMaxCounters() const { | 457 int StatsTable::GetMaxCounters() const { |
422 if (!impl_) | 458 if (!internal_) |
423 return 0; | 459 return 0; |
424 return impl_->max_counters(); | 460 return internal_->max_counters(); |
425 } | 461 } |
426 | 462 |
427 int StatsTable::GetMaxThreads() const { | 463 int StatsTable::GetMaxThreads() const { |
428 if (!impl_) | 464 if (!internal_) |
429 return 0; | 465 return 0; |
430 return impl_->max_threads(); | 466 return internal_->max_threads(); |
431 } | 467 } |
432 | 468 |
433 int* StatsTable::FindLocation(const char* name) { | 469 int* StatsTable::FindLocation(const char* name) { |
434 // Get the static StatsTable | 470 // Get the static StatsTable |
435 StatsTable *table = StatsTable::current(); | 471 StatsTable *table = StatsTable::current(); |
436 if (!table) | 472 if (!table) |
437 return NULL; | 473 return NULL; |
438 | 474 |
439 // Get the slot for this thread. Try to register | 475 // Get the slot for this thread. Try to register |
440 // it if none exists. | 476 // it if none exists. |
441 int slot = table->GetSlot(); | 477 int slot = table->GetSlot(); |
442 if (!slot && !(slot = table->RegisterThread(std::string()))) | 478 if (!slot && !(slot = table->RegisterThread(std::string()))) |
443 return NULL; | 479 return NULL; |
444 | 480 |
445 // Find the counter id for the counter. | 481 // Find the counter id for the counter. |
446 std::string str_name(name); | 482 std::string str_name(name); |
447 int counter = table->FindCounter(str_name); | 483 int counter = table->FindCounter(str_name); |
448 | 484 |
449 // Now we can find the location in the table. | 485 // Now we can find the location in the table. |
450 return table->GetLocation(counter, slot); | 486 return table->GetLocation(counter, slot); |
451 } | 487 } |
452 | 488 |
453 void StatsTable::UnregisterThread() { | 489 void StatsTable::UnregisterThread() { |
454 UnregisterThread(GetTLSData()); | 490 UnregisterThread(GetTLSData()); |
455 } | 491 } |
456 | 492 |
457 void StatsTable::UnregisterThread(TLSData* data) { | 493 void StatsTable::UnregisterThread(TLSData* data) { |
458 if (!data) | 494 if (!data) |
459 return; | 495 return; |
460 DCHECK(impl_); | 496 DCHECK(internal_); |
461 | 497 |
462 // Mark the slot free by zeroing out the thread name. | 498 // Mark the slot free by zeroing out the thread name. |
463 char* name = impl_->thread_name(data->slot); | 499 char* name = internal_->thread_name(data->slot); |
464 *name = '\0'; | 500 *name = '\0'; |
465 | 501 |
466 // Remove the calling thread's TLS so that it cannot use the slot. | 502 // Remove the calling thread's TLS so that it cannot use the slot. |
467 tls_index_.Set(NULL); | 503 tls_index_.Set(NULL); |
468 delete data; | 504 delete data; |
469 } | 505 } |
470 | 506 |
471 void StatsTable::SlotReturnFunction(void* data) { | 507 void StatsTable::SlotReturnFunction(void* data) { |
472 // This is called by the TLS destructor, which on some platforms has | 508 // This is called by the TLS destructor, which on some platforms has |
473 // already cleared the TLS info, so use the tls_data argument | 509 // already cleared the TLS info, so use the tls_data argument |
474 // rather than trying to fetch it ourselves. | 510 // rather than trying to fetch it ourselves. |
475 TLSData* tls_data = static_cast<TLSData*>(data); | 511 TLSData* tls_data = static_cast<TLSData*>(data); |
476 if (tls_data) { | 512 if (tls_data) { |
477 DCHECK(tls_data->table); | 513 DCHECK(tls_data->table); |
478 tls_data->table->UnregisterThread(tls_data); | 514 tls_data->table->UnregisterThread(tls_data); |
479 } | 515 } |
480 } | 516 } |
481 | 517 |
482 int StatsTable::FindEmptyThread() const { | 518 int StatsTable::FindEmptyThread() const { |
483 // Note: the API returns slots numbered from 1..N, although | 519 // Note: the API returns slots numbered from 1..N, although |
484 // internally, the array is 0..N-1. This is so that we can return | 520 // internally, the array is 0..N-1. This is so that we can return |
485 // zero as "not found". | 521 // zero as "not found". |
486 // | 522 // |
487 // The reason for doing this is because the thread 'slot' is stored | 523 // The reason for doing this is because the thread 'slot' is stored |
488 // in TLS, which is always initialized to zero, not -1. If 0 were | 524 // in TLS, which is always initialized to zero, not -1. If 0 were |
489 // returned as a valid slot number, it would be confused with the | 525 // returned as a valid slot number, it would be confused with the |
490 // uninitialized state. | 526 // uninitialized state. |
491 if (!impl_) | 527 if (!internal_) |
492 return 0; | 528 return 0; |
493 | 529 |
494 int index = 1; | 530 int index = 1; |
495 for (; index <= impl_->max_threads(); index++) { | 531 for (; index <= internal_->max_threads(); index++) { |
496 char* name = impl_->thread_name(index); | 532 char* name = internal_->thread_name(index); |
497 if (!*name) | 533 if (!*name) |
498 break; | 534 break; |
499 } | 535 } |
500 if (index > impl_->max_threads()) | 536 if (index > internal_->max_threads()) |
501 return 0; // The table is full. | 537 return 0; // The table is full. |
502 return index; | 538 return index; |
503 } | 539 } |
504 | 540 |
505 int StatsTable::FindCounterOrEmptyRow(const std::string& name) const { | 541 int StatsTable::FindCounterOrEmptyRow(const std::string& name) const { |
506 // Note: the API returns slots numbered from 1..N, although | 542 // Note: the API returns slots numbered from 1..N, although |
507 // internally, the array is 0..N-1. This is so that we can return | 543 // internally, the array is 0..N-1. This is so that we can return |
508 // zero as "not found". | 544 // zero as "not found". |
509 // | 545 // |
510 // There isn't much reason for this other than to be consistent | 546 // There isn't much reason for this other than to be consistent |
511 // with the way we track columns for thread slots. (See comments | 547 // with the way we track columns for thread slots. (See comments |
512 // in FindEmptyThread for why it is done this way). | 548 // in FindEmptyThread for why it is done this way). |
513 if (!impl_) | 549 if (!internal_) |
514 return 0; | 550 return 0; |
515 | 551 |
516 int free_slot = 0; | 552 int free_slot = 0; |
517 for (int index = 1; index <= impl_->max_counters(); index++) { | 553 for (int index = 1; index <= internal_->max_counters(); index++) { |
518 char* row_name = impl_->counter_name(index); | 554 char* row_name = internal_->counter_name(index); |
519 if (!*row_name && !free_slot) | 555 if (!*row_name && !free_slot) |
520 free_slot = index; // save that we found a free slot | 556 free_slot = index; // save that we found a free slot |
521 else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength)) | 557 else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength)) |
522 return index; | 558 return index; |
523 } | 559 } |
524 return free_slot; | 560 return free_slot; |
525 } | 561 } |
526 | 562 |
527 int StatsTable::AddCounter(const std::string& name) { | 563 int StatsTable::AddCounter(const std::string& name) { |
528 if (!impl_) | 564 if (!internal_) |
529 return 0; | 565 return 0; |
530 | 566 |
531 int counter_id = 0; | 567 int counter_id = 0; |
532 { | 568 { |
533 // To add a counter to the shared memory, we need the | 569 // To add a counter to the shared memory, we need the |
534 // shared memory lock. | 570 // shared memory lock. |
535 SharedMemoryAutoLock lock(impl_->shared_memory()); | 571 SharedMemoryAutoLock lock(internal_->shared_memory()); |
536 | 572 |
537 // We have space, so create a new counter. | 573 // We have space, so create a new counter. |
538 counter_id = FindCounterOrEmptyRow(name); | 574 counter_id = FindCounterOrEmptyRow(name); |
539 if (!counter_id) | 575 if (!counter_id) |
540 return 0; | 576 return 0; |
541 | 577 |
542 std::string counter_name = name; | 578 std::string counter_name = name; |
543 if (name.empty()) | 579 if (name.empty()) |
544 counter_name = kUnknownName; | 580 counter_name = kUnknownName; |
545 strlcpy(impl_->counter_name(counter_id), counter_name.c_str(), | 581 strlcpy(internal_->counter_name(counter_id), counter_name.c_str(), |
546 kMaxCounterNameLength); | 582 kMaxCounterNameLength); |
547 } | 583 } |
548 | 584 |
549 // now add to our in-memory cache | 585 // now add to our in-memory cache |
550 { | 586 { |
551 AutoLock lock(counters_lock_); | 587 AutoLock lock(counters_lock_); |
552 counters_[name] = counter_id; | 588 counters_[name] = counter_id; |
553 } | 589 } |
554 return counter_id; | 590 return counter_id; |
555 } | 591 } |
556 | 592 |
557 StatsTable::TLSData* StatsTable::GetTLSData() const { | 593 StatsTable::TLSData* StatsTable::GetTLSData() const { |
558 TLSData* data = | 594 TLSData* data = |
559 static_cast<TLSData*>(tls_index_.Get()); | 595 static_cast<TLSData*>(tls_index_.Get()); |
560 if (!data) | 596 if (!data) |
561 return NULL; | 597 return NULL; |
562 | 598 |
563 DCHECK(data->slot); | 599 DCHECK(data->slot); |
564 DCHECK_EQ(data->table, this); | 600 DCHECK_EQ(data->table, this); |
565 return data; | 601 return data; |
566 } | 602 } |
567 | 603 |
| 604 #if defined(OS_POSIX) |
| 605 SharedMemoryHandle StatsTable::GetSharedMemoryHandle() const { |
| 606 if (!internal_) |
| 607 return SharedMemory::NULLHandle(); |
| 608 return internal_->shared_memory()->handle(); |
| 609 } |
| 610 #endif |
| 611 |
568 } // namespace base | 612 } // namespace base |
OLD | NEW |