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 |
(...skipping 10 matching lines...) Expand all Loading... |
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/memory/shared_memory.h" |
29 #include "base/synchronization/lock.h" | 29 #include "base/synchronization/lock.h" |
30 #include "base/threading/thread_local_storage.h" | 30 #include "base/threading/thread_local_storage.h" |
| 31 #include "build/build_config.h" |
| 32 |
| 33 #if defined(OS_POSIX) |
| 34 #include "base/file_descriptor_posix.h" |
| 35 #endif |
31 | 36 |
32 namespace base { | 37 namespace base { |
33 | 38 |
34 class BASE_EXPORT StatsTable { | 39 class BASE_EXPORT StatsTable { |
35 public: | 40 public: |
| 41 // Identifies a StatsTable. We often want to share these between processes. |
| 42 // |
| 43 // On Windows, we use a named shared memory segment so the table identifier |
| 44 // should be a relatively unique string identifying the table to use. An |
| 45 // empty string can be used to use an anonymous shared memory segment for |
| 46 // cases where the table does not need to be shared between processes. |
| 47 // |
| 48 // Posix does not support named memory so we explicitly share file |
| 49 // descriptors. On Posix, pass a default-constructed file descriptor if a |
| 50 // handle doesn't already exist, and a new one will be created. |
| 51 // |
| 52 // If a table doesn't already exist with the given identifier, a new one will |
| 53 // be created with zeroed counters. |
| 54 #if defined(OS_POSIX) |
| 55 typedef FileDescriptor TableIdentifier; |
| 56 #elif defined(OS_WIN) |
| 57 typedef std::string TableIdentifier; |
| 58 #endif |
| 59 |
36 // Create a new StatsTable. | 60 // Create a new StatsTable. |
37 // If a StatsTable already exists with the specified name, this StatsTable | |
38 // will use the same shared memory segment as the original. Otherwise, | |
39 // a new StatsTable is created and all counters are zeroed. | |
40 // | |
41 // name is the name of the StatsTable to use. | |
42 // | 61 // |
43 // max_threads is the maximum number of threads the table will support. | 62 // max_threads is the maximum number of threads the table will support. |
44 // If the StatsTable already exists, this number is ignored. | 63 // If the StatsTable already exists, this number is ignored. |
45 // | 64 // |
46 // max_counters is the maximum number of counters the table will support. | 65 // max_counters is the maximum number of counters the table will support. |
47 // If the StatsTable already exists, this number is ignored. | 66 // If the StatsTable already exists, this number is ignored. |
48 StatsTable(const std::string& name, int max_threads, int max_counters); | 67 StatsTable(const TableIdentifier& table, |
| 68 int max_threads, |
| 69 int max_counters); |
49 | 70 |
50 // Destroys the StatsTable. When the last StatsTable is destroyed | 71 // Destroys the StatsTable. When the last StatsTable is destroyed |
51 // (across all processes), the StatsTable is removed from disk. | 72 // (across all processes), the StatsTable is removed from disk. |
52 ~StatsTable(); | 73 ~StatsTable(); |
53 | 74 |
54 // For convenience, we create a static table. This is generally | 75 // For convenience, we create a static table. This is generally |
55 // used automatically by the counters. | 76 // used automatically by the counters. |
56 static StatsTable* current(); | 77 static StatsTable* current(); |
57 | 78 |
58 // Set the global table for use in this process. | 79 // Set the global table for use in this process. |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // have created it. | 211 // have created it. |
191 CountersMap counters_; | 212 CountersMap counters_; |
192 ThreadLocalStorage::Slot tls_index_; | 213 ThreadLocalStorage::Slot tls_index_; |
193 | 214 |
194 DISALLOW_COPY_AND_ASSIGN(StatsTable); | 215 DISALLOW_COPY_AND_ASSIGN(StatsTable); |
195 }; | 216 }; |
196 | 217 |
197 } // namespace base | 218 } // namespace base |
198 | 219 |
199 #endif // BASE_METRICS_STATS_TABLE_H_ | 220 #endif // BASE_METRICS_STATS_TABLE_H_ |
OLD | NEW |