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

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

Issue 6085015: Order function definitions in base/ according to the header. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: lrn2merge Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/metrics/field_trial.cc ('k') | base/ref_counted.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/process_util.h" 8 #include "base/process_util.h"
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/shared_memory.h" 10 #include "base/shared_memory.h"
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 tls_index_.Free(); 271 tls_index_.Free();
272 272
273 // Cleanup our shared memory. 273 // Cleanup our shared memory.
274 delete impl_; 274 delete impl_;
275 275
276 // If we are the global table, unregister ourselves. 276 // If we are the global table, unregister ourselves.
277 if (global_table_ == this) 277 if (global_table_ == this)
278 global_table_ = NULL; 278 global_table_ = NULL;
279 } 279 }
280 280
281 int StatsTable::GetSlot() const {
282 TLSData* data = GetTLSData();
283 if (!data)
284 return 0;
285 return data->slot;
286 }
287
281 int StatsTable::RegisterThread(const std::string& name) { 288 int StatsTable::RegisterThread(const std::string& name) {
282 int slot = 0; 289 int slot = 0;
283 if (!impl_) 290 if (!impl_)
284 return 0; 291 return 0;
285 292
286 // Registering a thread requires that we lock the shared memory 293 // Registering a thread requires that we lock the shared memory
287 // so that two threads don't grab the same slot. Fortunately, 294 // so that two threads don't grab the same slot. Fortunately,
288 // thread creation shouldn't happen in inner loops. 295 // thread creation shouldn't happen in inner loops.
289 { 296 {
290 SharedMemoryAutoLock lock(impl_->shared_memory()); 297 SharedMemoryAutoLock lock(impl_->shared_memory());
(...skipping 13 matching lines...) Expand all
304 } 311 }
305 312
306 // Set our thread local storage. 313 // Set our thread local storage.
307 TLSData* data = new TLSData; 314 TLSData* data = new TLSData;
308 data->table = this; 315 data->table = this;
309 data->slot = slot; 316 data->slot = slot;
310 tls_index_.Set(data); 317 tls_index_.Set(data);
311 return slot; 318 return slot;
312 } 319 }
313 320
314 StatsTable::TLSData* StatsTable::GetTLSData() const { 321 int StatsTable::CountThreadsRegistered() const {
315 TLSData* data = 322 if (!impl_)
316 static_cast<TLSData*>(tls_index_.Get()); 323 return 0;
317 if (!data) 324
325 // Loop through the shared memory and count the threads that are active.
326 // We intentionally do not lock the table during the operation.
327 int count = 0;
328 for (int index = 1; index <= impl_->max_threads(); index++) {
329 char* name = impl_->thread_name(index);
330 if (*name != '\0')
331 count++;
332 }
333 return count;
334 }
335
336 int StatsTable::FindCounter(const std::string& name) {
337 // Note: the API returns counters numbered from 1..N, although
338 // internally, the array is 0..N-1. This is so that we can return
339 // zero as "not found".
340 if (!impl_)
341 return 0;
342
343 // Create a scope for our auto-lock.
344 {
345 AutoLock scoped_lock(counters_lock_);
346
347 // Attempt to find the counter.
348 CountersMap::const_iterator iter;
349 iter = counters_.find(name);
350 if (iter != counters_.end())
351 return iter->second;
352 }
353
354 // Counter does not exist, so add it.
355 return AddCounter(name);
356 }
357
358 int* StatsTable::GetLocation(int counter_id, int slot_id) const {
359 if (!impl_)
360 return NULL;
361 if (slot_id > impl_->max_threads())
318 return NULL; 362 return NULL;
319 363
320 DCHECK(data->slot); 364 int* row = impl_->row(counter_id);
321 DCHECK_EQ(data->table, this); 365 return &(row[slot_id-1]);
322 return data; 366 }
367
368 const char* StatsTable::GetRowName(int index) const {
369 if (!impl_)
370 return NULL;
371
372 return impl_->counter_name(index);
373 }
374
375 int StatsTable::GetRowValue(int index) const {
376 return GetRowValue(index, 0);
377 }
378
379 int StatsTable::GetRowValue(int index, int pid) const {
380 if (!impl_)
381 return 0;
382
383 int rv = 0;
384 int* row = impl_->row(index);
385 for (int slot_id = 0; slot_id < impl_->max_threads(); slot_id++) {
386 if (pid == 0 || *impl_->thread_pid(slot_id) == pid)
387 rv += row[slot_id];
388 }
389 return rv;
390 }
391
392 int StatsTable::GetCounterValue(const std::string& name) {
393 return GetCounterValue(name, 0);
394 }
395
396 int StatsTable::GetCounterValue(const std::string& name, int pid) {
397 if (!impl_)
398 return 0;
399
400 int row = FindCounter(name);
401 if (!row)
402 return 0;
403 return GetRowValue(row, pid);
404 }
405
406 int StatsTable::GetMaxCounters() const {
407 if (!impl_)
408 return 0;
409 return impl_->max_counters();
410 }
411
412 int StatsTable::GetMaxThreads() const {
413 if (!impl_)
414 return 0;
415 return impl_->max_threads();
416 }
417
418 int* StatsTable::FindLocation(const char* name) {
419 // Get the static StatsTable
420 StatsTable *table = StatsTable::current();
421 if (!table)
422 return NULL;
423
424 // Get the slot for this thread. Try to register
425 // it if none exists.
426 int slot = table->GetSlot();
427 if (!slot && !(slot = table->RegisterThread("")))
428 return NULL;
429
430 // Find the counter id for the counter.
431 std::string str_name(name);
432 int counter = table->FindCounter(str_name);
433
434 // Now we can find the location in the table.
435 return table->GetLocation(counter, slot);
323 } 436 }
324 437
325 void StatsTable::UnregisterThread() { 438 void StatsTable::UnregisterThread() {
326 UnregisterThread(GetTLSData()); 439 UnregisterThread(GetTLSData());
327 } 440 }
328 441
329 void StatsTable::UnregisterThread(TLSData* data) { 442 void StatsTable::UnregisterThread(TLSData* data) {
330 if (!data) 443 if (!data)
331 return; 444 return;
332 DCHECK(impl_); 445 DCHECK(impl_);
(...skipping 11 matching lines...) Expand all
344 // This is called by the TLS destructor, which on some platforms has 457 // This is called by the TLS destructor, which on some platforms has
345 // already cleared the TLS info, so use the tls_data argument 458 // already cleared the TLS info, so use the tls_data argument
346 // rather than trying to fetch it ourselves. 459 // rather than trying to fetch it ourselves.
347 TLSData* tls_data = static_cast<TLSData*>(data); 460 TLSData* tls_data = static_cast<TLSData*>(data);
348 if (tls_data) { 461 if (tls_data) {
349 DCHECK(tls_data->table); 462 DCHECK(tls_data->table);
350 tls_data->table->UnregisterThread(tls_data); 463 tls_data->table->UnregisterThread(tls_data);
351 } 464 }
352 } 465 }
353 466
354 int StatsTable::CountThreadsRegistered() const {
355 if (!impl_)
356 return 0;
357
358 // Loop through the shared memory and count the threads that are active.
359 // We intentionally do not lock the table during the operation.
360 int count = 0;
361 for (int index = 1; index <= impl_->max_threads(); index++) {
362 char* name = impl_->thread_name(index);
363 if (*name != '\0')
364 count++;
365 }
366 return count;
367 }
368
369 int StatsTable::GetSlot() const {
370 TLSData* data = GetTLSData();
371 if (!data)
372 return 0;
373 return data->slot;
374 }
375
376 int StatsTable::FindEmptyThread() const { 467 int StatsTable::FindEmptyThread() const {
377 // Note: the API returns slots numbered from 1..N, although 468 // Note: the API returns slots numbered from 1..N, although
378 // internally, the array is 0..N-1. This is so that we can return 469 // internally, the array is 0..N-1. This is so that we can return
379 // zero as "not found". 470 // zero as "not found".
380 // 471 //
381 // The reason for doing this is because the thread 'slot' is stored 472 // The reason for doing this is because the thread 'slot' is stored
382 // in TLS, which is always initialized to zero, not -1. If 0 were 473 // in TLS, which is always initialized to zero, not -1. If 0 were
383 // returned as a valid slot number, it would be confused with the 474 // returned as a valid slot number, it would be confused with the
384 // uninitialized state. 475 // uninitialized state.
385 if (!impl_) 476 if (!impl_)
(...skipping 25 matching lines...) Expand all
411 for (int index = 1; index <= impl_->max_counters(); index++) { 502 for (int index = 1; index <= impl_->max_counters(); index++) {
412 char* row_name = impl_->counter_name(index); 503 char* row_name = impl_->counter_name(index);
413 if (!*row_name && !free_slot) 504 if (!*row_name && !free_slot)
414 free_slot = index; // save that we found a free slot 505 free_slot = index; // save that we found a free slot
415 else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength)) 506 else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength))
416 return index; 507 return index;
417 } 508 }
418 return free_slot; 509 return free_slot;
419 } 510 }
420 511
421 int StatsTable::FindCounter(const std::string& name) {
422 // Note: the API returns counters numbered from 1..N, although
423 // internally, the array is 0..N-1. This is so that we can return
424 // zero as "not found".
425 if (!impl_)
426 return 0;
427
428 // Create a scope for our auto-lock.
429 {
430 AutoLock scoped_lock(counters_lock_);
431
432 // Attempt to find the counter.
433 CountersMap::const_iterator iter;
434 iter = counters_.find(name);
435 if (iter != counters_.end())
436 return iter->second;
437 }
438
439 // Counter does not exist, so add it.
440 return AddCounter(name);
441 }
442
443 int StatsTable::AddCounter(const std::string& name) { 512 int StatsTable::AddCounter(const std::string& name) {
444 if (!impl_) 513 if (!impl_)
445 return 0; 514 return 0;
446 515
447 int counter_id = 0; 516 int counter_id = 0;
448 { 517 {
449 // To add a counter to the shared memory, we need the 518 // To add a counter to the shared memory, we need the
450 // shared memory lock. 519 // shared memory lock.
451 SharedMemoryAutoLock lock(impl_->shared_memory()); 520 SharedMemoryAutoLock lock(impl_->shared_memory());
452 521
(...skipping 10 matching lines...) Expand all
463 } 532 }
464 533
465 // now add to our in-memory cache 534 // now add to our in-memory cache
466 { 535 {
467 AutoLock lock(counters_lock_); 536 AutoLock lock(counters_lock_);
468 counters_[name] = counter_id; 537 counters_[name] = counter_id;
469 } 538 }
470 return counter_id; 539 return counter_id;
471 } 540 }
472 541
473 int* StatsTable::GetLocation(int counter_id, int slot_id) const { 542 StatsTable::TLSData* StatsTable::GetTLSData() const {
474 if (!impl_) 543 TLSData* data =
475 return NULL; 544 static_cast<TLSData*>(tls_index_.Get());
476 if (slot_id > impl_->max_threads()) 545 if (!data)
477 return NULL; 546 return NULL;
478 547
479 int* row = impl_->row(counter_id); 548 DCHECK(data->slot);
480 return &(row[slot_id-1]); 549 DCHECK_EQ(data->table, this);
481 } 550 return data;
482
483 const char* StatsTable::GetRowName(int index) const {
484 if (!impl_)
485 return NULL;
486
487 return impl_->counter_name(index);
488 }
489
490 int StatsTable::GetRowValue(int index, int pid) const {
491 if (!impl_)
492 return 0;
493
494 int rv = 0;
495 int* row = impl_->row(index);
496 for (int slot_id = 0; slot_id < impl_->max_threads(); slot_id++) {
497 if (pid == 0 || *impl_->thread_pid(slot_id) == pid)
498 rv += row[slot_id];
499 }
500 return rv;
501 }
502
503 int StatsTable::GetRowValue(int index) const {
504 return GetRowValue(index, 0);
505 }
506
507 int StatsTable::GetCounterValue(const std::string& name, int pid) {
508 if (!impl_)
509 return 0;
510
511 int row = FindCounter(name);
512 if (!row)
513 return 0;
514 return GetRowValue(row, pid);
515 }
516
517 int StatsTable::GetCounterValue(const std::string& name) {
518 return GetCounterValue(name, 0);
519 }
520
521 int StatsTable::GetMaxCounters() const {
522 if (!impl_)
523 return 0;
524 return impl_->max_counters();
525 }
526
527 int StatsTable::GetMaxThreads() const {
528 if (!impl_)
529 return 0;
530 return impl_->max_threads();
531 }
532
533 int* StatsTable::FindLocation(const char* name) {
534 // Get the static StatsTable
535 StatsTable *table = StatsTable::current();
536 if (!table)
537 return NULL;
538
539 // Get the slot for this thread. Try to register
540 // it if none exists.
541 int slot = table->GetSlot();
542 if (!slot && !(slot = table->RegisterThread("")))
543 return NULL;
544
545 // Find the counter id for the counter.
546 std::string str_name(name);
547 int counter = table->FindCounter(str_name);
548
549 // Now we can find the location in the table.
550 return table->GetLocation(counter, slot);
551 } 551 }
552 552
553 } // namespace base 553 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/field_trial.cc ('k') | base/ref_counted.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698