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