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 // 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 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 // write its counter data. | 69 // write its counter data. |
70 // | 70 // |
71 // name is just a debugging tag to label the thread, and it does not | 71 // name is just a debugging tag to label the thread, and it does not |
72 // need to be unique. It will be truncated to kMaxThreadNameLength-1 | 72 // need to be unique. It will be truncated to kMaxThreadNameLength-1 |
73 // characters. | 73 // characters. |
74 // | 74 // |
75 // On success, returns the slot id for this thread. On failure, | 75 // On success, returns the slot id for this thread. On failure, |
76 // returns 0. | 76 // returns 0. |
77 int RegisterThread(const std::wstring& name); | 77 int RegisterThread(const std::wstring& name); |
78 | 78 |
79 // Returns the space occupied by a thread in the table. Generally used | |
80 // if a thread terminates but the process continues. This function | |
81 // does not zero out the thread's counters. | |
82 void UnregisterThread(); | |
83 | |
84 // Returns the number of threads currently registered. This is really not | 79 // Returns the number of threads currently registered. This is really not |
85 // useful except for diagnostics and debugging. | 80 // useful except for diagnostics and debugging. |
86 int CountThreadsRegistered() const; | 81 int CountThreadsRegistered() const; |
87 | 82 |
88 // Find a counter in the StatsTable. | 83 // Find a counter in the StatsTable. |
89 // | 84 // |
90 // Returns an id for the counter which can be used to call GetLocation(). | 85 // Returns an id for the counter which can be used to call GetLocation(). |
91 // If the counter does not exist, attempts to create a row for the new | 86 // If the counter does not exist, attempts to create a row for the new |
92 // counter. If there is no space in the table for the new counter, | 87 // counter. If there is no space in the table for the new counter, |
93 // returns 0. | 88 // returns 0. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 // The maximum length (in characters) of a Counter's name including | 125 // The maximum length (in characters) of a Counter's name including |
131 // null terminator, as stored in the shared memory. | 126 // null terminator, as stored in the shared memory. |
132 static const int kMaxCounterNameLength = 32; | 127 static const int kMaxCounterNameLength = 32; |
133 | 128 |
134 // Convenience function to lookup a counter location for a | 129 // Convenience function to lookup a counter location for a |
135 // counter by name for the calling thread. Will register | 130 // counter by name for the calling thread. Will register |
136 // the thread if it is not already registered. | 131 // the thread if it is not already registered. |
137 static int* FindLocation(const wchar_t *name); | 132 static int* FindLocation(const wchar_t *name); |
138 | 133 |
139 private: | 134 private: |
135 // Returns the space occupied by a thread in the table. Generally used | |
136 // if a thread terminates but the process continues. This function | |
137 // does not zero out the thread's counters. | |
138 void UnregisterThread(); | |
139 | |
140 // This variant expects the tls data to be passed in, so it is safe to | |
141 // call from inside a posix tls destructor (see doc for pthread_key_create). | |
142 void UnregisterThread(StatsTableTLSData *tls_data); | |
Evan Martin
2008/10/31 19:17:13
nit: star on the left.
| |
143 | |
144 // The SlotReturnFunction is called at thread exit for each thread | |
145 // which used the StatsTable. | |
146 static void SlotReturnFunction(void* data); | |
147 | |
140 // Locates a free slot in the table. Returns a number > 0 on success, | 148 // Locates a free slot in the table. Returns a number > 0 on success, |
141 // or 0 on failure. The caller must hold the shared_memory lock when | 149 // or 0 on failure. The caller must hold the shared_memory lock when |
142 // calling this function. | 150 // calling this function. |
143 int FindEmptyThread() const; | 151 int FindEmptyThread() const; |
144 | 152 |
145 // Locates a counter in the table or finds an empty row. Returns a | 153 // Locates a counter in the table or finds an empty row. Returns a |
146 // number > 0 on success, or 0 on failure. The caller must hold the | 154 // number > 0 on success, or 0 on failure. The caller must hold the |
147 // shared_memory_lock when calling this function. | 155 // shared_memory_lock when calling this function. |
148 int FindCounterOrEmptyRow(const std::wstring& name) const; | 156 int FindCounterOrEmptyRow(const std::wstring& name) const; |
149 | 157 |
(...skipping 23 matching lines...) Expand all Loading... | |
173 // we don't have a counter in our hash table, another process may | 181 // we don't have a counter in our hash table, another process may |
174 // have created it. | 182 // have created it. |
175 CountersMap counters_; | 183 CountersMap counters_; |
176 TLSSlot tls_index_; | 184 TLSSlot tls_index_; |
177 | 185 |
178 static StatsTable* global_table_; | 186 static StatsTable* global_table_; |
179 DISALLOW_EVIL_CONSTRUCTORS(StatsTable); | 187 DISALLOW_EVIL_CONSTRUCTORS(StatsTable); |
180 }; | 188 }; |
181 | 189 |
182 #endif // BASE_STATS_TABLE_H__ | 190 #endif // BASE_STATS_TABLE_H__ |
OLD | NEW |