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 3 // found in the LICENSE file. 4 // 5 // A StatsTable is a table of statistics. It can be used across multiple 6 // processes and threads, maintaining cheap statistics counters without 7 // locking. 8 // 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 11 // to track the counters, and also to allow a single "view" to display 12 // the contents of all counters. 13 // 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 16 // which governs the maximum number of counters and concurrent 17 // threads/processes which can use it. 18 // 19 20 #ifndef BASE_METRICS_STATS_TABLE_H_ 21 #define BASE_METRICS_STATS_TABLE_H_ 22 23 #include <string> 24 25 #include "base/base_export.h" 26 #include "base/basictypes.h" 27 #include "base/containers/hash_tables.h" 28 #include "base/synchronization/lock.h" 29 #include "base/threading/thread_local_storage.h" 30 31 namespace base { 32 33 class BASE_EXPORT StatsTable { 34 public: 35 // Create a new 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, 38 // a new StatsTable is created and all counters are zeroed. 39 // 40 // name is the name of the StatsTable to use. 41 // 42 // max_threads is the maximum number of threads the table will support. 43 // If the StatsTable already exists, this number is ignored. 44 // 45 // max_counters is the maximum number of counters the table will support. 46 // If the StatsTable already exists, this number is ignored. 47 StatsTable(const std::string& name, int max_threads, int max_counters); 48 49 // Destroys the StatsTable. When the last StatsTable is destroyed 50 // (across all processes), the StatsTable is removed from disk. 51 ~StatsTable(); 52 53 // For convenience, we create a static table. This is generally 54 // used automatically by the counters. 55 static StatsTable* current(); 56 57 // Set the global table for use in this process. 58 static void set_current(StatsTable* value); 59 60 // Get the slot id for the calling thread. Returns 0 if no 61 // slot is assigned. 62 int GetSlot() const; 63 64 // All threads that contribute data to the table must register with the 65 // table first. This function will set thread local storage for the 66 // thread containing the location in the table where this thread will 67 // write its counter data. 68 // 69 // name is just a debugging tag to label the thread, and it does not 70 // need to be unique. It will be truncated to kMaxThreadNameLength-1 71 // characters. 72 // 73 // On success, returns the slot id for this thread. On failure, 74 // returns 0. 75 int RegisterThread(const std::string& name); 76 77 // Returns the number of threads currently registered. This is really not 78 // useful except for diagnostics and debugging. 79 int CountThreadsRegistered() const; 80 81 // Find a counter in the StatsTable. 82 // 83 // Returns an id for the counter which can be used to call GetLocation(). 84 // If the counter does not exist, attempts to create a row for the new 85 // counter. If there is no space in the table for the new counter, 86 // returns 0. 87 int FindCounter(const std::string& name); 88 89 // TODO(mbelshe): implement RemoveCounter. 90 91 // Gets the location of a particular value in the table based on 92 // the counter id and slot id. 93 int* GetLocation(int counter_id, int slot_id) const; 94 95 // Gets the counter name at a particular row. If the row is empty, 96 // returns NULL. 97 const char* GetRowName(int index) const; 98 99 // Gets the sum of the values for a particular row. 100 int GetRowValue(int index) const; 101 102 // Gets the sum of the values for a particular row for a given pid. 103 int GetRowValue(int index, int pid) const; 104 105 // Gets the sum of the values for a particular counter. If the counter 106 // does not exist, creates the counter. 107 int GetCounterValue(const std::string& name); 108 109 // 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 int GetCounterValue(const std::string& name, int pid); 112 113 // The maxinum number of counters/rows in the table. 114 int GetMaxCounters() const; 115 116 // The maxinum number of threads/columns in the table. 117 int GetMaxThreads() const; 118 119 // The maximum length (in characters) of a Thread's name including 120 // null terminator, as stored in the shared memory. 121 static const int kMaxThreadNameLength = 32; 122 123 // The maximum length (in characters) of a Counter's name including 124 // null terminator, as stored in the shared memory. 125 static const int kMaxCounterNameLength = 64; 126 127 // Convenience function to lookup a counter location for a 128 // counter by name for the calling thread. Will register 129 // the thread if it is not already registered. 130 static int* FindLocation(const char *name); 131 132 private: 133 class Private; 134 struct TLSData; 135 typedef hash_map<std::string, int> CountersMap; 136 137 // Returns the space occupied by a thread in the table. Generally used 138 // if a thread terminates but the process continues. This function 139 // does not zero out the thread's counters. 140 // Cannot be used inside a posix tls destructor. 141 void UnregisterThread(); 142 143 // This variant expects the tls data to be passed in, so it is safe to 144 // call from inside a posix tls destructor (see doc for pthread_key_create). 145 void UnregisterThread(TLSData* tls_data); 146 147 // The SlotReturnFunction is called at thread exit for each thread 148 // which used the StatsTable. 149 static void SlotReturnFunction(void* data); 150 151 // Locates a free slot in the table. Returns a number > 0 on success, 152 // or 0 on failure. The caller must hold the shared_memory lock when 153 // calling this function. 154 int FindEmptyThread() const; 155 156 // Locates a counter in the table or finds an empty row. Returns a 157 // number > 0 on success, or 0 on failure. The caller must hold the 158 // shared_memory_lock when calling this function. 159 int FindCounterOrEmptyRow(const std::string& name) const; 160 161 // Internal function to add a counter to the StatsTable. Assumes that 162 // the counter does not already exist in the table. 163 // 164 // name is a unique identifier for this counter, and will be truncated 165 // to kMaxCounterNameLength-1 characters. 166 // 167 // On success, returns the counter_id for the newly added counter. 168 // On failure, returns 0. 169 int AddCounter(const std::string& name); 170 171 // Get the TLS data for the calling thread. Returns NULL if none is 172 // initialized. 173 TLSData* GetTLSData() const; 174 175 Private* impl_; 176 177 // The counters_lock_ protects the counters_ hash table. 178 base::Lock counters_lock_; 179 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 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 184 // have created it. 185 CountersMap counters_; 186 ThreadLocalStorage::Slot tls_index_; 187 188 DISALLOW_COPY_AND_ASSIGN(StatsTable); 189 }; 190 191 } // namespace base 192 193 #endif // BASE_METRICS_STATS_TABLE_H_ 194