| OLD | NEW |
| 1 // Copyright (c) 2010 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 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 static const int kMaxCounterNameLength = 32; | 125 static const int kMaxCounterNameLength = 32; |
| 126 | 126 |
| 127 // Convenience function to lookup a counter location for a | 127 // Convenience function to lookup a counter location for a |
| 128 // counter by name for the calling thread. Will register | 128 // counter by name for the calling thread. Will register |
| 129 // the thread if it is not already registered. | 129 // the thread if it is not already registered. |
| 130 static int* FindLocation(const char *name); | 130 static int* FindLocation(const char *name); |
| 131 | 131 |
| 132 private: | 132 private: |
| 133 class Private; | 133 class Private; |
| 134 struct TLSData; | 134 struct TLSData; |
| 135 typedef hash_map<std::string, int> CountersMap; |
| 135 | 136 |
| 136 // Returns the space occupied by a thread in the table. Generally used | 137 // Returns the space occupied by a thread in the table. Generally used |
| 137 // if a thread terminates but the process continues. This function | 138 // if a thread terminates but the process continues. This function |
| 138 // does not zero out the thread's counters. | 139 // does not zero out the thread's counters. |
| 139 // Cannot be used inside a posix tls destructor. | 140 // Cannot be used inside a posix tls destructor. |
| 140 void UnregisterThread(); | 141 void UnregisterThread(); |
| 141 | 142 |
| 142 // This variant expects the tls data to be passed in, so it is safe to | 143 // This variant expects the tls data to be passed in, so it is safe to |
| 143 // call from inside a posix tls destructor (see doc for pthread_key_create). | 144 // call from inside a posix tls destructor (see doc for pthread_key_create). |
| 144 void UnregisterThread(TLSData* tls_data); | 145 void UnregisterThread(TLSData* tls_data); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 164 // to kMaxCounterNameLength-1 characters. | 165 // to kMaxCounterNameLength-1 characters. |
| 165 // | 166 // |
| 166 // On success, returns the counter_id for the newly added counter. | 167 // On success, returns the counter_id for the newly added counter. |
| 167 // On failure, returns 0. | 168 // On failure, returns 0. |
| 168 int AddCounter(const std::string& name); | 169 int AddCounter(const std::string& name); |
| 169 | 170 |
| 170 // 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 |
| 171 // initialized. | 172 // initialized. |
| 172 TLSData* GetTLSData() const; | 173 TLSData* GetTLSData() const; |
| 173 | 174 |
| 174 typedef hash_map<std::string, int> CountersMap; | |
| 175 | |
| 176 Private* impl_; | 175 Private* impl_; |
| 177 | 176 |
| 178 // The counters_lock_ protects the counters_ hash table. | 177 // The counters_lock_ protects the counters_ hash table. |
| 179 Lock counters_lock_; | 178 Lock counters_lock_; |
| 180 | 179 |
| 181 // 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. |
| 182 // 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 |
| 183 // 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 |
| 184 // 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 |
| 185 // have created it. | 184 // have created it. |
| 186 CountersMap counters_; | 185 CountersMap counters_; |
| 187 ThreadLocalStorage::Slot tls_index_; | 186 ThreadLocalStorage::Slot tls_index_; |
| 188 | 187 |
| 189 static StatsTable* global_table_; | 188 static StatsTable* global_table_; |
| 190 | 189 |
| 191 DISALLOW_COPY_AND_ASSIGN(StatsTable); | 190 DISALLOW_COPY_AND_ASSIGN(StatsTable); |
| 192 }; | 191 }; |
| 193 | 192 |
| 194 } // namespace base | 193 } // namespace base |
| 195 | 194 |
| 196 #endif // BASE_METRICS_STATS_TABLE_H_ | 195 #endif // BASE_METRICS_STATS_TABLE_H_ |
| OLD | NEW |