| 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 // A StatsTable is a table of statistics. It can be used across multiple | 5 // A StatsTable is a table of statistics. It can be used across multiple |
| 6 // processes and threads, maintaining cheap statistics counters without | 6 // processes and threads, maintaining cheap statistics counters without |
| 7 // locking. | 7 // locking. |
| 8 // | 8 // |
| 9 // The goal is to make it very cheap and easy for developers to add | 9 // The goal is to make it very cheap and easy for developers to add |
| 10 // counters to code, without having to build one-off utilities or mechanisms | 10 // counters to code, without having to build one-off utilities or mechanisms |
| 11 // to track the counters, and also to allow a single "view" to display | 11 // to track the counters, and also to allow a single "view" to display |
| 12 // the contents of all counters. | 12 // the contents of all counters. |
| 13 // | 13 // |
| 14 // To achieve this, StatsTable creates a shared memory segment to store | 14 // To achieve this, StatsTable creates a shared memory segment to store |
| 15 // the data for the counters. Upon creation, it has a specific size | 15 // the data for the counters. Upon creation, it has a specific size |
| 16 // which governs the maximum number of counters and concurrent | 16 // which governs the maximum number of counters and concurrent |
| 17 // threads/processes which can use it. | 17 // threads/processes which can use it. |
| 18 // | 18 // |
| 19 | 19 |
| 20 #ifndef BASE_METRICS_STATS_TABLE_H_ | 20 #ifndef BASE_METRICS_STATS_TABLE_H_ |
| 21 #define BASE_METRICS_STATS_TABLE_H_ | 21 #define BASE_METRICS_STATS_TABLE_H_ |
| 22 #pragma once | 22 #pragma once |
| 23 | 23 |
| 24 #include <string> | 24 #include <string> |
| 25 | 25 |
| 26 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
| 27 #include "base/hash_tables.h" | 27 #include "base/hash_tables.h" |
| 28 #include "base/lock.h" | 28 #include "base/synchronization/lock.h" |
| 29 #include "base/threading/thread_local_storage.h" | 29 #include "base/threading/thread_local_storage.h" |
| 30 | 30 |
| 31 namespace base { | 31 namespace base { |
| 32 | 32 |
| 33 class StatsTable { | 33 class StatsTable { |
| 34 public: | 34 public: |
| 35 // Create a new StatsTable. | 35 // Create a new StatsTable. |
| 36 // If a StatsTable already exists with the specified name, this StatsTable | 36 // If a StatsTable already exists with the specified name, this StatsTable |
| 37 // will use the same shared memory segment as the original. Otherwise, | 37 // will use the same shared memory segment as the original. Otherwise, |
| 38 // a new StatsTable is created and all counters are zeroed. | 38 // a new StatsTable is created and all counters are zeroed. |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 // On failure, returns 0. | 168 // On failure, returns 0. |
| 169 int AddCounter(const std::string& name); | 169 int AddCounter(const std::string& name); |
| 170 | 170 |
| 171 // Get the TLS data for the calling thread. Returns NULL if none is | 171 // Get the TLS data for the calling thread. Returns NULL if none is |
| 172 // initialized. | 172 // initialized. |
| 173 TLSData* GetTLSData() const; | 173 TLSData* GetTLSData() const; |
| 174 | 174 |
| 175 Private* impl_; | 175 Private* impl_; |
| 176 | 176 |
| 177 // The counters_lock_ protects the counters_ hash table. | 177 // The counters_lock_ protects the counters_ hash table. |
| 178 Lock counters_lock_; | 178 base::Lock counters_lock_; |
| 179 | 179 |
| 180 // The counters_ hash map is an in-memory hash of the counters. | 180 // The counters_ hash map is an in-memory hash of the counters. |
| 181 // It is used for quick lookup of counters, but is cannot be used | 181 // It is used for quick lookup of counters, but is cannot be used |
| 182 // as a substitute for what is in the shared memory. Even though | 182 // as a substitute for what is in the shared memory. Even though |
| 183 // we don't have a counter in our hash table, another process may | 183 // we don't have a counter in our hash table, another process may |
| 184 // have created it. | 184 // have created it. |
| 185 CountersMap counters_; | 185 CountersMap counters_; |
| 186 ThreadLocalStorage::Slot tls_index_; | 186 ThreadLocalStorage::Slot tls_index_; |
| 187 | 187 |
| 188 static StatsTable* global_table_; | 188 static StatsTable* global_table_; |
| 189 | 189 |
| 190 DISALLOW_COPY_AND_ASSIGN(StatsTable); | 190 DISALLOW_COPY_AND_ASSIGN(StatsTable); |
| 191 }; | 191 }; |
| 192 | 192 |
| 193 } // namespace base | 193 } // namespace base |
| 194 | 194 |
| 195 #endif // BASE_METRICS_STATS_TABLE_H_ | 195 #endif // BASE_METRICS_STATS_TABLE_H_ |
| OLD | NEW |