| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/stats_table.h" | 5 #include "base/stats_table.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // we all need to use the same size ints. | 63 // we all need to use the same size ints. |
| 64 COMPILE_ASSERT(sizeof(int)==4, expect_4_byte_ints); | 64 COMPILE_ASSERT(sizeof(int)==4, expect_4_byte_ints); |
| 65 | 65 |
| 66 namespace { | 66 namespace { |
| 67 | 67 |
| 68 // An internal version in case we ever change the format of this | 68 // An internal version in case we ever change the format of this |
| 69 // file, and so that we can identify our table. | 69 // file, and so that we can identify our table. |
| 70 const int kTableVersion = 0x13131313; | 70 const int kTableVersion = 0x13131313; |
| 71 | 71 |
| 72 // The name for un-named counters and threads in the table. | 72 // The name for un-named counters and threads in the table. |
| 73 const wchar_t kUnknownName[] = L"<unknown>"; | 73 const char kUnknownName[] = "<unknown>"; |
| 74 | 74 |
| 75 // Calculates delta to align an offset to the size of an int | 75 // Calculates delta to align an offset to the size of an int |
| 76 inline int AlignOffset(int offset) { | 76 inline int AlignOffset(int offset) { |
| 77 return (sizeof(int) - (offset % sizeof(int))) % sizeof(int); | 77 return (sizeof(int) - (offset % sizeof(int))) % sizeof(int); |
| 78 } | 78 } |
| 79 | 79 |
| 80 inline int AlignedSize(int size) { | 80 inline int AlignedSize(int size) { |
| 81 return size + AlignOffset(size); | 81 return size + AlignOffset(size); |
| 82 } | 82 } |
| 83 | 83 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 102 // Various header information contained in the memory mapped segment. | 102 // Various header information contained in the memory mapped segment. |
| 103 struct TableHeader { | 103 struct TableHeader { |
| 104 int version; | 104 int version; |
| 105 int size; | 105 int size; |
| 106 int max_counters; | 106 int max_counters; |
| 107 int max_threads; | 107 int max_threads; |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 // Construct a new StatsTablePrivate based on expected size parameters, or | 110 // Construct a new StatsTablePrivate based on expected size parameters, or |
| 111 // return NULL on failure. | 111 // return NULL on failure. |
| 112 static StatsTablePrivate* New(const std::wstring& name, int size, | 112 static StatsTablePrivate* New(const std::string& name, int size, |
| 113 int max_threads, int max_counters); | 113 int max_threads, int max_counters); |
| 114 | 114 |
| 115 base::SharedMemory* shared_memory() { return &shared_memory_; } | 115 base::SharedMemory* shared_memory() { return &shared_memory_; } |
| 116 | 116 |
| 117 // Accessors for our header pointers | 117 // Accessors for our header pointers |
| 118 TableHeader* table_header() const { return table_header_; } | 118 TableHeader* table_header() const { return table_header_; } |
| 119 int version() const { return table_header_->version; } | 119 int version() const { return table_header_->version; } |
| 120 int size() const { return table_header_->size; } | 120 int size() const { return table_header_->size; } |
| 121 int max_counters() const { return table_header_->max_counters; } | 121 int max_counters() const { return table_header_->max_counters; } |
| 122 int max_threads() const { return table_header_->max_threads; } | 122 int max_threads() const { return table_header_->max_threads; } |
| 123 | 123 |
| 124 // Accessors for our tables | 124 // Accessors for our tables |
| 125 wchar_t* thread_name(int slot_id) const { | 125 char* thread_name(int slot_id) const { |
| 126 return &thread_names_table_[ | 126 return &thread_names_table_[ |
| 127 (slot_id-1) * (StatsTable::kMaxThreadNameLength)]; | 127 (slot_id-1) * (StatsTable::kMaxThreadNameLength)]; |
| 128 } | 128 } |
| 129 int* thread_tid(int slot_id) const { | 129 int* thread_tid(int slot_id) const { |
| 130 return &(thread_tid_table_[slot_id-1]); | 130 return &(thread_tid_table_[slot_id-1]); |
| 131 } | 131 } |
| 132 int* thread_pid(int slot_id) const { | 132 int* thread_pid(int slot_id) const { |
| 133 return &(thread_pid_table_[slot_id-1]); | 133 return &(thread_pid_table_[slot_id-1]); |
| 134 } | 134 } |
| 135 wchar_t* counter_name(int counter_id) const { | 135 char* counter_name(int counter_id) const { |
| 136 return &counter_names_table_[ | 136 return &counter_names_table_[ |
| 137 (counter_id-1) * (StatsTable::kMaxCounterNameLength)]; | 137 (counter_id-1) * (StatsTable::kMaxCounterNameLength)]; |
| 138 } | 138 } |
| 139 int* row(int counter_id) const { | 139 int* row(int counter_id) const { |
| 140 return &data_table_[(counter_id-1) * max_threads()]; | 140 return &data_table_[(counter_id-1) * max_threads()]; |
| 141 } | 141 } |
| 142 | 142 |
| 143 private: | 143 private: |
| 144 // Constructor is private because you should use New() instead. | 144 // Constructor is private because you should use New() instead. |
| 145 StatsTablePrivate() {} | 145 StatsTablePrivate() {} |
| 146 | 146 |
| 147 // Initializes the table on first access. Sets header values | 147 // Initializes the table on first access. Sets header values |
| 148 // appropriately and zeroes all counters. | 148 // appropriately and zeroes all counters. |
| 149 void InitializeTable(void* memory, int size, int max_counters, | 149 void InitializeTable(void* memory, int size, int max_counters, |
| 150 int max_threads); | 150 int max_threads); |
| 151 | 151 |
| 152 // Initializes our in-memory pointers into a pre-created StatsTable. | 152 // Initializes our in-memory pointers into a pre-created StatsTable. |
| 153 void ComputeMappedPointers(void* memory); | 153 void ComputeMappedPointers(void* memory); |
| 154 | 154 |
| 155 base::SharedMemory shared_memory_; | 155 base::SharedMemory shared_memory_; |
| 156 TableHeader* table_header_; | 156 TableHeader* table_header_; |
| 157 wchar_t* thread_names_table_; | 157 char* thread_names_table_; |
| 158 int* thread_tid_table_; | 158 int* thread_tid_table_; |
| 159 int* thread_pid_table_; | 159 int* thread_pid_table_; |
| 160 wchar_t* counter_names_table_; | 160 char* counter_names_table_; |
| 161 int* data_table_; | 161 int* data_table_; |
| 162 }; | 162 }; |
| 163 | 163 |
| 164 // static | 164 // static |
| 165 StatsTablePrivate* StatsTablePrivate::New(const std::wstring& name, | 165 StatsTablePrivate* StatsTablePrivate::New(const std::string& name, |
| 166 int size, | 166 int size, |
| 167 int max_threads, | 167 int max_threads, |
| 168 int max_counters) { | 168 int max_counters) { |
| 169 scoped_ptr<StatsTablePrivate> priv(new StatsTablePrivate()); | 169 scoped_ptr<StatsTablePrivate> priv(new StatsTablePrivate()); |
| 170 | 170 |
| 171 if (!priv->shared_memory_.Create(name, false, true, size)) | 171 if (!priv->shared_memory_.Create(UTF8ToWide(name), false, true, size)) |
| 172 return NULL; | 172 return NULL; |
| 173 if (!priv->shared_memory_.Map(size)) | 173 if (!priv->shared_memory_.Map(size)) |
| 174 return NULL; | 174 return NULL; |
| 175 void* memory = priv->shared_memory_.memory(); | 175 void* memory = priv->shared_memory_.memory(); |
| 176 | 176 |
| 177 TableHeader* header = static_cast<TableHeader*>(memory); | 177 TableHeader* header = static_cast<TableHeader*>(memory); |
| 178 | 178 |
| 179 // If the version does not match, then assume the table needs | 179 // If the version does not match, then assume the table needs |
| 180 // to be initialized. | 180 // to be initialized. |
| 181 if (header->version != kTableVersion) | 181 if (header->version != kTableVersion) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 205 char* data = static_cast<char*>(memory); | 205 char* data = static_cast<char*>(memory); |
| 206 int offset = 0; | 206 int offset = 0; |
| 207 | 207 |
| 208 table_header_ = reinterpret_cast<TableHeader*>(data); | 208 table_header_ = reinterpret_cast<TableHeader*>(data); |
| 209 offset += sizeof(*table_header_); | 209 offset += sizeof(*table_header_); |
| 210 offset += AlignOffset(offset); | 210 offset += AlignOffset(offset); |
| 211 | 211 |
| 212 // Verify we're looking at a valid StatsTable. | 212 // Verify we're looking at a valid StatsTable. |
| 213 DCHECK_EQ(table_header_->version, kTableVersion); | 213 DCHECK_EQ(table_header_->version, kTableVersion); |
| 214 | 214 |
| 215 thread_names_table_ = reinterpret_cast<wchar_t*>(data + offset); | 215 thread_names_table_ = reinterpret_cast<char*>(data + offset); |
| 216 offset += sizeof(wchar_t) * | 216 offset += sizeof(char) * |
| 217 max_threads() * StatsTable::kMaxThreadNameLength; | 217 max_threads() * StatsTable::kMaxThreadNameLength; |
| 218 offset += AlignOffset(offset); | 218 offset += AlignOffset(offset); |
| 219 | 219 |
| 220 thread_tid_table_ = reinterpret_cast<int*>(data + offset); | 220 thread_tid_table_ = reinterpret_cast<int*>(data + offset); |
| 221 offset += sizeof(int) * max_threads(); | 221 offset += sizeof(int) * max_threads(); |
| 222 offset += AlignOffset(offset); | 222 offset += AlignOffset(offset); |
| 223 | 223 |
| 224 thread_pid_table_ = reinterpret_cast<int*>(data + offset); | 224 thread_pid_table_ = reinterpret_cast<int*>(data + offset); |
| 225 offset += sizeof(int) * max_threads(); | 225 offset += sizeof(int) * max_threads(); |
| 226 offset += AlignOffset(offset); | 226 offset += AlignOffset(offset); |
| 227 | 227 |
| 228 counter_names_table_ = reinterpret_cast<wchar_t*>(data + offset); | 228 counter_names_table_ = reinterpret_cast<char*>(data + offset); |
| 229 offset += sizeof(wchar_t) * | 229 offset += sizeof(char) * |
| 230 max_counters() * StatsTable::kMaxCounterNameLength; | 230 max_counters() * StatsTable::kMaxCounterNameLength; |
| 231 offset += AlignOffset(offset); | 231 offset += AlignOffset(offset); |
| 232 | 232 |
| 233 data_table_ = reinterpret_cast<int*>(data + offset); | 233 data_table_ = reinterpret_cast<int*>(data + offset); |
| 234 offset += sizeof(int) * max_threads() * max_counters(); | 234 offset += sizeof(int) * max_threads() * max_counters(); |
| 235 | 235 |
| 236 DCHECK_EQ(offset, size()); | 236 DCHECK_EQ(offset, size()); |
| 237 } | 237 } |
| 238 | 238 |
| 239 | 239 |
| 240 | 240 |
| 241 // We keep a singleton table which can be easily accessed. | 241 // We keep a singleton table which can be easily accessed. |
| 242 StatsTable* StatsTable::global_table_ = NULL; | 242 StatsTable* StatsTable::global_table_ = NULL; |
| 243 | 243 |
| 244 StatsTable::StatsTable(const std::wstring& name, int max_threads, | 244 StatsTable::StatsTable(const std::string& name, int max_threads, |
| 245 int max_counters) | 245 int max_counters) |
| 246 : impl_(NULL), | 246 : impl_(NULL), |
| 247 tls_index_(SlotReturnFunction) { | 247 tls_index_(SlotReturnFunction) { |
| 248 int table_size = | 248 int table_size = |
| 249 AlignedSize(sizeof(StatsTablePrivate::TableHeader)) + | 249 AlignedSize(sizeof(StatsTablePrivate::TableHeader)) + |
| 250 AlignedSize((max_counters * sizeof(wchar_t) * kMaxCounterNameLength)) + | 250 AlignedSize((max_counters * sizeof(char) * kMaxCounterNameLength)) + |
| 251 AlignedSize((max_threads * sizeof(wchar_t) * kMaxThreadNameLength)) + | 251 AlignedSize((max_threads * sizeof(char) * kMaxThreadNameLength)) + |
| 252 AlignedSize(max_threads * sizeof(int)) + | 252 AlignedSize(max_threads * sizeof(int)) + |
| 253 AlignedSize(max_threads * sizeof(int)) + | 253 AlignedSize(max_threads * sizeof(int)) + |
| 254 AlignedSize((sizeof(int) * (max_counters * max_threads))); | 254 AlignedSize((sizeof(int) * (max_counters * max_threads))); |
| 255 | 255 |
| 256 impl_ = StatsTablePrivate::New(name, table_size, max_threads, max_counters); | 256 impl_ = StatsTablePrivate::New(name, table_size, max_threads, max_counters); |
| 257 | 257 |
| 258 // TODO(port): clean up this error reporting. | 258 // TODO(port): clean up this error reporting. |
| 259 #if defined(OS_WIN) | 259 #if defined(OS_WIN) |
| 260 if (!impl_) | 260 if (!impl_) |
| 261 LOG(ERROR) << "StatsTable did not initialize:" << GetLastError(); | 261 LOG(ERROR) << "StatsTable did not initialize:" << GetLastError(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 275 tls_index_.Free(); | 275 tls_index_.Free(); |
| 276 | 276 |
| 277 // Cleanup our shared memory. | 277 // Cleanup our shared memory. |
| 278 delete impl_; | 278 delete impl_; |
| 279 | 279 |
| 280 // If we are the global table, unregister ourselves. | 280 // If we are the global table, unregister ourselves. |
| 281 if (global_table_ == this) | 281 if (global_table_ == this) |
| 282 global_table_ = NULL; | 282 global_table_ = NULL; |
| 283 } | 283 } |
| 284 | 284 |
| 285 int StatsTable::RegisterThread(const std::wstring& name) { | 285 int StatsTable::RegisterThread(const std::string& name) { |
| 286 int slot = 0; | 286 int slot = 0; |
| 287 | 287 |
| 288 // Registering a thread requires that we lock the shared memory | 288 // Registering a thread requires that we lock the shared memory |
| 289 // so that two threads don't grab the same slot. Fortunately, | 289 // so that two threads don't grab the same slot. Fortunately, |
| 290 // thread creation shouldn't happen in inner loops. | 290 // thread creation shouldn't happen in inner loops. |
| 291 { | 291 { |
| 292 base::SharedMemoryAutoLock lock(impl_->shared_memory()); | 292 base::SharedMemoryAutoLock lock(impl_->shared_memory()); |
| 293 slot = FindEmptyThread(); | 293 slot = FindEmptyThread(); |
| 294 if (!slot) { | 294 if (!slot) { |
| 295 return 0; | 295 return 0; |
| 296 } | 296 } |
| 297 | 297 |
| 298 DCHECK(impl_); | 298 DCHECK(impl_); |
| 299 | 299 |
| 300 // We have space, so consume a column in the table. | 300 // We have space, so consume a column in the table. |
| 301 std::wstring thread_name = name; | 301 std::string thread_name = name; |
| 302 if (name.empty()) | 302 if (name.empty()) |
| 303 thread_name = kUnknownName; | 303 thread_name = kUnknownName; |
| 304 base::wcslcpy(impl_->thread_name(slot), thread_name.c_str(), | 304 base::strlcpy(impl_->thread_name(slot), thread_name.c_str(), |
| 305 kMaxThreadNameLength); | 305 kMaxThreadNameLength); |
| 306 *(impl_->thread_tid(slot)) = PlatformThread::CurrentId(); | 306 *(impl_->thread_tid(slot)) = PlatformThread::CurrentId(); |
| 307 *(impl_->thread_pid(slot)) = base::GetCurrentProcId(); | 307 *(impl_->thread_pid(slot)) = base::GetCurrentProcId(); |
| 308 } | 308 } |
| 309 | 309 |
| 310 // Set our thread local storage. | 310 // Set our thread local storage. |
| 311 StatsTableTLSData* data = new StatsTableTLSData; | 311 StatsTableTLSData* data = new StatsTableTLSData; |
| 312 data->table = this; | 312 data->table = this; |
| 313 data->slot = slot; | 313 data->slot = slot; |
| 314 tls_index_.Set(data); | 314 tls_index_.Set(data); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 329 void StatsTable::UnregisterThread() { | 329 void StatsTable::UnregisterThread() { |
| 330 UnregisterThread(GetTLSData()); | 330 UnregisterThread(GetTLSData()); |
| 331 } | 331 } |
| 332 | 332 |
| 333 void StatsTable::UnregisterThread(StatsTableTLSData* data) { | 333 void StatsTable::UnregisterThread(StatsTableTLSData* data) { |
| 334 if (!data) | 334 if (!data) |
| 335 return; | 335 return; |
| 336 DCHECK(impl_); | 336 DCHECK(impl_); |
| 337 | 337 |
| 338 // Mark the slot free by zeroing out the thread name. | 338 // Mark the slot free by zeroing out the thread name. |
| 339 wchar_t* name = impl_->thread_name(data->slot); | 339 char* name = impl_->thread_name(data->slot); |
| 340 *name = L'\0'; | 340 *name = '\0'; |
| 341 | 341 |
| 342 // Remove the calling thread's TLS so that it cannot use the slot. | 342 // Remove the calling thread's TLS so that it cannot use the slot. |
| 343 tls_index_.Set(NULL); | 343 tls_index_.Set(NULL); |
| 344 delete data; | 344 delete data; |
| 345 } | 345 } |
| 346 | 346 |
| 347 void StatsTable::SlotReturnFunction(void* data) { | 347 void StatsTable::SlotReturnFunction(void* data) { |
| 348 // This is called by the TLS destructor, which on some platforms has | 348 // This is called by the TLS destructor, which on some platforms has |
| 349 // already cleared the TLS info, so use the tls_data argument | 349 // already cleared the TLS info, so use the tls_data argument |
| 350 // rather than trying to fetch it ourselves. | 350 // rather than trying to fetch it ourselves. |
| 351 StatsTableTLSData* tls_data = static_cast<StatsTableTLSData*>(data); | 351 StatsTableTLSData* tls_data = static_cast<StatsTableTLSData*>(data); |
| 352 if (tls_data) { | 352 if (tls_data) { |
| 353 DCHECK(tls_data->table); | 353 DCHECK(tls_data->table); |
| 354 tls_data->table->UnregisterThread(tls_data); | 354 tls_data->table->UnregisterThread(tls_data); |
| 355 } | 355 } |
| 356 } | 356 } |
| 357 | 357 |
| 358 int StatsTable::CountThreadsRegistered() const { | 358 int StatsTable::CountThreadsRegistered() const { |
| 359 if (!impl_) | 359 if (!impl_) |
| 360 return 0; | 360 return 0; |
| 361 | 361 |
| 362 // Loop through the shared memory and count the threads that are active. | 362 // Loop through the shared memory and count the threads that are active. |
| 363 // We intentionally do not lock the table during the operation. | 363 // We intentionally do not lock the table during the operation. |
| 364 int count = 0; | 364 int count = 0; |
| 365 for (int index = 1; index <= impl_->max_threads(); index++) { | 365 for (int index = 1; index <= impl_->max_threads(); index++) { |
| 366 wchar_t* name = impl_->thread_name(index); | 366 char* name = impl_->thread_name(index); |
| 367 if (*name != L'\0') | 367 if (*name != '\0') |
| 368 count++; | 368 count++; |
| 369 } | 369 } |
| 370 return count; | 370 return count; |
| 371 } | 371 } |
| 372 | 372 |
| 373 int StatsTable::GetSlot() const { | 373 int StatsTable::GetSlot() const { |
| 374 StatsTableTLSData* data = GetTLSData(); | 374 StatsTableTLSData* data = GetTLSData(); |
| 375 if (!data) | 375 if (!data) |
| 376 return 0; | 376 return 0; |
| 377 return data->slot; | 377 return data->slot; |
| 378 } | 378 } |
| 379 | 379 |
| 380 int StatsTable::FindEmptyThread() const { | 380 int StatsTable::FindEmptyThread() const { |
| 381 // Note: the API returns slots numbered from 1..N, although | 381 // Note: the API returns slots numbered from 1..N, although |
| 382 // internally, the array is 0..N-1. This is so that we can return | 382 // internally, the array is 0..N-1. This is so that we can return |
| 383 // zero as "not found". | 383 // zero as "not found". |
| 384 // | 384 // |
| 385 // The reason for doing this is because the thread 'slot' is stored | 385 // The reason for doing this is because the thread 'slot' is stored |
| 386 // in TLS, which is always initialized to zero, not -1. If 0 were | 386 // in TLS, which is always initialized to zero, not -1. If 0 were |
| 387 // returned as a valid slot number, it would be confused with the | 387 // returned as a valid slot number, it would be confused with the |
| 388 // uninitialized state. | 388 // uninitialized state. |
| 389 if (!impl_) | 389 if (!impl_) |
| 390 return 0; | 390 return 0; |
| 391 | 391 |
| 392 int index = 1; | 392 int index = 1; |
| 393 for (; index <= impl_->max_threads(); index++) { | 393 for (; index <= impl_->max_threads(); index++) { |
| 394 wchar_t* name = impl_->thread_name(index); | 394 char* name = impl_->thread_name(index); |
| 395 if (!*name) | 395 if (!*name) |
| 396 break; | 396 break; |
| 397 } | 397 } |
| 398 if (index > impl_->max_threads()) | 398 if (index > impl_->max_threads()) |
| 399 return 0; // The table is full. | 399 return 0; // The table is full. |
| 400 return index; | 400 return index; |
| 401 } | 401 } |
| 402 | 402 |
| 403 int StatsTable::FindCounterOrEmptyRow(const std::wstring& name) const { | 403 int StatsTable::FindCounterOrEmptyRow(const std::string& name) const { |
| 404 // Note: the API returns slots numbered from 1..N, although | 404 // Note: the API returns slots numbered from 1..N, although |
| 405 // internally, the array is 0..N-1. This is so that we can return | 405 // internally, the array is 0..N-1. This is so that we can return |
| 406 // zero as "not found". | 406 // zero as "not found". |
| 407 // | 407 // |
| 408 // There isn't much reason for this other than to be consistent | 408 // There isn't much reason for this other than to be consistent |
| 409 // with the way we track columns for thread slots. (See comments | 409 // with the way we track columns for thread slots. (See comments |
| 410 // in FindEmptyThread for why it is done this way). | 410 // in FindEmptyThread for why it is done this way). |
| 411 if (!impl_) | 411 if (!impl_) |
| 412 return 0; | 412 return 0; |
| 413 | 413 |
| 414 int free_slot = 0; | 414 int free_slot = 0; |
| 415 for (int index = 1; index <= impl_->max_counters(); index++) { | 415 for (int index = 1; index <= impl_->max_counters(); index++) { |
| 416 wchar_t* row_name = impl_->counter_name(index); | 416 char* row_name = impl_->counter_name(index); |
| 417 if (!*row_name && !free_slot) | 417 if (!*row_name && !free_slot) |
| 418 free_slot = index; // save that we found a free slot | 418 free_slot = index; // save that we found a free slot |
| 419 else if (!wcsncmp(row_name, name.c_str(), kMaxCounterNameLength)) | 419 else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength)) |
| 420 return index; | 420 return index; |
| 421 } | 421 } |
| 422 return free_slot; | 422 return free_slot; |
| 423 } | 423 } |
| 424 | 424 |
| 425 int StatsTable::FindCounter(const std::wstring& name) { | 425 int StatsTable::FindCounter(const std::string& name) { |
| 426 // Note: the API returns counters numbered from 1..N, although | 426 // Note: the API returns counters numbered from 1..N, although |
| 427 // internally, the array is 0..N-1. This is so that we can return | 427 // internally, the array is 0..N-1. This is so that we can return |
| 428 // zero as "not found". | 428 // zero as "not found". |
| 429 if (!impl_) | 429 if (!impl_) |
| 430 return 0; | 430 return 0; |
| 431 | 431 |
| 432 // Create a scope for our auto-lock. | 432 // Create a scope for our auto-lock. |
| 433 { | 433 { |
| 434 AutoLock scoped_lock(counters_lock_); | 434 AutoLock scoped_lock(counters_lock_); |
| 435 | 435 |
| 436 // Attempt to find the counter. | 436 // Attempt to find the counter. |
| 437 CountersMap::const_iterator iter; | 437 CountersMap::const_iterator iter; |
| 438 iter = counters_.find(name); | 438 iter = counters_.find(name); |
| 439 if (iter != counters_.end()) | 439 if (iter != counters_.end()) |
| 440 return iter->second; | 440 return iter->second; |
| 441 } | 441 } |
| 442 | 442 |
| 443 // Counter does not exist, so add it. | 443 // Counter does not exist, so add it. |
| 444 return AddCounter(name); | 444 return AddCounter(name); |
| 445 } | 445 } |
| 446 | 446 |
| 447 int StatsTable::AddCounter(const std::wstring& name) { | 447 int StatsTable::AddCounter(const std::string& name) { |
| 448 DCHECK(impl_); | 448 DCHECK(impl_); |
| 449 | 449 |
| 450 if (!impl_) | 450 if (!impl_) |
| 451 return 0; | 451 return 0; |
| 452 | 452 |
| 453 int counter_id = 0; | 453 int counter_id = 0; |
| 454 { | 454 { |
| 455 // To add a counter to the shared memory, we need the | 455 // To add a counter to the shared memory, we need the |
| 456 // shared memory lock. | 456 // shared memory lock. |
| 457 base::SharedMemoryAutoLock lock(impl_->shared_memory()); | 457 base::SharedMemoryAutoLock lock(impl_->shared_memory()); |
| 458 | 458 |
| 459 // We have space, so create a new counter. | 459 // We have space, so create a new counter. |
| 460 counter_id = FindCounterOrEmptyRow(name); | 460 counter_id = FindCounterOrEmptyRow(name); |
| 461 if (!counter_id) | 461 if (!counter_id) |
| 462 return 0; | 462 return 0; |
| 463 | 463 |
| 464 std::wstring counter_name = name; | 464 std::string counter_name = name; |
| 465 if (name.empty()) | 465 if (name.empty()) |
| 466 counter_name = kUnknownName; | 466 counter_name = kUnknownName; |
| 467 base::wcslcpy(impl_->counter_name(counter_id), counter_name.c_str(), | 467 base::strlcpy(impl_->counter_name(counter_id), counter_name.c_str(), |
| 468 kMaxCounterNameLength); | 468 kMaxCounterNameLength); |
| 469 } | 469 } |
| 470 | 470 |
| 471 // now add to our in-memory cache | 471 // now add to our in-memory cache |
| 472 { | 472 { |
| 473 AutoLock lock(counters_lock_); | 473 AutoLock lock(counters_lock_); |
| 474 counters_[name] = counter_id; | 474 counters_[name] = counter_id; |
| 475 } | 475 } |
| 476 return counter_id; | 476 return counter_id; |
| 477 } | 477 } |
| 478 | 478 |
| 479 int* StatsTable::GetLocation(int counter_id, int slot_id) const { | 479 int* StatsTable::GetLocation(int counter_id, int slot_id) const { |
| 480 if (!impl_) | 480 if (!impl_) |
| 481 return NULL; | 481 return NULL; |
| 482 if (slot_id > impl_->max_threads()) | 482 if (slot_id > impl_->max_threads()) |
| 483 return NULL; | 483 return NULL; |
| 484 | 484 |
| 485 int* row = impl_->row(counter_id); | 485 int* row = impl_->row(counter_id); |
| 486 return &(row[slot_id-1]); | 486 return &(row[slot_id-1]); |
| 487 } | 487 } |
| 488 | 488 |
| 489 const wchar_t* StatsTable::GetRowName(int index) const { | 489 const char* StatsTable::GetRowName(int index) const { |
| 490 if (!impl_) | 490 if (!impl_) |
| 491 return NULL; | 491 return NULL; |
| 492 | 492 |
| 493 return impl_->counter_name(index); | 493 return impl_->counter_name(index); |
| 494 } | 494 } |
| 495 | 495 |
| 496 int StatsTable::GetRowValue(int index, int pid) const { | 496 int StatsTable::GetRowValue(int index, int pid) const { |
| 497 if (!impl_) | 497 if (!impl_) |
| 498 return 0; | 498 return 0; |
| 499 | 499 |
| 500 int rv = 0; | 500 int rv = 0; |
| 501 int* row = impl_->row(index); | 501 int* row = impl_->row(index); |
| 502 for (int index = 0; index < impl_->max_threads(); index++) { | 502 for (int index = 0; index < impl_->max_threads(); index++) { |
| 503 if (pid == 0 || *impl_->thread_pid(index) == pid) | 503 if (pid == 0 || *impl_->thread_pid(index) == pid) |
| 504 rv += row[index]; | 504 rv += row[index]; |
| 505 } | 505 } |
| 506 return rv; | 506 return rv; |
| 507 } | 507 } |
| 508 | 508 |
| 509 int StatsTable::GetRowValue(int index) const { | 509 int StatsTable::GetRowValue(int index) const { |
| 510 return GetRowValue(index, 0); | 510 return GetRowValue(index, 0); |
| 511 } | 511 } |
| 512 | 512 |
| 513 int StatsTable::GetCounterValue(const std::wstring& name, int pid) { | 513 int StatsTable::GetCounterValue(const std::string& name, int pid) { |
| 514 if (!impl_) | 514 if (!impl_) |
| 515 return 0; | 515 return 0; |
| 516 | 516 |
| 517 int row = FindCounter(name); | 517 int row = FindCounter(name); |
| 518 if (!row) | 518 if (!row) |
| 519 return 0; | 519 return 0; |
| 520 return GetRowValue(row, pid); | 520 return GetRowValue(row, pid); |
| 521 } | 521 } |
| 522 | 522 |
| 523 int StatsTable::GetCounterValue(const std::wstring& name) { | 523 int StatsTable::GetCounterValue(const std::string& name) { |
| 524 return GetCounterValue(name, 0); | 524 return GetCounterValue(name, 0); |
| 525 } | 525 } |
| 526 | 526 |
| 527 int StatsTable::GetMaxCounters() const { | 527 int StatsTable::GetMaxCounters() const { |
| 528 if (!impl_) | 528 if (!impl_) |
| 529 return 0; | 529 return 0; |
| 530 return impl_->max_counters(); | 530 return impl_->max_counters(); |
| 531 } | 531 } |
| 532 | 532 |
| 533 int StatsTable::GetMaxThreads() const { | 533 int StatsTable::GetMaxThreads() const { |
| 534 if (!impl_) | 534 if (!impl_) |
| 535 return 0; | 535 return 0; |
| 536 return impl_->max_threads(); | 536 return impl_->max_threads(); |
| 537 } | 537 } |
| 538 | 538 |
| 539 int* StatsTable::FindLocation(const wchar_t* name) { | 539 int* StatsTable::FindLocation(const char* name) { |
| 540 // Get the static StatsTable | 540 // Get the static StatsTable |
| 541 StatsTable *table = StatsTable::current(); | 541 StatsTable *table = StatsTable::current(); |
| 542 if (!table) | 542 if (!table) |
| 543 return NULL; | 543 return NULL; |
| 544 | 544 |
| 545 // Get the slot for this thread. Try to register | 545 // Get the slot for this thread. Try to register |
| 546 // it if none exists. | 546 // it if none exists. |
| 547 int slot = table->GetSlot(); | 547 int slot = table->GetSlot(); |
| 548 if (!slot && !(slot = table->RegisterThread(L""))) | 548 if (!slot && !(slot = table->RegisterThread(""))) |
| 549 return NULL; | 549 return NULL; |
| 550 | 550 |
| 551 // Find the counter id for the counter. | 551 // Find the counter id for the counter. |
| 552 std::wstring str_name(name); | 552 std::string str_name(name); |
| 553 int counter = table->FindCounter(str_name); | 553 int counter = table->FindCounter(str_name); |
| 554 | 554 |
| 555 // Now we can find the location in the table. | 555 // Now we can find the location in the table. |
| 556 return table->GetLocation(counter, slot); | 556 return table->GetLocation(counter, slot); |
| 557 } | 557 } |
| 558 | 558 |
| OLD | NEW |