| 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 | 22 |
| 23 #include <string> | 23 #include <string> |
| 24 | 24 |
| 25 #include "base/base_export.h" | 25 #include "base/base_export.h" |
| 26 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
| 27 #include "base/containers/hash_tables.h" | 27 #include "base/containers/hash_tables.h" |
| 28 #include "base/memory/shared_memory.h" |
| 28 #include "base/synchronization/lock.h" | 29 #include "base/synchronization/lock.h" |
| 29 #include "base/threading/thread_local_storage.h" | 30 #include "base/threading/thread_local_storage.h" |
| 30 | 31 |
| 31 namespace base { | 32 namespace base { |
| 32 | 33 |
| 33 class BASE_EXPORT StatsTable { | 34 class BASE_EXPORT StatsTable { |
| 34 public: | 35 public: |
| 35 // Create a new StatsTable. | 36 // Create a new StatsTable. |
| 36 // If a StatsTable already exists with the specified name, this StatsTable | 37 // If a StatsTable already exists with the specified name, this StatsTable |
| 37 // will use the same shared memory segment as the original. Otherwise, | 38 // will use the same shared memory segment as the original. Otherwise, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // Gets the sum of the values for a particular counter for a given pid. | 110 // Gets the sum of the values for a particular counter for a given pid. |
| 110 // If the counter does not exist, creates the counter. | 111 // If the counter does not exist, creates the counter. |
| 111 int GetCounterValue(const std::string& name, int pid); | 112 int GetCounterValue(const std::string& name, int pid); |
| 112 | 113 |
| 113 // The maxinum number of counters/rows in the table. | 114 // The maxinum number of counters/rows in the table. |
| 114 int GetMaxCounters() const; | 115 int GetMaxCounters() const; |
| 115 | 116 |
| 116 // The maxinum number of threads/columns in the table. | 117 // The maxinum number of threads/columns in the table. |
| 117 int GetMaxThreads() const; | 118 int GetMaxThreads() const; |
| 118 | 119 |
| 120 #if defined(OS_POSIX) |
| 121 // Get the underlying shared memory handle for the table. |
| 122 base::SharedMemoryHandle GetSharedMemoryHandle() const; |
| 123 #endif |
| 124 |
| 119 // The maximum length (in characters) of a Thread's name including | 125 // The maximum length (in characters) of a Thread's name including |
| 120 // null terminator, as stored in the shared memory. | 126 // null terminator, as stored in the shared memory. |
| 121 static const int kMaxThreadNameLength = 32; | 127 static const int kMaxThreadNameLength = 32; |
| 122 | 128 |
| 123 // The maximum length (in characters) of a Counter's name including | 129 // The maximum length (in characters) of a Counter's name including |
| 124 // null terminator, as stored in the shared memory. | 130 // null terminator, as stored in the shared memory. |
| 125 static const int kMaxCounterNameLength = 64; | 131 static const int kMaxCounterNameLength = 64; |
| 126 | 132 |
| 127 // Convenience function to lookup a counter location for a | 133 // Convenience function to lookup a counter location for a |
| 128 // counter by name for the calling thread. Will register | 134 // counter by name for the calling thread. Will register |
| 129 // the thread if it is not already registered. | 135 // the thread if it is not already registered. |
| 130 static int* FindLocation(const char *name); | 136 static int* FindLocation(const char *name); |
| 131 | 137 |
| 132 private: | 138 private: |
| 133 class Private; | 139 class Internal; |
| 134 struct TLSData; | 140 struct TLSData; |
| 135 typedef hash_map<std::string, int> CountersMap; | 141 typedef hash_map<std::string, int> CountersMap; |
| 136 | 142 |
| 137 // Returns the space occupied by a thread in the table. Generally used | 143 // Returns the space occupied by a thread in the table. Generally used |
| 138 // if a thread terminates but the process continues. This function | 144 // if a thread terminates but the process continues. This function |
| 139 // does not zero out the thread's counters. | 145 // does not zero out the thread's counters. |
| 140 // Cannot be used inside a posix tls destructor. | 146 // Cannot be used inside a posix tls destructor. |
| 141 void UnregisterThread(); | 147 void UnregisterThread(); |
| 142 | 148 |
| 143 // This variant expects the tls data to be passed in, so it is safe to | 149 // This variant expects the tls data to be passed in, so it is safe to |
| (...skipping 21 matching lines...) Expand all Loading... |
| 165 // to kMaxCounterNameLength-1 characters. | 171 // to kMaxCounterNameLength-1 characters. |
| 166 // | 172 // |
| 167 // On success, returns the counter_id for the newly added counter. | 173 // On success, returns the counter_id for the newly added counter. |
| 168 // On failure, returns 0. | 174 // On failure, returns 0. |
| 169 int AddCounter(const std::string& name); | 175 int AddCounter(const std::string& name); |
| 170 | 176 |
| 171 // Get the TLS data for the calling thread. Returns NULL if none is | 177 // Get the TLS data for the calling thread. Returns NULL if none is |
| 172 // initialized. | 178 // initialized. |
| 173 TLSData* GetTLSData() const; | 179 TLSData* GetTLSData() const; |
| 174 | 180 |
| 175 Private* impl_; | 181 Internal* internal_; |
| 176 | 182 |
| 177 // The counters_lock_ protects the counters_ hash table. | 183 // The counters_lock_ protects the counters_ hash table. |
| 178 base::Lock counters_lock_; | 184 base::Lock counters_lock_; |
| 179 | 185 |
| 180 // The counters_ hash map is an in-memory hash of the counters. | 186 // 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 | 187 // 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 | 188 // 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 | 189 // we don't have a counter in our hash table, another process may |
| 184 // have created it. | 190 // have created it. |
| 185 CountersMap counters_; | 191 CountersMap counters_; |
| 186 ThreadLocalStorage::Slot tls_index_; | 192 ThreadLocalStorage::Slot tls_index_; |
| 187 | 193 |
| 188 DISALLOW_COPY_AND_ASSIGN(StatsTable); | 194 DISALLOW_COPY_AND_ASSIGN(StatsTable); |
| 189 }; | 195 }; |
| 190 | 196 |
| 191 } // namespace base | 197 } // namespace base |
| 192 | 198 |
| 193 #endif // BASE_METRICS_STATS_TABLE_H_ | 199 #endif // BASE_METRICS_STATS_TABLE_H_ |
| OLD | NEW |