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/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 // Cleanup our shared memory. | 280 // Cleanup our shared memory. |
281 delete impl_; | 281 delete impl_; |
282 | 282 |
283 // If we are the global table, unregister ourselves. | 283 // If we are the global table, unregister ourselves. |
284 if (global_table_ == this) | 284 if (global_table_ == this) |
285 global_table_ = NULL; | 285 global_table_ = NULL; |
286 } | 286 } |
287 | 287 |
288 int StatsTable::RegisterThread(const std::string& name) { | 288 int StatsTable::RegisterThread(const std::string& name) { |
289 int slot = 0; | 289 int slot = 0; |
| 290 if (!impl_) |
| 291 return 0; |
290 | 292 |
291 // Registering a thread requires that we lock the shared memory | 293 // Registering a thread requires that we lock the shared memory |
292 // so that two threads don't grab the same slot. Fortunately, | 294 // so that two threads don't grab the same slot. Fortunately, |
293 // thread creation shouldn't happen in inner loops. | 295 // thread creation shouldn't happen in inner loops. |
294 { | 296 { |
295 base::SharedMemoryAutoLock lock(impl_->shared_memory()); | 297 base::SharedMemoryAutoLock lock(impl_->shared_memory()); |
296 slot = FindEmptyThread(); | 298 slot = FindEmptyThread(); |
297 if (!slot) { | 299 if (!slot) { |
298 return 0; | 300 return 0; |
299 } | 301 } |
300 | 302 |
301 DCHECK(impl_); | |
302 | |
303 // We have space, so consume a column in the table. | 303 // We have space, so consume a column in the table. |
304 std::string thread_name = name; | 304 std::string thread_name = name; |
305 if (name.empty()) | 305 if (name.empty()) |
306 thread_name = kUnknownName; | 306 thread_name = kUnknownName; |
307 base::strlcpy(impl_->thread_name(slot), thread_name.c_str(), | 307 base::strlcpy(impl_->thread_name(slot), thread_name.c_str(), |
308 kMaxThreadNameLength); | 308 kMaxThreadNameLength); |
309 *(impl_->thread_tid(slot)) = PlatformThread::CurrentId(); | 309 *(impl_->thread_tid(slot)) = PlatformThread::CurrentId(); |
310 *(impl_->thread_pid(slot)) = base::GetCurrentProcId(); | 310 *(impl_->thread_pid(slot)) = base::GetCurrentProcId(); |
311 } | 311 } |
312 | 312 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 iter = counters_.find(name); | 441 iter = counters_.find(name); |
442 if (iter != counters_.end()) | 442 if (iter != counters_.end()) |
443 return iter->second; | 443 return iter->second; |
444 } | 444 } |
445 | 445 |
446 // Counter does not exist, so add it. | 446 // Counter does not exist, so add it. |
447 return AddCounter(name); | 447 return AddCounter(name); |
448 } | 448 } |
449 | 449 |
450 int StatsTable::AddCounter(const std::string& name) { | 450 int StatsTable::AddCounter(const std::string& name) { |
451 DCHECK(impl_); | |
452 | |
453 if (!impl_) | 451 if (!impl_) |
454 return 0; | 452 return 0; |
455 | 453 |
456 int counter_id = 0; | 454 int counter_id = 0; |
457 { | 455 { |
458 // To add a counter to the shared memory, we need the | 456 // To add a counter to the shared memory, we need the |
459 // shared memory lock. | 457 // shared memory lock. |
460 base::SharedMemoryAutoLock lock(impl_->shared_memory()); | 458 base::SharedMemoryAutoLock lock(impl_->shared_memory()); |
461 | 459 |
462 // We have space, so create a new counter. | 460 // We have space, so create a new counter. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 if (!slot && !(slot = table->RegisterThread(""))) | 549 if (!slot && !(slot = table->RegisterThread(""))) |
552 return NULL; | 550 return NULL; |
553 | 551 |
554 // Find the counter id for the counter. | 552 // Find the counter id for the counter. |
555 std::string str_name(name); | 553 std::string str_name(name); |
556 int counter = table->FindCounter(str_name); | 554 int counter = table->FindCounter(str_name); |
557 | 555 |
558 // Now we can find the location in the table. | 556 // Now we can find the location in the table. |
559 return table->GetLocation(counter, slot); | 557 return table->GetLocation(counter, slot); |
560 } | 558 } |
OLD | NEW |